-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIngameClockGUI.cs
687 lines (606 loc) · 15.5 KB
/
IngameClockGUI.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
using System;
using System.Collections.Generic;
using Oxide.Core;
/* --- Do not edit anything here if you don't know what are you doing --- */
namespace Oxide.Plugins
{
[Info("IngameClockGUI", "deer_SWAG", "0.0.51", ResourceId = 1245)]
[Description("Displays ingame and server time")]
public class IngameClockGUI : RustPlugin
{
const string defaultInfoSize = "0.3";
const int isClockEnabled = 1 << 0;
const int isServerTime = 1 << 1;
class StoredData
{
public HashSet<Player> Players = new HashSet<Player>();
public StoredData() { }
}
class Player
{
public ulong UserID;
public int Options;
public Player() { }
public Player(ulong id, int options)
{
UserID = id;
Options = options;
}
}
private class TimedInfo
{
public long startTime;
public long endTime;
public string text;
public bool serverTime;
public string size;
public TimedInfo(long st, long et, string txt, bool server, string s)
{
startTime = st;
endTime = et;
text = txt;
serverTime = server;
size = s;
}
}
private string clockJson = @"
[{
""name"": ""Clock"",
""parent"": ""Overlay"",
""components"":
[
{
""type"": ""UnityEngine.UI.Button"",
""color"": ""%background%"",
""imagetype"": ""Tiled""
},
{
""type"": ""RectTransform"",
""anchormin"": ""%left% %bottom%"",
""anchormax"": ""%right% %top%""
}
]
},
{
""parent"": ""Clock"",
""components"":
[
{
""type"": ""UnityEngine.UI.Text"",
""text"": ""%prefix%%time%%postfix%"",
""fontSize"": %size%,
""color"": ""%color%"",
""align"": ""MiddleCenter""
},
{
""type"": ""RectTransform"",
""anchormin"": ""0 0"",
""anchormax"": ""1 0.9""
}
]
}]";
private string infoJson = @"
[{
""name"": ""ClockInfo"",
""parent"": ""Overlay"",
""components"":
[
{
""type"": ""UnityEngine.UI.Button"",
""color"": ""%background%"",
""imagetype"": ""Tiled""
},
{
""type"": ""RectTransform"",
""anchormin"": ""%info_left% %bottom%"",
""anchormax"": ""%info_right% %top%""
}
]
},
{
""parent"": ""ClockInfo"",
""components"":
[
{
""type"": ""UnityEngine.UI.Text"",
""text"": ""%info%"",
""fontSize"": %size%,
""color"": ""%color%"",
""align"": ""MiddleCenter""
},
{
""type"": ""RectTransform"",
""anchormin"": ""0.01 0"",
""anchormax"": ""0.99 1""
}
]
}]";
// -------------------- MAIN --------------------
StoredData data;
Timer updateTimer;
TOD_Sky sky;
DateTime dt;
bool isLoaded = false,
isInit = false;
string time = "";
DateTime gameTime;
DateTime serverTime;
private TimedInfo currentTI;
private List<TimedInfo> tiList;
protected override void LoadDefaultConfig()
{
Config.Clear();
CheckConfig();
Puts("Default config was saved and loaded!");
}
void Loaded()
{
isLoaded = true;
if(isInit) Load();
}
void OnServerInitialized()
{
isInit = true;
if(isLoaded) Load();
}
void Load()
{
data = Interface.GetMod().DataFileSystem.ReadObject<StoredData>(Title);
tiList = new List<TimedInfo>();
currentTI = null;
sky = TOD_Sky.Instance;
CheckConfig();
double left = (double)Config["Position", "Left"];
double right = (double)Config["Position", "Left"] + (double)Config["Size", "Width"];
double bottom = (double)Config["Position", "Bottom"];
double top = (double)Config["Position", "Bottom"] + (double)Config["Size", "Height"];
clockJson = clockJson.Replace("%background%", (string)Config["BackgroundColor"])
.Replace("%color%", (string)Config["TextColor"])
.Replace("%size%", Config["FontSize"].ToString())
.Replace("%left%", left.ToString())
.Replace("%right%", right.ToString())
.Replace("%bottom%", bottom.ToString())
.Replace("%top%", top.ToString())
.Replace("%prefix%", (string)Config["Prefix"])
.Replace("%postfix%", (string)Config["Postfix"]);
// --- for timed notifications
List<object> ti = (List<object>)Config["TimedInfo"];
int size = ti.Count;
for(int i = 0; i < size; i++)
{
string infoString = (string)ti[i];
if(infoString.Length > 0)
tiList.Add(GetTimedInfo(infoString));
}
double info_left = right + 0.002;
infoJson = infoJson.Replace("%background%", (string)Config["BackgroundColor"])
.Replace("%color%", (string)Config["TextColor"])
.Replace("%size%", Config["FontSize"].ToString())
.Replace("%bottom%", bottom.ToString())
.Replace("%top%", top.ToString())
.Replace("%info_left%", info_left.ToString())
.Replace("%info_right%", defaultInfoSize);
// ---
UpdateTime();
updateTimer = timer.Repeat((int)Config["UpdateTimeInSeconds"], 0, () => UpdateTime());
}
void OnPlayerInit(BasePlayer player)
{
//currentTI = null;
//UpdateInfo();
}
void Unload()
{
SaveData();
DestroyGUI();
DestroyInfo();
}
[ChatCommand("clock")]
void cmdChat(BasePlayer player, string command, string[] args)
{
if(args.Length == 1)
{
if(args[0] == "server" || args[0] == "s")
{
if((bool)Config["PreventChangingTime"])
PrintToChat(player, (string)Config["Messages", "PreventChangeEnabled"]);
else
if(data.Players.Count > 0)
{
foreach(Player p in data.Players)
{
if(p.UserID == player.userID)
{
if(GetOption(p.Options, isServerTime))
{
p.Options &= ~isServerTime;
PrintToChat(player, (string)Config["Messages", "STDisabled"]);
}
else
{
p.Options += isServerTime;
PrintToChat(player, (string)Config["Messages", "STEnabled"]);
}
break;
}
}
}
else
{
data.Players.Add(new Player(player.userID, isClockEnabled | isServerTime));
PrintToChat(player, (string)Config["Messages", "STEnabled"]);
}
}
else
{
SendHelpText(player);
}
}
else
{
bool found = false;
if(data.Players.Count > 0)
{
foreach(Player p in data.Players)
{
if(p.UserID == player.userID)
{
found = true;
if(GetOption(p.Options, isClockEnabled))
{
p.Options &= ~isClockEnabled;
DestroyGUI();
PrintToChat(player, (string)Config["Messages", "Disabled"]);
}
else
{
p.Options += isClockEnabled;
AddGUI();
PrintToChat(player, (string)Config["Messages", "Enabled"]);
}
break;
}
else
{
found = false;
}
}
if(!found)
{
data.Players.Add(new Player(player.userID, 0));
DestroyGUI();
PrintToChat(player, (string)Config["Messages", "Disabled"]);
}
}
else
{
data.Players.Add(new Player(player.userID, 0));
PrintToChat(player, (string)Config["Messages", "Disabled"]);
}
}
}
void AddGUI()
{
if(data.Players.Count > 0)
{
int size = BasePlayer.activePlayerList.Count;
for(int i = 0; i < size; i++)
{
BasePlayer bp = BasePlayer.activePlayerList[i];
bool found = false;
foreach(Player p in data.Players)
{
if(p.UserID == bp.userID)
{
found = true;
if(GetOption(p.Options, isClockEnabled))
{
if(!((bool)Config["PreventChangingTime"]))
if(GetOption(p.Options, isServerTime))
dt = serverTime;
else
dt = gameTime;
ShowTime();
SendClientCommand(bp, "AddUI", new Facepunch.ObjectList(clockJson.Replace("%time%", time)));
}
break;
}
else
{
found = false;
}
}
if(!found)
{
if(!((bool)Config["PreventChangingTime"]))
dt = gameTime;
ShowTime();
SendClientCommand(bp, "AddUI", new Facepunch.ObjectList(clockJson.Replace("%time%", time)));
}
}
}
else
{
int size = BasePlayer.activePlayerList.Count;
for(int i = 0; i < size; i++)
{
BasePlayer bp = BasePlayer.activePlayerList[i];
if(!((bool)Config["PreventChangingTime"]))
dt = gameTime;
ShowTime();
SendClientCommand(bp, "AddUI", new Facepunch.ObjectList(clockJson.Replace("%time%", time)));
}
}
}
private void UpdateTime()
{
gameTime = sky.Cycle.DateTime;
serverTime = DateTime.Now;
if((bool)Config["PreventChangingTime"])
if((bool)Config["ServerTime"])
dt = serverTime;
else
dt = gameTime;
DestroyGUI();
AddGUI();
UpdateInfo();
}
private void DestroyGUI()
{
int size = BasePlayer.activePlayerList.Count;
for(int i = 0; i < size; i++)
SendClientCommand(BasePlayer.activePlayerList[i], "DestroyUI", new Facepunch.ObjectList("Clock"));
}
private void ShowTime()
{
if((int)Config["TimeFormat"] == 24)
if((bool)Config["ShowSeconds"])
time = dt.ToString("HH:mm:ss");
else
time = dt.ToString("HH:mm");
else
if((bool)Config["ShowSeconds"])
time = dt.ToString("h:mm:ss tt");
else
time = dt.ToString("h:mm tt");
}
void ShowInfo(string text, string iSize)
{
int size = BasePlayer.activePlayerList.Count;
for(int i = 0; i < size; i++)
SendClientCommand(BasePlayer.activePlayerList[i], "AddUI", new Facepunch.ObjectList(infoJson.Replace("%info%", text).Replace("%info_right%", iSize)));
}
void UpdateInfo()
{
if(tiList.Count > 0)
{
DateTime g = DateTime.Parse(gameTime.ToString("HH:mm"));
DateTime s = DateTime.Parse(serverTime.ToString("HH:mm"));
if(currentTI == null)
{
for(int i = 0; i < tiList.Count; i++)
{
if(!tiList[i].serverTime)
{
if(g.Ticks > tiList[i].startTime && g.Ticks < tiList[i].endTime)
{
currentTI = tiList[i];
ShowInfo(tiList[i].text, tiList[i].size);
}
}
else
{
if(s.Ticks > tiList[i].startTime && s.Ticks < tiList[i].endTime)
{
currentTI = tiList[i];
ShowInfo(tiList[i].text, tiList[i].size);
}
}
}
}
else
{
if(!currentTI.serverTime)
{
if(g.Ticks > currentTI.endTime)
{
currentTI = null;
DestroyInfo();
}
}
else
{
if(s.Ticks > currentTI.endTime)
{
currentTI = null;
DestroyInfo();
}
}
}
}
}
void DestroyInfo()
{
int size = BasePlayer.activePlayerList.Count;
for(int i = 0; i < size; i++)
SendClientCommand(BasePlayer.activePlayerList[i], "DestroyUI", new Facepunch.ObjectList("ClockInfo"));
}
void SendHelpText(BasePlayer player)
{
PrintToChat(player, (string)Config["Messages", "Help"]);
}
void CheckConfig()
{
ConfigItem("UpdateTimeInSeconds", 2);
ConfigItem("ShowSeconds", false);
ConfigItem("BackgroundColor", "0.1 0.1 0.1 0.3");
ConfigItem("TextColor", "1 1 1 0.3");
ConfigItem("FontSize", 14);
ConfigItem("Position", "Left", 0.01);
ConfigItem("Position", "Bottom", 0.015);
ConfigItem("Size", "Width", 0.05);
ConfigItem("Size", "Height", 0.03);
ConfigItem("ServerTime", false);
ConfigItem("PreventChangingTime", false);
ConfigItem("TimeFormat", 24);
ConfigItem("Prefix", "");
ConfigItem("Postfix", "");
ConfigItem("TimedInfo", new string[] { "" });
ConfigItem("Messages", "Enabled", "You have enabled clock");
ConfigItem("Messages", "Disabled", "You have disabled clock");
ConfigItem("Messages", "STEnabled", "Now your clock shows server time");
ConfigItem("Messages", "STDisabled", "Now your clock shows ingame time");
ConfigItem("Messages", "Help", "Clock:\n/clock - toggle clock\n/clock server - toggle server/ingame time");
ConfigItem("Messages", "PreventChangeEnabled", "You can't choose between server or ingame time");
SaveConfig();
}
// ----------------------------- UTILS -----------------------------
// -----------------------------------------------------------------
private void ConfigItem(string name, object defaultValue)
{
Config[name] = Config[name] ?? defaultValue;
}
private void ConfigItem(string name1, string name2, object defaultValue)
{
Config[name1, name2] = Config[name1, name2] ?? defaultValue;
}
void SaveData()
{
Interface.GetMod().DataFileSystem.WriteObject(Title, data);
}
bool GetOption(int options, int option)
{
if((options & option) != 0)
return true;
else
return false;
}
void SendClientCommand(BasePlayer player, string functionName, Facepunch.ObjectList arguments)
{
CommunityEntity.ServerInstance.ClientRPCEx(new Network.SendInfo(player.net.connection), null, functionName, arguments);
}
private enum TIStates { Init, StartBracket, StartTime, FirstColon, SecondColon, Hyphen, EndTime, AfterBracket, Text, Size, SizeAfterDot };
private TimedInfo GetTimedInfo(string source)
{
source = source.TrimStart().TrimEnd();
TIStates currentState = TIStates.Init;
string startTime = "", endTime = "", text = "", nSize = "";
bool st = false; // Server time
int size = source.Length;
for(int i = 0; i < size; i++)
{
switch(currentState)
{
case TIStates.Init:
{
if (source[i] == '[')
{
currentState = TIStates.StartTime;
}
else if(source[i] == 's' || source[i] == 'S')
{
st = true;
i++;
currentState = TIStates.StartTime;
}
break;
}
case TIStates.StartTime:
{
if (Char.IsDigit(source[i]))
{
startTime += source[i];
}
else if (source[i] == ':')
{
startTime += source[i];
currentState = TIStates.FirstColon;
}
break;
}
case TIStates.FirstColon:
{
if (Char.IsDigit(source[i]))
{
startTime += source[i];
}
else if(source[i] == '-')
{
i--;
currentState = TIStates.Hyphen;
}
break;
}
case TIStates.Hyphen:
{
if (Char.IsDigit(source[i]))
{
i--;
currentState = TIStates.EndTime;
}
break;
}
case TIStates.EndTime:
{
if (Char.IsDigit(source[i]))
{
endTime += source[i];
}
else if (source[i] == ':')
{
endTime += source[i];
currentState = TIStates.SecondColon;
}
break;
}
case TIStates.SecondColon:
{
if (Char.IsDigit(source[i]))
endTime += source[i];
else if(source[i] == ']')
currentState = TIStates.AfterBracket;
else if(source[i] == '-')
currentState = TIStates.Size;
break;
}
case TIStates.Size:
{
if(Char.IsDigit(source[i]))
{
nSize += source[i];
}
else if(source[i] == '.')
{
nSize += source[i];
currentState = TIStates.SizeAfterDot;
}
break;
}
case TIStates.SizeAfterDot:
{
if(Char.IsDigit(source[i]))
nSize += source[i];
else if(source[i] == ']')
currentState = TIStates.AfterBracket;
break;
}
case TIStates.AfterBracket:
{
if(source[i] != ' ')
text += source[i];
currentState = TIStates.Text;
break;
}
case TIStates.Text:
{
text += source[i];
break;
}
}
}
if(nSize.Length == 0)
nSize = defaultInfoSize;
return new TimedInfo(DateTime.Parse(startTime).Ticks, DateTime.Parse(endTime).Ticks, text, st, nSize);
}
}
}