forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomTypeHandler.pas
executable file
·912 lines (708 loc) · 22.9 KB
/
CustomTypeHandler.pas
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
unit CustomTypeHandler;
{$mode delphi}
{
This class is used as a wrapper for different kinds of custom types
}
interface
{$ifdef jni} //not yet implemented, but the interface is available
uses
Classes, SysUtils, math;
type PLua_state=pointer;
{$else}
uses
dialogs, Classes, SysUtils,cefuncproc, lua, lauxlib, lualib,
math, commonTypeDefs;
{$endif}
type
TConversionRoutine=function(data: pointer):integer; stdcall;
TReverseConversionRoutine=procedure(i: integer; output: pointer); stdcall;
//I should have used cdecl from the start
TConversionRoutine2=function(data: pointer; address: ptruint):integer; cdecl;
TReverseConversionRoutine2=procedure(i: integer; address: ptruint; output: pointer); cdecl;
TCustomTypeException=class(Exception);
TCustomTypeType=(cttAutoAssembler, cttLuaScript, cttPlugin);
TCustomType=class
private
fname: string;
ffunctiontypename: string; //lua
lua_bytestovaluefunctionid: integer;
lua_valuetobytesfunctionid: integer;
lua_bytestovalue: string; //help string that contains the functionname so it doesn't have to build up this string at runtime
lua_valuetobytes: string;
routine: pointer;
reverseroutine: pointer;
currentscript: tstringlist;
fCustomTypeType: TCustomTypeType; //plugins set this to cttPlugin
fScriptUsesFloat: boolean;
fScriptUsesCDecl: boolean;
disableinfo: tobject;//Tdisableinfo;
procedure unloadscript;
procedure setName(n: string);
procedure setfunctiontypename(n: string);
public
bytesize: integer;
preferedAlignment: integer;
//these 4 functions are just to make it easier
procedure ConvertToData(f: single; output: pointer; address: ptruint); overload;
procedure ConvertToData(i: integer; output: pointer; address: ptruint); overload;
function ConvertDataToInteger(data: pointer; address: ptruint): integer;
function ConvertDataToIntegerLua(data: pbytearray; address: ptruint): integer;
procedure ConvertIntegerToData(i: integer; output: pointer; address: ptruint);
procedure ConvertIntegerToDataLua(i: integer; output: pbytearray; address: ptruint);
function ConvertDataToFloat(data: pointer; address: ptruint): single;
function ConvertDataToFloatLua(data: pbytearray; address: ptruint): single;
procedure ConvertFloatToData(f: single; output: pointer; address: ptruint);
procedure ConvertFloatToDataLua(f: single; output: pbytearray; address: ptruint);
function getScript:string;
procedure setScript(script:string; luascript: boolean=false);
constructor CreateTypeFromAutoAssemblerScript(script: string);
constructor CreateTypeFromLuaScript(script: string);
destructor destroy; override;
procedure remove; //call this instead of destroy
procedure showDebugInfo;
published
property name: string read fName write setName;
property functiontypename: string read ffunctiontypename write setfunctiontypename; //lua
property CustomTypeType: TCustomTypeType read fCustomTypeType;
property script: string read getScript write setScript;
property scriptUsesFloat: boolean read fScriptUsesFloat write fScriptUsesFloat;
end;
PCustomType=^TCustomType;
function GetCustomTypeFromName(name:string):TCustomType; //global function to retrieve a custom type
function registerCustomTypeLua(L: PLua_State): integer; cdecl;
function registerCustomTypeAutoAssembler(L: PLua_State): integer; cdecl;
var customTypes: TList; //list holding all the custom types
// AllIncludesCustomType: boolean;
MaxCustomTypeSize: integer;
implementation
{$ifndef jni}
uses mainunit, LuaHandler, LuaClass,autoassembler;
{$endif}
resourcestring
rsACustomTypeWithNameAlreadyExists = 'A custom type with name %s already '
+'exists';
rsACustomFunctionTypeWithNameAlreadyExists = 'A custom function type with '
+'name %s already exists';
rsFailureCreatingLuaObject = 'Failure creating lua object';
rsOnlyReturnTypenameBytecountAndFunctiontypename = 'Only return typename, '
+'bytecount and functiontypename';
rsBytesizeIs0 = 'bytesize is 0';
rsInvalidFunctiontypename = 'invalid functiontypename';
rsInvalidTypename = 'invalid typename';
rsUndefinedError = 'Undefined error';
rsCTHParameter3IsNotAValidFunction = 'Parameter 3 is not a valid function';
rsCTHParameter4IsNotAValidFunction = 'Parameter 4 is not a valid function';
rsCTHInvalidNumberOfParameters = 'Invalid number of parameters';
function GetCustomTypeFromName(name:string): TCustomType;
var i: integer;
begin
result:=nil;
for i:=0 to customTypes.Count-1 do
begin
if uppercase(TCustomType(customtypes.Items[i]).name)=uppercase(name) then
begin
result:=TCustomType(customtypes.Items[i]);
break;
end;
end;
end;
procedure TCustomType.setName(n: string);
var i: integer;
begin
//check if there is already a script with this name (and not this one)
for i:=0 to customtypes.count-1 do
if uppercase(TCustomType(customtypes[i]).name)=uppercase(n) then
begin
if TCustomType(customtypes[i])<>self then
raise TCustomTypeException.create(Format(rsACustomTypeWithNameAlreadyExists, [n]));
end;
fname:=n;
end;
procedure TCustomType.setfunctiontypename(n: string);
var i: integer;
begin
//check if there is already a script with this functiontype name (and not this one)
for i:=0 to customtypes.count-1 do
if uppercase(TCustomType(customtypes[i]).functiontypename)=uppercase(n) then
begin
if TCustomType(customtypes[i])<>self then
raise TCustomTypeException.create(Format(rsACustomFunctionTypeWithNameAlreadyExists, [n]));
end;
ffunctiontypename:=n;
lua_bytestovalue:=n+'_bytestovalue';
lua_valuetobytes:=n+'_valuetobytes';
end;
function TCustomType.getScript: string;
begin
if ((fCustomTypeType=cttAutoAssembler) or (fCustomTypeType=cttLuaScript)) and (currentscript<>nil) then
result:=currentscript.text
else
result:='';
end;
procedure TCustomType.ConvertIntegerToDataLua(i: integer; output: pbytearray; address: ptruint);
var
L: PLua_State;
r: integer;
c,b: integer;
begin
{$ifndef jni}
l:=LuaVM;
if lua_valuetobytesfunctionid=-1 then
begin
lua_getglobal(LuaVM, pchar(lua_valuetobytes));
lua_valuetobytesfunctionid:=luaL_ref(LuaVM,LUA_REGISTRYINDEX);
end;
lua_rawgeti(Luavm, LUA_REGISTRYINDEX, lua_valuetobytesfunctionid);
lua_pushinteger(L, i);
lua_pushinteger(L, address);
if lua_pcall(l,2,bytesize,0)=0 then
begin
r:=lua_gettop(L);
if r>0 then
begin
b:=0;
for c:=-r to -1 do
begin
output[b]:=lua_tointeger(L, c);
inc(b);
end;
lua_pop(L,r);
end;
end;
{$endif}
end;
procedure TCustomType.ConvertIntegerToData(i: integer; output: pointer; address: ptruint);
var f: single;
begin
if scriptUsesFloat then //convert to a float and pass that
begin
f:=i;
i:=pdword(@f)^;
end;
if assigned(reverseroutine) then
begin
if fScriptUsesCDecl then
TReverseConversionRoutine2(reverseroutine)(i,address, output)
else
TReverseConversionRoutine(reverseroutine)(i,output);
end
else
begin
//possible lua
if fCustomTypeType=cttLuaScript then
ConvertIntegerToDataLua(i, output, address);
end;
end;
function TCustomType.ConvertDataToIntegerLua(data: pbytearray; address: ptruint): integer; //split up for speed
var
L: PLua_State;
i: integer;
begin
{$IFNDEF jni}
l:=LuaVM;
result:=0;
if lua_bytestovaluefunctionid=-1 then
begin
lua_getglobal(LuaVM, pchar(lua_bytestovalue));
lua_bytestovaluefunctionid:=luaL_ref(LuaVM,LUA_REGISTRYINDEX);
end;
// messagebox(0,'going to call rawgeti','bla',0);
lua_rawgeti(Luavm, LUA_REGISTRYINDEX, lua_bytestovaluefunctionid);
// messagebox(0,'after call rawgeti','bla',0);
for i:=0 to bytesize-1 do
lua_pushinteger(L,data[i]);
lua_pushinteger(L, address);
lua_call(L, bytesize+1,1);
{
if lua_pcall(L, bytesize+1,1, 0)<>0 then
begin
Log('customtype error:'+Lua_ToString(L,-1));
end;
}
result:=lua_tointeger(L, -1);
lua_pop(L,lua_gettop(l));
{$ENDIF}
end;
function TCustomType.ConvertDataToInteger(data: pointer; address: ptruint): integer;
var
i: dword;
f: single absolute i;
begin
if assigned(routine) then
begin
if fScriptUsesCDecl then
result:=TConversionRoutine2(routine)(data, address)
else
result:=TConversionRoutine(routine)(data);
end
else
begin
//possible lua
if fCustomTypeType=cttLuaScript then
result:=ConvertDataToIntegerLua(data, address)
else
result:=0;
end;
if fScriptUsesFloat then //the result is still in float state
begin
i:=result;
result:=trunc(f);
end;
end;
procedure TCustomType.ConvertFloatToDataLua(f: single; output: pbytearray; address: ptruint);
//I REALLY doubt anyone in their right mind would use lua to encode a float as bytes, but it's here...
var
L: PLua_State;
r: integer;
c,b: integer;
begin
{$IFNDEF jni}
l:=LuaVM;
if lua_valuetobytesfunctionid=-1 then
begin
lua_getglobal(L, pchar(lua_valuetobytes));
lua_valuetobytesfunctionid:=luaL_ref(LuaVM,LUA_REGISTRYINDEX);
end;
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_valuetobytesfunctionid);
lua_pushnumber(L, f);
lua_pushinteger(L, address);
if lua_pcall(l,2,bytesize,0)=0 then
begin
r:=lua_gettop(L);
if r>0 then
begin
b:=0;
for c:=-r to -1 do
begin
output[b]:=lua_tointeger(L, c);
inc(b);
end;
lua_pop(L,r);
end;
end;
{$ENDIF}
end;
procedure TCustomType.ConvertFloatToData(f: single; output: pointer; address: ptruint);
var i: integer;
begin
i:=pdword(@f)^; //convert the f to a integer without conversion (reverseroutine takes an integer, but could be any 32-bit value really)
if not scriptUsesFloat then //WHY even call this ?
i:=trunc(f);
if assigned(reverseroutine) then
begin
if fScriptUsesCDecl then
TReverseConversionRoutine2(reverseroutine)(i,address, output)
else
TReverseConversionRoutine(reverseroutine)(i,output);
end
else
begin
//possible lua
if fCustomTypeType=cttLuaScript then
ConvertFloatToDataLua(f, output, address);
end;
end;
function TCustomType.ConvertDataToFloatLua(data: PByteArray; address: ptruint): single;
//again, why would anyone use lua for this ?
var
L: PLua_State;
i: integer;
begin
{$IFNDEF jni}
l:=LuaVM;
if lua_bytestovaluefunctionid=-1 then
begin
lua_getglobal(L, pchar(lua_bytestovalue));
lua_bytestovaluefunctionid:=luaL_ref(L,LUA_REGISTRYINDEX);
end;
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_bytestovaluefunctionid);
for i:=0 to bytesize-1 do
lua_pushinteger(L,data[i]);
lua_pushinteger(L, address);
lua_call(L, bytesize+1,1);
result:=lua_tonumber(L, -1);
lua_pop(L,lua_gettop(l));
{$ENDIF}
end;
function TCustomType.ConvertDataToFloat(data: pointer; address: ptruint): single;
var
i: dword;
f: single absolute i;
begin
if assigned(routine) then
begin
if fScriptUsesCDecl then
i:=TConversionRoutine2(routine)(data,address)
else
i:=TConversionRoutine(routine)(data);
if not fScriptUsesFloat then //the result is in integer format ,
f:=i; //convert the integer to float
end
else
begin
//possible lua
if fCustomTypeType=cttLuaScript then
f:=ConvertDataToFloatLua(data, address)
else
f:=0;
end;
result:=f;
end;
procedure TCustomType.ConvertToData(f: single; output: pointer; address: ptruint);
begin
ConvertFloatToData(f, output, address);
end;
procedure TCustomType.ConvertToData(i: integer; output: pointer; address: ptruint);
begin
ConvertIntegerToData(i, output, address);
end;
procedure TCustomType.unloadscript;
var enablepos, disablepos: integer;
begin
{$IFNDEF jni}
if fCustomTypeType=cttAutoAssembler then
begin
routine:=nil;
reverseroutine:=nil;
if currentscript<>nil then
begin
getenableanddisablepos(currentscript, enablepos, disablepos);
if disablepos>=0 then
autoassemble(currentscript,false, false, false, true, tdisableinfo(disableinfo));
freeandnil(currentscript);
end;
end;
{$ENDIF}
end;
procedure TCustomType.setScript(script:string; luascript: boolean=false);
var i: integer;
s: tstringlist;
error:pchar;
//lua vars
returncount: integer;
// templua: Plua_State;
ftn,tn: pchar;
oldname: string;
oldfunctiontypename: string;
newpreferedalignment, oldpreferedalignment: integer;
oldScriptUsesFloat, newScriptUsesFloat: boolean;
oldScriptUsesCDecl, newScriptUsesCDecl: boolean;
newroutine, oldroutine: pointer;
newreverseroutine, oldreverseroutine: pointer;
newbytesize, oldbytesize: integer;
newdisableinfo: TDisableInfo;
begin
{$IFNDEF jni}
oldname:=fname;
oldfunctiontypename:=ffunctiontypename;
oldroutine:=routine;
oldreverseroutine:=reverseroutine;
oldbytesize:=bytesize;
oldpreferedalignment:=preferedalignment;
oldScriptUsesFloat:=fScriptUsesFloat;
oldScriptUsesCDecl:=fScriptUsesCDecl;
try
//if anything goes wrong the old values get set back
if not luascript then
begin
s:=tstringlist.create;
try
s.text:=script;
newdisableinfo:=tdisableinfo.create;
if autoassemble(s,false, true, false, true, newdisableinfo) then
begin
newpreferedalignment:=-1;
newScriptUsesFloat:=false;
newScriptUsesCDecl:=false;
//find alloc "ConvertRoutine"
for i:=0 to length(newdisableinfo.allocs)-1 do
begin
if uppercase(newdisableinfo.allocs[i].varname)='TYPENAME' then
name:=pchar(newdisableinfo.allocs[i].address);
if uppercase(newdisableinfo.allocs[i].varname)='CONVERTROUTINE' then
newroutine:=pointer(newdisableinfo.allocs[i].address);
if uppercase(newdisableinfo.allocs[i].varname)='BYTESIZE' then
newbytesize:=pinteger(newdisableinfo.allocs[i].address)^;
if uppercase(newdisableinfo.allocs[i].varname)='PREFEREDALIGNMENT' then
newpreferedalignment:=pinteger(newdisableinfo.allocs[i].address)^;
if uppercase(newdisableinfo.allocs[i].varname)='USESFLOAT' then
newScriptUsesFloat:=pbyte(newdisableinfo.allocs[i].address)^<>0;
if uppercase(newdisableinfo.allocs[i].varname)='CALLMETHOD' then
newScriptUsesCDecl:=pbyte(newdisableinfo.allocs[i].address)^<>0;
if uppercase(newdisableinfo.allocs[i].varname)='CONVERTBACKROUTINE' then
newreverseroutine:=pointer(newdisableinfo.allocs[i].address);
end;
if newpreferedalignment=-1 then
newpreferedalignment:=newbytesize;
//still here
unloadscript; //unload the old script
//and now set the new values
bytesize:=newbytesize;
routine:=newroutine;
reverseroutine:=newreverseroutine;
preferedAlignment:=newpreferedalignment;
fScriptUsesFloat:=newScriptUsesFloat;
fScriptUsesCDecl:=newScriptUsesCDecl;
fCustomTypeType:=cttAutoAssembler;
if currentscript<>nil then
freeandnil(currentscript);
currentscript:=tstringlist.create;
currentscript.text:=script;
if disableinfo<>nil then
freeandnil(disableinfo);
disableinfo:=newdisableinfo;
end;
finally
s.free;
end;
end
else
begin
try
lua_pop(luavm, lua_gettop(luavm));
if lua_dostring(luavm, pchar(script))=0 then //success, lua script loaded
begin
returncount:=lua_gettop(luavm);
if returncount<>3 then
raise TCustomTypeException.create(rsOnlyReturnTypenameBytecountAndFunctiontypename);
//-1=functiontypename
//-2=bytecount
//-3=typename
ftn:=lua.lua_tostring(luavm,-1);
bytesize:=lua_tointeger(luavm,-2);
tn:=lua.lua_tostring(luavm,-3);
if bytesize=0 then raise TCustomTypeException.create(rsBytesizeIs0);
if ftn=nil then raise TCustomTypeException.create(rsInvalidFunctiontypename);
if tn=nil then raise TCustomTypeException.create(rsInvalidTypename);
name:=tn;
functiontypename:=ftn;
end
else
begin
//something went wrong
if lua_gettop(luavm)>0 then
begin
error:=lua.lua_tostring(luavm,-1);
raise TCustomTypeException.create(error);
end else raise TCustomTypeException.create(rsUndefinedError);
end;
finally
lua_pop(luavm, lua_gettop(luavm));
end;
//still here so the script got loaded and passed the tests
fCustomTypeType:=cttLuaScript;
if currentscript=nil then
currentscript:=tstringlist.create;
currentscript.text:=script;
lua_getglobal(LuaVM, pchar(lua_bytestovalue));
lua_bytestovaluefunctionid:=luaL_ref(LuaVM,LUA_REGISTRYINDEX);
lua_getglobal(LuaVM, pchar(lua_valuetobytes));
lua_valuetobytesfunctionid:=luaL_ref(LuaVM,LUA_REGISTRYINDEX);
lua_pop(LuaVM,lua_getTop(luavm));
end;
except
on e: exception do
begin
//restore the old state if there is any
fname:=oldname;
ffunctiontypename:=oldfunctiontypename;
routine:=oldroutine;
reverseroutine:=oldreverseroutine;
bytesize:=oldbytesize;
preferedAlignment:=oldpreferedalignment;
fScriptUsesFloat:=oldScriptUsesFloat;
fScriptUsesCDecl:=oldScriptUsesCDecl;
raise TCustomTypeException.create(e.Message); //and now raise the error
end;
end;
{$ENDIF}
end;
constructor TCustomType.CreateTypeFromLuaScript(script: string);
begin
inherited create;
lua_bytestovaluefunctionid:=-1;
lua_valuetobytesfunctionid:=-1;
setScript(script,true);
//still here so everything ok
customtypes.Add(self);
MaxCustomTypeSize:=max(MaxCustomTypeSize, bytesize);
end;
constructor TCustomType.CreateTypeFromAutoAssemblerScript(script: string);
begin
inherited create;
lua_bytestovaluefunctionid:=-1;
lua_valuetobytesfunctionid:=-1;
setScript(script);
//still here so everything ok
customtypes.Add(self);
MaxCustomTypeSize:=max(MaxCustomTypeSize, bytesize);
end;
procedure TCustomType.remove;
var i: integer;
begin
unloadscript;
//remove self from array
i:=customTypes.IndexOf(self);
if i<>-1 then
customTypes.Delete(i);
//get a new max
MaxCustomTypeSize:=0;
for i:=0 to customTypes.count-1 do
MaxCustomTypeSize:=max(MaxCustomTypeSize, TCustomType(customTypes[i]).bytesize);
mainform.RefreshCustomTypes;
end;
procedure TCustomType.showDebugInfo;
var x,y: pointer;
begin
{$IFNDEF jni}
x:=@routine;
y:=@reverseroutine;
ShowMessage(format('routine=%p reverseroutine=%p',[x, y]));
{$ENDIF}
end;
destructor TCustomType.destroy;
begin
remove;
//call destroy watchers
inherited destroy;
end;
//lua
function registerCustomTypeLua(L: PLua_State): integer; cdecl;
var
parameters: integer;
typename: string;
bytecount: integer;
f_bytestovalue: integer;
bytestovalue: string;
f_valuetobytes: integer;
valuetobytes: string;
isfloat: boolean;
ct: TCustomType;
begin
{$IFNDEF jni}
result:=0;
parameters:=lua_gettop(L);
if parameters>=4 then
begin
typename:=Lua_ToString(L, 1);
bytecount:=lua_tointeger(L, 2);
f_bytestovalue:=0;
f_valuetobytes:=0;
if lua_isfunction(L, 3) then
begin
lua_pushvalue(L, 3);
f_bytestovalue:=luaL_ref(L,LUA_REGISTRYINDEX);
end
else
if lua_isstring(L,3) then
begin
bytestovalue:=Lua_ToString(L, 3);
lua_getglobal(L, pchar(bytestovalue));
f_valuetobytes:=luaL_ref(L,LUA_REGISTRYINDEX);
end
else
begin
lua_pop(L, lua_gettop(L));
lua_pushstring(L,rsCTHParameter3IsNotAValidFunction);
lua_error(L);
exit;
end;
if lua_isfunction(L, 4) then
begin
lua_pushvalue(L, 4);
f_valuetobytes:=luaL_ref(L,LUA_REGISTRYINDEX);
//f_bytestovalue:=luaL_ref(L,LUA_REGISTRYINDEX);
end
else
if lua_isstring(L,4) then
begin
valuetobytes:=Lua_ToString(L, 4);
lua_getglobal(LuaVM, pchar(valuetobytes));
f_valuetobytes:=luaL_ref(L,LUA_REGISTRYINDEX);
end
else
begin
lua_pop(L, parameters);
lua_pushstring(L,rsCTHParameter4IsNotAValidFunction);
lua_error(L);
exit;
end;
if parameters>=5 then
isfloat:=lua_toboolean(L,5)
else
isFloat:=false;
lua_pop(L, parameters);
ct:=GetCustomTypeFromName(typename); //see if one with this name altready exists.
if ct=nil then //if not, create it
ct:=TCustomType.Create;
ct.fCustomTypeType:=cttLuaScript;
ct.lua_bytestovaluefunctionid:=f_bytestovalue;
ct.lua_valuetobytesfunctionid:=f_valuetobytes;
ct.name:=typename;
ct.bytesize:=bytecount;
ct.scriptUsesFloat:=isfloat;
customtypes.Add(ct);
mainform.RefreshCustomTypes;
luaclass_newClass(L, ct);
result:=1;
end
else lua_pop(L, parameters);
{$ENDIF}
end;
function registerCustomTypeAutoAssembler(L: PLua_State): integer; cdecl;
var
parameters: integer;
typename: string='';
bytecount: integer;
script: string;
ct: TCustomType;
s: TStringList;
i: integer;
begin
{$IFNDEF jni}
result:=0;
bytecount:=1;
parameters:=lua_gettop(L);
if parameters=3 then
begin
typename:=Lua_ToString(L, 1);
bytecount:=lua_tointeger(L, 2);
script:=Lua_ToString(L, 3);
end
else
if parameters=1 then
script:=Lua_ToString(L, 1)
else
begin
lua_pop(L, parameters);
lua_pushstring(L,rsCTHInvalidNumberOfParameters);
lua_error(L);
exit;
end;
lua_pop(L, parameters);
try
ct:=TCustomType.CreateTypeFromAutoAssemblerScript(script);
except
on e: exception do
begin
lua_pushnil(L);
lua_pushstring(L,e.message);
exit(2);
end;
end;
if parameters=3 then //old version support
begin
ct.name:=typename;
ct.bytesize:=bytecount;
end;
mainform.RefreshCustomTypes;
luaclass_newClass(L, ct);
result:=1;
{$ENDIF}
end;
initialization
customTypes:=Tlist.create;
finalization
if customTypes<>nil then
customtypes.free;
end.