118 lines
2.4 KiB
C++
118 lines
2.4 KiB
C++
#include "CoreInstance.h"
|
|
|
|
#include "SystemCalls.h"
|
|
|
|
#include <exception>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
std::shared_ptr<SaveMap> CoreInstance::MainConfig::save()
|
|
{
|
|
return std::make_shared<SaveMap>("MainConfig");
|
|
}
|
|
|
|
void CoreInstance::MainConfig::load(std::shared_ptr<SaveMap> save)
|
|
{
|
|
game_name = save->GetString("game_name");
|
|
base_world = save->GetString("base_world");
|
|
modules_names = save->GetVectorString("modules_names");
|
|
}
|
|
|
|
CoreInstance::CoreInstance()
|
|
{
|
|
countCPU = System::getCountCPU();
|
|
memorySize = System::getMemorySize();
|
|
|
|
std::ifstream main_config_file(main_config_name);
|
|
if (main_config_file.fail())
|
|
throw std::runtime_error("Fail open main config file");
|
|
|
|
std::string payload;
|
|
std::getline(main_config_file, payload);
|
|
std::shared_ptr<SaveMap> load_main_config = std::make_shared<SaveMap>(payload);
|
|
|
|
main_config.load(load_main_config);
|
|
}
|
|
|
|
CoreInstance::~CoreInstance()
|
|
{
|
|
if (game != nullptr)
|
|
delete game;
|
|
|
|
for (auto& module : modules)
|
|
System::QuitModule(module.handler);
|
|
modules.clear();
|
|
}
|
|
|
|
extern GameInstance* GameFactory(CoreInstance&);
|
|
|
|
void CoreInstance::start()
|
|
{
|
|
for (auto& name : main_config.modules_names)
|
|
{
|
|
try
|
|
{
|
|
void* handler = System::InitModule(name.c_str(), *this);
|
|
modules.push_back(Module {name.c_str(), handler});
|
|
} catch (const std::exception& e)
|
|
{
|
|
std::cout << e.what() << '\n';
|
|
}
|
|
}
|
|
|
|
for (auto& module : modules)
|
|
{
|
|
try
|
|
{
|
|
System::StartModule(module.handler);
|
|
} catch (const std::exception& e)
|
|
{
|
|
std::cout << e.what() << '\n';
|
|
}
|
|
}
|
|
|
|
game = GameFactory(*this);
|
|
if (game == nullptr)
|
|
throw std::runtime_error("Fail create game instance!");
|
|
game->start();
|
|
}
|
|
|
|
void* CoreInstance::getHandlerModule(const char* name) const
|
|
{
|
|
for (const auto& module : modules)
|
|
{
|
|
if (std::strcmp(module.name, name) == 0)
|
|
return module.handler;
|
|
}
|
|
throw std::runtime_error("Module not found");
|
|
}
|
|
|
|
void* CoreInstance::enableModule(const char* name)
|
|
{
|
|
for (const auto& module : modules)
|
|
{
|
|
if (std::strcmp(module.name, name) == 0)
|
|
throw std::runtime_error("Module already enabled");
|
|
}
|
|
|
|
void* module = System::InitModule(name, *this);
|
|
modules.push_back({ name, module });
|
|
return module;
|
|
}
|
|
|
|
void CoreInstance::disableModule(const char* name)
|
|
{
|
|
for (auto i = modules.cbegin(); i != modules.cend(); ++i)
|
|
{
|
|
if (std::strcmp(i->name, name) == 0)
|
|
{
|
|
System::QuitModule(i->handler);
|
|
modules.erase(i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
throw std::runtime_error("Module not found");
|
|
}
|