-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
244 lines (199 loc) · 8.8 KB
/
Copy pathmain.cpp
File metadata and controls
244 lines (199 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>
#include <string>
#include <vector>
#include <sstream>
#include <process.h>
#include <regex>
#include <fstream>
#include <algorithm>
#pragma comment(lib, "comctl32.lib")
#define IDC_EDIT_MODS 101
#define IDC_EDIT_STEAMCMD 102
#define IDC_EDIT_MODS_DIR 103
#define IDC_BTN_START 104
#define IDC_EDIT_LOG 105
#define IDC_BTN_FORMAT 106
HWND hModsEdit, hSteamCmdEdit, hModsDirEdit, hBtnStart, hBtnFormat, hLogEdit;
void Log(const std::wstring& text) {
int len = GetWindowTextLengthW(hLogEdit);
SendMessageW(hLogEdit, EM_SETSEL, len, len);
std::wstring line = text + L"\r\n";
SendMessageW(hLogEdit, EM_REPLACESEL, FALSE, (LPARAM)line.c_str());
}
std::vector<std::wstring> ExtractIds(const std::wstring& input) {
std::vector<std::wstring> ids;
std::wregex e(L"\\b\\d{8,12}\\b");
std::wsregex_iterator iter(input.begin(), input.end(), e);
std::wsregex_iterator end;
for (; iter != end; ++iter) {
ids.push_back(iter->str());
}
std::sort(ids.begin(), ids.end());
ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
return ids;
}
std::wstring GetModName(const std::wstring& modFolder) {
std::wstring aboutPath = modFolder + L"\\About\\About.xml";
std::ifstream file(aboutPath.c_str());
if (!file.is_open()) return L"Unknown Mod";
std::string line;
while (std::getline(file, line)) {
size_t start = line.find("<name>");
if (start != std::string::npos) {
size_t end = line.find("</name>", start);
if (end != std::string::npos) {
std::string name = line.substr(start + 6, end - start - 6);
int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, NULL, 0);
if (len > 0) {
std::wstring wname(len - 1, 0);
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, &wname[0], len);
return wname;
}
}
}
}
return L"Unknown Mod";
}
bool CopyDirectory(const std::wstring& src, const std::wstring& dst) {
CreateDirectoryW(dst.c_str(), NULL);
std::wstring searchPattern = src + L"\\*.*";
WIN32_FIND_DATAW fd;
HANDLE hFind = FindFirstFileW(searchPattern.c_str(), &fd);
if (hFind == INVALID_HANDLE_VALUE) return false;
do {
if (wcscmp(fd.cFileName, L".") == 0 || wcscmp(fd.cFileName, L"..") == 0)
continue;
std::wstring srcPath = src + L"\\" + fd.cFileName;
std::wstring dstPath = dst + L"\\" + fd.cFileName;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
CopyDirectory(srcPath, dstPath);
} else {
CopyFileW(srcPath.c_str(), dstPath.c_str(), FALSE);
}
} while (FindNextFileW(hFind, &fd));
FindClose(hFind);
return true;
}
unsigned __stdcall WorkerThread(void* pParam) {
EnableWindow(hBtnStart, FALSE);
EnableWindow(hBtnFormat, FALSE);
wchar_t bufSteamCmd[MAX_PATH], bufModsDir[MAX_PATH];
GetWindowTextW(hSteamCmdEdit, bufSteamCmd, MAX_PATH);
GetWindowTextW(hModsDirEdit, bufModsDir, MAX_PATH);
int textLen = GetWindowTextLengthW(hModsEdit);
std::vector<wchar_t> bufMods(textLen + 1);
GetWindowTextW(hModsEdit, &bufMods[0], textLen + 1);
std::vector<std::wstring> ids = ExtractIds(std::wstring(&bufMods[0]));
if (ids.empty()) {
Log(L"[ERROR] No valid mod IDs found!");
EnableWindow(hBtnStart, TRUE);
EnableWindow(hBtnFormat, TRUE);
return 0;
}
Log(L"=== Launching SteamCMD ===");
Log(L"Mods detected: " + std::to_wstring(ids.size()));
std::wstring cmdArgs = L"\"" + std::wstring(bufSteamCmd) + L"\" +login anonymous";
for (const auto& id : ids) {
cmdArgs += L" +workshop_download_item 294100 " + id;
}
cmdArgs += L" +quit";
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi = { 0 };
if (!CreateProcessW(NULL, &cmdArgs[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
Log(L"[ERROR] Failed to run steamcmd.exe. Check the file path!");
EnableWindow(hBtnStart, TRUE);
EnableWindow(hBtnFormat, TRUE);
return 0;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
Log(L"=== Download complete, copying files ===");
std::wstring steamCmdPath(bufSteamCmd);
std::wstring steamDir = steamCmdPath.substr(0, steamCmdPath.find_last_of(L"\\/"));
std::wstring workshopDir = steamDir + L"\\steamapps\\workshop\\content\\294100";
for (const auto& id : ids) {
std::wstring src = workshopDir + L"\\" + id;
std::wstring dst = std::wstring(bufModsDir) + L"\\" + id;
DWORD attr = GetFileAttributesW(src.c_str());
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
std::wstring modName = GetModName(src);
Log(L"Copying: [" + modName + L"] (ID: " + id + L")");
CopyDirectory(src, dst);
} else {
Log(L"[SKIP] Folder not found: " + id);
}
}
Log(L"=== Finished! Mods are installed ===");
EnableWindow(hBtnStart, TRUE);
EnableWindow(hBtnFormat, TRUE);
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
CreateWindowW(L"STATIC", L"SteamCMD.exe:", WS_CHILD | WS_VISIBLE, 15, 15, 120, 20, hWnd, NULL, NULL, NULL);
hSteamCmdEdit = CreateWindowW(L"EDIT", L"C:\\steamcmd\\steamcmd.exe", WS_CHILD | WS_VISIBLE | WS_BORDER, 140, 12, 425, 22, hWnd, (HMENU)IDC_EDIT_STEAMCMD, NULL, NULL);
CreateWindowW(L"STATIC", L"RimWorld Mods folder:", WS_CHILD | WS_VISIBLE, 15, 45, 150, 20, hWnd, NULL, NULL, NULL);
hModsDirEdit = CreateWindowW(L"EDIT", L"C:\\Games\\RimWorld\\Mods", WS_CHILD | WS_VISIBLE | WS_BORDER, 165, 42, 400, 22, hWnd, (HMENU)IDC_EDIT_MODS_DIR, NULL, NULL);
CreateWindowW(L"STATIC", L"Mod links or IDs (paste anywhere):", WS_CHILD | WS_VISIBLE, 15, 75, 400, 20, hWnd, NULL, NULL, NULL);
hModsEdit = CreateWindowW(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL, 15, 95, 550, 120, hWnd, (HMENU)IDC_EDIT_MODS, NULL, NULL);
hBtnFormat = CreateWindowW(L"BUTTON", L"Extract IDs", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 15, 225, 270, 32, hWnd, (HMENU)IDC_BTN_FORMAT, NULL, NULL);
hBtnStart = CreateWindowW(L"BUTTON", L"Download & Install", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 295, 225, 270, 32, hWnd, (HMENU)IDC_BTN_START, NULL, NULL);
hLogEdit = CreateWindowW(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL, 15, 270, 550, 170, hWnd, (HMENU)IDC_EDIT_LOG, NULL, NULL);
SendMessageW(hSteamCmdEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessageW(hModsDirEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessageW(hModsEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessageW(hBtnFormat, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessageW(hBtnStart, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessageW(hLogEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
break;
}
case WM_COMMAND: {
if (LOWORD(wParam) == IDC_BTN_START) {
_beginthreadex(NULL, 0, WorkerThread, NULL, 0, NULL);
}
else if (LOWORD(wParam) == IDC_BTN_FORMAT) {
int textLen = GetWindowTextLengthW(hModsEdit);
std::vector<wchar_t> bufMods(textLen + 1);
GetWindowTextW(hModsEdit, &bufMods[0], textLen + 1);
std::vector<std::wstring> ids = ExtractIds(std::wstring(&bufMods[0]));
std::wstring cleanText = L"";
for (const auto& id : ids) {
cleanText += id + L" ";
}
SetWindowTextW(hModsEdit, cleanText.c_str());
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
WNDCLASSEXW wc = { sizeof(wc) };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszClassName = L"RimModDownloader";
RegisterClassExW(&wc);
HWND hWnd = CreateWindowExW(0, wc.lpszClassName, L"RimWorld Mod Downloader Pro",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 490, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}