Add a convert command; JSON libraries come before layers

`anm2ed convert <file> [--format json|xml] [--out PATH]` saves a document as
the editor does: XML (the default) is the game's copy with the editor's
embedded, animation layers flattened through their libraries; JSON is the
editor's own. Without --out the file is rewritten in place. JSON problems go
to stderr; a file that can't load or write exits 1, a bad --format 2. A
document loads without an OpenGL context now: its custom shaders just aren't
compiled. The file and --out are made absolute, so a bare file name has a
folder (render and frame crashed on one too).

JSON writes and reads "libraries" before "layers", so a layer's library index
is checked against the libraries rather than reported missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-28 22:33:15 -04:00
1 parent 201a7c7cfc
commit c9d68781d0
5 files changed
+53 -8

No files matched your search

+32 -2
View File
@@ -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> 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);
}
}
+4 -1
View File
@@ -13,6 +13,8 @@
#include <utility>
#include <vector>
#include <SDL3/SDL_video.h>
#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);
+3 -2
View File
@@ -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 <class Io, class M> 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);