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:
1 parent
201a7c7cfc
commit
c9d68781d0
5 files changed
+53
-8
No files matched your search
+3
-3
@@ -32,6 +32,9 @@ A regular save is different: it writes the game's `.anm2` XML, with the editor's
|
|||||||
"shaders": [
|
"shaders": [
|
||||||
{"name": "glow", "vertex": "glow.vs", "fragment": "glow.fs", "uniforms": [{"name": "strength", "value": "0.5"}]}
|
{"name": "glow", "vertex": "glow.vs", "fragment": "glow.fs", "uniforms": [{"name": "strength", "value": "0.5"}]}
|
||||||
],
|
],
|
||||||
|
"libraries": [
|
||||||
|
"shared/shadows.anm2"
|
||||||
|
],
|
||||||
"layers": [
|
"layers": [
|
||||||
{"name": "torso", "sheet": 1},
|
{"name": "torso", "sheet": 1},
|
||||||
{"name": "hx", "animation": "Head"},
|
{"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": [
|
||||||
"sounds/step.ogg"
|
"sounds/step.ogg"
|
||||||
],
|
|
||||||
"libraries": [
|
|
||||||
"shared/shadows.anm2"
|
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+32
-2
@@ -28,12 +28,14 @@ namespace anm2ed::cli
|
|||||||
commands:
|
commands:
|
||||||
info animations, items, spritesheets and Isaac compatibility, as text
|
info animations, items, spritesheets and Isaac compatibility, as text
|
||||||
frame one frame as a PNG (--out, --time)
|
frame one frame as a PNG (--out, --time)
|
||||||
|
convert the document as --format, to --out (default: the file itself)
|
||||||
render the animation as --type:
|
render the animation as --type:
|
||||||
sheet all frames in a grid PNG (default; --rows, --columns)
|
sheet all frames in a grid PNG (default; --rows, --columns)
|
||||||
pngs one PNG per frame in the --out folder (--format, default "{}.png")
|
pngs one PNG per frame in the --out folder (--format, default "{}.png")
|
||||||
gif, webm, mp4, ogv through FFmpeg (--ffmpeg, default: the editor's setting)
|
gif, webm, mp4, ogv through FFmpeg (--ffmpeg, default: the editor's setting)
|
||||||
|
|
||||||
options:
|
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)
|
--animation NAME|INDEX which animation (default: the default animation)
|
||||||
--out PATH where to write
|
--out PATH where to write
|
||||||
--time T frame time for "frame" (default 0)
|
--time T frame time for "frame" (default 0)
|
||||||
@@ -79,7 +81,8 @@ options:
|
|||||||
std::optional<Arguments> arguments_get(int argc, const char** argv)
|
std::optional<Arguments> arguments_get(int argc, const char** argv)
|
||||||
{
|
{
|
||||||
if (argc < 3) return {};
|
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)
|
for (int i = 3; i < argc; ++i)
|
||||||
{
|
{
|
||||||
std::string name = argv[i];
|
std::string name = argv[i];
|
||||||
@@ -287,7 +290,33 @@ options:
|
|||||||
return 0;
|
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)
|
bool is_cli(int argc, const char** argv)
|
||||||
{
|
{
|
||||||
@@ -303,6 +332,7 @@ options:
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
if (arguments->command == "info") return info_print(*arguments);
|
if (arguments->command == "info") return info_print(*arguments);
|
||||||
|
if (arguments->command == "convert") return convert_run(*arguments);
|
||||||
return draw_run(*arguments);
|
return draw_run(*arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+4
-1
@@ -13,6 +13,8 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <SDL3/SDL_video.h>
|
||||||
|
|
||||||
#include "file.hpp"
|
#include "file.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
#include "manager.hpp"
|
#include "manager.hpp"
|
||||||
@@ -320,7 +322,8 @@ namespace anm2ed
|
|||||||
return false;
|
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)
|
if (!shaderElement)
|
||||||
{
|
{
|
||||||
document::shader_status_format_append(status, LABEL_SHADER_PATH_EMPTY, LABEL_FRAGMENT);
|
document::shader_status_format_append(status, LABEL_SHADER_PATH_EMPTY, LABEL_FRAGMENT);
|
||||||
|
|||||||
+3
-2
@@ -675,7 +675,7 @@ namespace anm2ed::model
|
|||||||
false);
|
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)
|
template <class Io, class M> void model_io(Io& io, M& model)
|
||||||
{
|
{
|
||||||
auto& content = model.content;
|
auto& content = model.content;
|
||||||
@@ -734,6 +734,8 @@ namespace anm2ed::model
|
|||||||
true);
|
true);
|
||||||
},
|
},
|
||||||
false, false);
|
false, false);
|
||||||
|
// Libraries come before the layers that name them.
|
||||||
|
io.values("libraries", content.libraries, [](auto& library) -> auto& { return library.path; });
|
||||||
io.list(
|
io.list(
|
||||||
"layers", content.layers,
|
"layers", content.layers,
|
||||||
[&](auto& layerIo, auto& layer)
|
[&](auto& layerIo, auto& layer)
|
||||||
@@ -754,7 +756,6 @@ namespace anm2ed::model
|
|||||||
true, false);
|
true, false);
|
||||||
io.values("events", content.events, [](auto& event) -> auto& { return event.name; });
|
io.values("events", content.events, [](auto& event) -> auto& { return event.name; });
|
||||||
io.values("sounds", content.sounds, [](auto& sound) -> auto& { return sound.path; });
|
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, {});
|
io.field("defaultAnimation", model.animations.defaultAnimation, {});
|
||||||
if constexpr (Io::IS_READING)
|
if constexpr (Io::IS_READING)
|
||||||
animations_read(io, model);
|
animations_read(io, model);
|
||||||
|
|||||||
@@ -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, "{ \"fps\": 30 }", &issues));
|
||||||
CHECK_FALSE(model::model_load_string(model, "{ \"anm2ed\": 1, ", &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);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user