#pragma once #include "IModule.h" #include #include #include using json = nlohmann::json; namespace warfactory { /** * @brief Base helper class for modules * * Provides common functionality and JSON helpers * LOCAL COPY - Completely autonomous */ class ModuleBase : public IModule { protected: std::string module_name; json module_config; bool initialized = false; public: explicit ModuleBase(const std::string& name) : module_name(name) {} std::string getName() const override { return module_name; } void initialize(const json& config) override { module_config = config; initialized = true; onInitialize(config); } void shutdown() override { onShutdown(); initialized = false; } json getMetadata() const override { return { {"name", module_name}, {"version", "1.0.0"}, {"initialized", initialized}, {"type", "game_module"} }; } protected: // Override these in concrete modules virtual void onInitialize(const json& config) {} virtual void onShutdown() {} // JSON helpers bool hasField(const json& obj, const std::string& field) const { return obj.contains(field) && !obj[field].is_null(); } template T getField(const json& obj, const std::string& field, const T& defaultValue = T{}) const { if (hasField(obj, field)) { return obj[field].get(); } return defaultValue; } void ensureInitialized() const { if (!initialized) { throw std::runtime_error("Module " + module_name + " not initialized"); } } }; } // namespace warfactory