-
Notifications
You must be signed in to change notification settings - Fork 3
/
Inbound.cs
826 lines (720 loc) · 36.5 KB
/
Inbound.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using Newtonsoft.Json;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Inbound", "Substrata", "0.6.9")]
[Description("Broadcasts notifications when patrol helicopters, supply drops, cargo ships, etc. are inbound")]
class Inbound : RustPlugin
{
[PluginReference]
Plugin DiscordMessages, PopupNotifications, UINotify,
// Compatibility
AirdropPrecision, FancyDrop;
bool initialized;
float worldSize; int cellCount; float cellSize; float gridBottom; float gridTop; float gridLeft; float gridRight;
bool hasOilRig; bool hasLargeRig; Vector3 oilRigPos; Vector3 largeRigPos; bool hasExcavator; Vector3 excavatorPos;
ulong chatIconID; string webhookURL;
void OnServerInitialized(bool initial) => InitVariables();
void OnEntitySpawned(PatrolHelicopter heli)
{
if (!initialized) return;
NextTick(() =>
{
if (heli == null || heli.IsDestroyed) return;
SendInboundMessage(Lang("PatrolHeli", null, Location(heli.transform.position), Destination(heli.myAI.destination)), configData.alerts.patrolHeli);
});
}
void OnEntitySpawned(CargoShip ship)
{
if (!initialized) return;
timer.Once(3f, () =>
{
if (ship == null || ship.IsDestroyed) return;
string destination = string.Empty;
if (TerrainMeta.Path.OceanPatrolFar != null)
{
int targetNodeIndex = ship.targetNodeIndex != -1 ? ship.targetNodeIndex : ship.GetClosestNodeToUs();
if (targetNodeIndex != -1 && TerrainMeta.Path.OceanPatrolFar.Count > targetNodeIndex)
{
destination = Destination(TerrainMeta.Path.OceanPatrolFar[targetNodeIndex]);
}
}
SendInboundMessage(Lang("CargoShip_", null, Location(ship.transform.position), destination), configData.alerts.cargoShip);
});
}
void OnCargoShipHarborApproach(CargoShip ship)
{
if (!initialized || ship == null || ship.IsDestroyed) return;
SendInboundMessage(Lang("CargoShipApproachHarbor", null, GetHarborLocation(ship)), configData.alerts.cargoShipHarbor);
}
void OnCargoShipHarborArrived(CargoShip ship)
{
if (!initialized || ship == null || ship.IsDestroyed) return;
SendInboundMessage(Lang("CargoShipAtHarbor", null, GetHarborLocation(ship)), configData.alerts.cargoShipHarbor);
}
void OnCargoShipHarborLeave(CargoShip ship)
{
if (!initialized || ship == null || ship.IsDestroyed) return;
SendInboundMessage(Lang("CargoShipLeaveHarbor", null, GetHarborLocation(ship)), configData.alerts.cargoShipHarbor);
}
void OnEntitySpawned(CH47HelicopterAIController ch47)
{
if (!initialized) return;
timer.Once(2f, () =>
{
if (ch47 == null || ch47.IsDestroyed) return;
SendInboundMessage(Lang("CH47", null, Location(ch47.transform.position), Destination(ch47.GetMoveTarget())), configData.alerts.ch47 && (!configData.misc.hideRigCrates || !ch47.ShouldLand()));
});
}
void OnBradleyApcInitialize(BradleyAPC apc)
{
if (!initialized) return;
NextTick(() =>
{
if (apc == null || apc.IsDestroyed) return;
SendInboundMessage(Lang("BradleyAPC", null, Location(apc.transform.position)), configData.alerts.bradleyAPC);
});
}
void OnEntitySpawned(TravellingVendor travellingVendor)
{
if (!initialized) return;
NextTick(() =>
{
if (travellingVendor == null || travellingVendor.IsDestroyed) return;
SendInboundMessage(Lang("TravellingVendor", null, Location(travellingVendor.transform.position)), configData.alerts.travellingVendor);
});
}
void OnExcavatorResourceSet(ExcavatorArm arm, string resourceName, BasePlayer player)
{
if (!initialized) return;
if (arm == null || arm.IsOn()) return;
NextTick(() =>
{
if (player == null || arm == null || arm.IsDestroyed || !arm.IsOn()) return;
SendInboundMessage(Lang("Excavator_", null, player.displayName, Location(arm.transform.position, null, true)), configData.alerts.excavator);
});
}
void OnEntitySpawned(HackableLockedCrate crate)
{
if (!initialized) return;
NextTick(() =>
{
if (crate == null || crate.IsDestroyed) return;
SendInboundMessage(Lang("HackableCrateSpawned", null, Location(crate.transform.position, crate)), configData.alerts.hackableCrateSpawn && !HideCrateAlert(crate));
});
}
void CanHackCrate(BasePlayer player, HackableLockedCrate crate)
{
if (!initialized) return;
NextTick(() =>
{
if (player == null || crate == null || crate.IsDestroyed || !crate.IsBeingHacked()) return;
SendInboundMessage(Lang("HackingCrate", null, player.displayName, Location(crate.transform.position, crate)), configData.alerts.hackingCrate && !HideCrateAlert(crate));
});
}
// Supply Drops
private HashSet<CalledDrop> calledDrops = new HashSet<CalledDrop>();
private class CalledDrop
{
public IPlayer _iplayer = null;
public SupplySignal _signal = null;
public CargoPlane _plane = null;
public SupplyDrop _drop = null;
}
void OnExplosiveThrown(BasePlayer player, SupplySignal signal)
{
if (!initialized) return;
NextTick(() =>
{
if (player == null || signal == null) return;
if (SupplyPlayerCompatible())
calledDrops.Add(new CalledDrop() { _iplayer = player.IPlayer, _signal = signal });
SendInboundMessage(Lang("SupplySignal", null, player.displayName, Location(player.transform.position, player)), configData.alerts.supplySignal);
});
}
void OnExplosiveDropped(BasePlayer player, SupplySignal signal) => OnExplosiveThrown(player, signal);
void OnCargoPlaneSignaled(CargoPlane plane, SupplySignal signal)
{
if (plane == null || signal == null) return;
foreach (var calledDrop in calledDrops)
{
if (calledDrop._signal == signal)
{
calledDrop._plane = plane;
break;
}
}
}
void OnExcavatorSuppliesRequested(ExcavatorSignalComputer computer, BasePlayer player, CargoPlane plane)
{
if (!initialized) return;
NextTick(() =>
{
if (player == null || plane == null) return;
if (SupplyPlayerCompatible())
calledDrops.Add(new CalledDrop() { _iplayer = player.IPlayer, _plane = plane });
SendInboundMessage(Lang("ExcavatorSupplyRequest", null, player.displayName, Location(player.transform.position)), configData.alerts.excavatorSupply);
});
}
void OnAirdrop(CargoPlane plane, Vector3 dest)
{
if (!initialized) return;
timer.Once(2f, () =>
{
if (plane == null) return;
CalledDrop calledDrop = GetCalledDrop(plane, null);
SendInboundMessage(Lang("CargoPlane_", null, SupplyDropPlayer(calledDrop), Location(plane.transform.position), Destination(dest)), configData.alerts.cargoPlane && !HideSupplyAlert(calledDrop));
});
}
private HashSet<ulong> droppedDrops = new HashSet<ulong>();
void OnSupplyDropDropped(SupplyDrop drop, CargoPlane plane)
{
if (!initialized) return;
NextTick(() =>
{
if (drop == null || droppedDrops.Contains(drop.net.ID.Value)) return;
droppedDrops.Add(drop.net.ID.Value);
CalledDrop calledDrop = GetCalledDrop(plane, null);
if (calledDrop != null) calledDrop._drop = drop;
SendInboundMessage(Lang("SupplyDropDropped", null, SupplyDropPlayer(calledDrop), Location(drop.transform.position)), configData.alerts.supplyDrop && !HideSupplyAlert(calledDrop));
});
}
void OnEntitySpawned(SupplyDrop drop) => NextTick(() => OnSupplyDropDropped(drop, null));
private HashSet<ulong> landedDrops = new HashSet<ulong>();
void OnSupplyDropLanded(SupplyDrop drop)
{
if (!initialized || drop == null || landedDrops.Contains(drop.net.ID.Value)) return;
landedDrops.Add(drop.net.ID.Value);
CalledDrop calledDrop = GetCalledDrop(null, drop);
SendInboundMessage(Lang("SupplyDropLanded_", null, SupplyDropPlayer(calledDrop), Location(drop.transform.position)), configData.alerts.supplyDropLand && !HideSupplyAlert(calledDrop));
}
void OnEntityKill(SupplyDrop drop)
{
if (drop == null) return;
CalledDrop calledDrop = GetCalledDrop(null, drop);
if (calledDrop != null) calledDrops.Remove(calledDrop);
droppedDrops.Remove(drop.net.ID.Value);
landedDrops.Remove(drop.net.ID.Value);
}
#region Messages
void SendInboundMessage(string message, bool alert)
{
if (string.IsNullOrEmpty(message)) return;
string msg = Regex.Replace(message, filterTags, string.Empty);
if (alert)
{
if (configData.notifications.chat)
Server.Broadcast(message, null, chatIconID);
if (configData.notifications.popup && PopupNotifications)
PopupNotifications.Call("CreatePopupNotification", msg);
if (configData.uiNotify.enabled && UINotify)
SendUINotify(msg);
if (configData.discordMessages.enabled && DiscordMessages && webhookURL.Contains("/api/webhooks/"))
SendDiscordMessage(msg);
}
if (alert || configData.logging.allEvents)
LogInboundMessage(msg);
}
void SendUINotify(string msg)
{
foreach (var player in BasePlayer.activePlayerList)
{
if (permission.UserHasPermission(player.UserIDString, "uinotify.see"))
UINotify.Call("SendNotify", player, configData.uiNotify.type, msg);
}
}
void SendDiscordMessage(string msg)
{
string dMsg = Lang("DiscordMessage_", null, msg);
if (string.IsNullOrWhiteSpace(dMsg)) return;
if (configData.discordMessages.embedded)
{
object fields = new[]
{
new {
name = configData.discordMessages.embedTitle, value = dMsg, inline = false
}
};
string json = JsonConvert.SerializeObject(fields);
DiscordMessages.Call("API_SendFancyMessage", webhookURL, string.Empty, configData.discordMessages.embedColor, json);
}
else
DiscordMessages.Call("API_SendTextMessage", webhookURL, dMsg);
}
void LogInboundMessage(string msg)
{
if (configData.logging.console)
Puts(msg);
if (configData.logging.file)
LogToFile("log", $"[{DateTime.Now.ToString("HH:mm:ss")}] {msg}", this);
}
#endregion
#region Helpers
private string Location(Vector3 pos, BaseEntity entity = null, bool hideExc = false)
{
string location = GetLocation(pos, entity, hideExc);
return !string.IsNullOrEmpty(location) ? Lang("Location", null, location) : string.Empty;
}
private string Destination(Vector3 pos)
{
string destination = GetLocation(pos, null);
return !string.IsNullOrEmpty(destination) ? Lang("Destination", null, destination) : string.Empty;
}
private string GetLocation(Vector3 pos, BaseEntity entity, bool hideExc = false)
{
var sb = new StringBuilder();
if (configData.location.showCargoShip)
if (IsAtCargoShip(entity)) sb.Append("Cargo Ship");
if (configData.location.showOilRigs && sb.Length == 0)
{
if (IsAtOilRig(pos)) sb.Append("Oil Rig");
else if (IsAtLargeRig(pos)) sb.Append("Large Oil Rig");
}
if (configData.location.showExcavator && !hideExc && sb.Length == 0)
if (IsAtExcavator(pos)) sb.Append("The Excavator");
if (configData.location.showGrid && sb.Length == 0)
{
if (pos.x > gridLeft && (!configData.location.hideOffGrid || !IsOffGrid(pos)))
sb.Append(GetGrid(pos));
}
if (configData.location.showCoords)
{
bool hideDecimals = configData.location.hideCoordDecimals;
string x = hideDecimals ? Mathf.Round(pos.x).ToString()+"," : pos.x.ToString("0.##")+",";
string y = hideDecimals ? Mathf.Round(pos.y).ToString()+"," : pos.y.ToString("0.##")+",";
string z = hideDecimals ? Mathf.Round(pos.z).ToString() : pos.z.ToString("0.##");
if (configData.location.hideYCoord) y = string.Empty;
sb.Append(sb.Length == 0 ? x+y+z : $" ({x}{y}{z})");
}
return sb.ToString();
}
string GetHarborLocation(CargoShip cargoShip)
{
if (cargoShip.harborIndex != -1 && CargoShip.harbors.Count > cargoShip.harborIndex)
{
var currentHarbor = CargoShip.harbors[cargoShip.harborIndex];
return Location(currentHarbor.harborTransform.position);
}
return string.Empty;
}
string GetGrid(Vector3 pos)
{
int x = Mathf.FloorToInt((pos.x + (worldSize / 2)) / cellSize);
int z = Mathf.FloorToInt((pos.z + (worldSize / 2)) / cellSize);
string columnLabel = string.Empty;
int num = x / 26;
if (num > 0)
{
for (int i = 0; i < num; i++)
{
columnLabel += Convert.ToChar(65 + i);
}
}
columnLabel += Convert.ToChar(65 + (x % 26));
int row = cellCount - 1 - z;
return $"{columnLabel}{row}";
}
private CalledDrop GetCalledDrop(CargoPlane plane, SupplyDrop drop)
{
foreach (var calledDrop in calledDrops)
{
if ((plane != null && calledDrop._plane == plane) || (drop != null && calledDrop._drop == drop))
return calledDrop;
}
return null;
}
private string SupplyDropPlayer(CalledDrop calledDrop)
{
IPlayer iplayer = calledDrop != null ? calledDrop._iplayer : null;
return configData.misc.showSupplyPlayer && iplayer != null ? Lang("SupplyDropPlayer", null, iplayer.Name) : string.Empty;
}
private bool HideSupplyAlert(CalledDrop calledDrop)
{
bool playerCalled = calledDrop != null;
return SupplyPlayerCompatible() && ((configData.misc.hideCalledSupply && playerCalled) || (configData.misc.hideRandomSupply && !playerCalled));
}
private bool HideCrateAlert(HackableLockedCrate crate)
{
Vector3 pos = crate.transform.position;
return (configData.misc.hideCargoCrates && IsAtCargoShip(crate)) || (configData.misc.hideRigCrates && (IsAtOilRig(pos) || IsAtLargeRig(pos)));
}
const string filterTags = @"(?i)<\/?(align|alpha|color|cspace|indent|line-height|line-indent|margin|mark|mspace|pos|size|space|voffset|b|i|lowercase|uppercase|smallcaps|s|u|sup|sub)(\s*=[^>]*?)?\s*\/?>";
private bool IsAtOilRig(Vector3 pos) => hasOilRig && Vector3Ex.Distance2D(oilRigPos, pos) <= 60f;
private bool IsAtLargeRig(Vector3 pos) => hasLargeRig && Vector3Ex.Distance2D(largeRigPos, pos) <= 75f;
private bool IsAtCargoShip(BaseEntity entity) => entity?.GetComponentInParent<CargoShip>();
private bool IsAtExcavator(Vector3 pos) => hasExcavator && Vector3Ex.Distance2D(excavatorPos, pos) <= 145f;
private bool IsOffGrid(Vector3 pos) => pos.x < gridLeft || pos.x > gridRight || pos.z < gridBottom || pos.z > gridTop;
private bool SupplyPlayerCompatible() => !FancyDrop && !AirdropPrecision;
void InitVariables()
{
// Grid
worldSize = TerrainMeta.Size.x;
cellCount = Mathf.FloorToInt((worldSize * 7) / 1024);
cellSize = worldSize / cellCount;
gridBottom = TerrainMeta.Position.z;
gridTop = gridBottom + (cellSize * cellCount);
gridLeft = TerrainMeta.Position.x;
gridRight = gridLeft + (cellSize * cellCount);
// Monuments
foreach (MonumentInfo monument in TerrainMeta.Path.Monuments)
{
var name = monument.name;
if (name == "assets/bundled/prefabs/autospawn/monument/large/excavator_1.prefab")
{
hasExcavator = true;
excavatorPos = monument.transform.localToWorldMatrix.MultiplyPoint3x4(new Vector3(20f,0,-30f));
continue;
}
if (name == "assets/bundled/prefabs/autospawn/monument/offshore/oilrig_1.prefab")
{
hasLargeRig = true;
largeRigPos = monument.transform.position;
continue;
}
if (name == "assets/bundled/prefabs/autospawn/monument/offshore/oilrig_2.prefab")
{
hasOilRig = true;
oilRigPos = monument.transform.position;
continue;
}
}
// General & plugins
if (configData.notifications.chat && configData.misc.chatIcon != 0)
{
if (!configData.misc.chatIcon.IsSteamId())
PrintWarning("Chat Icon is not set to a valid SteamID64.");
else
chatIconID = configData.misc.chatIcon;
}
if (configData.notifications.popup && !PopupNotifications)
PrintWarning("The 'Popup Notifications' plugin could not be found.");
if (configData.discordMessages.enabled)
{
webhookURL = configData.discordMessages.webhookURL;
if (!DiscordMessages)
PrintWarning("The 'Discord Messages' plugin could not be found.");
else if (!webhookURL.Contains("/api/webhooks/"))
PrintWarning("The 'Discord Messages' Webhook URL is missing or invalid.");
}
if (configData.uiNotify.enabled && !UINotify)
PrintWarning("The 'UI Notify' plugin could not be found.");
if (!SupplyPlayerCompatible() && (configData.misc.showSupplyPlayer || configData.misc.hideCalledSupply || configData.misc.hideRandomSupply))
PrintWarning("The 'Supply Drop Player' options are not currently compatible with Fancy Drop or Aidrop Precision. Using defaults (false).");
initialized = true;
}
#endregion
#region Configuration
private ConfigData configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Notifications")]
public Notifications notifications { get; set; }
[JsonProperty(PropertyName = "Discord Messages")]
public DiscordMessages discordMessages { get; set; }
[JsonProperty(PropertyName = "UI Notify")]
public UINotify uiNotify { get; set; }
[JsonProperty(PropertyName = "Alerts")]
public Alerts alerts { get; set; }
[JsonProperty(PropertyName = "Location")]
public Location location { get; set; }
[JsonProperty(PropertyName = "Misc")]
public Misc misc { get; set; }
[JsonProperty(PropertyName = "Logging")]
public Logging logging { get; set; }
public class Notifications
{
[JsonProperty(PropertyName = "Chat Notifications")]
public bool chat { get; set; }
[JsonProperty(PropertyName = "Popup Notifications")]
public bool popup { get; set; }
}
public class DiscordMessages
{
[JsonProperty(PropertyName = "Enabled")]
public bool enabled { get; set; }
[JsonProperty(PropertyName = "Webhook URL")]
public string webhookURL { get; set; }
[JsonProperty(PropertyName = "Embedded Messages")]
public bool embedded { get; set; }
[JsonProperty(PropertyName = "Embed Color")]
public int embedColor { get; set; }
[JsonProperty(PropertyName = "Embed Title")]
public string embedTitle { get; set; }
}
public class UINotify
{
[JsonProperty(PropertyName = "Enabled")]
public bool enabled { get; set; }
[JsonProperty(PropertyName = "Notification Type")]
public int type { get; set; }
}
public class Alerts
{
[JsonProperty(PropertyName = "Patrol Helicopter Alerts")]
public bool patrolHeli { get; set; }
[JsonProperty(PropertyName = "Cargo Ship Alerts")]
public bool cargoShip { get; set; }
[JsonProperty(PropertyName = "Cargo Ship Harbor Alerts")]
public bool cargoShipHarbor { get; set; }
[JsonProperty(PropertyName = "Cargo Plane Alerts")]
public bool cargoPlane { get; set; }
[JsonProperty(PropertyName = "CH47 Chinook Alerts")]
public bool ch47 { get; set; }
[JsonProperty(PropertyName = "Bradley APC Alerts")]
public bool bradleyAPC { get; set; }
[JsonProperty(PropertyName = "Travelling Vendor Alerts")]
public bool travellingVendor { get; set; }
[JsonProperty(PropertyName = "Excavator Activated Alerts")]
public bool excavator { get; set; }
[JsonProperty(PropertyName = "Excavator Supply Request Alerts")]
public bool excavatorSupply { get; set; }
[JsonProperty(PropertyName = "Hackable Crate Alerts")]
public bool hackableCrateSpawn { get; set; }
[JsonProperty(PropertyName = "Player Hacking Crate Alerts")]
public bool hackingCrate { get; set; }
[JsonProperty(PropertyName = "Supply Signal Alerts")]
public bool supplySignal { get; set; }
[JsonProperty(PropertyName = "Supply Drop Alerts")]
public bool supplyDrop { get; set; }
[JsonProperty(PropertyName = "Supply Drop Landed Alerts")]
public bool supplyDropLand { get; set; }
}
public class Location
{
[JsonProperty(PropertyName = "Show Grid")]
public bool showGrid { get; set; }
[JsonProperty(PropertyName = "Show 'Oil Rig' Labels")]
public bool showOilRigs { get; set; }
[JsonProperty(PropertyName = "Show 'Cargo Ship' Label")]
public bool showCargoShip { get; set; }
[JsonProperty(PropertyName = "Show 'Excavator' Label")]
public bool showExcavator { get; set; }
[JsonProperty(PropertyName = "Hide Unmarked Grids")]
public bool hideOffGrid { get; set; }
[JsonProperty(PropertyName = "Show Coordinates")]
public bool showCoords { get; set; }
[JsonProperty(PropertyName = "Hide Y Coordinate")]
public bool hideYCoord { get; set; }
[JsonProperty(PropertyName = "Hide Coordinate Decimals")]
public bool hideCoordDecimals { get; set; }
}
public class Misc
{
[JsonProperty(PropertyName = "Chat Icon (SteamID64)")]
public ulong chatIcon { get; set; }
[JsonProperty(PropertyName = "Hide Cargo Ship Crate Messages")]
public bool hideCargoCrates { get; set; }
[JsonProperty(PropertyName = "Hide Oil Rig Crate & Chinook Messages")]
public bool hideRigCrates { get; set; }
[JsonProperty(PropertyName = "Show Supply Drop Player")]
public bool showSupplyPlayer { get; set; }
[JsonProperty(PropertyName = "Hide Player-Called Supply Drop Messages")]
public bool hideCalledSupply { get; set; }
[JsonProperty(PropertyName = "Hide Random Supply Drop Messages")]
public bool hideRandomSupply { get; set; }
}
public class Logging
{
[JsonProperty(PropertyName = "Log To Console")]
public bool console { get; set; }
[JsonProperty(PropertyName = "Log To File")]
public bool file { get; set; }
[JsonProperty(PropertyName = "Log All Events")]
public bool allEvents { get; set; }
}
[JsonProperty(PropertyName = "Version (Do not modify)")]
public Oxide.Core.VersionNumber Version { get; set; }
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
configData = Config.ReadObject<ConfigData>();
if (configData == null) throw new Exception();
if (configData.Version < Version)
UpdateConfigValues();
SaveConfig();
}
catch
{
PrintError("Your configuration file contains an error. Using default configuration values.");
LoadDefaultConfig();
}
}
private ConfigData GetBaseConfig()
{
return new ConfigData
{
notifications = new ConfigData.Notifications
{
chat = true,
popup = false
},
discordMessages = new ConfigData.DiscordMessages
{
enabled = false,
webhookURL = string.Empty,
embedded = true,
embedColor = 3447003,
embedTitle = ":arrow_lower_right: Inbound"
},
uiNotify = new ConfigData.UINotify
{
enabled = false,
type = 0
},
alerts = new ConfigData.Alerts
{
patrolHeli = true,
cargoShip = true,
cargoShipHarbor = true,
cargoPlane = true,
ch47 = true,
bradleyAPC = true,
travellingVendor = true,
excavator = true,
excavatorSupply = true,
hackableCrateSpawn = true,
hackingCrate = true,
supplySignal = true,
supplyDrop = true,
supplyDropLand = true
},
location = new ConfigData.Location
{
showGrid = true,
showOilRigs = true,
showCargoShip = true,
showExcavator = true,
hideOffGrid = true,
showCoords = false,
hideYCoord = false,
hideCoordDecimals = false
},
misc = new ConfigData.Misc
{
chatIcon = 0,
hideCargoCrates = false,
hideRigCrates = false,
showSupplyPlayer = false,
hideCalledSupply = false,
hideRandomSupply = false
},
logging = new ConfigData.Logging
{
console = false,
file = false,
allEvents = false
},
Version = Version
};
}
private void UpdateConfigValues()
{
PrintWarning("Config update detected! Updating config values...");
ConfigData baseConfig = GetBaseConfig();
if (configData.Version < new Core.VersionNumber(0, 6, 0))
{
configData = baseConfig;
configData.notifications.chat = Convert.ToBoolean(GetConfig("Notifications (true/false)", "Chat Notifications", true));
configData.notifications.popup = Convert.ToBoolean(GetConfig("Notifications (true/false)", "Popup Notifications", false));
configData.discordMessages.enabled = Convert.ToBoolean(GetConfig("Discord Messages", "Enabled (true/false)", false));
configData.discordMessages.webhookURL = Convert.ToString(GetConfig("Discord Messages", "Webhook URL", string.Empty));
configData.alerts.bradleyAPC = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Bradley APC Alerts", true));
configData.alerts.cargoPlane = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Cargo Plane Alerts", true));
configData.alerts.cargoShip = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Cargo Ship Alerts", true));
configData.alerts.ch47 = Convert.ToBoolean(GetConfig("Alerts (true/false)", "CH47 Chinook Alerts", true));
configData.alerts.excavator = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Excavator Alerts", true));
configData.alerts.hackableCrateSpawn = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Hackable Crate Alerts", true));
configData.alerts.patrolHeli = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Patrol Helicopter Alerts", true));
configData.alerts.hackingCrate = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Player Hacking Crate Alerts", true));
configData.alerts.supplySignal = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Player Supply Signal Alerts", true));
configData.alerts.supplyDrop = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Supply Drop Alerts", true));
configData.alerts.supplyDropLand = Convert.ToBoolean(GetConfig("Alerts (true/false)", "Supply Drop Landed Alerts", true));
configData.location.showGrid = Convert.ToBoolean(GetConfig("Grid (true/false)", "Show Grid", true));
configData.location.showOilRigs = Convert.ToBoolean(GetConfig("Grid (true/false)", "Show Oil Rig / Cargo Ship Labels", true));
configData.location.showCargoShip = Convert.ToBoolean(GetConfig("Grid (true/false)", "Show Oil Rig / Cargo Ship Labels", true));
configData.location.showCoords = Convert.ToBoolean(GetConfig("Coordinates (true/false)", "Show Coordinates", false));
configData.misc.chatIcon = Convert.ToUInt64(GetConfig("Chat Icon", "Steam ID", 0));
configData.misc.hideCargoCrates = Convert.ToBoolean(GetConfig("Misc (true/false)", "Hide Cargo Ship Crate Messages", false));
configData.misc.hideRigCrates = Convert.ToBoolean(GetConfig("Misc (true/false)", "Hide Oil Rig Crate Messages", false));
configData.logging.console = Convert.ToBoolean(GetConfig("Misc (true/false)", "Log To Console", false));
configData.logging.file = Convert.ToBoolean(GetConfig("Misc (true/false)", "Log To File", false));
BackupLang();
}
if (configData.Version < new Core.VersionNumber(0, 6, 5))
{
configData.alerts.cargoShipHarbor = baseConfig.alerts.cargoShipHarbor;
}
if (configData.Version < new Core.VersionNumber(0, 6, 7))
{
configData.alerts.travellingVendor = baseConfig.alerts.travellingVendor;
}
configData.Version = Version;
PrintWarning("Config update completed!");
}
object GetConfig(string menu, string dataValue, object defaultValue)
{
var data = Config[menu] as Dictionary<string, object>;
if (data == null)
{
data = new Dictionary<string, object>();
Config[menu] = data;
}
object value;
if (!data.TryGetValue(dataValue, out value))
{
value = defaultValue;
data[dataValue] = value;
}
return value;
}
protected override void LoadDefaultConfig() => configData = GetBaseConfig();
protected override void SaveConfig() => Config.WriteObject(configData, true);
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["PatrolHeli"] = "Patrol Helicopter inbound{0}{1}",
["CargoShip_"] = "Cargo Ship inbound{0}{1}",
["CargoShipApproachHarbor"] = "Cargo Ship is approaching the harbor{0}",
["CargoShipAtHarbor"] = "Cargo Ship has docked at the harbor{0}",
["CargoShipLeaveHarbor"] = "Cargo Ship is leaving the harbor{0}",
["CargoPlane_"] = "{0}Cargo Plane inbound{1}{2}",
["CH47"] = "Chinook inbound{0}{1}",
["BradleyAPC"] = "Bradley APC inbound{0}",
["TravellingVendor"] = "Travelling Vendor inbound{0}",
["Excavator_"] = "{0} has activated The Excavator{1}",
["ExcavatorSupplyRequest"] = "{0} has requested a supply drop{1}",
["HackableCrateSpawned"] = "Hackable Crate has spawned{0}",
["HackingCrate"] = "{0} is hacking a locked crate{1}",
["SupplySignal"] = "{0} has deployed a supply signal{1}",
["SupplyDropDropped"] = "{0}Supply Drop has dropped{1}",
["SupplyDropLanded_"] = "{0}Supply Drop has landed{1}",
["SupplyDropPlayer"] = "{0}'s ",
["Location"] = " at {0}",
["Destination"] = " and headed to {0}",
["DiscordMessage_"] = "{0}"
}, this);
}
private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
void BackupLang()
{
PrintWarning("Backing up language file to /oxide/logs/Inbound");
Dictionary<string, string> langFile = lang.GetMessages(lang.GetServerLanguage(), this);
string langJson = JsonConvert.SerializeObject(langFile, Formatting.Indented);
LogToFile($"lang_backup_{DateTime.Now.ToString("HH-mm-ss")}", langJson, this);
}
#endregion
}
}