This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EMXHookingLibrary.lua
1577 lines (1342 loc) · 72.6 KB
/
EMXHookingLibrary.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
BigNum = {RADIX = 10^7, RADIX_LEN = math.floor(math.log10(10^7)), mt = {}};
-- ************************************************************************************************************************************************************ --
-- Here starts the main hook lib code
-- ************************************************************************************************************************************************************ --
EMXHookLibrary = {
IsHistoryEdition = false,
OverriddenUpgradeCosts = false,
Internal = {
CurrentGameVariant = 0,
GlobalAdressEntity = 0,
GlobalHeapStart = 0,
GlobalOVOffset = 4128768,
AllocatedMemoryStart = 0,
AllocatedMemorySize = 0,
AllocatedMemoryMaxSize = 1000,
ASCIIStringCache = {},
InstanceCache = {},
ColorSetCache = {},
CurrentVersion = "2.0.8 - 22.12.2024 18:39 - Eisenmonoxid",
},
Helpers = {},
Bugfixes = {},
};
EMXHookLibrary.GameVariant = {
Original = 0,
OriginalWithOffset = 1,
HistoryEditionUbi = 2,
HistoryEditionSteam = 3,
};
EMXHookLibrary.RawPointer = {
__index = function(_rawPointer, _index)
local Object = EMXHookLibrary.RawPointer.New(_rawPointer.Pointer)
Object.Pointer = BigNum.mt.add(Object.Pointer, _index)
Object.Pointer = BigNum.new(EMXHookLibrary.Internal.GetValueAtPointer(Object))
return Object
end,
__newindex = function(_rawPointer, _index)
assert(false, "EMXHookLibrary: ERROR - RawPointer: __newindex not implemented!")
end,
__call = function(_rawPointer, _index, _value, _isFloat)
local Object = EMXHookLibrary.RawPointer.New(_rawPointer.Pointer)
Object.Pointer = BigNum.mt.add(Object.Pointer, _index)
local Value = (_isFloat and EMXHookLibrary.Helpers.Float2Int(_value)) or _value
EMXHookLibrary.Internal.SetValueAtPointer(Object, Value)
return _rawPointer
end,
__tostring = function(_rawPointer)
return BigNum.mt.tostring(_rawPointer.Pointer)
end,
__add = function(_rawPointer, _summand)
local Object = EMXHookLibrary.RawPointer.New(_rawPointer.Pointer)
Object.Pointer = BigNum.mt.add(Object.Pointer, _summand)
return Object
end,
__sub = function(_rawPointer, _subtrahend)
local Object = EMXHookLibrary.RawPointer.New(_rawPointer.Pointer)
Object.Pointer = BigNum.mt.sub(Object.Pointer, _subtrahend)
return Object
end,
__metatable = "Hidden",
New = function(_pointer)
local Object = {Pointer = BigNum.new(_pointer)};
setmetatable(Object, EMXHookLibrary.RawPointer)
return Object
end,
};
-- ************************************************************************************************************************************************************ --
-- **************************************************** -> These methods are exported into userspace <- -- **************************************************** --
-- ************************************************************************************************************************************************************ --
EMXHookLibrary.ModifyTerrainHeightWithoutTextureUpdate = function(_posX, _posY, _height)
local xPos = EMXHookLibrary.Internal.Convert2DPlanePositionToSingle(_posX)
local yPos = EMXHookLibrary.Internal.Convert2DPlanePositionToSingle(_posY)
yPos = yPos + 1
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "36", "4"}) or {"32", "44", "8"}
local Pointer = EMXHookLibrary.Internal.GetCGameLogic()["48"][Offsets[1]]
local CTerrainHiRes = Pointer[Offsets[2]]
CTerrainHiRes.Pointer = BigNum.mt.mul(CTerrainHiRes.Pointer, BigNum.new(yPos))
CTerrainHiRes = CTerrainHiRes + xPos
CTerrainHiRes.Pointer = BigNum.mt.mul(CTerrainHiRes.Pointer, BigNum.new("2"))
local TerrainHeightArray = Pointer[Offsets[3]]
CTerrainHiRes = CTerrainHiRes + tonumber(tostring(TerrainHeightArray))
CTerrainHiRes = CTerrainHiRes + 2
CTerrainHiRes("0", _height); -- This is a ushort (2 byte), but we can only write 4 byte, so the next index is also changed
end
EMXHookLibrary.SetAndReloadModelSpecificShader = function(_modelID, _shaderName)
-- E.G. "Object_Aligned_Additive", "ShipMovementEx", "WealthLightObject", "IceCliff", "Waterfall", "StaticBanner"
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"80", "4", "8"}) or {"92", "8", "12"}
local ModelArray = EMXHookLibrary.Internal.GetCDisplay()[Offsets[1]]["16"][Offsets[2]]
local ResourceManager = EMXHookLibrary.Internal.GetCGlobalsBaseEx()["124"][Offsets[3]]
local ModelEntry = ModelArray + (_modelID * 108)
local OriginalValue = tonumber(tostring(ModelEntry[0]))
local Pointer = EMXHookLibrary.Internal.CreatePureASCIITextInMemory(_shaderName)
ModelEntry(0, Pointer)
ResourceManager(_modelID * 4, 0) -- Yep, if the model was already loaded, then this is a memory leak (148 Byte) :(
return OriginalValue
end
EMXHookLibrary.ModifyModelPropertiesByReferenceType = function(_modelID, _referenceModelID, _entryIndex)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"80", "4", "8"}) or {"92", "8", "12"}
local ModelArray = EMXHookLibrary.Internal.GetCDisplay()[Offsets[1]]["16"][Offsets[2]]
local ResourceManager = EMXHookLibrary.Internal.GetCGlobalsBaseEx()["124"][Offsets[3]]
local ModelEntry = ModelArray + (_modelID * 108)
local ReferenceEntry = ModelArray + (_referenceModelID * 108)
local OriginalValue = tonumber(tostring(ModelEntry[_entryIndex]))
ModelEntry(_entryIndex, tonumber(tostring(ReferenceEntry[_entryIndex])))
ResourceManager(_modelID * 4, 0) -- Yep, if the model was already loaded, then this is a memory leak (148 Byte) :(
return OriginalValue
end
EMXHookLibrary.ChangeModelFilePath = function(_modelID, _filePath, _pathLength) -- _pathLength max 64 characters!
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"80", "16"}) or {"92", "20"}
local CModelsProps = EMXHookLibrary.Internal.GetCDisplay()[Offsets[1]]["16"]
local ModelEntry = CModelsProps[Offsets[2]]["12"]["0"]
local Pointer = EMXHookLibrary.Internal.CreatePureASCIITextInMemory(_filePath)
ModelEntry[_modelID * 4]("0", Pointer)("4", 64)("8", _pathLength)
end
EMXHookLibrary.SetEntityDisplayModelParameters = function(_entityIDOrType, _paramType, _params, _model)
local Mapping = {{"Models", 124, 116}, {"UpgradeSite", 140, 132}, {"Destroyed", 160, 152}, {"Lights", 180, 172}}
for Key, Value in pairs(Mapping) do
if Value[1] == _paramType then
EMXHookLibrary.Internal.ModifyEntityDisplay(_entityIDOrType, Value[2], Value[3], _params)
break;
end
end
if _model ~= nil then
EMXHookLibrary.Internal.ModifyEntityDisplay(_entityIDOrType, 8, 8, _model)
end
end
EMXHookLibrary.SetBuildingDisplayModelParameters = function(_entityIDOrType, _paramType, _params, _model)
local Mapping = {{"Yards", 124, 116}, {"Roofs", 136, 128}, {"RoofDestroyed", 148, 140}, {"UpgradeSite", 160, 152},
{"Floors", 204, 196}, {"Gables", 216, 108}, {"Lights", 324, 316}, {"FireCounts", 188, 180}}
for Key, Value in pairs(Mapping) do
if Value[1] == _paramType then
EMXHookLibrary.Internal.ModifyEntityDisplay(_entityIDOrType, Value[2], Value[3], _params)
break;
end
end
if _model ~= nil then
EMXHookLibrary.Internal.ModifyEntityDisplay(_entityIDOrType, 8, 8, _model)
end
end
EMXHookLibrary.SetEntityDisplayProperties = function(_entityIDOrType, _property, _value)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"84", "4"}) or {"88", "8"}
local Properties = {
{"ShowDestroyedModelAt", "220", "212"}, {"MaxDarknessFactor", "216", "208"}, {"ExplodeOnDestroyedModel", "224", "216"},
{"SnowFactor", "76", "72"}, {"SeasonColorSet", "68", "64"}, {"LODDistance", "80", "76"}, {"ConstructionSite", "104", "96"}, {"Decal", "108", "100"}};
local BitProperties = {{"HighQualityOnly", "39", "39"}, {"RenderInFow", "38", "38"}, {"CastShadow", "37", "37"}, {"DrawPlayerColor", "36", "36"}};
local Pointer = 0
if math.ceil(math.log10(_entityIDOrType)) <= 4 then
Pointer = EMXHookLibrary.Internal.GetCGlobalsLogicEx()["100"]["12"][Offsets[2]][_entityIDOrType * 4]
else
Pointer = EMXHookLibrary.Internal.CalculateEntityIDToDisplayObject(_entityIDOrType)[Offsets[1]]
end
for i = 1, #Properties do
if Properties[i][1] == _property then
Pointer((EMXHookLibrary.IsHistoryEdition and Properties[i][3]) or Properties[i][2], _value)
end
end
end
EMXHookLibrary.SetColorSetColorRGB = function(_colorSetName, _season, _rgb, _wetFactor)
local SeasonIndizes = {0, 16, 32, 48}
local OriginalValues = {0, 0, 0, 0, 0}
local Pointer = 0
local ColorSetEntryIndex = EMXHookLibrary.Internal.GetColorSetEntryIndexByName(_colorSetName)
if ColorSetEntryIndex == -1 then
return;
end
if EMXHookLibrary.Internal.ColorSetCache[ColorSetEntryIndex] == nil then
Pointer = EMXHookLibrary.Internal.FindColorSetEntryPointer(ColorSetEntryIndex)
if Pointer == nil then
return;
end
else
Pointer = EMXHookLibrary.Internal.ColorSetCache[ColorSetEntryIndex]
end
local CurrentIndex = SeasonIndizes[_season]
for i = 1, 4 do
OriginalValues[i] = EMXHookLibrary.Helpers.Int2Float(tonumber(tostring(Pointer[CurrentIndex])))
Pointer(CurrentIndex, _rgb[i], true)
CurrentIndex = CurrentIndex + 4
end
if _wetFactor then
OriginalValues[5] = EMXHookLibrary.Helpers.Int2Float(tonumber(tostring(Pointer["64"])))
Pointer("64", _wetFactor, true)
end
return OriginalValues
end
EMXHookLibrary.EditStringTableText = function(_IDManagerEntryIndex, _newString, _useAlternativePointer)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"20", 24, 0}) or {"24", 28, 4}
local Index = _IDManagerEntryIndex * Offsets[2]
local WideCharAsMultiByte = EMXHookLibrary.Helpers.ConvertWideCharToMultiByte(_newString)
local TextSegment = EMXHookLibrary.Internal.GetCTextSet()["4"][Offsets[1]]
if not _useAlternativePointer then
for i = 1, #WideCharAsMultiByte do TextSegment(Index + Offsets[3], WideCharAsMultiByte[i]) Offsets[3] = Offsets[3] + 4 end
else
TextSegment = TextSegment[Index + Offsets[3]]
local Iterator = 0
for i = 1, #WideCharAsMultiByte do TextSegment(Iterator, WideCharAsMultiByte[i]) Iterator = Iterator + 4 end
end
end
EMXHookLibrary.SetPlayerColorRGB = function(_playerColorEntryIndex, _rgb)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"328", "172"}) or {"332", "176"}
local Index = _playerColorEntryIndex * 4
local ColorStringHex = "";
for i = 1, #_rgb, 1 do
ColorStringHex = ColorStringHex .. string.format("%0x", _rgb[i]);
end
ColorStringHex = tonumber("0x" .. ColorStringHex)
local Pointer = EMXHookLibrary.Internal.GetCGlobalsBaseEx()
Pointer["108"](Index, ColorStringHex)[Offsets[1]](Index, ColorStringHex)
Pointer["20"][Offsets[2]](Index, ColorStringHex)
Logic.ExecuteInLuaLocalState([[
Display.UpdatePlayerColors();
GUI.RebuildMinimapTerrain();
GUI.RebuildMinimapTerritory();
]]);
end
EMXHookLibrary.ToggleDEBUGMode = function(_magicWord, _setNewMagicWord)
local Text = "EMXHookLibrary: Debug Word value is: ";
if not EMXHookLibrary.IsHistoryEdition then
local Value = 11190056;
local Pointer = (EMXHookLibrary.Internal.CurrentGameVariant == EMXHookLibrary.GameVariant.OriginalWithOffset and (Value - EMXHookLibrary.Internal.GlobalOVOffset)) or Value;
local Word = EMXHookLibrary.Internal.GetValueAtPointer(EMXHookLibrary.RawPointer.New(Pointer))
Logic.DEBUG_AddNote(Text .. Word)
Framework.WriteToLog(Text .. Word)
if _setNewMagicWord ~= nil then
EMXHookLibrary.Internal.SetValueAtPointer(EMXHookLibrary.RawPointer.New(Pointer), _magicWord)
end
return Word;
end
if EMXHookLibrary.Internal.CurrentGameVariant ~= EMXHookLibrary.GameVariant.HistoryEditionSteam then
local Error = "EMXHookLibrary: ERROR -> Can't set Debug mode in Ubisoft-HE, use the S6Patcher for that!"
Framework.WriteToLog(Error)
assert(false, Error)
return "";
end
local Pointer = EMXHookLibrary.RawPointer.New(Logic.GetEntityScriptingValue(EMXHookLibrary.Internal.GlobalAdressEntity, -78))["0"]
Pointer = (Pointer - "2100263")["0"]
local Word = tostring(Pointer["0"])
Logic.DEBUG_AddNote(Text .. Word)
Framework.WriteToLog(Text .. Word)
if _setNewMagicWord ~= nil then Pointer("0", _magicWord) end
return Word;
end
EMXHookLibrary.EditFestivalProperties = function(_festivalDuration, _promotionDuration, _promotionParticipantLimit, _festivalParticipantLimit)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"496", "8", "144", "52", "176", "84"}) or {"504", "12", "188", "72", "224", "108"}
local Pointer = EMXHookLibrary.Internal.GetCMain()[Offsets[1]]["44"][Offsets[2]]
if _promotionParticipantLimit ~= nil then
for i = 0, 12, 4 do Pointer[Offsets[3]](i, _promotionParticipantLimit) end
end
if _festivalParticipantLimit ~= nil then
for i = 0, 12, 4 do Pointer[Offsets[4]](i, _festivalParticipantLimit) end
end
if _promotionDuration ~= nil then Pointer(Offsets[5], _promotionDuration) end
if _festivalDuration ~= nil then Pointer(Offsets[6], _festivalDuration) end
end
EMXHookLibrary.SetBuildingTypeOutStockGood = function(_buildingID, _newGood, _setEntityTypeProduct)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"352", "20", "20", "560", "16"}) or {"368", "16", "24", "612", "12"}
local SharedIdentifier = BigNum.new("-1035359747")
local Good = Logic.GetGoodTypeOnOutStockByIndex(_buildingID, 0) -- If behavior does not exist, create it
if _setEntityTypeProduct ~= nil then EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_buildingID)["128"](Offsets[4], _newGood) end
local Pointer = EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_buildingID)[Offsets[1]]["4"]
local CurrentIdentifier = Pointer[Offsets[5]].Pointer
while BigNum.compareAbs(CurrentIdentifier, SharedIdentifier) ~= 0 do
Pointer = Pointer["0"]
CurrentIdentifier = Pointer[Offsets[5]].Pointer
end
Pointer[Offsets[2]][Offsets[3]]("0", _newGood)
if Logic.GetGoodTypeOnOutStockByIndex(_buildingID, 0) ~= _newGood then
assert(false, "EMXHookLibrary: ERROR setting the building OutStock good!")
end
end
EMXHookLibrary.CreateBuildingInStockGoods = function(_buildingID, _newGoods)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"352", "20", 20, "16"}) or {"368", "16", 24, "12"}
local SharedIdentifier = BigNum.new("1501117341")
local Pointer = EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_buildingID)[Offsets[1]]["4"]
local CurrentIdentifier = Pointer[Offsets[4]].Pointer
while BigNum.compareAbs(SharedIdentifier, CurrentIdentifier) ~= 0 do
Pointer = Pointer["8"]
CurrentIdentifier = Pointer[Offsets[4]].Pointer
end
assert(type(_newGoods) == "table")
local NeededMemorySize = (#_newGoods * 2) * 4
local MemoryPointer = EMXHookLibrary.Internal.MemoryAllocator(NeededMemorySize)
if MemoryPointer == nil then
return;
end
local Counter = 0
for Key, Value in pairs(_newGoods) do
MemoryPointer(Counter, Value)(Counter + 4, 0)
Counter = Counter + 8
end
local StartPointer = tonumber(tostring(MemoryPointer))
local EndPointer = tonumber(tostring(MemoryPointer + Counter))
local OriginalValues = {tostring(Pointer[Offsets[2]][Offsets[3]]),
tostring(Pointer[Offsets[2]][Offsets[3] + 4]),
tostring(Pointer[Offsets[2]][Offsets[3] + 8])};
Pointer[Offsets[2]](Offsets[3], StartPointer)(Offsets[3] + 4, EndPointer)(Offsets[3] + 8, EndPointer)
return OriginalValues
end
EMXHookLibrary.SetBuildingInStockGood = function(_buildingID, _newGood)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"352", "20", "20", "16"}) or {"368", "16", "24", "12"}
local SharedIdentifier = BigNum.new("1501117341")
local Good = Logic.GetGoodTypeOnInStockByIndex(_buildingID, 0) -- If behavior does not exist, create it
local Pointer = EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_buildingID)[Offsets[1]]["4"]
local CurrentIdentifier = Pointer[Offsets[4]].Pointer
while BigNum.compareAbs(SharedIdentifier, CurrentIdentifier) ~= 0 do
Pointer = Pointer["8"]
CurrentIdentifier = Pointer[Offsets[4]].Pointer
end
Pointer[Offsets[2]][Offsets[3]]("0", _newGood)
if Logic.GetGoodTypeOnInStockByIndex(_buildingID, 0) ~= _newGood then
assert(false, "EMXHookLibrary: ERROR setting the building InStock good!")
end
end
EMXHookLibrary.SetMaxBuildingStockSize = function(_buildingID, _maxStockSize)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"352", "20", "44", "16"}) or {"368", "16", "52", "12"}
local SharedIdentifier = BigNum.new("-1035359747")
local Stock = Logic.GetMaxAmountOnStock(_buildingID) -- If behavior does not exist, create it
local Pointer = EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_buildingID)[Offsets[1]]["4"]
local CurrentIdentifier = Pointer[Offsets[4]].Pointer
while BigNum.compareAbs(SharedIdentifier, CurrentIdentifier) ~= 0 do
Pointer = Pointer["0"]
CurrentIdentifier = Pointer[Offsets[4]].Pointer
end
Pointer[Offsets[2]](Offsets[3], _maxStockSize)
if Logic.GetMaxAmountOnStock(_buildingID) ~= _maxStockSize then
assert(false, "EMXHookLibrary: ERROR setting the building stock limit!")
end
end
EMXHookLibrary.SetMaxStorehouseStockSize = function(_storehouseID, _maxStockSize)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"352", "20", "68", "16"}) or {"368", "16", "76", "12"}
local SharedIdentifier = BigNum.new("625443837")
local Stock = Logic.GetMaxAmountOnStock(_storehouseID) -- If behavior does not exist, create it
local Pointer = EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_storehouseID)[Offsets[1]]["4"]
local CurrentIdentifier = Pointer[Offsets[4]].Pointer
while BigNum.compareAbs(SharedIdentifier, CurrentIdentifier) ~= 0 do
Pointer = Pointer["8"]
CurrentIdentifier = Pointer[Offsets[4]].Pointer
end
Pointer[Offsets[2]](Offsets[3], _maxStockSize)
if Logic.GetMaxAmountOnStock(_storehouseID) ~= _maxStockSize then
assert(false, "EMXHookLibrary: ERROR setting the storehouse stock limit!")
end
end
EMXHookLibrary.SetGoodTypeParameters = function(_goodType, _requiredResource, _amount, _goodCategory, _animationParameters)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"4", "36"}) or {"8", "40"}
local Pointer = EMXHookLibrary.Internal.GetCGoodProps()[Offsets[1]][_goodType * 4]
if _requiredResource ~= nil then Pointer[Offsets[2]]("0", _requiredResource) end
if _amount ~= nil then Pointer[Offsets[2]]("4", _amount) end
if _goodCategory ~= nil then Pointer("4", _goodCategory) end
if _animationParameters ~= nil then Pointer("8", _animationParameters[1])("12", _animationParameters[2]) end
end
EMXHookLibrary.CreateGoodTypeRequiredResources = function(_goodType, _requiredResources)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"4", "36", "40", "44"}) or {"8", "40", "44", "48"}
local GoodPointer = EMXHookLibrary.Internal.GetCGoodProps()[Offsets[1]][_goodType * 4]
local OriginalValues = {0, 0, 0}
assert(type(_requiredResources) == "table")
local NeededMemorySize = (#_requiredResources * 3) * 4
local Pointer = EMXHookLibrary.Internal.MemoryAllocator(NeededMemorySize)
if Pointer == nil then
return;
end
local Counter = 0
for Key, Value in pairs(_requiredResources) do
Pointer(Counter, Value[1])(Counter + 4, Value[2])(Counter + 8, Value[3])
Counter = Counter + 12
end
local StartPointer = tonumber(tostring(Pointer))
local EndPointer = tonumber(tostring(Pointer + Counter))
for i = 2, 4, 1 do
OriginalValues[i - 1] = tonumber(tostring(GoodPointer[Offsets[i]]))
end
GoodPointer(Offsets[2], StartPointer)
GoodPointer(Offsets[3], EndPointer)
GoodPointer(Offsets[4], EndPointer)
return OriginalValues
end
EMXHookLibrary.CopyGoodTypePointer = function(_good, _copyGood)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"4", "36", "40", "44"}) or {"8", "40", "44", "48"}
local CopyGoodPointer = EMXHookLibrary.Internal.GetCGoodProps()[Offsets[1]][_copyGood * 4]
local GoodPointer = EMXHookLibrary.Internal.GetCGoodProps()[Offsets[1]][_good * 4]
local OriginalValues = {0, 0, 0}
for i = 2, 4, 1 do
OriginalValues[i - 1] = tonumber(tostring(GoodPointer[Offsets[i]]))
GoodPointer(Offsets[i], tonumber(tostring(CopyGoodPointer[Offsets[i]])))
end
return OriginalValues
end
EMXHookLibrary.ReplaceUpgradeCategoryEntityType = function(_upgradeCategory, _newEntityType)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"364", "40", "552", "12", "4", "16", "24", "0"}) or {"412", "40", "648", "16", "4", "12", "20", "4"}
local Pointer = (EMXHookLibrary.Internal.GetCGameLogic()[Offsets[1]][Offsets[2]][Offsets[3]] + Offsets[4])[Offsets[8]][Offsets[5]]
local SharedIdentifier = BigNum.new(_upgradeCategory)
local CurrentIdentifier = Pointer[Offsets[6]].Pointer
while BigNum.compareAbs(SharedIdentifier, CurrentIdentifier) ~= 0 do
if tonumber(tostring(CurrentIdentifier)) < _upgradeCategory then
Pointer = Pointer["8"]
else
Pointer = Pointer["0"]
end
CurrentIdentifier = Pointer[Offsets[6]].Pointer
end
Pointer(Offsets[7], _newEntityType)
end
EMXHookLibrary.AddBehaviorToEntityType = function(_entityType, _behaviorName)
local Mapping = {{"CInteractiveObjectBehavior", Entities.I_X_Well_Destroyed, "-313534907"}, {"CMountableBehavior", Entities.B_Outpost_ME, "-504538299"},
{"CFarmAnimalBehavior", Entities.A_X_Sheep01, "1234712196"}, {"CAnimalMovementBehavior", Entities.A_X_Sheep01, "-870260891"},
{"CAmmunitionFillerBehavior", Entities.B_Outpost_ME, "1689179045"}};
for Key, Value in pairs(Mapping) do
if Value[1] == _behaviorName then
local Index = EMXHookLibrary.Internal.GetObjectBehaviorIndexByID(Value[2], Value[3]);
if Index ~= -1 then
return EMXHookLibrary.Internal.ModifyEntityBehaviors(_entityType, Value[2], Index);
end
end
end
end
EMXHookLibrary.SetSettlerIllnessCount = function(_newCount) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_newCount, "760", "700") end
EMXHookLibrary.SetCarnivoreHealingSeconds = function(_newTime) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_newTime, "680", "624") end
EMXHookLibrary.SetKnightResurrectionTime = function(_newTime) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_newTime, "184", "164") end
EMXHookLibrary.SetMaxBuildingTaxAmount = function(_newTaxAmount) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_newTaxAmount, "624", "580") end
EMXHookLibrary.SetAmountOfTaxCollectors = function(_newAmount) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_newAmount, "808", "744") end
EMXHookLibrary.SetFogOfWarVisibilityFactor = function(_newFactor) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(EMXHookLibrary.Helpers.Float2Int(_newFactor), "620", "576") end
EMXHookLibrary.SetBuildingKnockDownCompensation = function(_percent) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_percent, "4", "4") end
-- These three get set correctly but don't seem to do anything ingame. Might need further testing however.
--EMXHookLibrary.SetTrailSpeedModifier = function(_newFactor) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(EMXHookLibrary.Helpers.Float2Int(_newFactor), "496", "464") end
--EMXHookLibrary.SetRoadSpeedModifier = function(_newFactor) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(EMXHookLibrary.Helpers.Float2Int(_newFactor), "320", "300") end
--EMXHookLibrary.SetWaterDepthBlockingThreshold = function(_threshold) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_threshold, "456", "424") end
EMXHookLibrary.SetTerritoryCombatBonus = function(_newFactor) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(EMXHookLibrary.Helpers.Float2Int(_newFactor), "604", "560") end
EMXHookLibrary.SetCathedralCollectAmount = function(_newAmount) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_newAmount, "436", "404") end
EMXHookLibrary.SetFireHealthDecreasePerSecond = function(_newAmount) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_newAmount, "260", "240") end
EMXHookLibrary.SetWealthGoodDecayPerSecond = function(_decay) EMXHookLibrary.Internal.ModifyLogicPropertiesEx(_decay, "492", "460") end
EMXHookLibrary.GetModel = function(_entityID) return tostring(EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_entityID)["28"]) end
EMXHookLibrary.SetTerritoryGoldCostByIndex = function(_arrayIndex, _price)
local Offset = (EMXHookLibrary.IsHistoryEdition and 628) or 684
EMXHookLibrary.Internal.GetLogicPropertiesEx()((_arrayIndex * 4) + Offset, _price)
end
EMXHookLibrary.SetSettlerLimit = function(_cathedralIndex, _limit)
local Offset = (EMXHookLibrary.IsHistoryEdition and "376") or "408"
EMXHookLibrary.Internal.GetLogicPropertiesEx()[Offset](_cathedralIndex * 4, _limit)
end
EMXHookLibrary.SetSermonSettlerLimit = function(_cathedralEntityType, _upgradeLevel, _newLimit)
EMXHookLibrary.Internal.SetLimitByEntityType(_cathedralEntityType, _upgradeLevel, _newLimit, {"756", "680"})
end
EMXHookLibrary.SetSoldierLimit = function(_castleEntityType, _upgradeLevel, _newLimit)
EMXHookLibrary.Internal.SetLimitByEntityType(_castleEntityType, _upgradeLevel, _newLimit, {"788", "704"})
end
EMXHookLibrary.SetEntityTypeOutStockCapacity = function(_entityType, _upgradeLevel, _newLimit)
EMXHookLibrary.Internal.SetLimitByEntityType(_entityType, _upgradeLevel, _newLimit, {"676", "612"})
end
EMXHookLibrary.SetEntityTypeSpouseProbabilityFactor = function(_entityType, _factor)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "596"}) or {"28", "648"}
EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4](Offsets[2], _factor, true)
end
EMXHookLibrary.SetTypeAndMaxNumberOfWorkersForBuilding = function(_entityType, _maxWorkers, _workerType)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "256", "260"}) or {"28", "288", "292"}
EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4](Offsets[2], _maxWorkers)(Offsets[3], _workerType)
end
EMXHookLibrary.SetSettlersWorkBuilding = function(_settlerID, _buildingID)
EMXHookLibrary.Internal.CalculateEntityIDToLogicObject(_settlerID)["84"]["0"]("0", _buildingID)
end
EMXHookLibrary.SetEntityTypeMinimapIcon = function(_entityType, _iconIndex)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "88"}) or {"28", "92"}
EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4](Offsets[2], _iconIndex)
end
EMXHookLibrary.SetEntityTypeMaxHealth = function(_entityType, _newMaxHealth)
local Offset = (EMXHookLibrary.IsHistoryEdition and "24") or "28"
EMXHookLibrary.Internal.GetCEntityProps()[Offset][_entityType * 4]("36", _newMaxHealth)
end
EMXHookLibrary.SetBallistaAmmunitionAmount = function(_amount)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "152"}) or {"28", "164"}
EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][Entities.U_MilitaryBallista * 4][Offsets[2]]["8"]("20", _amount)
end
-- _costs = {_good, _amount, _secondGood, _secondAmount}
EMXHookLibrary.SetEntityTypeFullCost = function(_entityType, _costs, _overrideSecondGoodPointer)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "136", "140", "144"}) or {"28", "144", "148", "152"}
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4]
local ValuePointer = Pointer[Offsets[2]]
local OriginalValues = {0, 0, 0}
assert(type(_costs) == "table" and #_costs >= 2, "Error: Invalid Costtable!")
for i = 2, 4, 1 do
OriginalValues[i - 1] = tonumber(tostring(Pointer[Offsets[i]]))
end
if tonumber(tostring(ValuePointer)) == 0 then
local Size = (_costs[3] ~= nil and 16) or 8
local MemoryPointer = EMXHookLibrary.Internal.MemoryAllocator(Size)
MemoryPointer("0", _costs[1])("4", _costs[2])
if _costs[3] ~= nil then
MemoryPointer("8", _costs[3])("12", _costs[4])
end
Pointer(Offsets[2], tonumber(tostring(MemoryPointer)))(Offsets[3], tonumber(tostring(MemoryPointer + Size)))(Offsets[4], tonumber(tostring(MemoryPointer + Size)))
else
ValuePointer("0", _costs[1])("4", _costs[2])
if _costs[3] ~= nil then
ValuePointer("8", _costs[3])("12", _costs[4])
if _overrideSecondGoodPointer then
local EndPointer = Pointer[Offsets[3]]
Pointer(Offsets[3], tonumber(tostring(EndPointer + 8)))(Offsets[4], tonumber(tostring(EndPointer + 8)))
end
end
end
return OriginalValues
end
-- _costs = {_good, _amount, _secondGood, _secondAmount}
EMXHookLibrary.SetEntityTypeUpgradeCost = function(_entityType, _upgradeLevel, _costs, _overrideSecondGoodPointer, _overrideUpgradeCostHandling)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "600", 0, 12}) or {"28", "660", 4, 16}
local UpgradeLevelOffset = Offsets[3] + (_upgradeLevel * Offsets[4])
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4][Offsets[2]]
local ValuePointer = Pointer[UpgradeLevelOffset]
local OriginalValues = {0, 0}
assert(type(_costs) == "table" and #_costs >= 2, "Error: Invalid Costtable!")
for i = 4, 8, 4 do
OriginalValues[i / 4] = tonumber(tostring(Pointer[UpgradeLevelOffset + i]))
end
ValuePointer("0", _costs[1])("4", _costs[2])
if _costs[3] ~= nil then
ValuePointer("8", _costs[3])("12", _costs[4])
if _overrideSecondGoodPointer then
local EndPointer = Pointer[UpgradeLevelOffset + 4]
Pointer(UpgradeLevelOffset + 4, tonumber(tostring(EndPointer + 8)))(UpgradeLevelOffset + 8, tonumber(tostring(EndPointer + 8)))
end
end
if not EMXHookLibrary.OverriddenUpgradeCosts and _overrideUpgradeCostHandling then
Logic.ExecuteInLuaLocalState([[
function GUI_BuildingButtons.GetUpgradeCosts()
local EntityID = GUI.GetSelectedEntity()
local Costs = {}
local CurrentGoodCost = 0
for Key, Value in pairs(Goods) do
CurrentGoodCost = Logic.GetBuildingUpgradeCostByGoodType(EntityID, Value, 0)
if CurrentGoodCost ~= 0 then
table.insert(Costs, Value)
table.insert(Costs, CurrentGoodCost)
end
if #Costs >= 4 then
break;
end
end
return Costs
end
]]);
EMXHookLibrary.OverriddenUpgradeCosts = true
end
return OriginalValues
end
EMXHookLibrary.SetEntityTypeBlocking = function(_entityType, _blocking, _isBuildBlocking)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "168", "240"}) or {"28", "184", "276"}
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4]
Pointer = Pointer[(_isBuildBlocking and Offsets[3]) or Offsets[2]]
local Iterator = 1
for i = 0, (#_blocking * 3), 4 do
Pointer(i, _blocking[Iterator], true)
Iterator = Iterator + 1
end
end
-- _distances = {_rowDistance, _colDistance, _cartRowDistance, _cartColDistance, _engineRowDistance, _engineColDistance}
EMXHookLibrary.SetMilitaryMetaFormationParameters = function(_distances)
assert(type(_distances) == "table")
local Offset = (EMXHookLibrary.IsHistoryEdition and 652) or 708
local Counter = 0
for Key, Value in pairs(_distances) do
if Value ~= nil then
EMXHookLibrary.Internal.GetLogicPropertiesEx()(Offset + Counter, Value, true)
end
Counter = Counter + 4
end
end
EMXHookLibrary.SetTerritoryAcquiringBuildingID = function(_territoryID, _buildingID)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"332", "4", 168}) or {"372", "8", 180}
local Pointer = EMXHookLibrary.Internal.GetCGameLogic()[Offsets[1]][Offsets[2]]
Pointer = Pointer + (_territoryID * Offsets[3])
Pointer("24", _buildingID)
end
EMXHookLibrary.SetEGLEffectDuration = function(_effect, _duration)
local Offset = (EMXHookLibrary.IsHistoryEdition and "8") or "12"
EMXHookLibrary.Internal.GetCEffectProps()[Offset][_effect * 4]("16", _duration, true)
end
EMXHookLibrary.ToggleRTSCameraMouseRotation = function(_enableMouseRotation, _optionalRotationSpeed)
local Speed = _optionalRotationSpeed or 2500
EMXHookLibrary.Internal.GetCCameraBehaviorRTS()("40", (_enableMouseRotation and Speed) or 0, true)
end
-- ************************************************************************************************************************************************************ --
-- Internal library functions
-- ************************************************************************************************************************************************************ --
EMXHookLibrary.Internal.ModifyEntityDisplay = function(_entityIDOrType, _vanillaOffset, _heOffset, _params)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"84", "4"}) or {"88", "8"}
local Pointer = 0
if math.ceil(math.log10(_entityIDOrType)) <= 4 then
Pointer = EMXHookLibrary.Internal.GetCGlobalsLogicEx()["100"]["12"][Offsets[2]][_entityIDOrType * 4]
else
Pointer = EMXHookLibrary.Internal.CalculateEntityIDToDisplayObject(_entityIDOrType)[Offsets[1]]
end
local StartOffset = (EMXHookLibrary.IsHistoryEdition and _heOffset) or _vanillaOffset
for i = 1, #_params do
if _params[i] ~= nil then
Pointer(StartOffset, _params[i])
end
StartOffset = StartOffset + 4
end
end
EMXHookLibrary.Internal.FindColorSetEntryPointer = function(_colorSetEntryIndex)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"0", "16", "20"}) or {"4", "12", "16"}
local OriginalPointer = EMXHookLibrary.Internal.GetCGlobalsBaseEx()["128"][Offsets[1]]
local Pointer, CurrentEntry = 0, 0;
for i = 0, 8, 4 do
Pointer = OriginalPointer[i]
repeat
CurrentEntry = tonumber(tostring(Pointer[Offsets[2]]))
if CurrentEntry == _colorSetEntryIndex then
Pointer = Pointer[Offsets[3]]
EMXHookLibrary.Internal.ColorSetCache[_colorSetEntryIndex] = Pointer
return Pointer;
end
if CurrentEntry < _colorSetEntryIndex then
Pointer = Pointer["8"]
else
Pointer = Pointer["0"]
end
until tonumber(tostring(Pointer["0"])) == tonumber(tostring(Pointer["8"]))
end
Framework.WriteToLog("EMXHookLibrary: No ColorSet entry found for index ".._colorSetEntryIndex.."! Aborting ...")
return;
end
EMXHookLibrary.Internal.GetColorSetEntryIndexByName = function(_colorSetName)
local Input = string.lower(_colorSetName)
local Pointer = EMXHookLibrary.Internal.GetColorSetIDManager()["12"]["0"]
local Counter = 0
while true do
if tonumber(tostring(Pointer[Counter * 4])) == 0 then
break;
end
local String = EMXHookLibrary.Internal.GetLuaASCIIStringFromPointer(Pointer[Counter * 4]["0"])
if string.find(String, Input) then
return Counter;
end
Counter = Counter + 1
end
return -1;
end
EMXHookLibrary.Internal.ModifyEntityBehaviors = function(_entityTypeToAdd, _entityTypeToReference, _behaviorIndex)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", 152}) or {"28", 164}
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityTypeToAdd * 4]
local AmountOfBehaviors = tonumber(tostring(Pointer[Offsets[2] + 12]))
local OriginalPointers = {0, 0, 0, 0}
local MemorySize = (AmountOfBehaviors + 1) * 4
local MemoryPointer = EMXHookLibrary.Internal.MemoryAllocator(MemorySize)
if MemoryPointer == nil then
return;
end
local CurrentValue = 0
for i = 0, AmountOfBehaviors - 1 do
CurrentValue = tonumber(tostring(Pointer[Offsets[2]][i * 4]))
MemoryPointer(i * 4, CurrentValue)
end
local ReferenceEntity = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityTypeToReference * 4]
MemoryPointer(MemorySize - 4, tonumber(tostring(ReferenceEntity[Offsets[2]][_behaviorIndex])))
for i = 0, 3, 1 do
OriginalPointers[i + 1] = tonumber(tostring(Pointer[Offsets[2] + (i * 4)]))
end
local EndPointer = tonumber(tostring(MemoryPointer + MemorySize))
Pointer(Offsets[2], tonumber(tostring(MemoryPointer)))(Offsets[2] + 4, EndPointer)(Offsets[2] + 8, EndPointer)(Offsets[2] + 12, AmountOfBehaviors + 1)
return OriginalPointers
end
EMXHookLibrary.Internal.GetObjectBehaviorIndexByID = function(_entityType, _behaviorID)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", 152}) or {"28", 164}
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4]
local AmountOfBehaviors = tonumber(tostring(Pointer[Offsets[2] + 12]))
Pointer = Pointer[Offsets[2]]
local SharedIdentifier = BigNum.new(_behaviorID);
local CurrentIdentifier = 0;
for i = 0, AmountOfBehaviors - 1, 1 do
CurrentIdentifier = Pointer[i * 4]["12"].Pointer
if BigNum.compareAbs(SharedIdentifier, CurrentIdentifier) == 0 then
return i * 4;
end
end
return -1;
end
EMXHookLibrary.Internal.ModifyLogicPropertiesEx = function(_value, _ovOffset, _heOffset)
local Offset = (EMXHookLibrary.IsHistoryEdition and _heOffset) or _ovOffset
EMXHookLibrary.Internal.GetLogicPropertiesEx()(Offset, _value);
end
EMXHookLibrary.Internal.SetLimitByEntityType = function(_entityType, _upgradeLevel, _newLimit, _pointerValues)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", _pointerValues[2]}) or {"28", _pointerValues[1]}
EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4][Offsets[2]](_upgradeLevel * 4, _newLimit)
end
EMXHookLibrary.Internal.Convert2DPlanePositionToSingle = function(_position)
return math.ceil(math.floor(((_position * 0.01) + (_position * 0.01)) + 0.5) * 0.5);
end
-- ************************************************************************************************************************************************************ --
-- Reset Functions
-- ************************************************************************************************************************************************************ --
EMXHookLibrary.ResetModelProperties = function(_modelID, _entryIndex, _resetValue)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"80", "4", "8"}) or {"92", "8", "12"}
local ModelArray = EMXHookLibrary.Internal.GetCDisplay()[Offsets[1]]["16"][Offsets[2]]
local ResourceManager = EMXHookLibrary.Internal.GetCGlobalsBaseEx()["124"][Offsets[3]]
local ModelEntry = ModelArray + (_modelID * 108)
ModelEntry(_entryIndex, _resetValue)
ResourceManager(_modelID * 4, 0)
end
EMXHookLibrary.ResetGoodTypePointer = function(_goodType, _resetPointers)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"4", "36", "40", "44"}) or {"8", "40", "44", "48"}
local GoodPointer = EMXHookLibrary.Internal.GetCGoodProps()[Offsets[1]][_goodType * 4]
for i = 2, 4, 1 do
GoodPointer(Offsets[i], _resetPointers[i - 1])
end
end
EMXHookLibrary.ResetEntityBehaviors = function(_entityType, _resetPointers)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", 152}) or {"28", 164}
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4]
for i = 0, 3, 1 do
Pointer(Offsets[2] + (i * 4), _resetPointers[i + 1])
end
end
EMXHookLibrary.ResetEntityTypeFullCost = function(_entityType, _resetPointers)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "136", "140", "144"}) or {"28", "144", "148", "152"}
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4]
for i = 2, 4, 1 do
Pointer(Offsets[i], _resetPointers[i - 1])
end
end
EMXHookLibrary.ResetEntityTypeUpgradeCost = function(_entityType, _upgradeLevel, _resetPointers)
local Offsets = (EMXHookLibrary.IsHistoryEdition and {"24", "600", 0, 12}) or {"28", "660", 4, 16}
local UpgradeLevelOffset = Offsets[3] + (_upgradeLevel * Offsets[4])
local Pointer = EMXHookLibrary.Internal.GetCEntityProps()[Offsets[1]][_entityType * 4][Offsets[2]]
Pointer(UpgradeLevelOffset + 4, _resetPointers[1])(UpgradeLevelOffset + 8, _resetPointers[2])
end
-- ************************************************************************************************************************************************************ --
-- Hooking Utility Methods
-- ************************************************************************************************************************************************************ --
EMXHookLibrary.Internal.GetObjectInstance = function(_ovPointer, _steamHEChars, _ubiHEChars, _subtract)
if not EMXHookLibrary.IsHistoryEdition then
if EMXHookLibrary.Internal.CurrentGameVariant == EMXHookLibrary.GameVariant.OriginalWithOffset then
_ovPointer = (_ovPointer - EMXHookLibrary.Internal.GlobalOVOffset)
end
return EMXHookLibrary.RawPointer.New(_ovPointer);
end
if EMXHookLibrary.Internal.InstanceCache[_ovPointer] ~= nil then
return EMXHookLibrary.RawPointer.New(tonumber("0x" .. EMXHookLibrary.Internal.InstanceCache[_ovPointer]));
end
local _hexSplitChars = {};
local _lowestDigit = 0;
local HexString01, HexString02;
if EMXHookLibrary.Internal.CurrentGameVariant == EMXHookLibrary.GameVariant.HistoryEditionSteam then
_lowestDigit = _steamHEChars[1]
_hexSplitChars = {_steamHEChars[2], _steamHEChars[3], _steamHEChars[4], _steamHEChars[5]}
else
_lowestDigit = _ubiHEChars[1]
_hexSplitChars = {_ubiHEChars[2], _ubiHEChars[3], _ubiHEChars[4], _ubiHEChars[5]}
end
local Pointer = EMXHookLibrary.RawPointer.New(Logic.GetEntityScriptingValue(EMXHookLibrary.Internal.GlobalAdressEntity, -78))["0"]
if _subtract ~= nil then
HexString01 = string.format("%x", tostring((Pointer - (_lowestDigit))["0"]))
HexString02 = string.format("%x", tostring((Pointer - (_lowestDigit - 1))["0"]))
else
HexString01 = string.format("%x", tostring((Pointer + (_lowestDigit))["0"]))
HexString02 = string.format("%x", tostring((Pointer + (_lowestDigit + 1))["0"]))
end
-- Both strings need to consist of 8 digits, otherwise trailing zeroes got lost, so we need to re-add them
while (string.len(HexString01) < 8) do HexString01 = "0" .. HexString01 end
while (string.len(HexString02) < 8) do HexString02 = "0" .. HexString02 end
HexString01 = string.sub(HexString01, _hexSplitChars[1], _hexSplitChars[2])
HexString02 = string.sub(HexString02, _hexSplitChars[3], _hexSplitChars[4])
local DereferenceString = HexString02 .. HexString01
Framework.WriteToLog("EMXHookLibrary: Going to dereference HEPointer: 0x"..DereferenceString..". OVPointer: ".._ovPointer)
EMXHookLibrary.Internal.InstanceCache[_ovPointer] = DereferenceString
return EMXHookLibrary.RawPointer.New(tonumber("0x" .. DereferenceString));
end
-- ************************************************************************************************************************************************************ --
-- Get global instances of classes in memory, static value in OV, and offset in both HEs
-- ************************************************************************************************************************************************************ --
EMXHookLibrary.Internal.GetCEntityManager = function() return EMXHookLibrary.Internal.GetObjectInstance(11199488, {85, 1, 4, 5, 8}, {293, 0, 0, 1, 8})["0"] end
EMXHookLibrary.Internal.GetLogicPropertiesEx = function() return EMXHookLibrary.Internal.GetObjectInstance(11198716, {1601, 1, 2, 3, 8}, {28002, 0, 0, 1, 8})["0"] end
EMXHookLibrary.Internal.GetCEntityProps = function() return EMXHookLibrary.Internal.GetObjectInstance(11198560, {2593, 1, 6, 7, 8}, {2358, 0, 0, 1, 8})["0"] end
EMXHookLibrary.Internal.GetCEffectProps = function() return EMXHookLibrary.Internal.GetObjectInstance(11198564, {69981, 1, 4, 5, 8}, {189755, 0, 0, 1, 8})["0"] end
EMXHookLibrary.Internal.GetCGoodProps = function() return EMXHookLibrary.Internal.GetObjectInstance(11198636, {16529, 0, 0, 1, 8}, {30412, 1, 6, 7, 8})["0"] end
EMXHookLibrary.Internal.GetCGameLogic = function() return EMXHookLibrary.Internal.GetObjectInstance(11198552, {39, 0, 0, 1, 8}, {104, 1, 2, 3, 8})["0"] end
EMXHookLibrary.Internal.GetCGlobalsBaseEx = function() return EMXHookLibrary.Internal.GetObjectInstance(11674352, {774921, 1, 4, 5, 8}, {1803892, 1, 2, 3, 8})["0"] end
EMXHookLibrary.Internal.GetCGlobalsLogicEx = function() return EMXHookLibrary.Internal.GetObjectInstance(11674344, {1136615, 1, 6, 7, 8}, {108296, 1, 2, 3, 8}, true)["0"] end
EMXHookLibrary.Internal.GetCMain = function() return EMXHookLibrary.Internal.GetObjectInstance(11158232, {2250717, 0, 0, 1, 8}, {1338624, 1, 4, 5, 8}, true)["0"] end
EMXHookLibrary.Internal.GetCTextSet = function() return EMXHookLibrary.Internal.GetObjectInstance(11469188, {475209, 1, 4, 4, 8}, {1504636, 1, 6, 7, 8})["0"] end
EMXHookLibrary.Internal.GetCDisplay = function() return EMXHookLibrary.Internal.GetObjectInstance(11674360, {1617395, 1, 6, 7, 8}, {589264, 1, 2, 3, 8}, true)["0"] end
EMXHookLibrary.Internal.GetCCameraBehaviorRTS = function() return EMXHookLibrary.Internal.GetObjectInstance(11568248, {1766975, 1, 6, 7, 8}, {738468, 1, 4, 5, 8}, true) end
EMXHookLibrary.Internal.GetFPPrecisionObject = function() return EMXHookLibrary.Internal.GetObjectInstance(11795144, {6275025, 1, 4, 5, 8}, {7303684, 1, 4, 5, 8}) end
EMXHookLibrary.Internal.GetCFileSystemManager = function() return EMXHookLibrary.Internal.GetObjectInstance(11188828, {424694, 1, 8, 0, 0}, {5752, 1, 4, 5, 8})["0"] end
EMXHookLibrary.Internal.GetColorSetIDManager = function() return EMXHookLibrary.Internal.GetObjectInstance(11678212, {1351584, 7, 8, 1, 6}, {323476, 1, 6, 7, 8}, true) end
EMXHookLibrary.Internal.CalculateEntityIDToDisplayObject = function(_entityID)
local Result = EMXHookLibrary.Helpers.BitAnd(_entityID, 65535)
return EMXHookLibrary.Internal.GetCGlobalsLogicEx()["100"][(Result * 4) + 20];
end
EMXHookLibrary.Internal.CalculateEntityIDToLogicObject = function(_entityID)
local Result = EMXHookLibrary.Helpers.BitAnd(_entityID, 65535)
return EMXHookLibrary.Internal.GetCEntityManager()[(Result * 8) + 20];
end
-- ************************************************************************************************************************************************************ --
-- Dereference RawPointers
-- ************************************************************************************************************************************************************ --
EMXHookLibrary.Internal.GetValueAtPointer = function(_rawPointer)
if not Logic.IsEntityAlive(EMXHookLibrary.Internal.GlobalAdressEntity) then
local Error = "EMXHookLibrary: ERROR! Tried to get value at address "..tostring(_rawPointer).." without existing AdressEntity!"
Framework.WriteToLog(Error)
assert(false, Error)
return;
end
local Offset = (EMXHookLibrary.IsHistoryEdition and "-78") or "-81"
local Index = BigNum.mt.sub(_rawPointer.Pointer, EMXHookLibrary.Internal.GlobalHeapStart)
Index = BigNum.mt.div(Index, "4")
Index = BigNum.mt.add(Offset, Index)
return Logic.GetEntityScriptingValue(EMXHookLibrary.Internal.GlobalAdressEntity, tonumber(BigNum.mt.tostring(Index)))
end
EMXHookLibrary.Internal.SetValueAtPointer = function(_rawPointer, _Value)
if not Logic.IsEntityAlive(EMXHookLibrary.Internal.GlobalAdressEntity) then
local Error = "EMXHookLibrary: ERROR! Tried to set value at address "..tostring(_rawPointer).." without existing AdressEntity!"
Framework.WriteToLog(Error)
assert(false, Error)
return;
end
--if _Value >= 2147483648 then
-- EMXHookLibrary.Internal.GetFPPrecisionObject()("0", 0) -- Only works in OV currently, not HE!
--end
local Offset = (EMXHookLibrary.IsHistoryEdition and "-78") or "-81"
local Index = BigNum.mt.sub(_rawPointer.Pointer, EMXHookLibrary.Internal.GlobalHeapStart)
Index = BigNum.mt.div(Index, "4")
Index = BigNum.mt.add(Offset, Index)
Logic.SetEntityScriptingValue(EMXHookLibrary.Internal.GlobalAdressEntity, tonumber(BigNum.mt.tostring(Index)), _Value)