diff --git a/CMakeLists.txt b/CMakeLists.txt index 462c830..c84e5b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,8 +84,6 @@ set(IMGUI_SRC set(TINYXML2_SRC external/tinyxml2/tinyxml2.cpp) file(GLOB PROJECT_SRC CONFIGURE_DEPENDS - src/anm2/*.cpp - src/anm2/*.hpp src/resource/*.cpp src/resource/*.hpp src/imgui/*.cpp @@ -108,7 +106,7 @@ file(GLOB PROJECT_SRC CONFIGURE_DEPENDS src/*.hpp) set(CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) -file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/anm2/*.cpp ${CORE_DIR}/edit/*.cpp ${CORE_DIR}/model/*.cpp) +file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/edit/*.cpp ${CORE_DIR}/model/*.cpp) foreach(CORE_FILE log resource/strings resource/image resource/audio_data resource/asset_store util/file util/path util/math util/string util/time util/working_directory util/xml util/pack util/imgui/multiselect snapshots storage playback selection) list(APPEND CORE_SRC ${CORE_DIR}/${CORE_FILE}.cpp) diff --git a/src/anm2/anm2.cpp b/src/anm2/anm2.cpp deleted file mode 100644 index 341d750..0000000 --- a/src/anm2/anm2.cpp +++ /dev/null @@ -1,148 +0,0 @@ -#include "internal.hpp" - -using namespace anm2ed::util; -using namespace tinyxml2; - -namespace anm2ed -{ - constexpr const char* CREATED_ON_FORMAT = "%m/%d/%Y %I:%M:%S %p"; - - bool is_source_document_tag(std::string_view tag) { return tag == SOURCE_DOCUMENT_TAG; } - - const XMLElement* source_document_get(const XMLElement* rootElement) - { - if (!rootElement) return nullptr; - if (is_source_document_tag(rootElement->Name() ? rootElement->Name() : "")) return rootElement; - - for (auto child = rootElement->FirstChildElement(); child; child = child->NextSiblingElement()) - if (is_source_document_tag(child->Name() ? child->Name() : "")) return child; - - return nullptr; - } - - bool anm2_document_load(Anm2& anm2, XMLDocument& document, std::string* errorString) - { - auto rootElement = document.RootElement(); - if (!rootElement) - { - if (errorString) *errorString = "No root element."; - anm2.isValid = false; - return false; - } - - auto sourceElement = source_document_get(rootElement); - anm2.root = element_read(sourceElement ? sourceElement : rootElement); - if (sourceElement) - { - anm2.root.type = ElementType::ANIMATED_ACTOR; - anm2.root.tag = "AnimatedActor"; - } - group_metadata_embed(anm2.root); - group_frames_restore(anm2.root); - groups_flatten(anm2.root); - content_ids_repair(anm2.root); - shader_frame_ids_repair(anm2.root); - region_frame_ids_repair(anm2.root); - anm2.region_frames_sync(true); - anm2.isValid = true; - return true; - } - - Anm2::Anm2() - { - root = element_make(ElementType::ANIMATED_ACTOR); - - auto info = element_make(ElementType::INFO); - info.createdOn = time::get(CREATED_ON_FORMAT); - - auto content = element_make(ElementType::CONTENT); - for (auto type : {ElementType::SPRITESHEETS, ElementType::SHADERS, ElementType::LAYERS, ElementType::NULLS, - ElementType::EVENTS}) - content.children.push_back(element_make(type)); - - root.children.push_back(std::move(info)); - root.children.push_back(std::move(content)); - root.children.push_back(element_make(ElementType::ANIMATIONS)); - } - - bool Anm2::load(const std::filesystem::path& path, std::string* errorString) - { - XMLDocument document{}; - File file(path, "rb"); - if (!file) - { - if (errorString) *errorString = "File not found."; - isValid = false; - return false; - } - - if (document.LoadFile(file.get()) != XML_SUCCESS) - { - if (errorString) *errorString = document.ErrorStr(); - isValid = false; - return false; - } - - return anm2_document_load(*this, document, errorString); - } - - bool Anm2::load_string(std::string_view string, std::string* errorString) - { - XMLDocument document{}; - if (document.Parse(string.data(), string.size()) != XML_SUCCESS) - { - if (errorString) *errorString = document.ErrorStr(); - isValid = false; - return false; - } - - return anm2_document_load(*this, document, errorString); - } - - bool Anm2::save(const std::filesystem::path& path, std::string* errorString, Options options) const - { - XMLDocument document{}; - document.InsertFirstChild(to_element(document, options)); - - File file(path, "wb"); - if (!file) - { - if (errorString) *errorString = "File permissions."; - return false; - } - - if (document.SaveFile(file.get()) != XML_SUCCESS) - { - if (errorString) *errorString = document.ErrorStr(); - return false; - } - - return true; - } - - std::string Anm2::to_string(Options options) const - { - XMLDocument document{}; - document.InsertEndChild(to_element(document, options)); - return xml::document_to_string(document); - } - - XMLElement* Anm2::to_element(XMLDocument& document, Options options) const - { - auto serialize = [&](Flags flags) - { - auto normalized = normalized_for_serialize(flags); - normalized.region_frames_sync(true); - return element_to_xml(document, normalized.root, ElementType::UNKNOWN, flags); - }; - - auto editor = serialize(SERIALIZE_ANM2ED_DEFAULT); - if (options.isExtendedFormat) return editor; - - auto out = serialize(SERIALIZE_ISAAC_DEFAULT); - editor->SetName(SOURCE_DOCUMENT_TAG.data()); - out->InsertEndChild(editor); - return out; - } - -} diff --git a/src/anm2/anm2.hpp b/src/anm2/anm2.hpp deleted file mode 100644 index 1b2297b..0000000 --- a/src/anm2/anm2.hpp +++ /dev/null @@ -1,256 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "model/common.hpp" - -namespace anm2ed -{ - struct Element - { - ElementType type{ElementType::UNKNOWN}; - std::string tag{}; - std::vector children{}; - std::string name{}; - std::string createdBy{"robot"}; - std::string createdOn{}; - std::filesystem::path path{}; - std::filesystem::path vertex{}; - std::filesystem::path fragment{}; - std::string binding{}; - std::string value{}; - std::string defaultAnimation{}; - int id{-1}; - int layerId{-1}; - int nullId{-1}; - int spritesheetId{}; - int fps{30}; - int version{}; - int frameNum{1}; - int duration{1}; - int atFrame{-1}; - int eventId{-1}; - int regionId{-1}; - int shaderId{-1}; - int soundId{-1}; - int groupId{-1}; - int index{-1}; - bool isLoop{true}; - bool isVisible{true}; - bool isShowRect{}; - bool isExpanded{true}; - bool isEnabled{true}; - Interpolation interpolation{Interpolation::NONE}; - Origin origin{Origin::CUSTOM}; - float rotation{}; - std::vector soundIds{}; - std::vector> extraAttributes{}; - std::string text{}; - glm::vec2 pivot{}; - glm::vec2 crop{}; - glm::vec2 position{}; - glm::vec2 size{}; - glm::vec2 scale{100.0f, 100.0f}; - glm::vec2 shear{}; - glm::vec3 colorOffset{}; - glm::vec4 tint{types::color::WHITE}; - }; - - struct TrackContainer - { - ItemType itemType; - ElementType container; - ElementType track; - ElementType groups; - ElementType elements; - ElementType element; - int Element::* id; - }; - - inline constexpr TrackContainer TRACK_CONTAINERS[] = { - {ItemType::LAYER, ElementType::LAYER_ANIMATIONS, ElementType::LAYER_ANIMATION, - ElementType::LAYER_ANIMATION_GROUPS, ElementType::LAYERS, ElementType::LAYER_ELEMENT, &Element::layerId}, - {ItemType::NULL_, ElementType::NULL_ANIMATIONS, ElementType::NULL_ANIMATION, ElementType::NULL_ANIMATION_GROUPS, - ElementType::NULLS, ElementType::NULL_ELEMENT, &Element::nullId}}; - - inline constexpr int GROUP_ANY = -2; - - template using ElementPointer = std::conditional_t, const Element*, Element*>; - - constexpr const TrackContainer* track_container_get(ElementType type) - { - for (const auto& row : TRACK_CONTAINERS) - if (type == row.container || type == row.track || type == row.groups || type == row.elements || - type == row.element) - return &row; - return nullptr; - } - - constexpr const TrackContainer* track_container_get(ItemType type) - { - for (const auto& row : TRACK_CONTAINERS) - if (type == row.itemType) return &row; - return nullptr; - } - - constexpr bool is_track(const Element& element) - { - return element.type != ElementType::UNKNOWN && std::ranges::contains(TYPE_TRACKS, element.type); - } - - constexpr ElementType track_frame_type_get(const Element& track) - { - return track.type == ElementType::TRIGGERS ? ElementType::TRIGGER : ElementType::FRAME; - } - - constexpr int track_id_get(const Element& track) - { - auto row = track_container_get(track.type); - return row && row->track == track.type ? track.*(row->id) : -1; - } - - template ElementPointer child_find(E& element, Predicate&& isMatch) - { - for (auto& child : element.children) - if (isMatch(child)) return &child; - return nullptr; - } - - template ElementPointer child_first_get(E& element, ElementType type) - { - return child_find(element, [&](const Element& child) { return child.type == type; }); - } - - template ElementPointer child_id_get(E& element, ElementType type, int id) - { - return child_find(element, [&](const Element& child) { return child.type == type && child.id == id; }); - } - - template ElementPointer shader_uniform_get(E& shader, std::string_view name) - { - return child_find(shader, - [&](const Element& child) { return child.type == ElementType::UNIFORM && child.name == name; }); - } - - template ElementPointer shader_uniform_component_get(E& uniform, int index) - { - return child_find(uniform, [&](const Element& child) - { return child.type == ElementType::COMPONENT && child.index == index; }); - } - - template ElementPointer element_first_get(E& element, ElementType type) - { - if (element.type == type) return &element; - for (auto& child : element.children) - if (auto result = element_first_get(child, type)) return result; - return nullptr; - } - - template ElementPointer track_find(E& parent, ElementType trackType, int id, int groupId = GROUP_ANY) - { - for (auto& child : parent.children) - { - if (child.type == trackType && track_id_get(child) == id && (groupId == GROUP_ANY || child.groupId == groupId)) - return &child; - if (child.type == ElementType::GROUP) - if (auto found = track_find(child, trackType, id, groupId)) return found; - } - return nullptr; - } - - template ElementPointer animation_group_get(E& animation, int groupType, int groupId) - { - auto container = groupId < 0 ? nullptr : child_first_get(animation, TYPE_CONTAINERS[groupType]); - return container ? child_id_get(*container, ElementType::GROUP, groupId) : nullptr; - } - - template - ElementPointer animation_item_get(E& animation, ItemType type, int id = -1, int groupType = NONE, int groupId = -1) - { - auto trackType = TYPE_TRACKS[(int)type]; - auto isGrouped = groupType != NONE && groupId != -1; - if (type == ItemType::ROOT && isGrouped) - { - auto group = animation_group_get(animation, groupType, groupId); - return group ? child_first_get(*group, ElementType::ROOT_ANIMATION) : nullptr; - } - if (type == ItemType::ROOT || type == ItemType::TRIGGER) return child_first_get(animation, trackType); - - auto container = child_first_get(animation, TYPE_CONTAINERS[(int)type]); - return container ? track_find(*container, trackType, id, isGrouped ? groupId : GROUP_ANY) : nullptr; - } - - template ElementPointer track_frame_get(E& track, int index) - { - auto childIndex = track_frame_child_index_get(track, index); - return childIndex == -1 ? nullptr : &track.children[childIndex]; - } - - template void tracks_each(E& parent, ElementType trackType, Callback&& callback) - { - for (auto& child : parent.children) - if (child.type == trackType) - callback(child); - else if (child.type == ElementType::GROUP) - tracks_each(child, trackType, callback); - } - - template void animations_tracks_each(E& root, ElementType trackType, Callback&& callback) - { - auto animations = element_first_get(root, ElementType::ANIMATIONS); - auto row = track_container_get(trackType); - if (!animations || !row) return; - for (auto& animation : animations->children) - if (animation.type == ElementType::ANIMATION) - if (auto container = child_first_get(animation, row->container)) tracks_each(*container, trackType, callback); - } - - template void element_each(E& element, Callback&& callback) - { - callback(element); - for (auto& child : element.children) - element_each(child, callback); - } - - Element element_make(ElementType); - Element& child_ensure(Element&, ElementType); - Element element_read(const tinyxml2::XMLElement*); - std::string element_to_string(const Element&, Flags = SERIALIZE_EDITOR_DEFAULT); - std::string element_to_string(const Element&, ElementType, Flags = SERIALIZE_EDITOR_DEFAULT); - tinyxml2::XMLElement* element_to_xml(tinyxml2::XMLDocument&, const Element&, ElementType, - Flags = SERIALIZE_EDITOR_DEFAULT); - Element root_animation_make(); - void frame_mix(Element&, const Element&, float); - Element frame_generate(const Element&, float); - int track_length_get(const Element&); - - class Anm2 - { - public: - bool isValid{true}; - Element root{}; - - Anm2(); - - bool load(const std::filesystem::path&, std::string* = nullptr); - bool load_string(std::string_view, std::string* = nullptr); - bool save(const std::filesystem::path&, std::string* = nullptr, Options = {}) const; - std::string to_string(Options = {}) const; - tinyxml2::XMLElement* to_element(tinyxml2::XMLDocument&, Options = {}) const; - void region_frames_sync(bool); - Anm2 normalized_for_serialize(Flags = SERIALIZE_DEFAULT) const; - }; - -} diff --git a/src/anm2/element.cpp b/src/anm2/element.cpp deleted file mode 100644 index 063e01d..0000000 --- a/src/anm2/element.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "internal.hpp" - -using namespace anm2ed::util; -using namespace tinyxml2; - -namespace anm2ed -{ - constexpr std::array ELEMENT_TAGS = { -#define X(symbol, tag, container) std::string_view{tag}, - ANM2_ELEMENT_TYPES -#undef X - }; - - ElementType element_type_get(std::string_view tag) - { - auto it = std::ranges::find(ELEMENT_TAGS, tag); - return it == ELEMENT_TAGS.end() ? ElementType::UNKNOWN : (ElementType)(it - ELEMENT_TAGS.begin()); - } - - std::string_view element_tag_get(ElementType type) - { - return (std::size_t)type < ELEMENT_TAGS.size() ? ELEMENT_TAGS[(std::size_t)type] : std::string_view{}; - } - - Element element_make(ElementType type) - { - Element element{}; - element.type = type; - element.tag = std::string(element_tag_get(type)); - return element; - } - - Element root_animation_make() - { - auto root = element_make(ElementType::ROOT_ANIMATION); - root.children.push_back(element_make(ElementType::FRAME)); - return root; - } - - Element& child_ensure(Element& element, ElementType type) - { - if (auto child = child_first_get(element, type)) return *child; - return element.children.emplace_back(element_make(type)); - } - - Element* content_container_get(Element& root, ElementType type) - { - auto content = child_first_get(root, ElementType::CONTENT); - return content ? child_first_get(*content, type) : nullptr; - } - - std::unordered_map child_ids_compact(Element& element, ElementType type) - { - std::unordered_map remap{}; - int nextId{}; - for (auto& child : element.children) - if (child.type == type) - { - remap[child.id] = nextId; - child.id = nextId++; - } - return remap; - } - - bool is_track_child_valid(ElementType parentType, ElementType childType) - { - if (parentType == ElementType::CONTENT && childType == ElementType::SHADERS) return true; - if (parentType == ElementType::SHADERS) return childType == ElementType::SHADER; - if (parentType == ElementType::SHADER) return childType == ElementType::UNIFORM; - if (parentType == ElementType::UNIFORM) return childType == ElementType::COMPONENT; - if (childType == ElementType::SHADERS || childType == ElementType::SHADER || childType == ElementType::UNIFORM || - childType == ElementType::COMPONENT) - return false; - if (parentType == ElementType::TRIGGERS) return childType == ElementType::TRIGGER; - if (parentType == ElementType::TRIGGER) return childType == ElementType::SOUND_ELEMENT; - if (parentType == ElementType::LAYER_ANIMATION_GROUPS || parentType == ElementType::NULL_ANIMATION_GROUPS) - return childType == ElementType::GROUP; - if (parentType == ElementType::ROOT_ANIMATION || parentType == ElementType::LAYER_ANIMATION || - parentType == ElementType::NULL_ANIMATION) - return childType == ElementType::FRAME; - return true; - } - -} diff --git a/src/anm2/frames.cpp b/src/anm2/frames.cpp deleted file mode 100644 index cf22efb..0000000 --- a/src/anm2/frames.cpp +++ /dev/null @@ -1,160 +0,0 @@ -#include "internal.hpp" - -using namespace anm2ed::util; -using namespace tinyxml2; - -namespace anm2ed -{ - enum class InterpolationBakeType - { - SPECIAL, - ALL - }; - - void frame_mix(Element& frame, const Element& next, float amount) - { - frame.rotation = glm::mix(frame.rotation, next.rotation, amount); - frame.position = glm::mix(frame.position, next.position, amount); - frame.scale = glm::mix(frame.scale, next.scale, amount); - frame.shear = glm::mix(frame.shear, next.shear, amount); - frame.colorOffset = glm::mix(frame.colorOffset, next.colorOffset, amount); - frame.tint = glm::mix(frame.tint, next.tint, amount); - } - - std::vector frame_bake_split(const Element& original, const Element& next, int interval, bool isRoundScale, - bool isRoundRotation) - { - auto total = std::max(original.duration, FRAME_DURATION_MIN); - interval = std::max(interval, FRAME_DURATION_MIN); - std::vector frames{}; - frames.reserve((total + interval - 1) / interval); - for (int duration = 0; duration < total; duration += interval) - { - auto& baked = frames.emplace_back(original); - frame_mix(baked, next, interpolation_factor(original.interpolation, (float)duration / total)); - baked.duration = std::min(interval, total - duration); - baked.interpolation = Interpolation::NONE; - if (isRoundScale) baked.scale = glm::round(baked.scale); - if (isRoundRotation) baked.rotation = std::round(baked.rotation); - } - return frames; - } - - Element frame_generate(const Element& track, float time) - { - auto frame = element_make(track_frame_type_get(track)); - frame.isVisible = false; - if (track.children.empty()) return frame; - - time = std::max(time, 0.0f); - - const Element* frameNext = nullptr; - int durationCurrent{}; - int durationNext{}; - auto frameType = track_frame_type_get(track); - - for (int i = 0; i < (int)track.children.size(); ++i) - { - const auto& iFrame = track.children[i]; - if (iFrame.type != frameType) continue; - - if (frameType == ElementType::TRIGGER) - { - if ((int)time == iFrame.atFrame) - { - frame = iFrame; - break; - } - continue; - } - - frame = iFrame; - durationNext += frame.duration; - - if (time >= durationCurrent && time < durationNext) - { - for (int next = i + 1; next < (int)track.children.size(); ++next) - if (track.children[next].type == ElementType::FRAME) - { - frameNext = &track.children[next]; - break; - } - break; - } - - durationCurrent += frame.duration; - } - - if (frameType != ElementType::TRIGGER && frame.interpolation != Interpolation::NONE && frameNext && - frame.duration > 1) - { - frame_mix(frame, *frameNext, - interpolation_factor(frame.interpolation, (time - durationCurrent) / (durationNext - durationCurrent))); - } - - return frame; - } - - bool is_frame_interpolation_baked(const Element& frame, InterpolationBakeType type) - { - if (frame.interpolation == Interpolation::NONE) return false; - return type == InterpolationBakeType::ALL || frame.interpolation != Interpolation::LINEAR; - } - - void track_frames_bake(Element& track, int interval, bool isRoundScale, bool isRoundRotation, - InterpolationBakeType type) - { - for (int index = (int)track.children.size() - 1; index >= 0; --index) - { - auto original = track.children[index]; - if (original.type != ElementType::FRAME || !is_frame_interpolation_baked(original, type)) continue; - - if (original.duration <= FRAME_DURATION_MIN) - { - track.children[index].interpolation = Interpolation::NONE; - continue; - } - - auto isNextFrame = index + 1 < (int)track.children.size() && track.children[index + 1].type == ElementType::FRAME; - auto baked = frame_bake_split(original, isNextFrame ? track.children[index + 1] : original, interval, - isRoundScale, isRoundRotation); - track.children.erase(track.children.begin() + index); - track.children.insert(track.children.begin() + index, std::make_move_iterator(baked.begin()), - std::make_move_iterator(baked.end())); - } - } - - void interpolated_frames_bake(Element& element, int interval, bool isRoundScale, bool isRoundRotation, - InterpolationBakeType type) - { - if (is_track(element)) - { - track_frames_bake(element, interval, isRoundScale, isRoundRotation, type); - return; - } - - for (auto& child : element.children) - interpolated_frames_bake(child, interval, isRoundScale, isRoundRotation, type); - } - - void all_interpolated_frames_bake(Element& element, int interval, bool isRoundScale, bool isRoundRotation) - { - interpolated_frames_bake(element, interval, isRoundScale, isRoundRotation, InterpolationBakeType::ALL); - } - - int track_length_get(const Element& track) - { - int length{}; - if (track.type == ElementType::TRIGGERS) - { - for (const auto& trigger : track.children) - if (trigger.type == ElementType::TRIGGER) length = std::max(length, trigger.atFrame); - return length; - } - - for (const auto& frame : track.children) - if (frame.type == ElementType::FRAME) length += frame.duration; - return length; - } - -} diff --git a/src/anm2/groups.cpp b/src/anm2/groups.cpp deleted file mode 100644 index ae6a761..0000000 --- a/src/anm2/groups.cpp +++ /dev/null @@ -1,388 +0,0 @@ -#include "internal.hpp" - -using namespace anm2ed::util; -using namespace tinyxml2; - -namespace anm2ed -{ - constexpr std::string_view ANM2ED_GROUP_FRAME_BACKUP_TAG = "FrameRestore"; - - - bool is_frame_transform_default(const Element& frame) - { - auto base = element_make(ElementType::FRAME); - return frame.position == base.position && frame.scale == base.scale && frame.rotation == base.rotation && - frame.shear == base.shear && frame.tint == base.tint && frame.colorOffset == base.colorOffset; - } - - bool is_root_animation_transform_default(const Element& root) - { - for (const auto& frame : root.children) - if (frame.type == ElementType::FRAME && !is_frame_transform_default(frame)) return false; - return true; - } - - bool is_group_frame_backup(const Element& element) - { - return element.type == ElementType::UNKNOWN && element.tag == ANM2ED_GROUP_FRAME_BACKUP_TAG; - } - - void group_frame_backups_erase(Element& group) - { - std::erase_if(group.children, [](const Element& child) { return is_group_frame_backup(child); }); - } - - Element group_frame_backup_make(const Element& container, const Element& group, ElementType trackType) - { - Element backup{}; - backup.type = ElementType::UNKNOWN; - backup.tag = std::string(ANM2ED_GROUP_FRAME_BACKUP_TAG); - - if (auto root = child_first_get(group, ElementType::ROOT_ANIMATION)) backup.children.push_back(*root); - - for (const auto& item : container.children) - if (item.type == trackType && item.groupId == group.id) backup.children.push_back(item); - - return backup; - } - - void frame_group_root_apply(Element& frame, const Element& rootFrame) - { - auto rootScale = math::percent_to_unit(rootFrame.scale); - auto offset = frame.position * rootScale; - auto radians = glm::radians(rootFrame.rotation); - auto cos = std::cos(radians); - auto sin = std::sin(radians); - - frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos); - frame.scale *= rootScale; - frame.rotation += rootFrame.rotation; - frame.tint *= rootFrame.tint; - frame.colorOffset += rootFrame.colorOffset; - } - - void track_group_root_bake(Element& track, const Element& root, int frameNum) - { - auto trackLength = track_length_get(track); - if (trackLength <= 0) return; - - auto length = std::max({trackLength, track_length_get(root), frameNum, FRAME_DURATION_MIN}); - std::vector frames{}; - frames.reserve(length); - - for (int time = 0; time < length; ++time) - { - auto frame = frame_generate(track, (float)time); - auto rootFrame = frame_generate(root, (float)time); - frame_group_root_apply(frame, rootFrame); - frame.duration = FRAME_DURATION_MIN; - frame.interpolation = Interpolation::NONE; - frames.push_back(std::move(frame)); - } - - track.children = std::move(frames); - } - - void group_frames_bake(Element& container, ElementType trackType, int frameNum) - { - for (auto& group : container.children) - { - if (group.type != ElementType::GROUP) continue; - group_frame_backups_erase(group); - auto root = child_first_get(group, ElementType::ROOT_ANIMATION); - if (!root || is_root_animation_transform_default(*root)) continue; - - auto backup = group_frame_backup_make(container, group, trackType); - for (auto& item : container.children) - if (item.type == trackType && item.groupId == group.id) track_group_root_bake(item, *root, frameNum); - - *root = root_animation_make(); - group.children.push_back(std::move(backup)); - } - } - - template void animation_track_containers_each(Element& root, Callback&& callback) - { - element_each(root, - [&](Element& element) - { - if (element.type != ElementType::ANIMATION) return; - for (const auto& row : TRACK_CONTAINERS) - if (auto container = child_first_get(element, row.container)) callback(element, *container, row); - }); - } - - void group_frames_bake(Element& root) - { - animation_track_containers_each(root, [](Element& animation, Element& container, const TrackContainer& row) - { group_frames_bake(container, row.track, animation.frameNum); }); - } - - void group_frames_restore(Element& container, ElementType trackType) - { - std::set restoredGroupIds{}; - - for (auto& group : container.children) - { - if (group.type != ElementType::GROUP) continue; - if (group.id < 0) continue; - - const Element* backup{}; - for (const auto& child : group.children) - if (is_group_frame_backup(child)) - { - backup = &child; - break; - } - - if (!backup) continue; - - auto root = root_animation_make(); - if (auto currentRoot = child_first_get(group, ElementType::ROOT_ANIMATION)) root = *currentRoot; - - std::vector restoredTracks{}; - bool isRootRestored{}; - for (const auto& child : backup->children) - { - if (child.type == ElementType::ROOT_ANIMATION && !isRootRestored) - { - root = child; - isRootRestored = true; - } - else if (child.type == trackType) - { - auto track = child; - track.groupId = group.id; - restoredTracks.push_back(std::move(track)); - } - } - - if (!isRootRestored && restoredTracks.empty()) continue; - - group.children = {std::move(root)}; - group.children.insert(group.children.end(), std::make_move_iterator(restoredTracks.begin()), - std::make_move_iterator(restoredTracks.end())); - restoredGroupIds.insert(group.id); - } - - if (restoredGroupIds.empty()) return; - - std::erase_if(container.children, [&](const Element& item) - { return item.type == trackType && restoredGroupIds.contains(item.groupId); }); - } - - void group_frames_restore(Element& root) - { - animation_track_containers_each(root, [](Element&, Element& container, const TrackContainer& row) - { group_frames_restore(container, row.track); }); - } - - int child_index_get(const Element& element, ElementType type) - { - auto child = child_first_get(element, type); - return child ? (int)(child - element.children.data()) : -1; - } - - ElementType group_child_type_get(ElementType parentType) - { - if (parentType == ElementType::ANIMATIONS) return ElementType::ANIMATION; - auto row = track_container_get(parentType); - return row && row->container == parentType ? row->track : ElementType::UNKNOWN; - } - - int track_group_insert_index_get(const Element& container, ElementType trackType, int groupId) - { - for (int i = 0; i < (int)container.children.size(); ++i) - if (container.children[i].type == trackType && container.children[i].groupId == groupId) return i; - return (int)container.children.size(); - } - - void group_metadata_embed(Element& animation, const TrackContainer& row) - { - auto groupContainer = child_first_get(animation, row.groups); - if (!groupContainer) return; - - std::vector groups{}; - for (const auto& child : groupContainer->children) - if (child.type == ElementType::GROUP) groups.push_back(child); - - if (!groups.empty()) - { - auto& container = child_ensure(animation, row.container); - std::set groupIds{}; - for (const auto& group : groups) - if (group.id >= 0) groupIds.insert(group.id); - std::erase_if(container.children, [&](const Element& child) - { return child.type == ElementType::GROUP && groupIds.contains(child.id); }); - - std::stable_sort(groups.begin(), groups.end(), - [](const Element& left, const Element& right) - { - if (left.index == -1 || right.index == -1) return left.index != -1; - return left.index < right.index; - }); - - for (auto& group : groups) - { - auto insertIndex = group.index >= 0 ? std::min(group.index, (int)container.children.size()) - : track_group_insert_index_get(container, row.track, group.id); - group.index = -1; - container.children.insert(container.children.begin() + insertIndex, std::move(group)); - } - } - - std::erase_if(animation.children, [&](const Element& child) { return child.type == row.groups; }); - } - - void group_metadata_embed(Element& root) - { - element_each(root, - [](Element& element) - { - if (element.type != ElementType::ANIMATION) return; - for (const auto& row : TRACK_CONTAINERS) - group_metadata_embed(element, row); - }); - } - - void group_metadata_extract(Element& animation, const TrackContainer& row, Flags flags) - { - std::erase_if(animation.children, [&](const Element& child) { return child.type == row.groups; }); - if (!has_flag(flags, SERIALIZE_GROUPS)) return; - - auto trackContainerIndex = child_index_get(animation, row.container); - if (trackContainerIndex < 0) return; - - auto& container = animation.children[trackContainerIndex]; - auto groupContainer = element_make(row.groups); - std::vector tracks{}; - tracks.reserve(container.children.size()); - - for (int i = 0; i < (int)container.children.size(); ++i) - { - auto child = container.children[i]; - if (child.type == ElementType::GROUP) - { - child.index = i; - groupContainer.children.push_back(std::move(child)); - } - else - tracks.push_back(std::move(child)); - } - - container.children = std::move(tracks); - if (groupContainer.children.empty()) return; - animation.children.insert(animation.children.begin() + trackContainerIndex + 1, std::move(groupContainer)); - } - - void group_metadata_extract(Element& root, Flags flags) - { - element_each(root, - [&](Element& element) - { - if (element.type != ElementType::ANIMATION) return; - for (const auto& row : TRACK_CONTAINERS) - group_metadata_extract(element, row, flags); - }); - } - - void group_children_flatten(Element& container, ElementType childType) - { - if (childType == ElementType::UNKNOWN) return; - - int nextGroupId{}; - for (const auto& item : container.children) - if (item.type == ElementType::GROUP) nextGroupId = std::max(nextGroupId, item.id + 1); - - std::vector flattened{}; - for (auto item : container.children) - { - if (item.type != ElementType::GROUP) - { - flattened.push_back(item); - continue; - } - - if (item.id < 0) item.id = nextGroupId++; - auto children = std::move(item.children); - item.children.clear(); - - if (childType != ElementType::ANIMATION) - { - auto root = std::ranges::find(children, ElementType::ROOT_ANIMATION, &Element::type); - item.children.push_back(root == children.end() ? root_animation_make() : *root); - } - - flattened.push_back(item); - - for (auto child : children) - { - if (child.type != childType) continue; - child.groupId = item.id; - flattened.push_back(child); - } - } - - std::set groupIds{}; - for (const auto& item : flattened) - if (item.type == ElementType::GROUP) groupIds.insert(item.id); - - for (auto& item : flattened) - if (item.type == childType && !groupIds.contains(item.groupId)) item.groupId = -1; - - container.children = std::move(flattened); - } - - void groups_flatten(Element& root) - { - element_each(root, [](Element& element) { group_children_flatten(element, group_child_type_get(element.type)); }); - } - - void group_children_nest(Element& container, ElementType childType) - { - if (childType == ElementType::UNKNOWN) return; - - std::set groupIds{}; - for (const auto& child : container.children) - if (child.type == ElementType::GROUP && child.id >= 0) groupIds.insert(child.id); - if (groupIds.empty()) return; - - std::unordered_map> groupedChildren{}; - for (const auto& child : container.children) - { - if (child.type != childType || !groupIds.contains(child.groupId)) continue; - auto groupedChild = child; - groupedChild.groupId = -1; - groupedChildren[child.groupId].push_back(std::move(groupedChild)); - } - - std::vector nested{}; - nested.reserve(container.children.size()); - for (auto child : container.children) - { - if (child.type == childType && groupIds.contains(child.groupId)) continue; - if (child.type == ElementType::GROUP) - { - auto groupId = child.id; - child.index = -1; - if (auto grouped = groupedChildren.find(groupId); grouped != groupedChildren.end()) - child.children.insert(child.children.end(), std::make_move_iterator(grouped->second.begin()), - std::make_move_iterator(grouped->second.end())); - } - nested.push_back(std::move(child)); - } - - container.children = std::move(nested); - } - - void groups_nest(Element& root) - { - element_each(root, [](Element& element) { group_children_nest(element, group_child_type_get(element.type)); }); - } - - void source_document_erase(Element& root) - { - element_each(root, [](Element& element) - { std::erase_if(element.children, [](const Element& child) { return is_source_document_tag(child.tag); }); }); - } -} diff --git a/src/anm2/hash.cpp b/src/anm2/hash.cpp deleted file mode 100644 index a1e94ff..0000000 --- a/src/anm2/hash.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "internal.hpp" - -using namespace tinyxml2; - -namespace anm2ed -{ - constexpr std::uint64_t ANM2_HASH_OFFSET = 14695981039346656037ull; - constexpr std::uint64_t ANM2_HASH_PRIME = 1099511628211ull; - constexpr std::size_t HASH_BUFFER_SIZE = 64; - - struct HashSink : ElementSink - { - std::uint64_t hash{ANM2_HASH_OFFSET}; - - void byte_append(unsigned char value) - { - hash ^= value; - hash *= ANM2_HASH_PRIME; - } - - void bytes_append(const char* data, std::size_t size) - { - for (std::size_t i = 0; i < size; ++i) - byte_append((unsigned char)data[i]); - } - - void string_append(std::string_view value) - { - auto size = (std::uint64_t)value.size(); - bytes_append(reinterpret_cast(&size), sizeof(size)); - bytes_append(value.data(), value.size()); - } - - template void formatted_append(const char* name, T value) - { - char buffer[HASH_BUFFER_SIZE]{}; - XMLUtil::ToStr(value, buffer, sizeof(buffer)); - attribute(name, (const char*)buffer); - } - - void open(std::string_view tag) override - { - byte_append('E'); - string_append(tag); - } - - void attribute(const char* name, const char* value) override - { - byte_append('A'); - string_append(name); - string_append(value); - } - - 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(']'); } - }; - - std::uint64_t anm2_hash_get(const Element& root, Options options) - { - HashSink sink{}; - sink.byte_append(options.isExtendedFormat ? 'E' : 'A'); - element_emit(sink, root, ElementType::UNKNOWN, SERIALIZE_ANM2ED_DEFAULT); - return sink.hash; - } -} diff --git a/src/anm2/internal.hpp b/src/anm2/internal.hpp deleted file mode 100644 index 248c5f5..0000000 --- a/src/anm2/internal.hpp +++ /dev/null @@ -1,115 +0,0 @@ -#pragma once - -#include "anm2.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "file.hpp" -#include "math.hpp" -#include "path.hpp" -#include "time.hpp" -#include "working_directory.hpp" -#include "xml.hpp" - -namespace anm2ed -{ - inline constexpr std::string_view ANIMATION_MERGED_SUFFIX = " (Merged)"; - inline constexpr std::array INTERPOLATION_VALUES = { - "", "", "EaseIn", "EaseOut", "EaseInOut"}; - inline constexpr std::array ORIGIN_VALUES = {"", "TopLeft", "Center"}; - inline constexpr std::string_view SOURCE_DOCUMENT_TAG = "SourceDocument"; - - struct ElementSink - { - virtual ~ElementSink() = default; - virtual void open(std::string_view) = 0; - virtual void attribute(const char*, const char*) = 0; - 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; - }; - - struct BakeAttributes - { - Interpolation interpolation{Interpolation::NONE}; - int delay{FRAME_DURATION_MIN}; - }; - - void element_emit(ElementSink&, const Element&, ElementType, Flags, std::optional = std::nullopt); - - bool is_source_document_tag(std::string_view); - ElementType element_type_get(std::string_view); - std::string_view element_tag_get(ElementType); - ElementType group_child_type_get(ElementType); - Element* content_container_get(Element&, ElementType); - std::unordered_map child_ids_compact(Element&, ElementType); - void group_frames_bake(Element&); - void group_frames_restore(Element&); - void group_metadata_embed(Element&); - void group_metadata_extract(Element&, Flags); - void groups_flatten(Element&); - void groups_nest(Element&); - void source_document_erase(Element&); - void content_ids_repair(Element&); - void shader_frame_ids_repair(Element&); - void shader_ids_remap(Element&); - void region_ids_remap(Element&); - void region_frame_ids_repair(Element&); - bool is_track_child_valid(ElementType, ElementType); - bool is_nested_group_parent(ElementType, Flags); - bool is_group_id_serialized(Flags); - bool element_write_skip(const Element&, ElementType, Flags); - bool is_frame_bake_serialized(const Element&, Flags); - std::vector frame_bake_split(const Element&, const Element&, int, bool, bool); - void all_interpolated_frames_bake(Element&, int, bool, bool); - int color_write(float); - bool is_region_matched(const Element&, const Element&); - Element* region_match_get(Element&, const Element&); - - template void layer_frames_each(Element& root, Element& animation, Callback&& callback) - { - auto layers = content_container_get(root, ElementType::LAYERS); - auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS); - auto tracks = child_first_get(animation, ElementType::LAYER_ANIMATIONS); - if (!layers || !spritesheets || !tracks) return; - - tracks_each(*tracks, ElementType::LAYER_ANIMATION, - [&](Element& track) - { - auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, track.layerId); - auto spritesheet = - layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET, layer->spritesheetId) : nullptr; - if (!spritesheet) return; - for (auto& frame : track.children) - if (frame.type == ElementType::FRAME) callback(frame, *spritesheet); - }); - } - - template void layer_frames_each(Element& root, Callback&& callback) - { - if (auto animations = element_first_get(root, ElementType::ANIMATIONS)) - for (auto& animation : animations->children) - if (animation.type == ElementType::ANIMATION) layer_frames_each(root, animation, callback); - } - - std::uint64_t anm2_hash_get(const Element&, Options); -} diff --git a/src/anm2/normalize.cpp b/src/anm2/normalize.cpp deleted file mode 100644 index b56afc5..0000000 --- a/src/anm2/normalize.cpp +++ /dev/null @@ -1,172 +0,0 @@ -#include "internal.hpp" - -namespace anm2ed -{ - constexpr std::string_view SHADER_NAME_DEFAULT = "New Shader"; - - void frame_ids_remap(Element& root, int Element::* member, const std::unordered_map& remap) - { - element_each(root, - [&](Element& element) - { - if (element.type != ElementType::FRAME || element.*member == -1) return; - auto it = remap.find(element.*member); - element.*member = it == remap.end() ? -1 : it->second; - }); - } - - // 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) - { - std::set usedIds{}; - for (const auto& child : container.children) - if (child.type == type && child.id >= 0) usedIds.insert(child.id); - - std::set 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; - 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); - auto animations = element_first_get(root, ElementType::ANIMATIONS); - if (!animations) return; - - std::unordered_map identity{}; - if (shaders) - for (const auto& shader : shaders->children) - if (shader.type == ElementType::SHADER) identity[shader.id] = shader.id; - frame_ids_remap(*animations, &Element::shaderId, identity); - } - - bool is_region_matched(const Element& region, const Element& frame) - { - return region.type == ElementType::REGION && glm::ivec2(region.crop) == glm::ivec2(frame.crop) && - glm::ivec2(region.size) == glm::ivec2(frame.size) && glm::ivec2(region.pivot) == glm::ivec2(frame.pivot); - } - - Element* region_match_get(Element& spritesheet, const Element& frame) - { - return child_find(spritesheet, [&](const Element& region) { return is_region_matched(region, frame); }); - } - - void region_frame_ids_repair(Element& root) - { - layer_frames_each(root, - [](Element& frame, Element& spritesheet) - { - auto region = frame.regionId == -1 - ? nullptr - : child_id_get(spritesheet, ElementType::REGION, frame.regionId); - if (region && is_region_matched(*region, frame)) return; - if (auto match = region_match_get(spritesheet, frame)) frame.regionId = match->id; - }); - } - - void region_ids_remap(Element& root) - { - auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS); - if (!spritesheets) return; - - std::unordered_map> remaps{}; - for (auto& spritesheet : spritesheets->children) - if (spritesheet.type == ElementType::SPRITESHEET) - if (auto remap = child_ids_compact(spritesheet, ElementType::REGION); !remap.empty()) - remaps[spritesheet.id] = std::move(remap); - - if (remaps.empty()) return; - - layer_frames_each(root, - [&](Element& frame, const Element& spritesheet) - { - auto remap = remaps.find(spritesheet.id); - if (remap == remaps.end() || frame.regionId == -1) return; - auto it = remap->second.find(frame.regionId); - frame.regionId = it == remap->second.end() ? -1 : it->second; - }); - } - - void shader_ids_remap(Element& root) - { - auto shaders = content_container_get(root, ElementType::SHADERS); - auto remap = shaders ? child_ids_compact(*shaders, ElementType::SHADER) : std::unordered_map{}; - frame_ids_remap(root, &Element::shaderId, remap); - } - - void Anm2::region_frames_sync(bool isClearInvalid) - { - layer_frames_each(root, - [&](Element& frame, const Element& spritesheet) - { - if (frame.regionId == -1) return; - auto region = child_id_get(spritesheet, ElementType::REGION, frame.regionId); - if (!region) - { - if (isClearInvalid) frame.regionId = -1; - return; - } - - frame.crop = region->crop; - frame.size = region->size; - frame.pivot = region->pivot; - }); - } - - Anm2 Anm2::normalized_for_serialize(Flags flags) const - { - auto normalized = *this; - source_document_erase(normalized.root); - group_frames_restore(normalized.root); - groups_flatten(normalized.root); - if (has_flag(flags, SERIALIZE_BAKE_GROUP_FRAMES)) group_frames_bake(normalized.root); - if (has_flag(flags, SERIALIZE_FLATTEN_SPECIAL_INTERPOLATED_FRAMES)) - all_interpolated_frames_bake(normalized.root, FRAME_DURATION_MIN, false, false); - region_ids_remap(normalized.root); - shader_ids_remap(normalized.root); - if (has_flag(flags, SERIALIZE_GROUPS) && has_flag(flags, SERIALIZE_NESTED_GROUPS)) - groups_nest(normalized.root); - else - group_metadata_extract(normalized.root, flags); - - if (auto content = child_first_get(normalized.root, ElementType::CONTENT)) - std::erase_if(content->children, [](const Element& element) { return element.tag == "Groups"; }); - if (auto layers = content_container_get(normalized.root, ElementType::LAYERS)) - { - auto remap = child_ids_compact(*layers, ElementType::LAYER_ELEMENT); - element_each(normalized.root, - [&](Element& element) - { - if (element.type != ElementType::LAYER_ANIMATION) return; - if (auto it = remap.find(element.layerId); it != remap.end()) element.layerId = it->second; - }); - } - - return normalized; - } -} diff --git a/src/anm2/xml.cpp b/src/anm2/xml.cpp deleted file mode 100644 index 728bd37..0000000 --- a/src/anm2/xml.cpp +++ /dev/null @@ -1,555 +0,0 @@ -#include "internal.hpp" - -using namespace anm2ed::util; -using namespace tinyxml2; - -namespace anm2ed -{ -#define ANM2_STRING_ATTRIBUTES \ - X("Name", name) \ - X("CreatedBy", createdBy) \ - X("CreatedOn", createdOn) \ - X("Binding", binding) \ - X("Value", value) \ - X("DefaultAnimation", defaultAnimation) - -#define ANM2_PATH_ATTRIBUTES \ - X("Path", path) \ - X("Vertex", vertex) \ - X("Fragment", fragment) - -#define ANM2_INT_ATTRIBUTES \ - X("Id", id) \ - X("LayerId", layerId) \ - X("NullId", nullId) \ - X("SpritesheetId", spritesheetId) \ - X("Fps", fps) \ - X("Version", version) \ - X("FrameNum", frameNum) \ - X("Delay", duration) \ - X("AtFrame", atFrame) \ - X("EventId", eventId) \ - X("RegionId", regionId) \ - X("ShaderId", shaderId) \ - X("SoundId", soundId) \ - X("GroupId", groupId) \ - X("Index", index) - -#define ANM2_BOOL_ATTRIBUTES \ - X("Loop", isLoop) \ - X("Visible", isVisible) \ - X("ShowRect", isShowRect) \ - X("IsExpanded", isExpanded) \ - X("Enabled", isEnabled) - -#define ANM2_FLOAT_ATTRIBUTES \ - X("Rotation", rotation) \ - X("XPivot", pivot.x) \ - X("YPivot", pivot.y) \ - X("XCrop", crop.x) \ - X("YCrop", crop.y) \ - X("XPosition", position.x) \ - X("YPosition", position.y) \ - X("Width", size.x) \ - X("Height", size.y) \ - X("XScale", scale.x) \ - X("YScale", scale.y) \ - X("ShearX", shear.x) \ - X("ShearY", shear.y) - -#define ANM2_COLOR_ATTRIBUTES \ - X("RedTint", tint.r) \ - X("GreenTint", tint.g) \ - X("BlueTint", tint.b) \ - X("AlphaTint", tint.a) \ - X("RedOffset", colorOffset.r) \ - X("GreenOffset", colorOffset.g) \ - X("BlueOffset", colorOffset.b) - - 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) - { - if (!element) return false; - if (auto value = element->Attribute(name); value) - { - out = value; - return true; - } - return false; - } - - bool path_query(const XMLElement* element, const char* name, std::filesystem::path& out) - { - std::string value{}; - if (!string_query(element, name, value)) return false; - out = path::from_utf8(value); - return true; - } - - float color_read(const XMLElement* element, const char* name, float fallback) - { - int value{}; - if (!element || element->QueryIntAttribute(name, &value) != XML_SUCCESS) return fallback; - return math::uint8_to_float(value); - } - - int color_write(float value) { return math::float_to_uint8(value); } - - Interpolation interpolation_read(const XMLElement* element, const char* attribute) - { - bool isFallback{}; - if (element) element->QueryBoolAttribute(attribute, &isFallback); - - auto value = element ? element->Attribute(attribute) : nullptr; - if (!value) return Interpolation::NONE; - - for (std::size_t i = 0; i < INTERPOLATION_VALUES.size(); ++i) - if (!INTERPOLATION_VALUES[i].empty() && INTERPOLATION_VALUES[i] == value) return (Interpolation)i; - - return isFallback ? Interpolation::LINEAR : Interpolation::NONE; - } - - Interpolation interpolation_read(const XMLElement* element) { return interpolation_read(element, "Interpolated"); } - - Origin origin_read(const XMLElement* element) - { - auto value = element ? element->Attribute("Origin") : nullptr; - if (!value) return Origin::CUSTOM; - - for (std::size_t i = 0; i < ORIGIN_VALUES.size(); ++i) - if (!ORIGIN_VALUES[i].empty() && ORIGIN_VALUES[i] == value) return (Origin)i; - - return Origin::CUSTOM; - } - - void element_attributes_read(Element& out, const XMLElement* element) - { -#define X(attribute, member) string_query(element, attribute, out.member); - ANM2_STRING_ATTRIBUTES -#undef X -#define X(attribute, member) path_query(element, attribute, out.member); - ANM2_PATH_ATTRIBUTES -#undef X -#define X(attribute, member) element->QueryIntAttribute(attribute, &out.member); - ANM2_INT_ATTRIBUTES -#undef X -#define X(attribute, member) element->QueryBoolAttribute(attribute, &out.member); - ANM2_BOOL_ATTRIBUTES -#undef X -#define X(attribute, member) element->QueryFloatAttribute(attribute, &out.member); - ANM2_FLOAT_ATTRIBUTES -#undef X -#define X(attribute, member) out.member = color_read(element, attribute, out.member); - 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 = {}; - if (out.type == ElementType::REGION && out.origin == Origin::CENTER) out.pivot = out.size * 0.5f; - } - - bool bake_attributes_read(const XMLElement* element, Interpolation& interpolation, int& bakeDelay) - { - if (!element || !element->Attribute("BakeInterpolation")) return false; - - interpolation = interpolation_read(element, "BakeInterpolation"); - element->QueryIntAttribute("BakeDelay", &bakeDelay); - return bakeDelay >= FRAME_DURATION_MIN; - } - - const XMLElement* bake_frames_skip(const XMLElement* element, int bakeDelay) - { - for (int i = 0; element && i < bakeDelay; ++i) - element = element->NextSiblingElement(); - return element; - } - - void element_children_read(Element& out, const XMLElement* element) - { - auto child = element->FirstChildElement(); - while (child) - { - auto childName = std::string_view(child->Name() ? child->Name() : ""); - if (childName == LEGACY_OVERLAY_TAG) - { - child = child->NextSiblingElement(); - continue; - } - - auto childType = element_type_get(childName); - if (childType == ElementType::FRAME && is_track(out)) - { - Interpolation bakeInterpolation{}; - int bakeDelay{}; - if (bake_attributes_read(child, bakeInterpolation, bakeDelay)) - { - auto restored = element_read(child); - restored.interpolation = bakeInterpolation; - restored.duration = bakeDelay; - if (is_track_child_valid(out.type, restored.type)) out.children.push_back(std::move(restored)); - child = bake_frames_skip(child, bakeDelay); - continue; - } - } - - auto childElement = element_read(child); - if (is_track_child_valid(out.type, childElement.type)) out.children.push_back(std::move(childElement)); - child = child->NextSiblingElement(); - } - } - - Element element_read(const XMLElement* element) - { - Element out{}; - if (!element) return out; - - out.tag = element->Name() ? element->Name() : ""; - out.type = element_type_get(out.tag); - element_attributes_read(out, element); - - element_children_read(out, element); - - if (out.type == ElementType::TRIGGER && out.soundId != -1) - { - bool isSoundFound{}; - for (const auto& child : out.children) - if (child.type == ElementType::SOUND_ELEMENT && child.id == out.soundId) isSoundFound = true; - - if (!isSoundFound) - { - auto sound = element_make(ElementType::SOUND_ELEMENT); - sound.id = out.soundId; - out.children.insert(out.children.begin(), sound); - } - } - - if (out.type == ElementType::TRIGGER) - for (const auto& child : out.children) - if (child.type == ElementType::SOUND_ELEMENT && child.id != -1) out.soundIds.push_back(child.id); - - return out; - } - - struct WriteRule - { - ElementType type; - Flag flag; - 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}}; - - struct XmlSink : ElementSink - { - XMLDocument& document; - std::vector stack{}; - XMLElement* root{}; - - explicit XmlSink(XMLDocument& document) : document(document) {} - - void open(std::string_view tag) override - { - auto element = document.NewElement(std::string(tag).c_str()); - if (stack.empty()) - root = element; - else - stack.back()->InsertEndChild(element); - stack.push_back(element); - } - - void attribute(const char* name, const char* value) override { stack.back()->SetAttribute(name, value); } - 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(); } - }; - - bool is_nested_group_parent(ElementType parentType, Flags flags) - { - return has_flag(flags, SERIALIZE_NESTED_GROUPS) && group_child_type_get(parentType) != ElementType::UNKNOWN; - } - - bool is_group_id_serialized(Flags flags) - { - return has_flag(flags, SERIALIZE_GROUPS) && !has_flag(flags, SERIALIZE_NESTED_GROUPS); - } - - bool element_write_skip(const Element& element, ElementType parentType, Flags flags) - { - 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()); - return false; - } - - bool is_frame_bake_serialized(const Element& frame, Flags flags) - { - return has_flag(flags, SERIALIZE_BAKE_SPECIAL_INTERPOLATED_FRAMES) && frame.type == ElementType::FRAME && - frame.interpolation != Interpolation::NONE && frame.interpolation != Interpolation::LINEAR; - } - - void path_emit(ElementSink& sink, const char* name, const std::filesystem::path& value) - { - if (!value.empty()) sink.attribute(name, path::to_utf8(value).c_str()); - } - - void interpolation_emit(ElementSink& sink, const char* name, Interpolation interpolation) - { - auto value = INTERPOLATION_VALUES[(std::size_t)interpolation]; - if (interpolation == Interpolation::NONE || interpolation == Interpolation::LINEAR) - sink.attribute(name, interpolation == Interpolation::LINEAR); - else if (!value.empty()) - sink.attribute(name, value.data()); - } - - void id_name_emit(ElementSink& sink, const Element& element) - { - sink.attribute("Id", element.id); - sink.attribute("Name", element.name.c_str()); - } - - void binding_value_emit(ElementSink& sink, const Element& element) - { - if (!element.binding.empty()) sink.attribute("Binding", element.binding.c_str()); - if (!element.value.empty()) sink.attribute("Value", element.value.c_str()); - } - - void group_id_emit(ElementSink& sink, const Element& element, Flags flags) - { - if (element.groupId != -1 && is_group_id_serialized(flags)) sink.attribute("GroupId", element.groupId); - } - - void crop_size_emit(ElementSink& sink, const Element& element) - { - sink.attribute("XCrop", element.crop.x); - sink.attribute("YCrop", element.crop.y); - sink.attribute("Width", element.size.x); - sink.attribute("Height", element.size.y); - } - - void frame_attributes_emit(ElementSink& sink, const Element& element, ElementType parentType, Flags flags) - { - if (parentType == ElementType::LAYER_ANIMATION) - { - bool isHasValidRegion = has_flag(flags, SERIALIZE_REGIONS) && element.regionId != -1; - if (isHasValidRegion) sink.attribute("RegionId", element.regionId); - if (has_flag(flags, SERIALIZE_REDUNDANT_FRAME_REGION_VALUES) || !isHasValidRegion) - { - sink.attribute("XPivot", element.pivot.x); - sink.attribute("YPivot", element.pivot.y); - crop_size_emit(sink, element); - } - } - - sink.attribute("XPosition", element.position.x); - sink.attribute("YPosition", element.position.y); - sink.attribute("Delay", element.duration); - sink.attribute("Visible", element.isVisible); - sink.attribute("XScale", element.scale.x); - sink.attribute("YScale", element.scale.y); - if (has_flag(flags, SERIALIZE_EXTENSIONS)) - { - sink.attribute("ShearX", element.shear.x); - sink.attribute("ShearY", element.shear.y); - } - sink.attribute("RedTint", color_write(element.tint.r)); - sink.attribute("GreenTint", color_write(element.tint.g)); - sink.attribute("BlueTint", color_write(element.tint.b)); - sink.attribute("AlphaTint", color_write(element.tint.a)); - sink.attribute("RedOffset", color_write(element.colorOffset.r)); - sink.attribute("GreenOffset", color_write(element.colorOffset.g)); - sink.attribute("BlueOffset", color_write(element.colorOffset.b)); - sink.attribute("Rotation", element.rotation); - interpolation_emit(sink, "Interpolated", element.interpolation); - if (has_flag(flags, SERIALIZE_EXTENSIONS) && element.shaderId != -1) sink.attribute("ShaderId", element.shaderId); - } - - void element_attributes_emit(ElementSink& sink, const Element& element, ElementType parentType, Flags flags) - { - switch (element.type) - { - case ElementType::INFO: - sink.attribute("CreatedBy", element.createdBy.c_str()); - sink.attribute("CreatedOn", element.createdOn.c_str()); - sink.attribute("Fps", element.fps); - sink.attribute("Version", element.version); - break; - case ElementType::ANIMATIONS: - sink.attribute("DefaultAnimation", element.defaultAnimation.c_str()); - break; - case ElementType::SPRITESHEET: - sink.attribute("Id", element.id); - path_emit(sink, "Path", element.path); - break; - case ElementType::SHADER: - id_name_emit(sink, element); - path_emit(sink, "Vertex", element.vertex); - path_emit(sink, "Fragment", element.fragment); - break; - case ElementType::UNIFORM: - sink.attribute("Name", element.name.c_str()); - binding_value_emit(sink, element); - break; - case ElementType::COMPONENT: - sink.attribute("Index", element.index); - binding_value_emit(sink, element); - break; - case ElementType::REGION: - { - id_name_emit(sink, element); - crop_size_emit(sink, element); - auto origin = ORIGIN_VALUES[(std::size_t)element.origin]; - if (!origin.empty()) - sink.attribute("Origin", origin.data()); - else - { - sink.attribute("XPivot", element.pivot.x); - sink.attribute("YPivot", element.pivot.y); - } - break; - } - case ElementType::LAYER_ELEMENT: - id_name_emit(sink, element); - sink.attribute("SpritesheetId", element.spritesheetId); - break; - case ElementType::NULL_ELEMENT: - id_name_emit(sink, element); - if (element.isShowRect) sink.attribute("ShowRect", element.isShowRect); - break; - case ElementType::GROUP: - if (!is_nested_group_parent(parentType, flags)) sink.attribute("Id", element.id); - sink.attribute("Name", element.name.c_str()); - sink.attribute("IsExpanded", element.isExpanded); - sink.attribute("Visible", element.isVisible); - if ((parentType == ElementType::LAYER_ANIMATION_GROUPS || parentType == ElementType::NULL_ANIMATION_GROUPS) && - element.index != -1) - sink.attribute("Index", element.index); - break; - case ElementType::EVENT_ELEMENT: - id_name_emit(sink, element); - break; - case ElementType::SOUND_ELEMENT: - sink.attribute("Id", element.id); - if (parentType != ElementType::TRIGGER) path_emit(sink, "Path", element.path); - break; - case ElementType::ANIMATION: - sink.attribute("Name", element.name.c_str()); - sink.attribute("FrameNum", element.frameNum); - sink.attribute("Loop", element.isLoop); - group_id_emit(sink, element, flags); - break; - case ElementType::LAYER_ANIMATION: - case ElementType::NULL_ANIMATION: - sink.attribute(element.type == ElementType::LAYER_ANIMATION ? "LayerId" : "NullId", track_id_get(element)); - sink.attribute("Visible", element.isVisible); - group_id_emit(sink, element, flags); - break; - case ElementType::FRAME: - frame_attributes_emit(sink, element, parentType, flags); - break; - case ElementType::TRIGGER: - if (element.eventId != -1) sink.attribute("EventId", element.eventId); - sink.attribute("AtFrame", element.atFrame); - break; - default: - break; - } - } - - void element_emit(ElementSink& sink, const Element& element, ElementType parentType, Flags flags, - std::optional bake) - { - 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]; - if (!value.empty()) sink.attribute("BakeInterpolation", value.data()); - sink.attribute("BakeDelay", bake->delay); - } - sink.body(); - - if (element.type == ElementType::TRIGGER && has_flag(flags, SERIALIZE_SOUNDS)) - for (auto soundId : element.soundIds) - { - if (soundId == -1) continue; - auto sound = element_make(ElementType::SOUND_ELEMENT); - sound.id = soundId; - element_emit(sink, sound, ElementType::TRIGGER, flags); - } - - for (int i = 0; i < (int)element.children.size(); ++i) - { - const auto& child = element.children[i]; - if (element_write_skip(child, element.type, flags)) continue; - if (!is_track(element) || !is_frame_bake_serialized(child, flags)) - { - element_emit(sink, child, element.type, flags); - continue; - } - - 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); - for (int bakeIndex = 0; bakeIndex < (int)baked.size(); ++bakeIndex) - element_emit(sink, baked[bakeIndex], element.type, flags, - bakeIndex == 0 ? std::optional({child.interpolation, (int)baked.size()}) - : std::nullopt); - } - sink.close(); - } - - XMLElement* element_to_xml(XMLDocument& document, const Element& element, ElementType parentType, Flags flags) - { - XmlSink sink(document); - element_emit(sink, element, parentType, flags); - return sink.root; - } - - std::string element_to_string(const Element& element, ElementType parentType, Flags flags) - { - XMLDocument document{}; - document.InsertEndChild(element_to_xml(document, element, parentType, flags)); - return xml::document_to_string(document); - } - - std::string element_to_string(const Element& element, Flags flags) - { - return element_to_string(element, ElementType::UNKNOWN, flags); - } - -} diff --git a/src/edit/edit.hpp b/src/edit/edit.hpp index f923810..6dd5b63 100644 --- a/src/edit/edit.hpp +++ b/src/edit/edit.hpp @@ -73,7 +73,6 @@ namespace anm2ed::edit Uids triggers_sort(model::Model&, Reference); Uids frames_paste(model::Model&, Reference, const std::set&, const std::string&, int, std::string*); Uids root_bake_into(model::Model&, const std::set&, const std::set&, RootBakeOptions); - void frame_root_transform_apply(model::Frame&, const model::Frame&, bool, bool, bool); Uids items_remove(model::Model&, int, const std::map>&, const std::map>&); Uids items_group(model::Model&, int, int, const std::set&, const std::string&); diff --git a/src/edit/frames.cpp b/src/edit/frames.cpp index ebb02e3..94223aa 100644 --- a/src/edit/frames.cpp +++ b/src/edit/frames.cpp @@ -273,26 +273,6 @@ namespace anm2ed::edit return uids; } - void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, bool isRoundScale, bool isRoundRotation, - bool isUseRootPivot) - { - auto rootScale = math::percent_to_unit(rootFrame.scale); - auto pivot = isUseRootPivot ? rootFrame.position : glm::vec2(); - auto offset = (frame.position - pivot) * rootScale; - auto radians = glm::radians(rootFrame.rotation); - auto cos = std::cos(radians); - auto sin = std::sin(radians); - - frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos); - frame.scale *= rootScale; - frame.rotation += rootFrame.rotation; - frame.tint *= rootFrame.tint; - frame.colorOffset += rootFrame.colorOffset; - - if (isRoundScale) frame.scale = glm::round(frame.scale); - if (isRoundRotation) frame.rotation = std::round(frame.rotation); - } - // Applies the selected root frames' transforms to the target tracks' frames under them, then resets those root // frames to a default frame spanning the same time. Uids root_bake_into(Model& model, const std::set& rootFrames, const std::set& targets, diff --git a/src/model/common.hpp b/src/model/common.hpp index 631d883..b1d551e 100644 --- a/src/model/common.hpp +++ b/src/model/common.hpp @@ -12,55 +12,49 @@ #include "types.hpp" #define ANM2_ELEMENT_TYPES \ - X(UNKNOWN, "", UNKNOWN) \ - X(ANIMATED_ACTOR, "AnimatedActor", UNKNOWN) \ - X(INFO, "Info", UNKNOWN) \ - X(CONTENT, "Content", UNKNOWN) \ - X(SPRITESHEETS, "Spritesheets", UNKNOWN) \ - X(SPRITESHEET, "Spritesheet", SPRITESHEETS) \ - X(SHADERS, "Shaders", UNKNOWN) \ - X(SHADER, "Shader", SHADERS) \ - X(UNIFORM, "Uniform", UNKNOWN) \ - X(COMPONENT, "Component", UNKNOWN) \ - X(REGION, "Region", UNKNOWN) \ - X(LAYERS, "Layers", UNKNOWN) \ - X(LAYER_ELEMENT, "Layer", LAYERS) \ - X(NULLS, "Nulls", UNKNOWN) \ - X(NULL_ELEMENT, "Null", NULLS) \ - X(EVENTS, "Events", UNKNOWN) \ - X(EVENT_ELEMENT, "Event", EVENTS) \ - X(SOUNDS, "Sounds", UNKNOWN) \ - X(SOUND_ELEMENT, "Sound", SOUNDS) \ - X(ANIMATIONS, "Animations", UNKNOWN) \ - X(ANIMATION, "Animation", ANIMATIONS) \ - X(ROOT_ANIMATION, "RootAnimation", UNKNOWN) \ - X(LAYER_ANIMATIONS, "LayerAnimations", UNKNOWN) \ - X(LAYER_ANIMATION_GROUPS, "LayerAnimationGroups", UNKNOWN) \ - X(LAYER_ANIMATION, "LayerAnimation", UNKNOWN) \ - X(NULL_ANIMATIONS, "NullAnimations", UNKNOWN) \ - X(NULL_ANIMATION_GROUPS, "NullAnimationGroups", UNKNOWN) \ - X(NULL_ANIMATION, "NullAnimation", UNKNOWN) \ - X(GROUP, "Group", UNKNOWN) \ - X(TRIGGERS, "Triggers", UNKNOWN) \ - X(FRAME, "Frame", UNKNOWN) \ - X(TRIGGER, "Trigger", UNKNOWN) + X(UNKNOWN, "") \ + X(ANIMATED_ACTOR, "AnimatedActor") \ + X(INFO, "Info") \ + X(CONTENT, "Content") \ + X(SPRITESHEETS, "Spritesheets") \ + X(SPRITESHEET, "Spritesheet") \ + X(SHADERS, "Shaders") \ + X(SHADER, "Shader") \ + X(UNIFORM, "Uniform") \ + X(COMPONENT, "Component") \ + X(REGION, "Region") \ + X(LAYERS, "Layers") \ + X(LAYER_ELEMENT, "Layer") \ + X(NULLS, "Nulls") \ + X(NULL_ELEMENT, "Null") \ + X(EVENTS, "Events") \ + X(EVENT_ELEMENT, "Event") \ + X(SOUNDS, "Sounds") \ + X(SOUND_ELEMENT, "Sound") \ + X(ANIMATIONS, "Animations") \ + X(ANIMATION, "Animation") \ + X(ROOT_ANIMATION, "RootAnimation") \ + X(LAYER_ANIMATIONS, "LayerAnimations") \ + X(LAYER_ANIMATION_GROUPS, "LayerAnimationGroups") \ + X(LAYER_ANIMATION, "LayerAnimation") \ + X(NULL_ANIMATIONS, "NullAnimations") \ + X(NULL_ANIMATION_GROUPS, "NullAnimationGroups") \ + X(NULL_ANIMATION, "NullAnimation") \ + X(GROUP, "Group") \ + X(TRIGGERS, "Triggers") \ + X(FRAME, "Frame") \ + X(TRIGGER, "Trigger") namespace anm2ed { enum class ElementType { -#define X(symbol, tag, container) symbol, +#define X(symbol, tag) symbol, ANM2_ELEMENT_TYPES #undef X COUNT }; - inline constexpr ElementType ELEMENT_CONTAINERS[] = { -#define X(symbol, tag, container) ElementType::container, - ANM2_ELEMENT_TYPES -#undef X - }; - enum class Interpolation { NONE, @@ -129,60 +123,38 @@ namespace anm2ed inline const glm::vec4 TRIGGER_COLOR_HOVERED = glm::vec4(0.950f, 0.330f, 0.490f, 1.000f); #define ANM2_ITEM_TYPES \ - X(NONE, STRING_UNDEFINED, "", resource::icon::NONE, glm::vec4(), glm::vec4(), glm::vec4(), UNKNOWN, UNKNOWN) \ - X(ROOT, BASIC_ROOT, "RootAnimation", resource::icon::ROOT, ROOT_COLOR, ROOT_COLOR_ACTIVE, ROOT_COLOR_HOVERED, \ - ROOT_ANIMATION, UNKNOWN) \ - X(LAYER, BASIC_LAYER_ANIMATION, "LayerAnimation", resource::icon::LAYER, LAYER_COLOR, LAYER_COLOR_ACTIVE, \ - LAYER_COLOR_HOVERED, LAYER_ANIMATION, LAYER_ANIMATIONS) \ - X(NULL_, BASIC_NULL_ANIMATION, "NullAnimation", resource::icon::NULL_, NULL_COLOR, NULL_COLOR_ACTIVE, \ - NULL_COLOR_HOVERED, NULL_ANIMATION, NULL_ANIMATIONS) \ - X(TRIGGER, BASIC_TRIGGERS, "Triggers", resource::icon::TRIGGERS, TRIGGER_COLOR, TRIGGER_COLOR_ACTIVE, \ - TRIGGER_COLOR_HOVERED, TRIGGERS, UNKNOWN) + X(NONE, STRING_UNDEFINED, resource::icon::NONE, glm::vec4(), glm::vec4(), glm::vec4()) \ + X(ROOT, BASIC_ROOT, resource::icon::ROOT, ROOT_COLOR, ROOT_COLOR_ACTIVE, ROOT_COLOR_HOVERED) \ + X(LAYER, BASIC_LAYER_ANIMATION, resource::icon::LAYER, LAYER_COLOR, LAYER_COLOR_ACTIVE, LAYER_COLOR_HOVERED) \ + X(NULL_, BASIC_NULL_ANIMATION, resource::icon::NULL_, NULL_COLOR, NULL_COLOR_ACTIVE, NULL_COLOR_HOVERED) \ + X(TRIGGER, BASIC_TRIGGERS, resource::icon::TRIGGERS, TRIGGER_COLOR, TRIGGER_COLOR_ACTIVE, TRIGGER_COLOR_HOVERED) constexpr StringType TYPE_STRINGS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) string, - ANM2_ITEM_TYPES -#undef X - }; - - constexpr const char* TYPE_ITEM_STRINGS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) itemString, +#define X(symbol, string, icon, color, colorActive, colorHovered) string, ANM2_ITEM_TYPES #undef X }; constexpr resource::icon::Type TYPE_ICONS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) icon, +#define X(symbol, string, icon, color, colorActive, colorHovered) icon, ANM2_ITEM_TYPES #undef X }; inline const glm::vec4 TYPE_COLOR[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) color, +#define X(symbol, string, icon, color, colorActive, colorHovered) color, ANM2_ITEM_TYPES #undef X }; inline const glm::vec4 TYPE_COLOR_ACTIVE[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) colorActive, +#define X(symbol, string, icon, color, colorActive, colorHovered) colorActive, ANM2_ITEM_TYPES #undef X }; inline const glm::vec4 TYPE_COLOR_HOVERED[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) colorHovered, - ANM2_ITEM_TYPES -#undef X - }; - - inline constexpr ElementType TYPE_TRACKS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) ElementType::track, - ANM2_ITEM_TYPES -#undef X - }; - - inline constexpr ElementType TYPE_CONTAINERS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) ElementType::container, +#define X(symbol, string, icon, color, colorActive, colorHovered) colorHovered, ANM2_ITEM_TYPES #undef X }; @@ -236,7 +208,6 @@ namespace anm2ed struct Options { - Flags flags{SERIALIZE_DEFAULT}; bool isExtendedFormat{}; }; diff --git a/src/model/frames.cpp b/src/model/frames.cpp index 00ddcbe..d1ae73b 100644 --- a/src/model/frames.cpp +++ b/src/model/frames.cpp @@ -44,6 +44,26 @@ namespace anm2ed::model math::percent_to_unit(frame.shear)); } + void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, bool isRoundScale, bool isRoundRotation, + bool isUseRootPivot) + { + auto rootScale = math::percent_to_unit(rootFrame.scale); + auto pivot = isUseRootPivot ? rootFrame.position : glm::vec2(); + auto offset = (frame.position - pivot) * rootScale; + auto radians = glm::radians(rootFrame.rotation); + auto cos = std::cos(radians); + auto sin = std::sin(radians); + + frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos); + frame.scale *= rootScale; + frame.rotation += rootFrame.rotation; + frame.tint *= rootFrame.tint; + frame.colorOffset += rootFrame.colorOffset; + + if (isRoundScale) frame.scale = glm::round(frame.scale); + if (isRoundRotation) frame.rotation = std::round(frame.rotation); + } + // The track's state at a time: the frame playing then, mixed toward the next one when interpolated. A trigger is // returned only on its exact frame; otherwise the result is invisible. Frame frame_generate(const Track& track, float time) diff --git a/src/model/frames.hpp b/src/model/frames.hpp index 4a2bca2..73fb74d 100644 --- a/src/model/frames.hpp +++ b/src/model/frames.hpp @@ -10,6 +10,7 @@ namespace anm2ed::model void frame_mix(Frame&, const Frame&, float); std::vector frame_bake_split(const Frame&, const Frame&, int, bool, bool); glm::mat4 frame_parent_model_get(const Frame&); + void frame_root_transform_apply(Frame&, const Frame&, bool, bool, bool); Frame frame_generate(const Track&, float); int frame_index_from_at_frame_get(const Track&, int); int frame_index_from_time_get(const Track&, float); diff --git a/src/model/xml.cpp b/src/model/xml.cpp index 804bed3..c087695 100644 --- a/src/model/xml.cpp +++ b/src/model/xml.cpp @@ -1,132 +1,61 @@ #include "xml.hpp" +#include +#include #include +#include +#include -#include "anm2/internal.hpp" +#include + +#include "file.hpp" +#include "frames.hpp" +#include "math.hpp" +#include "path.hpp" +#include "time.hpp" +#include "util/xml.hpp" using namespace tinyxml2; +using namespace anm2ed::util; namespace anm2ed::model { - constexpr std::size_t XML_NODE_BUFFER_SIZE = 64; - - // Records an emitted element as an XmlNode, attribute values formatted exactly as the XML writer formats them. - struct XmlNodeSink : ElementSink - { - std::vector stack{}; - XmlNode root{}; - - void open(std::string_view tag) override - { - auto& node = stack.empty() ? root : stack.back()->children.emplace_back(); - node.tag = std::string(tag); - stack.push_back(&node); - } - - template void formatted_add(const char* name, T value) - { - char buffer[XML_NODE_BUFFER_SIZE]{}; - XMLUtil::ToStr(value, buffer, sizeof(buffer)); - attribute(name, (const char*)buffer); - } - - void attribute(const char* name, const char* value) override { stack.back()->attributes.emplace_back(name, value); } - void attribute(const char* name, int value) override { formatted_add(name, value); } - void attribute(const char* name, bool value) override { formatted_add(name, value); } - void attribute(const char* name, float value) override { formatted_add(name, value); } - void text(const char* value) override { stack.back()->text = value; } - void close() override { stack.pop_back(); } + constexpr std::string_view ELEMENT_TAGS[] = { +#define X(symbol, tag) tag, + ANM2_ELEMENT_TYPES +#undef X }; + constexpr std::string_view INTERPOLATION_VALUES[] = {"", "", "EaseIn", "EaseOut", "EaseInOut"}; + constexpr std::string_view ORIGIN_VALUES[] = {"", "TopLeft", "Center"}; + constexpr const char* TINT_ATTRIBUTES[] = {"RedTint", "GreenTint", "BlueTint", "AlphaTint"}; + constexpr const char* OFFSET_ATTRIBUTES[] = {"RedOffset", "GreenOffset", "BlueOffset"}; + // Attributes the editor reads (or older builds wrote); any other attribute is kept verbatim as Extras. + // clang-format off + constexpr std::string_view KNOWN_ATTRIBUTES[] = { + "Name", "CreatedBy", "CreatedOn", "Binding", "Value", "DefaultAnimation", "Path", "Vertex", "Fragment", "Id", + "LayerId", "NullId", "SpritesheetId", "Fps", "Version", "FrameNum", "Delay", "AtFrame", "EventId", "RegionId", + "ShaderId", "SoundId", "GroupId", "Index", "Loop", "Visible", "ShowRect", "IsExpanded", "Enabled", "Rotation", + "XPivot", "YPivot", "XCrop", "YCrop", "XPosition", "YPosition", "Width", "Height", "XScale", "YScale", "ShearX", + "ShearY", "RedTint", "GreenTint", "BlueTint", "AlphaTint", "RedOffset", "GreenOffset", "BlueOffset", + "Interpolated", "Origin", "BakeInterpolation", "BakeDelay", "BakeCount", "OriginalDelay"}; + // clang-format on + constexpr std::string_view SOURCE_DOCUMENT_TAG = "SourceDocument"; + constexpr std::string_view FRAME_RESTORE_TAG = "FrameRestore"; + constexpr std::string_view LEGACY_OVERLAY_TAG = "Overlay"; + constexpr std::string_view LEGACY_GROUPS_TAG = "Groups"; + constexpr const char* CREATED_ON_FORMAT = "%m/%d/%Y %I:%M:%S %p"; + constexpr const char* SHADER_NAME_DEFAULT = "New Shader"; + constexpr std::size_t VALUE_BUFFER_SIZE = 64; + constexpr std::uint64_t HASH_OFFSET = 14695981039346656037ull; + constexpr std::uint64_t HASH_PRIME = 1099511628211ull; + constexpr Flags CLIPBOARD_FLAGS = SERIALIZE_EDITOR_DEFAULT; - XmlNode node_get(const Element& element, ElementType parentType) + const char* tag_get(ElementType type) { return ELEMENT_TAGS[(int)type].data(); } + + ElementType type_get(const XMLElement* element) { - XmlNodeSink sink{}; - element_emit(sink, element, parentType, SERIALIZE_ANM2ED_DEFAULT); - return sink.root; - } - - Element node_element_get(const XmlNode& node) - { - Element element{}; - element.tag = node.tag; - element.extraAttributes = node.attributes; - element.text = node.text; - for (const auto& child : node.children) - element.children.push_back(node_element_get(child)); - return element; - } - - Extras extras_read(const Element& element) { return {element.extraAttributes, element.text, {}}; } - - void extras_write(Element& element, const Extras& extras) - { - element.extraAttributes = extras.attributes; - element.text = extras.text; - for (const auto& [index, node] : extras.children) - element.children.insert(element.children.begin() + std::min(index, (int)element.children.size()), - node_element_get(node)); - } - - // Reads children: `known` returns false for a child it does not take, which is kept as unknown XML. - template void children_read(const Element& element, Extras& extras, Known&& known) - { - for (int i = 0; i < (int)element.children.size(); ++i) - if (!known(element.children[i])) extras.children.emplace_back(i, node_get(element.children[i], element.type)); - } - - Element element_make_with(ElementType type, const Extras& extras) - { - auto element = element_make(type); - extras_write(element, extras); - return element; - } - - Frame frame_read(const Element& element) - { - Frame frame{}; - frame.crop = element.crop; - frame.size = element.size; - frame.pivot = element.pivot; - frame.position = element.position; - frame.scale = element.scale; - frame.shear = element.shear; - frame.rotation = element.rotation; - frame.tint = element.tint; - frame.colorOffset = element.colorOffset; - frame.duration = element.duration; - frame.isVisible = element.isVisible; - frame.interpolation = element.interpolation; - frame.regionId = element.regionId; - frame.shaderId = element.shaderId; - frame.atFrame = element.atFrame; - frame.eventId = element.eventId; - frame.soundIds = element.soundIds; - frame.extras = extras_read(element); - return frame; - } - - Element frame_write(const Frame& frame, ItemType type) - { - auto element = element_make(type == ItemType::TRIGGER ? ElementType::TRIGGER : ElementType::FRAME); - element.crop = frame.crop; - element.size = frame.size; - element.pivot = frame.pivot; - element.position = frame.position; - element.scale = frame.scale; - element.shear = frame.shear; - element.rotation = frame.rotation; - element.tint = frame.tint; - element.colorOffset = frame.colorOffset; - element.duration = frame.duration; - element.isVisible = frame.isVisible; - element.interpolation = frame.interpolation; - element.regionId = frame.regionId; - element.shaderId = frame.shaderId; - element.atFrame = frame.atFrame; - element.eventId = frame.eventId; - element.soundIds = frame.soundIds; - extras_write(element, frame.extras); - return element; + auto it = std::ranges::find(ELEMENT_TAGS, std::string_view(element->Name())); + return it == std::end(ELEMENT_TAGS) ? ElementType::UNKNOWN : (ElementType)(it - std::begin(ELEMENT_TAGS)); } ElementType track_element_type_get(ItemType type) @@ -144,417 +73,513 @@ namespace anm2ed::model } } - Track track_read(const Element& element, ItemType type) + // Reading: tinyxml2 straight into the model. + + int int_get(const XMLElement* element, const char* name, int value = -1) { - Track track{.type = type, .id = track_id_get(element), .isVisible = element.isVisible}; - auto frameType = track_frame_type_get(element); - children_read(element, track.extras, - [&](const Element& child) + element->QueryIntAttribute(name, &value); + return value; + } + + float float_get(const XMLElement* element, const char* name, float value = 0.0f) + { + element->QueryFloatAttribute(name, &value); + return value; + } + + bool bool_get(const XMLElement* element, const char* name, bool value) + { + element->QueryBoolAttribute(name, &value); + return value; + } + + std::string string_get(const XMLElement* element, const char* name, std::string value = {}) + { + if (auto attribute = element->Attribute(name)) value = attribute; + return value; + } + + std::filesystem::path path_get(const XMLElement* element, const char* name) + { + auto attribute = element->Attribute(name); + return attribute ? path::from_utf8(attribute) : std::filesystem::path{}; + } + + float color_get(const XMLElement* element, const char* name, float value) + { + int color{}; + if (element->QueryIntAttribute(name, &color) == XML_SUCCESS) value = math::uint8_to_float(color); + return value; + } + + Interpolation interpolation_read(const XMLElement* element, const char* name) + { + auto value = element->Attribute(name); + if (!value) return Interpolation::NONE; + for (std::size_t i = 0; i < std::size(INTERPOLATION_VALUES); ++i) + if (!INTERPOLATION_VALUES[i].empty() && INTERPOLATION_VALUES[i] == value) return (Interpolation)i; + return bool_get(element, name, false) ? Interpolation::LINEAR : Interpolation::NONE; + } + + // Shader elements are only read where they belong; one anywhere else is dropped. + bool is_child_kept(const XMLNode* parent, const XMLElement* child) + { + std::string_view tag = child->Name(); + if (tag == LEGACY_OVERLAY_TAG || tag == SOURCE_DOCUMENT_TAG) return false; + auto parentElement = parent->ToElement(); + auto parentType = parentElement ? type_get(parentElement) : ElementType::UNKNOWN; + switch (type_get(child)) + { + case ElementType::SHADERS: + return parentType == ElementType::CONTENT; + case ElementType::SHADER: + return parentType == ElementType::SHADERS; + case ElementType::UNIFORM: + return parentType == ElementType::SHADER; + case ElementType::COMPONENT: + return parentType == ElementType::UNIFORM; + default: + return true; + } + } + + // Child elements with their position, skipping legacy Overlay, nested SourceDocument copies and misplaced shaders. + template void children_each(const XMLNode* parent, Callback&& callback) + { + int position{}; + for (auto child = parent->FirstChildElement(); child; child = child->NextSiblingElement()) + if (is_child_kept(parent, child)) callback(child, type_get(child), position++); + } + + XmlNode node_read(const XMLElement* element) + { + XmlNode node{.tag = element->Name()}; + for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next()) + node.attributes.emplace_back(attribute->Name(), attribute->Value()); + if (auto text = element->GetText()) node.text = text; + children_each(element, + [&](const XMLElement* child, ElementType, int) { node.children.push_back(node_read(child)); }); + return node; + } + + Extras extras_read(const XMLElement* element) + { + Extras extras{}; + for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next()) + if (!std::ranges::contains(KNOWN_ATTRIBUTES, std::string_view(attribute->Name()))) + extras.attributes.emplace_back(attribute->Name(), attribute->Value()); + if (auto text = element->GetText()) extras.text = text; + return extras; + } + + void extras_child_add(Extras& extras, const XMLElement* child, int position) + { + extras.children.emplace_back(position, node_read(child)); + } + + Frame frame_read(const XMLElement* element) + { + Frame frame{}; + frame.crop = {float_get(element, "XCrop"), float_get(element, "YCrop")}; + frame.size = {float_get(element, "Width"), float_get(element, "Height")}; + frame.pivot = {float_get(element, "XPivot"), float_get(element, "YPivot")}; + frame.position = {float_get(element, "XPosition"), float_get(element, "YPosition")}; + frame.scale = {float_get(element, "XScale", frame.scale.x), float_get(element, "YScale", frame.scale.y)}; + frame.shear = {float_get(element, "ShearX"), float_get(element, "ShearY")}; + frame.rotation = float_get(element, "Rotation"); + for (int i = 0; i < (int)std::size(TINT_ATTRIBUTES); ++i) + frame.tint[i] = color_get(element, TINT_ATTRIBUTES[i], frame.tint[i]); + for (int i = 0; i < (int)std::size(OFFSET_ATTRIBUTES); ++i) + frame.colorOffset[i] = color_get(element, OFFSET_ATTRIBUTES[i], frame.colorOffset[i]); + frame.duration = int_get(element, "Delay", frame.duration); + frame.isVisible = bool_get(element, "Visible", frame.isVisible); + frame.interpolation = interpolation_read(element, "Interpolated"); + frame.regionId = int_get(element, "RegionId"); + frame.shaderId = int_get(element, "ShaderId"); + frame.atFrame = int_get(element, "AtFrame"); + frame.eventId = int_get(element, "EventId"); + frame.extras = extras_read(element); + if (type_get(element) != ElementType::TRIGGER) return frame; + + // A trigger's sounds are children; older files name one with a SoundId attribute instead. + auto soundId = int_get(element, "SoundId"); + std::vector soundIds{}; + children_each(element, + [&](const XMLElement* child, ElementType type, int) { - if (child.type != frameType) return false; - track.frames.push_back(frame_read(child)); - return true; + if (type == ElementType::SOUND_ELEMENT) soundIds.push_back(int_get(child, "Id")); }); - track.extras.attributes = element.extraAttributes; - track.extras.text = element.text; + if (soundId != -1 && !std::ranges::contains(soundIds, soundId)) soundIds.insert(soundIds.begin(), soundId); + std::ranges::copy_if(soundIds, std::back_inserter(frame.soundIds), [](int id) { return id != -1; }); + return frame; + } + + Track track_read(const XMLElement* element, ItemType type) + { + Track track{.type = type, .isVisible = bool_get(element, "Visible", true), .extras = extras_read(element)}; + if (type == ItemType::LAYER) track.id = int_get(element, "LayerId"); + if (type == ItemType::NULL_) track.id = int_get(element, "NullId"); + auto frameType = type == ItemType::TRIGGER ? ElementType::TRIGGER : ElementType::FRAME; + for (auto child = element->FirstChildElement(); child;) + { + auto childType = type_get(child); + // A frame written baked (BakeInterpolation/BakeDelay) is read back as the one interpolated frame it came from. + if (auto delay = int_get(child, "BakeDelay", 0); + childType == ElementType::FRAME && child->Attribute("BakeInterpolation") && delay >= FRAME_DURATION_MIN) + { + auto frame = frame_read(child); + frame.interpolation = interpolation_read(child, "BakeInterpolation"); + frame.duration = delay; + if (frameType == ElementType::FRAME) track.frames.push_back(std::move(frame)); + for (int i = 0; child && i < delay; ++i) + child = child->NextSiblingElement(); + continue; + } + if (childType == frameType) track.frames.push_back(frame_read(child)); + child = child->NextSiblingElement(); + } return track; } - Element track_write(const Track& track, int groupId) + // A container's children before groups are resolved: a child (with the group it names), a group (with any children + // nested in it), or unknown XML. + template struct ContainerItem { - auto element = element_make(track_element_type_get(track.type)); - if (track.type == ItemType::LAYER) element.layerId = track.id; - if (track.type == ItemType::NULL_) element.nullId = track.id; - element.isVisible = track.isVisible; - element.groupId = groupId; - for (const auto& frame : track.frames) - element.children.push_back(frame_write(frame, track.type)); - extras_write(element, track.extras); - return element; - } + using Group = GroupType; - // In memory the anm2 tracks are flat: a group element is followed (somewhere) by tracks carrying its id. - std::vector track_entries_read(const Element& container, ItemType type, Extras& extras) + std::optional child{}; + std::optional group{}; + std::optional node{}; + int groupId{-1}; + int index{-1}; + std::vector children{}; + std::optional root{}; + std::optional backupRoot{}; + std::vector backupTracks{}; + bool isBackup{}; + }; + + using TrackItem = ContainerItem; + using AnimationItem = ContainerItem, AnimationGroup>; + + auto& group_children_get(TrackGroup& group) { return group.tracks; } + auto& group_children_get(AnimationGroup& group) { return group.animations; } + + // Groups become entries followed by their children: nested children move out after their group, and a child joins a + // group seen before it. Unknown XML becomes extras at its position. + template std::vector entries_build(std::vector items, Extras& extras) { - std::vector entries{}; + int nextGroupId{}; + for (const auto& item : items) + if (item.group) nextGroupId = std::max(nextGroupId, item.group->id + 1); + + std::vector flat{}; + for (auto& item : items) + { + if (!item.group) + { + flat.push_back(std::move(item)); + continue; + } + if (item.group->id < 0) item.group->id = nextGroupId++; + auto groupId = item.group->id; + auto children = std::move(item.children); + flat.push_back(std::move(item)); + for (auto& child : children) + flat.push_back({.child = std::move(child), .groupId = groupId}); + } + + std::vector entries{}; std::map groupEntries{}; - auto trackType = track_element_type_get(type); - children_read(container, extras, - [&](const Element& child) - { - if (child.type == ElementType::GROUP) - { - TrackGroup group{.id = child.id, - .name = child.name, - .isExpanded = child.isExpanded, - .isVisible = child.isVisible, - .extras = extras_read(child)}; - if (auto root = child_first_get(child, ElementType::ROOT_ANIMATION)) - group.root = track_read(*root, ItemType::ROOT); - groupEntries[child.id] = entries.size(); - entries.emplace_back(std::move(group)); - return true; - } - if (child.type != trackType) return false; - auto track = track_read(child, type); - if (auto group = groupEntries.find(child.groupId); group != groupEntries.end()) - std::get(entries[group->second]).tracks.push_back(std::move(track)); - else - entries.emplace_back(std::move(track)); - return true; - }); - extras.attributes = container.extraAttributes; - extras.text = container.text; + for (int i = 0; i < (int)flat.size(); ++i) + { + auto& item = flat[i]; + if (item.node) + extras.children.emplace_back(i, std::move(*item.node)); + else if (item.group) + { + groupEntries[item.group->id] = entries.size(); + entries.emplace_back(std::move(*item.group)); + } + else if (auto group = groupEntries.find(item.groupId); group != groupEntries.end()) + group_children_get(std::get(entries[group->second])).push_back(std::move(*item.child)); + else + entries.emplace_back(std::move(*item.child)); + } return entries; } - Element track_entries_write(const std::vector& entries, ElementType containerType, const Extras& extras) + TrackItem track_group_item_read(const XMLElement* element, ItemType type) { - auto container = element_make(containerType); - for (const auto& entry : entries) - if (auto track = std::get_if(&entry)) - container.children.push_back(track_write(*track, -1)); - else - { - const auto& group = std::get(entry); - auto element = element_make(ElementType::GROUP); - element.id = group.id; - element.name = group.name; - element.isExpanded = group.isExpanded; - element.isVisible = group.isVisible; - element.children.push_back(track_write(group.root, -1)); - extras_write(element, group.extras); - container.children.push_back(std::move(element)); - for (const auto& groupTrack : group.tracks) - container.children.push_back(track_write(groupTrack, group.id)); - } - extras_write(container, extras); - return container; + TrackItem item{.group = TrackGroup{.id = int_get(element, "Id"), + .name = string_get(element, "Name"), + .isExpanded = bool_get(element, "IsExpanded", true), + .isVisible = bool_get(element, "Visible", true), + .extras = extras_read(element)}, + .index = int_get(element, "Index")}; + auto trackType = track_element_type_get(type); + children_each(element, + [&](const XMLElement* child, ElementType childType, int) + { + if (childType == ElementType::ROOT_ANIMATION && !item.root) + item.root = track_read(child, ItemType::ROOT); + else if (childType == trackType) + item.children.push_back(track_read(child, type)); + else if (child->Name() == FRAME_RESTORE_TAG && !item.isBackup) + { + item.isBackup = true; + children_each(child, + [&](const XMLElement* backup, ElementType backupType, int) + { + if (backupType == ElementType::ROOT_ANIMATION && !item.backupRoot) + item.backupRoot = track_read(backup, ItemType::ROOT); + else if (backupType == trackType) + item.backupTracks.push_back(track_read(backup, type)); + }); + } + }); + return item; } - Animation animation_read(const Element& element) + std::vector track_items_read(const XMLElement* container, ItemType type) { - Animation animation{.name = element.name, - .frameNum = element.frameNum, - .isLoop = element.isLoop, - .root = {.type = ItemType::ROOT}, - .layout = {}}; - std::set read{}; - children_read(element, animation.extras, - [&](const Element& child) + std::vector items{}; + children_each(container, + [&](const XMLElement* child, ElementType childType, int) { - if (read.contains(child.type)) return false; - switch (child.type) - { - case ElementType::ROOT_ANIMATION: - animation.root = track_read(child, ItemType::ROOT); - break; - case ElementType::LAYER_ANIMATIONS: - animation.layers = track_entries_read(child, ItemType::LAYER, animation.layersExtras); - break; - case ElementType::NULL_ANIMATIONS: - animation.nulls = track_entries_read(child, ItemType::NULL_, animation.nullsExtras); - break; - case ElementType::TRIGGERS: - animation.triggers = track_read(child, ItemType::TRIGGER); - break; - default: - return false; - } - read.insert(child.type); - animation.layout.push_back(child.type); - return true; + if (childType == ElementType::GROUP) + items.push_back(track_group_item_read(child, type)); + else if (childType == track_element_type_get(type)) + items.push_back({.child = track_read(child, type), .groupId = int_get(child, "GroupId")}); + else + items.push_back({.node = node_read(child)}); }); - animation.extras.attributes = element.extraAttributes; - animation.extras.text = element.text; + return items; + } + + // Track groups as any version wrote them: legacy *AnimationGroups metadata is placed back at its Index, and a group + // whose tracks were written baked (FrameRestore) gets its original root and tracks back. + std::vector track_entries_build(std::vector items, std::vector legacyGroups, + Extras& extras) + { + if (!legacyGroups.empty()) + { + std::set legacyIds{}; + for (const auto& item : legacyGroups) + if (item.group->id >= 0) legacyIds.insert(item.group->id); + std::erase_if(items, [&](const TrackItem& item) { return item.group && legacyIds.contains(item.group->id); }); + std::ranges::stable_sort(legacyGroups, + [](const TrackItem& left, const TrackItem& right) + { + if (left.index == -1 || right.index == -1) return left.index != -1; + return left.index < right.index; + }); + for (auto& legacy : legacyGroups) + { + auto first = std::ranges::find_if(items, [&](const TrackItem& item) + { return item.child && item.groupId == legacy.group->id; }); + auto position = legacy.index >= 0 ? std::min(legacy.index, (int)items.size()) : (int)(first - items.begin()); + items.insert(items.begin() + position, std::move(legacy)); + } + } + + std::set restoredIds{}; + for (auto& item : items) + if (item.group && item.group->id >= 0 && item.isBackup && (item.backupRoot || !item.backupTracks.empty())) + { + if (item.backupRoot) item.root = item.backupRoot; + item.children = std::move(item.backupTracks); + restoredIds.insert(item.group->id); + } + std::erase_if(items, [&](const TrackItem& item) { return item.child && restoredIds.contains(item.groupId); }); + + for (auto& item : items) + if (item.group) item.group->root = item.root.value_or(TrackGroup{}.root); + return entries_build(std::move(items), extras); + } + + Animation animation_read(const XMLElement* element) + { + Animation animation{.name = string_get(element, "Name"), + .frameNum = int_get(element, "FrameNum", 1), + .isLoop = bool_get(element, "Loop", true), + .root = {.type = ItemType::ROOT}, + .layout = {}, + .extras = extras_read(element)}; + std::map> items{}; + std::map> legacyGroups{}; + std::set legacyRead{}; + int skipped{}; + children_each(element, + [&](const XMLElement* child, ElementType type, int position) + { + if (type == ElementType::LAYER_ANIMATION_GROUPS || type == ElementType::NULL_ANIMATION_GROUPS) + { + auto isLayers = type == ElementType::LAYER_ANIMATION_GROUPS; + auto container = isLayers ? ElementType::LAYER_ANIMATIONS : ElementType::NULL_ANIMATIONS; + if (legacyRead.insert(type).second) + children_each(child, + [&](const XMLElement* group, ElementType groupType, int) + { + if (groupType == ElementType::GROUP) + legacyGroups[container].push_back(track_group_item_read( + group, isLayers ? ItemType::LAYER : ItemType::NULL_)); + }); + ++skipped; + return; + } + + auto isKnown = type == ElementType::ROOT_ANIMATION || type == ElementType::LAYER_ANIMATIONS || + type == ElementType::NULL_ANIMATIONS || type == ElementType::TRIGGERS; + if (!isKnown || std::ranges::contains(animation.layout, type)) + return extras_child_add(animation.extras, child, position - skipped); + + animation.layout.push_back(type); + if (type == ElementType::ROOT_ANIMATION) animation.root = track_read(child, ItemType::ROOT); + if (type == ElementType::TRIGGERS) animation.triggers = track_read(child, ItemType::TRIGGER); + if (type == ElementType::LAYER_ANIMATIONS) + { + items[type] = track_items_read(child, ItemType::LAYER); + animation.layersExtras = extras_read(child); + } + if (type == ElementType::NULL_ANIMATIONS) + { + items[type] = track_items_read(child, ItemType::NULL_); + animation.nullsExtras = extras_read(child); + } + }); + + for (auto [type, entries, extras] : + {std::tuple{ElementType::LAYER_ANIMATIONS, &animation.layers, &animation.layersExtras}, + std::tuple{ElementType::NULL_ANIMATIONS, &animation.nulls, &animation.nullsExtras}}) + { + if (!legacyGroups[type].empty() && !std::ranges::contains(animation.layout, type)) + animation.layout.push_back(type); + if (std::ranges::contains(animation.layout, type)) + *entries = track_entries_build(std::move(items[type]), std::move(legacyGroups[type]), *extras); + } return animation; } - // Known children in their recorded order; a part with content that was never in the file is appended. - template - void layout_write(Element& element, const std::vector& layout, - const std::vector>& parts, Write&& write) + std::vector animation_entries_read(const XMLNode* parent, Extras& extras) { - for (auto type : layout) - element.children.push_back(write(type)); - for (auto [type, isContent] : parts) - if (isContent && !std::ranges::contains(layout, type)) element.children.push_back(write(type)); - } - - Element animation_write(const Animation& animation, int groupId) - { - auto element = element_make(ElementType::ANIMATION); - element.name = animation.name; - element.frameNum = animation.frameNum; - element.isLoop = animation.isLoop; - element.groupId = groupId; - layout_write(element, animation.layout, - {{ElementType::ROOT_ANIMATION, !animation.root.frames.empty()}, - {ElementType::LAYER_ANIMATIONS, !animation.layers.empty()}, - {ElementType::NULL_ANIMATIONS, !animation.nulls.empty()}, - {ElementType::TRIGGERS, !animation.triggers.frames.empty()}}, - [&](ElementType type) - { - switch (type) - { - case ElementType::ROOT_ANIMATION: - return track_write(animation.root, -1); - case ElementType::LAYER_ANIMATIONS: - return track_entries_write(animation.layers, type, animation.layersExtras); - case ElementType::NULL_ANIMATIONS: - return track_entries_write(animation.nulls, type, animation.nullsExtras); - default: - return track_write(animation.triggers, -1); - } - }); - extras_write(element, animation.extras); - return element; - } - - AnimationGroup animation_group_read(const Element& element) - { - return {.id = element.id, - .name = element.name, - .isExpanded = element.isExpanded, - .isVisible = element.isVisible, - .extras = extras_read(element)}; - } - - Element animation_group_write(const AnimationGroup& group) - { - auto element = element_make(ElementType::GROUP); - element.id = group.id; - element.name = group.name; - element.isExpanded = group.isExpanded; - element.isVisible = group.isVisible; - extras_write(element, group.extras); - return element; - } - - Animations animations_read(const Element& element) - { - Animations animations{.defaultAnimation = element.defaultAnimation}; - std::map groupEntries{}; - children_read(element, animations.extras, - [&](const Element& child) + std::vector items{}; + children_each(parent, + [&](const XMLElement* child, ElementType type, int) { - if (child.type == ElementType::GROUP) + if (type == ElementType::GROUP) { - groupEntries[child.id] = animations.entries.size(); - animations.entries.emplace_back(animation_group_read(child)); - return true; + AnimationItem item{.group = AnimationGroup{.id = int_get(child, "Id"), + .name = string_get(child, "Name"), + .isExpanded = bool_get(child, "IsExpanded", true), + .isVisible = bool_get(child, "Visible", true), + .extras = extras_read(child)}}; + children_each(child, + [&](const XMLElement* animation, ElementType animationType, int) + { + if (animationType == ElementType::ANIMATION) + item.children.push_back( + std::make_shared(animation_read(animation))); + }); + items.push_back(std::move(item)); } - if (child.type != ElementType::ANIMATION) return false; - auto animation = std::make_shared(animation_read(child)); - if (auto group = groupEntries.find(child.groupId); group != groupEntries.end()) - std::get(animations.entries[group->second]).animations.push_back(animation); + else if (type == ElementType::ANIMATION) + items.push_back({.child = std::make_shared(animation_read(child)), + .groupId = int_get(child, "GroupId")}); else - animations.entries.emplace_back(animation); - return true; + items.push_back({.node = node_read(child)}); }); - animations.extras.attributes = element.extraAttributes; - animations.extras.text = element.text; - return animations; + return entries_build(std::move(items), extras); } - Element animations_write(const Animations& animations) + Region region_read(const XMLElement* element) { - auto element = element_make(ElementType::ANIMATIONS); - element.defaultAnimation = animations.defaultAnimation; - for (const auto& entry : animations.entries) - if (auto animation = std::get_if>(&entry)) - element.children.push_back(animation_write(**animation, -1)); - else - { - const auto& group = std::get(entry); - element.children.push_back(animation_group_write(group)); - for (const auto& groupAnimation : group.animations) - element.children.push_back(animation_write(*groupAnimation, group.id)); - } - extras_write(element, animations.extras); - return element; + Region region{.id = int_get(element, "Id"), + .name = string_get(element, "Name"), + .crop = {float_get(element, "XCrop"), float_get(element, "YCrop")}, + .size = {float_get(element, "Width"), float_get(element, "Height")}, + .pivot = {float_get(element, "XPivot"), float_get(element, "YPivot")}, + .extras = extras_read(element)}; + if (auto origin = element->Attribute("Origin")) + for (std::size_t i = 0; i < std::size(ORIGIN_VALUES); ++i) + if (!ORIGIN_VALUES[i].empty() && ORIGIN_VALUES[i] == origin) region.origin = (Origin)i; + if (region.origin == Origin::TOP_LEFT) region.pivot = {}; + if (region.origin == Origin::CENTER) region.pivot = region.size * 0.5f; + return region; } - Region region_read(const Element& element) + Spritesheet spritesheet_read(const XMLElement* element) { - return {.id = element.id, - .name = element.name, - .crop = element.crop, - .size = element.size, - .pivot = element.pivot, - .origin = element.origin, - .extras = extras_read(element)}; - } - - Element item_write(const Region& region) - { - auto element = element_make(ElementType::REGION); - element.id = region.id; - element.name = region.name; - element.crop = region.crop; - element.size = region.size; - element.pivot = region.pivot; - element.origin = region.origin; - extras_write(element, region.extras); - return element; - } - - Spritesheet spritesheet_read(const Element& element) - { - Spritesheet spritesheet{.id = element.id, .path = element.path}; - children_read(element, spritesheet.extras, - [&](const Element& child) + Spritesheet spritesheet{ + .id = int_get(element, "Id"), .path = path_get(element, "Path"), .extras = extras_read(element)}; + children_each(element, + [&](const XMLElement* child, ElementType type, int position) { - if (child.type != ElementType::REGION) return false; - spritesheet.regions.push_back(region_read(child)); - return true; + if (type == ElementType::REGION) + spritesheet.regions.push_back(region_read(child)); + else + extras_child_add(spritesheet.extras, child, position); }); - spritesheet.extras.attributes = element.extraAttributes; - spritesheet.extras.text = element.text; return spritesheet; } - Element item_write(const Spritesheet& spritesheet) + Shader shader_read(const XMLElement* element) { - auto element = element_make(ElementType::SPRITESHEET); - element.id = spritesheet.id; - element.path = spritesheet.path; - for (const auto& region : spritesheet.regions) - element.children.push_back(item_write(region)); - extras_write(element, spritesheet.extras); - return element; - } - - Component component_read(const Element& element) - { - return {.index = element.index, .binding = element.binding, .value = element.value, .extras = extras_read(element)}; - } - - Uniform uniform_read(const Element& element) - { - Uniform uniform{.name = element.name, .binding = element.binding, .value = element.value}; - children_read(element, uniform.extras, - [&](const Element& child) + Shader shader{.id = int_get(element, "Id"), + .name = string_get(element, "Name"), + .vertex = path_get(element, "Vertex"), + .fragment = path_get(element, "Fragment"), + .extras = extras_read(element)}; + children_each(element, + [&](const XMLElement* child, ElementType type, int position) { - if (child.type != ElementType::COMPONENT) return false; - uniform.components.push_back(component_read(child)); - return true; + if (type != ElementType::UNIFORM) return extras_child_add(shader.extras, child, position); + Uniform uniform{.name = string_get(child, "Name"), + .binding = string_get(child, "Binding"), + .value = string_get(child, "Value"), + .extras = extras_read(child)}; + children_each(child, + [&](const XMLElement* component, ElementType componentType, int componentPosition) + { + if (componentType != ElementType::COMPONENT) + return extras_child_add(uniform.extras, component, componentPosition); + uniform.components.push_back({.index = int_get(component, "Index"), + .binding = string_get(component, "Binding"), + .value = string_get(component, "Value"), + .extras = extras_read(component)}); + }); + shader.uniforms.push_back(std::move(uniform)); }); - uniform.extras.attributes = element.extraAttributes; - uniform.extras.text = element.text; - return uniform; - } - - Shader shader_read(const Element& element) - { - Shader shader{.id = element.id, .name = element.name, .vertex = element.vertex, .fragment = element.fragment}; - children_read(element, shader.extras, - [&](const Element& child) - { - if (child.type != ElementType::UNIFORM) return false; - shader.uniforms.push_back(uniform_read(child)); - return true; - }); - shader.extras.attributes = element.extraAttributes; - shader.extras.text = element.text; return shader; } - Element item_write(const Shader& shader) + template Item item_read(const XMLElement*); + template <> Spritesheet item_read(const XMLElement* element) { return spritesheet_read(element); } + template <> Region item_read(const XMLElement* element) { return region_read(element); } + template <> Shader item_read(const XMLElement* element) { return shader_read(element); } + template <> Layer item_read(const XMLElement* element) { - auto element = element_make(ElementType::SHADER); - element.id = shader.id; - element.name = shader.name; - element.vertex = shader.vertex; - element.fragment = shader.fragment; - for (const auto& uniform : shader.uniforms) - { - auto& uniformElement = element.children.emplace_back(element_make(ElementType::UNIFORM)); - uniformElement.name = uniform.name; - uniformElement.binding = uniform.binding; - uniformElement.value = uniform.value; - for (const auto& component : uniform.components) - { - auto& componentElement = uniformElement.children.emplace_back(element_make(ElementType::COMPONENT)); - componentElement.index = component.index; - componentElement.binding = component.binding; - componentElement.value = component.value; - extras_write(componentElement, component.extras); - } - extras_write(uniformElement, uniform.extras); - } - extras_write(element, shader.extras); - return element; + return {.id = int_get(element, "Id"), + .name = string_get(element, "Name"), + .spritesheetId = int_get(element, "SpritesheetId", 0), + .extras = extras_read(element)}; } - - Layer layer_read(const Element& element) + template <> Null item_read(const XMLElement* element) { - return { - .id = element.id, .name = element.name, .spritesheetId = element.spritesheetId, .extras = extras_read(element)}; + return {.id = int_get(element, "Id"), + .name = string_get(element, "Name"), + .isShowRect = bool_get(element, "ShowRect", false), + .extras = extras_read(element)}; } - - Element item_write(const Layer& layer) + template <> Event item_read(const XMLElement* element) { - auto element = element_make(ElementType::LAYER_ELEMENT); - element.id = layer.id; - element.name = layer.name; - element.spritesheetId = layer.spritesheetId; - extras_write(element, layer.extras); - return element; + return {.id = int_get(element, "Id"), .name = string_get(element, "Name"), .extras = extras_read(element)}; } - - Null null_read(const Element& element) + template <> Sound item_read(const XMLElement* element) { - return {.id = element.id, .name = element.name, .isShowRect = element.isShowRect, .extras = extras_read(element)}; + return {.id = int_get(element, "Id"), .path = path_get(element, "Path"), .extras = extras_read(element)}; } - Element item_write(const Null& null) - { - auto element = element_make(ElementType::NULL_ELEMENT); - element.id = null.id; - element.name = null.name; - element.isShowRect = null.isShowRect; - extras_write(element, null.extras); - return element; - } - - Event event_read(const Element& element) - { - return {.id = element.id, .name = element.name, .extras = extras_read(element)}; - } - - Element item_write(const Event& event) - { - auto element = element_make(ElementType::EVENT_ELEMENT); - element.id = event.id; - element.name = event.name; - extras_write(element, event.extras); - return element; - } - - Sound sound_read(const Element& element) - { - return {.id = element.id, .path = element.path, .extras = extras_read(element)}; - } - - Element item_write(const Sound& sound) - { - auto element = element_make(ElementType::SOUND_ELEMENT); - element.id = sound.id; - element.path = sound.path; - extras_write(element, sound.extras); - return element; - } - - template Item item_read(const Element&); - template <> Spritesheet item_read(const Element& element) { return spritesheet_read(element); } - template <> Region item_read(const Element& element) { return region_read(element); } - template <> Shader item_read(const Element& element) { return shader_read(element); } - template <> Layer item_read(const Element& element) { return layer_read(element); } - template <> Null item_read(const Element& element) { return null_read(element); } - template <> Event item_read(const Element& element) { return event_read(element); } - template <> Sound item_read(const Element& element) { return sound_read(element); } - template constexpr ElementType ITEM_TYPE{}; template <> constexpr ElementType ITEM_TYPE{ElementType::SPRITESHEET}; template <> constexpr ElementType ITEM_TYPE{ElementType::REGION}; @@ -564,206 +589,846 @@ namespace anm2ed::model template <> constexpr ElementType ITEM_TYPE{ElementType::EVENT_ELEMENT}; template <> constexpr ElementType ITEM_TYPE{ElementType::SOUND_ELEMENT}; - template std::vector items_read(const Element& container, Content& content) + template std::vector items_read(const XMLElement* container, Content& content, ElementType type) { std::vector items{}; - Extras extras{extras_read(container)}; - children_read(container, extras, - [&](const Element& child) + auto extras = extras_read(container); + children_each(container, + [&](const XMLElement* child, ElementType childType, int position) { - if (child.type != ITEM_TYPE) return false; - items.push_back(item_read(child)); - return true; + if (childType == ITEM_TYPE) + items.push_back(item_read(child)); + else + extras_child_add(extras, child, position); }); - if (extras != Extras{}) content.containerExtras.emplace_back(container.type, std::move(extras)); + if (extras != Extras{}) content.containerExtras.emplace_back(type, std::move(extras)); return items; } - template Element items_write(const std::vector& items, ElementType type, const Content& content) + Content content_read(const XMLElement* element) { - auto container = element_make(type); - for (const auto& item : items) - container.children.push_back(item_write(item)); - for (const auto& [extrasType, extras] : content.containerExtras) - if (extrasType == type) extras_write(container, extras); - return container; - } - - Content content_read(const Element& element) - { - Content content{.layout = {}}; - std::set read{}; - children_read(element, content.extras, - [&](const Element& child) + Content content{.layout = {}, .extras = extras_read(element)}; + children_each(element, + [&](const XMLElement* child, ElementType type, int position) { - if (read.contains(child.type)) return false; - switch (child.type) - { - case ElementType::SPRITESHEETS: - content.spritesheets = items_read(child, content); - break; - case ElementType::SHADERS: - content.shaders = items_read(child, content); - break; - case ElementType::LAYERS: - content.layers = items_read(child, content); - break; - case ElementType::NULLS: - content.nulls = items_read(child, content); - break; - case ElementType::EVENTS: - content.events = items_read(child, content); - break; - case ElementType::SOUNDS: - content.sounds = items_read(child, content); - break; - default: - return false; - } - read.insert(child.type); - content.layout.push_back(child.type); - return true; + if (child->Name() == LEGACY_GROUPS_TAG) return; + auto isKnown = type == ElementType::SPRITESHEETS || type == ElementType::SHADERS || + type == ElementType::LAYERS || type == ElementType::NULLS || + type == ElementType::EVENTS || type == ElementType::SOUNDS; + if (!isKnown || std::ranges::contains(content.layout, type)) + return extras_child_add(content.extras, child, position); + content.layout.push_back(type); + if (type == ElementType::SPRITESHEETS) + content.spritesheets = items_read(child, content, type); + if (type == ElementType::SHADERS) content.shaders = items_read(child, content, type); + if (type == ElementType::LAYERS) content.layers = items_read(child, content, type); + if (type == ElementType::NULLS) content.nulls = items_read(child, content, type); + if (type == ElementType::EVENTS) content.events = items_read(child, content, type); + if (type == ElementType::SOUNDS) content.sounds = items_read(child, content, type); }); - content.extras.attributes = element.extraAttributes; - content.extras.text = element.text; return content; } - Element content_write(const Content& content) + // Gives every item a unique, non-negative id; the first holder of a duplicated id keeps it. + template void ids_repair(Items& items) { - auto element = element_make(ElementType::CONTENT); - layout_write(element, content.layout, - {{ElementType::SPRITESHEETS, !content.spritesheets.empty()}, - {ElementType::SHADERS, !content.shaders.empty()}, - {ElementType::LAYERS, !content.layers.empty()}, - {ElementType::NULLS, !content.nulls.empty()}, - {ElementType::EVENTS, !content.events.empty()}, - {ElementType::SOUNDS, !content.sounds.empty()}}, - [&](ElementType type) - { - switch (type) - { - case ElementType::SPRITESHEETS: - return items_write(content.spritesheets, type, content); - case ElementType::SHADERS: - return items_write(content.shaders, type, content); - case ElementType::LAYERS: - return items_write(content.layers, type, content); - case ElementType::NULLS: - return items_write(content.nulls, type, content); - case ElementType::EVENTS: - return items_write(content.events, type, content); - default: - return items_write(content.sounds, type, content); - } - }); - extras_write(element, content.extras); + std::set usedIds{}; + for (const auto& item : items) + if (item.id >= 0) usedIds.insert(item.id); + std::set keptIds{}; + int nextId{}; + for (auto& item : items) + { + if (item.id >= 0 && keptIds.insert(item.id).second) continue; + while (usedIds.contains(nextId)) + ++nextId; + item.id = nextId; + usedIds.insert(nextId); + keptIds.insert(nextId); + } + } + + bool is_region_matched(const Region& region, const Frame& frame) + { + return glm::ivec2(region.crop) == glm::ivec2(frame.crop) && glm::ivec2(region.size) == glm::ivec2(frame.size) && + glm::ivec2(region.pivot) == glm::ivec2(frame.pivot); + } + + // Loaded documents get unique content ids, frames lose dangling shader ids, and layer frames point at the region + // matching their rectangle and take that region's rectangle. + void model_repair(Model& model) + { + auto& content = model.content; + ids_repair(content.spritesheets); + ids_repair(content.shaders); + ids_repair(content.layers); + ids_repair(content.nulls); + ids_repair(content.events); + ids_repair(content.sounds); + for (auto& spritesheet : content.spritesheets) + ids_repair(spritesheet.regions); + for (auto& shader : content.shaders) + if (shader.name.empty()) shader.name = SHADER_NAME_DEFAULT; + + for (int i = 0; i < model.animations_count_get(); ++i) + { + auto& animation = *model.animation_edit(i); + animation_tracks_each(animation, + [&](Track& track) + { + if (track.type == ItemType::TRIGGER) return; + for (auto& frame : track.frames) + if (frame.shaderId != -1 && !item_get(content.shaders, frame.shaderId)) + frame.shaderId = -1; + }); + tracks_each(animation.layers, + [&](Track& track, auto*) + { + auto layer = item_get(content.layers, track.id); + auto spritesheet = layer ? item_get(content.spritesheets, layer->spritesheetId) : nullptr; + if (!spritesheet) return; + for (auto& frame : track.frames) + { + auto region = frame.regionId == -1 ? nullptr : item_get(spritesheet->regions, frame.regionId); + auto match = std::ranges::find_if(spritesheet->regions, [&](const Region& candidate) + { return is_region_matched(candidate, frame); }); + if ((!region || !is_region_matched(*region, frame)) && match != spritesheet->regions.end()) + frame.regionId = match->id; + if (frame.regionId == -1) continue; + region = item_get(spritesheet->regions, frame.regionId); + if (!region) + { + frame.regionId = -1; + continue; + } + frame.crop = region->crop; + frame.size = region->size; + frame.pivot = region->pivot; + } + }); + } + } + + Model model_read(const XMLElement* element) + { + Model model{.layout = {}, .extras = extras_read(element)}; + children_each(element, + [&](const XMLElement* child, ElementType type, int position) + { + auto isKnown = + type == ElementType::INFO || type == ElementType::CONTENT || type == ElementType::ANIMATIONS; + if (!isKnown || std::ranges::contains(model.layout, type)) + return extras_child_add(model.extras, child, position); + model.layout.push_back(type); + if (type == ElementType::INFO) + { + model.info = {.createdBy = string_get(child, "CreatedBy", Info{}.createdBy), + .createdOn = string_get(child, "CreatedOn"), + .fps = int_get(child, "Fps", Info{}.fps), + .version = int_get(child, "Version", 0), + .extras = extras_read(child)}; + children_each(child, [&](const XMLElement* infoChild, ElementType, int infoPosition) + { extras_child_add(model.info.extras, infoChild, infoPosition); }); + } + if (type == ElementType::CONTENT) model.content = content_read(child); + if (type == ElementType::ANIMATIONS) + { + model.animations.defaultAnimation = string_get(child, "DefaultAnimation"); + model.animations.extras = extras_read(child); + model.animations.entries = animation_entries_read(child, model.animations.extras); + } + }); + model_repair(model); + return model; + } + + // Writing: the model to an XmlNode tree in the shape a set of flags asks for, then to tinyxml2 or a hash. + + struct Writer + { + Flags flags{}; + const Model* model{}; + std::map shaderIds{}; + std::map layerIds{}; + std::map> regionIds{}; + + // With a model, a whole document is written: shader, layer and region ids are renumbered by position. + Writer(Flags flags, const Model* model = nullptr) : flags(flags), model(model) + { + if (!model) return; + auto& content = model->content; + for (int i = 0; i < (int)content.shaders.size(); ++i) + shaderIds[content.shaders[i].id] = i; + for (int i = 0; i < (int)content.layers.size(); ++i) + layerIds[content.layers[i].id] = i; + std::set spritesheetIds{}; + for (const auto& spritesheet : content.spritesheets) + if (spritesheetIds.insert(spritesheet.id).second && !spritesheet.regions.empty()) + for (int i = 0; i < (int)spritesheet.regions.size(); ++i) + regionIds[spritesheet.id][spritesheet.regions[i].id] = i; + } + + bool is(Flag flag) const { return has_flag(flags, flag); } + }; + + int id_map_get(const std::map& ids, int id) + { + auto it = ids.find(id); + return it == ids.end() ? -1 : it->second; + } + + template void attribute_add(XmlNode& node, const char* name, const T& value) + { + if constexpr (std::is_convertible_v) + node.attributes.emplace_back(name, std::string(std::string_view(value))); + else + { + char buffer[VALUE_BUFFER_SIZE]{}; + XMLUtil::ToStr(value, buffer, sizeof(buffer)); + node.attributes.emplace_back(name, buffer); + } + } + + void path_add(XmlNode& node, const char* name, const std::filesystem::path& value) + { + if (!value.empty()) attribute_add(node, name, path::to_utf8(value)); + } + + void interpolation_add(XmlNode& node, const char* name, Interpolation interpolation) + { + if (interpolation == Interpolation::NONE || interpolation == Interpolation::LINEAR) + attribute_add(node, name, interpolation == Interpolation::LINEAR); + else + attribute_add(node, name, INTERPOLATION_VALUES[(int)interpolation]); + } + + // Unknown XML goes back where it was read: attributes after the known ones, children at their recorded position. + void extras_write(XmlNode& node, const Extras& extras) + { + node.attributes.insert(node.attributes.end(), extras.attributes.begin(), extras.attributes.end()); + if (!extras.text.empty()) node.text = extras.text; + for (const auto& [index, child] : extras.children) + node.children.insert(node.children.begin() + std::min(index, (int)node.children.size()), child); + } + + bool is_group_id_written(const Writer& writer) + { + return writer.is(SERIALIZE_GROUPS) && !writer.is(SERIALIZE_NESTED_GROUPS); + } + + bool is_special_interpolation(Interpolation interpolation) + { + return interpolation != Interpolation::NONE && interpolation != Interpolation::LINEAR; + } + + // A whole document's frames use renumbered shader and region ids; a layer frame on a region takes its rectangle. + Frame frame_normalize(const Writer& writer, Frame frame, const Track* layerTrack) + { + if (!writer.model) return frame; + if (frame.shaderId != -1) frame.shaderId = id_map_get(writer.shaderIds, frame.shaderId); + if (!layerTrack || frame.regionId == -1) return frame; + + auto& content = writer.model->content; + auto layer = item_get(content.layers, layerTrack->id); + auto spritesheet = layer ? item_get(content.spritesheets, layer->spritesheetId) : nullptr; + if (!spritesheet) return frame; + auto regionIds = writer.regionIds.find(spritesheet->id); + frame.regionId = regionIds == writer.regionIds.end() ? -1 : id_map_get(regionIds->second, frame.regionId); + if (frame.regionId == -1) return frame; + auto& region = spritesheet->regions[frame.regionId]; + frame.crop = region.crop; + frame.size = region.size; + frame.pivot = region.pivot; + return frame; + } + + XmlNode frame_write(const Writer& writer, const Frame& frame, ItemType type) + { + if (type == ItemType::TRIGGER) + { + XmlNode node{.tag = tag_get(ElementType::TRIGGER)}; + if (frame.eventId != -1) attribute_add(node, "EventId", frame.eventId); + attribute_add(node, "AtFrame", frame.atFrame); + if (writer.is(SERIALIZE_SOUNDS)) + for (auto soundId : frame.soundIds) + if (soundId != -1) + attribute_add(node.children.emplace_back(XmlNode{.tag = tag_get(ElementType::SOUND_ELEMENT)}), "Id", + soundId); + extras_write(node, frame.extras); + return node; + } + + XmlNode node{.tag = tag_get(ElementType::FRAME)}; + if (type == ItemType::LAYER) + { + auto isRegion = writer.is(SERIALIZE_REGIONS) && frame.regionId != -1; + if (isRegion) attribute_add(node, "RegionId", frame.regionId); + if (writer.is(SERIALIZE_REDUNDANT_FRAME_REGION_VALUES) || !isRegion) + { + attribute_add(node, "XPivot", frame.pivot.x); + attribute_add(node, "YPivot", frame.pivot.y); + attribute_add(node, "XCrop", frame.crop.x); + attribute_add(node, "YCrop", frame.crop.y); + attribute_add(node, "Width", frame.size.x); + attribute_add(node, "Height", frame.size.y); + } + } + attribute_add(node, "XPosition", frame.position.x); + attribute_add(node, "YPosition", frame.position.y); + attribute_add(node, "Delay", frame.duration); + attribute_add(node, "Visible", frame.isVisible); + attribute_add(node, "XScale", frame.scale.x); + attribute_add(node, "YScale", frame.scale.y); + if (writer.is(SERIALIZE_EXTENSIONS)) + { + attribute_add(node, "ShearX", frame.shear.x); + attribute_add(node, "ShearY", frame.shear.y); + } + for (int i = 0; i < (int)std::size(TINT_ATTRIBUTES); ++i) + attribute_add(node, TINT_ATTRIBUTES[i], math::float_to_uint8(frame.tint[i])); + for (int i = 0; i < (int)std::size(OFFSET_ATTRIBUTES); ++i) + attribute_add(node, OFFSET_ATTRIBUTES[i], math::float_to_uint8(frame.colorOffset[i])); + attribute_add(node, "Rotation", frame.rotation); + interpolation_add(node, "Interpolated", frame.interpolation); + if (writer.is(SERIALIZE_EXTENSIONS) && frame.shaderId != -1) attribute_add(node, "ShaderId", frame.shaderId); + extras_write(node, frame.extras); + return node; + } + + // A backup (FrameRestore) track keeps its frames' region ids as they are. + XmlNode track_write(const Writer& writer, const Track& track, int groupId = -1, bool isBackup = false) + { + XmlNode node{.tag = tag_get(track_element_type_get(track.type))}; + if (track.type == ItemType::LAYER || track.type == ItemType::NULL_) + { + auto id = track.id; + if (track.type == ItemType::LAYER && writer.model) + if (auto it = writer.layerIds.find(id); it != writer.layerIds.end()) id = it->second; + attribute_add(node, track.type == ItemType::LAYER ? "LayerId" : "NullId", id); + attribute_add(node, "Visible", track.isVisible); + if (groupId != -1 && is_group_id_written(writer)) attribute_add(node, "GroupId", groupId); + } + + auto layerTrack = track.type == ItemType::LAYER && !isBackup ? &track : nullptr; + for (int i = 0; i < (int)track.frames.size(); ++i) + { + auto frame = frame_normalize(writer, track.frames[i], layerTrack); + if (!writer.is(SERIALIZE_BAKE_SPECIAL_INTERPOLATED_FRAMES) || track.type == ItemType::TRIGGER || + !is_special_interpolation(frame.interpolation)) + { + node.children.push_back(frame_write(writer, frame, track.type)); + continue; + } + // Written for the game as single-step frames; the first records how to read them back as one. + auto baked = frame_bake_split(frame, i + 1 < (int)track.frames.size() ? track.frames[i + 1] : frame, + FRAME_DURATION_MIN, false, false); + for (int bakeIndex = 0; bakeIndex < (int)baked.size(); ++bakeIndex) + { + auto& child = node.children.emplace_back(frame_write(writer, baked[bakeIndex], track.type)); + if (bakeIndex > 0) continue; + attribute_add(child, "BakeInterpolation", INTERPOLATION_VALUES[(int)frame.interpolation]); + attribute_add(child, "BakeDelay", (int)baked.size()); + } + } + extras_write(node, track.extras); + return node; + } + + template XmlNode group_write(const Writer& writer, const Group& group) + { + XmlNode node{.tag = tag_get(ElementType::GROUP)}; + if (!writer.is(SERIALIZE_NESTED_GROUPS)) attribute_add(node, "Id", group.id); + attribute_add(node, "Name", group.name); + attribute_add(node, "IsExpanded", group.isExpanded); + attribute_add(node, "Visible", group.isVisible); + if constexpr (requires { group.root; }) node.children.push_back(track_write(writer, group.root)); + extras_write(node, group.extras); + return node; + } + + struct ContainerChild + { + XmlNode node{}; + int groupId{-1}; + std::optional ownGroupId{}; + std::size_t knownCount{}; + }; + + template ContainerChild group_child_make(const Writer& writer, const Group& group) + { + auto node = group_write(writer, group); + auto knownCount = node.attributes.size() - group.extras.attributes.size(); + return {.node = std::move(node), .ownGroupId = group.id, .knownCount = knownCount}; + } + + // Children are laid out flat (a group, then its children) and reshaped for the flags: nested into their groups, or, + // in a track container, moved to a *AnimationGroups sibling that records each group's position as Index. + XmlNode container_write(const Writer& writer, XmlNode container, std::vector flat, + const Extras& extras, XmlNode* groups = nullptr) + { + container.attributes.insert(container.attributes.end(), extras.attributes.begin(), extras.attributes.end()); + if (!extras.text.empty()) container.text = extras.text; + for (const auto& [index, node] : extras.children) + flat.insert(flat.begin() + std::min(index, (int)flat.size()), ContainerChild{.node = node}); + + auto isGroups = writer.is(SERIALIZE_GROUPS); + if (isGroups && writer.is(SERIALIZE_NESTED_GROUPS)) + { + std::set groupIds{}; + for (const auto& child : flat) + if (child.ownGroupId && *child.ownGroupId >= 0) groupIds.insert(*child.ownGroupId); + std::map> grouped{}; + for (const auto& child : flat) + if (!child.ownGroupId && groupIds.contains(child.groupId)) grouped[child.groupId].push_back(child.node); + std::erase_if(flat, + [&](const ContainerChild& child) { return !child.ownGroupId && groupIds.contains(child.groupId); }); + for (auto& child : flat) + if (child.ownGroupId) std::ranges::copy(grouped[*child.ownGroupId], std::back_inserter(child.node.children)); + } + + auto isExtracted = groups && isGroups && !writer.is(SERIALIZE_NESTED_GROUPS); + for (int i = 0; i < (int)flat.size(); ++i) + { + auto& child = flat[i]; + if (child.ownGroupId && !isGroups) continue; + if (!child.ownGroupId || !isExtracted) + { + container.children.push_back(std::move(child.node)); + continue; + } + XmlNode index{}; + attribute_add(index, "Index", i); + child.node.attributes.insert(child.node.attributes.begin() + child.knownCount, index.attributes.front()); + groups->children.push_back(std::move(child.node)); + } + return container; + } + + XmlNode track_entries_write(const Writer& writer, const std::vector& entries, ElementType type, + const Extras& extras, XmlNode& groups) + { + std::vector flat{}; + for (const auto& entry : entries) + if (auto track = std::get_if(&entry)) + flat.push_back({.node = track_write(writer, *track)}); + else + { + auto& group = std::get(entry); + flat.push_back(group_child_make(writer, group)); + for (const auto& groupTrack : group.tracks) + flat.push_back({.node = track_write(writer, groupTrack, group.id), .groupId = group.id}); + } + return container_write(writer, {.tag = tag_get(type)}, std::move(flat), extras, &groups); + } + + bool is_frame_transform_default(const Frame& frame) + { + Frame base{}; + return frame.position == base.position && frame.scale == base.scale && frame.rotation == base.rotation && + frame.shear == base.shear && frame.tint == base.tint && frame.colorOffset == base.colorOffset; + } + + // For the game, a group root's transform is baked into its tracks one frame at a time. + void track_group_root_bake(Track& track, const Track& root, int frameNum) + { + auto trackLength = track_length_get(track); + if (trackLength <= 0) return; + auto length = std::max({trackLength, track_length_get(root), frameNum, FRAME_DURATION_MIN}); + std::vector frames{}; + for (int time = 0; time < length; ++time) + { + auto& frame = frames.emplace_back(frame_generate(track, (float)time)); + frame_root_transform_apply(frame, frame_generate(root, (float)time), false, false, false); + frame.duration = FRAME_DURATION_MIN; + frame.interpolation = Interpolation::NONE; + } + track.frames = std::move(frames); + } + + // The game's copy: group roots baked into their tracks (originals kept in a FrameRestore node the editor reads + // back), and interpolation flattened into single-step frames. + Animation animation_bake(const Writer& writer, Animation animation) + { + if (writer.is(SERIALIZE_BAKE_GROUP_FRAMES)) + for (auto* entries : {&animation.layers, &animation.nulls}) + groups_each(*entries, + [&](TrackGroup& group) + { + if (std::ranges::all_of(group.root.frames, is_frame_transform_default)) return; + XmlNode backup{.tag = std::string(FRAME_RESTORE_TAG)}; + backup.children.push_back(track_write(writer, group.root)); + for (auto& track : group.tracks) + { + backup.children.push_back(track_write(writer, track, group.id, true)); + track_group_root_bake(track, group.root, animation.frameNum); + } + group.root = TrackGroup{}.root; + group.extras.children.emplace_back(1, std::move(backup)); + }); + + if (writer.is(SERIALIZE_FLATTEN_SPECIAL_INTERPOLATED_FRAMES)) + animation_tracks_each(animation, + [](Track& track) + { + if (track.type == ItemType::TRIGGER) return; + for (int index = (int)track.frames.size() - 1; index >= 0; --index) + if (track.frames[index].interpolation != Interpolation::NONE) + frame_bake(track, index, FRAME_DURATION_MIN, false, false); + }); + return animation; + } + + // Known children in their recorded order; a part with content that was never in the file is appended. + std::vector layout_get(std::vector layout, + std::initializer_list> parts) + { + for (auto [type, isContent] : parts) + if (isContent && !std::ranges::contains(layout, type)) layout.push_back(type); + return layout; + } + + XmlNode animation_write(const Writer& writer, const Animation& source, int groupId = -1) + { + auto isBaked = writer.model && + (writer.is(SERIALIZE_BAKE_GROUP_FRAMES) || writer.is(SERIALIZE_FLATTEN_SPECIAL_INTERPOLATED_FRAMES)); + auto baked = isBaked ? std::optional(animation_bake(writer, source)) : std::nullopt; + const auto& animation = baked ? *baked : source; + + XmlNode node{.tag = tag_get(ElementType::ANIMATION)}; + attribute_add(node, "Name", animation.name); + attribute_add(node, "FrameNum", animation.frameNum); + attribute_add(node, "Loop", animation.isLoop); + if (groupId != -1 && is_group_id_written(writer)) attribute_add(node, "GroupId", groupId); + + std::vector> groups{}; + for (auto type : layout_get(animation.layout, {{ElementType::ROOT_ANIMATION, !animation.root.frames.empty()}, + {ElementType::LAYER_ANIMATIONS, !animation.layers.empty()}, + {ElementType::NULL_ANIMATIONS, !animation.nulls.empty()}, + {ElementType::TRIGGERS, !animation.triggers.frames.empty()}})) + { + if (type == ElementType::ROOT_ANIMATION) node.children.push_back(track_write(writer, animation.root)); + if (type == ElementType::TRIGGERS) node.children.push_back(track_write(writer, animation.triggers)); + if (type != ElementType::LAYER_ANIMATIONS && type != ElementType::NULL_ANIMATIONS) continue; + auto isLayers = type == ElementType::LAYER_ANIMATIONS; + auto& [groupsType, groupsNode] = groups.emplace_back( + type, + XmlNode{.tag = tag_get(isLayers ? ElementType::LAYER_ANIMATION_GROUPS : ElementType::NULL_ANIMATION_GROUPS)}); + node.children.push_back(track_entries_write(writer, isLayers ? animation.layers : animation.nulls, type, + isLayers ? animation.layersExtras : animation.nullsExtras, + groupsNode)); + } + extras_write(node, animation.extras); + + for (auto& [type, groupsNode] : groups) + { + if (groupsNode.children.empty()) continue; + auto container = std::ranges::find(node.children, std::string(tag_get(type)), &XmlNode::tag); + node.children.insert(container + 1, std::move(groupsNode)); + } + return node; + } + + XmlNode animations_write(const Writer& writer, const Animations& animations) + { + std::vector flat{}; + for (const auto& entry : animations.entries) + if (auto animation = std::get_if>(&entry)) + flat.push_back({.node = animation_write(writer, **animation)}); + else + { + auto& group = std::get(entry); + flat.push_back(group_child_make(writer, group)); + for (const auto& groupAnimation : group.animations) + flat.push_back({.node = animation_write(writer, *groupAnimation, group.id), .groupId = group.id}); + } + XmlNode node{.tag = tag_get(ElementType::ANIMATIONS)}; + attribute_add(node, "DefaultAnimation", animations.defaultAnimation); + return container_write(writer, std::move(node), std::move(flat), animations.extras); + } + + XmlNode item_write(const Writer&, const Region& region, int id) + { + XmlNode node{.tag = tag_get(ElementType::REGION)}; + attribute_add(node, "Id", id); + attribute_add(node, "Name", region.name); + attribute_add(node, "XCrop", region.crop.x); + attribute_add(node, "YCrop", region.crop.y); + attribute_add(node, "Width", region.size.x); + attribute_add(node, "Height", region.size.y); + if (region.origin != Origin::CUSTOM) + attribute_add(node, "Origin", ORIGIN_VALUES[(int)region.origin]); + else + { + attribute_add(node, "XPivot", region.pivot.x); + attribute_add(node, "YPivot", region.pivot.y); + } + extras_write(node, region.extras); + return node; + } + + XmlNode item_write(const Writer& writer, const Spritesheet& spritesheet, int id) + { + XmlNode node{.tag = tag_get(ElementType::SPRITESHEET)}; + attribute_add(node, "Id", id); + path_add(node, "Path", spritesheet.path); + if (writer.is(SERIALIZE_REGIONS)) + for (int i = 0; i < (int)spritesheet.regions.size(); ++i) + node.children.push_back( + item_write(writer, spritesheet.regions[i], writer.model ? i : spritesheet.regions[i].id)); + extras_write(node, spritesheet.extras); + return node; + } + + void binding_value_add(XmlNode& node, const std::string& binding, const std::string& value) + { + if (!binding.empty()) attribute_add(node, "Binding", binding); + if (!value.empty()) attribute_add(node, "Value", value); + } + + XmlNode item_write(const Writer&, const Shader& shader, int id) + { + XmlNode node{.tag = tag_get(ElementType::SHADER)}; + attribute_add(node, "Id", id); + attribute_add(node, "Name", shader.name); + path_add(node, "Vertex", shader.vertex); + path_add(node, "Fragment", shader.fragment); + for (const auto& uniform : shader.uniforms) + { + auto& uniformNode = node.children.emplace_back(XmlNode{.tag = tag_get(ElementType::UNIFORM)}); + attribute_add(uniformNode, "Name", uniform.name); + binding_value_add(uniformNode, uniform.binding, uniform.value); + for (const auto& component : uniform.components) + { + auto& componentNode = uniformNode.children.emplace_back(XmlNode{.tag = tag_get(ElementType::COMPONENT)}); + attribute_add(componentNode, "Index", component.index); + binding_value_add(componentNode, component.binding, component.value); + extras_write(componentNode, component.extras); + } + extras_write(uniformNode, uniform.extras); + } + extras_write(node, shader.extras); + return node; + } + + XmlNode item_write(const Writer&, const Layer& layer, int id) + { + XmlNode node{.tag = tag_get(ElementType::LAYER_ELEMENT)}; + attribute_add(node, "Id", id); + attribute_add(node, "Name", layer.name); + attribute_add(node, "SpritesheetId", layer.spritesheetId); + extras_write(node, layer.extras); + return node; + } + + XmlNode item_write(const Writer&, const Null& null, int id) + { + XmlNode node{.tag = tag_get(ElementType::NULL_ELEMENT)}; + attribute_add(node, "Id", id); + attribute_add(node, "Name", null.name); + if (null.isShowRect) attribute_add(node, "ShowRect", null.isShowRect); + extras_write(node, null.extras); + return node; + } + + XmlNode item_write(const Writer&, const Event& event, int id) + { + XmlNode node{.tag = tag_get(ElementType::EVENT_ELEMENT)}; + attribute_add(node, "Id", id); + attribute_add(node, "Name", event.name); + extras_write(node, event.extras); + return node; + } + + XmlNode item_write(const Writer&, const Sound& sound, int id) + { + XmlNode node{.tag = tag_get(ElementType::SOUND_ELEMENT)}; + attribute_add(node, "Id", id); + path_add(node, "Path", sound.path); + extras_write(node, sound.extras); + return node; + } + + template + XmlNode items_write(const Writer& writer, const std::vector& items, ElementType type, const Content& content) + { + XmlNode node{.tag = tag_get(type)}; + auto isRenumbered = writer.model && (type == ElementType::LAYERS || type == ElementType::SHADERS); + for (int i = 0; i < (int)items.size(); ++i) + node.children.push_back(item_write(writer, items[i], isRenumbered ? i : items[i].id)); + for (const auto& [extrasType, extras] : content.containerExtras) + if (extrasType == type) extras_write(node, extras); + return node; + } + + XmlNode content_write(const Writer& writer, const Content& content) + { + XmlNode node{.tag = tag_get(ElementType::CONTENT)}; + for (auto type : layout_get(content.layout, {{ElementType::SPRITESHEETS, !content.spritesheets.empty()}, + {ElementType::SHADERS, !content.shaders.empty()}, + {ElementType::LAYERS, !content.layers.empty()}, + {ElementType::NULLS, !content.nulls.empty()}, + {ElementType::EVENTS, !content.events.empty()}, + {ElementType::SOUNDS, !content.sounds.empty()}})) + { + auto& container = node.children.emplace_back(); + if (type == ElementType::SPRITESHEETS) container = items_write(writer, content.spritesheets, type, content); + if (type == ElementType::SHADERS) container = items_write(writer, content.shaders, type, content); + if (type == ElementType::LAYERS) container = items_write(writer, content.layers, type, content); + if (type == ElementType::NULLS) container = items_write(writer, content.nulls, type, content); + if (type == ElementType::EVENTS) container = items_write(writer, content.events, type, content); + if (type == ElementType::SOUNDS) container = items_write(writer, content.sounds, type, content); + // Optional containers are left out when the flags drop them or they are empty; an emptied tag marks them so + // extras still land at their recorded positions. + auto isOptional = type == ElementType::SHADERS || type == ElementType::SOUNDS; + auto isEnabled = writer.is(type == ElementType::SHADERS ? SERIALIZE_EXTENSIONS : SERIALIZE_SOUNDS); + if (isOptional && (!isEnabled || container.children.empty())) container.tag.clear(); + } + extras_write(node, content.extras); + std::erase_if(node.children, [](const XmlNode& child) { return child.tag.empty(); }); + return node; + } + + XmlNode model_write(const Model& model, Flags flags) + { + Writer writer(flags, &model); + XmlNode node{.tag = tag_get(ElementType::ANIMATED_ACTOR)}; + auto isInfo = model.info.fps != Info{}.fps || model.info.createdBy != Info{}.createdBy; + for (auto type : layout_get(model.layout, {{ElementType::INFO, isInfo}, + {ElementType::CONTENT, true}, + {ElementType::ANIMATIONS, !model.animations.entries.empty()}})) + { + if (type == ElementType::CONTENT) node.children.push_back(content_write(writer, model.content)); + if (type == ElementType::ANIMATIONS) node.children.push_back(animations_write(writer, model.animations)); + if (type != ElementType::INFO) continue; + auto& info = node.children.emplace_back(XmlNode{.tag = tag_get(ElementType::INFO)}); + attribute_add(info, "CreatedBy", model.info.createdBy); + attribute_add(info, "CreatedOn", model.info.createdOn); + attribute_add(info, "Fps", model.info.fps); + attribute_add(info, "Version", model.info.version); + extras_write(info, model.info.extras); + } + extras_write(node, model.extras); + return node; + } + + // A save is the game's document with the editor's own copy embedded as SourceDocument (or just the editor's copy). + XmlNode document_write(const Model& model, Options options) + { + auto editor = model_write(model, SERIALIZE_ANM2ED_DEFAULT); + if (options.isExtendedFormat) return editor; + auto game = model_write(model, SERIALIZE_ISAAC_DEFAULT); + editor.tag = SOURCE_DOCUMENT_TAG; + game.children.push_back(std::move(editor)); + return game; + } + + XMLElement* element_write(XMLDocument& document, const XmlNode& node) + { + auto element = document.NewElement(node.tag.c_str()); + for (const auto& [name, value] : node.attributes) + element->SetAttribute(name.c_str(), value.c_str()); + if (!node.text.empty()) element->SetText(node.text.c_str()); + for (const auto& child : node.children) + element->InsertEndChild(element_write(document, child)); return element; } - Model model_read(const Element& root) + std::string node_to_string(const XmlNode& node) { - Model model{.layout = {}}; - std::set read{}; - children_read(root, model.extras, - [&](const Element& child) - { - if (read.contains(child.type)) return false; - switch (child.type) - { - case ElementType::INFO: - model.info = {child.createdBy, child.createdOn, child.fps, child.version, {}}; - children_read(child, model.info.extras, [](const Element&) { return false; }); - model.info.extras.attributes = child.extraAttributes; - model.info.extras.text = child.text; - break; - case ElementType::CONTENT: - model.content = content_read(child); - break; - case ElementType::ANIMATIONS: - model.animations = animations_read(child); - break; - default: - return false; - } - read.insert(child.type); - model.layout.push_back(child.type); - return true; - }); - model.extras.attributes = root.extraAttributes; - model.extras.text = root.text; + XMLDocument document{}; + document.InsertEndChild(element_write(document, node)); + return xml::document_to_string(document); + } + + void node_hash(std::uint64_t& hash, const XmlNode& node) + { + auto add = [&](char marker, std::string_view value) + { + hash = (hash ^ (unsigned char)marker) * HASH_PRIME; + for (unsigned char character : value) + hash = (hash ^ character) * HASH_PRIME; + hash *= HASH_PRIME; + }; + add('E', node.tag); + for (const auto& [name, value] : node.attributes) + { + add('A', name); + add('V', value); + } + add('T', node.text); + for (const auto& child : node.children) + node_hash(hash, child); + add(']', {}); + } + + bool document_load(Model& model, XMLDocument& document, XMLError result, std::string* errorString) + { + auto root = result == XML_SUCCESS ? document.RootElement() : nullptr; + if (!root) + { + if (errorString) *errorString = result == XML_SUCCESS ? "No root element." : document.ErrorStr(); + model = model_make(); + model.isValid = false; + return false; + } + auto source = root->Name() == SOURCE_DOCUMENT_TAG ? root : root->FirstChildElement(SOURCE_DOCUMENT_TAG.data()); + model = model_read(source ? source : root); + return true; + } + + Model model_make() + { + Model model{}; + model.info.createdOn = time::get(CREATED_ON_FORMAT); return model; } - Element model_write(const Model& model) - { - auto root = element_make(ElementType::ANIMATED_ACTOR); - layout_write(root, model.layout, - {{ElementType::INFO, model.info.fps != Info{}.fps || model.info.createdBy != Info{}.createdBy}, - {ElementType::CONTENT, true}, - {ElementType::ANIMATIONS, !model.animations.entries.empty()}}, - [&](ElementType type) - { - if (type == ElementType::CONTENT) return content_write(model.content); - if (type == ElementType::ANIMATIONS) return animations_write(model.animations); - auto info = element_make(ElementType::INFO); - info.createdBy = model.info.createdBy; - info.createdOn = model.info.createdOn; - info.fps = model.info.fps; - info.version = model.info.version; - extras_write(info, model.info.extras); - return info; - }); - extras_write(root, model.extras); - return root; - } - - Anm2 anm2_get(const Model& model) - { - Anm2 anm2{}; - anm2.root = model_write(model); - anm2.isValid = model.isValid; - return anm2; - } - - Model model_from_anm2(const Anm2& anm2) - { - auto model = model_read(anm2.root); - model.isValid = anm2.isValid; - return model; - } - - Model model_make() { return model_from_anm2(Anm2()); } - bool model_load(Model& model, const std::filesystem::path& path, std::string* errorString) { - Anm2 anm2{}; - auto isLoaded = anm2.load(path, errorString); - model = model_from_anm2(anm2); - return isLoaded; + XMLDocument document{}; + File file(path, "rb"); + if (file) return document_load(model, document, document.LoadFile(file.get()), errorString); + if (errorString) *errorString = "File not found."; + model = model_make(); + model.isValid = false; + return false; } bool model_load_string(Model& model, std::string_view text, std::string* errorString) { - Anm2 anm2{}; - auto isLoaded = anm2.load_string(text, errorString); - model = model_from_anm2(anm2); - return isLoaded; + XMLDocument document{}; + return document_load(model, document, document.Parse(text.data(), text.size()), errorString); } bool model_save(const Model& model, const std::filesystem::path& path, std::string* errorString, Options options) { - return anm2_get(model).save(path, errorString, options); + XMLDocument document{}; + document.InsertFirstChild(element_write(document, document_write(model, options))); + File file(path, "wb"); + if (!file) + { + if (errorString) *errorString = "File permissions."; + return false; + } + if (document.SaveFile(file.get()) == XML_SUCCESS) return true; + if (errorString) *errorString = document.ErrorStr(); + return false; } - std::string model_to_string(const Model& model, Options options) { return anm2_get(model).to_string(options); } - - std::string model_serialize(const Model& model, Flags flags) + std::string model_to_string(const Model& model, Options options) { - return element_to_string(anm2_get(model).normalized_for_serialize(flags).root, flags); + return node_to_string(document_write(model, options)); } - std::uint64_t model_hash(const Model& model, Options options) { return anm2_hash_get(model_write(model), options); } + std::string model_serialize(const Model& model, Flags flags) { return node_to_string(model_write(model, flags)); } + + // The hash of what the editor would save, so it changes exactly when a save would. + std::uint64_t model_hash(const Model& model) + { + auto hash = HASH_OFFSET; + node_hash(hash, model_write(model, SERIALIZE_ANM2ED_DEFAULT)); + return hash; + } bool is_special_interpolated_frames(const Model& model) { @@ -774,9 +1439,8 @@ namespace anm2ed::model [&](const Track& track) { for (const auto& frame : track.frames) - isSpecial |= track.type != ItemType::TRIGGER && - frame.interpolation != Interpolation::NONE && - frame.interpolation != Interpolation::LINEAR; + isSpecial |= + track.type != ItemType::TRIGGER && is_special_interpolation(frame.interpolation); }); if (isSpecial) return true; } @@ -785,17 +1449,17 @@ namespace anm2ed::model std::string frame_to_string(const Frame& frame, ItemType type) { - return element_to_string(frame_write(frame, type), track_element_type_get(type)); + return node_to_string(frame_write(Writer(CLIPBOARD_FLAGS), frame, type)); } std::string animation_to_string(const Animation& animation, int groupId) { - return element_to_string(animation_write(animation, groupId)); + return node_to_string(animation_write(Writer(CLIPBOARD_FLAGS), animation, groupId)); } std::string animation_group_to_string(const AnimationGroup& group) { - return element_to_string(animation_group_write(group)); + return node_to_string(group_write(Writer(CLIPBOARD_FLAGS), group)); } bool document_parse(XMLDocument& document, const std::string& text, std::string* errorString) @@ -809,43 +1473,39 @@ namespace anm2ed::model { XMLDocument document{}; if (!document_parse(document, text, errorString)) return {}; - auto tag = element_tag_get(type == ItemType::TRIGGER ? ElementType::TRIGGER : ElementType::FRAME).data(); + auto tag = tag_get(type == ItemType::TRIGGER ? ElementType::TRIGGER : ElementType::FRAME); std::vector frames{}; for (auto element = document.FirstChildElement(tag); element; element = element->NextSiblingElement(tag)) - frames.push_back(frame_read(element_read(element))); + frames.push_back(frame_read(element)); if (frames.empty() && errorString) *errorString = std::format("No valid {}(s).", tag); return frames; } - // Animations and animation groups in clipboard order; an animation keeps the group id it was copied with. std::vector animations_from_string(const std::string& text, std::string* errorString) { XMLDocument document{}; if (!document_parse(document, text, errorString)) return {}; - auto container = element_make(ElementType::ANIMATIONS); - for (auto element = document.FirstChildElement(); element; element = element->NextSiblingElement()) - { - auto item = element_read(element); - if (item.type == ElementType::ANIMATION || item.type == ElementType::GROUP) - container.children.push_back(std::move(item)); - } - auto entries = animations_read(container).entries; + Extras extras{}; + auto entries = animation_entries_read(&document, extras); if (entries.empty() && errorString) *errorString = "No valid animation(s)."; return entries; } - template std::string item_to_string(const Item& item) { return element_to_string(item_write(item)); } + template std::string item_to_string(const Item& item) + { + return node_to_string(item_write(Writer(CLIPBOARD_FLAGS), item, item.id)); + } template std::vector items_from_string(const std::string& text, std::string* errorString) { XMLDocument document{}; if (!document_parse(document, text, errorString)) return {}; - auto tag = element_tag_get(ITEM_TYPE).data(); + auto tag = tag_get(ITEM_TYPE); std::vector items{}; for (auto element = document.FirstChildElement(tag); element; element = element->NextSiblingElement(tag)) { - auto item = item_read(element_read(element)); - if constexpr (requires { item.path; }) item.path = util::path::backslash_handle(item.path); + auto item = item_read(element); + if constexpr (requires { item.path; }) item.path = path::backslash_handle(item.path); items.push_back(std::move(item)); } if (items.empty() && errorString) *errorString = std::format("No valid {}(s).", tag); diff --git a/src/model/xml.hpp b/src/model/xml.hpp index c239720..affd2e7 100644 --- a/src/model/xml.hpp +++ b/src/model/xml.hpp @@ -15,7 +15,7 @@ namespace anm2ed::model bool model_save(const Model&, const std::filesystem::path&, std::string* = nullptr, Options = {}); std::string model_to_string(const Model&, Options = {}); std::string model_serialize(const Model&, Flags); - std::uint64_t model_hash(const Model&, Options = {}); + std::uint64_t model_hash(const Model&); bool is_special_interpolated_frames(const Model&); std::string frame_to_string(const Frame&, ItemType); diff --git a/src/settings.cpp b/src/settings.cpp index 1741d91..587fed8 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -266,10 +266,7 @@ DockSpace ID=0x123F8F08 Window=0x6D581B32 Pos=8,62 Size=1902,991 } } - Options Settings::anm2_options_get() const - { - return {.flags = SERIALIZE_DEFAULT, .isExtendedFormat = isFileExtendedFormat}; - } + Options Settings::anm2_options_get() const { return {.isExtendedFormat = isFileExtendedFormat}; } std::string Settings::imgui_data_load(const std::filesystem::path& path) { diff --git a/src/util/xml.cpp b/src/util/xml.cpp index 33d8445..5a413d5 100644 --- a/src/util/xml.cpp +++ b/src/util/xml.cpp @@ -1,9 +1,5 @@ #include "xml.hpp" -#include "math.hpp" -#include "path.hpp" - -using namespace anm2ed::util; using namespace tinyxml2; namespace anm2ed::util::xml @@ -14,102 +10,4 @@ namespace anm2ed::util::xml self.Print(&printer); return std::string(printer.CStr()); } - - bool query_string_attribute(const XMLElement* element, const char* attribute, std::string& out) - { - if (!element || !attribute) return false; - - const char* value = nullptr; - if (element->QueryStringAttribute(attribute, &value) != XML_SUCCESS || !value) return false; - out = value; - return true; - } - - bool query_path_attribute(const XMLElement* element, const char* attribute, std::filesystem::path& out) - { - std::string temp{}; - if (!query_string_attribute(element, attribute, temp)) return false; - out = path::from_utf8(temp); - return true; - } - - bool query_int_attribute(const XMLElement* element, const char* attribute, int& out) - { - return element && attribute && element->QueryIntAttribute(attribute, &out) == XML_SUCCESS; - } - - bool query_float_attribute(const XMLElement* element, const char* attribute, float& out) - { - return element && attribute && element->QueryFloatAttribute(attribute, &out) == XML_SUCCESS; - } - - bool query_bool_attribute(const XMLElement* element, const char* attribute, bool& out) - { - return element && attribute && element->QueryBoolAttribute(attribute, &out) == XML_SUCCESS; - } - - bool query_color_attribute(const XMLElement* element, const char* attribute, float& out) - { - int value{}; - if (!query_int_attribute(element, attribute, value)) return false; - out = math::uint8_to_float(value); - return true; - } - - void set_string_attribute(XMLElement* element, const char* attribute, const std::string& value) - { - if (element && attribute && !value.empty()) element->SetAttribute(attribute, value.c_str()); - } - - void set_path_attribute(XMLElement* element, const char* attribute, const std::filesystem::path& value) - { - if (!element || !attribute || value.empty()) return; - auto utf8 = path::to_utf8(value); - element->SetAttribute(attribute, utf8.c_str()); - } - - void set_int_attribute(XMLElement* element, const char* attribute, int value) - { - if (element && attribute) element->SetAttribute(attribute, value); - } - - void set_float_attribute(XMLElement* element, const char* attribute, float value) - { - if (element && attribute) element->SetAttribute(attribute, value); - } - - void set_bool_attribute(XMLElement* element, const char* attribute, bool value) - { - if (element && attribute) element->SetAttribute(attribute, value); - } - - void set_color_attribute(XMLElement* element, const char* attribute, float value) - { - if (element && attribute) element->SetAttribute(attribute, math::float_to_uint8(value)); - } - - XMLError query_string_attribute(XMLElement* element, const char* attribute, std::string* out) - { - if (!element || !attribute || !out) return XML_NO_ATTRIBUTE; - - const char* value = nullptr; - auto result = element->QueryStringAttribute(attribute, &value); - if (result == XML_SUCCESS && value) *out = value; - return result; - } - - XMLError query_path_attribute(XMLElement* element, const char* attribute, std::filesystem::path* out) - { - if (!out) return XML_NO_ATTRIBUTE; - - std::string temp{}; - auto result = query_string_attribute(element, attribute, &temp); - if (result == XML_SUCCESS) *out = path::from_utf8(temp); - return result; - } - - void query_color_attribute(XMLElement* element, const char* attribute, float& out) - { - query_color_attribute(static_cast(element), attribute, out); - } } diff --git a/src/util/xml.hpp b/src/util/xml.hpp index b4ec4b6..dba0ed5 100644 --- a/src/util/xml.hpp +++ b/src/util/xml.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include @@ -8,22 +7,4 @@ namespace anm2ed::util::xml { std::string document_to_string(tinyxml2::XMLDocument&); - - bool query_string_attribute(const tinyxml2::XMLElement*, const char*, std::string&); - bool query_path_attribute(const tinyxml2::XMLElement*, const char*, std::filesystem::path&); - bool query_int_attribute(const tinyxml2::XMLElement*, const char*, int&); - bool query_float_attribute(const tinyxml2::XMLElement*, const char*, float&); - bool query_bool_attribute(const tinyxml2::XMLElement*, const char*, bool&); - bool query_color_attribute(const tinyxml2::XMLElement*, const char*, float&); - - void set_string_attribute(tinyxml2::XMLElement*, const char*, const std::string&); - void set_path_attribute(tinyxml2::XMLElement*, const char*, const std::filesystem::path&); - void set_int_attribute(tinyxml2::XMLElement*, const char*, int); - void set_float_attribute(tinyxml2::XMLElement*, const char*, float); - void set_bool_attribute(tinyxml2::XMLElement*, const char*, bool); - void set_color_attribute(tinyxml2::XMLElement*, const char*, float); - - tinyxml2::XMLError query_string_attribute(tinyxml2::XMLElement*, const char*, std::string*); - tinyxml2::XMLError query_path_attribute(tinyxml2::XMLElement*, const char*, std::filesystem::path*); - void query_color_attribute(tinyxml2::XMLElement*, const char*, float&); } diff --git a/tests/common.hpp b/tests/common.hpp index a5ad9f6..78b29b8 100644 --- a/tests/common.hpp +++ b/tests/common.hpp @@ -64,13 +64,8 @@ namespace anm2ed::test inline std::vector variants_get(const model::Model& model) { std::vector variants{}; - std::string hashes{}; for (auto [name, flags] : PRESETS) - { variants.emplace_back(name, model::model_serialize(model, flags)); - for (auto [formatName, isExtended] : FORMATS) - hashes += std::format("{}.{} {:016x}\n", name, formatName, model::model_hash(model, {flags, isExtended})); - } for (auto [name, isExtended] : FORMATS) variants.emplace_back(name, model::model_to_string(model, {.isExtendedFormat = isExtended})); for (auto [name, type] : MERGES) @@ -81,7 +76,6 @@ namespace anm2ed::test {}); variants.emplace_back(name, model::model_to_string(merged, {.isExtendedFormat = true})); } - variants.emplace_back("hashes", hashes); return variants; } diff --git a/tests/golden/01_minimal.hashes.xml b/tests/golden/01_minimal.hashes.xml deleted file mode 100644 index 0fd8b2c..0000000 --- a/tests/golden/01_minimal.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 85e1752e262873f9 -default.extended d659a6dc486bdc1d -editor.save 85e1752e262873f9 -editor.extended d659a6dc486bdc1d -isaac.save 85e1752e262873f9 -isaac.extended d659a6dc486bdc1d -anm2ed.save 85e1752e262873f9 -anm2ed.extended d659a6dc486bdc1d diff --git a/tests/golden/02_items.hashes.xml b/tests/golden/02_items.hashes.xml deleted file mode 100644 index 40e0343..0000000 --- a/tests/golden/02_items.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 8065c1ac59ae41c3 -default.extended 74639ef0767af8bf -editor.save 8065c1ac59ae41c3 -editor.extended 74639ef0767af8bf -isaac.save 8065c1ac59ae41c3 -isaac.extended 74639ef0767af8bf -anm2ed.save 8065c1ac59ae41c3 -anm2ed.extended 74639ef0767af8bf diff --git a/tests/golden/03a_groups_nested.hashes.xml b/tests/golden/03a_groups_nested.hashes.xml deleted file mode 100644 index 18e17fa..0000000 --- a/tests/golden/03a_groups_nested.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 0ad05788dd0e2efb -default.extended 205c9b7c539d95ff -editor.save 0ad05788dd0e2efb -editor.extended 205c9b7c539d95ff -isaac.save 0ad05788dd0e2efb -isaac.extended 205c9b7c539d95ff -anm2ed.save 0ad05788dd0e2efb -anm2ed.extended 205c9b7c539d95ff diff --git a/tests/golden/03b_groups_legacy_flat.hashes.xml b/tests/golden/03b_groups_legacy_flat.hashes.xml deleted file mode 100644 index 063d800..0000000 --- a/tests/golden/03b_groups_legacy_flat.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 6d3d8c95645b9c17 -default.extended cc73ebf8ebc18b03 -editor.save 6d3d8c95645b9c17 -editor.extended cc73ebf8ebc18b03 -isaac.save 6d3d8c95645b9c17 -isaac.extended cc73ebf8ebc18b03 -anm2ed.save 6d3d8c95645b9c17 -anm2ed.extended cc73ebf8ebc18b03 diff --git a/tests/golden/04_group_root_transform.hashes.xml b/tests/golden/04_group_root_transform.hashes.xml deleted file mode 100644 index e47571f..0000000 --- a/tests/golden/04_group_root_transform.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save eeca10ea8173394c -default.extended 372eba70a0319040 -editor.save eeca10ea8173394c -editor.extended 372eba70a0319040 -isaac.save eeca10ea8173394c -isaac.extended 372eba70a0319040 -anm2ed.save eeca10ea8173394c -anm2ed.extended 372eba70a0319040 diff --git a/tests/golden/05_regions.hashes.xml b/tests/golden/05_regions.hashes.xml deleted file mode 100644 index 39f883d..0000000 --- a/tests/golden/05_regions.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 7e865b837f99ffb7 -default.extended 7ba7db7402487463 -editor.save 7e865b837f99ffb7 -editor.extended 7ba7db7402487463 -isaac.save 7e865b837f99ffb7 -isaac.extended 7ba7db7402487463 -anm2ed.save 7e865b837f99ffb7 -anm2ed.extended 7ba7db7402487463 diff --git a/tests/golden/06_shaders.hashes.xml b/tests/golden/06_shaders.hashes.xml deleted file mode 100644 index aac309a..0000000 --- a/tests/golden/06_shaders.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 69d0c8bb8d35f83a -default.extended 1816db2c80b34ffe -editor.save 69d0c8bb8d35f83a -editor.extended 1816db2c80b34ffe -isaac.save 69d0c8bb8d35f83a -isaac.extended 1816db2c80b34ffe -anm2ed.save 69d0c8bb8d35f83a -anm2ed.extended 1816db2c80b34ffe diff --git a/tests/golden/07_special_interpolation.hashes.xml b/tests/golden/07_special_interpolation.hashes.xml deleted file mode 100644 index 45a6920..0000000 --- a/tests/golden/07_special_interpolation.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 8cfeb73803e8ced0 -default.extended 67fc2f29d9f7bc2c -editor.save 8cfeb73803e8ced0 -editor.extended 67fc2f29d9f7bc2c -isaac.save 8cfeb73803e8ced0 -isaac.extended 67fc2f29d9f7bc2c -anm2ed.save 8cfeb73803e8ced0 -anm2ed.extended 67fc2f29d9f7bc2c diff --git a/tests/golden/08_source_document.hashes.xml b/tests/golden/08_source_document.hashes.xml deleted file mode 100644 index afa0c77..0000000 --- a/tests/golden/08_source_document.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 275b545f8362708a -default.extended a0eb28cc14731106 -editor.save 275b545f8362708a -editor.extended a0eb28cc14731106 -isaac.save 275b545f8362708a -isaac.extended a0eb28cc14731106 -anm2ed.save 275b545f8362708a -anm2ed.extended a0eb28cc14731106 diff --git a/tests/golden/09_legacy_quirks.hashes.xml b/tests/golden/09_legacy_quirks.hashes.xml deleted file mode 100644 index c92be7f..0000000 --- a/tests/golden/09_legacy_quirks.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 840c57e5bb2a0fe6 -default.extended ee0626347a260722 -editor.save 840c57e5bb2a0fe6 -editor.extended ee0626347a260722 -isaac.save 840c57e5bb2a0fe6 -isaac.extended ee0626347a260722 -anm2ed.save 840c57e5bb2a0fe6 -anm2ed.extended ee0626347a260722 diff --git a/tests/golden/10_paths.hashes.xml b/tests/golden/10_paths.hashes.xml deleted file mode 100644 index e2f521c..0000000 --- a/tests/golden/10_paths.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 1ec436489ff9aa8d -default.extended 2dd8495758796ce9 -editor.save 1ec436489ff9aa8d -editor.extended 2dd8495758796ce9 -isaac.save 1ec436489ff9aa8d -isaac.extended 2dd8495758796ce9 -anm2ed.save 1ec436489ff9aa8d -anm2ed.extended 2dd8495758796ce9 diff --git a/tests/golden/11_unknown.hashes.xml b/tests/golden/11_unknown.hashes.xml deleted file mode 100644 index 488090b..0000000 --- a/tests/golden/11_unknown.hashes.xml +++ /dev/null @@ -1,8 +0,0 @@ -default.save 2e68569c0d94cf3c -default.extended 54ef7a9072214850 -editor.save 2e68569c0d94cf3c -editor.extended 54ef7a9072214850 -isaac.save 2e68569c0d94cf3c -isaac.extended 54ef7a9072214850 -anm2ed.save 2e68569c0d94cf3c -anm2ed.extended 54ef7a9072214850