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 LoadString(const std::string& data) { return T::LoadString(data); } | |||||
bool CheckForInclude() const { return T::CheckForInclude(); } | |||||
bool IsDisableNode(const nodetype& node) const { return T::IsDisableNode(node); } | |||||
bool IsIncludeNode(const nodetype& node) const { return T::IsIncludeNode(node); } | |||||
bool IsFilterNode(const nodetype& node) const { return T::IsFilterNode(node); }; | |||||
bool IsReplaceNode(const nodetype& node) const { return T::IsReplaceNode(node); } | |||||
std::vector<VfsPath> GetIncludeFiles(const nodetype& node) const { return T::GetIncludeFiles(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); } | |||||
nodetype DeleteNode(nodetype& node) { return T::DeleteNode(node); } | |||||
void NodePostProcessing(nodetype& node) { T::NodePostProcessing(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();) | |||||
{ | |||||
nodetype merge_child = GetSameChild(merge_from, child); | |||||
if (IsDisableNode(child)) | |||||
{ | |||||
if (merge_child) | |||||
DeleteNode(merge_child); | |||||
child = DeleteNode(child); | |||||
continue; | |||||
} | |||||
else if (merge_child && !IsReplaceNode(child) && !IsIncludeNode(child) && !IsFilterNode(child)) | |||||
{ | |||||
MergeNodes(vfs, child, merge_child); | |||||
++child; | |||||
} | |||||
else | |||||
{ | |||||
ParseNode(vfs, child); | |||||
++child; | |||||
} | |||||
if (merge_child) | |||||
DeleteNode(merge_child); | |||||
} | |||||
for (nodetype child : merge_from) | |||||
AddChild(node, child); | |||||
NodePostProcessing(node); | |||||
return 0; | |||||
} | |||||
PSRETURN FilterNodes(const PIVFS& vfs, nodetype& node, nodetype filter_from) | |||||
{ | |||||
// Filter mode -> loop through our own nodes, if it exists in filter_from, keep. | |||||
// Ignore nodes we don't find. | |||||
for (nodetype child = node.begin(); child != node.end(); ++child) | |||||
{ | |||||
nodetype merge_child = GetSameChild(filter_from, child); | |||||
if (merge_child) | |||||
MergeNodes(vfs, child, merge_child); | |||||
} | |||||
NodePostProcessing(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); | |||||
NodePostProcessing(node); | |||||
} | |||||
else | |||||
{ | |||||
// In this path, we'll load the included DataTree, then recursively merge us into them. | |||||
DataTree merge_from(m_IncludePaths); | |||||
std::vector<VfsPath> include_files = GetIncludeFiles(node); | |||||
// Simple path: only one file. | |||||
if (include_files.size() == 1) | |||||
{ | |||||
int err = merge_from.Load(vfs, include_files.front(), m_Filename); | |||||
m_Dependencies.insert(include_files.front()); | |||||
// Not sure if std::merge would be faster. | |||||
for (const VfsPath& path : merge_from.GetDependencies()) | |||||
m_Dependencies.insert(path); | |||||
if (err) | |||||
return err; | |||||
} | |||||
else | |||||
{ | |||||
// Apply from end to start. | |||||
std::vector<VfsPath>::const_reverse_iterator next = ++include_files.rbegin(); | |||||
DataTree current(m_IncludePaths); | |||||
current.Load(vfs, include_files.back(), m_Filename); | |||||
m_Dependencies.insert(include_files.back()); | |||||
while (next != include_files.rend()) | |||||
{ | |||||
DataTree modulate(m_IncludePaths); | |||||
modulate.Load(vfs, *next, m_Filename); | |||||
m_Dependencies.insert(*next); | |||||
modulate.ApplyOver(vfs, current); // Handles dependencies | |||||
current = std::move(modulate); | |||||
++next; | |||||
} | |||||
merge_from = std::move(current); | |||||
} | |||||
if (IsFilterNode(node)) | |||||
FilterNodes(vfs, node, GetRootNode(merge_from)); | |||||
else | |||||
MergeNodes(vfs, node, GetRootNode(merge_from)); | |||||
return 0; | |||||
} | |||||
return 0; | |||||
} | |||||
public: | |||||
DataTree() = default; | |||||
DataTree(const std::deque<VfsPath>& includePaths) : m_IncludePaths(includePaths) {}; | |||||
operator doctype() { return GetDoc(); } | |||||
operator doctype() const { return GetDoc(); } | |||||
doctype GetDoc() { return T::GetDoc(); } | |||||
const doctype GetDoc() const { return T::GetDoc(); } | |||||
void AddIncludePath(const VfsPath& path) | |||||
{ | |||||
// Trust the caller to not include the same path several time | |||||
// (it's not buggy, just inefficient). | |||||
m_IncludePaths.push_back(path); | |||||
} | |||||
PSRETURN Load(const PIVFS& vfs, const VfsPath& filename, const VfsPath& include_from = "") | |||||
{ | |||||
bool loaded = false; | |||||
std::deque<VfsPath> paths = m_IncludePaths; | |||||
paths.emplace_front(include_from); | |||||
for (const VfsPath& path : paths) | |||||
{ | |||||
VfsPath file = path / filename; | |||||
if (vfs->GetFileInfo(file, 0) != INFO::OK) | |||||
continue; | |||||
if (!LoadFile(vfs, file)) | |||||
{ | |||||
LOGWARNING("DataTree: error loading file %s", filename.string8()); | |||||
return 1; | |||||
} | |||||
loaded = true; | |||||
m_Filename = file; | |||||
break; | |||||
} | |||||
if (!loaded) | |||||
{ | |||||
LOGWARNING("DataTree: File %s not found", filename.string8()); | |||||
return 1; | |||||
} | |||||
bool use_include = CheckForInclude(); | |||||
if (!use_include) | |||||
return 0; | |||||
// Iterate over nodes. | |||||
nodetype root = GetRootNode(GetDoc()); | |||||
ParseNode(vfs, root); | |||||
return 0; | |||||
} | |||||
PSRETURN Load(const PIVFS& vfs, const std::string& data) | |||||
{ | |||||
if (!LoadString(data)) | |||||
{ | |||||
LOGWARNING("DataTree: error loading string: %s", data); | |||||
return 1; | |||||
} | |||||
bool use_include = CheckForInclude(); | |||||
if (!use_include) | |||||
return 0; | |||||
// Iterate over nodes. | |||||
nodetype root = GetRootNode(GetDoc()); | |||||
ParseNode(vfs, root); | |||||
return 0; | |||||
} | |||||
PSRETURN ApplyOver(const PIVFS& vfs, DataTree& original) | |||||
{ | |||||
// Not sure if std::merge would be faster. | |||||
for (const VfsPath& path : original.GetDependencies()) | |||||
m_Dependencies.insert(path); | |||||
nodetype root = GetRootNode(GetDoc()); | |||||
nodetype original_root = GetRootNode(original.GetDoc()); | |||||
if (IsFilterNode(root)) | |||||
FilterNodes(vfs, root, original_root); | |||||
else | |||||
MergeNodes(vfs, root, original_root); | |||||
return 0; | |||||
} | |||||
const std::set<VfsPath>& GetDependencies() const { return m_Dependencies; } | |||||
protected: | |||||
VfsPath m_Filename; | |||||
// 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