2022-10-08 05:41:36 -04:00
|
|
|
#include <Windows.h>
|
2022-10-08 06:21:57 -04:00
|
|
|
#include <iostream>
|
2022-12-12 14:23:53 -05:00
|
|
|
#include <ghc/filesystem.hpp>
|
2022-10-08 05:41:36 -04:00
|
|
|
|
2022-10-08 06:26:45 -04:00
|
|
|
void showError(std::string const& error) {
|
|
|
|
MessageBoxA(nullptr, error.c_str(), "Error Loading Geode", MB_ICONERROR);
|
|
|
|
}
|
|
|
|
|
2022-10-11 15:00:08 -04:00
|
|
|
int loadGeode(PVOID module) {
|
2022-10-08 06:30:05 -04:00
|
|
|
if (!LoadLibraryW(L"Geode.dll")) {
|
|
|
|
auto code = GetLastError();
|
|
|
|
showError("Unable to load Geode (code " + std::to_string(code) + ")");
|
|
|
|
return code;
|
|
|
|
}
|
2022-10-11 15:00:08 -04:00
|
|
|
FreeLibraryAndExitThread(static_cast<HINSTANCE>(module), 0);
|
2022-10-08 06:30:05 -04:00
|
|
|
return 0;
|
2022-10-08 06:21:57 -04:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:00:08 -04:00
|
|
|
DWORD WINAPI load(PVOID module) {
|
2023-05-01 05:11:32 -04:00
|
|
|
auto workingDir = utils::file::current_path();
|
2022-10-08 05:41:36 -04:00
|
|
|
auto updatesDir = workingDir / "geode" / "update";
|
|
|
|
auto resourcesDir = workingDir / "geode" / "resources";
|
|
|
|
|
|
|
|
auto error = std::error_code();
|
|
|
|
|
|
|
|
if (ghc::filesystem::exists(updatesDir / "Geode.dll", error) && !error) {
|
2022-10-11 12:14:13 -04:00
|
|
|
ghc::filesystem::rename(
|
|
|
|
updatesDir / "Geode.dll",
|
|
|
|
workingDir / "Geode.dll", error
|
|
|
|
);
|
|
|
|
if (error) {
|
2022-10-08 06:26:45 -04:00
|
|
|
showError("Unable to update Geode: Unable to move Geode.dll - " + error.message());
|
|
|
|
return error.value();
|
|
|
|
}
|
2022-10-11 12:14:13 -04:00
|
|
|
}
|
2022-10-08 05:41:36 -04:00
|
|
|
|
2022-10-11 12:14:13 -04:00
|
|
|
if (ghc::filesystem::exists(updatesDir / "resources", error) && !error) {
|
|
|
|
ghc::filesystem::remove_all(resourcesDir / "geode.loader", error);
|
2022-10-08 06:21:57 -04:00
|
|
|
|
2022-10-11 12:14:13 -04:00
|
|
|
if (error) {
|
2022-10-08 06:26:45 -04:00
|
|
|
showError("Unable to update Geode resources: " + error.message());
|
2022-10-11 12:14:13 -04:00
|
|
|
} else {
|
2022-10-08 06:30:05 -04:00
|
|
|
ghc::filesystem::rename(
|
|
|
|
updatesDir / "resources",
|
|
|
|
resourcesDir / "geode.loader", error
|
|
|
|
);
|
|
|
|
if (error) {
|
|
|
|
showError("Unable to update Geode resources: " + error.message());
|
|
|
|
}
|
2022-10-08 06:26:45 -04:00
|
|
|
}
|
2022-10-11 12:14:13 -04:00
|
|
|
}
|
2022-10-08 06:26:45 -04:00
|
|
|
|
2022-10-11 15:00:08 -04:00
|
|
|
return loadGeode(module);
|
2022-10-08 05:41:36 -04:00
|
|
|
}
|
|
|
|
|
2022-10-11 12:14:13 -04:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE module, DWORD reason, LPVOID) {
|
2022-10-08 05:41:36 -04:00
|
|
|
if (reason == DLL_PROCESS_ATTACH) {
|
|
|
|
HANDLE handle = CreateThread(NULL, 0, load, module, 0, NULL);
|
2022-10-11 15:00:08 -04:00
|
|
|
if (handle) {
|
2022-10-08 05:41:36 -04:00
|
|
|
CloseHandle(handle);
|
2022-10-11 15:00:08 -04:00
|
|
|
} else {
|
2022-10-08 05:41:36 -04:00
|
|
|
return FALSE;
|
2022-10-11 15:00:08 -04:00
|
|
|
}
|
2022-10-08 05:41:36 -04:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|