Rename CoreInstance directory to Core
This commit is contained in:
@@ -1,139 +0,0 @@
|
|||||||
#include "CoreInstance.h"
|
|
||||||
|
|
||||||
#include "SystemCalls.h"
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <cstring>
|
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <list>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "Game/GameInstance.h"
|
|
||||||
|
|
||||||
class CoreInstance {
|
|
||||||
struct Module {
|
|
||||||
const char* name;
|
|
||||||
void* handler;
|
|
||||||
};
|
|
||||||
|
|
||||||
#pragma region Key words for main config
|
|
||||||
const char* key_base_world= "base_world";
|
|
||||||
const char* key_modules = "modules";
|
|
||||||
#pragma endregion
|
|
||||||
|
|
||||||
#pragma region config parameters
|
|
||||||
const char* main_config = "main_config.conf";
|
|
||||||
std::string base_world;
|
|
||||||
std::vector<std::string> modules_names;
|
|
||||||
#pragma endregion
|
|
||||||
|
|
||||||
int countCPU;
|
|
||||||
unsigned long long memorySize;
|
|
||||||
std::list<Module> modules;
|
|
||||||
|
|
||||||
GameInstance* game = nullptr;
|
|
||||||
public:
|
|
||||||
CoreInstance();
|
|
||||||
~CoreInstance();
|
|
||||||
|
|
||||||
void start();
|
|
||||||
|
|
||||||
int getCountCPU() const { return countCPU; }
|
|
||||||
unsigned long long getMemorySize() const { return memorySize; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
|
|
||||||
const std::string& getBaseWorldName() const { return base_world; }
|
|
||||||
};
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
#ifdef __linux__
|
|
||||||
|
|
||||||
#include "SystemCalls.h"
|
|
||||||
|
|
||||||
#include <sys/sysinfo.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <dlfcn.h>
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
int System::getCountCPU()
|
|
||||||
{
|
|
||||||
int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
|
|
||||||
if(num_cores <= 0)
|
|
||||||
throw std::runtime_error("Fail get number of cores");
|
|
||||||
|
|
||||||
return num_cores;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long long System::getMemorySize()
|
|
||||||
{
|
|
||||||
struct sysinfo info{};
|
|
||||||
if(sysinfo(&info) == -1)
|
|
||||||
std::runtime_error("Fail get system info");
|
|
||||||
return info.totalram;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* System::InitModule(const char* ModuleName, CoreInstance& core)
|
|
||||||
{
|
|
||||||
std::string fileName = "./lib";
|
|
||||||
fileName += ModuleName;
|
|
||||||
fileName += ".so";
|
|
||||||
|
|
||||||
void* handler = dlopen(fileName.c_str(), RTLD_NOW);
|
|
||||||
if(handler == nullptr)
|
|
||||||
throw std::runtime_error("Module not found");
|
|
||||||
|
|
||||||
void(*init)(CoreInstance&) = reinterpret_cast<void(*)(CoreInstance&)>(dlsym(handler, "InitModule"));
|
|
||||||
if(init == nullptr)
|
|
||||||
throw std::runtime_error("Module not contains InitModule");
|
|
||||||
init(core);
|
|
||||||
|
|
||||||
return handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
void System::StartModule(void* handler)
|
|
||||||
{
|
|
||||||
void(*start)() = reinterpret_cast<void(*)()>(dlsym(handler, "StartModule"));
|
|
||||||
if(start == nullptr)
|
|
||||||
throw std::runtime_error("Module not contins StartModule");
|
|
||||||
start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void System::StopModule(void* handler)
|
|
||||||
{
|
|
||||||
void(*stop)() = reinterpret_cast<void(*)()>(dlsym(handler, "StopModule"));
|
|
||||||
if(stop == nullptr)
|
|
||||||
std::runtime_error("Module mot contins StopModule");
|
|
||||||
stop();
|
|
||||||
dlclose(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void System::QuitModule(void* handler)
|
|
||||||
{
|
|
||||||
void(*quit)() = reinterpret_cast<void(*)()>(dlsym(handler, "QuitModule"));
|
|
||||||
if(quit == nullptr)
|
|
||||||
throw std::runtime_error("Module not contains QuitModule");
|
|
||||||
quit();
|
|
||||||
dlclose(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
class CoreInstance;
|
|
||||||
|
|
||||||
namespace System {
|
|
||||||
/**
|
|
||||||
* @throws std::exception if the operation finished with failed
|
|
||||||
* @return count CPU
|
|
||||||
*/
|
|
||||||
int getCountCPU();
|
|
||||||
unsigned long long getMemorySize();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load module and init handler
|
|
||||||
* @throws std::runtime_error if module not found
|
|
||||||
* @return module handler
|
|
||||||
*/
|
|
||||||
void* InitModule(const char* ModuleName, CoreInstance& core);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start module
|
|
||||||
* @throws std::runtime_error if module not contains StartModule function
|
|
||||||
*/
|
|
||||||
void StartModule(void* handler) noexcept(false);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Stop the module when unload there
|
|
||||||
* @throws std::runtime_error if module not contains StopModule function
|
|
||||||
*/
|
|
||||||
void StopModule(void* handler) noexcept(false);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call stop module
|
|
||||||
* @throws std::runtime_error if module not contains QuitModule function
|
|
||||||
*/
|
|
||||||
void QuitModule(void* handler) noexcept(false);
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
#ifdef _WIN32
|
|
||||||
|
|
||||||
#include "SystemCalls.h"
|
|
||||||
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <exception>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
int System::getCountCPU() {
|
|
||||||
DWORD len = 0;
|
|
||||||
GetLogicalProcessorInformation(nullptr, &len);
|
|
||||||
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
|
||||||
throw std::runtime_error(std::to_string(GetLastError()).c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> buffer(len);
|
|
||||||
if (!GetLogicalProcessorInformation(buffer.data(), &len)) {
|
|
||||||
throw std::runtime_error(std::to_string(GetLastError()).c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
DWORD count = len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
|
|
||||||
int physicalCoreCount = 0;
|
|
||||||
|
|
||||||
for (DWORD i = 0; i < count; ++i) {
|
|
||||||
if (buffer[i].Relationship == RelationProcessorCore) {
|
|
||||||
physicalCoreCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return physicalCoreCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long long System::getMemorySize() {
|
|
||||||
MEMORYSTATUSEX memStatus{};
|
|
||||||
memStatus.dwLength = sizeof(memStatus);
|
|
||||||
|
|
||||||
GlobalMemoryStatusEx(&memStatus);
|
|
||||||
return memStatus.ullTotalPhys;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* System::InitModule(const char* ModuleName, CoreInstance& core) {
|
|
||||||
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)(CoreInstance&) = reinterpret_cast<void(*)(CoreInstance&)>(GetProcAddress(hModule, "InitModule"));
|
|
||||||
if(load == nullptr) {
|
|
||||||
throw std::runtime_error(std::to_string(GetLastError()).c_str());
|
|
||||||
}
|
|
||||||
load(core);
|
|
||||||
|
|
||||||
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::StopModule(void* handler) {
|
|
||||||
void(*stop)() = reinterpret_cast<void(*)()>(GetProcAddress(static_cast<HMODULE>(handler), "StopModule"));
|
|
||||||
if(stop == nullptr) {
|
|
||||||
throw std::runtime_error(std::to_string(GetLastError()).c_str());
|
|
||||||
}
|
|
||||||
stop();
|
|
||||||
FreeLibrary(static_cast<HMODULE>(handler));
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "CoreInstance/CoreInstance.h"
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
|
||||||
try {
|
|
||||||
CoreInstance core;
|
|
||||||
std::cout << "CPU: " << core.getCountCPU() << std::endl;
|
|
||||||
std::cout << "Memory: " << core.getMemorySize() << std::endl;
|
|
||||||
core.start();
|
|
||||||
} catch(std::exception& e) {
|
|
||||||
std::cout << "[!] Unknown error (" << e.what() << ")\n";
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user