Repair duplicate content ids on load; pass unknown attributes and text through

- Duplicate ids in any content list (spritesheets, shaders, layers, nulls, events,
  sounds, regions) are repaired at load: the first holder keeps the id, so tracks
  stay bound to the item they referenced (previously remapped to the wrong layer)
- Unknown attributes and element text survive save; retired BakeCount/OriginalDelay
  are still dropped. Private corpus (1,251 files) output unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-10-03 14:40:20 -04:00
1 parent 8953101245
commit 5311c2835f
28 files changed
+223 -175

No files matched your search

+1 -1
View File
@@ -62,7 +62,7 @@ namespace anm2ed
group_metadata_embed(anm2.root);
group_frames_restore(anm2.root);
groups_flatten(anm2.root);
shader_ids_repair(anm2.root);
content_ids_repair(anm2.root);
shader_frame_ids_repair(anm2.root);
region_frame_ids_repair(anm2.root);
anm2.region_frames_sync(true);
+2
View File
@@ -323,6 +323,8 @@ namespace anm2ed
Origin origin{Origin::CUSTOM};
float rotation{};
std::vector<int> soundIds{};
std::vector<std::pair<std::string, std::string>> extraAttributes{};
std::string text{};
glm::vec2 pivot{};
glm::vec2 crop{};
glm::vec2 position{};
+6
View File
@@ -54,6 +54,12 @@ namespace anm2ed
void attribute(const char* name, int value) override { formatted_append(name, value); }
void attribute(const char* name, bool value) override { formatted_append(name, value); }
void attribute(const char* name, float value) override { formatted_append(name, value); }
void text(const char* value) override
{
byte_append('T');
string_append(value);
}
void body() override { byte_append('['); }
void close() override { byte_append(']'); }
};
+2 -1
View File
@@ -43,6 +43,7 @@ namespace anm2ed
virtual void attribute(const char*, int) = 0;
virtual void attribute(const char*, bool) = 0;
virtual void attribute(const char*, float) = 0;
virtual void text(const char*) = 0;
virtual void body() {}
virtual void close() = 0;
};
@@ -68,7 +69,7 @@ namespace anm2ed
void groups_flatten(Element&);
void groups_nest(Element&);
void source_document_erase(Element&);
void shader_ids_repair(Element&);
void content_ids_repair(Element&);
void shader_frame_ids_repair(Element&);
void shader_ids_remap(Element&);
void region_ids_remap(Element&);
+29 -11
View File
@@ -15,25 +15,43 @@ namespace anm2ed
});
}
void shader_ids_repair(Element& root)
// Gives every child of a type a unique, non-negative id; the first holder of a duplicated id keeps it.
void child_ids_repair(Element& container, ElementType type)
{
auto shaders = content_container_get(root, ElementType::SHADERS);
if (!shaders) return;
std::set<int> usedIds{};
int nextId{};
for (auto& shader : shaders->children)
{
if (shader.type != ElementType::SHADER) continue;
for (const auto& child : container.children)
if (child.type == type && child.id >= 0) usedIds.insert(child.id);
std::set<int> keptIds{};
int nextId{};
for (auto& child : container.children)
{
if (child.type != type) continue;
if (child.id >= 0 && keptIds.insert(child.id).second) continue;
while (usedIds.contains(nextId))
++nextId;
if (shader.id < 0 || usedIds.contains(shader.id)) shader.id = nextId++;
usedIds.insert(shader.id);
if (shader.name.empty()) shader.name = SHADER_NAME_DEFAULT;
child.id = nextId;
usedIds.insert(nextId);
keptIds.insert(nextId);
}
}
void content_ids_repair(Element& root)
{
for (auto type : {ElementType::SPRITESHEET, ElementType::SHADER, ElementType::LAYER_ELEMENT,
ElementType::NULL_ELEMENT, ElementType::EVENT_ELEMENT, ElementType::SOUND_ELEMENT})
if (auto container = content_container_get(root, ELEMENT_CONTAINERS[(int)type]))
child_ids_repair(*container, type);
if (auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS))
for (auto& spritesheet : spritesheets->children)
child_ids_repair(spritesheet, ElementType::REGION);
if (auto shaders = content_container_get(root, ElementType::SHADERS))
for (auto& shader : shaders->children)
if (shader.type == ElementType::SHADER && shader.name.empty()) shader.name = SHADER_NAME_DEFAULT;
}
void shader_frame_ids_repair(Element& root)
{
auto shaders = content_container_get(root, ElementType::SHADERS);
+35 -14
View File
@@ -68,6 +68,17 @@ namespace anm2ed
constexpr std::string_view LEGACY_OVERLAY_TAG = "Overlay";
// Attributes read into Element members (or retired by older anm2ed builds) are not passed through.
#define X(attribute, member) attribute,
constexpr std::string_view KNOWN_ATTRIBUTES[] = {ANM2_STRING_ATTRIBUTES ANM2_PATH_ATTRIBUTES ANM2_INT_ATTRIBUTES
ANM2_BOOL_ATTRIBUTES ANM2_FLOAT_ATTRIBUTES ANM2_COLOR_ATTRIBUTES
"Interpolated",
"Origin",
"BakeInterpolation",
"BakeDelay",
"BakeCount",
"OriginalDelay"};
#undef X
bool string_query(const XMLElement* element, const char* name, std::string& out)
{
@@ -145,6 +156,11 @@ namespace anm2ed
ANM2_COLOR_ATTRIBUTES
#undef X
for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next())
if (!std::ranges::contains(KNOWN_ATTRIBUTES, std::string_view(attribute->Name())))
out.extraAttributes.emplace_back(attribute->Name(), attribute->Value());
if (auto text = element->GetText()) out.text = text;
out.interpolation = interpolation_read(element);
out.origin = origin_read(element);
if (out.type == ElementType::REGION && out.origin == Origin::TOP_LEFT) out.pivot = {};
@@ -240,16 +256,15 @@ namespace anm2ed
bool isEmptySkipped;
};
constexpr WriteRule WRITE_RULES[] = {
{ElementType::SHADERS, SERIALIZE_EXTENSIONS, true},
{ElementType::SHADER, SERIALIZE_EXTENSIONS, false},
{ElementType::UNIFORM, SERIALIZE_EXTENSIONS, false},
{ElementType::COMPONENT, SERIALIZE_EXTENSIONS, false},
{ElementType::SOUNDS, SERIALIZE_SOUNDS, true},
{ElementType::LAYER_ANIMATION_GROUPS, SERIALIZE_GROUPS, true},
{ElementType::NULL_ANIMATION_GROUPS, SERIALIZE_GROUPS, true},
{ElementType::REGION, SERIALIZE_REGIONS, false},
{ElementType::GROUP, SERIALIZE_GROUPS, false}};
constexpr WriteRule WRITE_RULES[] = {{ElementType::SHADERS, SERIALIZE_EXTENSIONS, true},
{ElementType::SHADER, SERIALIZE_EXTENSIONS, false},
{ElementType::UNIFORM, SERIALIZE_EXTENSIONS, false},
{ElementType::COMPONENT, SERIALIZE_EXTENSIONS, false},
{ElementType::SOUNDS, SERIALIZE_SOUNDS, true},
{ElementType::LAYER_ANIMATION_GROUPS, SERIALIZE_GROUPS, true},
{ElementType::NULL_ANIMATION_GROUPS, SERIALIZE_GROUPS, true},
{ElementType::REGION, SERIALIZE_REGIONS, false},
{ElementType::GROUP, SERIALIZE_GROUPS, false}};
struct XmlSink : ElementSink
{
@@ -273,6 +288,7 @@ namespace anm2ed
void attribute(const char* name, int value) override { stack.back()->SetAttribute(name, value); }
void attribute(const char* name, bool value) override { stack.back()->SetAttribute(name, value); }
void attribute(const char* name, float value) override { stack.back()->SetAttribute(name, value); }
void text(const char* value) override { stack.back()->SetText(value); }
void close() override { stack.pop_back(); }
};
@@ -291,7 +307,8 @@ namespace anm2ed
if (!is_track_child_valid(parentType, element.type)) return true;
if (element.type == ElementType::SOUND_ELEMENT && parentType == ElementType::TRIGGER) return true;
for (const auto& rule : WRITE_RULES)
if (rule.type == element.type) return !has_flag(flags, rule.flag) || (rule.isEmptySkipped && element.children.empty());
if (rule.type == element.type)
return !has_flag(flags, rule.flag) || (rule.isEmptySkipped && element.children.empty());
return false;
}
@@ -475,6 +492,9 @@ namespace anm2ed
auto tag = element.type == ElementType::UNKNOWN ? std::string_view(element.tag) : element_tag_get(element.type);
sink.open(tag.empty() ? std::string_view(element.tag) : tag);
element_attributes_emit(sink, element, parentType, flags);
for (const auto& [name, value] : element.extraAttributes)
sink.attribute(name.c_str(), value.c_str());
if (!element.text.empty()) sink.text(element.text.c_str());
if (bake)
{
auto value = INTERPOLATION_VALUES[(std::size_t)bake->interpolation];
@@ -503,8 +523,8 @@ namespace anm2ed
}
auto isNextFrame = i + 1 < (int)element.children.size() && element.children[i + 1].type == ElementType::FRAME;
auto baked = frame_bake_split(child, isNextFrame ? element.children[i + 1] : child, FRAME_DURATION_MIN, false,
false);
auto baked =
frame_bake_split(child, isNextFrame ? element.children[i + 1] : child, FRAME_DURATION_MIN, false, false);
for (int bakeIndex = 0; bakeIndex < (int)baked.size(); ++bakeIndex)
element_emit(sink, baked[bakeIndex], element.type, flags,
bakeIndex == 0 ? std::optional<BakeAttributes>({child.interpolation, (int)baked.size()})
@@ -555,7 +575,8 @@ namespace anm2ed
}
auto containerType = type == ElementType::REGION ? ElementType::SPRITESHEET : ELEMENT_CONTAINERS[(int)type];
auto container = type == ElementType::REGION ? element_get(containerType, spritesheetId) : element_get(containerType);
auto container =
type == ElementType::REGION ? element_get(containerType, spritesheetId) : element_get(containerType);
if (!container)
{
if (errorString) *errorString = std::format("No {} container.", element_tag_get(containerType));