-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsteamugc.cpp
More file actions
449 lines (377 loc) · 10 KB
/
steamugc.cpp
File metadata and controls
449 lines (377 loc) · 10 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "steamugc.h"
#include "framework.h"
#include "steam/steam_api.h"
#include <intrin.h>
#include <fstream>
#include <unordered_map>
#include "PicoSHA2.h"
#include <filesystem>
#include "hks.h"
struct workshop_json
{
enum workshop_json_parserstate
{
WJSPS_OPEN_BRACE, // open brace
WJSPS_READY, // close brace or quote
WJSPS_TOKEN, // close quote
WJSPS_DONE
};
private:
char* __internal_data;
std::unordered_map<uint32_t, const char*> props;
size_t data_len;
void clear()
{
if (__internal_data)
{
delete[] __internal_data;
__internal_data = NULL;
}
props.clear();
}
char* make_buf(size_t len)
{
clear();
__internal_data = new char[len];
memset(__internal_data, 0, len);
data_len = len;
return __internal_data;
}
public:
bool parse(std::ifstream& inFile)
{
inFile.seekg(0, std::ios::end);
size_t fileSize = inFile.tellg();
inFile.seekg(0, std::ios::beg);
inFile.read(make_buf(fileSize + 1), fileSize);
char* current_index = __internal_data;
char* current_key = NULL;
char* current_value = NULL;
workshop_json_parserstate state = WJSPS_OPEN_BRACE;
bool isKey = true;
bool escaped = false;
while (*current_index && (current_index < (__internal_data + data_len)) && (state != WJSPS_DONE))
{
switch (state)
{
case WJSPS_OPEN_BRACE:
{
if (*current_index++ != '{')
{
break;
}
state = WJSPS_READY;
}
break;
case WJSPS_READY:
{
auto ch = *current_index++;
if (ch == '"')
{
state = WJSPS_TOKEN;
if (isKey)
{
current_key = current_index;
}
else
{
current_value = current_index;
}
}
else if (ch == '}')
{
state = WJSPS_DONE;
}
}
break;
case WJSPS_TOKEN:
{
auto ch = *current_index;
if (ch == '"')
{
if (!escaped)
{
*current_index = 0;
if (!isKey)
{
props[fnv1a(current_key)] = current_value;
current_key = current_value = NULL;
}
isKey = !isKey;
escaped = false;
state = WJSPS_READY;
}
}
escaped = !escaped && (ch == '\\');
current_index++;
}
break;
}
}
return state == WJSPS_DONE;
}
const char* find(const char* key)
{
auto it = props.find(fnv1a(key));
return (it == props.end()) ? NULL : it->second;
}
bool is_mod()
{
auto val = find("Type");
return !val || strcmp(val, "map");
}
const char* title()
{
auto val = find("Title");
return val ? val : "<Error: Unknown Mod>";
}
const char* folder_name()
{
auto val = find("FolderName");
return val ? val : "";
}
const char* description()
{
auto val = find("Description");
return val ? val : "";
}
workshop_json()
{
__internal_data = NULL;
data_len = 0;
}
~workshop_json()
{
clear();
}
};
bool hasCheckedForUpdates = false;
void check_for_updates(const char* rootPath)
{
if (hasCheckedForUpdates)
{
return;
}
char wsUpdaterPathOnDisk[MAX_PATH]{ 0 };
strncat_s(wsUpdaterPathOnDisk, rootPath, strlen(rootPath));
strncat_s(wsUpdaterPathOnDisk, "\\" WSTORE_UPDATER_FILENAME, strlen("\\" WSTORE_UPDATER_FILENAME));
std::ifstream ifUpdater;
ifUpdater.open(wsUpdaterPathOnDisk, std::ios::binary);
if (!ifUpdater.is_open())
{
ALOG("FAILED to open '%s', cannot check for updates.", wsUpdaterPathOnDisk);
return; // failed to check
}
std::vector<unsigned char> hashUpdater(picosha2::k_digest_size);
picosha2::hash256(ifUpdater, hashUpdater.begin(), hashUpdater.end());
ifUpdater.close();
std::ifstream ifT7InternalWS;
ifT7InternalWS.open(WSINTERNAL_MOD_NAME, std::ios::binary);
if (!ifT7InternalWS.is_open())
{
ALOG("FAILED to open '%s', cannot check for updates.", WSINTERNAL_MOD_NAME);
return; // failed to check
}
std::vector<unsigned char> hashCurrent(picosha2::k_digest_size);
picosha2::hash256(ifT7InternalWS, hashCurrent.begin(), hashCurrent.end());
ifT7InternalWS.close();
bool needsUpdate = false;
for (int i = 0; i < picosha2::k_digest_size; i++)
{
if (hashCurrent.at(i) != hashUpdater.at(i))
{
needsUpdate = true;
break;
}
}
#if DEV_TEST_UPDATES
goto update;
#endif
if (!needsUpdate)
{
nlog("Binary is up to date. Skipping update!");
goto end;
}
#if IS_DEVELOPMENT && DEV_SKIP_UPDATES
nlog("DEV_SKIP_UPDATES. Skipping update...");
goto end;
#endif
update:
nlog("Binary is outdated. Installing update...");
g_needsUpdatePrompt = std::filesystem::copy_file(wsUpdaterPathOnDisk, WSTORE_UPDATER_DEST_FILENAME, std::filesystem::copy_options::overwrite_existing);
end:
hasCheckedForUpdates = true;
}
void parse_ugc_entry(bool isMod, PublishedFileId_t fileId)
{
auto itemState = SteamUGC()->GetItemState(fileId);
// make sure its actually installed
if (!(itemState & k_EItemStateInstalled))
{
return;
}
// check to make sure its not pending modifications
if (itemState & (k_EItemStateNeedsUpdate | k_EItemStateDownloading | k_EItemStateDownloadPending | k_EItemStateDisabledLocally))
{
return;
}
uint64_t sizeOnDisk = 0;
uint32_t timeStamp = 0;
char pathOnDisk[MAX_PATH]{ 0 };
char wsJsonPathOnDisk[MAX_PATH]{ 0 };
// retrieve workshop info for the item
if (!SteamUGC()->GetItemInstallInfo(fileId, &sizeOnDisk, pathOnDisk, MAX_PATH, &timeStamp))
{
return;
}
strncat_s(wsJsonPathOnDisk, pathOnDisk, strlen(pathOnDisk));
strncat_s(wsJsonPathOnDisk, "\\workshop.json", strlen("\\workshop.json"));
std::ifstream inFile;
inFile.open(wsJsonPathOnDisk, std::ifstream::in | std::ios::binary);
// make sure we found the workshop.json
if (!inFile.is_open())
{
return;
}
workshop_json json;
auto res = json.parse(inFile);
inFile.close();
// ensure workshop json was parsed correctly
if (!res)
{
return;
}
// ensure its matching the query type
if (json.is_mod() != isMod)
{
return;
}
if (WSTORE_UPDATER_WSID == fileId)
{
check_for_updates(pathOnDisk);
return;
}
auto dest_list = (ugcinfo_wstor*)REBASE(0x18A7ED68); // usermaps
if (isMod)
{
dest_list = (ugcinfo_wstor*)REBASE(0x18A58F60); // mods
}
// make sure we have space for a new entry
if (dest_list->num_entries >= (uint32_t)WORKSHOP_MAX_ENTRIES)
{
return;
}
ugcinfo_entry_wstor* dest_entry = dest_list->entries + dest_list->num_entries++;
memset(dest_entry, 0, sizeof(ugcinfo_entry_wstor));
strncpy_s(dest_entry->name, json.title(), _TRUNCATE);
strncpy_s(dest_entry->internalName, json.folder_name(), _TRUNCATE);
snprintf(dest_entry->ugcName, sizeof(dest_entry->ugcName), "%llu", fileId);
strncpy_s(dest_entry->description, json.description(), _TRUNCATE);
strncpy_s(dest_entry->ugcPath, pathOnDisk, _TRUNCATE);
auto sPos = strstr(pathOnDisk, "311210");
if (sPos)
{
strncpy_s(dest_entry->ugcPathBasic, sPos, _TRUNCATE);
strncpy_s(dest_entry->ugcRoot, pathOnDisk, sPos - pathOnDisk - 1);
}
auto fs_game = dest_entry->ugcName;
auto v39 = *fs_game;
for (dest_entry->hash_id = 5381; *fs_game; v39 = *fs_game)
{
++fs_game;
dest_entry->hash_id = tolower(v39) + 33 * dest_entry->hash_id;
}
dest_entry->unk_4B0 = 1;
dest_entry->unk_4B8 = isMod ? 1 : 2;
}
void update_steam_ugc(bool isMod)
{
uint32_t numItems = SteamUGC()->GetNumSubscribedItems();
auto itemsList = new PublishedFileId_t[numItems];
SteamUGC()->GetSubscribedItems(itemsList, numItems);
for (uint32_t i = 0; i < numItems; i++)
{
parse_ugc_entry(isMod, itemsList[i]);
}
delete[] itemsList;
itemsList = NULL;
}
MDT_Define_FASTCALL(REBASE(0x21EF320), populate_ugc_list_hook, void, (uint64_t a1, unsigned char a2))
{
update_steam_ugc((uint64_t)_ReturnAddress() != REBASE(0x21EF73A));
MDT_ORIGINAL(populate_ugc_list_hook, (a1, a2));
}
int __cdecl Lua_CoD_LuaCall_Mods_IsSubscribedItem(lua_State* luaVM)
{
auto idStr = lua_tostring(luaVM, 1);
PublishedFileId_t id = strtoull(idStr, nullptr, 10);
// steam version calls GetSubscribedItems and loops over it
lua_pushboolean(luaVM, SteamUGC()->GetItemState(id) & k_EItemStateSubscribed);
return 1;
}
int __cdecl Lua_CoD_LuaCall_Mods_SubscribeUGC(lua_State* luaVM)
{
auto idStr = lua_tostring(luaVM, 1);
PublishedFileId_t id = strtoull(idStr, nullptr, 10);
// steam version calls this twice for some reason
if (id)
{
SteamUGC()->SubscribeItem(id);
lua_pushboolean(luaVM, true);
}
else
{
lua_pushboolean(luaVM, false);
}
return 1;
}
float steam_install_progress(PublishedFileId_t id)
{
auto itemState = SteamUGC()->GetItemState(id);
nlog("steam_install_progress: itemState = 0x%X", itemState);
/*
// steam version does this but this feels like a mistake
if ((itemState & k_EItemStateInstalled) == 0)
{
return 0.0f;
}
*/
if ((itemState & (k_EItemStateDownloadPending | k_EItemStateDownloading | k_EItemStateNeedsUpdate)) == 0)
{
return 1.0f;
}
uint64 cur = 0;
uint64 total = 0;
if (!SteamUGC()->GetItemDownloadInfo(id, &cur, &total) || (itemState & k_EItemStateDownloading) == 0 || !total)
{
nlog("steam_install_progress: not downloading yet");
return 0.0f;
}
nlog("steam_install_progress: cur = %llu, total = %llu", cur, total);
// try to avoid precision loss from larger mods
return (float)((double)cur / total);
}
int __cdecl Lua_CoD_LuaCall_Mods_InstallProgressUGC(lua_State* luaVM)
{
auto idStr = lua_tostring(luaVM, 1);
PublishedFileId_t id = strtoull(idStr, nullptr, 10);
float progress = steam_install_progress(id);
nlog("Mods_InstallProgressUGC: progress = %f", progress);
lua_pushnumber(luaVM, progress);
return 1;
}
int __cdecl Lua_CoD_LuaCall_Mods_InstalledUGC(lua_State* luaVM)
{
auto idStr = lua_tostring(luaVM, 1);
PublishedFileId_t id = strtoull(idStr, nullptr, 10);
lua_pushboolean(luaVM, (SteamUGC()->GetItemState(id) & k_EItemStateInstalled) && !(SteamUGC()->GetItemState(id) & (k_EItemStateDownloadPending | k_EItemStateDownloading | k_EItemStateNeedsUpdate)));
return 1;
}
void steamugc::setup()
{
MDT_Activate(populate_ugc_list_hook);
// chgmem<void*>(REBASE(0x336F020), &Lua_CoD_LuaCall_Mods_IsSubscribedItem);
// chgmem<void*>(REBASE(0x336DCE0), &Lua_CoD_LuaCall_Mods_SubscribeUGC);
// chgmem<void*>(REBASE(0x336E900), &Lua_CoD_LuaCall_Mods_InstallProgressUGC);
// chgmem<void*>(REBASE(0x336E0A0), &Lua_CoD_LuaCall_Mods_InstalledUGC);
}