This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.asm
379 lines (306 loc) · 11.1 KB
/
main.asm
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
; ****************************************************************************
; * LICENSE: *
; * *
; * GMThreads is free software; you can redistribute it and/or *
; * modify it under the terms of the GNU Lesser General Public *
; * License as published by the Free Software Foundation; either *
; * version 2.1 of the License, or (at your option) any later version. *
; * *
; * GMThreads is distributed in the hope that it will be useful, *
; * but WITHOUT ANY WARRANTY; without even the implied warranty of *
; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
; * Lesser General Public License for more details. *
; * *
; * You should have received a copy of the GNU Lesser General Public *
; * License along with GMThreads; if not, write to the Free Software *
; * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
; * 02110-1301 USA *
; ****************************************************************************
; * main.asm *
; * *
; * Copyright 2009 (C) Snake (http://sgames.ovh.org/) *
; ****************************************************************************
.386
.model flat, stdcall
include windows.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib
GM_SIGNATURE_ADDRESS equ 00500000h
GM_SIGNATURE_GM80 equ 0E982754Fh
GM_SIGNATURE_GM70 equ 00589A24h
GM_SIGNATURE_GM61 equ 0E8005386h
GM70_UTILITY_CODEOBJECTCOMPILE equ 00545C2Ch
GM70_UTILITY_CREATECODEOBJECT equ 00545B50h
GM70_UTILITY_CREATEINSTANCE equ 004ACC54h
GM70_UTILITY_EXECUTECODEOBJECT equ 0052CB6Ch
GM70_UTILITY_DESTROYOBJECT equ 004041D4h
GM70_ADDRESS_INSTANCECLASS equ 004AC1BCh
GM70_ADDRESS_CODECLASS equ 00545858h
GM61_UTILITY_CODEOBJECTCOMPILE equ 004F6E1Ch
GM61_UTILITY_CREATECODEOBJECT equ 004F6D58h
GM61_UTILITY_CREATEINSTANCE equ 004A1688h
GM61_UTILITY_EXECUTECODEOBJECT equ 004D7614h
GM61_UTILITY_DESTROYOBJECT equ 00404184h
GM61_ADDRESS_INSTANCECLASS equ 004A0C10h
GM61_ADDRESS_CODECLASS equ 004F6C58h
.data
; Initialized to GM8 addresses
GM_UTILITY_CODEOBJECTCOMPILE dd 00543B38h
GM_UTILITY_CREATECODEOBJECT dd 00543A5Ch
GM_UTILITY_CREATEINSTANCE dd 004AB45Ch
GM_UTILITY_EXECUTECODEOBJECT dd 00528CECh
GM_UTILITY_DESTROYOBJECT dd 00404A30h
GM_ADDRESS_INSTANCECLASS dd 004AA8C4h
GM_ADDRESS_CODECLASS dd 00543754h
.code
; ======================================================================================
; = String constants =
; ======================================================================================
strError db "GMThreads error", 0
strIncompatibleVersion db "Error: Trying to use GMThreads with Instant Play or Game Maker version different than 6.1, 7.0 or 8.0.", 0
strCannotCompile db "Error: Unable to compile specified GML code for a thread.", 0
; ======================================================================================
; = Utilities =
; ======================================================================================
ErrorMessage proc aMessage: DWORD
push MB_SYSTEMMODAL or MB_ICONERROR ; Options
push offset strError ; Caption
push aMessage ; Message
push 0 ; Handle
call [MessageBoxA]
ret
ErrorMessage endp
; ======================================================================================
; = Thread function =
; ======================================================================================
GMLThread proc aCodeObject: DWORD
local result[6]: DWORD
; ZeroMemory( result, 24 )
lea edi, result
mov ecx, 6
cld
xor eax, eax
rep stosd
push 0
push 0
push 0
push 0
push 0
xor ecx, ecx
mov dl, 1
mov eax, dword ptr ds:[GM_ADDRESS_INSTANCECLASS]
mov eax, dword ptr ds:[eax]
call [GM_UTILITY_CREATEINSTANCE]
mov esi, eax
lea eax, result
push eax
mov ecx, ebx
mov edx, esi
mov eax, esi
call [GM_UTILITY_EXECUTECODEOBJECT]
xor al, 1
push eax
mov eax, esi
call [GM_UTILITY_DESTROYOBJECT]
mov eax, ebx
call [GM_UTILITY_DESTROYOBJECT]
pop eax
ret
GMLThread endp
; ======================================================================================
; = DLL functions =
; ======================================================================================
ThreadCreate proc aCode: DWORD, aSuspend: QWORD
push 0
mov ecx, aCode
mov dl, 1
mov eax, dword ptr ds:[GM_ADDRESS_CODECLASS]
mov eax, dword ptr ds:[eax]
call [GM_UTILITY_CREATECODEOBJECT]
mov ebx, eax
call [GM_UTILITY_CODEOBJECTCOMPILE]
test al, al
jnz CompilationSuccess
push offset strCannotCompile
call [ErrorMessage]
fldz
ret
CompilationSuccess:
; if ( aSuspend ) eax = CREATE_SUSPENDED; else eax = 0;
sub esp, 4
fld aSuspend
fistp dword ptr ss:[esp]
pop eax
neg eax
sbb eax, eax
and eax, CREATE_SUSPENDED
push 0 ; lpThreadId
push eax ; dwCreationFlags
push ebx ; lpParameter; pass code object to the thread
push GMLThread ; lpStartAddress
push 0 ; dwStackSize
push 0 ; lpThreadAttributes
call [CreateThread]
mov dword ptr ds:[aSuspend], eax
fild dword ptr ds:[aSuspend]
ret
ThreadCreate endp
ThreadTerminate proc aHandle: QWORD
push 1
sub esp, 4h
fld aHandle
fistp dword ptr ss:[esp]
call [TerminateThread]
mov dword ptr ds:[aHandle], eax
fild dword ptr ds:[aHandle]
ret
ThreadTerminate endp
ThreadSuspend proc aHandle: QWORD
sub esp, 4
fld aHandle
fistp dword ptr ds:[esp]
call [SuspendThread]
fldz
ret
ThreadSuspend endp
ThreadResume proc aHandle: QWORD
sub esp, 4
fld aHandle
fistp dword ptr ds:[esp]
call [ResumeThread]
fldz
ret
ThreadResume endp
ThreadSetPriority proc aHandle: QWORD, aPriority: QWORD
sub esp, 8h
fld aPriority
fistp dword ptr ss:[esp + 4h]
fld aHandle
fistp dword ptr ss:[esp]
call [SetThreadPriority]
fldz
ret
ThreadSetPriority endp
ThreadSetAffinity proc aHandle: QWORD, aAffinity: QWORD
sub esp, 8h
fld aAffinity
fistp dword ptr ss:[esp + 4h]
fld aHandle
fistp dword ptr ss:[esp]
call [SetThreadAffinityMask]
mov dword ptr ds:[aHandle], eax
fild dword ptr ds:[aHandle]
ret
ThreadSetAffinity endp
ThreadSetProcessor proc aHandle: QWORD, aProcessor: QWORD
sub esp, 8h
fld aProcessor
fistp dword ptr ss:[esp + 4h]
fld aHandle
fistp dword ptr ss:[esp]
call [SetThreadIdealProcessor]
mov dword ptr ds:[aHandle], eax
fild dword ptr ds:[aHandle]
ret
ThreadSetProcessor endp
ThreadGetPriority proc aHandle: QWORD
sub esp, 4h
fld aHandle
fistp dword ptr ss:[esp]
call [GetThreadPriority]
mov dword ptr ds:[aHandle], eax
fild dword ptr ds:[aHandle]
ret
ThreadGetPriority endp
ThreadGetError proc aHandle: QWORD
push 0 ; lpExitCode buffer
push esp ; lpExitCode parameter
sub esp, 4h ; Handle parameter
fld aHandle
fistp dword ptr ss:[esp] ; Handle
call [GetExitCodeThread]
test al, al
jnz Success
mov dword ptr ds:[esp], -1
Success:
fild dword ptr ds:[esp]
ret
ThreadGetError endp
GetProcessorCount proc
local systemInfo: SYSTEM_INFO
lea eax, systemInfo
push eax
call [GetSystemInfo]
fild systemInfo.dwNumberOfProcessors
ret
GetProcessorCount endp
ThreadClose proc aHandle: QWORD
sub esp, 4h
fld aHandle
fistp dword ptr ss:[esp]
call [CloseHandle]
mov dword ptr ds:[aHandle], eax
fild dword ptr ds:[aHandle]
ret
ThreadClose endp
ThreadWait proc aHandle: QWORD, aTimeout: QWORD
sub esp, 8h
fld aTimeout
fistp dword ptr ds:[esp + 4h]
fld aHandle
fistp dword ptr ds:[esp]
call [WaitForSingleObject]
mov dword ptr ds:[aHandle], eax
fild dword ptr ds:[aHandle]
ret
ThreadWait endp
; ======================================================================================
; = Entrypoint =
; ======================================================================================
DllEntryPoint proc aModule:DWORD, aReason: DWORD, aReserved: DWORD
cmp aReason, DLL_PROCESS_ATTACH
jne Return
push aModule
call [DisableThreadLibraryCalls]
; Correct addresses to suitable GM version
mov eax, dword ptr ds:[GM_SIGNATURE_ADDRESS]
cmp eax, GM_SIGNATURE_GM80
jne Version61
; Exit - addresses already set to GM8
jmp Return
Version61:
; Get base pointer to the beginning of address variables
mov ebx, offset GM_UTILITY_CODEOBJECTCOMPILE
cmp eax, GM_SIGNATURE_GM61
jne Version70
mov dword ptr ds:[ebx], GM61_UTILITY_CODEOBJECTCOMPILE
mov dword ptr ds:[ebx + 04h], GM61_UTILITY_CREATECODEOBJECT
mov dword ptr ds:[ebx + 08h], GM61_UTILITY_CREATEINSTANCE
mov dword ptr ds:[ebx + 0Ch], GM61_UTILITY_EXECUTECODEOBJECT
mov dword ptr ds:[ebx + 10h], GM61_UTILITY_DESTROYOBJECT
mov dword ptr ds:[ebx + 14h], GM61_ADDRESS_INSTANCECLASS
mov dword ptr ds:[ebx + 18h], GM61_ADDRESS_CODECLASS
jmp Return
Version70:
cmp eax, GM_SIGNATURE_GM70
jne VersionError
mov dword ptr ds:[ebx], GM70_UTILITY_CODEOBJECTCOMPILE
mov dword ptr ds:[ebx + 04h], GM70_UTILITY_CREATECODEOBJECT
mov dword ptr ds:[ebx + 08h], GM70_UTILITY_CREATEINSTANCE
mov dword ptr ds:[ebx + 0Ch], GM70_UTILITY_EXECUTECODEOBJECT
mov dword ptr ds:[ebx + 10h], GM70_UTILITY_DESTROYOBJECT
mov dword ptr ds:[ebx + 14h], GM70_ADDRESS_INSTANCECLASS
mov dword ptr ds:[ebx + 18h], GM70_ADDRESS_CODECLASS
jmp Return
VersionError:
push offset strIncompatibleVersion
call [ErrorMessage]
xor eax, eax
ret
Return:
mov eax, 1
ret
DllEntryPoint endp
end DllEntryPoint