-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdllmain.cpp
More file actions
402 lines (338 loc) · 11.8 KB
/
Copy pathdllmain.cpp
File metadata and controls
402 lines (338 loc) · 11.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
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
#include "framework.h"
#include "minhook/include/MinHook.h"
#define __YYDEFINE_EXTENSION_FUNCTIONS__
#include "Extension_Interface.h"
#include "Ref.h"
#include "YYRValue.h"
#define YYEXPORT __declspec(dllexport)
#include "version.h"
YYRunnerInterface* g_pYYRunnerInterface;
namespace
{
constexpr char c_ExtensionName[] = "CoffeeTools";
constexpr char c_ExtensionVersion[] = MOD_VERSION;
constexpr size_t c_exe_run_loop = 0x140187310;
constexpr size_t c_exe_wndproc = 0x1400bbce0;
constexpr size_t c_exe_poll_messages = 0x14038dc00;
constexpr size_t c_exe_refresh_screen = 0x14000ac90;
constexpr size_t c_exe_check_audio_groups_loaded = 0x140482be0;
constexpr size_t c_exe_update_texture_status = 0x1400e66d0;
constexpr size_t c_exe_perform_game_load = 0x140182c70;
constexpr size_t c_exe_yygml_exception_handler = 0x14027d130;
YYRunnerInterface g_runnerInterface;
CInstance* g_selfinst;
void* g_origRunloop;
void* g_origWndproc;
void* g_origCheckAudioGroupsLoaded;
void* g_origUpdateTextureStatus;
int gml_Script_incrementFrame;
int gml_Script_performActions;
int gml_Script_refreshScreen;
int gml_game_load;
int g_savestateSlot;
int g_changeSpeed;
bool g_inRunloop;
bool g_isQuitting;
bool g_isPaused;
bool g_isFrameAdvancing;
bool g_isSaving;
bool g_resetSpeed;
bool g_prevInput[256];
bool g_input[256];
LRESULT Wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_QUIT || message == WM_CLOSE)
{
g_isQuitting = true;
}
return ((decltype(&Wndproc))g_origWndproc)(hwnd, message, wParam, lParam);
}
void PerformActions()
{
memcpy(g_prevInput, g_input, sizeof(g_input));
if (GetActiveWindow())
{
for (int i = 0; i < ARRAYSIZE(g_input); i++)
{
g_input[i] = GetKeyState(i) < 0;
}
}
else
{
memset(g_input, 0, sizeof(g_input));
}
if (g_selfinst)
{
RValue result = {};
Script_Perform(gml_Script_performActions, g_selfinst, g_selfinst, 0, &result, nullptr);
FREE_RValue(&result);
}
else
{
// handle input when the game first launches before performAction takes over (hold space when running the game to start paused)
if (g_input[VK_SPACE] && !g_prevInput[VK_SPACE])
{
if (g_input[VK_CONTROL])
{
g_isPaused = false;
g_isFrameAdvancing = true;
}
else if (g_isPaused)
{
g_isFrameAdvancing = true;
}
else
{
g_isPaused = true;
}
}
else if (g_input[VK_BACK] && !g_prevInput[VK_BACK])
{
g_isPaused = false;
}
}
}
void RefreshScreen()
{
if (g_selfinst)
{
RValue result = {};
Script_Perform(gml_Script_refreshScreen, g_selfinst, g_selfinst, 0, &result, nullptr);
FREE_RValue(&result);
}
((decltype(&RefreshScreen))c_exe_refresh_screen)();
}
int Runloop()
{
try
{
auto lastRefreshTime = Timing_Time();
g_inRunloop = true;
while (true)
{
PerformActions();
if (!g_isPaused || g_isQuitting || g_isFrameAdvancing)
{
break;
}
auto time = Timing_Time();
auto timeSinceLastRefresh = time - lastRefreshTime;
if (timeSinceLastRefresh >= 16667)
{
lastRefreshTime = time;
RefreshScreen(); // so that the steam overlay still works while paused
}
else
{
Timing_Sleep(16667 - timeSinceLastRefresh);
}
((void (*)())c_exe_poll_messages)();
}
g_isFrameAdvancing = false;
if (g_selfinst)
{
RValue result = {};
Script_Perform(gml_Script_incrementFrame, g_selfinst, g_selfinst, 0, &result, nullptr);
FREE_RValue(&result);
}
g_inRunloop = false;
}
catch (YYGMLException e)
{
((void (*)(YYGMLException))c_exe_yygml_exception_handler)(e);
}
return ((decltype(&Runloop))g_origRunloop)();
}
void CheckAudioGroupsLoaded(intptr_t arg)
{
struct Group
{
Group* next;
intptr_t unk1;
intptr_t something;
intptr_t val;
};
// wait until all loading audio groups have finished loading (happens in a worker thread)
// this essentially makes audio group loading synchronous and prevents desyncs
auto a = *(Group**)(arg + 8);
for (auto g = a->next; g != a; g = g->next)
{
if (g->val != 0 && g->something != 0 && *(int*)g->val == 1)
{
while (*(bool*)(g->val + 0x11) == 0)
{
Timing_Sleep(1000);
}
}
}
((decltype(&CheckAudioGroupsLoaded))g_origCheckAudioGroupsLoaded)(arg);
}
int UpdateTextureStatus(intptr_t arg1, intptr_t arg2, bool arg3)
{
// wait until the texture status has finished being updated (happens in a worker thread)
// (status 2 updates to status 3 in a separate thread, and then afterwards status 4 updates to status 6 in another thread)
// with this, we essentially make texture loading as fast as possible, since we always update our status in as few frames as possible, fixing desyncs
if (arg2 != 0)
{
auto status = *(int*)(arg2 + 0x3c);
if (status == 2 || status == 4)
{
// the function that calls this function enters us into a critical section, but we need to get out in order to let the worker thread do its thing
if (arg1 != 0) LeaveCriticalSection(**(LPCRITICAL_SECTION**)(arg1 + 0x50));
do
{
Timing_Sleep(1000);
status = *(int*)(arg2 + 0x3c);
}
while (status == 2 || status == 4);
if (arg1 != 0) EnterCriticalSection(**(LPCRITICAL_SECTION**)(arg1 + 0x50));
}
}
return ((decltype(&UpdateTextureStatus))g_origUpdateTextureStatus)(arg1, arg2, arg3);
}
}
YYEXPORT void YYExtensionInitialise(const struct YYRunnerInterface* _pFunctions, size_t _functions_size)
{
if (_functions_size < sizeof(YYRunnerInterface))
{
YYError("RunnerInterface size mismatch in CoffeeTools extension DLL!");
return;
}
memcpy(&g_runnerInterface, _pFunctions, sizeof(YYRunnerInterface));
g_pYYRunnerInterface = &g_runnerInterface;
auto version = extGetVersion(c_ExtensionName);
if (strncmp(version, c_ExtensionVersion, ARRAYSIZE(c_ExtensionVersion) - 1) != 0)
{
YYError("Extension DLL version mismatch: expected v%s but CoffeeTools.dll is v%s\nMake sure you copied the matching CoffeeTools.dll into the same folder as data.win.", version, c_ExtensionVersion);
return;
}
Code_Function_Find("game_load", &gml_game_load);
if (gml_game_load == -1)
{
YYError("Failed to find game_load!");
return;
}
if (MH_Initialize() != MH_OK)
{
YYError("MH_Initialize failed!");
return;
}
if (MH_CreateHook((void*)c_exe_run_loop, &Runloop, &g_origRunloop) != MH_OK)
{
YYError("MH_CreateHook Runloop failed!");
return;
}
if (MH_CreateHook((void*)c_exe_wndproc, &Wndproc, &g_origWndproc) != MH_OK)
{
YYError("MH_CreateHook Wndproc failed!");
return;
}
if (MH_CreateHook((void*)c_exe_check_audio_groups_loaded, &CheckAudioGroupsLoaded, &g_origCheckAudioGroupsLoaded) != MH_OK)
{
YYError("MH_CreateHook CheckAudioGroupsLoaded failed!");
return;
}
if (MH_CreateHook((void*)c_exe_update_texture_status, &UpdateTextureStatus, &g_origUpdateTextureStatus) != MH_OK)
{
YYError("MH_CreateHook UpdateTextureStatus failed!");
return;
}
if (MH_EnableHook(MH_ALL_HOOKS) != MH_OK)
{
YYError("MH_EnableHook failed!");
return;
}
// start the game paused if you launch the game while holding space
if (GetKeyState(VK_SPACE) < 0)
{
g_isPaused = true;
}
DebugConsoleOutput("CoffeeTools YYExtensionInitialise CONFIGURED\n");
}
YYEXPORT void ct_init(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
if (!g_selfinst)
{
gml_Script_incrementFrame = Script_Find_Id("gml_Script_incrementFrame");
if (gml_Script_incrementFrame == -1)
{
YYError("Failed to find gml_Script_incrementFrame!");
return;
}
gml_Script_performActions = Script_Find_Id("gml_Script_performActions");
if (gml_Script_performActions == -1)
{
YYError("Failed to find gml_Script_performActions!");
return;
}
gml_Script_refreshScreen = Script_Find_Id("gml_Script_refreshScreen");
if (gml_Script_refreshScreen == -1)
{
YYError("Failed to find gml_Script_refreshScreen!");
return;
}
}
g_selfinst = selfinst;
result.val = 1;
result.kind = VALUE_BOOL;
}
YYEXPORT void ct_is_paused(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
result.val = g_isPaused;
result.kind = VALUE_BOOL;
}
YYEXPORT void ct_set_paused(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
g_isPaused = YYGetBool(arg, 0);
if (argc > 1)
{
g_isFrameAdvancing = YYGetBool(arg, 1);
}
}
YYEXPORT void ct_in_runloop(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
result.val = g_inRunloop;
result.kind = VALUE_BOOL;
}
YYEXPORT void ct_output_debug_string(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
OutputDebugStringA(YYGetString(arg, 0));
}
YYEXPORT void ct_game_load(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
Script_Perform(gml_game_load, selfinst, otherinst, argc, &result, arg);
((int(*)())c_exe_perform_game_load)();
}
YYEXPORT void ct_is_ref(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
result.val = arg[0].kind == VALUE_REF;
result.kind = VALUE_REF;
}
YYEXPORT void ct_ref_to_int64(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
result.v64 = arg[0].v64;
result.kind = VALUE_INT64;
}
YYEXPORT void ct_int64_to_ref(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
result.v64 = arg[0].v64;
result.kind = VALUE_REF;
}
YYEXPORT void ct_keyboard_check(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
int vk = YYGetInt32(arg, 0);
result.val = g_input[vk];
result.kind = VALUE_BOOL;
}
YYEXPORT void ct_keyboard_check_pressed(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
int vk = YYGetInt32(arg, 0);
result.val = g_input[vk] && !g_prevInput[vk];
result.kind = VALUE_BOOL;
}
YYEXPORT void ct_keyboard_check_released(RValue& result, CInstance* selfinst, CInstance* otherinst, int argc, RValue* arg)
{
int vk = YYGetInt32(arg, 0);
result.val = !g_input[vk] && g_prevInput[vk];
result.kind = VALUE_BOOL;
}