Skip to content

Commit 0b44e42

Browse files
authored
Addition of a priority list system
each mod has a priority ID that allow the user to choose which mod should load first
1 parent 794ab73 commit 0b44e42

File tree

1 file changed

+74
-22
lines changed

1 file changed

+74
-22
lines changed

main.cpp

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#define _CRT_SECURE_NO_DEPRECATE
12
#include <iostream>
3+
#include <fstream>
24
#include <windows.h>
5+
#include <string>
36
#include <vector>
4-
7+
#include <map>
58

69
using namespace std;
710

@@ -13,22 +16,22 @@ bool FileExists(LPCTSTR szPath)
1316

1417
int main()
1518
{
16-
vector <std::string> modDLLs;
19+
std::map<int, std::vector<std::string>> modDLLs;
1720

1821
//Cube world is obviously required
19-
if (!FileExists("Cube.exe")){
22+
if (!FileExists("Cube.exe")) {
2023
printf("Cube World not found.\n");
2124
Sleep(1000);
2225
return 1;
2326
}
2427

25-
FILE *file = fopen("Cube.exe", "rb");
28+
FILE* file = fopen("Cube.exe", "rb");
2629
fseek(file, 0, SEEK_END);
2730
int fileSize = ftell(file);
2831
fclose(file);
2932

3033
const int CUBE_SIZE = 3885568;
31-
if (fileSize != CUBE_SIZE){
34+
if (fileSize != CUBE_SIZE) {
3235
printf("Cube World was found, but it is not version 0.1.1. Please update your game.\n");
3336
printf("Press enter to exit.\n");
3437
cin.ignore();
@@ -37,16 +40,18 @@ int main()
3740

3841

3942

40-
43+
string CALLBACKMANAGER_PATH = "CallbackManager.dll";
4144
//The callback manager is required.
42-
if ( !FileExists("CallbackManager.dll") ){
45+
if (!FileExists(CALLBACKMANAGER_PATH.c_str())) {
4346
printf("Callback manager not found.\n");
4447
Sleep(1000);
4548
return 1;
4649
}
4750

48-
modDLLs.push_back( std::string("CallbackManager.dll") );
51+
std::map<string, int> priorityListData;
52+
modDLLs.insert({ -1, {CALLBACKMANAGER_PATH} });
4953
const char MOD_PATH[] = "Mods\\*.dll";
54+
const char MOD_PRIORITY_LIST_PATH[] = "ModPriorityList.txt";
5055
STARTUPINFO si;
5156
PROCESS_INFORMATION pi;
5257
ZeroMemory(&si, sizeof(si));
@@ -56,7 +61,7 @@ int main()
5661
//Create game in suspended state
5762
printf("Starting Cube.exe...\n\n");
5863
if (!CreateProcess(NULL,
59-
"Cube.exe",
64+
(char*)"Cube.exe",
6065
NULL,
6166
NULL,
6267
true,
@@ -73,6 +78,25 @@ int main()
7378
printf("Cube.exe was successfully started.\n\n");
7479
}
7580

81+
boolean priorityListFileExist = FileExists(MOD_PRIORITY_LIST_PATH);
82+
// Load priorityFile content into priorityListData
83+
if (priorityListFileExist) {
84+
ifstream file;
85+
string line;
86+
file.open(MOD_PRIORITY_LIST_PATH);
87+
while (getline(file, line)) {
88+
// line exemple -> "mod.dll"=0
89+
if (line.substr(0, 2) == string("##")) continue; // Comment
90+
int endOfName = line.find_last_of("\"");
91+
string modName = line.substr(1, endOfName-1);
92+
int modPriority = stoi(line.substr(endOfName+2));
93+
priorityListData.insert({ modName, modPriority});// modPriority });
94+
}
95+
file.close();
96+
} else {
97+
printf("ModPriorityList file not found.\n");
98+
printf("Default priority values will be used.\n\n");
99+
}
76100

77101
//Find mods
78102
HANDLE hFind;
@@ -81,26 +105,54 @@ int main()
81105
hFind = FindFirstFile(MOD_PATH, &data);
82106
if (hFind != INVALID_HANDLE_VALUE) {
83107
do {
84-
modDLLs.push_back( std::string("Mods\\") + data.cFileName);
108+
string modFilePath = std::string("Mods\\") + data.cFileName;
109+
int modPriority = 1;
110+
if (priorityListData.find(modFilePath) == priorityListData.end()) {
111+
priorityListData.insert({ modFilePath, modPriority });
112+
printf("file %s not found\n", modFilePath.c_str());
113+
}
114+
modPriority = priorityListData.at(modFilePath);
115+
if (modDLLs.find(modPriority) == modDLLs.end()) {
116+
modDLLs.insert({ modPriority, {} });
117+
}
118+
modDLLs.at(modPriority).push_back(modFilePath);
85119
} while (FindNextFile(hFind, &data));
86120
FindClose(hFind);
87121
}
88122

123+
// Save ModPriorityList file
124+
ofstream priorityListFile;
125+
priorityListFile.open(MOD_PRIORITY_LIST_PATH);
126+
priorityListFile << "## This file contain the path of all the mods detected by the ModLoader\n";
127+
priorityListFile << "## Each mod is ordered using a priority value. Mods with value of 0 load before mods with value of 1\n";
128+
priorityListFile << "## 1 is the default value used by mods.\n";
129+
priorityListFile << "## If a mod need to access another mod you may want it to be higher than the one it is accessing\n";
89130

90-
//Inject DLLs
131+
//Inject DLLs ordered by priority
91132
vector <HANDLE> threads;
92-
for (string S_DLLName : modDLLs){
93-
printf("Loading %s\n", S_DLLName.c_str());
94-
95-
LPVOID load_library = (LPVOID) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "LoadLibraryA");
96-
LPVOID remote_string = (LPVOID) VirtualAllocEx(pi.hProcess, NULL, strlen(S_DLLName.c_str()) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
97-
98-
WriteProcessMemory(pi.hProcess, remote_string, S_DLLName.c_str(), strlen(S_DLLName.c_str()) + 1, NULL);
99-
100-
HANDLE thread = CreateRemoteThread(pi.hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE) load_library, remote_string, CREATE_SUSPENDED, NULL);
101-
threads.push_back(thread);
102-
ResumeThread(thread);
133+
for (auto const& modVectors : modDLLs){
134+
int I_DLLPriority = modVectors.first;
135+
vector<string> V_DLLVector = modVectors.second;
136+
for (string S_DLLName : V_DLLVector) {
137+
printf("Loading mod with priority %d : '%s'\n", I_DLLPriority, S_DLLName.c_str());
138+
139+
// Save the mod in ModPriorityList file
140+
if (S_DLLName != CALLBACKMANAGER_PATH) {
141+
priorityListFile << "\"" << S_DLLName << "\"=" << to_string(I_DLLPriority) << "\n";
142+
}
143+
144+
LPVOID load_library = (LPVOID)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "LoadLibraryA");
145+
LPVOID remote_string = (LPVOID)VirtualAllocEx(pi.hProcess, NULL, strlen(S_DLLName.c_str()) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
146+
147+
WriteProcessMemory(pi.hProcess, remote_string, S_DLLName.c_str(), strlen(S_DLLName.c_str()) + 1, NULL);
148+
149+
HANDLE thread = CreateRemoteThread(pi.hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)load_library, remote_string, CREATE_SUSPENDED, NULL);
150+
threads.push_back(thread);
151+
ResumeThread(thread);
152+
Sleep(10); // wait a bit to ensure mod is initialised (hacky i know)
153+
}
103154
}
155+
priorityListFile.close();
104156

105157
ResumeThread(pi.hThread);
106158
CloseHandle(pi.hProcess);

0 commit comments

Comments
 (0)