-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOfflineRaidProtection.cs
More file actions
13681 lines (11605 loc) · 461 KB
/
Copy pathOfflineRaidProtection.cs
File metadata and controls
13681 lines (11605 loc) · 461 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if CARBON
using Carbon.Components;
using Carbon.Extensions;
using Carbon.Plugins.OfflineRaidProtectionEx;
using System.Runtime.InteropServices;
#if CARBON && !MINIMAL
using Carbon.Modules;
#endif
#else
using Facepunch.Extend;
using Oxide.Game.Rust.Cui;
using Oxide.Plugins.OfflineRaidProtectionEx;
#endif
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Oxide.Core;
using Oxide.Core.Libraries;
using Oxide.Core.Plugins;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using PluginTimer = Oxide.Plugins.Timer;
namespace
#if CARBON
Carbon.Plugins
#else
Oxide.Plugins
#endif
{
[Info("Offline Raid Protection", "realedwin/HunterZ", "1.7.2"), Description("Prevents/reduces offline raids by other players")]
public sealed class OfflineRaidProtection :
#if CARBON
CarbonPlugin
#else
RustPlugin
#endif
{
#region Fields
[PluginReference] private Plugin Clans;
private static OfflineRaidProtection Instance { get; set; }
private static ConfigData Configuration { get; set; }
private readonly Dictionary<ulong, LastOnlineData> _lastOnline = new();
private readonly Dictionary<ulong, PlayerScaleCache> _scaleCache = new();
private readonly Dictionary<string, List<ulong>> _clanMemberCache = new();
private readonly Dictionary<ulong, string> _clanTagCache = new();
private readonly Dictionary<uint, bool> _prefabProtection = new();
private readonly Dictionary<uint, float> _prefabProtectionMultipliers = new();
private readonly Dictionary<uint, TcState> _tcCache = new();
private readonly Dictionary<uint, CodeLockWhitelistIndex> _codeLockWhitelistCache = new();
private readonly Dictionary<ulong, uint> _codeLockBuildingIds = new();
private readonly List<CodeLock> _queuedSpawnedCodeLocks = new();
private readonly Queue<uint> _queuedTcCacheRefreshes = new();
private readonly HashSet<uint> _queuedTcCacheRefreshIds = new();
private readonly List<uint> _tcCacheRefreshScratch = new();
private readonly Dictionary<ulong, TcCreationData> _tcCreationData = new();
private readonly HashSet<ulong> _griefCupboardIds = new();
private readonly HashSet<ulong> _adminIDCache = new();
private readonly PlayerRuntimeIndex _players = new();
private readonly DamageScratchSlot _damageScratch = new();
private readonly PlayerIdSet _relatedPlayersScratch = new();
private readonly PlayerIdSet _teamMembersScratch = new();
private bool _dataDirty;
private bool _saveQueued;
private bool _serverInitialized;
private bool _spawnedCodeLocksQueued;
private System.Action _processQueuedSpawnedCodeLocksAction;
private bool _tcCacheRefreshQueued;
private System.Action _processQueuedTcCacheRefreshesAction;
private static readonly object
BoxedFalse = false,
BoxedTrue = true;
private System.TimeZoneInfo _timeZone;
#region Temp
private readonly StringBuilder _sb = new(2048);
private readonly HashSet<ulong> _tmpIdsScratch = new();
private readonly PlayerIdSet _tmpIdSetScratch = new();
#endregion Temp
#region Constants
private const string
ORP_PREFIX = "[ORP] ",
ORP_PREFIX_COLORED = "<color=" + COLOR_BLUE + ">" + ORP_PREFIX + "</color>",
COMMAND_SHOWTOAST = "gametip.showtoast",
#if !CARBON
LANG_MESSAGE_NOPERMISSION = "You don't have the permission to use this command",
#endif
LANG_PROTECTION_MESSAGE_BUILDING = "Protection Message Building",
LANG_PROTECTION_MESSAGE_VEHICLE = "Protection Message Vehicle",
MESSAGE_INVALID_SYNTAX = "Invalid Syntax",
MESSAGE_PLAYER_NOT_FOUND = "No player found",
FALLBACK_MESSAGE =
"No profile is active; using configuration values.",
EMPTY_FALLBACK_MESSAGE =
"No profile is active; configuration values are empty.",
TEXT_CLAN_MEMBER = "Clan Members",
TEXT_TEAM_MEMBER = "Team Members";
#region Colors
private const string
COLOR_AQUA = "#1ABC9C",
COLOR_BLUE = "#3498DB",
COLOR_DARK_GREEN = "#1F8B4C",
COLOR_GREEN = "#57F287",
COLOR_ORANGE = "#E67E22",
COLOR_RED = "#ED4245",
COLOR_WHITE = "#FFFFFF",
COLOR_YELLOW = "#FFFF00";
#endregion Colors
#endregion Constants
#endregion Fields
#region Classes
private sealed class ConfigData
{
[JsonProperty(PropertyName = "Raid Protection Options")]
public RaidProtectionOptions RaidProtection { get; set; }
[JsonProperty(PropertyName = "Tax Protection Options")]
public TaxProtectionOptions TaxProtection { get; set; }
[JsonProperty(PropertyName = "Apartment Complex Options")]
public ApartmentOptions ApartmentProtection { get; set; }
[JsonProperty(PropertyName = "Team Options")]
public TeamOptions Team { get; set; }
[JsonProperty(PropertyName = "Command Options")]
public CommandOptions Command { get; set; }
[JsonProperty(PropertyName = "Permission Options")]
public PermissionOptions Permission { get; set; }
[JsonProperty(PropertyName = "Other Options")]
public OtherOptions Other { get; set; }
[JsonProperty(PropertyName = "Timezone Options")]
public TimeZoneOptions TimeZone { get; set; }
[JsonProperty(PropertyName = "Status HUD Options")]
public StatusHudOptions StatusHud { get; set; }
[JsonProperty(PropertyName = "Map Marker Options")]
public MapMarkerOptions MapMarker { get; set; }
public VersionNumber Version { get; set; }
public sealed class RaidProtectionOptions
{
[JsonProperty(PropertyName = "Only mitigate damage caused by players")]
public bool OnlyPlayerDamage { get; set; }
[JsonProperty(PropertyName = "Protect players that are online")]
public bool OnlineRaidProtection { get; set; }
[JsonProperty(PropertyName = "Enable scheduled timescales")]
public bool EnableScheduledTimescales { get; set; }
[JsonProperty(PropertyName = "Scale of damage depending on the current hour of the real day")]
public Dictionary<int, float> AbsoluteTimeScale { get; set; }
[JsonProperty(PropertyName = "Scale of damage depending on the offline time in hours")]
public Dictionary<float, float> DamageScale { get; set; }
[JsonProperty(PropertyName = "Cooldown in minutes")]
public int CooldownMinutes { get; set; }
[JsonProperty(PropertyName = "Online time to qualify for offline raid protection in minutes")]
public int CooldownQualifyMinutes { get; set; }
[JsonProperty(PropertyName = "Scale of damage between the cooldown and the first configured time")]
public float InterimDamage { get; set; }
[JsonProperty(PropertyName = "Protect all prefabs")]
public bool ProtectAll { get; set; }
[JsonProperty(PropertyName = "Protect AI (animals, NPCs, Bradley and attack helicopters etc.) if 'Protect all Prefabs' is enabled")]
public bool ProtectAi { get; set; }
[JsonProperty(PropertyName = "Protect modular and tug boats")]
public bool ProtectBaseBoats { get; set; }
[JsonProperty(PropertyName = "Protect vehicles")]
public bool ProtectVehicles { get; set; }
[JsonProperty(PropertyName = "Protect twigs")]
public bool ProtectTwigs { get; set; }
[JsonProperty(PropertyName = "Protect decaying buildings")]
public bool ProtectDecayingBase { get; set; }
[JsonProperty(PropertyName = "Ignore wood decay if only due to twig")]
public bool DecayIgnoreTwig { get; set; }
[JsonProperty(PropertyName = "Protect grief TCs")]
public bool ProtectGriefTcs { get; set; }
[JsonProperty(PropertyName = "Prefabs to protect")]
public HashSet<string> Prefabs { get; set; }
[JsonProperty(PropertyName = "Prefabs blacklist")]
public HashSet<string> PrefabsBlacklist { get; set; }
[JsonProperty(PropertyName = "Protection multipliers by prefab")]
public Dictionary<string, float> PrefabProtectionMultipliers { get; set; }
}
public sealed class TaxProtectionOptions
{
[JsonProperty(PropertyName = "Enabled")]
public bool Enabled { get; set; }
[JsonProperty(PropertyName = "Currency item ID")]
public int CurrencyItemID { get; set; }
[JsonProperty(PropertyName = "Cost per hour")]
public int CostPerHour { get; set; }
[JsonProperty(PropertyName = "Refund unused tax protection on Tool Cupboard destruction")]
public bool RefundOnDestruction { get; set; }
[JsonIgnore]
private int _maxCurrencyReserves;
[JsonProperty(PropertyName = "Maximum tax currency reserves per Tool Cupboard")]
public int MaxCurrencyReserves
{
get => _maxCurrencyReserves;
set => _maxCurrencyReserves = System.Math.Max(0, value);
}
[JsonIgnore]
private int _maxPurchaseHours;
[JsonProperty(PropertyName = "Maximum total purchased protection hours")]
public int MaxPurchaseHours
{
get => _maxPurchaseHours;
set => _maxPurchaseHours = System.Math.Max(1, System.Math.Min(value,
(int)(System.DateTime.MaxValue.Ticks /
System.TimeSpan.TicksPerHour)));
}
[JsonProperty(PropertyName = "Tax Overlay Options")]
public TaxOverlayOptions TaxOverlay { get; set; } = new();
public sealed class TaxOverlayOptions
{
[JsonProperty(PropertyName = "Enabled")]
public bool Enabled { get; set; }
[JsonProperty(PropertyName = "Anchor minimum")]
public string AnchorMin { get; set; }
[JsonProperty(PropertyName = "Anchor maximum")]
public string AnchorMax { get; set; }
[JsonProperty(PropertyName = "Offset minimum")]
public string OffsetMin { get; set; }
[JsonProperty(PropertyName = "Offset maximum")]
public string OffsetMax { get; set; }
}
}
public sealed class ApartmentOptions
{
[JsonProperty(PropertyName = "Protect apartments from break-ins")]
public bool ProtectApartments { get; set; }
[JsonProperty(PropertyName = "Protect apartment even when owner absent")]
public bool WhenAbsent { get; set; }
[JsonProperty(PropertyName = "Protect apartment even when rent due")]
public bool WhenRentDue { get; set; }
[JsonProperty(PropertyName = "Protect shops from break-ins")]
public bool ProtectShops { get; set; }
[JsonProperty(PropertyName = "Protect only when damage scale below")]
public float WhenDamageBelow { get; set; }
[JsonProperty(PropertyName = "Use damage scale as break-in success chance")]
public bool DamageAsChance { get; set; }
}
public sealed class TeamOptions
{
[JsonProperty(PropertyName = "Enable team offline protection sharing")]
public bool TeamShare { get; set; }
[JsonProperty(PropertyName = "Mitigate damage by the team-mate who was offline the longest")]
public bool TeamFirstOffline { get; set; }
[JsonProperty(PropertyName = "Include players that are whitelisted on Codelocks")]
public bool IncludeWhitelistPlayers { get; set; }
[JsonProperty(PropertyName = "Prevent players from leaving or disbanding their team if at least one team member is offline")]
public bool TeamAvoidAbuse { get; set; }
[JsonProperty(PropertyName = "Enable offline raid protection penalty for leaving or disbanding a team")]
public bool TeamEnablePenalty { get; set; }
[JsonProperty(PropertyName = "Penalty duration in hours")]
public float TeamPenaltyDuration { get; set; }
}
public sealed class CommandOptions
{
[JsonProperty(PropertyName = "Commands to check offline protection status")]
public string[] Commands { get; set; }
[JsonProperty(PropertyName = "Command to display offline raid protection information")]
public string CommandHelp { get; set; }
[JsonProperty(PropertyName = "Command to fill the offline times of all players")]
public string CommandFillOnlineTimes { get; set; }
[JsonProperty(PropertyName = "Command to update the permission status for all players.")]
public string CommandUpdatePermissions { get; set; }
[JsonProperty(PropertyName = "Command to change a player's offline time")]
public string CommandTestOffline { get; set; }
[JsonProperty(PropertyName = "Command to change a player's offline time to the current time")]
public string CommandTestOnline { get; set; }
[JsonProperty(PropertyName = "Command to change a player's penalty duration")]
public string CommandTestPenalty { get; set; }
[JsonProperty(PropertyName = "Command to toggle a TC's forced grief status")]
public string CommandTestGrief { get; set; } = "orp.test.grief";
[JsonProperty(PropertyName = "Command to edit scheduled timescale profiles")]
public string CommandScheduledTimescales { get; set; } = "orp.schedule";
[JsonProperty(PropertyName = "Command to update the Prefabs to protect list")]
public string CommandUpdatePrefabList { get; set; }
[JsonProperty(PropertyName = "Command to dump the Prefabs to protect list")]
public string CommandDumpPrefabList { get; set; }
[JsonProperty(PropertyName = "Command to manually manage tax protection")]
public string CommandTaxProtection { get; set; } = "orp.tax";
#if CARBON
[JsonProperty(PropertyName = "Command cooldown in seconds")]
public int CommandCooldown
{
get;
set => field = System.Math.Max(0, value);
}
#endif
internal void RegisterCommands(Plugin plugin, OfflineRaidProtection offlineRaidProtection)
{
RegisterChatCommands(Commands, plugin, offlineRaidProtection.cmdStatus, Configuration.Permission.Check);
RegisterChatCommands(new[] {CommandHelp}, plugin, offlineRaidProtection.cmdHelp, Configuration.Permission.Protect);
RegisterChatCommands(new[] {CommandFillOnlineTimes}, plugin, offlineRaidProtection.cmdFillOnlineTimes, Configuration.Permission.Admin);
RegisterChatCommands(new[] {CommandTestOffline}, plugin, offlineRaidProtection.cmdTestOffline, Configuration.Permission.Admin);
RegisterChatCommands(new[] {CommandTestOnline}, plugin, offlineRaidProtection.cmdTestOnline, Configuration.Permission.Admin);
RegisterChatCommands(new[] {CommandTestPenalty}, plugin, offlineRaidProtection.cmdTestPenalty, Configuration.Permission.Admin);
RegisterChatCommands(new[] {CommandTestGrief}, plugin, offlineRaidProtection.cmdTestGrief, Configuration.Permission.Admin);
RegisterChatCommands(new[] {CommandScheduledTimescales}, plugin, offlineRaidProtection.cmdScheduledTimescales, Configuration.Permission.Admin);
RegisterChatCommands(new[] {CommandTaxProtection}, plugin, offlineRaidProtection.cmdBuyTaxProtection, Configuration.Permission.TaxProtection);
RegisterConsoleCommands(new[] {CommandFillOnlineTimes}, plugin, nameof(Instance.ccFillOnlineTimes), Configuration.Permission.Admin);
RegisterConsoleCommands(new[] {CommandUpdatePermissions}, plugin, nameof(Instance.ccUpdatePermissions), Configuration.Permission.Admin);
RegisterConsoleCommands(new[] {CommandUpdatePrefabList}, plugin, nameof(Instance.ccUpdatePrefabList), Configuration.Permission.Admin);
RegisterConsoleCommands(new[] {CommandDumpPrefabList}, plugin, nameof(Instance.ccDumpPrefabList), Configuration.Permission.Admin);
#if !CARBON
RegisterConsoleCommands(new[] {TAX_OVERLAY_COMMAND}, plugin, nameof(Instance.ccTaxOverlay), string.Empty);
#endif
}
private void RegisterChatCommands(string[] commands, Plugin plugin, System.Action<BasePlayer, string, string[]> callback, string permission)
{
foreach (var command in commands)
{
if (string.IsNullOrEmpty(command))
continue;
#if CARBON
if (string.IsNullOrEmpty(permission))
Community.Runtime.Core.cmd.AddChatCommand(
command, plugin, callback,
cooldown: CommandCooldown * 1000);
else
Community.Runtime.Core.cmd.AddChatCommand(
command, plugin, callback,
cooldown: CommandCooldown * 1000,
permissions: [permission]);
#else
Instance.cmd.AddChatCommand(command, plugin, callback);
#endif
}
}
private void RegisterConsoleCommands(string[] commands, Plugin plugin, string callback, string permission)
{
foreach (var command in commands)
{
if (string.IsNullOrEmpty(command))
continue;
#if CARBON
if (string.IsNullOrEmpty(permission))
Community.Runtime.Core.cmd.AddConsoleCommand(
command, plugin, callback,
cooldown: CommandCooldown * 1000);
else
Community.Runtime.Core.cmd.AddConsoleCommand(
command, plugin, callback,
cooldown: CommandCooldown * 1000,
permissions: [permission]);
#else
Instance.cmd.AddConsoleCommand(command, plugin, callback);
#endif
}
}
}
public sealed class PermissionOptions
{
[JsonProperty(PropertyName = "Permission required to enable offline protection")]
public string Protect { get; set; }
[JsonProperty(PropertyName = "Permission required to check offline protection status")]
public string Check { get; set; }
[JsonProperty(PropertyName = "Permission required to use admin functions")]
public string Admin { get; set; }
[JsonProperty(PropertyName = "Permission required to manage tax protection")]
public string TaxProtection { get; set; }
[JsonProperty(PropertyName = "Permission to force online protection for specific players")]
public string OnlineProtect { get; set; }
internal void RegisterPermissions(Permission permission, Plugin plugin)
{
string[] permissions = {Protect, Check, Admin, TaxProtection, OnlineProtect};
foreach (var perm in permissions)
{
if (!string.IsNullOrEmpty(perm))
permission.RegisterPermission(perm, plugin);
}
}
}
public sealed class OtherOptions
{
[JsonProperty(PropertyName = "Play sound when damage is mitigated")]
public bool PlaySound { get; set; }
[JsonProperty(PropertyName = "Asset path of the sound to be played")]
public string SoundPath { get; set; }
[JsonProperty(PropertyName = "Display a game tip message when a prefab is protected")]
public bool ShowMessage { get; set; }
[JsonProperty(PropertyName = "Weapon categories that trigger game tip messages")]
public HashSet<GameTipWeaponCategory> GameTipWeaponCategories { get; set; }
[JsonProperty(PropertyName = "Game tip message shows remaining protection time")]
public bool ShowRemainingTime { get; set; }
[JsonProperty(PropertyName = "Message duration in seconds")]
public float MessageDuration { get; set; }
}
public sealed class TimeZoneOptions
{
#if CARBON
[JsonProperty(PropertyName = "Timezone for Windows")]
public string WinTimeZone { get; set; }
[JsonProperty(PropertyName = "Timezone for Linux")]
public string UnixTimeZone { get; set; }
#else
[JsonProperty(PropertyName = "Timezone")]
public string TimeZone { get; set; }
#endif
}
public sealed class StatusHudOptions
{
[JsonProperty(PropertyName = "Enabled")]
public bool Enabled { get; set; }
[JsonProperty(PropertyName = "Anchor minimum")]
public string AnchorMin { get; set; }
[JsonProperty(PropertyName = "Anchor maximum")]
public string AnchorMax { get; set; }
[JsonProperty(PropertyName = "Offset minimum")]
public string OffsetMin { get; set; }
[JsonProperty(PropertyName = "Offset maximum")]
public string OffsetMax { get; set; }
[JsonProperty(PropertyName = "Refresh interval in seconds")]
public float RefreshInterval { get; set; }
[JsonProperty(PropertyName = "Display inside trusted privilege")]
public bool DisplayInTrustedPrivilege { get; set; }
[JsonProperty(PropertyName = "Display only when protection is active")]
public bool DisplayOnlyWhenProtectionActive { get; set; }
[JsonProperty(PropertyName = "Display after status command")]
public bool DisplayOnStatusCommand { get; set; }
[JsonProperty(PropertyName = "Status command display duration in seconds")]
public float Duration { get; set; }
[JsonProperty(PropertyName = "Show protection percentage")]
public bool ShowProtectionPercentage { get; set; }
[JsonProperty(PropertyName = "Show remaining protection time")]
public bool ShowRemainingTime { get; set; }
[JsonProperty(PropertyName = "Show penalty timer")]
public bool ShowPenaltyTimer { get; set; }
}
public sealed class MapMarkerOptions
{
[JsonProperty(PropertyName = "Enabled")]
public bool Enabled { get; set; }
[JsonProperty(PropertyName = "Refresh interval in seconds")]
public float RefreshInterval { get; set; }
[JsonProperty(PropertyName = "Enable boat live circle")]
public bool EnableBoatLiveCircle { get; set; }
[JsonProperty(PropertyName = "Visual radius in metres")]
public float Radius { get; set; }
[JsonProperty(PropertyName = "Alpha")]
public float Alpha { get; set; }
[JsonProperty(PropertyName = "Protected color")]
public string ProtectedColor { get; set; }
[JsonProperty(PropertyName = "Partial protection color")]
public string PartialColor { get; set; }
[JsonProperty(PropertyName = "Vulnerable color")]
public string VulnerableColor { get; set; }
[JsonProperty(PropertyName = "Decaying color")]
public string DecayingColor { get; set; }
[JsonProperty(PropertyName = "Grief color")]
public string GriefColor { get; set; }
[JsonProperty(PropertyName = "Outline color")]
public string OutlineColor { get; set; }
[JsonProperty(PropertyName = "Tooltip marker text max. players")]
public int TooltipMaxPlayers { get; set; }
}
}
private sealed class LastOnlineData
{
private long _lastOnline;
private long _penaltyEnd;
private long _lastConnect;
[JsonProperty(PropertyName = "User ID")]
public ulong UserID { get; set; }
[JsonProperty(PropertyName = "User Name")]
public string UserName { get; set; }
[JsonProperty(PropertyName = "Last Online")]
public long LastOnline
{
get => _lastOnline;
set
{
_lastOnline = value;
LastOnlineTicks = value & 0x3FFFFFFFFFFFFFFF; // Clear top 2 bits (Kind flags)
}
}
[JsonProperty(PropertyName = "End of Penalty")]
public long PenaltyEnd
{
get => _penaltyEnd;
set
{
_penaltyEnd = value;
PenaltyEndTicks = value;
}
}
[JsonProperty(PropertyName = "Last Connect")]
public long LastConnect
{
get => _lastConnect;
set
{
_lastConnect = value;
LastConnectTicks = value & 0x3FFFFFFFFFFFFFFF; // Clear top 2 bits (Kind flags)
}
}
[JsonIgnore]
public long LastOnlineTicks { get; private set; }
[JsonIgnore]
public long PenaltyEndTicks { get; private set; }
[JsonIgnore]
public long LastConnectTicks { get; private set; }
[JsonIgnore]
public System.DateTime LastOnlineDT
{
get => System.DateTime.FromBinary(LastOnline);
set => LastOnline = value.ToBinary();
}
[JsonIgnore]
public System.DateTime PenaltyEndDT
{
get => new(PenaltyEndTicks);
private set => PenaltyEnd = value.Ticks;
}
[JsonIgnore]
public System.DateTime LastConnectDT
{
get => System.DateTime.FromBinary(LastConnect);
set => LastConnect = value.ToBinary();
}
[JsonConstructor]
public LastOnlineData(
ulong userid, string userName, long lastOnline,
long lastConnect)
{
UserID = userid;
UserName = userName;
LastOnline = lastOnline;
LastConnect = lastConnect;
}
public LastOnlineData(
BasePlayer player, System.DateTime currentTime,
bool connected = false) :
this(player.userID.Get(), player.displayName, 0, 0)
{
LastOnlineDT = currentTime;
LastConnectDT = connected ? currentTime : LastConnectDT;
}
public void EnablePenalty(System.DateTime penaltyEndUtc) =>
PenaltyEndDT = penaltyEndUtc;
public void DisablePenalty() => PenaltyEnd = 0L;
public void RefreshRuntimeTicks()
{
LastOnlineTicks = System.DateTime.FromBinary(LastOnline).Ticks;
LastConnectTicks = System.DateTime.FromBinary(LastConnect).Ticks;
PenaltyEndTicks = PenaltyEnd;
}
}
private sealed class PlayerScaleCache
{
public float Scale { get; set; }
public long ExpiresTicks { get; set; }
public bool ActiveGameTipMessage { get; set; }
public System.TimeSpan RemainingTime { get; set; }
public bool HasProtectPermission { get; set; }
public bool HasTaxPermission { get; set; }
public bool HasOnlineProtectPermission { get; set; }
public System.Action HideGameTipAction { get; }
public string ProtectionMessageBuilding { get; private set; }
public string ProtectionMessageVehicle { get; private set; }
public PlayerScaleCache(
System.DateTime expires, float scale, bool hasProtectPermission,
bool hasTaxPermission, bool hasOnlineProtectPermission)
{
ExpiresDT = expires;
Scale = scale;
ActiveGameTipMessage = false;
HasProtectPermission = hasProtectPermission;
HasTaxPermission = hasTaxPermission;
HasOnlineProtectPermission = hasOnlineProtectPermission;
HideGameTipAction = HideGameTip;
}
public System.DateTime ExpiresDT
{
// get => new(Expires);
set => ExpiresTicks = value.Ticks;
}
private void HideGameTip() => ActiveGameTipMessage = false;
public void CacheMessages(
OfflineRaidProtection plugin, string userID)
{
ProtectionMessageBuilding =
PrefixMessage(plugin.Msg(LANG_PROTECTION_MESSAGE_BUILDING, userID), true);
ProtectionMessageVehicle =
PrefixMessage(plugin.Msg(LANG_PROTECTION_MESSAGE_VEHICLE, userID), true);
}
}
private sealed class PlayerRuntimeIndex
{
// Runtime-only player lookup to avoid touching global player managers in
// the hot path; kept instance-bound so connect/disconnect state stays local
private readonly Dictionary<ulong, BasePlayer> _playersByUserID = new();
private readonly Dictionary<string, BasePlayer> _playersByName = new();
public void AddPlayer(BasePlayer player)
{
if (!player)
return;
_playersByUserID[player.userID.Get()] = player;
if (!string.IsNullOrEmpty(player.displayName))
_playersByName[player.displayName] = player;
}
public void UpdateName(
ulong userID, string oldName, string newName)
{
if (!string.IsNullOrEmpty(oldName))
_playersByName.Remove(oldName);
if (!_playersByUserID.TryGetValue(userID, out var player))
return;
if (!string.IsNullOrEmpty(newName))
_playersByName[newName] = player;
}
public BasePlayer GetPlayer(ulong userID) =>
_playersByUserID.GetValueOrDefault(userID, null);
public BasePlayer GetPlayer(string displayName)
{
if (string.IsNullOrEmpty(displayName))
return null;
return _playersByName.TryGetValue(displayName, out var player) ||
ulong.TryParse(displayName, out var userID) &&
_playersByUserID.TryGetValue(userID, out player) ?
player : null;
}
public void Clear()
{
_playersByUserID.Clear();
_playersByName.Clear();
}
}
private sealed class PlayerIdSet : Facepunch.Pool.IPooled
{
// Fixed-capacity insertion-ordered ID set for hot-path authorization
// expansion. HashSet handles dedupe; List preserves existing iteration order
private const int Capacity = 1024;
private readonly HashSet<ulong> _lookup = new(Capacity);
private readonly List<ulong> _items = new(Capacity);
public int Count => _items.Count;
public bool Overflowed { get; private set; }
public ulong First => _items.Count > 0 ? _items[0] : 0UL;
public ulong this[int index] => _items[index];
public List<ulong> GetList() => _items;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
_items.Clear();
_lookup.Clear();
Overflowed = false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(ulong value) => _lookup.Contains(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(ulong value)
{
if (value is 0UL)
return;
if (_items.Count >= Capacity)
{
if (!_lookup.Contains(value))
Overflowed = true;
return;
}
if (!_lookup.Add(value))
return;
_items.Add(value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Remove(ulong value)
{
if (_lookup.Remove(value))
_items.Remove(value);
}
public void AddRange(HashSet<ulong> values)
{
if (values is null)
return;
foreach (var value in values)
Add(value);
}
public void AddRange(List<ulong> values)
{
if (values is null)
return;
for (var i = 0; i < values.Count; i++)
Add(values[i]);
}
public void AddRange(PlayerIdSet values)
{
if (values is null)
return;
for (var i = 0; i < values.Count; i++)
Add(values[i]);
}
public void EnterPool() => Clear();
public void LeavePool() { }
}
private sealed class CodeLockWhitelistSnapshot : Facepunch.Pool.IPooled
{
public readonly HashSet<ulong> PlayerIds = new(64);
public void EnterPool() => PlayerIds.Clear();
public void LeavePool() { }
}
private sealed class CodeLockWhitelistIndex : Facepunch.Pool.IPooled
{
public readonly Dictionary<ulong, CodeLockWhitelistSnapshot> Locks = new();
public readonly Dictionary<ulong, int> PlayerReferences = new();
public PlayerIdSet AuthorizedPlayers;
public void EnterPool()
{
foreach (var snapshot in Locks.Values)
{
var pooledSnapshot = snapshot;
Facepunch.Pool.Free(ref pooledSnapshot);
}
Locks.Clear();
PlayerReferences.Clear();
if (AuthorizedPlayers is not null)
Facepunch.Pool.Free(ref AuthorizedPlayers);
}
public void LeavePool() =>
AuthorizedPlayers = Facepunch.Pool.Get<PlayerIdSet>();
}
private sealed class DamageScratchSlot
{
// Single-owner scratch state for the damage/evaluation path. The
// evaluator clears this slot before each run to stay allocation-free
public readonly PlayerIdSet AuthorizedIds = new();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear() => AuthorizedIds.Clear();
}
private enum TcGriefState : byte
{
None,
ForceTrue,
ForceFalse
}
private sealed class TcCreationData
{
[JsonProperty(PropertyName = "Creation time")]
public long CreatedUtcTicks { get; set; }
[JsonProperty(PropertyName = "Trusted")]
public bool HasTrustedCreationTime { get; set; }
[JsonProperty(PropertyName = "Force grief status")]
public TcGriefState GriefState { get; set; } = TcGriefState.None;
}
private readonly struct TcState
{
public readonly BuildingPrivlidge Privilege;
public readonly ulong CupboardNetworkID;
public readonly bool IsDecaying;
public TcState(BuildingPrivlidge privilege, ulong cupboardNetworkID, bool isDecaying)
{
Privilege = privilege;
CupboardNetworkID = cupboardNetworkID;
IsDecaying = isDecaying;
}
}
private enum DamageDecisionKind : byte
{
Allow,
ApplyScale
}
[System.Flags]
private enum DamageDecisionFlags : byte
{
None = 0,
Vehicle = 1,
Decaying = 2,
Grief = 4
}
private readonly struct DamageDecision
{
// Decision object returned by the damage pipeline so the hot path can
// branch once after all evaluation has completed
public readonly DamageDecisionKind Kind;
public readonly ulong TargetID;
public readonly float Scale;
public readonly PlayerScaleCache TargetScaleCache;
public readonly long PurchasedProtectionEndTicks;
public readonly bool TaxProtectionGated;
private readonly DamageDecisionFlags _flags;
public bool IsVehicle => (_flags & DamageDecisionFlags.Vehicle) is not 0;
public bool IsDecaying => (_flags & DamageDecisionFlags.Decaying) is not 0;
public bool IsGrief => (_flags & DamageDecisionFlags.Grief) is not 0;
public DamageDecision(
DamageDecisionKind kind, ulong targetID = 0UL, float scale = -1f,
bool isVehicle = false, bool isDecaying = false, bool isGrief = false,
PlayerScaleCache targetScaleCache = null,
long purchasedProtectionEndTicks = 0L,
bool taxProtectionGated = false)
{
Kind = kind;
TargetID = targetID;
Scale = scale;