src/data.h: the curated loaders' view of a converted game document (resources/<path>.json via simdjson)
Elements are {tag, attrs, children, text} with attribute strings as written, so a loader walks them the way the game
walked rapidxml nodes and converts values with the same semantics.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1326bb1571
commit
2d3c4eb75b
2 files changed
+68
No files matched your search
@@ -0,0 +1,41 @@
|
|||||||
|
#include "data.h"
|
||||||
|
|
||||||
|
std::string dataDirectory = "resources/";
|
||||||
|
|
||||||
|
// Where a game path lives once imported: its converted JSON under the data directory.
|
||||||
|
std::string data_path(std::string_view path)
|
||||||
|
{
|
||||||
|
return dataDirectory + std::string(path) + ".json";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 : std::string_view();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 : std::string_view();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view node_text(DataNode node)
|
||||||
|
{
|
||||||
|
std::string_view text;
|
||||||
|
return node["text"].get(text) == simdjson::SUCCESS ? text : std::string_view();
|
||||||
|
}
|
||||||
|
|
||||||
|
// The element's children in document order, empty when it has none.
|
||||||
|
simdjson::dom::array node_children(DataNode node)
|
||||||
|
{
|
||||||
|
simdjson::dom::array children;
|
||||||
|
return node["children"].get(children) == simdjson::SUCCESS ? children : simdjson::dom::array();
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "simdjson.h"
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
// A game document the import tool converted from XML (items.xml, an .anm2 ...) into resources/<path>.json.
|
||||||
|
// Each element is {"tag", "attrs", "children", "text"}, with every attribute value kept as the string the XML held,
|
||||||
|
// so loaders read it the way the game read its rapidxml nodes.
|
||||||
|
struct Data
|
||||||
|
{
|
||||||
|
simdjson::dom::parser parser;
|
||||||
|
simdjson::dom::element root;
|
||||||
|
bool isLoaded = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
using DataNode = simdjson::dom::element;
|
||||||
|
|
||||||
|
void data_load(Data& data, std::string_view path);
|
||||||
|
std::string data_path(std::string_view path);
|
||||||
|
|
||||||
|
std::string_view node_tag(DataNode node);
|
||||||
|
std::string_view node_attr(DataNode node, std::string_view name);
|
||||||
|
std::string_view node_text(DataNode node);
|
||||||
|
simdjson::dom::array node_children(DataNode node);
|
||||||
|
|
||||||
|
extern std::string dataDirectory;
|
||||||
Reference in new issue
Block a user