Changeset View
Changeset View
Standalone View
Standalone View
source/ps/DataTree.h
- This file was added.
/* Copyright (C) 2020 Wildfire Games. | |||||
* This file is part of 0 A.D. | |||||
* | |||||
* 0 A.D. is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 2 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* 0 A.D. is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef INCLUDED_DATATREE | |||||
#define INCLUDED_DATATREE | |||||
#include "ps/Filesystem.h" | |||||
template <typename T> | |||||
class DataTree : public T | |||||
{ | |||||
// This defines the interface of T | |||||
private: | |||||
using doctype = typename T::doctype; | |||||
using nodetype = typename T::nodetype; | |||||
bool LoadFile(const PIVFS& vfs, const VfsPath& filename) { return T::LoadFile(vfs, filename); } | |||||
bool CheckForInclude() const { return T::CheckForInclude(); } | |||||
bool IsIncludeNode(const nodetype& node) const { return T::IsIncludeNode(node); } | |||||
VfsPath GetIncludePath(const PIVFS& vfs, const std::deque<VfsPath>& include_paths, const nodetype& node) const | |||||
{ return T::GetIncludePath(vfs, include_paths, node); } | |||||
bool IsReplaceNode(const nodetype& node) const { return T::IsReplaceNode(node); } | |||||
nodetype GetRootNode(const doctype& doc) const { return T::GetRootNode(doc); } | |||||
nodetype GetSameChild(const nodetype& node, const nodetype& similar_to) const { return T::GetSameChild(node, similar_to); } | |||||
void UpdateNode(nodetype& node_to_update, const nodetype& reference) { T::UpdateNode(node_to_update, reference); } | |||||
void AddChild(nodetype& node, nodetype& child) { T::AddChild(node, child); } | |||||
void DeleteNode(nodetype& node) { T::DeleteNode(node); } | |||||
void PostMergeProcessing(nodetype& node) { T::PostMergeProcessing(node); } | |||||
// Actual implementation | |||||
private: | |||||
PSRETURN MergeNodes(const PIVFS& vfs, nodetype& node, nodetype merge_from) | |||||
{ | |||||
// Update node attributes | |||||
UpdateNode(node, merge_from); | |||||
// Merge mode -> loop through our own nodes, if it exists in merge_from, modulate / replace / else | |||||
// If it doesn't, just use as-is. | |||||
// Then add all unused node from the target doc. | |||||
for (nodetype child = node.begin(); child != node.end(); ++child) | |||||
{ | |||||
nodetype merge_child = GetSameChild(merge_from, child); | |||||
if (merge_child && !IsReplaceNode(child) && !IsIncludeNode(child)) | |||||
MergeNodes(vfs, child, merge_child); | |||||
else | |||||
ParseNode(vfs, child); | |||||
if (merge_child) | |||||
DeleteNode(merge_child); | |||||
} | |||||
for (nodetype child : merge_from) | |||||
AddChild(node, child); | |||||
PostMergeProcessing(node); | |||||
return 0; | |||||
} | |||||
PSRETURN ParseNode(const PIVFS& vfs, nodetype& node) | |||||
{ | |||||
// Regular path -> just recurse. | |||||
if (!IsIncludeNode(node)) | |||||
// Cannot use a range-for loop as the iterator is the node and must be passed by reference below. | |||||
for (nodetype child = node.begin(); child != node.end(); ++child) | |||||
ParseNode(vfs, child); | |||||
else | |||||
{ | |||||
// In this path, we'll load the included DataTree, then recursively merge us into them. | |||||
DataTree merge_from; | |||||
VfsPath include_path = GetIncludePath(vfs, m_IncludePaths, node); | |||||
int err = merge_from.Load(vfs, include_path); | |||||
m_Dependencies.insert(include_path); | |||||
// Not sure if std::merge would be faster. | |||||
for (const VfsPath& path : merge_from.GetDependencies()) | |||||
m_Dependencies.insert(path); | |||||
if (err) | |||||
return err; | |||||
MergeNodes(vfs, node, GetRootNode(merge_from)); | |||||
return 0; | |||||
} | |||||
return 0; | |||||
} | |||||
public: | |||||
operator doctype() { return GetDoc(); } | |||||
operator doctype() const { return GetDoc(); } | |||||
doctype GetDoc() { return T::GetDoc(); } | |||||
const doctype GetDoc() const { return T::GetDoc(); } | |||||
PSRETURN Load(const PIVFS& vfs, const VfsPath& filename) | |||||
{ | |||||
m_IncludePaths.push_front(filename.Parent()); | |||||
if (!LoadFile(vfs, filename)) | |||||
return 1; | |||||
bool use_include = CheckForInclude(); | |||||
if (!use_include) | |||||
return 0; | |||||
// Iterate over nodes. | |||||
nodetype root = GetRootNode(GetDoc()); | |||||
ParseNode(vfs, root); | |||||
return 0; | |||||
} | |||||
PSRETURN Apply(const PIVFS& vfs, const VfsPath& modulate, const VfsPath& original) | |||||
{ | |||||
Load(modulate); | |||||
DataTree og; | |||||
og.Load(original); | |||||
nodetype root = GetRootNode(GetDoc()); | |||||
nodetype original_root = GetRootNode(og.GetDoc()); | |||||
MergeNodes(vfs, root, original_root); | |||||
} | |||||
const std::set<VfsPath>& GetDependencies() const { return m_Dependencies; } | |||||
protected: | |||||
// Ordered list (decreasing priority) of include paths for new files. | |||||
std::deque<VfsPath> m_IncludePaths; | |||||
// Set of all files this relies on (i.e. all inclusions). Excludes the original file. | |||||
std::set<VfsPath> m_Dependencies; | |||||
}; | |||||
#endif // INCLUDED_DATATREE |
Wildfire Games · Phabricator