#include "data.h" #include "manager.h" #include #include constexpr std::string_view EMPTY_ARRAY = "[]"; // What a missing tag, attribute or text reads as: empty, but C text that atoi can take. constexpr std::string_view EMPTY_TEXT = ""; constexpr std::string_view JSON_EXTENSION = ".json"; // Where the import tool puts a language archive's files (the original's resources. mount). constexpr std::string_view LANGUAGE_FOLDER = "lang/"; std::string dataDirectory = "resources/"; std::string languageDirectory; // A game path lowercased with forward slashes, as the game normalizes every path before a lookup. static std::string path_normalize(std::string_view path) { std::string normalized(path); for (char& letter : normalized) letter = letter == '\\' ? '/' : (char)std::tolower((unsigned char)letter); return normalized; } // A normalized path under the language's folder when that holds it, under the data directory otherwise. static std::string mount_resolve(const std::string& normalized) { std::error_code error; std::string localized = languageDirectory + normalized; if (!languageDirectory.empty() && std::filesystem::exists(localized, error)) return localized; return dataDirectory + normalized; } // Where a game path lives once imported. std::string resource_path(std::string_view path) { return mount_resolve(path_normalize(path)); } // Where a converted XML document lives: its game path with ".json" appended. std::string data_path(std::string_view path) { return mount_resolve(path_normalize(path) + std::string(JSON_EXTENSION)); } // 009abbd0 Remounts the resources, with the folder of the string table's language in front once there is a manager. // The language folder is only mounted when the import tool wrote one, which finds the same files. // The original also mounts the game folder behind the resources when its file system already knows it. Nothing on // Linux reads from there. void reload_resource_paths() { languageDirectory.clear(); if (!g_manager) return; std::string localized = dataDirectory + std::string(LANGUAGE_FOLDER) + std::string(Manager::GetLanguage()) + '/'; std::error_code error; if (std::filesystem::is_directory(localized, error)) languageDirectory = localized; } // Loads a converted document, leaving isLoaded false when it is missing or unreadable. void data_load(Data& data, std::string_view path) { data.isLoaded = data.parser.load(data_path(path)).get(data.root) == simdjson::SUCCESS; } std::string_view node_tag(DataNode node) { std::string_view tag; return node["tag"].get(tag) == simdjson::SUCCESS ? tag : EMPTY_TEXT; } // An attribute's raw string, or an empty one when the element lacks it. std::string_view node_attr(DataNode node, std::string_view name) { std::string_view value; return node["attrs"][name].get(value) == simdjson::SUCCESS ? value : EMPTY_TEXT; } std::string_view node_text(DataNode node) { std::string_view text; return node["text"].get(text) == simdjson::SUCCESS ? text : EMPTY_TEXT; } // The element's children in document order, empty when it has none. // A default-constructed array points at no document, so a parsed empty one stands in. simdjson::dom::array node_children(DataNode node) { static simdjson::dom::parser emptyParser; static simdjson::dom::array empty = emptyParser.parse(EMPTY_ARRAY.data(), EMPTY_ARRAY.size()).get_array().value_unsafe(); simdjson::dom::array children; return node["children"].get(children) == simdjson::SUCCESS ? children : empty; }