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

78 lines
2.0 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;
}
double System::getMemorySize() {
MEMORYSTATUSEX memStatus{};
memStatus.dwLength = sizeof(memStatus);
GlobalMemoryStatusEx(&memStatus);
return 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