Remove old module system
This commit is contained in:
@@ -19,6 +19,5 @@ endif()
|
|||||||
add_subdirectory(FastRTTI)
|
add_subdirectory(FastRTTI)
|
||||||
add_subdirectory(glfw)
|
add_subdirectory(glfw)
|
||||||
add_subdirectory(Core)
|
add_subdirectory(Core)
|
||||||
add_subdirectory(ModuleLib)
|
|
||||||
add_subdirectory(TestGame)
|
add_subdirectory(TestGame)
|
||||||
add_subdirectory(ProjectGenerator)
|
add_subdirectory(ProjectGenerator)
|
||||||
|
|||||||
@@ -13,8 +13,6 @@
|
|||||||
#include "Log/Log.h"
|
#include "Log/Log.h"
|
||||||
#include "Game/SaveMap/SaveMap.h"
|
#include "Game/SaveMap/SaveMap.h"
|
||||||
|
|
||||||
CoreInstance* CoreInstance::self = nullptr;
|
|
||||||
|
|
||||||
extern GameInstance* GameFactory(CoreInstance&);
|
extern GameInstance* GameFactory(CoreInstance&);
|
||||||
|
|
||||||
std::shared_ptr<SaveMap> CoreInstance::MainConfig::save()
|
std::shared_ptr<SaveMap> CoreInstance::MainConfig::save()
|
||||||
@@ -31,17 +29,8 @@ void CoreInstance::MainConfig::load(std::shared_ptr<SaveMap> save)
|
|||||||
min_CPU_count = save->GetInteger("min_CPU_count");
|
min_CPU_count = save->GetInteger("min_CPU_count");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreInstance::Quit_callback()
|
|
||||||
{
|
|
||||||
self->game_->quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
CoreInstance::CoreInstance()
|
CoreInstance::CoreInstance()
|
||||||
{
|
{
|
||||||
self = this;
|
|
||||||
|
|
||||||
callbacks_.Quit = &Quit_callback;
|
|
||||||
|
|
||||||
countCPU_ = System::getCountCPU();
|
countCPU_ = System::getCountCPU();
|
||||||
memorySize_ = System::getMemorySize();
|
memorySize_ = System::getMemorySize();
|
||||||
|
|
||||||
@@ -87,8 +76,6 @@ CoreInstance::CoreInstance()
|
|||||||
|
|
||||||
CoreInstance::~CoreInstance()
|
CoreInstance::~CoreInstance()
|
||||||
{
|
{
|
||||||
for (auto& module : modules_)
|
|
||||||
System::QuitModule(module.handler);
|
|
||||||
modules_.clear();
|
modules_.clear();
|
||||||
|
|
||||||
delete render_engine_;
|
delete render_engine_;
|
||||||
@@ -100,31 +87,11 @@ CoreInstance::~CoreInstance()
|
|||||||
|
|
||||||
void CoreInstance::start()
|
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_);
|
std::thread game_thread(&GameInstance::start, game_);
|
||||||
|
|
||||||
|
// Main thread is render
|
||||||
render_engine_->start();
|
render_engine_->start();
|
||||||
|
|
||||||
game_->stop();
|
game_->stop();
|
||||||
game_thread.join();
|
game_thread.join();
|
||||||
}
|
}
|
||||||
@@ -135,40 +102,3 @@ void CoreInstance::quit()
|
|||||||
render_engine_->stop_render();
|
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");
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -44,14 +44,8 @@ class CoreInstance {
|
|||||||
RenderEngine* render_engine_;
|
RenderEngine* render_engine_;
|
||||||
GameInstance* game_;
|
GameInstance* game_;
|
||||||
|
|
||||||
static CoreInstance* self;
|
|
||||||
CoreCallBacks callbacks_;
|
|
||||||
|
|
||||||
bool is_ready = false;
|
bool is_ready = false;
|
||||||
|
|
||||||
#pragma region Callbacks
|
|
||||||
static void Quit_callback();
|
|
||||||
#pragma endregion
|
|
||||||
public:
|
public:
|
||||||
CoreInstance();
|
CoreInstance();
|
||||||
~CoreInstance();
|
~CoreInstance();
|
||||||
@@ -64,23 +58,4 @@ public:
|
|||||||
double getMemorySize() const { return memorySize_; }
|
double getMemorySize() const { return memorySize_; }
|
||||||
const std::string& getBaseWorldName() const { return main_config_.base_world; }
|
const std::string& getBaseWorldName() const { return main_config_.base_world; }
|
||||||
const std::string& getGameName() const { return main_config_.game_name; }
|
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);
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -15,23 +15,4 @@ namespace System {
|
|||||||
* @return memory size in MB
|
* @return memory size in MB
|
||||||
*/
|
*/
|
||||||
double getMemorySize();
|
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,38 +41,4 @@ double System::getMemorySize() {
|
|||||||
return static_cast<double>(memStatus.ullTotalPhys) / 1024. / 1024.;
|
return static_cast<double>(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<void(*)(CoreCallBacks*)>(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<void(*)()>(GetProcAddress(static_cast<HMODULE>(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<void(*)()>(GetProcAddress(static_cast<HMODULE>(handler), "QuitModule"));
|
|
||||||
if(quit == nullptr) {
|
|
||||||
throw std::runtime_error(std::to_string(GetLastError()).c_str());
|
|
||||||
}
|
|
||||||
quit();
|
|
||||||
FreeLibrary(static_cast<HMODULE>(handler));
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -17,8 +17,10 @@ class GameInstance
|
|||||||
std::atomic<bool> is_running = true;
|
std::atomic<bool> is_running = true;
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
|
// Stop call from core
|
||||||
void stop();
|
void stop();
|
||||||
public:
|
public:
|
||||||
|
// Stop call from game
|
||||||
void quit();
|
void quit();
|
||||||
|
|
||||||
// Init vulkan
|
// Init vulkan
|
||||||
|
|||||||
@@ -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()
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
#include "ModuleInstance.h"
|
|
||||||
|
|
||||||
#include <mutex>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
#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<std::mutex> lock(mModuleInstance);
|
|
||||||
callbacks_ = callbacks;
|
|
||||||
if(module == nullptr)
|
|
||||||
module = Factory();
|
|
||||||
}
|
|
||||||
|
|
||||||
void StartModule()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mModuleInstance);
|
|
||||||
if (module != nullptr)
|
|
||||||
moduleThread = new std::thread(&ModuleInstance::start, module);
|
|
||||||
}
|
|
||||||
|
|
||||||
void QuitModule()
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mModuleInstance);
|
|
||||||
if(module != nullptr)
|
|
||||||
{
|
|
||||||
module->stop();
|
|
||||||
moduleThread->join();
|
|
||||||
delete module;
|
|
||||||
module = nullptr;
|
|
||||||
delete moduleThread;
|
|
||||||
moduleThread = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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<void*>(&InitModule);} \
|
|
||||||
EXPORT void* protect_start() {return reinterpret_cast<void*>(&StartModule);} \
|
|
||||||
EXPORT void* protect_quit() {return reinterpret_cast<void*>(&QuitModule);} \
|
|
||||||
}\
|
|
||||||
ModuleInstance* Factory() { return static_cast<ModuleInstance*>(new Class()); }
|
|
||||||
|
|
||||||
struct CoreCallBacks;
|
|
||||||
|
|
||||||
class ModuleInstance {
|
|
||||||
public:
|
|
||||||
Delegate<void> 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();
|
|
||||||
}
|
|
||||||
@@ -1 +1 @@
|
|||||||
{"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld","vsmodules_names":[]}
|
{"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld"}
|
||||||
Reference in New Issue
Block a user