-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIkarus.d
5314 lines (4219 loc) · 168 KB
/
Ikarus.d
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
//######################################################
//
// Kern des Skriptpakets "Ikarus"
// Autor : Sektenspinner
// Co-Autor : Gottfried, mud-freak, Neconspictor
// Version : 1.2.3
//
//######################################################
//************************************************
// Content:
//************************************************
/*
## Preamble ##
-Versioncheck
-Logging Functions (preliminary)
-Parser Data Stack Hacking
## Basic Read Write ##
-Read/Write of Integers, Strings, Arrays, Bytes
## Basic zCParser related functions ##
-MEM_ReinitParser: Locate Parser data structures.
-Get and set instance offsets (e.g. MEM_PtrToInst)
-Jumps (via MEM_StackPos)
-MEM_GetFuncID and friends
-Address Operator _@ and friends
-Access static Arrays
## Preliminary MEM_Alloc and MEM_Free ##
-(De-)Allocation with Strings
## CALL Package ##
-ASM: Bytecode Streams and their execution
-CALL_Begin / End: The faster Mode of the package
-Parameter pushing
-Result poping
-calling conventions
## UTILITY ##
-MEM_SetShowDebug
-MEM_Copy
-MEM_Swap
-MEM_Clear
-MEM_Realloc
-MEM_Compare
## Windows Utilities ##
-LoadLibrary / GetProcAddress
-VirtualProtect / MemoryProtectionOverride
-MEM_MessageBox / MEM_InfoBox
## Arrays ##
-Alloc / Clear / Free / Size / Read / Write
-Insert / Push / Pop / Top
-IndexOf / RemoveIndex / RemoveValue[Once]
-Sort / Unique
-ToString
## String Tools ##
-GetCharAt / Length
-Substring / Prefix
-Compare
-STR_ToInt
-STR_IndexOf
-STR_Split
-STR_Upper
## Elaborate zCParser related functions ##
-MEM_(Find/Get)ParserSymbol
-MEM_Call[, ByID, ByString]
-Find function by Stack Offset
-Locate current execution position on machine stack
* MEM_GetCallerStackPos
* MEM_SetCallerStackPos
-Label / goto / while / repeat
* Split function into tokens
* Trace calculation of parameter
* patch function
* Handle first while
* Handle first goto
* Handle first repeat
## Access Menu Objects ##
-Find Menus and Menuitems by string
## zCObjects ##
-Commonly used objects (MEM_InitGlobalInst)
-Validity checks (Hlp_Is_*)
-Find zCClassDef and class name for object
-Create and delete vobs
* MEM_InsertVob
* MEM_DeleteVob
-Locate Objects in the worlds Hash table
* Evaluate hash function
* Find Objects by name
* Properly change object name
-Send trigger and untrigger
## Keyboard interaction ##
-MEM_KeyState
-MEM_InsertKeyEvent
## Read and Write Ini Values ##
-Reading
* In Gothic's configuration
* In the mod's configuration
* Get command line
* Get key assignment
-Writing
* in Gothic's configuration
* Apply changes and write to disk
## Benchmarking and time measurement ##
-Time Measurement
* Milliseconds
* Performance Counter
-Benchmark
## Logging and Debug ##
-Send Info/Warning/Error to zSpy
-Print the Stacktrace
* Print Stacktrace line
* Print full Stack Trace
* Exception handler
* Installing the exeption handler
## Revised functions ##
-Faster MEM_ReadInt / MEM_WriteInt
-Faster MEM_Alloc and MEM_Free
## MEM_InitAll
*/
//#################################################
//
// Preamble
//
//#################################################
//----------------------------------------------
// Versioncheck
// If your Code relies on fixes introduced in
// a certain version of Ikarus,
// and you want to give your code to users
// that may have old versions, use this:
//----------------------------------------------
const int IKARUS_VERSION = 10203; //2 digits for Major and Minor Revision number.
/* returns 1 if the version of Ikarus is the specified version or newer */
func int MEM_CheckVersion(var int base, var int major, var int minor) {
if (major > 99 || minor > 99) {
return false;
};
return base*10000 + major * 100 + minor <= IKARUS_VERSION;
};
//--------------------------------------
// Logging functions
// MEM_SendToSpy will be revised
// by MEM_InitAll to print neatly
//--------------------------------------
/* should the next message have an error box? */
var int MEMINT_ForceErrorBox;
func void MEM_SendToSpy(var int errorType, var string text) {
/* Implementierung wird von MEM_InitAll ersetzt! */
PrintDebug(ConcatStrings(text, "<<< (This is a preliminary printing variant, use MEM_InitAll to get neat 'Q:' prefixed messages.) >>>")); /* Q: is the Ikarus mark */
};
func void MEM_ErrorBox(var string text) {
MEMINT_ForceErrorBox = true;
MEM_SendToSpy(zERR_TYPE_FAULT, text);
};
func void MEM_PrintStackTrace() {
var string error; error = "MEM_PrintStackTrace: Cannot print the stacktrace before MEM_InitAll was called!";
MEM_SendToSpy(zERR_TYPE_FAULT, error);
};
func void MEMINT_HandleError(var int errorType, var string text) {
if (errorType >= zERR_PrintStackTrace) {
const int once = 0;
if (!once || !zERR_StackTraceOnlyForFirst) {
once = true;
MEM_PrintStackTrace();
};
};
if (errorType >= zERR_ReportToZSpy) {
const int errorBoxOnce = 0;
if (errorType >= zERR_ShowErrorBox)
&& (!zERR_ErrorBoxOnlyForFirst || !errorBoxOnce) {
MEMINT_ForceErrorBox = true;
errorBoxOnce = true;
};
MEM_SendToSpy(errorType, text);
};
};
func void MEM_Error(var string error) {
MEMINT_HandleError(zERR_TYPE_FAULT, error);
};
func void MEM_Warn(var string warn) {
MEMINT_HandleError(zERR_TYPE_WARN, warn);
};
func void MEM_Info(var string info) {
if (zERR_ReportToZSpy > zERR_TYPE_INFO)
&& (zERR_PrintStackTrace > zERR_TYPE_INFO) {
return; //dont waste time
};
MEMINT_HandleError(zERR_TYPE_INFO, info);
};
func void MEM_AssertFail (var string assertFailText) {
assertFailText = ConcatStrings ("Assertion failed. Report this: ", assertFailText);
MEM_Error (assertFailText);
};
/* custom channel */
func void MEM_Debug(var string message) {
message = ConcatStrings(zERR_DEBUG_PREFIX, message);
if (zERR_DEBUG_TOSCREEN) {
Print(message);
};
if (zERR_DEBUG_ERRORBOX) {
MEMINT_ForceErrorBox = true;
};
if (zERR_DEBUG_ERRORBOX || zERR_DEBUG_TOSPY) {
MEM_SendToSpy(zERR_DEBUG_TYPE, message);
};
};
//--------------------------------------
// Parser Data Stack Hacking
//--------------------------------------
class MEMINT_HelperClass {};
var MEMINT_HelperClass MEMINT_INSTUNASSIGNED;
var MEMINT_HelperClass MEMINT_PopDump;
func int MEMINT_StackPushInt (var int val) {
return +val;
};
//Vorsicht: Referenz wird gepusht!
func string MEMINT_StackPushString (var string val) {
return val;
};
func MEMINT_HelperClass MEMINT_StackPopInstSub () {};
func void MEMINT_StackPopInst () {
MEMINT_PopDump = MEMINT_StackPopInstSub();
};
func void MEMINT_StackPushInst (var int val) {
MEMINT_StackPushInt (val);
MEMINT_StackPopInst();
};
func void MEMINT_StackPushVar (var int adr) {
MEMINT_StackPushInst (adr);
MEMINT_StackPushInst (zPAR_TOK_PUSHVAR);
};
//Alternative Formulierungen:
func int MEMINT_PopInt() {};
func string MEMINT_PopString() {};
func int MEMINT_StackPopInt() {};
func string MEMINT_StackPopString() {};
func int MEMINT_StackPopInstAsInt() {
MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
};
//--------------------------------------
// MEM_Helper
//--------------------------------------
INSTANCE MEM_HELPER_INST (C_NPC)
{
name = MEM_HELPER_NAME;
id = 42;
/* unsterblich: */
flags = 2;
attribute [ATR_HITPOINTS_MAX] = 2;
attribute [ATR_HITPOINTS] = 2;
/* irgendein Visual: */
Mdl_SetVisual (self, "Meatbug.mds");
};
var oCNpc MEM_Helper;
func void MEMINT_GetMemHelper() {
MEM_Helper = Hlp_GetNpc (MEM_HELPER_INST);
if (!Hlp_IsValidNpc (MEM_Helper)) {
//self zwischenspeichern
var C_NPC selfBak;
selfBak = Hlp_GetNpc (self);
Wld_InsertNpc (MEM_HELPER_INST, MEM_FARFARAWAY);
MEM_Helper = Hlp_GetNpc (MEM_HELPER_INST);
self = Hlp_GetNpc (selfBak);
};
};
//GOTHIC_BASE_VERSION == 1 ? g1Val : g2Val
func int MEMINT_SwitchG1G2(var int g1Val, var int g2Val) {
if (GOTHIC_BASE_VERSION == 1) {
return +g1Val;
} else {
return +g2Val;
};
};
//######################################################
//
// Basic Read Write Operations
//
//######################################################
//--------------------------------------
// Reading Parser-Data-Stack-Hacking
//--------------------------------------
func int MEM_ReadInt (var int address) {
/* note: there will not be error handling once Ikarus is
* fully set up by MEM_InitAll. This function will be replaced. */
if (address == 0) {
MEM_Error ("MEM_ReadInt: address is NULL");
return 0;
};
MEMINT_StackPushVar (address);
MEMINT_StackPushInt (MEMINT_StackPopInt()); //als int nicht als var auf dem Stack
};
func string MEM_ReadString (var int address) {
if (address == 0) {
MEM_Error ("MEM_ReadString: address is NULL");
return "";
};
MEMINT_StackPushVar (address);
};
//--------------------------------------
// Assignments
//--------------------------------------
//Alte Lesemethode wird nur zum Bootstrap des neuen Systems gebraucht.
func void MEMINT_OldWriteInt (var int address, var int val) {
/* other = address - MEM_NpcID_Offset */
MEM_Helper.enemy = address - MEM_NpcID_Offset;
/* res wird nicht gebraucht, müllt aber sonst den Stack zu! */
var int res; res = Npc_GetTarget (MEM_Helper);
/* *(other + oCNpc_idx_offset) = val */
other.id = val;
};
func void MEMINT_PrepareAssignments() {
/* sorgt dafür, dass MEMINT_Assign und MEMINT_StrAssign
* genau die Funktion von zPAR_OP_IS bzw. zPAR_TOK_ASSIGNSTR
* erfüllen.
* Diese Funktion wird nach Start von Gothic genau einmal aufgerufen. */
var int symTab; var int MEMINT_Assign_Sym; var int MEMINT_Assign_StackPos; var int stackStart;
//Navigation zum Code dieser Funktionen:
symTab = MEM_ReadInt (ContentParserAddress + zCParser_symtab_table_array_offset);
stackStart = MEM_ReadInt (ContentParserAddress + zCParser_stack_offset);
MEMINT_Assign_Sym = MEM_ReadInt (symTab + 4 * (MEMINT_AssignPredecessor + 1));
MEMINT_Assign_StackPos = MEM_ReadInt (MEMINT_Assign_Sym + zCParSymbol_content_offset);
//alte Lesemethode braucht Npc
MEMINT_GetMemHelper();
var C_NPC othBak;
othBak = Hlp_GetNpc (other);
//Code überschreiben. Vorsicht: Der erste Aufruf soll auch klappen!
MEMINT_OldWriteInt (stackStart + MEMINT_Assign_StackPos , (zPAR_OP_IS << 0) | (zPAR_TOK_RET << 8) | (zPAR_TOK_RET << 16) | (zPAR_TOK_RET << 24));
MEMINT_OldWriteInt (stackStart + MEMINT_Assign_StackPos + 4, (zPAR_TOK_RET << 0) | (zPAR_OP_IS << 8) | (zPAR_TOK_RET << 16) | (zPAR_TOK_RET << 24));
MEMINT_OldWriteInt (stackStart + MEMINT_Assign_StackPos + 8, (zPAR_TOK_ASSIGNSTR << 0) | (zPAR_TOK_RET << 8) | (zPAR_TOK_RET << 16) | (zPAR_TOK_RET << 24));
MEMINT_OldWriteInt (stackStart + MEMINT_Assign_StackPos + 12, (zPAR_TOK_RET << 0) | (zPAR_TOK_ASSIGNSTR << 8) | (zPAR_TOK_RET << 16) | (zPAR_TOK_RET << 24));
//alte Lesemethode muss aufräumen
MEM_Helper.enemy = 0;
other = Hlp_GetNpc (othBak);
};
var MEMINT_HelperClass MEMINT_AssignPredecessor;
func void MEMINT_Assign() {
/* Hier soll stehen:
* zPAR_OP_IS
* zPAR_TOK_RET
*
* das schreibe ich da jetzt hin: */
MEMINT_PrepareAssignments (); //zPAR_TOK_CALL + 4 bytes
return; //zPAR_TOK_RET
return; //zPAR_TOK_RET
//zPAR_TOK_RET
//Summe: 8 Bytes
};
func void MEMINT_StrAssign() {
/* Hier soll stehen:
* zPAR_TOK_ASSIGNSTR
* zPAR_TOK_RET
*
* das schreibe ich da jetzt hin: */
MEMINT_PrepareAssignments (); //zPAR_TOK_CALL + 4 bytes
return; //zPAR_TOK_RET
return; //zPAR_TOK_RET
//zPAR_TOK_RET
//Summe: 8 Bytes
};
//--------------------------------------
// Schreiboperationen
//--------------------------------------
func void MEM_WriteInt (var int address, var int val) {
/* note: there will not be error handling once Ikarus is
* fully set up by MEM_InitAll. This function will be replaced. */
if (address == 0) {
MEM_Error ("MEM_WriteInt: address is NULL");
return;
};
MEMINT_StackPushInt (val);
MEMINT_StackPushVar (address);
MEMINT_Assign();
};
func void MEM_WriteString (var int address, var string val) {
if (address == 0) {
MEM_Error ("MEM_WriteString: address is NULL");
return;
};
MEMINT_StackPushString (val);
MEMINT_StackPushVar (address);
MEMINT_StrAssign();
};
//------------------------------------------------
// Byte-Zugriff
//------------------------------------------------
func int MEM_ReadByte (var int adr) {
return MEM_ReadInt (adr) & 255;
};
func void MEM_WriteByte (var int adr, var int val) {
if (val & ~ 255) {
MEM_Warn ("MEM_WriteByte: Val out of range! Truncating to 8 bits.");
val = val & 255;
};
MEM_WriteInt (adr, (MEM_ReadInt (adr) & ~ 255) | val);
};
//--------------------------------------
// Arrayzugriff
//--------------------------------------
func int MEM_ReadIntArray (var int arrayAddress, var int offset) {
return MEM_ReadInt (arrayAddress + 4 * offset);
};
func void MEM_WriteIntArray (var int arrayAddress, var int offset, var int value) {
MEM_WriteInt (arrayAddress + 4 * offset, value);
};
func int MEM_ReadByteArray (var int arrayAddress, var int offset) {
return MEM_ReadByte (arrayAddress + offset);
};
func void MEM_WriteByteArray (var int arrayAddress, var int offset, var int value) {
MEM_WriteByte (arrayAddress + offset, value);
};
/* Zurzeit in LeGo drin.
func string MEM_ReadStringArray (var int arrayAddress, var int offset) {
return MEM_ReadString (arrayAddress + offset * sizeof_zString);
};*/
func void MEM_WriteStringArray (var int arrayAddress, var int offset, var string value) {
MEM_WriteString (arrayAddress + sizeof_zString * offset, value);
};
//######################################################
//
// Basic zCParser related functions
//
//######################################################
//Deprecated, use MEM_Parser instead!
const int currParserAddress = 0; //const to keep it valid through loading
const int currSymbolTableAddress = 0;
const int currSymbolTableLength = 0;
const int currSortedSymbolTableAddress = 0;
const int currParserStackAddress = 0;
const int contentSymbolTableAddress = 0;
func void MEM_ReinitParser() {
currParserAddress = ContentParserAddress;
//Die Symboltabelle im Parser:
currSymbolTableAddress = MEM_ReadInt (currParserAddress + zCParser_symtab_table_array_offset);
currSymbolTableLength = MEM_ReadInt (currParserAddress + zCParser_symtab_table_array_offset + 8);
currSortedSymbolTableAddress = MEM_ReadInt (currParserAddress + zCParser_sorted_symtab_table_array_offset);
currParserStackAddress = MEM_ReadInt (currParserAddress + zCParser_stack_offset);
//Die Contentsymboltabelle braucht man immer mal wieder:
contentSymbolTableAddress = MEM_ReadInt (ContentParserAddress + zCParser_symtab_table_array_offset);
};
//removed, but keep stub
func void MEM_SetParser(var int ID) {
if (!ID) {
MEM_Warn("MEM_SetParser was removed in Ikarus Version 1.2 and should not be used any more.");
} else {
MEM_Error("MEM_SetParser was removed in Ikarus Version 1.2 and cannot be used to change the current parser any more.");
};
};
//************************************************
// Get and set instance offsets
//************************************************
//--------------------------------------
// Instanz auf Pointer zeigen lassen
//--------------------------------------
var int MEM_AssignInstSuppressNullWarning;
func void MEM_AssignInst (var int inst, var int ptr) {
if (inst <= 0) {
/* Anmerkung: inst == 0 kann auch nicht sein,
* da es keine Instance vor einer Klassendeklaration
* geben kann. */
MEM_Error (ConcatStrings ("MEM_AssignInst: Invalid instance: ", IntToString (inst)));
return;
};
if (ptr == 0 && !MEM_AssignInstSuppressNullWarning) {
/* Instanzen die Null sind, will man eigentlich nicht, die machen nur Ärger. */
MEM_Warn ("MEM_AssignInst: ptr is NULL. Use MEM_AssignInstNull if that's what you want.");
};
var int sym;
sym = MEM_ReadIntArray (currSymbolTableAddress, inst);
MEM_WriteInt (sym + zCParSymbol_offset_offset, ptr);
};
func void MEM_AssignInstNull (var int inst) {
/* Normalerweise will man Instanzen nicht zurück auf 0 setzen.
* Oft wird es ein Fehler sein. Daher wird oben eine Warnung ausgegeben.
* Um die nicht zu bekommen gibt es hier die explizite Funktion */
MEM_AssignInstSuppressNullWarning = true;
MEM_AssignInst (inst, 0);
MEM_AssignInstSuppressNullWarning = false;
};
func MEMINT_HelperClass MEM_PtrToInst (var int ptr) {
var MEMINT_HelperClass hlp;
const int hlpOffsetPtr = 0;
if (!hlpOffsetPtr) {
hlpOffsetPtr = MEM_ReadIntArray (currSymbolTableAddress, hlp) + zCParSymbol_offset_offset;
};
if (ptr == 0 && !MEM_AssignInstSuppressNullWarning ) {
/* Instanzen die Null sind, will man eigentlich nicht, die machen nur Ärger. */
MEM_Warn ("MEM_PtrToInst: ptr is NULL. Use MEM_NullToInst if that's what you want.");
MEM_WriteInt(hlpOffsetPtr, 0);
} else {
MEM_WriteInt(hlpOffsetPtr, ptr);
};
MEMINT_StackPushInst (hlp);
};
func MEMINT_HelperClass _^ (var int ptr) {
MEM_PtrToInst(ptr);
};
func MEMINT_HelperClass MEM_NullToInst () {
var MEMINT_HelperClass hlp;
MEMINT_StackPushInst (hlp);
};
func MEMINT_HelperClass MEM_CpyInst (var int inst) {
MEMINT_StackPushInst (inst);
};
//--------------------------------------
// Deprecated relict from the time
// when direct access to menu/pfx/vfx parsers
// was possible
//--------------------------------------
func void MEM_AssignContentInst (var int inst, var int ptr) {
const int once = 0;
if (!once) { once = true;
MEM_Warn("MEM_AssignContentInst: This function was deprecated in Ikarus Version 1.2. Use the equivalent MEM_AssignInst instead.");
};
MEM_AssignInst(inst, ptr);
};
func void MEM_AssignContentInstNull (var int inst) {
const int once = 0;
if (!once) { once = true;
MEM_Warn("MEM_AssignContentInstNull: This function was deprecated in Ikarus Version 1.2. Use the equivalent MEM_AssignInstNull instead.");
};
MEM_AssignInstNull(inst);
};
//--------------------------------------
// Get offset of an instance
//--------------------------------------
func int MEM_InstToPtr(var int inst) {
if (inst <= 0) {
/* Anmerkung: inst == 0 kann auch nicht sein,
* da es keine Instance vor eine Klassendeklaration
* geben kann. */
MEM_Error (ConcatStrings ("MEM_InstGetOffset: Invalid inst: ", IntToString (inst)));
return 0;
};
var int symb;
symb = MEM_ReadIntArray (currSymbolTableAddress, inst);
return MEM_ReadInt (symb + zCParSymbol_offset_offset);
};
//Abwärtskompatibilität
func int MEM_InstGetOffset (var int inst) {
return MEM_InstToPtr(inst);
};
//--------------------------------------
// Unsinnig. Nur zur Abwärtskompatibilität
// überhaupt noch drin. Google sagt,
// Lehona hat es mal irgendwo benutzt.
//--------------------------------------
//Lässt currParserSymb auf das Symbol mit Instanz inst zeigen.
INSTANCE currParserSymb (zCPar_Symbol);
func void MEM_SetCurrParserSymb (var int inst) {
if (inst <= 0) {
MEM_Error (ConcatStrings ("MEM_SetCurrParserSymb: Invalid inst: ", IntToString (inst)));
return;
};
var int symOffset; var int currParserSymOffset;
symOffset = MEM_ReadIntArray (currSymbolTableAddress, inst);
currParserSymOffset = MEM_ReadIntArray (contentSymbolTableAddress, currParserSymb);
MEM_WriteInt (currParserSymOffset + zCParSymbol_offset_offset, symOffset);
};
//************************************************
// Sprünge
//************************************************
/* Es sieht einfach aus, gell? Aber das das funktioniert ist
* gar nicht so offensichtlich wie man glaubt.
* Das hier geht zum Beispiel:
{
label = MEM_StackPos.position;
[...]
MEM_StackPos.position = label;
};
* Das hier geht grandios schief:
{
label = MEM_StackPos.position + 0;
[...]
MEM_StackPos.position = label;
};
* Wer Experimente macht, wird wahrscheinlich auf die Nase fallen.
* Es ist Zufall, dass es so einfach funktioniert! */
class MEMINT_StackPos {
var int position;
};
var MEMINT_StackPos MEM_StackPos;
func void MEM_InitLabels() {
MEM_StackPos = _^(ContentParserAddress + zCParser_stack_stackPtr_offset);
};
func void MEM_CallByPtr(var int ptr) {
MEM_StackPos.position = ptr;
};
func void MEM_CallByOffset(var int offset) {
MEM_CallByPtr(offset + currParserStackAddress);
};
//************************************************
// Idee von Gottfried: ID einer Funktion
//************************************************
func int MEM_GetFuncID(var func fnc) {
var zCPar_Symbol symb; /* dummy symbol with index indexOf(fnc)+1 */
symb = MEM_PtrToInst(MEM_ReadIntArray(contentSymbolTableAddress, symb - 1));
var int res;
var int loop; loop = MEM_StackPos.position;
if ((symb.bitfield & zCPar_Symbol_bitfield_type) != zPAR_TYPE_FUNC) {
MEM_Warn("MEM_GetFuncID: Unresolvable request (probably uninitialised function variable).");
return -1;
};
if (symb.bitfield & zPAR_FLAG_CONST) {
return +res;
} else {
res = symb.content;
symb = MEM_PtrToInst(MEM_ReadIntArray(contentSymbolTableAddress, res));
MEM_StackPos.position = loop;
};
};
func int MEM_GetFuncOffset(var func fnc) {
var int r;
r = MEM_GetFuncID(fnc); //ID(fnc)
r = MEM_ReadIntArray(contentSymbolTableAddress, r); //symbolTable[ID(fnc)]
r = MEM_ReadInt(r + zCParSymbol_content_offset); //symbolTable[ID(fnc)].content
return r + 0;
};
func int MEM_GetFuncPtr(var func fnc) {
return MEM_GetFuncOffset(fnc) + currParserStackAddress;
};
func void MEM_ReplaceFunc(var func f1, var func f2) {
var int ptr; ptr = MEM_GetFuncPtr(f1);
var int target; target = MEM_GetFuncOffset(f2);
/* jetzt bitte in einem Rutsch, nicht, dass da einer was ersetzen will, was ich brauche. */
MEM_WriteByte(ptr, zPAR_TOK_JUMP);
MEM_WriteInt (ptr + 1, target);
};
//************************************************
// Functions that help me write Byte Code
//************************************************
var int MEMINT_OverrideFunc_Ptr;
func void MEMINT_InitOverideFunc(var func f) {
MEMINT_OverrideFunc_Ptr = MEM_GetFuncPtr(f);
};
/* override function, token */
func void MEMINT_OFTok(var int tok) {
MEM_WriteByte(MEMINT_OverrideFunc_Ptr, tok);
MEMINT_OverrideFunc_Ptr += 1;
};
/* override function, token + parameter */
func void MEMINT_OFTokPar(var int tok, var int param) {
MEMINT_OFTok(tok);
MEM_WriteInt(MEMINT_OverrideFunc_Ptr, param);
MEMINT_OverrideFunc_Ptr += 4;
};
//************************************************
// New Operators
//************************************************
//--------------------------------------
// Address Operator
//--------------------------------------
//Dummies that are filled later:
func int MEM_GetIntAddress(var int i) {
MEM_Error("MEM_GetIntAddress called before MEM_GetAddress_Init!");
return 0;
};
func int MEM_GetFloatAddress(var float f) {
MEM_Error("MEM_GetFloatAddress called before MEM_GetAddress_Init!");
return 0;
};
func int MEM_GetStringAddress(var string s) {
MEM_Error("MEM_GetStringAddress called before MEM_GetAddress_Init!");
return 0;
};
func int _@(var int i) {
MEM_Error("_@ called before MEM_GetAddress_Init!");
i = i; i = i; i = i; i = i; i = i; i = i; /* some space */
return 0;
};
func int _@s(var string s) {
MEM_Error("_@s called before MEM_GetAddress_Init!");
return 0;
};
func int _@f(var float f) {
MEM_Error("_@f called before MEM_GetAddress_Init!");
return 0;
};
func void MEMINT_GetAddress_Init(var func f) {
var MEMINT_HelperClass symb;
MEMINT_InitOverideFunc(f);
MEMINT_OFTokPar(zPAR_TOK_PUSHINST , symb );
MEMINT_OFTok (zPAR_TOK_ASSIGNINST );
MEMINT_OFTokPar(zPAR_TOK_PUSHINST , zPAR_TOK_PUSHINT);
MEMINT_OFTok (zPAR_TOK_RET );
};
func void MEM_GetAddress_Init() {
const int init_done = 0;
if (!init_done) {
MEMINT_GetAddress_Init(MEM_GetIntAddress);
MEMINT_GetAddress_Init(MEM_GetFloatAddress);
MEMINT_GetAddress_Init(MEM_GetStringAddress);
MEMINT_GetAddress_Init(STR_GetAddress);
MEMINT_GetAddress_Init(_@f);
MEMINT_GetAddress_Init(_@s);
/* something else for _@ */
MEMINT_InitOverideFunc(_@);
/* push zPAR_TOK_PUSHINT */ MEMINT_OFTokPar(zPAR_TOK_PUSHINST , zPAR_TOK_PUSHINT );
/* push int zPAR_TOK_PUSHINT */ MEMINT_OFTokPar(zPAR_TOK_PUSHINT , zPAR_TOK_PUSHINT );
/* equal? */ MEMINT_OFTok (zPAR_OP_EQUAL);
/* jumpF */ MEMINT_OFTokPar(zPAR_TOK_JUMPF , MEMINT_OverrideFunc_Ptr + 16 - currParserStackAddress);
/* push zPAR_TOK_PUSHINT */ MEMINT_OFTokPar(zPAR_TOK_PUSHINST , zPAR_TOK_PUSHINT );
/* call MEM_InstToPtr */ MEMINT_OFTokPar(zPAR_TOK_CALL , MEM_GetFuncOffset(MEM_InstToPtr) );
/* ret */ MEMINT_OFTok (zPAR_TOK_RET);
/* push zPAR_TOK_PUSHINT */ MEMINT_OFTokPar(zPAR_TOK_PUSHINST , zPAR_TOK_PUSHINT );
/* ret */ MEMINT_OFTok (zPAR_TOK_RET);
/* return var address as int */
init_done = true;
};
};
/**** downward compatiblity: ****/
//alias for downward compatibility
func void STR_GetAddressInit() {
MEM_GetAddress_Init();
};
/* for downward compatiblity there is a guarantee, that
* STR_GetAddress works ininitialised, but the first time
* may only return an address of a copy of the string */
func int STR_GetAddress(var string str) {
str = str; //waste 11 bytes
MEM_GetAddress_Init(); //will override 12 bytes of THIS function
return STR_GetAddress(str);
};
//************************************************
// Access static Arrays
//************************************************
//Workers
func int MEMINT_ReadStatArr(var int offset) {
if (offset < 0) {
MEM_Error("MEM_ReadStatArr: Offset < 0!");
return 0;
};
MEMINT_StackPopInst();
MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
var int adr;
adr = MEMINT_StackPopInt();
return MEM_ReadIntArray(adr, offset);
};
func void MEMINT_WriteStatArr(var int offset, var int value) {
if (offset < 0) {
MEM_Error("MEM_WriteStatArr: Offset < 0!");
return;
};
/* pop only the first two, the third differently: */
MEMINT_StackPopInst();
MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
var int adr;
adr = MEMINT_StackPopInt();
MEM_WriteIntArray(adr, offset, value);
};
func void MEMINT_WriteStatStringArr(var int offset, var string value) {
if (offset < 0) {
MEM_Error("MEM_WriteStatStringArr: Offset < 0!");
return;
};
MEMINT_StackPopInst();
MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
var int adr; adr = MEMINT_StackPopInt();
adr += sizeof_zString * offset;
MEM_WriteString(adr, value);
};
func string MEMINT_ReadStatStringArr(var int offset) {
if (offset < 0) {
MEM_Error("MEM_ReadStatStringArr: Offset < 0!");
return "";
};
MEMINT_StackPopInst();
MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
var int adr; adr = MEMINT_StackPopInt();
adr += sizeof_zString * offset;
return MEM_ReadString(adr);
};
//Stubs
func void MEM_WriteStatArr (var int array, var int offset, var int value) {
MEM_Error ("MEM_WriteStatArr was called before MEM_InitStatArrs!");
};
func int MEM_ReadStatArr (var int array, var int offset) {
MEM_Error ("MEM_ReadStatArr was called before MEM_InitStatArrs!");
return 0;
};
func void MEM_WriteStatStringArr(var string array, var int offset, var string value) {
MEM_Error ("MEM_WriteStatStringArr was called before MEM_InitStatArrs!");
};
func string MEM_ReadStatStringArr(var string array, var int offset) {
MEM_Error ("MEM_ReadStatStringArr was called before MEM_InitStatArrs!");
};
func void MEM_InitStatArrs() {
const int done = 0;
if (!done) {
MEM_ReplaceFunc(MEM_WriteStatArr, MEMINT_WriteStatArr);
MEM_ReplaceFunc(MEM_ReadStatArr, MEMINT_ReadStatArr);
MEM_ReplaceFunc(MEM_WriteStatStringArr, MEMINT_WriteStatStringArr);
MEM_ReplaceFunc(MEM_ReadStatStringArr, MEMINT_ReadStatStringArr);
done = true;
};
};
//######################################################
//
// Speicher allozieren
//
//######################################################
func int MEM_Alloc (var int amount) {
/* string mit AAAA holen */
var int strPtr;
var string str; str = "AAAA";
strPtr = _@s(str); //Adresse des zStrings im Symbol str.
var zString zstr;
zstr = _^(strPtr); //zstr zeigt jetzt auf str