forked from MMOBugs/MQ2GMCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQ2GMCheck.cpp
1663 lines (1473 loc) · 50.2 KB
/
MQ2GMCheck.cpp
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
// MQ2GMCheck.cpp : Defines the entry point for the DLL application.
//
// Check to see if a GM is in the zone. This is not fool proof. It is absolutely
// true that a GM could be right in front of you and you'd never know it. This
// plugin will simply find those who are in the zone and not gm-invis, or who
// just came into the zone and were not gm-invised at the time. If a GM comes
// into the zone already gm-invised, we will not know about that.
//
//
// PLUGIN_API is only to be used for callbacks. All existing callbacks at this time
// are shown below. Remove the ones your plugin does not use. Always use Initialize
// and Shutdown for setup and cleanup.
//
// TODO: Sound settings are loaded by character but saved globally (in the /gmcheck save command and write settings)
// need separate settings to handle this (an interim fix might be to just track when it was loaded by char)
#include <mq/Plugin.h>
#include <vector>
#include <mmsystem.h>
#include <mq/imgui/ImGuiUtils.h>
PreSetup("MQ2GMCheck");
PLUGIN_VERSION(5.4);
constexpr const char* PluginMsg = "\ay[\aoMQ2GMCheck\ax] ";
uint32_t bmMQ2GMCheck = 0;
uint64_t StopSoundTimer = 0;
DWORD dwVolume;
DWORD NewVol;
bool bGMCmdActive = false;
bool bVolSet = false;
enum FlagOptions { Off, On, Toggle };
enum class GMStatuses
{
Enter,
Leave,
Reminder
};
class GMTrack
{
private:
typedef std::chrono::high_resolution_clock clock;
typedef std::chrono::duration<float, std::milli> duration;
clock::time_point pulsestart;
clock::time_point reminderstart;
clock::time_point reminderdelay;
enum ExcludeZone { Exclude, Include, Zoning };
public:
ExcludeZone eExcludeZone = ExcludeZone::Include;
std::map<std::string, bool> GMNames;
std::string LastGMName = "NONE";
std::string LastGMTime = "NEVER";
std::string LastGMDate = "NEVER";
std::string LastGMZone = "NONE";
GMTrack();
template <class Iterator> Iterator ciEqual(Iterator first, Iterator last, const char* value);
void CheckAlerts();
bool AlertPending();
uint32_t GMCount() const;
void AddGM(const char* gm_name);
void RemoveGM(const char* gm_name);
void PlayAlerts();
void Clear();
void BeginZone();
void EndZone();
void SetExcludedZone();
bool IsIncludedZone() const;
} *gmTrack;
static void DoGMAlert(const char* gm_name, GMStatuses status, bool test = false);
static void TrackGMs(const char* GMName);
static const char* DisplayDT(const char* Format);
class BooleanOption
{
private:
std::string KeyName;
std::string ChatMessage;
bool bFlag = false;
public:
BooleanOption() {};
BooleanOption(bool Default, std::string Key, std::string Message)
{
KeyName = Key;
ChatMessage = Message;
bFlag = Default;
if (KeyName.length())
bFlag = GetPrivateProfileBool("Settings", KeyName.c_str(), Default, INIFileName);
};
bool Read()
{
if (KeyName.length())
bFlag = GetPrivateProfileBool("Settings", KeyName.c_str(), bFlag, INIFileName);
return(bFlag);
};
void Write(enum FlagOptions fopt, bool silent = false)
{
if (fopt == FlagOptions::Toggle)
bFlag = !bFlag;
else if (fopt == FlagOptions::On)
bFlag = true;
else
bFlag = false;
if (KeyName.length())
WritePrivateProfileBool("Settings", KeyName.c_str(), bFlag, INIFileName);
if (!silent)
WriteChatf("%s\am%s %s\am.", PluginMsg, ChatMessage.c_str(), bFlag ? "\agENABLED" : "\arDISABLED");
};
};
//----------------------------------------------------------------------------
// this class holds persisted settings for this plugin.
class Settings
{
public:
static constexpr inline FlagOptions default_GMCheckEnabled = FlagOptions::On;
static constexpr inline FlagOptions default_GMQuietEnabled = FlagOptions::Off;
static constexpr inline FlagOptions default_GMSoundEnabled = FlagOptions::On;
static constexpr inline FlagOptions default_GMBeepEnabled = FlagOptions::Off;
static constexpr inline FlagOptions default_GMPopupEnabled = FlagOptions::Off;
static constexpr inline FlagOptions default_GMCorpseEnabled = FlagOptions::Off;
static constexpr inline FlagOptions default_GMChatAlertEnabled = FlagOptions::On;
static constexpr inline FlagOptions default_ExcludeZonesEnabled = FlagOptions::Off;
static constexpr inline int default_ReminderInterval = 30;
static constexpr inline char* default_ExcludeZones = "nexus|poknowledge";
std::string szGMEnterCmd = std::string();
std::string szGMEnterCmdIf = std::string();
std::string szGMLeaveCmd = std::string();
std::string szGMLeaveCmdIf = std::string();
std::string szExcludeZones = std::string();
std::filesystem::path Sound_GMEnter = std::filesystem::path(gPathResources) / "Sounds\\gmenter.mp3";
std::filesystem::path Sound_GMLeave = std::filesystem::path(gPathResources) / "Sounds\\gmleave.mp3";
std::filesystem::path Sound_GMRemind = std::filesystem::path(gPathResources) / "Sounds\\gmremind.mp3";
BooleanOption m_GMCheckEnabled;
BooleanOption m_GMSoundEnabled;
BooleanOption m_GMBeepEnabled;
BooleanOption m_GMPopupEnabled;
BooleanOption m_GMCorpseEnabled;
BooleanOption m_GMChatAlertEnabled;
BooleanOption m_GMQuietEnabled;
BooleanOption m_ExcludeZonesEnabled;
inline int GetReminderInterval() const { return m_ReminderInterval; }
void SetReminderInterval(int reminderinterval);
void Load();
void Reset();
[[nodiscard]] std::filesystem::path SearchSoundPaths(std::filesystem::path file_path);
[[nodiscard]] std::filesystem::path GetBestSoundFile(const std::filesystem::path& file_path, bool try_alternate_extension = true);
void SetGMSoundFile(const char* friendly_name, std::filesystem::path* global_path);
void SetAllGMSoundFiles();
Settings()
{
m_GMCheckEnabled = BooleanOption(default_GMCheckEnabled, "GMCheck", "GM checking is now");
m_GMSoundEnabled = BooleanOption(default_GMSoundEnabled, "GMSound", "Sound playing on GM detection is now");
m_GMBeepEnabled = BooleanOption(default_GMBeepEnabled, "GMBeep", "Beeping on GM detection is now");
m_GMPopupEnabled = BooleanOption(default_GMPopupEnabled, "GMPopup", "Showing popup message on GM detection is now");
m_GMCorpseEnabled = BooleanOption(default_GMCorpseEnabled, "GMCorpse", "Alerting for GM corpses is now");
m_GMChatAlertEnabled = BooleanOption(default_GMChatAlertEnabled, "GMChat", "Displaying GM detection alerts in MQ chat window is now");
m_GMQuietEnabled = BooleanOption(FlagOptions::Off, "", "GM alert and reminder quiet mode is now");
m_ExcludeZonesEnabled = BooleanOption(default_ExcludeZonesEnabled, "ExcludeZones", "Excluding zones listed in ExcludeZoneList from GM detection is now");
};
private:
int m_ReminderInterval = default_ReminderInterval;
};
Settings s_settings;
void Settings::Load()
{
m_GMCheckEnabled.Read();
m_GMSoundEnabled.Read();
m_GMBeepEnabled.Read();
m_GMPopupEnabled.Read();
m_GMCorpseEnabled.Read();
m_GMChatAlertEnabled.Read();
m_ExcludeZonesEnabled.Read();
m_GMQuietEnabled.Write(FlagOptions::Off, true);
m_ReminderInterval = GetPrivateProfileInt("Settings", "RemInt", default_ReminderInterval, INIFileName);
if (m_ReminderInterval < 10 && m_ReminderInterval)
m_ReminderInterval = 10;
SetAllGMSoundFiles();
szGMEnterCmd = GetPrivateProfileString("Settings", "GMEnterCmd", std::string(), INIFileName);
szGMEnterCmdIf = GetPrivateProfileString("Settings", "GMEnterCmdIf", std::string(), INIFileName);
szGMLeaveCmd = GetPrivateProfileString("Settings", "GMLeaveCmd", std::string(), INIFileName);
szGMLeaveCmdIf = GetPrivateProfileString("Settings", "GMLeaveCmdIf", std::string(), INIFileName);
szExcludeZones = GetPrivateProfileString("Settings", "ExcludeZoneList", default_ExcludeZones, INIFileName);
gmTrack->SetExcludedZone();
}
void Settings::Reset()
{
m_GMCheckEnabled.Write(default_GMCheckEnabled);
m_GMSoundEnabled.Write(default_GMSoundEnabled);
m_GMBeepEnabled.Write(default_GMBeepEnabled);
m_GMPopupEnabled.Write(default_GMPopupEnabled);
m_GMCorpseEnabled.Write(default_GMCorpseEnabled);
m_GMChatAlertEnabled.Write(default_GMChatAlertEnabled);
m_GMQuietEnabled.Write(default_GMQuietEnabled);
m_ExcludeZonesEnabled.Write(default_ExcludeZonesEnabled);
szGMEnterCmd = "";
szGMEnterCmdIf = "";
szGMLeaveCmd = "";
szGMLeaveCmdIf = "";
szExcludeZones = default_ExcludeZones;
m_ReminderInterval = default_ReminderInterval;
Sound_GMEnter = std::filesystem::path(gPathResources) / "Sounds\\gmenter.mp3";
Sound_GMLeave = std::filesystem::path(gPathResources) / "Sounds\\gmleave.mp3";
Sound_GMRemind = std::filesystem::path(gPathResources) / "Sounds\\gmremind.mp3";
gmTrack->SetExcludedZone();
}
void Settings::SetReminderInterval(int ReminderInterval)
{
if (ReminderInterval == m_ReminderInterval)
return;
m_ReminderInterval = ReminderInterval;
if (m_ReminderInterval < 10 && m_ReminderInterval)
m_ReminderInterval = 10;
WritePrivateProfileInt("Settings", "RemInt", m_ReminderInterval, INIFileName);
}
[[nodiscard]] std::filesystem::path Settings::SearchSoundPaths(std::filesystem::path file_path)
{
std::error_code ec;
const std::filesystem::path resources_path = gPathResources;
// If they gave an absolute path, no sense checking other locations
if (file_path.is_relative())
{
// Try relative to the Sounds directory first
if (exists(resources_path / "Sounds" / file_path, ec))
{
file_path = resources_path / "Sounds" / file_path;
}
// Then relative to the resources directory
else if (exists(resources_path / file_path, ec))
{
file_path = resources_path / file_path;
}
}
return file_path;
}
[[nodiscard]] std::filesystem::path Settings::GetBestSoundFile(const std::filesystem::path& file_path, bool try_alternate_extension)
{
std::error_code ec;
std::filesystem::path return_path = file_path;
// Only need to worry about it if it doesn't exist (could also not be a file, but that's bad input)
if (!file_path.empty() && !exists(return_path, ec))
{
// If there is no extension, assume mp3
if (!return_path.has_extension())
{
return_path.replace_extension("mp3");
}
std::filesystem::path tmp = SearchSoundPaths(return_path);
if (exists(tmp, ec))
{
return_path = tmp;
}
else
{
tmp = SearchSoundPaths(return_path.filename());
if (exists(tmp, ec))
{
return_path = tmp;
}
else if (try_alternate_extension)
{
tmp = return_path;
if (tmp.extension() == ".mp3")
{
tmp = GetBestSoundFile(tmp.replace_extension("wav"), false);
}
else
{
tmp = GetBestSoundFile(tmp.replace_extension("mp3"), false);
}
if (exists(tmp, ec))
{
return_path = tmp;
}
}
}
}
if (return_path != file_path)
{
WriteChatf("%s\atWARNING - Sound file could not be found. Replacing \"\ay%s\ax\" with \"\ay%s\ax\"", PluginMsg, file_path.string().c_str(), return_path.string().c_str());
}
return return_path;
}
void Settings::SetGMSoundFile(const char* friendly_name, std::filesystem::path* global_path)
{
std::error_code ec;
std::filesystem::path tmp;
if (pLocalPC && PrivateProfileKeyExists(pLocalPC->Name, friendly_name, INIFileName))
{
tmp = GetBestSoundFile(GetPrivateProfileString(pLocalPC->Name, friendly_name, (*global_path).string(), INIFileName));
if (!exists(tmp, ec))
{
WriteChatf("%s\atWARNING - GM '%s' file not found for %s (Global Setting will be used instead): \am%s", PluginMsg, friendly_name, pLocalPC->Name, tmp.string().c_str());
}
}
if (tmp.empty() || !exists(tmp, ec))
{
tmp = GetBestSoundFile(GetPrivateProfileString("Settings", friendly_name, (*global_path).string(), INIFileName));
}
if (!exists(tmp, ec))
{
WriteChatf("%s\atWARNING - GM '%s' file not found: \am%s", PluginMsg, friendly_name, tmp.string().c_str());
}
else
{
*global_path = tmp;
}
}
void Settings::SetAllGMSoundFiles()
{
SetGMSoundFile("EnterSound", &Sound_GMEnter);
SetGMSoundFile("LeaveSound", &Sound_GMLeave);
SetGMSoundFile("RemindSound", &Sound_GMRemind);
}
GMTrack::GMTrack()
{
pulsestart = clock::now();
reminderstart = clock::now();
reminderdelay = clock::now();
}
template <class Iterator>
Iterator GMTrack::ciEqual(Iterator first, Iterator last, const char* value)
{
while (first != last)
{
if (_stricmp((*first).c_str(), value) == 0)
{
return first;
}
first++;
}
return last;
}
void GMTrack::CheckAlerts()
{
// Remove ourself if we were placed in the list
if (!GMNames.empty())
{
for (auto it = GMNames.begin(); it != GMNames.end(); it++)
{
const PlayerClient* pSpawn = GetSpawnByName(it->first.c_str());
if (pSpawn && pSpawn->GM && pSpawn->SpawnID == pLocalPlayer->SpawnID)
{
GMNames.erase(it);
}
}
}
// Remove any GMs that left
if (!GMNames.empty())
{
for (auto it = GMNames.begin(); it != GMNames.end(); it++)
{
const PlayerClient* pSpawn = GetSpawnByName(it->first.c_str());
if (!pSpawn)
{
GMNames.erase(it);
}
else if (!pSpawn->GM)
{
GMNames.erase(it);
}
}
}
// Add any GMs that appeared
SPAWNINFO* pSpawn = pSpawnList;
while (pSpawn) {
if (pSpawn->GM && pSpawn->SpawnID != pLocalPlayer->SpawnID)
{
AddGM(pSpawn->DisplayedName);
}
pSpawn = pSpawn->GetNext();
}
// Alert if not flagged yet
if (!GMNames.empty() && !s_settings.m_GMQuietEnabled.Read() && s_settings.m_GMCheckEnabled.Read())
{
for (auto it = GMNames.begin(); it != GMNames.end(); it++)
{
if (!it->second)
{
it->second = true;
DoGMAlert(it->first.c_str(), GMStatuses::Enter);
}
}
}
}
bool GMTrack::AlertPending()
{
if (!GMNames.empty() && !s_settings.m_GMQuietEnabled.Read() && s_settings.m_GMCheckEnabled.Read())
{
for (auto it = GMNames.begin(); it != GMNames.end(); it++)
{
if (!it->second)
{
return true;
}
}
}
return false;
}
uint32_t GMTrack::GMCount() const
{
return GMNames.size();
}
void GMTrack::AddGM(const char* gm_name)
{
for (auto it = GMNames.begin(); it != GMNames.end(); it++)
{
if (!_stricmp(it->first.c_str(), gm_name))
return;
}
TrackGMs(gm_name);
GMNames.insert(std::make_pair(gm_name, false));
LastGMName = gm_name;
LastGMTime = DisplayDT("%I:%M:%S %p");
LastGMDate = DisplayDT("%m-%d-%y");
LastGMZone = "UNKNOWN";
const int zoneid = pLocalPC->get_zoneId();
if (zoneid <= MAX_ZONES)
{
LastGMZone = pWorldData->ZoneArray[zoneid]->LongName;
}
}
void GMTrack::RemoveGM(const char* gm_name)
{
for (auto it = GMNames.begin(); it != GMNames.end(); it++)
{
if (!_stricmp(it->first.c_str(), gm_name))
{
GMNames.erase(it);
}
}
}
void GMTrack::PlayAlerts()
{
MQScopedBenchmark bm(bmMQ2GMCheck);
if (eExcludeZone == ExcludeZone::Zoning)
return;
if (bVolSet && StopSoundTimer && MQGetTickCount64() >= StopSoundTimer)
{
StopSoundTimer = 0;
waveOutSetVolume(nullptr, dwVolume);
}
if (gGameState == GAMESTATE_INGAME)
{
duration elapsed = clock::now() - pulsestart;
bool AlertPending = gmTrack->AlertPending();
if (elapsed.count() > 15000 || AlertPending)
{
uint32_t gmc = gmTrack->GMCount();
pulsestart = clock::now();
gmTrack->CheckAlerts();
if (gmTrack->GMCount() > gmc)
reminderstart = clock::now();
}
if (s_settings.GetReminderInterval() > 0)
{
duration elapsed = clock::now() - reminderstart;
duration delayelapsed = clock::now() - reminderdelay;
if (elapsed.count() > s_settings.GetReminderInterval() * 1000 && delayelapsed.count() > 10000)
{
reminderstart = clock::now();
if (!GMNames.empty() && !s_settings.m_GMQuietEnabled.Read() && s_settings.m_GMCheckEnabled.Read() && !AlertPending)
{
std::string joined_names = "";
for (auto it = GMNames.begin(); it != GMNames.end(); it++)
{
if (it == GMNames.begin())
joined_names = "\ag";
else
joined_names += "\ax\am,\ax \ag";
joined_names += it->first;
}
DoGMAlert(joined_names.c_str(), GMStatuses::Reminder);
}
}
}
}
}
void GMTrack::Clear()
{
GMNames.clear();
}
void GMTrack::BeginZone()
{
eExcludeZone = ExcludeZone::Zoning;
GMNames.clear();
}
void GMTrack::EndZone()
{
s_settings.m_GMQuietEnabled.Write(FlagOptions::Off, true);
SetExcludedZone();
reminderdelay = clock::now();
}
void GMTrack::SetExcludedZone()
{
if (s_settings.m_ExcludeZonesEnabled.Read() && !s_settings.szExcludeZones.empty())
{
const int CurrentZone = pLocalPC ? (pLocalPC->zoneId & 0x7FFF) : 0;
if (CurrentZone > 0)
{
const std::vector<std::string> ExcludeZones = split(s_settings.szExcludeZones, '|');
if (ciEqual(ExcludeZones.begin(), ExcludeZones.end(), GetShortZone(CurrentZone)) != ExcludeZones.end())
{
eExcludeZone = ExcludeZone::Exclude;
GMNames.clear();
return;
}
}
}
eExcludeZone = ExcludeZone::Include;
}
bool GMTrack::IsIncludedZone() const
{
if (eExcludeZone == ExcludeZone::Include)
return true;
return false;
}
enum HistoryType {
eHistory_Zone,
eHistory_Server,
eHistory_All
};
int MCEval(const char* zBuffer)
{
char zOutput[MAX_STRING] = { 0 };
if (zBuffer[0] == '\0')
return 1;
strcpy_s(zOutput, zBuffer);
ParseMacroData(zOutput, MAX_STRING);
return GetIntFromString(zOutput, 0);
}
class MQ2GMCheckType* pGMCheckType = nullptr;
class MQ2GMCheckType : public MQ2Type
{
public:
enum class GMCheckMembers
{
Status = 1,
GM,
Names,
Sound,
Beep,
Popup,
Corpse,
Quiet,
Interval,
Enter,
Leave,
Remind,
ExcludeZones,
LastGMName,
LastGMTime,
LastGMDate,
LastGMZone,
GMEnterCmd,
GMEnterCmdIf,
GMLeaveCmd,
GMLeaveCmdIf,
ExcludeZoneList,
};
MQ2GMCheckType() :MQ2Type("GMCheck")
{
ScopedTypeMember(GMCheckMembers, Status);
ScopedTypeMember(GMCheckMembers, GM);
ScopedTypeMember(GMCheckMembers, Names);
ScopedTypeMember(GMCheckMembers, Sound);
ScopedTypeMember(GMCheckMembers, Beep);
ScopedTypeMember(GMCheckMembers, Popup);
ScopedTypeMember(GMCheckMembers, Corpse);
ScopedTypeMember(GMCheckMembers, Quiet);
ScopedTypeMember(GMCheckMembers, Interval);
ScopedTypeMember(GMCheckMembers, Enter);
ScopedTypeMember(GMCheckMembers, Leave);
ScopedTypeMember(GMCheckMembers, Remind);
ScopedTypeMember(GMCheckMembers, ExcludeZones);
ScopedTypeMember(GMCheckMembers, LastGMName);
ScopedTypeMember(GMCheckMembers, LastGMTime);
ScopedTypeMember(GMCheckMembers, LastGMDate);
ScopedTypeMember(GMCheckMembers, LastGMZone);
ScopedTypeMember(GMCheckMembers, GMEnterCmd);
ScopedTypeMember(GMCheckMembers, GMEnterCmdIf);
ScopedTypeMember(GMCheckMembers, GMLeaveCmd);
ScopedTypeMember(GMCheckMembers, GMLeaveCmdIf);
ScopedTypeMember(GMCheckMembers, ExcludeZoneList);
}
virtual bool GetMember(MQVarPtr VarPtr, const char* Member, char* Index, MQTypeVar& Dest) override
{
using namespace mq::datatypes;
MQTypeMember* pMember = MQ2GMCheckType::FindMember(Member);
if (!pMember)
return false;
switch ((GMCheckMembers)pMember->ID)
{
case GMCheckMembers::Status:
Dest.DWord = s_settings.m_GMCheckEnabled.Read();
Dest.Type = pBoolType;
return true;
case GMCheckMembers::GM:
Dest.DWord = gmTrack->GMCount() > 0 ? true : false;
Dest.Type = pBoolType;
return true;
case GMCheckMembers::Names:
{
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
if (!gmTrack->GMNames.empty() && s_settings.m_GMCheckEnabled.Read())
{
std::string joined_names = "";
for (auto it = gmTrack->GMNames.begin(); it != gmTrack->GMNames.end(); it++)
{
if (it != gmTrack->GMNames.begin())
joined_names += ", ";
joined_names += it->first;
}
strcpy_s(DataTypeTemp, joined_names.c_str());
return true;
}
return false;
}
case GMCheckMembers::Sound:
Dest.DWord = s_settings.m_GMSoundEnabled.Read();
Dest.Type = pBoolType;
return true;
case GMCheckMembers::Beep:
Dest.DWord = s_settings.m_GMBeepEnabled.Read();
Dest.Type = pBoolType;
return true;
case GMCheckMembers::Popup:
Dest.DWord = s_settings.m_GMPopupEnabled.Read();
Dest.Type = pBoolType;
return true;
case GMCheckMembers::Corpse:
Dest.DWord = s_settings.m_GMCorpseEnabled.Read();
Dest.Type = pBoolType;
return true;
case GMCheckMembers::Quiet:
Dest.DWord = s_settings.m_GMQuietEnabled.Read();
Dest.Type = pBoolType;
return true;
case GMCheckMembers::Interval:
Dest.Int = s_settings.GetReminderInterval();
Dest.Type = pIntType;
return true;
case GMCheckMembers::Enter:
strcpy_s(DataTypeTemp, s_settings.Sound_GMEnter.string().c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::Leave:
strcpy_s(DataTypeTemp, s_settings.Sound_GMLeave.string().c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::Remind:
strcpy_s(DataTypeTemp, s_settings.Sound_GMRemind.string().c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::ExcludeZones:
Dest.DWord = s_settings.m_ExcludeZonesEnabled.Read();
Dest.Type = pBoolType;
return true;
case GMCheckMembers::LastGMName:
strcpy_s(DataTypeTemp, gmTrack->LastGMName.c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::LastGMTime:
strcpy_s(DataTypeTemp, gmTrack->LastGMTime.c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::LastGMDate:
strcpy_s(DataTypeTemp, gmTrack->LastGMDate.c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::LastGMZone:
strcpy_s(DataTypeTemp, gmTrack->LastGMZone.c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::GMEnterCmd:
strcpy_s(DataTypeTemp, s_settings.szGMEnterCmd.c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::GMEnterCmdIf:
if (MCEval(s_settings.szGMEnterCmdIf.c_str()))
strcpy_s(DataTypeTemp, "TRUE");
else
strcpy_s(DataTypeTemp, "FALSE");
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::GMLeaveCmd:
strcpy_s(DataTypeTemp, s_settings.szGMLeaveCmd.c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::GMLeaveCmdIf:
if (MCEval(s_settings.szGMLeaveCmdIf.c_str()))
strcpy_s(DataTypeTemp, "TRUE");
else
strcpy_s(DataTypeTemp, "FALSE");
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
case GMCheckMembers::ExcludeZoneList:
strcpy_s(DataTypeTemp, s_settings.szExcludeZones.c_str());
Dest.Ptr = &DataTypeTemp[0];
Dest.Type = pStringType;
return true;
}
return false;
}
virtual bool ToString(MQVarPtr VarPtr, char* Destination) override
{
strcpy_s(Destination, MAX_STRING, gmTrack->GMNames.empty() ? "FALSE" : "TRUE");
return true;
}
static bool dataGMCheck(const char* szName, MQTypeVar& Ret)
{
Ret.DWord = 1;
Ret.Type = pGMCheckType;
return true;
}
};
static void GMCheckStatus(bool MentionHelp = false)
{
WriteChatf("\at%s \agv%1.2f", mqplugin::PluginName, MQ2Version);
char szTemp[MAX_STRING] = { 0 };
if (s_settings.GetReminderInterval())
sprintf_s(szTemp, "\ag%u \atsecs", s_settings.GetReminderInterval());
else
strcpy_s(szTemp, "\arDisabled");
WriteChatf("%s\ar- \atGM Check is: %s \at(Chat: %s \at- Sound: %s \at- Beep: %s \at- Popup: %s \at- Corpses: %s \at- Exclude: \ag%s\at) - Reminder Interval: %s",
PluginMsg,
s_settings.m_GMCheckEnabled.Read() ? "\agON" : "\arOFF",
s_settings.m_GMChatAlertEnabled.Read() ? "\agON" : "\arOFF",
s_settings.m_GMSoundEnabled.Read() ? "\agON" : "\arOFF",
s_settings.m_GMBeepEnabled.Read() ? "\agON" : "\arOFF",
s_settings.m_GMPopupEnabled.Read() ? "\agON" : "\arOFF",
s_settings.m_GMCorpseEnabled.Read() ? "\agINCLUDED" : "\ayIGNORED",
s_settings.m_ExcludeZonesEnabled.Read() ? s_settings.szExcludeZones.c_str() : "\arOFF",
szTemp);
if (MentionHelp)
WriteChatf("%s\ayUse '/gmcheck help' for command help", PluginMsg);
}
static void PlayErrorSound(const char* sound = "SystemDefault")
{
PlaySound(nullptr, nullptr, SND_NODEFAULT);
PlaySound(sound, nullptr, SND_ALIAS | SND_ASYNC | SND_NODEFAULT);
}
static void StopGMSound()
{
mciSendString("Close mySound", nullptr, 0, nullptr);
}
static void PlayGMSound(const std::filesystem::path& sound_file)
{
StopGMSound();
std::error_code ec;
if (!exists(sound_file, ec))
{
WriteChatf("%s\atERROR - Sound file not found: \am%s", PluginMsg, sound_file.string().c_str());
}
else
{
if (!bVolSet)
{
if (waveOutGetVolume(nullptr, &dwVolume) == MMSYSERR_NOERROR)
{
bVolSet = true;
waveOutSetVolume(nullptr, NewVol);
}
}
std::string sound_open;
if (sound_file.extension() == ".mp3")
{
sound_open = "MPEGVideo";
}
else if (sound_file.extension() == ".wav")
{
sound_open = "waveaudio";
}
if (sound_open.empty())
{
WriteChatf("%s\atERROR - Sound file not supported: \am%s", PluginMsg, sound_file.string().c_str());
}
else
{
sound_open = fmt::format("Open \"{}\" type {} Alias mySound", absolute(sound_file, ec).string(), sound_open);
int mci_error = mciSendString(sound_open.c_str(), nullptr, 0, nullptr);
if (mci_error == 0)
{
char szMsg[MAX_STRING] = { 0 };
mci_error = mciSendString("status mySound length", szMsg, MAX_STRING, nullptr);
if (mci_error == 0)
{
const int i = std::clamp(GetIntFromString(szMsg, 0), 0, 9000);
StopSoundTimer = MQGetTickCount64() + i;
const std::string play_command = fmt::format("play mySound from 0 {}notify", i < 9000 ? "" : "to 9000 ");
mci_error = mciSendString(play_command.c_str(), nullptr, 0, nullptr);
if (mci_error == 0)
return;
WriteChatf("%s\atERROR - Something went wrong playing: \am%s\ax", PluginMsg, sound_file.string().c_str());
}
else
{
WriteChatf("%s\atERROR - Something went wrong checking length of: \am%s\ax", PluginMsg, sound_file.string().c_str());
}
}
else
{
WriteChatf("%s\atERROR - Something went wrong opening: \am%s", PluginMsg, sound_file.string().c_str());
}
}
}
PlayErrorSound();
StopSoundTimer = MQGetTickCount64() + 1000;
}
static const char* DisplayDT(const char* Format)
{
static char CurrentDT[MAX_STRING] = { 0 };
struct tm currentDT;
time_t long_dt;
CurrentDT[0] = 0;
time(&long_dt);
localtime_s(¤tDT, &long_dt);
strftime(CurrentDT, MAX_STRING - 1, Format, ¤tDT);
return(CurrentDT);
}
static void GMReminder(char* szLine)
{
char Interval[MAX_STRING];
GetArg(Interval, szLine, 1);
if (Interval[0] == 0)
{
WriteChatf("%s\aw: Usage is /gmcheck rem VALUE (where value is num of seconds to set reminder to, min 10 secs - or 0 to disable)", PluginMsg);
return;
}
s_settings.SetReminderInterval(GetIntFromString(Interval, 0));
if (s_settings.GetReminderInterval() < 10 && s_settings.GetReminderInterval())
s_settings.SetReminderInterval(10);
if (s_settings.GetReminderInterval())
WriteChatf("%s\aw: Reminder interval set to \ar%u \awseconds.", PluginMsg, s_settings.GetReminderInterval());
else
WriteChatf("%s\aw: Reminder interval set to \ar%u \awseconds (\arDISABLED\aw).", PluginMsg, s_settings.GetReminderInterval());
}
static void GMQuiet(char* szLine)
{
char szArg[MAX_STRING];
GetArg(szArg, szLine, 1);
if (!szArg[0])
s_settings.m_GMQuietEnabled.Write(FlagOptions::Toggle);
else if (!_strnicmp(szArg, "on", 2))
s_settings.m_GMQuietEnabled.Write(FlagOptions::On);
else if (!_strnicmp(szArg, "off", 3))
s_settings.m_GMQuietEnabled.Write(FlagOptions::Off);
}
static void TrackGMs(const char* GMName)
{
char szSection[MAX_STRING] = { 0 };
char szTemp[MAX_STRING] = { 0 };
int iCount = 0;
char szTime[MAX_STRING] = { 0 };
sprintf_s(szTime, "Date: %s Time: %s", DisplayDT("%m-%d-%y"), DisplayDT("%I:%M:%S %p"));
// Store total GM count regardless of server
strcpy_s(szSection, "GM");
iCount = GetPrivateProfileInt(szSection, GMName, 0, INIFileName) + 1;
sprintf_s(szTemp, "%d,%s,%s", iCount, GetServerShortName(), szTime);
WritePrivateProfileString(szSection, GMName, szTemp, INIFileName);
// Store GM count by Server
sprintf_s(szSection, "%s", GetServerShortName());
iCount = GetPrivateProfileInt(szSection, GMName, 0, INIFileName) + 1;
sprintf_s(szTemp, "%d,%s", iCount, szTime);
WritePrivateProfileString(szSection, GMName, szTemp, INIFileName);
// Store GM count by Server-Zone
sprintf_s(szSection, "%s-%s", GetServerShortName(), pZoneInfo->LongName);
iCount = GetPrivateProfileInt(szSection, GMName, 0, INIFileName) + 1;
sprintf_s(szTemp, "%d,%s", iCount, szTime);
WritePrivateProfileString(szSection, GMName, szTemp, INIFileName);
}
static void DoGMAlert(const char* gm_name, GMStatuses status, bool test)
{
char szMsg[MAX_STRING] = { 0 };
std::filesystem::path sound_to_play;
int overlay_color = CONCOLOR_RED;
std::string beep_sound = "SystemDefault";