-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmonoscript.lua
5340 lines (4324 loc) · 142 KB
/
monoscript.lua
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
if getTranslationFolder()~='' then
loadPOFile(getTranslationFolder()..'monoscript.po')
end
local thread_checkifmonoanyhow=nil
local StructureElementCallbackID=nil
local pathsep
local libfolder
if getOperatingSystem()==0 then
pathsep=[[\]]
libfolder='dlls'
else
pathsep='/'
libfolder='dylibs'
end
local dpiscale=getScreenDPI()/96
--[[local]] monocache={}
mono_timeout=3000 --change to 0 to never timeout (meaning: 0 will freeze your face off if it breaks on a breakpoint, just saying ...)
MONO_DATACOLLECTORVERSION=20231214
MONOCMD_INITMONO=0
MONOCMD_OBJECT_GETCLASS=1
MONOCMD_ENUMDOMAINS=2
MONOCMD_SETCURRENTDOMAIN=3
MONOCMD_ENUMASSEMBLIES=4
MONOCMD_GETIMAGEFROMASSEMBLY=5
MONOCMD_GETIMAGENAME=6
MONOCMD_ENUMCLASSESINIMAGE=7
MONOCMD_ENUMFIELDSINCLASS=8
MONOCMD_ENUMMETHODSINCLASS=9
MONOCMD_COMPILEMETHOD=10
MONOCMD_GETMETHODHEADER=11
MONOCMD_GETMETHODHEADER_CODE=12
MONOCMD_LOOKUPRVA=13
MONOCMD_GETJITINFO=14
MONOCMD_FINDCLASS=15
MONOCMD_FINDMETHOD=16
MONOCMD_GETMETHODNAME=17
MONOCMD_GETMETHODCLASS=18
MONOCMD_GETCLASSNAME=19
MONOCMD_GETCLASSNAMESPACE=20
MONOCMD_FREEMETHOD=21
MONOCMD_TERMINATE=22
MONOCMD_DISASSEMBLE=23
MONOCMD_GETMETHODSIGNATURE=24
MONOCMD_GETPARENTCLASS=25
MONOCMD_GETSTATICFIELDADDRESSFROMCLASS=26
MONOCMD_GETTYPECLASS=27
MONOCMD_GETARRAYELEMENTCLASS=28
MONOCMD_FINDMETHODBYDESC=29
MONOCMD_INVOKEMETHOD=30
MONOCMD_LOADASSEMBLY=31
MONOCMD_GETFULLTYPENAME=32
MONOCMD_OBJECT_NEW=33
MONOCMD_OBJECT_INIT=34
MONOCMD_GETVTABLEFROMCLASS=35
MONOCMD_GETMETHODPARAMETERS=36
MONOCMD_ISCLASSGENERIC=37
MONOCMD_ISIL2CPP=38
MONOCMD_FILLOPTIONALFUNCTIONLIST=39
MONOCMD_GETSTATICFIELDVALUE=40 --fallback for il2cpp which doesn't expose what's needed
MONOCMD_SETSTATICFIELDVALUE=41
MONOCMD_GETCLASSIMAGE=42
MONOCMD_FREE=43
MONOCMD_GETIMAGEFILENAME=44
MONOCMD_GETCLASSNESTINGTYPE=45
MONOCMD_LIMITEDCONNECTION=46
MONOCMD_GETMONODATACOLLECTORVERSION=47
MONOCMD_NEWSTRING=48
MONOCMD_ENUMIMAGES=49
MONOCMD_ENUMCLASSESINIMAGEEX=50
MONOCMD_ISCLASSENUM = 51
MONOCMD_ISCLASSVALUETYPE = 52
MONOCMD_ISCLASSISSUBCLASSOF = 53
MONOCMD_ARRAYELEMENTSIZE = 54
MONOCMD_GETCLASSTYPE = 55
MONOCMD_GETCLASSOFTYPE = 56
MONOCMD_GETTYPEOFMONOTYPE = 57
MONOCMD_GETREFLECTIONTYPEOFCLASSTYPE = 58
MONOCMD_GETREFLECTIONMETHODOFMONOMETHOD = 59
MONOCMD_MONOOBJECTUNBOX = 60
MONOCMD_MONOARRAYNEW = 61
MONOCMD_ENUMINTERFACESOFCLASS = 62
MONOCMD_GETMETHODFULLNAME = 63
MONOCMD_TYPEISBYREF = 64
MONOCMD_GETPTRTYPECLASS = 65
MONOCMD_GETFIELDTYPE = 66
MONOCMD_GETTYPEPTRTYPE = 67
MONO_TYPE_END = 0x00 -- End of List
MONO_TYPE_VOID = 0x01
MONO_TYPE_BOOLEAN = 0x02
MONO_TYPE_CHAR = 0x03
MONO_TYPE_I1 = 0x04
MONO_TYPE_U1 = 0x05
MONO_TYPE_I2 = 0x06
MONO_TYPE_U2 = 0x07
MONO_TYPE_I4 = 0x08
MONO_TYPE_U4 = 0x09
MONO_TYPE_I8 = 0x0a
MONO_TYPE_U8 = 0x0b
MONO_TYPE_R4 = 0x0c
MONO_TYPE_R8 = 0x0d
MONO_TYPE_STRING = 0x0e
MONO_TYPE_PTR = 0x0f -- arg: <type> token
MONO_TYPE_BYREF = 0x10 -- arg: <type> token
MONO_TYPE_VALUETYPE = 0x11 -- arg: <type> token
MONO_TYPE_CLASS = 0x12 -- arg: <type> token
MONO_TYPE_VAR = 0x13 -- number
MONO_TYPE_ARRAY = 0x14 -- type, rank, boundsCount, bound1, loCount, lo1
MONO_TYPE_GENERICINST= 0x15 -- <type> <type-arg-count> <type-1> \x{2026} <type-n> */
MONO_TYPE_TYPEDBYREF = 0x16
MONO_TYPE_I = 0x18
MONO_TYPE_U = 0x19
MONO_TYPE_FNPTR = 0x1b -- arg: full method signature */
MONO_TYPE_OBJECT = 0x1c
MONO_TYPE_SZARRAY = 0x1d -- 0-based one-dim-array */
MONO_TYPE_MVAR = 0x1e -- number */
MONO_TYPE_CMOD_REQD = 0x1f -- arg: typedef or typeref token */
MONO_TYPE_CMOD_OPT = 0x20 -- optional arg: typedef or typref token */
MONO_TYPE_INTERNAL = 0x21 -- CLR internal type */
MONO_TYPE_MODIFIER = 0x40 -- Or with the following types */
MONO_TYPE_SENTINEL = 0x41 -- Sentinel for varargs method signature */
MONO_TYPE_PINNED = 0x45 -- Local var that points to pinned object */
MONO_TYPE_ENUM = 0x55 -- an enumeration */
monoTypeToVartypeLookup={}
monoTypeToVartypeLookup[MONO_TYPE_BOOLEAN]=vtByte
monoTypeToVartypeLookup[MONO_TYPE_CHAR]=vtUnicodeString --the actual chars...
monoTypeToVartypeLookup[MONO_TYPE_I1]=vtByte
monoTypeToVartypeLookup[MONO_TYPE_U1]=vtByte
monoTypeToVartypeLookup[MONO_TYPE_I2]=vtWord
monoTypeToVartypeLookup[MONO_TYPE_U2]=vtWord
monoTypeToVartypeLookup[MONO_TYPE_I4]=vtDword
monoTypeToVartypeLookup[MONO_TYPE_U4]=vtDword
monoTypeToVartypeLookup[MONO_TYPE_I8]=vtQword
monoTypeToVartypeLookup[MONO_TYPE_U8]=vtQword
monoTypeToVartypeLookup[MONO_TYPE_R4]=vtSingle
monoTypeToVartypeLookup[MONO_TYPE_R8]=vtDouble
monoTypeToVartypeLookup[MONO_TYPE_STRING]=vtPointer --pointer to a string object
monoTypeToVartypeLookup[MONO_TYPE_PTR]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_I]=vtPointer --IntPtr
monoTypeToVartypeLookup[MONO_TYPE_U]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_OBJECT]=vtPointer --object
monoTypeToVartypeLookup[MONO_TYPE_BYREF]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_CLASS]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_FNPTR]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_GENERICINST]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_ARRAY]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_SZARRAY]=vtPointer
monoTypeToVartypeLookup[MONO_TYPE_VALUETYPE]=vtPointer --needed for structs when returned by invoking a method( even though they are not qwords)
monoTypeToCStringLookup={}
monoTypeToCStringLookup[MONO_TYPE_END]='void'
monoTypeToCStringLookup[MONO_TYPE_BOOLEAN]='boolean'
monoTypeToCStringLookup[MONO_TYPE_CHAR]='char'
monoTypeToCStringLookup[MONO_TYPE_I1]='char'
monoTypeToCStringLookup[MONO_TYPE_U1]='unsigned char'
monoTypeToCStringLookup[MONO_TYPE_I2]='short'
monoTypeToCStringLookup[MONO_TYPE_U2]='unsigned short'
monoTypeToCStringLookup[MONO_TYPE_I4]='int'
monoTypeToCStringLookup[MONO_TYPE_U4]='unsigned int'
monoTypeToCStringLookup[MONO_TYPE_I8]='int64'
monoTypeToCStringLookup[MONO_TYPE_U8]='unsigned int 64'
monoTypeToCStringLookup[MONO_TYPE_R4]='single'
monoTypeToCStringLookup[MONO_TYPE_R8]='double'
monoTypeToCStringLookup[MONO_TYPE_STRING]='String'
monoTypeToCStringLookup[MONO_TYPE_PTR]='Pointer'
monoTypeToCStringLookup[MONO_TYPE_BYREF]='Object'
monoTypeToCStringLookup[MONO_TYPE_CLASS]='Object'
monoTypeToCStringLookup[MONO_TYPE_FNPTR]='Function'
monoTypeToCStringLookup[MONO_TYPE_GENERICINST]='<Generic>'
monoTypeToCStringLookup[MONO_TYPE_ARRAY]='Array[]'
monoTypeToCStringLookup[MONO_TYPE_SZARRAY]='String[]'
FIELD_ATTRIBUTE_FIELD_ACCESS_MASK=0x0007
FIELD_ATTRIBUTE_COMPILER_CONTROLLED=0x0000
FIELD_ATTRIBUTE_PRIVATE=0x0001
FIELD_ATTRIBUTE_FAM_AND_ASSEM=0x0002
FIELD_ATTRIBUTE_ASSEMBLY=0x0003
FIELD_ATTRIBUTE_FAMILY=0x0004
FIELD_ATTRIBUTE_FAM_OR_ASSEM=0x0005
FIELD_ATTRIBUTE_PUBLIC=0x0006
FIELD_ATTRIBUTE_STATIC=0x0010
FIELD_ATTRIBUTE_INIT_ONLY=0x0020
FIELD_ATTRIBUTE_LITERAL=0x0040
FIELD_ATTRIBUTE_NOT_SERIALIZED=0x0080
FIELD_ATTRIBUTE_SPECIAL_NAME=0x0200
FIELD_ATTRIBUTE_PINVOKE_IMPL=0x2000
FIELD_ATTRIBUTE_RESERVED_MASK=0x9500
FIELD_ATTRIBUTE_RT_SPECIAL_NAME=0x0400
FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL=0x1000
FIELD_ATTRIBUTE_HAS_DEFAULT=0x8000
FIELD_ATTRIBUTE_HAS_FIELD_RVA=0x0100
METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK =0x0007
METHOD_ATTRIBUTE_COMPILER_CONTROLLED =0x0000
METHOD_ATTRIBUTE_PRIVATE =0x0001
METHOD_ATTRIBUTE_FAM_AND_ASSEM =0x0002
METHOD_ATTRIBUTE_ASSEM =0x0003
METHOD_ATTRIBUTE_FAMILY =0x0004
METHOD_ATTRIBUTE_FAM_OR_ASSEM =0x0005
METHOD_ATTRIBUTE_PUBLIC =0x0006
METHOD_ATTRIBUTE_STATIC =0x0010
METHOD_ATTRIBUTE_FINAL =0x0020
METHOD_ATTRIBUTE_VIRTUAL =0x0040
METHOD_ATTRIBUTE_HIDE_BY_SIG =0x0080
METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK =0x0100
METHOD_ATTRIBUTE_REUSE_SLOT =0x0000
METHOD_ATTRIBUTE_NEW_SLOT =0x0100
METHOD_ATTRIBUTE_STRICT =0x0200
METHOD_ATTRIBUTE_ABSTRACT =0x0400
METHOD_ATTRIBUTE_SPECIAL_NAME =0x0800
METHOD_ATTRIBUTE_PINVOKE_IMPL =0x2000
METHOD_ATTRIBUTE_UNMANAGED_EXPORT =0x0008
MONO_TYPE_NAME_FORMAT_IL=0
MONO_TYPE_NAME_FORMAT_REFLECTION=1
MONO_TYPE_NAME_FORMAT_FULL_NAME=2
MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED=3
function mono_clearcache()
monocache={}
monocache.processid=getOpenedProcessID()
end
function monoTypeToVarType(monoType)
--MonoTypeEnum
local result=monoTypeToVartypeLookup[monoType]
if result==nil then
result=vtDword --just give it something
end
return result
end
function parseImage(t, image)
if image.parsed then return end
local classes=mono_image_enumClasses(image.handle)
if t.Terminated then return end
if classes then
--monoSymbolList.addSymbol('','Pen15',address,1)
local i
for i=1,#classes do
local classname=classes[i].classname
local namespace=classes[i].namespace
local methods=mono_class_enumMethods(classes[i].class)
if methods then
local j
for j=1,#methods do
local address=readPointer(methods[j].method) --first pointer is a pointer to the code
if address and address~=0 then
local sname=classname..'.'..methods[j].name
if namespace and namespace~='' then
sname=namespace..'.'..sname
end
monoSymbolList.addSymbol('',sname,address,1)
end
end
end
if t.Terminated then return end
end
end
end
function monoIL2CPPSymbolEnum(t)
t.freeOnTerminate(false)
t.Name='monoIL2CPPSymbolEnum'
-- print("monoIL2CPPSymbolEnum");
local priority=nil
--first enum all images
local images={}
local assemblies=mono_enumAssemblies()
monoSymbolList.IL2CPPSymbolEnumProgress=0
if assemblies then
for i=1,#assemblies do
images[i]={}
images[i].handle=mono_getImageFromAssembly(assemblies[i])
images[i].name=mono_image_get_name(images[i].handle)
images[i].parsed=false
if images[i].name=='Assembly-CSharp.dll' then
priority=i
end
end
end
if monopipe==nil or t.Terminated then return end
if priority then
parseImage(t, images[priority])
monoSymbolList.IL2CPPSymbolEnumProgress=(1/#assemblies) * 100
end
if monopipe==nil or t.Terminated then return end
for i=1,#images do
local x=i
if i~=priority then
parseImage(t, images[i])
end
if priority then
monoSymbolList.IL2CPPSymbolEnumProgress=((i-1)/#assemblies) * 100
else
monoSymbolList.IL2CPPSymbolEnumProgress=(i/#assemblies) * 100
end
if monopipe==nil or t.Terminated then return end
end
--print("all symbols loaded") --print is threadsafe
monoSymbolList.FullyLoaded=true
end
function mono_StructureListCallback()
local r={}
local ri=1;
if monopipe then
--return a list of all classes
--print("Getting classlist")
mono_enumImages(
function(image)
--enum classes
--print("Getting classes for ".. mono_image_get_name(image))
local classlist=mono_image_enumClasses(image)
if classlist then
local i
for i=1,#classlist do
r[ri]={}
r[ri].name=classlist[i].classname
r[ri].id1=classlist[i].class
ri=ri+1
end
end
end
)
end
return r
end
function mono_ElementListCallback(class) --2nd param ignored
local r={}
--print("Getting class fields for "..class..",",extra)
if monopipe~=nil then
--enumerate the fields in the class and return it
local fields=mono_class_enumFields(class, true)
if fields then
for i=1, #fields do
if fields[i].isStatic==false then
r[i]={}
r[i].name=fields[i].name
r[i].offset=fields[i].offset
r[i].vartype=monoTypeToVarType(fields[i].monotype)
end
end
end
end
return r
end
function fillMissingFunctions()
local result
outputDebugString('fillMissingFunctions')
local mono_type_get_name_full
if getOperatingSystem()==0 then --windows
waitForExports()
mono_type_get_name_full=getAddressSafe("mono_type_get_name_full") --it's defined as a symbol, but not public on some games
else
outputDebugString('get address of mono_type_get_name_full')
mono_type_get_name_full=getAddressSafe("mono_type_get_name_full")
end
local cmd=MONOCMD_FILLOPTIONALFUNCTIONLIST
monopipe.lock()
monopipe.writeByte(MONOCMD_FILLOPTIONALFUNCTIONLIST)
monopipe.writeQword(mono_type_get_name_full)
result=monopipe.readByte()
monopipe.unlock()
return result
end
local lastMonoError
function mono_connectionmode2()
monopipe.lock()
monopipe.writeByte(MONOCMD_LIMITEDCONNECTION)
monopipe.unlock()
end
function LaunchMonoDataCollector(internalReconnectDisconnectEachTime)
if debug_isBroken() then
if inMainThread() then
messageDialog(translate('You can not use this while the process is frozen'), mtError, mbOK)
end
return nil
end
--if debug_canBreak() then return 0 end
if monoSymbolEnum then
monoSymbolEnum.terminate()
monoSymbolEnum.waitfor()
--print("bye monoSymbolEnum")
monoSymbolEnum.destroy()
monoSymbolEnum=nil
end
if monoSymbolList then
--print("monoSymbolList exists");
if tonumber(monoSymbolList.ProcessID)~=getOpenedProcessID() or (monoSymbolList.FullyLoaded==false) then
--print("new il2cpp SymbolList")
monoSymbolList.destroy()
monoSymbolList=nil
end
end
if (monopipe~=nil) then
if (mono_AttachedProcess==getOpenedProcessID()) then
return monoBase --already attached to this process
end
monopipe.destroy()
monopipe=nil
end
if (monoeventpipe~=nil) then
monoeventpipe.destroy()
monoeventpipe=nil
end
local dllname
local dllpath
local skipsymbols=true
if isConnectedToCEServer() then
local basename='libMonoDataCollector'
if targetIsAndroid() then
dllname=basename
else
--assume linux
dllname=basename..'-linux'
end
if targetIsArm() then
if targetIs64Bit() then
dllname=dllname..'-aarch64.so'
else
dllname=dllname..'-arm.so'
end
else
if targetIs64Bit() then
dllname=dllname..'-x86_64.so'
else
dllname=dllname..'-i386.so'
end
end
dllpath=getCEServerPath()..dllname
else
if getOperatingSystem()==0 then
skipsymbols=false --for the alternative (can not create pipes) situation
dllname="MonoDataCollector"
if targetIs64Bit() then
dllname=dllname.."64.dll"
else
dllname=dllname.."32.dll"
end
autoAssemble([[
mono-2.0-bdwgc.mono_error_ok:
mov eax,1
ret
]]) --don't care if it fails
else
dllname='libMonoDataCollectorMac.dylib'
end
dllpath=getAutorunPath()..libfolder..pathsep..dllname
end
-- printf("Injecting %s\n", dllpath);
local injectResult, injectError=injectLibrary(dllpath, skipsymbols)
if (not injectResult) and isConnectedToCEServer() then --try the searchpath
outputDebugString('mdc lua: calling injectLibrary')
injectResult, injectError=injectLibrary(dllname, skipsymbols)
outputDebugString('mdc lua: after calling injectLibrary')
end
if (skipsymbols==false) and (getAddressSafe("MDC_ServerPipe")==nil) then
outputDebugString('mdc lua: calling waitForExports')
waitForExports()
if getAddressSafe("MDC_ServerPipe")==nil then
print("DLL Injection failed or invalid DLL version")
return 0
end
end
--wait till attached
local timeout=getTickCount()+5000
while (monopipe==nil) and (getTickCount()<timeout) do
if (skipsymbols==false) and (readInteger(getAddressSafe("MDC_ServerPipe"))==0xdeadbeef) then
--likely an UWP target which can not create a named pipe
--print("UWP situation")
local serverpipe=createPipe('cemonodc_pid'..getOpenedProcessID(), 256*1024,1024)
local newhandle=duplicateHandle(serverpipe.Handle)
serverpipe.destroy() --the old handle is not needed anymore
--print("New pipe handle is "..newhandle)
writeInteger(getAddressSafe("MDC_ServerPipe"), newhandle)
end
monopipe=connectToPipe('cemonodc_pid'..getOpenedProcessID() ,mono_timeout)
end
if (monopipe==nil) then
return 0 --failure
end
local v=mono_getMonoDatacollectorDLLVersion();
if (v==nil) or (v~=MONO_DATACOLLECTORVERSION) then
local s='There is an inconsistency with the monodatacollector dll and monoscript.lua . Unexpected behaviour and crashes are to be expected'
if inMainThread then
messageDialog(s, mtWarning)
else
print('Warning:'..s)
end
end
monopipe.OnError=function(self)
print("monopipe error")
monopipe.OnTimeout(self)
end
monopipe.OnTimeout=function(self)
print("monopipe disconnected")
local oldmonopipe=monopipe
monopipe=nil
mono_AttachedProcess=0
monoBase=0
if self then
oldmonopipe.unlock()
end
if inMainThread() and monoSymbolEnum then
monoSymbolEnum.terminate()
monoSymbolEnum.waitfor()
print("terminating monoSymbolEnum due to timeout or error")
monoSymbolEnum.destroy()
monoSymbolEnum=nil
end
oldmonopipe.destroy()
if StructureElementCallbackID then
unregisterStructureAndElementListCallback(StructureElementCallbackID)
StructureElementCallbackID=nil
end
if (lastMonoError==nil) or (getTickCount()>lastMonoError+200) then
--print("monopipe error. Reattaching in 500 ms")
--auto re-attach after error
--createTimer(200,function()
--print("Reattaching")
-- LaunchMonoDataCollector()
--end)
--else
-- print("monopipe error. Last reattach too soon. Giving up")
end
lastMonoError=getTickCount()
end
--in case you implement the profiling tools use a secondary pipe to receive profiler events
-- while (monoeventpipe==nil) do
-- monoeventpipe=connectToPipe('cemonodc_pid'..getOpenedProcessID()..'_events')
-- end
mono_AttachedProcess=getOpenedProcessID()
monopipe.writeByte(CMD_INITMONO)
monopipe.ProcessID=getOpenedProcessID()
monoBase=monopipe.readQword()
if (monoBase==nil) or (monoBase==0) then
print("Mono not usable in target")
return 0
end
if monoBase~=0 then
if mono_AddressLookupID==nil then
mono_AddressLookupID=registerAddressLookupCallback(mono_addressLookupCallback)
end
if mono_SymbolLookupID==nil then
mono_SymbolLookupID=registerSymbolLookupCallback(mono_symbolLookupCallback, slNotSymbol)
end
if mono_StructureNameLookupID==nil then
mono_StructureNameLookupID=registerStructureNameLookup(mono_structureNameLookupCallback)
end
if mono_StructureDissectOverrideID==nil then
mono_StructureDissectOverrideID=registerStructureDissectOverride(mono_structureDissectOverrideCallback)
end
end
StructureElementCallbackID=registerStructureAndElementListCallback(mono_StructureListCallback, mono_ElementListCallback)
monopipe.IL2CPP=mono_isil2cpp()
if monopipe.IL2CPP then
if monoSymbolList==nil then
monoSymbolList=createSymbolList()
monoSymbolList.register()
monoSymbolList.ProcessID=getOpenedProcessID()
monoSymbolList.FullyLoaded=false
monoSymbolList.IL2CPPSymbolEnumProgress=0
monoSymbolEnum=createThread(monoIL2CPPSymbolEnum)
createTimer(500,function()
--print("0.5 second delayed timer running now")
if monoSymbolList.FullyLoaded==false then
--show a progressbar in CE
if monoSymbolList.progressbar then
monoSymbolList.progressbar.destroy()
monoSymbolList.progressbar=nil
end
local pb=monoSymbolList.progressbar
pb=createProgressBar(MainForm.Panel4)
pb.Align=alBottom
pb.Max=100
local pmCancelEnum=createPopupMenu(pb)
local miCancelEnum=createMenuItem(pmCancelEnum)
miCancelEnum.Caption=translate('Cancel symbol enum')
pb.PopupMenu=pmCancelEnum
local pbl=createLabel(pb)
pbl.Caption=translate('IL2CPP symbol enum: 0%')
pbl.AnchorSideLeft.Control=pb
pbl.AnchorSideLeft.Side=asrCenter
pbl.AnchorSideTop.Control=pb
pbl.AnchorSideTop.Side=asrCenter
pb.Height=pbl.Height
monoSymbolList.progressbar=pb
local t=createTimer(pb)
t.enabled=true
t.interval=250
t.OnTimer=function()
--print("Check progress")
pb.Position=math.ceil(monoSymbolList.IL2CPPSymbolEnumProgress)
pbl.Caption=string.format("IL2CPP symbol enum: %.f%%",monoSymbolList.IL2CPPSymbolEnumProgress)
if monoSymbolList.FullyLoaded then
--print("done. Turning off check timer, and starting cleanup timer in 1.5 seconds")
t.enabled=false
pb.Position=100
pbl.Caption=string.format("IL2CPP symbol enum: Done"); --enum done. Now wait 1.5 seconds and then delete the bar
createTimer(1500,function()
--print("cleanup timer that runs after 1.5 seconds. destroying progressbar")
pb.destroy() --also destroys t
end)
end
end
end
end)
end
end
if getOperatingSystem()==1 then
--mac sometimes doesn't export mono_type_get_name_full but the symbol is defined. CE can help with this
fillMissingFunctions()
monopipe.AntiIdleThread=createTimer()
--in some games on the mac version the mainthread freezes when the thread is suspended/idle and not really sure why. fetching the domains resumes the game
if monopipe then
monopipe.AntiIdleThread.Interval=50
monopipe.AntiIdleThread.OnTimer=function(t)
mono_enumDomains()
end
else
t.destroy()
end
end
if internalReconnectDisconnectEachTime==nil then --old scripts don't give the parameter
if AddressList.LoadedTableVersion and AddressList.LoadedTableVersion<=40 then
internalReconnectDisconnectEachTime=false --old behaviour
else
internalReconnectDisconnectEachTime=true
end
end
if internalReconnectDisconnectEachTime then
mono_connectionmode2() --Change the behaviour from always connected to the mono runtime to only issuing a command, attach the handler thread to the mono runtime, and afterwards disconnect the thread from the mono runtime
end
mono_clearcache()
if miMonoTopMenuItem==nil then --launched mono with lua before it was detected
mono_setMonoMenuItem(true,false)
end
return monoBase
end
function mono_structureDissectOverrideCallback(structure, baseaddress)
-- print("oc")
if monopipe==nil then return nil end
local realaddress, classaddress=mono_object_findRealStartOfObject(baseaddress)
if (realaddress==baseaddress) then
local smap = {}
local s = monoform_exportStructInternal(structure, classaddress, true, false, smap, false)
return s~=nil
else
return nil
end
end
function mono_structureNameLookupCallback(address)
local currentaddress, classaddress, classname
if monopipe==nil then return nil end
local always=monoSettings.Value["AlwaysUseForDissect"]
local r
if (always==nil) or (always=="") then
r=messageDialog(translate("Do you wish to let the mono extention figure out the name and start address? If it's not a proper object this may crash the target."), mtConfirmation, mbYes, mbNo, mbYesToAll, mbNoToAll)
else
if (always=="1") then
r=mrYes
else
r=mrNo
end
end
if (r==mrYes) or (r==mbYesToAll) then
currentaddress, classaddress, classname=mono_object_findRealStartOfObject(address)
if (currentaddress~=nil) then
-- print("currentaddress~=nil : "..currentaddress)
return classname,currentaddress
else
-- print("currentaddress==nil")
return nil
end
end
--still alive, so the user made a good choice
if (r==mrYesToAll) then
monoSettings.Value["AlwaysUseForDissect"]="1"
elseif (r==mrNoToAll) then
monoSettings.Value["AlwaysUseForDissect"]="0"
end
end
function mono_splitSymbol(symbol)
local result=nil
local parts={}
local x
for x in string.gmatch(symbol, "[^:.]+") do
table.insert(parts, x)
end
local methodname=''
local classname=''
local namespace=''
if (#parts>0) then
methodname=(symbol:find("[:.]%.cc?tor$") ~= nil and '.' or '')..parts[#parts] --methodname=parts[#parts]
if (#parts>1) then
classname=parts[#parts-1]
if (#parts>2) then
for x=1,#parts-2 do
if x==1 then
namespace=parts[x]
else
namespace=namespace..'.'..parts[x]
end
end
end
end
end
--[[
if (methodname=='ctor' and symbol.endswith('.ctor')) or
(methodname=='cctor' and symbol.endswith('.cctor')) then
methodname='.'..methodname
end--]]
result={}
result.methodname=methodname
result.classname=classname
result.namespace=namespace
return result
end
function mono_symbolLookupCallback(symbol)
--if debug_canBreak() then return nil end
if monopipe == nil then return nil end
if monopipe.IL2CPP then return nil end
if symbol:match('[()%[%]]')~=nil then return nil end --no formulas/indexer
local methodname=''
local classname=''
local namespace=''
local ss=mono_splitSymbol(symbol)
methodname=ss.methodname
classname=ss.classname
namespace=ss.namespace
if (methodname~='') and (classname~='') then
local method=mono_findMethod(namespace, classname, methodname)
if (method==0) then
return nil
end
local methodaddress=mono_compile_method(method)
if (methodaddress~=0) then
return methodaddress
end
end
--still here,
return nil
end
function mono_addressLookupCallback(address)
--if (inMainThread()==false) or (debug_canBreak()) then --the debugger thread might call this
-- return nil
--end
if monopipe==nil then return nil end
if monopipe.IL2CPP then return nil end
if debug_isBroken() then return nil end
if tonumber(monopipe.ProcessID)~=getOpenedProcessID() then return nil end
local ji=mono_getJitInfo(address)
local result=''
if ji~=nil then
--[[
ji.jitinfo;
ji.method
ji.code_start
ji.code_size
--]]
if (ji.method~=0) then
local class=mono_method_getClass(ji.method)
if class==nil then return nil end
local classname=mono_class_getName(class)
local namespace=mono_class_getNamespace(class)
if (classname==nil) or (namespace==nil) then return nil end
if namespace~='' then
namespace=namespace..':'
end
if mono_class_getNestingType(class) then
result=mono_class_getFullName(class)..":"..mono_method_getName(ji.method)
else
result=namespace..classname..":"..mono_method_getName(ji.method)
end
if address~=ji.code_start then
result=result..string.format("+%x",address-ji.code_start)
end
end
end
return result
end
function mono_object_getClass(address)
--if debug_canBreak() then return nil end
if monopipe==nil then return nil end
monopipe.lock()
monopipe.writeByte(MONOCMD_OBJECT_GETCLASS)
monopipe.writeQword(address)
local classaddress=monopipe.readQword()
if (classaddress~=nil) and (classaddress~=0) then
if monopipe==nil then return nil end
local stringlength=monopipe.readWord()
local classname
if stringlength>0 then
classname=monopipe.readString(stringlength)
end
monopipe.unlock()
return classaddress, classname
else
if monopipe then
monopipe.unlock()
end
return nil
end
end
function mono_image_enumClassesEx(image)
--printf("mono_image_enumClassesEx(%.8x)", image)
local result=nil
if monopipe then
m=createMemoryStream()
m.writeByte(MONOCMD_ENUMCLASSESINIMAGEEX)
m.writeQword(image)
m.Position=0