GroveEngine/include/grove/JsonDataTree.h
StillHammer fad105afb2 feat: Implement complete IDataNode/IDataTree system with JSON backend
Major feature: Unified config/data/runtime tree system

**New System Architecture:**
- Unified data tree for config, persistent data, and runtime state
- Three separate roots: config/ (read-only + hot-reload), data/ (read-write + save), runtime/ (temporary)
- Support for modding, saves, and hot-reload in single system

**Interfaces:**
- IDataValue: Abstract data value interface (type-safe access)
- IDataNode: Tree node with navigation, search, and modification
- IDataTree: Root container with config/data/runtime management

**Concrete Implementations:**
- JsonDataValue: nlohmann::json backed value
- JsonDataNode: Full tree navigation with pattern matching & queries
- JsonDataTree: File-based JSON storage with hot-reload

**Features:**
- Pattern matching search (wildcards support)
- Property-based queries with predicates
- SHA256 hashing for validation/sync
- Hot-reload for config/ directory
- Save operations for data/ persistence
- Read-only enforcement for config/

**API Changes:**
- All namespaces changed from 'warfactory' to 'grove'
- IDataTree: Added getConfigRoot(), getDataRoot(), getRuntimeRoot()
- IDataTree: Added saveData(), saveNode() for persistence
- IDataNode: Added setChild(), removeChild(), clearChildren()
- CMakeLists.txt: Added OpenSSL dependency for hashing

**Usage:**
```cpp
auto tree = DataTreeFactory::create("json", "./gamedata");
auto config = tree->getConfigRoot();     // Read-only game config
auto data = tree->getDataRoot();         // Player saves
auto runtime = tree->getRuntimeRoot();   // Temporary state

// Hot-reload config on file changes
if (tree->reloadIfChanged()) { /* refresh modules */ }

// Save player progress
data->setChild("progress", progressNode);
tree->saveData();
```

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 15:36:25 +08:00

87 lines
2.7 KiB
C++

#pragma once
#include "IDataTree.h"
#include "JsonDataNode.h"
#include <string>
#include <memory>
#include <functional>
#include <map>
#include <chrono>
#include <filesystem>
namespace grove {
/**
* @brief Concrete implementation of IDataTree backed by JSON files
*
* Manages three separate trees:
* - config/ : Read-only configuration loaded from files (hot-reload enabled)
* - data/ : Persistent player data (read-write, saved to disk)
* - runtime/ : Temporary runtime state (read-write, never saved)
*
* File structure:
* basePath/
* ├─ config/
* │ ├─ tanks.json
* │ ├─ weapons.json
* │ └─ ...
* ├─ data/
* │ ├─ campaign.json
* │ ├─ unlocks.json
* │ └─ ...
* └─ runtime/ (in-memory only, not on disk)
*/
class JsonDataTree : public IDataTree {
public:
/**
* @brief Create a data tree from a base directory
* @param basePath Base directory containing config/, data/ subdirs
*/
explicit JsonDataTree(const std::string& basePath);
virtual ~JsonDataTree() = default;
// Tree access
std::unique_ptr<IDataNode> getRoot() override;
std::unique_ptr<IDataNode> getNode(const std::string& path) override;
// Separate roots
std::unique_ptr<IDataNode> getConfigRoot() override;
std::unique_ptr<IDataNode> getDataRoot() override;
std::unique_ptr<IDataNode> getRuntimeRoot() override;
// Save operations
bool saveData() override;
bool saveNode(const std::string& path) override;
// Hot-reload
bool checkForChanges() override;
bool reloadIfChanged() override;
void onTreeReloaded(std::function<void()> callback) override;
// Metadata
std::string getType() override;
private:
std::string m_basePath;
std::unique_ptr<JsonDataNode> m_root;
std::unique_ptr<JsonDataNode> m_configRoot;
std::unique_ptr<JsonDataNode> m_dataRoot;
std::unique_ptr<JsonDataNode> m_runtimeRoot;
std::map<std::string, std::filesystem::file_time_type> m_configFileTimes;
std::vector<std::function<void()>> m_reloadCallbacks;
// Helper methods
void loadConfigTree();
void loadDataTree();
void initializeRuntimeTree();
void scanDirectory(const std::string& dirPath, JsonDataNode* parentNode, bool readOnly);
json loadJsonFile(const std::string& filePath);
bool saveJsonFile(const std::string& filePath, const json& data);
void buildNodeFromJson(const std::string& name, const json& data, JsonDataNode* parentNode, bool readOnly);
json nodeToJson(const JsonDataNode* node);
void updateFileTimestamps(const std::string& dirPath);
};
} // namespace grove