GroveEngine/include/grove/ModuleFactory.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

102 lines
3.5 KiB
C++

#pragma once
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include <functional>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include "IModule.h"
using json = nlohmann::json;
namespace grove {
/**
* @brief Factory for loading and creating modules from shared libraries (.so files)
*
* ModuleFactory handles dynamic loading of module implementations from .so files.
* It manages symbol resolution, error handling, and module lifecycle.
*
* Features:
* - Dynamic loading of .so files with dlopen/dlsym
* - Automatic symbol resolution for module entry points
* - Hot-reload support with proper cleanup
* - Comprehensive error reporting and logging
* - Module registration and discovery
* - Thread-safe operations
*
* Expected module .so structure:
* - extern "C" IModule* create_module()
* - extern "C" void destroy_module(IModule*)
* - extern "C" const char* get_module_type()
* - extern "C" const char* get_module_version()
*/
class ModuleFactory {
public:
struct ModuleInfo {
std::string path;
std::string type;
std::string version;
void* handle = nullptr;
std::function<IModule*()> createFunc;
std::function<void(IModule*)> destroyFunc;
};
ModuleFactory();
~ModuleFactory();
// Module loading
std::unique_ptr<IModule> loadModule(const std::string& modulePath);
std::unique_ptr<IModule> createModule(const std::string& moduleType);
// Module discovery and registration
void scanModulesDirectory(const std::string& directory);
void registerModule(const std::string& modulePath);
void unloadModule(const std::string& moduleType);
void unloadAllModules();
// Information and diagnostics
std::vector<std::string> getAvailableModules() const;
std::vector<std::string> getLoadedModules() const;
ModuleInfo getModuleInfo(const std::string& moduleType) const;
bool isModuleLoaded(const std::string& moduleType) const;
bool isModuleAvailable(const std::string& moduleType) const;
// Configuration
void setModulesDirectory(const std::string& directory);
std::string getModulesDirectory() const;
// Hot-reload support
bool reloadModule(const std::string& moduleType);
void enableHotReload(bool enable);
bool isHotReloadEnabled() const;
// Diagnostics and debugging
json getDetailedStatus() const;
void validateModule(const std::string& modulePath);
void setLogLevel(spdlog::level::level_enum level);
private:
std::shared_ptr<spdlog::logger> logger;
std::string modulesDirectory;
bool hotReloadEnabled = false;
// Module registry
std::unordered_map<std::string, ModuleInfo> loadedModules;
std::unordered_map<std::string, std::string> availableModules; // type -> path
// Helper methods
std::shared_ptr<spdlog::logger> getFactoryLogger();
bool loadSharedLibrary(const std::string& path, ModuleInfo& info);
void unloadSharedLibrary(ModuleInfo& info);
bool resolveSymbols(ModuleInfo& info);
std::string extractModuleTypeFromPath(const std::string& path) const;
bool isValidModuleFile(const std::string& path) const;
void logModuleLoad(const std::string& type, const std::string& path) const;
void logModuleUnload(const std::string& type) const;
void logModuleError(const std::string& operation, const std::string& details) const;
};
} // namespace grove