Cube-World-Mod-Launcher/main.cpp

118 lines
3.2 KiB
C++
Raw Normal View History

2018-10-16 05:55:57 +08:00
#include <iostream>
#include <windows.h>
#include <vector>
2018-10-22 07:01:59 +08:00
2018-10-16 05:55:57 +08:00
using namespace std;
2018-10-22 07:01:59 +08:00
bool FileExists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
2018-10-16 05:55:57 +08:00
int main()
{
2018-10-22 07:01:59 +08:00
vector <std::string> modDLLs;
//Cube world is obviously required
if (!FileExists("Cube.exe")){
printf("Cube World not found.\n");
Sleep(1000);
return 1;
}
FILE *file = fopen("Cube.exe", "rb");
fseek(file, 0, SEEK_END);
int fileSize = ftell(file);
fclose(file);
const int CUBE_SIZE = 3885568;
if (fileSize != CUBE_SIZE){
printf("Cube World was found, but it is not version 0.1.1. Please update your game.\n");
printf("Press enter to exit.\n");
cin.ignore();
return 1;
}
2018-10-22 07:01:59 +08:00
//The callback manager is required.
if ( !FileExists("CallbackManager.dll") ){
printf("Callback manager not found.\n");
Sleep(1000);
return 1;
}
modDLLs.push_back( std::string("CallbackManager.dll") );
2018-10-16 05:55:57 +08:00
const char MOD_PATH[] = "Mods\\*.dll";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
2018-10-19 02:08:52 +08:00
CreateDirectory("Mods", NULL);
2018-10-16 05:55:57 +08:00
//Create game in suspended state
2018-10-19 01:14:27 +08:00
printf("Starting Cube.exe...\n\n");
2018-10-16 05:55:57 +08:00
if (!CreateProcess(NULL,
"Cube.exe",
NULL,
NULL,
2018-10-19 01:14:27 +08:00
true,
2018-10-16 05:55:57 +08:00
CREATE_SUSPENDED,
NULL,
NULL,
&si,
&pi))
{
printf("Failed to create process: %lu", GetLastError());
return 1;
}
2018-10-19 01:14:27 +08:00
else {
printf("Cube.exe was successfully started.\n\n");
}
2018-10-16 05:55:57 +08:00
//Find mods
HANDLE hFind;
WIN32_FIND_DATA data;
2018-10-22 07:01:59 +08:00
2018-10-16 05:55:57 +08:00
hFind = FindFirstFile(MOD_PATH, &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
modDLLs.push_back( std::string("Mods\\") + data.cFileName);
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
//Inject DLLs
vector <HANDLE> threads;
for (string S_DLLName : modDLLs){
printf("Loading %s\n", S_DLLName.c_str());
LPVOID load_library = (LPVOID) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "LoadLibraryA");
LPVOID remote_string = (LPVOID) VirtualAllocEx(pi.hProcess, NULL, strlen(S_DLLName.c_str()) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(pi.hProcess, remote_string, S_DLLName.c_str(), strlen(S_DLLName.c_str()) + 1, NULL);
HANDLE thread = CreateRemoteThread(pi.hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE) load_library, remote_string, CREATE_SUSPENDED, NULL);
threads.push_back(thread);
ResumeThread(thread);
}
ResumeThread(pi.hThread);
CloseHandle(pi.hProcess);
2018-10-19 01:14:27 +08:00
printf("\nAll available mods have been loaded.\n");
Sleep(1500);
//
//
// WaitForSingleObject(pi.hThread, INFINITE);
// for (HANDLE thread : threads){
// CloseHandle(thread);
// }
2018-10-16 05:55:57 +08:00
return 0;
}