-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMAPRaceRecord.as
114 lines (100 loc) · 2.99 KB
/
CMAPRaceRecord.as
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
namespace CMAPRaceRecord {
const string MS_EVENT_NAME = "AlteredWRReporter_RaceRecord";
const string PATCH_ORIG = "return ZoneRecords;";
const string PATCH_TXT = """
foreach (Zone in _ZonesRecords) {
if (Zone.ZoneName == "World" && Zone.Records.count >= 1) {
SendCustomEvent("MLHook_Event_AlteredWRReporter_RaceRecord", ["" ^ Zone.Records[0].Score]);
}
}
return ZoneRecords;
""";
bool isEnabled = false;
string curMapUid = "";
int64 curWRTime = -1;
class Hook : MLHook::HookMLEventsByType {
Hook() {
super(MS_EVENT_NAME);
}
void OnEvent(MLHook::PendingEvent@ event) override final {
int64 time = Text::ParseInt(event.data[0]);
if (curWRTime != time) {
curWRTime = time;
Notify::Call(curMapUid, time);
}
}
}
Hook@ hook = Hook();
void Enable() {
isEnabled = true;
MLHook::RegisterMLHook(hook, MS_EVENT_NAME);
}
void Disable(CTrackMania@ app) {
isEnabled = false;
MLHook::UnregisterMLHookFromAll(hook);
Unpatch(app);
}
void Update(CTrackMania@ app) {
if (!isEnabled) return;
auto rootMap = app.RootMap;
if (rootMap is null) {
curMapUid = "";
} else {
string mapUid = rootMap.MapInfo.MapUid;
if (curMapUid != mapUid) {
curWRTime = -1;
if (Patch(app)) {
curMapUid = mapUid;
}
}
}
}
bool Patch(CTrackMania@ app) {
auto cmap = app.Network.ClientManiaAppPlayground;
if (cmap is null) {
return false;
}
auto uiLayers = cmap.UILayers;
for (uint i = 0; i < uiLayers.Length; i++) {
CGameUILayer@ curLayer = uiLayers[i];
int start = curLayer.ManialinkPageUtf8.IndexOf("<");
int end = curLayer.ManialinkPageUtf8.IndexOf(">");
if (start != -1 && end != -1) {
string manialinkName = curLayer.ManialinkPageUtf8.SubStr(start, end);
if (manialinkName.Contains("UIModule_Race_Record")) {
string manialinkInitial = curLayer.ManialinkPage;
string manialinkPatched = manialinkInitial.Replace(
PATCH_ORIG,
PATCH_TXT
);
curLayer.ManialinkPage = manialinkPatched;
return true;
}
}
}
return false;
}
void Unpatch(CTrackMania@ app) {
auto cmap = app.Network.ClientManiaAppPlayground;
if (cmap is null) {
return;
}
auto uiLayers = cmap.UILayers;
for (uint i = 0; i < uiLayers.Length; i++) {
CGameUILayer@ curLayer = uiLayers[i];
int start = curLayer.ManialinkPageUtf8.IndexOf("<");
int end = curLayer.ManialinkPageUtf8.IndexOf(">");
if (start != -1 && end != -1) {
string manialinkName = curLayer.ManialinkPageUtf8.SubStr(start, end);
if (manialinkName.Contains("UIModule_Race_Record")) {
string manialinkInitial = curLayer.ManialinkPage;
string manialinkPatched = manialinkInitial.Replace(
PATCH_TXT,
PATCH_ORIG
);
curLayer.ManialinkPage = manialinkPatched;
}
}
}
}
}