Files
UwU-Engine/Core/CoreInstance/Windows.cpp
T

87 lines
2.3 KiB
C++

#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