Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
!.vscode/launch.json
!.vscode/extensions.json

#Visual Studio
.vs/*

# Private code that will not be shared publically
/libs/internal/
/.ccls-cache/
Expand Down
8 changes: 7 additions & 1 deletion libs/xml-operations/include/xml_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include <optional>
#include <string>
#include <vector>
#include <stack>
#include <unordered_set>

namespace fs = std::filesystem;

class XmlOperation
{
public:
enum Type { None, Add, AddNextSibling, AddPrevSibling, Remove, Replace, Merge };
enum class Type { None, Add, AddNextSibling, AddPrevSibling, Remove, Replace, Merge, Patch };

XmlOperation(std::shared_ptr<pugi::xml_document> doc, pugi::xml_node node,
std::string guid = "", std::string temp = "", std::string mod_name = "",
Expand Down Expand Up @@ -70,6 +72,10 @@ class XmlOperation
}
void RecursiveMerge(pugi::xml_node root_game_node, pugi::xml_node game_node,
pugi::xml_node patching_node);
void PatchOp(pugi::xml_object_range<pugi::xml_node_iterator> content_node,
pugi::xml_node game_node);
void PatchNode(pugi::xml_node game_node, pugi::xml_node patching_node);

void ReadPath(pugi::xml_node node, std::string guid = "", std::string temp = "");
void ReadType(pugi::xml_node node, std::string mod_name, fs::path game_path, fs::path mod_path);

Expand Down
96 changes: 96 additions & 0 deletions libs/xml-operations/src/xml_operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ void XmlOperation::ReadType(pugi::xml_node node, std::string mod_name, fs::path
type_ = Type::Replace;
} else if (stricmp(type.c_str(), "merge") == 0) {
type_ = Type::Merge;
} else if (stricmp(type.c_str(), "patch") == 0) {
type_ = Type::Patch;
} else {
type_ = Type::None;
offset_data_t offset_data;
Expand Down Expand Up @@ -375,6 +377,9 @@ void XmlOperation::Apply(std::shared_ptr<pugi::xml_document> doc)
}
pugi::xml_node patching_node = *content_node.begin();
RecursiveMerge(game_node, game_node, patching_node);
} else if (GetType() == XmlOperation::Type::Patch){
auto content_node = GetContentNode();
PatchOp(content_node, game_node);
} else if (GetType() == XmlOperation::Type::AddNextSibling) {
for (auto &&node : GetContentNode()) {
game_node = game_node.parent().insert_copy_after(node, game_node);
Expand Down Expand Up @@ -559,6 +564,97 @@ void XmlOperation::RecursiveMerge(pugi::xml_node root_game_node, pugi::xml_node
}
}

void XmlOperation::PatchOp(pugi::xml_object_range<pugi::xml_node_iterator> content_node,
pugi::xml_node game_node)
{
auto num_content_nodes = std::distance(content_node.begin(), content_node.end());
if (num_content_nodes <= 0) {
return;
} else if (num_content_nodes > 1) {
spdlog::error("Patch ModOps can only have one patching node, but {0} "
"were supplied. "
"If you want to target multiple siblings, consider either "
"targeting their parent or "
"targeting the items with an XPath expression.",
num_content_nodes);
return;
} else {
pugi::xml_node patching_node = *content_node.begin();
if (std::string(patching_node.name()) == std::string(game_node.name())) {
PatchNode(game_node, patching_node);
} else {
spdlog::error("Patch ModOps can only have a patching node that has "
"the same name as the target node, "
"but the patching node is named {0} while the targeted node "
"is named {1}. ",
std::string(patching_node.name()), std::string(game_node.name()));
}
}
}

void XmlOperation::PatchNode(pugi::xml_node game_node,
pugi::xml_node patching_node)
{
std::stack<std::tuple<int, pugi::xml_node, pugi::xml_node>> nodes;

struct xml_hash {
size_t operator()(const pugi::xml_node &x) const
{
return x.hash_value();
}
};
std::unordered_set<pugi::xml_node, xml_hash> patched_game_nodes;

nodes.emplace(1, game_node, patching_node);

while (!nodes.empty()) {
auto [level, game_node, patch_node] = nodes.top();
nodes.pop();

if (!patch_node || !game_node) {
continue;
}

// Already patched game nodes shouldn't be considered
if (patched_game_nodes.count(game_node) > 0) {
nodes.emplace(level, game_node.next_sibling(patch_node.name()), patch_node);
continue;
}

using node_type = pugi::xml_node_type;

// If both are plain character nodes we want to patch them now
// All the other code made sure that they are a match (name)
if (game_node.type() == node_type::node_pcdata
&& patch_node.type() == node_type::node_pcdata) {
patched_game_nodes.emplace(game_node);
game_node.set_value(patch_node.value());
} else if (game_node.type() != patch_node.type()) {
// If the types of the node don't match, we can't consider this branch for further
// processing
spdlog::info("Skip");
} else if (game_node.type() == node_type::node_element
&& strcmp(game_node.name(), patch_node.name()) == 0) {
// This is a valid match for a subtree node, so we merge props and then move on to the
// next things
MergeProperties(game_node, patch_node);
// Traverse down the tree by pushing all patch children with the first game child
// We don't have to worry here whether one of them is null as that is checked at the
// start of the loop
auto patch_child = patch_node.last_child();
while (patch_child) {
nodes.emplace(level + 1, game_node.first_child(), patch_child);
patch_child = patch_child.previous_sibling();
}

patched_game_nodes.emplace(game_node);

} else {
nodes.emplace(level, game_node.next_sibling(patch_node.name()), patch_node);
}
}
}

std::string XmlOperation::GetPath()
{
return path_;
Expand Down
2 changes: 1 addition & 1 deletion tests/xml/merge/merge_strange_shit_input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
</Asset>
</Assets>
</Group>
</Group>
</Groups>
</AssetList>
9 changes: 9 additions & 0 deletions tests/xml/patch/patch_multi_node_content_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "patch content",
"expected": [
"/Test/Node/FullSatisfactionDistance",
"/Test/Node/NoSatisfactionDistance",
"/Test/Node[FullSatisfactionDistance='60']",
"/Test/Node[NoSatisfactionDistance='90']"
]
}
6 changes: 6 additions & 0 deletions tests/xml/patch/patch_multi_node_content_1_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Test>
<Node>
<FullSatisfactionDistance>5</FullSatisfactionDistance>
<NoSatisfactionDistance>5</NoSatisfactionDistance>
</Node>
</Test>
8 changes: 8 additions & 0 deletions tests/xml/patch/patch_multi_node_content_1_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ModOps>
<ModOp Type="patch" Path="/Test/Node">
<Node>
<FullSatisfactionDistance>60</FullSatisfactionDistance>
<NoSatisfactionDistance>90</NoSatisfactionDistance>
</Node>
</ModOp>
</ModOps>
9 changes: 9 additions & 0 deletions tests/xml/patch/patch_multi_node_content_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "patch content nested",
"expected": [
"/Test/Node/FullSatisfactionDistance",
"/Test/Node/NoSatisfactionDistance",
"/Test/Node[FullSatisfactionDistance='60']",
"/Test/Node/NoSatisfactionDistance[NoSatisfactionDistance2='100']"
]
}
8 changes: 8 additions & 0 deletions tests/xml/patch/patch_multi_node_content_2_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Test>
<Node>
<FullSatisfactionDistance>5</FullSatisfactionDistance>
<NoSatisfactionDistance>
<NoSatisfactionDistance2>90</NoSatisfactionDistance2>
</NoSatisfactionDistance>
</Node>
</Test>
10 changes: 10 additions & 0 deletions tests/xml/patch/patch_multi_node_content_2_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ModOps>
<ModOp Type="patch" Path="/Test/Node">
<Node>
<FullSatisfactionDistance>60</FullSatisfactionDistance>
<NoSatisfactionDistance>
<NoSatisfactionDistance2>100</NoSatisfactionDistance2>
</NoSatisfactionDistance>
</Node>
</ModOp>
</ModOps>
9 changes: 9 additions & 0 deletions tests/xml/patch/patch_multi_node_content_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "patch content nested missing parent",
"expected": [
"/Test/Node/FullSatisfactionDistance",
"/Test/Node/NoSatisfactionDistance",
"/Test/Node[FullSatisfactionDistance='60']",
"/Test/Node/NoSatisfactionDistance[NoSatisfactionDistance2='90']"
]
}
8 changes: 8 additions & 0 deletions tests/xml/patch/patch_multi_node_content_3_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Test>
<Node>
<FullSatisfactionDistance>5</FullSatisfactionDistance>
<NoSatisfactionDistance>
<NoSatisfactionDistance2>90</NoSatisfactionDistance2>
</NoSatisfactionDistance>
</Node>
</Test>
8 changes: 8 additions & 0 deletions tests/xml/patch/patch_multi_node_content_3_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ModOps>
<ModOp Type="patch" Path="/Test/Node">
<Node>
<FullSatisfactionDistance>60</FullSatisfactionDistance>
<NoSatisfactionDistance2>100</NoSatisfactionDistance2>
</Node>
</ModOp>
</ModOps>
9 changes: 9 additions & 0 deletions tests/xml/patch/patch_multi_node_content_fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Multi node patch content - patching fails",
"expected": [
"/Test/Node/FullSatisfactionDistance",
"/Test/Node/NoSatisfactionDistance",
"/Test/Node[FullSatisfactionDistance='5']",
"/Test/Node[NoSatisfactionDistance='5']"
]
}
6 changes: 6 additions & 0 deletions tests/xml/patch/patch_multi_node_content_fail_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Test>
<Node>
<FullSatisfactionDistance>5</FullSatisfactionDistance>
<NoSatisfactionDistance>5</NoSatisfactionDistance>
</Node>
</Test>
6 changes: 6 additions & 0 deletions tests/xml/patch/patch_multi_node_content_fail_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ModOps>
<ModOp Type="patch" Path="/Test/Node">
<FullSatisfactionDistance>60</FullSatisfactionDistance>
<NoSatisfactionDistance>90</NoSatisfactionDistance>
</ModOp>
</ModOps>
7 changes: 7 additions & 0 deletions tests/xml/patch/patch_second_child_only.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "patch second list child only without XPath",
"expected": [
"//Values/Cost/Costs/Item[Ingredient='1010017' and Amount='500']",
"//Values/Cost/Costs/Item[Ingredient='1010196' and Amount='123']"
]
}
Loading