diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d6e0a6..93282a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,5 @@ endif() add_subdirectory(FastRTTI) add_subdirectory(glfw) add_subdirectory(Core) -add_subdirectory(ModuleLib) add_subdirectory(TestGame) add_subdirectory(ProjectGenerator) diff --git a/Core/Core/CoreInstance.cpp b/Core/Core/CoreInstance.cpp index 9f5ae64..cbe0ceb 100644 --- a/Core/Core/CoreInstance.cpp +++ b/Core/Core/CoreInstance.cpp @@ -13,8 +13,6 @@ #include "Log/Log.h" #include "Game/SaveMap/SaveMap.h" -CoreInstance* CoreInstance::self = nullptr; - extern GameInstance* GameFactory(CoreInstance&); std::shared_ptr CoreInstance::MainConfig::save() @@ -31,17 +29,8 @@ void CoreInstance::MainConfig::load(std::shared_ptr save) min_CPU_count = save->GetInteger("min_CPU_count"); } -void CoreInstance::Quit_callback() -{ - self->game_->quit(); -} - CoreInstance::CoreInstance() { - self = this; - - callbacks_.Quit = &Quit_callback; - countCPU_ = System::getCountCPU(); memorySize_ = System::getMemorySize(); @@ -87,8 +76,6 @@ CoreInstance::CoreInstance() CoreInstance::~CoreInstance() { - for (auto& module : modules_) - System::QuitModule(module.handler); modules_.clear(); delete render_engine_; @@ -100,31 +87,11 @@ CoreInstance::~CoreInstance() void CoreInstance::start() { - for (auto& name : main_config_.modules_names) - { - try - { - void* handler = System::InitModule(name.c_str(), &callbacks_); - 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'; - } - } - std::thread game_thread(&GameInstance::start, game_); + + // Main thread is render render_engine_->start(); + game_->stop(); game_thread.join(); } @@ -135,40 +102,3 @@ void CoreInstance::quit() render_engine_->stop_render(); } -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, &callbacks_); - 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"); -} diff --git a/Core/Core/CoreInstance.h b/Core/Core/CoreInstance.h index fbd793c..c6add8e 100644 --- a/Core/Core/CoreInstance.h +++ b/Core/Core/CoreInstance.h @@ -44,14 +44,8 @@ class CoreInstance { RenderEngine* render_engine_; GameInstance* game_; - static CoreInstance* self; - CoreCallBacks callbacks_; bool is_ready = false; - -#pragma region Callbacks - static void Quit_callback(); -#pragma endregion public: CoreInstance(); ~CoreInstance(); @@ -64,23 +58,4 @@ public: double getMemorySize() const { return memorySize_; } const std::string& getBaseWorldName() const { return main_config_.base_world; } const std::string& getGameName() const { return main_config_.game_name; } - - /** - * @throws std::runtime_error if module isn't enabled - * @throws std::runtime_error if module not found - * @return module handler - */ - void* getHandlerModule(const char* name) const noexcept(false); - /** - * @throws std::runtime_error if the module already enabled - * @throws std::runtime_error if the module isn't found - * @throws std::runtime_error if the module doesn't contain InitModule or StartModule - * @retuen module handler - */ - void* enableModule(const char* name) noexcept(false); - /** - * @throws std::runtime_error if the module isn't found - * @throws std::runtime_error module isn't contain QuitModule function - */ - void disableModule(const char* name) noexcept(false); }; diff --git a/Core/Core/SystemCalls.h b/Core/Core/SystemCalls.h index 2a7f8e2..4bfbaac 100644 --- a/Core/Core/SystemCalls.h +++ b/Core/Core/SystemCalls.h @@ -15,23 +15,4 @@ namespace System { * @return memory size in MB */ double getMemorySize(); - - /** - * Load module and init handler - * @throws std::runtime_error if module not found - * @return module handler - */ - void* InitModule(const char* ModuleName, CoreCallBacks* callbacks); - - /** - * Start module - * @throws std::runtime_error if module not contains StartModule function - */ - void StartModule(void* handler) noexcept(false); - - /** - * Call stop module - * @throws std::runtime_error if module not contains QuitModule function - */ - void QuitModule(void* handler) noexcept(false); } diff --git a/Core/Core/Windows/WindowsCalls.cpp b/Core/Core/Windows/WindowsCalls.cpp index ad05a22..e735662 100644 --- a/Core/Core/Windows/WindowsCalls.cpp +++ b/Core/Core/Windows/WindowsCalls.cpp @@ -41,38 +41,4 @@ double System::getMemorySize() { return static_cast(memStatus.ullTotalPhys) / 1024. / 1024.; } -void* System::InitModule(const char* ModuleName, CoreCallBacks* callbacks) { - std::string fileName = ModuleName; - fileName += ".dll"; - - HMODULE hModule = LoadLibraryA(fileName.c_str()); - if(hModule == nullptr) - throw std::runtime_error(std::to_string(GetLastError()).c_str()); - - void(*load)(CoreCallBacks*) = reinterpret_cast(GetProcAddress(hModule, "InitModule")); - if(load == nullptr) { - throw std::runtime_error(std::to_string(GetLastError()).c_str()); - } - load(callbacks); - - return hModule; -} - -void System::StartModule(void* handler) { - void(*start)() = reinterpret_cast(GetProcAddress(static_cast(handler), "StartModule")); - if(start == nullptr) { - throw std::runtime_error(std::to_string(GetLastError()).c_str()); - } - start(); -} - -void System::QuitModule(void* handler) { - void(*quit)() = reinterpret_cast(GetProcAddress(static_cast(handler), "QuitModule")); - if(quit == nullptr) { - throw std::runtime_error(std::to_string(GetLastError()).c_str()); - } - quit(); - FreeLibrary(static_cast(handler)); -} - #endif \ No newline at end of file diff --git a/Core/Game/GameInstance.h b/Core/Game/GameInstance.h index 3da336a..ac26b8c 100644 --- a/Core/Game/GameInstance.h +++ b/Core/Game/GameInstance.h @@ -17,8 +17,10 @@ class GameInstance std::atomic is_running = true; void start(); + // Stop call from core void stop(); public: + // Stop call from game void quit(); // Init vulkan diff --git a/ModuleLib/CMakeLists.txt b/ModuleLib/CMakeLists.txt deleted file mode 100644 index 37f27f8..0000000 --- a/ModuleLib/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -set(MODULE_LIB_NAME ModuleLib) - -file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp") - -add_library(${MODULE_LIB_NAME} STATIC ${SRC}) - -target_include_directories(${MODULE_LIB_NAME} PRIVATE - ${PROJECT_SOURCE_DIR}/Core - ${PROJECT_SOURCE_DIR}/Delegate -) - -if(UNIX AND NOT APPLE) - target_compile_options(${MODULE_LIB_NAME} PRIVATE -fPIC) -endif() diff --git a/ModuleLib/ModuleInstance.cpp b/ModuleLib/ModuleInstance.cpp deleted file mode 100644 index 0de6931..0000000 --- a/ModuleLib/ModuleInstance.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "ModuleInstance.h" - -#include -#include - -#include "Core/CoreCallBacks.h" - -extern ModuleInstance* Factory(); - -static std::mutex mModuleInstance; -static ModuleInstance* module = nullptr; -static std::thread* moduleThread; -// Don't free. It's memory free in core -static CoreCallBacks* callbacks_; - -void ModuleInstance::start() -{ -} - -void ModuleInstance::stop() -{ - onStop.Call(); -} - -void ModuleInstance::QuitGame() -{ - callbacks_->Quit(); -} - -void InitModule(CoreCallBacks* callbacks) -{ - std::lock_guard lock(mModuleInstance); - callbacks_ = callbacks; - if(module == nullptr) - module = Factory(); -} - -void StartModule() -{ - std::lock_guard lock(mModuleInstance); - if (module != nullptr) - moduleThread = new std::thread(&ModuleInstance::start, module); -} - -void QuitModule() -{ - std::lock_guard lock(mModuleInstance); - if(module != nullptr) - { - module->stop(); - moduleThread->join(); - delete module; - module = nullptr; - delete moduleThread; - moduleThread = nullptr; - } -} diff --git a/ModuleLib/ModuleInstance.h b/ModuleLib/ModuleInstance.h deleted file mode 100644 index fc53ca6..0000000 --- a/ModuleLib/ModuleInstance.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include "Delegate/Delegate.h" - -#ifdef _WIN32 -#define EXPORT __declspec(dllexport) -#elif __linux__ -#define EXPORT -#else -#error message("Unknow platform") -#endif -/* -* This macro protects -* module callbacks from GB -*/ -#define PROTECTION_FROM_GB(Class) \ -extern "C" {\ -EXPORT void* protect_init() {return reinterpret_cast(&InitModule);} \ -EXPORT void* protect_start() {return reinterpret_cast(&StartModule);} \ -EXPORT void* protect_quit() {return reinterpret_cast(&QuitModule);} \ -}\ -ModuleInstance* Factory() { return static_cast(new Class()); } - -struct CoreCallBacks; - -class ModuleInstance { -public: - Delegate onStop; - - virtual ~ModuleInstance() = default; - - virtual void start(); - void stop(); - - void QuitGame(); -}; - -extern "C" { - // Create module instance - EXPORT void InitModule(CoreCallBacks* callbacks); - - // Start module instance - EXPORT void StartModule(); - - // Stop module when core calls this - EXPORT void QuitModule(); -} diff --git a/TestGame/main_config.conf b/TestGame/main_config.conf index a51e392..1edecc2 100644 --- a/TestGame/main_config.conf +++ b/TestGame/main_config.conf @@ -1 +1 @@ -{"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld","vsmodules_names":[]} \ No newline at end of file +{"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld"} \ No newline at end of file