-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.as
625 lines (522 loc) · 15.4 KB
/
Main.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
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
CGameManiaPlanet@ g_app;
bool g_updateQueued = false;
Discord::User@ g_joinRequest;
NotifyInt g_statusMode = -1;
NotifyInt g_inServerTimeStart;
#if TURBO
CWebServicesTaskResult_GetDisplayNameScriptResult@ g_serverDisplayNameTask;
wstring g_serverDisplayName;
string g_serverDisplayID;
#endif
#if TMNEXT
ServicesMapInfo g_currentServicesMapInfo;
#endif
CGameCtnChallenge@ GetCurrentMap()
{
#if MP41 || TMNEXT
return g_app.RootMap;
#else
return g_app.Challenge;
#endif
}
string GetMapName(CGameCtnChallenge@ challenge)
{
#if TURBO
if (Regex::IsMatch(challenge.MapName, "^[0-9]+$")) {
return "#" + challenge.MapName;
}
#endif
return Text::StripFormatCodes(challenge.MapName);
}
void OnDisabled()
{
Discord::Shutdown();
}
void OnSettingsChanged()
{
g_updateQueued = true;
}
string Nth(int n)
{
switch (n % 100) {
case 11:
case 12:
case 13: return n + "th";
}
switch (n % 10) {
case 1: return n + "st";
case 2: return n + "nd";
case 3: return n + "rd";
}
return n + "th";
}
string GetTitleBaseId(CGameManiaTitle@ title)
{
if (title.BaseTitleId != "") {
return title.BaseTitleId;
}
return title.IdName;
}
int GetSecondsForTime(string _time)
{
if (_time == "") {
return 0;
}
_time = Text::StripFormatCodes(_time);
auto parse = _time.Split(":");
int hours = 0;
int minutes = 0;
int seconds = 0;
if (parse.Length == 3) {
hours = Text::ParseInt(parse[0]);
minutes = Text::ParseInt(parse[1]);
seconds = Text::ParseInt(parse[2]);
} else if (parse.Length == 2) {
minutes = Text::ParseInt(parse[0]);
seconds = Text::ParseInt(parse[1]);
} else if (parse.Length == 1) {
seconds = Text::ParseInt(parse[0]);
}
return (hours * 3600) + (minutes * 60) + seconds;
}
CControlBase@ FindControl(CControlBase@ control, const string &in id)
{
if (control.IdName == id) {
return control;
}
auto container = cast<CControlContainer>(control);
if (container is null) {
return null;
}
for (uint i = 0; i < container.Childs.Length; i++) {
auto ret = FindControl(container.Childs[i], id);
if (ret !is null) {
return ret;
}
}
return null;
}
Discord::Status GetTitleStatus(CGameManiaTitle@ title)
{
Discord::Status status;
status.LargeImageKey = GetTitleBaseId(title).ToLower();
auto keys = g_titles.GetKeys();
for (uint i = 0; i < keys.Length; i++) {
if (title.TitleId == keys[i]) {
g_titles.Get(keys[i], status.LargeImageKey);
break;
}
}
status.LargeImageText = Text::StripFormatCodes(title.Name);
return status;
}
bool IsSpectating()
{
if (g_app.Network is null) {
return false;
}
return g_app.Network.Spectator;
}
int GetServerPosition()
{
if (g_app.CurrentPlayground is null) {
return 0;
}
#if !TMNEXT
auto interfaceTM = cast<CTrackManiaRaceInterface>(g_app.CurrentPlayground.Interface);
if (interfaceTM !is null) {
return interfaceTM.PlayerGeneralPosition;
}
#endif
return 0;
}
int GetSecondsLeft()
{
if (g_app.CurrentPlayground is null) {
return 0;
}
#if TMNEXT
// Trackmania
auto manialinkPages = g_app.Network.GetManialinkPages();
if (manialinkPages !is null) {
for (uint i = 0; i < manialinkPages.Length; i++) {
auto page = manialinkPages[i];
if (page.MainFrame.Controls.Length == 0) {
continue;
}
auto pageFrame = cast<CGameManialinkFrame>(page.MainFrame.Controls[0]);
if (pageFrame is null || pageFrame.Controls.Length != 1) {
continue;
}
if (pageFrame.Controls[0].ControlId != "Race_Countdown") {
continue;
}
auto labelCountdown = cast<CGameManialinkLabel>(pageFrame.GetFirstChild("label-countdown"));
if (labelCountdown is null) {
continue;
}
return GetSecondsForTime(labelCountdown.Value);
}
}
#else
// Maniaplanet
auto interfaceTM = cast<CTrackManiaRaceInterface>(g_app.CurrentPlayground.Interface);
if (interfaceTM !is null) {
if (int(interfaceTM.TimeCountDown) > 0) {
return int(interfaceTM.TimeCountDown) / 1000;
}
}
#endif
// ShootMania
auto interfaceSM = cast<CSmArenaInterfaceUI>(g_app.CurrentPlayground.Interface);
if (interfaceSM !is null) {
auto labelTimeLeft = cast<CControlLabel>(FindControl(interfaceSM.InterfaceRoot, "LabelTimeLeft"));
if (labelTimeLeft !is null && labelTimeLeft.IsVisible) {
return GetSecondsForTime(labelTimeLeft.Label);
}
}
return 0;
}
void SetStatus_MainMenu()
{
Discord::Status status;
status.State = "Main menu";
status.LargeImageKey = "logo";
Discord::SetStatus(status);
}
void SetStatus_TitleMenu(CGameManiaTitle@ title)
{
Discord::Status status = GetTitleStatus(title);
status.State = "In menu";
Discord::SetStatus(status);
}
void SetStatus_TitleEditor(CGameEditorBase@ editorBase, CGameCtnEditor@ editor, CGameManiaTitle@ title)
{
Discord::Status status = GetTitleStatus(title);
if (editor !is null) {
auto mapEditor = cast<CGameCtnEditorFree>(editor);
if (mapEditor !is null) {
if (Setting_DisplayLevelNameEditor) {
#if TMNEXT
if (g_currentServicesMapInfo.m_uid == mapEditor.Challenge.IdName) {
status.LargeImageKey = g_currentServicesMapInfo.m_thumbUrl;
}
#endif
status.Details = GetMapName(mapEditor.Challenge);
}
status.State = "Editing map";
}
#if TMNEXT
auto mediaTracker = cast<CGameEditorMediaTracker>(editor);
#else
auto mediaTracker = cast<CGameCtnMediaTracker>(editor);
#endif
if (mediaTracker !is null) {
if (Setting_DisplayLevelNameEditor) {
auto currentMap = GetCurrentMap();
#if TMNEXT
if (g_currentServicesMapInfo.m_uid == currentMap.IdName) {
status.LargeImageKey = g_currentServicesMapInfo.m_thumbUrl;
}
#endif
status.Details = GetMapName(currentMap);
}
status.State = "In Mediatracker";
}
auto itemEditor = cast<CGameEditorItem>(editor);
if (itemEditor !is null) { status.State = "In item editor"; }
#if !TURBO
auto meshEditor = cast<CGameEditorMesh>(editor);
if (meshEditor !is null) { status.State = "In mesh editor"; }
#endif
auto actionMaker = cast<CGameActionMaker>(editor);
if (actionMaker !is null) { status.State = "In action maker"; }
auto moduleEditor = cast<CGameEditorModule>(editor);
if (moduleEditor !is null) { status.State = "In module editor"; }
#if !TURBO
auto editorEditor = cast<CGameEditorEditor>(editor);
if (editorEditor !is null) { status.State = "In editor editor"; }
#endif
#if MP40
auto pixelEditor = cast<CGameEditorPixel>(editor);
if (pixelEditor !is null) { status.State = "In pixel editor"; }
#endif
} else if (editorBase !is null) {
auto interfaceDesigner = cast<CGameEditorManialink>(editorBase);
if (interfaceDesigner !is null) { status.State = "In interface designer"; }
auto animSetEditor = cast<CGameEditorAnimSet>(editorBase);
if (animSetEditor !is null) { status.State = "In animation set editor"; }
}
Discord::SetStatus(status);
}
void SetStatus_TitleSolo(CGameCtnChallenge@ challenge, CGameManiaTitle@ title)
{
Discord::Status status = GetTitleStatus(title);
if (Setting_DisplayLevelNameSolo) {
#if TMNEXT
if (g_currentServicesMapInfo !is null && challenge !is null && g_currentServicesMapInfo.m_uid == challenge.IdName) {
status.LargeImageKey = g_currentServicesMapInfo.m_thumbUrl;
}
#endif
status.Details = GetMapName(challenge);
}
status.State = "Playing solo";
Discord::SetStatus(status);
}
void SetStatus_Server(CGameCtnChallenge@ challenge, CGameCtnNetServerInfo@ serverInfo, CGameManiaTitle@ title)
{
#if TURBO
int numPlayers = g_app.BuddiesManager.CurrentServerPlayerCount - 1; // -1 because 1 of these is the server account
int maxPlayers = g_app.BuddiesManager.CurrentServerPlayerCountMax;
#else
int numPlayers = 0;
int maxPlayers = 0;
// Not sure why this can be null but Nsgr reported it once (July 12th 2022)
if (g_app.ChatManagerScript !is null) {
numPlayers = g_app.ChatManagerScript.CurrentServerPlayerCount - 1; // -1 because 1 of these is the server account
maxPlayers = g_app.ChatManagerScript.CurrentServerPlayerCountMax;
}
#endif
int position = GetServerPosition();
int secondsLeft = GetSecondsLeft();
bool spectating = IsSpectating();
Discord::Status status = GetTitleStatus(title);
if (spectating) {
status.Details = "Spectating | ";
} else if (position > 0) {
status.Details = Nth(position) + " | ";
}
if (challenge !is null && Setting_DisplayLevelNameOnline) {
#if TMNEXT
if (g_currentServicesMapInfo.m_uid == challenge.IdName) {
status.LargeImageKey = g_currentServicesMapInfo.m_thumbUrl;
}
#endif
status.Details += GetMapName(challenge);
} else {
status.Details += "In server";
}
if (Setting_DisplayServerInfoOnline) {
#if TURBO
status.State = g_serverDisplayName;
#else
status.State = Text::StripFormatCodes(serverInfo.ServerName);
#endif
status.PartyId = serverInfo.ServerLogin;
string serverLink = serverInfo.ServerLogin + "@" + title.IdName;
status.JoinSecret = serverLink;
status.SpectateSecret = "spec|" + serverLink;
status.PartySize = numPlayers;
status.PartyMax = maxPlayers;
}
if (secondsLeft > 0) {
status.StartTimestamp = g_inServerTimeStart;
status.EndTimestamp = Time::Stamp + secondsLeft;
}
Discord::SetStatus(status);
}
void SetStatus()
{
if (g_statusMode == -1) {
return;
} else if (g_statusMode == 0) {
auto serverInfo = cast<CGameCtnNetServerInfo>(g_app.Network.ServerInfo);
SetStatus_Server(GetCurrentMap(), serverInfo, g_app.LoadedManiaTitle);
} else if (g_statusMode == 1) {
SetStatus_TitleEditor(g_app.EditorBase, g_app.Editor, g_app.LoadedManiaTitle);
} else if (g_statusMode == 2) {
SetStatus_TitleSolo(GetCurrentMap(), g_app.LoadedManiaTitle);
} else if (g_statusMode == 3) {
SetStatus_TitleMenu(g_app.LoadedManiaTitle);
} else if (g_statusMode == 4) {
SetStatus_MainMenu();
}
}
void JoinThread()
{
while (g_app.ManiaTitles.Length == 0) {
yield();
}
sleep(2000);
while (true) {
string joinVerb = "qjoin";
string joinUrl = "";
string joinSecret = Discord::GetQueuedJoin();
if (joinSecret != "") {
joinUrl = joinSecret;
} else {
string spectateSecret = Discord::GetQueuedSpectate();
if (spectateSecret != "") {
auto parse = spectateSecret.Split("|", 2);
if (parse.Length == 2 && parse[0] == "spec") {
joinVerb = "qspectate";
joinUrl = parse[1];
}
}
}
if (joinUrl != "") {
print("Joining " + joinUrl);
joinUrl = "#" + joinVerb + "=" + joinUrl;
g_app.ManiaPlanetScriptAPI.OpenLink(joinUrl, CGameManiaPlanetScriptAPI::ELinkType::ManialinkBrowser);
}
if (Discord::GetNumJoinRequests() > 0) {
if (g_joinRequest is null) {
if (Setting_IgnoreJoinRequests) {
while (true) {
auto request = Discord::GetQueuedJoinRequest();
if (request is null) {
break;
}
Discord::Respond(request.ID, Discord::Response::Ignore);
print("Automatically ignored a Discord join request from " + request.Name);
}
} else {
@g_joinRequest = Discord::GetQueuedJoinRequest();
UI::ShowNotification("\\$78d" + Icons::Discord + "\\$z Discord Invite", "\\$ff7" + g_joinRequest.Name + "\\$z wants to join. Open the overlay to accept or reject!");
auto dialog = Dialogs::Question(g_joinRequest.Name + " is asking to join. Do you want them to join you?", function() {
print("Responding to " + g_joinRequest.Name + " with YES");
Discord::Respond(g_joinRequest.ID, Discord::Response::Yes);
@g_joinRequest = null;
}, function() {
print("Responding to " + g_joinRequest.Name + " with NO");
Discord::Respond(g_joinRequest.ID, Discord::Response::No);
@g_joinRequest = null;
});
dialog.AddButton("Ignore", function() {
print("Responding to " + g_joinRequest.Name + " with IGNORE");
Discord::Respond(g_joinRequest.ID, Discord::Response::Ignore);
@g_joinRequest = null;
});
dialog.AddCheckbox("Always ignore join requests in the future", function(checked) {
Setting_IgnoreJoinRequests = checked;
});
dialog.m_title = "Join request";
}
}
}
yield();
}
}
void RenderInterface()
{
Dialogs::RenderInterface();
}
void Main()
{
@g_app = cast<CGameManiaPlanet>(GetApp());
print("Initializing Discord...");
#if TMNEXT
Discord::Initialize("689165864028864558");
#elif TURBO
Discord::Initialize("500620964195991562");
#else
Discord::Initialize("415975536343646208");
#endif
while (!Discord::IsReady()) {
yield();
}
auto user = Discord::GetUser();
print("Discord is ready: " + user.Name + " (ID " + user.ID + ")");
#if !TURBO && !TMNEXT
while (g_app.CurrentProfile is null) {
yield();
}
#endif
SetStatus();
startnew(JoinThread);
NotifyNod inTitle;
NotifyNod inServerChallenge;
NotifyInt inServerPlayerCount;
NotifyInt inServerPosition;
NotifyBool inServerSpectating;
NotifyString inMapUid;
NotifyString inServerLogin;
#if TURBO
NotifyString inServerDisplayName;
#endif
while (true) {
sleep(1000);
auto serverInfo = cast<CGameCtnNetServerInfo>(g_app.Network.ServerInfo);
inTitle = g_app.LoadedManiaTitle;
auto currentMap = GetCurrentMap();
string currentFrame = "";
if (g_app.ActiveMenus.Length > 0) {
currentFrame = g_app.ActiveMenus[0].CurrentFrame.IdName;
}
if (serverInfo !is null && serverInfo.ServerLogin != "") {
g_statusMode = 0;
} else if (g_app.LoadedManiaTitle !is null && currentFrame != "FrameManiaPlanetMain") {
if (g_app.Editor !is null) {
g_statusMode = 1;
} else if (currentMap !is null) {
g_statusMode = 2;
} else {
g_statusMode = 3;
}
} else {
g_statusMode = 4;
}
if (g_statusMode == 0) {
#if TURBO
if (inServerLogin != serverInfo.ServerLogin) {
inServerLogin = serverInfo.ServerLogin;
inServerDisplayName = "";
g_serverDisplayName = "";
auto serverDesc = string(serverInfo.ServerName).Split("|", 3);
g_serverDisplayID = serverDesc[0];
auto msUser = g_app.ManiaPlanetScriptAPI.MasterServer_MSUsers[0];
@g_serverDisplayNameTask = g_app.ManiaPlanetScriptAPI.MasterServer_GetDisplayName(msUser.Id);
g_serverDisplayNameTask.AddLogin(g_serverDisplayID);
g_serverDisplayNameTask.StartTask();
}
if (g_serverDisplayNameTask !is null && !g_serverDisplayNameTask.IsProcessing) {
if (g_serverDisplayNameTask.HasSucceeded) {
g_serverDisplayName = g_serverDisplayNameTask.GetDisplayName(g_serverDisplayID);
inServerDisplayName = string(g_serverDisplayName);
print("Server display name: \"" + g_serverDisplayName + "\"");
} else {
print("Failed to fetch server display name!");
}
g_app.ManiaPlanetScriptAPI.MasterServer_ReleaseMSTaskResult(g_serverDisplayNameTask.Id);
@g_serverDisplayNameTask = null;
}
#else
inServerLogin = serverInfo.ServerLogin;
#endif
if (inServerChallenge != currentMap) {
g_inServerTimeStart = Time::Stamp;
inServerChallenge = currentMap;
}
inServerPlayerCount = int(g_app.Network.PlayerInfos.Length) - 1;
int serverPos = GetServerPosition();
if (serverPos > 0) {
inServerPosition = serverPos;
}
inServerSpectating = IsSpectating();
}
bool canFetchCurrentMap = true;
if (g_statusMode == 1 && !Setting_DisplayLevelNameEditor) {
canFetchCurrentMap = false;
} else if (g_statusMode == 2 && !Setting_DisplayLevelNameSolo) {
canFetchCurrentMap = false;
} else if (g_statusMode == 0 && !Setting_DisplayLevelNameOnline) {
canFetchCurrentMap = false;
}
if (currentMap !is null) {
#if TMNEXT
if (inMapUid != currentMap.IdName && currentMap.Id.Value != 0xFFFFFFFF && canFetchCurrentMap) {
g_currentServicesMapInfo = GetMapFromServices(currentMap.IdName);
}
#endif
inMapUid = currentMap.IdName;
} else {
#if TMNEXT
g_currentServicesMapInfo.m_uid = "";
#endif
}
if (g_updateQueued) {
g_updateQueued = false;
SetStatus();
}
}
}