diff --git a/docs/json_format.md b/docs/json_format.md index 542771d..5638fb5 100644 --- a/docs/json_format.md +++ b/docs/json_format.md @@ -32,6 +32,9 @@ A regular save is different: it writes the game's `.anm2` XML, with the editor's "shaders": [ {"name": "glow", "vertex": "glow.vs", "fragment": "glow.fs", "uniforms": [{"name": "strength", "value": "0.5"}]} ], +"libraries": [ + "shared/shadows.anm2" +], "layers": [ {"name": "torso", "sheet": 1}, {"name": "hx", "animation": "Head"}, @@ -46,9 +49,6 @@ A regular save is different: it writes the game's `.anm2` XML, with the editor's ], "sounds": [ "sounds/step.ogg" -], -"libraries": [ - "shared/shadows.anm2" ] ``` diff --git a/src/cli.cpp b/src/cli.cpp index fa03af6..eb69edb 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -28,12 +28,14 @@ namespace anm2ed::cli commands: info animations, items, spritesheets and Isaac compatibility, as text frame one frame as a PNG (--out, --time) + convert the document as --format, to --out (default: the file itself) render the animation as --type: sheet all frames in a grid PNG (default; --rows, --columns) pngs one PNG per frame in the --out folder (--format, default "{}.png") gif, webm, mp4, ogv through FFmpeg (--ffmpeg, default: the editor's setting) options: + --format json|xml the document format for "convert" (default xml: the game's, with the editor's copy) --animation NAME|INDEX which animation (default: the default animation) --out PATH where to write --time T frame time for "frame" (default 0) @@ -79,7 +81,8 @@ options: std::optional arguments_get(int argc, const char** argv) { if (argc < 3) return {}; - Arguments arguments{.command = argv[1], .file = path::from_utf8(argv[2])}; + // Absolute, so a bare file name still has a folder for the paths in it. + Arguments arguments{.command = argv[1], .file = std::filesystem::absolute(path::from_utf8(argv[2]))}; for (int i = 3; i < argc; ++i) { std::string name = argv[i]; @@ -287,7 +290,33 @@ options: return 0; } - constexpr std::string_view COMMANDS[] = {"info", "frame", "render"}; + // Saved as the editor saves it: XML (the game's copy, with the editor's embedded) or the editor's JSON. + int convert_run(const Arguments& arguments) + { + auto format = arguments.get("format").value_or("xml"); + if (format != "xml" && format != "json") + { + std::println(stderr, "--format must be json or xml"); + return 2; + } + std::string error{}; + Document document(arguments.file, false, &error); + auto fileUtf8 = path::to_utf8(arguments.file); + if (!document.is_valid()) + { + std::println(stderr, "could not load {}: {}", fileUtf8, error); + return 1; + } + if (!error.empty()) std::println(stderr, "{}:\n{}", fileUtf8, error); + auto out = std::filesystem::absolute(arguments.get("out").transform(path::from_utf8).value_or(arguments.file)); + auto options = SAVE_OPTIONS_DEFAULT; + options.isJson = format == "json"; + if (document.save(out, &error, options)) return 0; + std::println(stderr, "could not write {}: {}", path::to_utf8(out), error); + return 1; + } + + constexpr std::string_view COMMANDS[] = {"info", "frame", "render", "convert"}; bool is_cli(int argc, const char** argv) { @@ -303,6 +332,7 @@ options: return 2; } if (arguments->command == "info") return info_print(*arguments); + if (arguments->command == "convert") return convert_run(*arguments); return draw_run(*arguments); } } diff --git a/src/document.cpp b/src/document.cpp index cfd7ad2..44f51a3 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -13,6 +13,8 @@ #include #include +#include + #include "file.hpp" #include "log.hpp" #include "manager.hpp" @@ -320,7 +322,8 @@ namespace anm2ed return false; }; - auto shaderElement = model::item_get(model.content.shaders, shaderId); + // Without an OpenGL context (as the command line converts), there is nothing to compile it with. + auto shaderElement = SDL_GL_GetCurrentContext() ? model::item_get(model.content.shaders, shaderId) : nullptr; if (!shaderElement) { document::shader_status_format_append(status, LABEL_SHADER_PATH_EMPTY, LABEL_FRAGMENT); diff --git a/src/model/json.cpp b/src/model/json.cpp index ba07949..ac57df2 100644 --- a/src/model/json.cpp +++ b/src/model/json.cpp @@ -675,7 +675,7 @@ namespace anm2ed::model false); } - // Content comes before animations, which read it. + // Content comes before animations, which read it (and anything named comes before what names it). template void model_io(Io& io, M& model) { auto& content = model.content; @@ -734,6 +734,8 @@ namespace anm2ed::model true); }, false, false); + // Libraries come before the layers that name them. + io.values("libraries", content.libraries, [](auto& library) -> auto& { return library.path; }); io.list( "layers", content.layers, [&](auto& layerIo, auto& layer) @@ -754,7 +756,6 @@ namespace anm2ed::model true, false); io.values("events", content.events, [](auto& event) -> auto& { return event.name; }); io.values("sounds", content.sounds, [](auto& sound) -> auto& { return sound.path; }); - io.values("libraries", content.libraries, [](auto& library) -> auto& { return library.path; }); io.field("defaultAnimation", model.animations.defaultAnimation, {}); if constexpr (Io::IS_READING) animations_read(io, model); diff --git a/tests/invariants.cpp b/tests/invariants.cpp index 183fbf0..eb5065a 100644 --- a/tests/invariants.cpp +++ b/tests/invariants.cpp @@ -101,3 +101,14 @@ TEST_CASE("JSON that can't be read is reported by where it is, and the rest stil CHECK_FALSE(model::model_load_string(model, "{ \"fps\": 30 }", &issues)); CHECK_FALSE(model::model_load_string(model, "{ \"anm2ed\": 1, ", &issues)); } + +TEST_CASE("JSON layers can name a library listed after them in the file") +{ + auto text = R"({ "anm2ed": 1, "layers": [ { "name": "shadow", "library": 0, "animation": "Small" } ], + "libraries": [ "shared/shadows.anm2" ] })"; + model::Model model{}; + std::string issues{}; + REQUIRE(model::model_load_string(model, text, &issues)); + CHECK(issues.empty()); + CHECK(model.content.layers[0].libraryId == 0); +}