#include "CoreInstance.h" #include "SystemCalls.h" #include #include #include #include CoreInstance::CoreInstance() { countCPU = System::getCountCPU(); memorySize = System::getMemorySize(); std::ifstream main_config_file(main_config); if (main_config_file.fail()) throw std::runtime_error("Fail open main config file"); std::string line; while (std::getline(main_config_file, line)) { if (base_world.empty() && line.find(key_base_world) == 0) { size_t pos = line.find_first_of(':') + 1; base_world = line.substr(pos, line.size() - pos); // trim while (base_world[0] == ' ') base_world.erase(0, 1); while (base_world[base_world.size() - 1] == ' ') base_world.erase(base_world.size() - 2, base_world.size() - 1); } else if (modules_names.empty() && line.find(key_modules) == 0) { size_t pos = line.find(':') + 1; line.erase(0, pos); size_t end = line.find(','); while (end != std::string::npos) { // trim while (line[0] == ' ') line.erase(0, 1); std::string module_name = line.substr(0, end - 1); // trim while (module_name[module_name.size() - 1] == ' ') module_name.erase(module_name.size() - 2, module_name.size() - 1); modules_names.push_back(module_name); line.erase(0, end); end = line.find(','); } } } if (base_world.empty()) throw std::runtime_error("Fail read base world name"); } 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 : 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"); }