GroveEngine/include/grove/SequentialModuleSystem.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 <memory>
#include <string>
#include <queue>
#include <chrono>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include "IModuleSystem.h"
#include "IModule.h"
using json = nlohmann::json;
namespace grove {
/**
* @brief Sequential module system implementation for debug and testing
*
* SequentialModuleSystem processes modules one at a time in a simple, predictable manner.
* Perfect for development, debugging, and testing scenarios where deterministic execution
* is more important than performance.
*
* Features:
* - Single-threaded execution (thread-safe by design)
* - Immediate task execution (no actual scheduling)
* - Comprehensive logging of all operations
* - Simple state management
* - Perfect for step-by-step debugging
*
* Task scheduling behavior:
* - scheduleTask() executes immediately (no queue)
* - hasCompletedTasks() always returns 0 (tasks complete immediately)
* - getCompletedTask() throws (no queued results)
*/
class SequentialModuleSystem : public IModuleSystem {
private:
std::shared_ptr<spdlog::logger> logger;
std::unique_ptr<IModule> module;
std::string moduleName = "unknown";
// Performance tracking
std::chrono::high_resolution_clock::time_point lastProcessTime;
size_t processCallCount = 0;
float totalProcessTime = 0.0f;
float lastProcessDuration = 0.0f;
// Task execution tracking (for logging purposes)
size_t taskExecutionCount = 0;
// Helper methods
void logSystemStart();
void logProcessStart(float deltaTime);
void logProcessEnd(float processTime);
void logTaskExecution(const std::string& taskType, const json& taskData);
void validateModule() const;
public:
SequentialModuleSystem();
virtual ~SequentialModuleSystem();
// IModuleSystem implementation
void setModule(std::unique_ptr<IModule> module) override;
IModule* getModule() const override;
int processModule(float deltaTime) override;
ModuleSystemType getType() const override;
// Hot-reload support
std::unique_ptr<IModule> extractModule();
// ITaskScheduler implementation (inherited)
void scheduleTask(const std::string& taskType, const json& taskData) override;
int hasCompletedTasks() const override;
json getCompletedTask() override;
// Debug and monitoring methods
json getPerformanceMetrics() const;
void resetPerformanceMetrics();
float getAverageProcessTime() const;
size_t getProcessCallCount() const;
size_t getTaskExecutionCount() const;
// Configuration
void setLogLevel(spdlog::level::level_enum level);
};
} // namespace grove