-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.c
4161 lines (3424 loc) · 135 KB
/
main.c
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 OpenSWE1R Maintainers
// Licensed under GPLv2 or any later version
// Refer to the included LICENSE.txt file.
#include "main.h"
#include "app_version.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <assert.h>
#include "common.h"
#include "descriptor.h"
#include "emulation.h"
#include "exe.h"
//FIXME: Alternative for non-posix OS!
#include <time.h>
#include "SDL.h"
static SDL_Window* sdlWindow;
#include <GL/glew.h>
#include "com/d3d.h"
#include "com/ddraw.h"
#include "com/dinput.h"
uint32_t callId = 0;
typedef struct {
const char* name;
void(*callback)(void*, Address, void*);
Address address;
} Export;
unsigned int exportCount = 0;
Export* exports = NULL;
void AddExport(const char* name, void* callback, Address address) {
exports = realloc(exports, (exportCount + 1) * sizeof(Export));
Export* export = &exports[exportCount];
export->name = malloc(strlen(name) + 1);
strcpy((char*)export->name, name);
export->callback = callback;
export->address = 0;
exportCount++;
}
Export* LookupExportByName(const char* name) {
for(unsigned int i = 0; i < exportCount; i++) {
Export* export = &exports[i];
if (!strcmp(export->name, name)) {
return export;
}
}
return NULL;
}
Export* LookupExportByOrdinal(const char* name, uint32_t ordinal) {
if (!strcmp(name, "COMCTL32.dll")) {
if (ordinal == 17) {
return LookupExportByName("InitCommonControls");
}
}
return NULL;
}
// HACK BLOCK!
#if 1
const char** dirlisting = NULL;
Address clearEax = 0;
uint32_t tls[1000] = {0};
//FIXME: To be moved elsewhere
#include "shaders.h"
#include "shader.h"
#include "windows.h" // Hack while exports are not ready
// HACK:
#include <unicorn/unicorn.h>
static void UnknownImport(void* uc, Address address, void* user_data);
Address CreateInterface(const char* name, unsigned int slotCount) {
//FIXME: Unsure about most terminology / inner workings here
Address interfaceAddress = Allocate(1000); //FIXME: Size of object
Address vtableAddress = Allocate(4 * slotCount);
uint32_t* vtable = (uint32_t*)Memory(vtableAddress);
for(unsigned int i = 0; i < slotCount; i++) {
// Point addresses to themself
char* slotName = malloc(128);
sprintf(slotName, "%s__%d", name, i);
Export* export = LookupExportByName(slotName);
Address hltAddress;
if (export != NULL) {
if (export->address == 0) {
hltAddress = CreateHlt();
AddHltHandler(hltAddress, export->callback, (void*)slotName);
export->address = hltAddress;
} else {
hltAddress = export->address;
}
} else {
hltAddress = CreateHlt();
AddHltHandler(hltAddress, UnknownImport, (void*)slotName);
AddExport(slotName, UnknownImport, hltAddress);
}
vtable[i] = hltAddress;
}
// First element in object is pointer to vtable
*(uint32_t*)Memory(interfaceAddress) = vtableAddress;
return interfaceAddress;
}
#endif
Exe* exe; //FIXME: This is hack. I feel this shouldn't be exposed aside from the loader
const char* exeName = "swep1rcr.exe";
static char* TranslatePath(const char* path, bool* is_cd) {
size_t length = strlen(path) + 1;
char* newPath = malloc(length);
char* cursor = strcpy(newPath, path);
while(*cursor != '\0') {
if (*cursor == '\\') {
*cursor = '/';
} else {
*cursor = tolower(*cursor);
}
cursor++;
}
// Check if this is a CD access
if ((length >= 3) && !memcmp(newPath, "d:/", 3)) {
// Normally, the game stores the data in the "/gnome/" folder on the CD.
// All files from that folder will be in the games root folder after
// installation.
//
// However, some games will have a patched CD checked to directly access
// data in the CD root folder.
// This allows to set the "CD Path" to the installation folder of the game
// to run the game without CD.
// The most prominent version doing this is probably the GOG.com re-release
// from 2018.
//
// To support both of these variations, we redirect "/gnome/" to the CD root
// folder on our own. Regardless of what the game does.
// This will do nothing on patched games, but will patch the unpatched one.
// This causes all versions to behave like the patched one.
if ((length >= 9) && !memcmp(newPath, "d:/gnome/", 9)) {
memmove(&newPath[3], &newPath[9], length - 9);
}
// We map the CD root folder back to our game root folder.
memcpy(newPath, "./", 2);
memmove(&newPath[2], &newPath[3], length - 3);
// Mark this as a CD access
if (is_cd != NULL) {
*is_cd = true;
}
} else {
// This is not a CD access, mark it accordingly
if (is_cd != NULL) {
*is_cd = false;
}
}
return newPath;
}
void StackTrace(uint32_t base, unsigned int frames, unsigned int arguments) {
uint32_t stackAddress = base;
for(unsigned int i = 0; i < frames; i++) {
printf("Base: 0x%" PRIX32 "\n", stackAddress);
if (stackAddress == 0) {
// End of stack trace!
return;
}
uint32_t* stack = (uint32_t*)Memory(stackAddress);
// stack[0] = EBP of calling function
// stack[1] = Return address
// stack[2..] = Arguments
if (stack == NULL) {
printf("Corrupt base in trace!\n");
return;
}
printf("#%2d Returns to 0x%" PRIX32 " (", i, stack[1]);
for(unsigned int j = 0; j < arguments; j++) {
printf("@%d=0x%08" PRIX32 ", ", j, stack[j + 2]);
}
printf("...)\n");
// Get the previous ebp
stackAddress = stack[0];
}
}
// FIXME: Move to exe functions
void RelocateSection(Exe* exe, unsigned int sectionIndex) {
// Relocate
//reloc 21589 offset 0 [301d3017] ABSOLUTE
//reloc 21590 offset 11 [301d3028] HIGHLOW
#if 0
switch(relocation->type)
case RelocationAbsolute:
assert(0);
break;
case RelocationHighLow:
assert(0);
break;
default:
assert(0);
break;
}
#endif
}
void LoadSection(Exe* exe, unsigned int sectionIndex) {
PeSection* section = &exe->sections[sectionIndex];
// Map memory for section
uint8_t* mem = (uint8_t*)aligned_malloc(0x1000, section->virtualSize);
// Read data from exe and fill rest of space with zero
fseek(exe->f, section->rawAddress, SEEK_SET);
uint32_t rawSize = section->rawSize;
if (rawSize > section->virtualSize) {
rawSize = section->virtualSize;
}
fread(mem, 1, rawSize, exe->f);
if (rawSize < section->virtualSize) {
memset(&mem[rawSize], 0x00, section->virtualSize - rawSize);
}
// Write back address to the exe object
exe->mappedSections[sectionIndex] = mem;
}
void UnloadSection(Exe* exe, unsigned int sectionIndex) {
aligned_free(exe->mappedSections[sectionIndex]);
exe->mappedSections[sectionIndex] = NULL;
}
static void UcTimerHook(void* uc, uint64_t address, uint32_t size, void* user_data) {
printf("Time is %" PRIu64 "\n", SDL_GetTicks());
}
// This is strictly for debug purposes, it attempts to dump fscanf (internally used by sscanf too)
static void UcFscanfHook(void* uc, uint64_t address, uint32_t size, void* user_data) {
printf("\nfscanf\n\n");
int eip;
uc_reg_read(uc, UC_X86_REG_EIP, &eip);
int esp;
uc_reg_read(uc, UC_X86_REG_ESP, &esp);
int eax;
uc_reg_read(uc, UC_X86_REG_EAX, &eax);
Address stackAddress = esp;
uint32_t* stack = (uint32_t*)Memory(stackAddress);
// This is the FILE struct used by microsoft CRT
typedef struct {
Address _ptr;
int32_t _cnt;
Address _base;
int32_t _flag;
int32_t _file;
int32_t _charbuf;
int32_t _bufsiz;
Address _tmpfname;
} _iobuf;
// Pop the return address
Address returnAddress = stack[0];
printf("Return at 0x%" PRIX32 "\n", returnAddress);
_iobuf* iob = Memory(stack[1]); // Get FILE object
char* buf = Memory(iob->_ptr);
printf("stream: 0x%" PRIX32 " ('%.100s...')\n", stack[1], buf);
char* fmt = (char*)Memory(stack[2]);
printf("fmt: 0x%" PRIX32 " ('%s')\n", stack[2], fmt);
// We'll let MS code handle buffer loads
if (buf == NULL) {
return;
}
#if 0
if (!strcmp(fmt, "%s")) {
SetTracing(true);
}
#endif
#if 0
//FIXME: Hack.. for these to work, they'd have to consume the buffer
if (!strcmp(fmt, "%s")) {
eax = sscanf(buf, fmt, Memory(stack[3]));
uc_reg_write(uc, UC_X86_REG_EAX, &eax);
eip = returnAddress;
uc_reg_write(uc, UC_X86_REG_EIP, &eip);
esp += 4;
uc_reg_write(uc, UC_X86_REG_ESP, &esp);
return;
}
if (!strcmp(fmt, "%f %f %f")) {
eax = 3; //sscanf(buf, fmt);
uc_reg_write(uc, UC_X86_REG_EAX, &eax);
eip = returnAddress;
uc_reg_write(uc, UC_X86_REG_EIP, &eip);
esp += 4;
uc_reg_write(uc, UC_X86_REG_ESP, &esp);
return;
}
#endif
}
// This is strictly for debug purposes, it attempts to add messages to the log in case of the weird-crash
static void UcCrashHook(void* uc, uint64_t address, uint32_t size, void* user_data) {
int eip;
uc_reg_read(uc, UC_X86_REG_EIP, &eip);
int esp;
uc_reg_read(uc, UC_X86_REG_ESP, &esp);
int eax;
uc_reg_read(uc, UC_X86_REG_EAX, &eax);
int ecx;
uc_reg_read(uc, UC_X86_REG_ECX, &ecx);
int edi;
uc_reg_read(uc, UC_X86_REG_EDI, &edi);
Address stackAddress = esp;
uint32_t* stack = (uint32_t*)Memory(stackAddress);
char buf[1024];
sprintf(buf, "Bug: ecx=0x%08" PRIX32 ", at eip=0x%08" PRIX32 ". 0x%08" PRIX32 " 0x%08" PRIX32 " 0x%08" PRIX32 " 0x%08" PRIX32,
ecx, eip,
stack[0], stack[1], stack[2], stack[3]);
glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_MARKER, 0, GL_DEBUG_SEVERITY_NOTIFICATION, -1, buf);
}
static void PrintVertices(unsigned int vertexFormat, Address address, unsigned int count) {
unsigned int texCount = ((vertexFormat & 0xF00) >> 8);
GLsizei stride = 4 * 4 + 4 + 4 + texCount * 8;
uint32_t* p = (uint32_t*)Memory(address);
for(unsigned int i = 0; i < count; i++) {
float* f = (float*)p;
printf(" %d. %f %f %f %f 0x%08" PRIX32 " 0x%08" PRIX32, i, f[0], f[1], f[2], f[3], p[4], p[5]);
p += 6;
if (texCount >= 1) {
printf(" %f %f", f[6], f[7]);
p += 2;
}
if (texCount >= 2) {
printf(" %f %f", f[8], f[9]);
p += 2;
}
printf("\n");
}
}
static void LoadIndices(Address address, unsigned int count) {
static GLuint buffer = 0;
if (buffer == 0) {
glGenBuffers(1, &buffer);
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * 2, Memory(address), GL_STREAM_DRAW);
}
static void LoadVertices(unsigned int vertexFormat, Address address, unsigned int count) {
unsigned int texCount = ((vertexFormat & 0xF00) >> 8);
GLsizei stride = 4 * 4 + 4 + 4 + texCount * 8;
static GLuint buffer = 0;
if (buffer == 0) {
glGenBuffers(1, &buffer);
}
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, count * stride, Memory(address), GL_STREAM_DRAW);
}
int tex0Blend;
bool depthMask;
GLenum destBlend;
GLenum srcBlend;
bool alphaTest;
uint32_t fogColor; // ARGB
bool fogEnable;
int fogMode;
float fogStart;
float fogEnd;
float projectionMatrix[16];
float clipScale[3];
float clipOffset[3];
static GLenum SetupRenderer(unsigned int primitiveType, unsigned int vertexFormat) {
unsigned int texCount = ((vertexFormat & 0xF00) >> 8);
GLsizei stride = 4 * 4 + 4 + 4 + texCount * 8;
// Re-Volt only uses D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR OR'd with either D3DFVF_TEX{0,1,2}
assert(texCount == 1);
GLuint program = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&program);
GLint positionIn = glGetAttribLocation(program, "positionIn");
if (positionIn != -1) {
glEnableVertexAttribArray(positionIn);
glVertexAttribPointer(positionIn, 4, GL_FLOAT, GL_FALSE, stride, (const GLvoid*)0);
}
GLint diffuseIn = glGetAttribLocation(program, "diffuseIn");
if (diffuseIn != -1) {
glEnableVertexAttribArray(diffuseIn);
glVertexAttribPointer(diffuseIn, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (const GLvoid*)16);
}
GLint specularIn = glGetAttribLocation(program, "specularIn");
if (specularIn != -1) {
glEnableVertexAttribArray(specularIn);
glVertexAttribPointer(specularIn, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (const GLvoid*)20);
}
GLint uv0In = glGetAttribLocation(program, "uv0In");
if (uv0In != -1) {
glEnableVertexAttribArray(uv0In);
glVertexAttribPointer(uv0In, 2, GL_FLOAT, GL_TRUE, stride, (const GLvoid*)24);
}
glUniform1i(glGetUniformLocation(program, "tex0Blend"), tex0Blend);
glUniform1i(glGetUniformLocation(program, "tex0"), 0);
glUniformMatrix4fv(glGetUniformLocation(program, "projectionMatrix"), 1, GL_FALSE, projectionMatrix);
glUniform1i(glGetUniformLocation(program, "alphaTest"), alphaTest);
glUniform1i(glGetUniformLocation(program, "fogMode"), fogEnable ? fogMode : API(D3DFOG_NONE));
glUniform1f(glGetUniformLocation(program, "fogStart"), fogStart);
glUniform1f(glGetUniformLocation(program, "fogEnd"), fogEnd);
glUniform3f(glGetUniformLocation(program, "fogColor"),
((fogColor >> 16) & 0xFF) / 255.0,
((fogColor >> 8) & 0xFF) / 255.0,
(fogColor & 0xFF) / 255.0);
glUniform3fv(glGetUniformLocation(program, "clipScale"), 1, clipScale);
glUniform3fv(glGetUniformLocation(program, "clipOffset"), 1, clipOffset);
glBlendFunc(srcBlend, destBlend);
#if 0
// Wireframe mode
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
#endif
glDepthMask(depthMask ? GL_TRUE : GL_FALSE);
GLenum mode;
switch(primitiveType) {
case 1: mode = GL_POINTS; break; // D3DPT_POINTLIST
case 2: mode = GL_LINES; break; // D3DPT_LINELIST
case 3: mode = GL_LINE_STRIP; break; // D3DPT_LINESTRIP
case 4: mode = GL_TRIANGLES; break; // D3DPT_TRIANGLELIST
case 5: mode = GL_TRIANGLE_STRIP; break; // D3DPT_TRIANGLESTRIP
case 6: mode = GL_TRIANGLE_FAN; break; // D3DPT_TRIANGLEFAN
default:
assert(false);
break;
}
return mode;
}
// CRT-Startup / pre-WinMain:
HACKY_IMPORT_BEGIN(GetVersion)
hacky_printf("(No parameters)\n");
eax = 0x00010A04;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(HeapCreate)
hacky_printf("flOptions 0x%" PRIX32 "\n", stack[1]);
hacky_printf("dwInitialSize 0x%" PRIX32 "\n", stack[2]);
hacky_printf("dwMaximumSize 0x%" PRIX32 "\n", stack[3]);
eax = 0x555;
esp += 3 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(HeapAlloc)
hacky_printf("hHeap 0x%" PRIX32 "\n", stack[1]);
hacky_printf("dwFlags 0x%" PRIX32 "\n", stack[2]);
hacky_printf("dwBytes 0x%" PRIX32 "\n", stack[3]);
eax = Allocate(stack[3]);
//FIXME: Only do this if flag is set..
memset(Memory(eax), 0x00, stack[3]);
esp += 3 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(InitializeCriticalSection)
hacky_printf("lpCriticalSection 0x%" PRIX32 "\n", stack[1]);
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(TlsAlloc)
static int tlsIndex = 0;
assert(tlsIndex < 500);
eax = tlsIndex++; // TLS Index
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(EnterCriticalSection)
// Heavily impacts performance!
#if 1
hacky_printf("lpCriticalSection 0x%" PRIX32 "\n", stack[1]);
#else
silent = true;
#endif
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(LeaveCriticalSection)
// Heavily impacts performance!
#if 1
hacky_printf("lpCriticalSection 0x%" PRIX32 "\n", stack[1]);
#else
silent = true;
#endif
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(VirtualAlloc)
hacky_printf("lpAddress 0x%" PRIX32 "\n", stack[1]);
hacky_printf("dwSize 0x%" PRIX32 "\n", stack[2]);
hacky_printf("flAllocationType 0x%" PRIX32 "\n", stack[3]);
hacky_printf("flProtect 0x%" PRIX32 "\n", stack[4]);
eax = Allocate(stack[2]);
memset(Memory(eax), 0x00, stack[2]);
esp += 4 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(TlsSetValue)
hacky_printf("dwTlsIndex 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpTlsValue 0x%" PRIX32 "\n", stack[2]);
tls[stack[1]] = stack[2];
eax = 1; // nonzero if succeeds
esp += 2 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetCurrentThreadId)
eax = 666; // nonzero if succeeds
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetACP)
eax = 777; // nonzero if succeeds
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetModuleFileNameA)
hacky_printf("hModule 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpFilename 0x%" PRIX32 "\n", stack[2]);
hacky_printf("nSize 0x%" PRIX32 "\n", stack[3]);
const char* path = "XYZ";
assert(stack[3] >= (strlen(path) + 1));
eax = sprintf((char*)Memory(stack[2]), "%s", path); // number of chars written
esp += 3 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetCPInfo)
hacky_printf("CodePage 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpCPInfo 0x%" PRIX32 "\n", stack[2]);
eax = 1; // Returns 1 if successful, or 0 otherwise.
esp += 2 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetStartupInfoA)
hacky_printf("lpStartupInfo 0x%" PRIX32 "\n", stack[1]);
memset(Memory(stack[1]), 0x00, 68);
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetStdHandle)
hacky_printf("nStdHandle 0x%" PRIX32 "\n", stack[1]);
eax = 888;
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetFileType)
hacky_printf("hFile 0x%" PRIX32 "\n", stack[1]);
//eax = 2; // FILE_TYPE_CHAR
eax = 1; // FILE_TYPE_DISK
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(SetHandleCount)
hacky_printf("uNumber 0x%" PRIX32 "\n", stack[1]);
eax = stack[1];
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetCommandLineA)
const char* cmd = "swep1rcr.exe";
Address tmp = Allocate(strlen(cmd) + 1);
strcpy((char*)Memory(tmp), cmd);
eax = tmp;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetEnvironmentStringsW)
Address tmp = Allocate(4);
((char*)Memory(tmp))[0] = '\0';
((char*)Memory(tmp))[1] = '\0';
((char*)Memory(tmp))[2] = '\0';
((char*)Memory(tmp))[3] = '\0';
eax = tmp;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(WideCharToMultiByte)
hacky_printf("CodePage 0x%" PRIX32 "\n", stack[1]);
hacky_printf("dwFlags 0x%" PRIX32 "\n", stack[2]);
hacky_printf("lpWideCharStr 0x%" PRIX32 "\n", stack[3]);
hacky_printf("cchWideChar 0x%" PRIX32 "\n", stack[4]);
hacky_printf("lpMultiByteStr 0x%" PRIX32 "\n", stack[5]);
hacky_printf("cbMultiByte 0x%" PRIX32 "\n", stack[6]);
hacky_printf("lpDefaultChar 0x%" PRIX32 "\n", stack[7]);
hacky_printf("lpUsedDefaultChar 0x%" PRIX32 "\n", stack[8]);
eax = 1; //FIXME: Number of chars written
esp += 8 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(MultiByteToWideChar)
hacky_printf("CodePage 0x%" PRIX32 "\n", stack[1]);
hacky_printf("dwFlags 0x%" PRIX32 "\n", stack[2]);
hacky_printf("lpMultiByteStr 0x%" PRIX32 "\n", stack[3]);
hacky_printf("cbMultiByte 0x%" PRIX32 "\n", stack[4]);
hacky_printf("lpWideCharStr 0x%" PRIX32 "\n", stack[5]);
hacky_printf("cchWideChar 0x%" PRIX32 "\n", stack[6]);
//FIXME: MOVE SYMBOLS?!
eax = 0; //FIXME: Number of chars written
esp += 6 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(LCMapStringW)
hacky_printf("Locale 0x%" PRIX32 "\n", stack[1]);
hacky_printf("dwMapFlags 0x%" PRIX32 "\n", stack[2]);
hacky_printf("lpSrcStr 0x%" PRIX32 "\n", stack[3]);
hacky_printf("cchSrc 0x%" PRIX32 "\n", stack[4]);
hacky_printf("lpDestStr 0x%" PRIX32 "\n", stack[5]);
hacky_printf("cchDest 0x%" PRIX32 "\n", stack[6]);
//FIXME: MOVE SYMBOLS?!
eax = 1 + 1; //FIXME: Number of chars in translated string including zero term
esp += 6 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetModuleHandleA)
hacky_printf("lpModuleName 0x%" PRIX32 " ('%s')\n", stack[1], Memory(stack[1]));
eax = 999;
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetProcAddress)
Address lpProcName = stack[2];
const char* procName = Memory(lpProcName);
hacky_printf("hModule 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpProcName 0x%" PRIX32 " ('%s')\n", lpProcName, procName);
Export* export = LookupExportByName(procName);
if (export == NULL) {
printf("Export for '%s' could not be found\n", procName);
eax = 0;
assert(false);
} else {
//FIXME: Use existing address for export
Address hltAddress = CreateHlt();
AddHltHandler(hltAddress, export->callback, (void*)procName);
eax = hltAddress;
printf("Providing at 0x%08X\n", hltAddress);
}
esp += 2 * 4;
HACKY_IMPORT_END()
// This one is retrieved using GetProcAddress!
HACKY_IMPORT_BEGIN(IsProcessorFeaturePresent)
hacky_printf("ProcessorFeature 0x%" PRIX32 "\n", stack[1]);
eax = 0; // Feature not supported = zero; else nonzero
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(SetUnhandledExceptionFilter)
// (Only used in 1207 revolt.exe CRT)
hacky_printf("lpTopLevelExceptionFilter 0x%" PRIX32 "\n", stack[1]);
eax = 0; // Previous handler (NULL = none existed)
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(QueryPerformanceCounter)
hacky_printf("lpPerformanceCount 0x%" PRIX32 "\n", stack[1]);
*(uint64_t*)Memory(stack[1]) = SDL_GetPerformanceCounter();
eax = 1; // nonzero if succeeds
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(FreeEnvironmentStringsW)
hacky_printf("lpszEnvironmentBlock 0x%" PRIX32 "\n", stack[1]);
eax = 1; // nonzero if succeeds
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetStringTypeW)
hacky_printf("dwInfoType 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpSrcStr 0x%" PRIX32 "\n", stack[2]);
hacky_printf("cchSrc 0x%" PRIX32 "\n", stack[3]);
hacky_printf("lpCharType 0x%" PRIX32 "\n", stack[4]);
eax = 1; // nonzero if succeeds
esp += 4 * 4;
HACKY_IMPORT_END()
// Actual revolt.exe starts here, anything until this point was CRT-Startup / pre-WinMain:
HACKY_IMPORT_BEGIN(GetCursorPos)
hacky_printf("lpPoint 0x%" PRIX32 "\n", stack[1]);
int32_t* point = (int32_t*)Memory(stack[1]);
int x;
int y;
SDL_GetMouseState(&x, &y);
point[0] = x;
point[1] = y;
eax = 1; // nonzero if succeeds
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(SetCursorPos)
hacky_printf("x %" PRIu32 "\n", stack[1]);
hacky_printf("y %" PRIu32 "\n", stack[2]);
eax = 1; // nonzero if succeeds
esp += 2 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetKeyNameTextA)
hacky_printf("lParam 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpString 0x%" PRIX32 " ('%s')\n", stack[2], (char*)Memory(stack[2]));
hacky_printf("cchSize %" PRIu32 "\n", stack[3]);
eax = snprintf(Memory(stack[2]), stack[3], "k%" PRIu32, stack[1]); // Cancel was selected
esp += 3 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetComputerNameA)
hacky_printf("lpBuffer 0x%" PRIX32 "\n", stack[1]);
uint32_t* size = (uint32_t*)Memory(stack[2]);
hacky_printf("lpnSize 0x%" PRIX32 " (%" PRIu32 ")\n", stack[2], *size);
*size = snprintf(Memory(stack[1]), *size, "ComputerName"); // Cancel was selected
eax = 1; // nonzero if succeeds
esp += 2 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(timeGetTime)
//FIXME: Avoid overflow?
eax = SDL_GetTicks();
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetLastError)
silent = true;
eax = 0; // no error
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(TlsGetValue)
silent = true;
if (!silent) {
hacky_printf("dwTlsIndex 0x%" PRIX32 "\n", stack[1]);
}
eax = tls[stack[1]]; // TLS value FIXME!
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(SetLastError)
silent = true;
if (!silent) {
hacky_printf("dwErrCode 0x%" PRIX32 "\n", stack[1]);
}
eax = 0; // TLS value FIXME!
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(LocalAlloc)
silent = true;
if (!silent) {
hacky_printf("uFlags 0x%" PRIX32 "\n", stack[1]);
hacky_printf("uBytes 0x%" PRIX32 "\n", stack[2]);
}
eax = Allocate(stack[2]);
// Only if zeroinit: clear
if (stack[1] & 0x40) {
memset(Memory(eax), 0x00, stack[2]);
}
esp += 2 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(LocalFree)
silent = true;
if (!silent) {
hacky_printf("hMem 0x%" PRIX32 "\n", stack[1]);
}
Free(stack[1]);
eax = 0; // NULL on success
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(FindWindowA)
hacky_printf("lpClassName 0x%" PRIX32 " ('%s')\n", stack[1], (char*)Memory(stack[1]));
hacky_printf("lpWindowName 0x%" PRIX32 " ('%s')\n", stack[2], (char*)Memory(stack[2]));
eax = 0; // NULL = window not found, else HWND
esp += 2 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(MessageBoxA)
hacky_printf("hWnd 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpText 0x%" PRIX32 " ('%s')\n", stack[2], (char*)Memory(stack[2]));
hacky_printf("lpCaption 0x%" PRIX32 " ('%s')\n", stack[3], (char*)Memory(stack[3]));
hacky_printf("uType 0x%" PRIX32 "\n", stack[4]);
SDL_Delay(5000);
eax = 2; // Cancel was selected
esp += 4 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(ExitProcess)
hacky_printf("uExitCode 0x%" PRIX32 "\n", stack[1]);
exit(EXIT_FAILURE); //FIXME: Instead, handle this gracefully somehow?!
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(GetTempPathA)
hacky_printf("nBufferLength 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpBuffer 0x%" PRIX32 "\n", stack[2]);
assert(stack[1] >= 6);
eax = sprintf((char*)Memory(stack[2]), "%s", "/tmp/"); // number of chars writte
esp += 2 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(wsprintfA)
// __cdecl!
hacky_printf("lpOut 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpFmt 0x%" PRIX32 " ('%s')\n", stack[2], (char*)Memory(stack[2]));
char* out = (char*)Memory(stack[1]);
const char* in = (char*)Memory(stack[2]);
unsigned int count = 0;
unsigned int stackIndex = 3;
while(*in != '\0') {
const char* nextPercent = strchr(in, '%');
if (nextPercent == NULL) {
count += sprintf(&out[count], in); // Copy rest of the input to output
break;
}
unsigned int length = nextPercent - in;
memcpy(&out[count], in, length);
in += length;
count += length;
in = nextPercent + 1;
char type = *in++;
switch(type) {
case 'c':
count += sprintf(&out[count], "%c", stack[stackIndex++]);
break;
case 's':
count += sprintf(&out[count], "%s", (char*)Memory(stack[stackIndex++]));
break;
case 'd':
count += sprintf(&out[count], "%d", stack[stackIndex++]);
break;
default:
printf("Unknown format type '%c'\n", type);
assert(false);
}
}
eax = count;
printf("Out: '%s'\n", out);
HACKY_IMPORT_END()
static FILE* file_handles[16] = {0};
static unsigned int find_free_file_handle() {
for(unsigned int i = 0; i < ARRAY_SIZE(file_handles); i++) {
if (file_handles[i] == NULL) {
return i;
}
}
assert(false);
return (unsigned int)-1;
}
HACKY_IMPORT_BEGIN(CreateFileA)
const char* lpFileName = (char*)Memory(stack[1]);
hacky_printf("lpFileName 0x%" PRIX32 " ('%s')\n", stack[1], lpFileName);
hacky_printf("dwDesiredAccess 0x%" PRIX32 "\n", stack[2]);
hacky_printf("dwShareMode 0x%" PRIX32 "\n", stack[3]);
hacky_printf("lpSecurityAttributes 0x%" PRIX32 "\n", stack[4]);
hacky_printf("dwCreationDisposition 0x%" PRIX32 "\n", stack[5]);
hacky_printf("dwFlagsAndAttributes 0x%" PRIX32 "\n", stack[6]);
hacky_printf("hTemplateFile 0x%" PRIX32 "\n", stack[7]);
bool is_cd;
char* path = TranslatePath(lpFileName, &is_cd);
// Get desired access type
bool is_write = stack[2] & 0x40000000;
// Disallow writing to CD
if (is_cd && is_write) {
printf("Denied to open file ('%s' as '%s')\n", lpFileName, path);
eax = 0xFFFFFFFF;
} else {
FILE* f = fopen(path, is_write ? (stack[5] == 4 ? "ab" : "wb") : "rb");
if (f != NULL) {
unsigned int handle_index = find_free_file_handle();
printf("File handle is 0x%" PRIX32 "\n", handle_index);
file_handles[handle_index] = f;
eax = handle_index;
} else {
printf("Failed to open file ('%s' as '%s')\n", lpFileName, path);
eax = 0xFFFFFFFF;
}
}
free(path);
esp += 7 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(WriteFile)
hacky_printf("hFile 0x%" PRIX32 "\n", stack[1]);
hacky_printf("lpBuffer 0x%" PRIX32 "\n", stack[2]);
hacky_printf("nNumberOfBytesToWrite 0x%" PRIX32 "\n", stack[3]);
hacky_printf("lpNumberOfBytesWritten 0x%" PRIX32 "\n", stack[4]);
hacky_printf("lpOverlapped 0x%" PRIX32 "\n", stack[5]);
*(uint32_t*)Memory(stack[4]) = fwrite(Memory(stack[2]), 1, stack[3], file_handles[stack[1]]);
eax = 1; // nonzero if succeeds
esp += 5 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(HeapFree)
hacky_printf("hHeap 0x%" PRIX32 "\n", stack[1]);
hacky_printf("dwFlags 0x%" PRIX32 "\n", stack[2]);
hacky_printf("lpMem 0x%" PRIX32 "\n", stack[3]);
assert(stack[2] == 0x0);
Free(stack[3]);
eax = 1; // nonzero if succeeds
esp += 3 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(CloseHandle)
hacky_printf("hObject 0x%" PRIX32 "\n", stack[1]);
if (stack[1] == 5554321) { // Thread handle..
eax = 1; // nonzero if succeeds
} else if (stack[1] == 5551337) { // Thread handle..
eax = 1; // nonzero if succeeds
} else {
unsigned int handle_index = stack[1];
eax = fclose(file_handles[handle_index]) ? 0 : 1; // nonzero if succeeds
file_handles[handle_index] = NULL;
}
esp += 1 * 4;
HACKY_IMPORT_END()
HACKY_IMPORT_BEGIN(CoInitialize)
hacky_printf("pvReserved 0x%" PRIX32 "\n", stack[1]);
assert(stack[1] == 0x00000000);