-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZoneCommand.cs
993 lines (808 loc) · 23 KB
/
ZoneCommand.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
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
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Oxide.Core;
/* --------------------------------------------------------------------- */
/* --- Don't edit anything here if you don't know what you are doing --- */
/* --------------------------------------------------------------------- */
namespace Oxide.Plugins
{
[Info("ZoneCommand", "deer_SWAG", "0.1.1", ResourceId = 1254)]
[Description("Executes the commands when a player is entering a zone")]
class ZoneCommand : RustPlugin
{
#region Definitions
enum Methods
{
/// <summary>Only once</summary>
Once,
/// <summary>Every time a player enters/exits a zone</summary>
Always,
/// <summary>Per every player</summary>
PerPlayer,
/// <summary>Every day</summary>
PerDay,
/// <summary>Every in-game day</summary>
PerGameDay
}
enum Modes
{
/// <summary>When a player enters a zone</summary>
OnEnter,
/// <summary>When a player exits a zone</summary>
OnExit
}
const string PluginPermission = "zonecommand.use";
class PluginData
{
public HashSet<Zone> Zones = new HashSet<Zone>();
public void Add(Zone zone) => Zones.Add(zone);
public void Remove(Zone zone) => Zones.Remove(zone);
}
class Zone
{
public string Id;
public Methods Method;
public Modes Mode;
public int Amount = -1;
public string UserGroup;
public List<string> Commands = new List<string>();
public HashSet<ZonePlayer> Players = new HashSet<ZonePlayer>();
public Zone() { }
public Zone(string Id) { this.Id = Id; }
public void Add(string command) => Commands.Add(command);
public void Add(ZonePlayer player) => Players.Add(player);
}
class ZonePlayer
{
public ulong UserId;
public int Count;
}
#endregion Definitions
PluginData data;
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>()
{
{ "HelpText", "ZoneCommand:\n" +
"/zcmd add [zoneID] {command} {command} ... - add a zone with commands\n" +
"/zcmd remove <zoneID> - remove a zone with commands\n" +
"/zcmd list - show all zones with commands" +
"/zcmd clear - remove all zones with commands" +
"/zcmd vars - show available variables" },
{ "AvailableVars", "Available variables: $player.id, $player.name, $player.xyz, $player.x, $player.y, $player.z" },
{ "ErrorEnterCommands", "You must enter at least one command" },
{ "ErrorNotFound", "Zone was not found" },
{ "Added", "Zone with commands was successfully added!" },
{ "Removed", "Commands for zone has been removed!" },
{ "Clear", "All commands for zones were deleted" },
{ "List", "Zones with commands:\n" },
{ "DataLoadFail", "Unable to load data file. Creating a new one" },
{ "NoZoneManager", "You need to install ZoneManager or RectZones to use this plugin" }
}, this);
}
void OnServerInitialized()
{
data = Interface.Oxide.DataFileSystem.ReadObject<PluginData>(Name);
if(data == null)
{
PrintWarning(Lang("DataLoadFail"));
SaveData();
}
if(!IsPluginExists("ZoneManager") && !IsPluginExists("RectZones"))
RaiseError(Lang("NoZoneManager"));
}
void Init()
{
permission.RegisterPermission(PluginPermission, this);
}
void Unload()
{
SaveData();
}
#region ZoneManager hooks
void OnEnterZone(string zoneID, BasePlayer player)
{
if (data.Zones.Count == 0)
return;
foreach (Zone zone in data.Zones)
{
if (zone.Id == zoneID && zone.Mode == Modes.OnEnter)
{
ExecuteZone(zone, player);
return;
}
}
}
void OnExitZone(string zoneID, BasePlayer player)
{
if (data.Zones.Count == 0)
return;
foreach (Zone zone in data.Zones)
{
if (zone.Id == zoneID && zone.Mode == Modes.OnExit)
{
ExecuteZone(zone, player);
return;
}
}
}
#endregion ZoneManager hooks
// TODO
void ExecuteZone(Zone zone, BasePlayer player)
{
bool addPlayer = false;
bool addOne = false;
switch (zone.Method)
{
case Methods.Always:
if(zone.Amount != -1)
{
if(zone.Amount != zone.Players.Count)
addPlayer = true;
else
return;
}
break;
case Methods.PerPlayer:
{
if(zone.Amount != -1)
{
if (zone.Players.Count > 0)
{
bool found = false;
foreach (ZonePlayer zp in zone.Players)
{
if(zp.UserId == player.userID)
{
found = true;
if(zp.Count == zone.Amount)
return;
else
zp.Count++;
break;
}
}
if(!found)
{
addPlayer = true;
addOne = true;
}
}
else
{
addPlayer = true;
addOne = true;
}
}
else
{
if (zone.Players.Count > 0)
{
foreach (ZonePlayer zp in zone.Players)
{
if (zp.UserId == player.userID)
return;
}
addPlayer = true;
}
else
{
addPlayer = true;
}
}
}
break;
case Methods.PerDay:
// TODO: per day
break;
case Methods.PerGameDay:
// TODO: per game day
break;
}
if (addPlayer)
zone.Add(new ZonePlayer { UserId = player.userID, Count = addOne ? 1 : 0 });
foreach (string s in zone.Commands)
{
string command = s.Replace("$player.id", player.userID.ToString())
.Replace("$player.name", player.displayName)
.Replace("$player.xyz", player.transform.position.x + " " + player.transform.position.y + " " + player.transform.position.z)
.Replace("$player.x", player.transform.position.x.ToString())
.Replace("$player.y", player.transform.position.y.ToString())
.Replace("$player.z", player.transform.position.z.ToString());
if (command.StartsWith("sayto", StringComparison.CurrentCultureIgnoreCase))
PrintToChat(player, command.Substring(6));
rust.RunServerCommand(command);
}
}
// /zcmd add 81195143 {say hello there} {say okay then}
// id is optional (if there is no id then generate it)
[ChatCommand("zcmd")]
void cmdChat(BasePlayer player, string command, string[] args)
{
if (!PlayerHasPermission(player, PluginPermission))
return;
if (args.Length > 0)
{
string cmdWithArgs = ArrayToString(args);
QueryLanguage.Lexer lexer = new QueryLanguage.Lexer();
lexer.Parse(cmdWithArgs);
QueryLanguage.Parser parser = new QueryLanguage.Parser(lexer.Lexems as List<QueryLanguage.Lexem>);
QueryLanguage.LexemType type = parser.ParseCommand();
string id = parser.ParseId();
switch (type)
{
case QueryLanguage.LexemType.AddCmd:
{
if (string.IsNullOrEmpty(id))
{
//PrintToChat(player, Lang("ErrorEnterID", player));
return;
}
AddCommand(parser, id, player);
}
break;
case QueryLanguage.LexemType.RemoveCmd:
{
if (string.IsNullOrEmpty(id))
{
//PrintToChat(player, Lang("ErrorEnterID", player));
return;
}
RemoveCommand(parser, id, player);
}
break;
case QueryLanguage.LexemType.ListCmd:
ListCommand(parser, player);
break;
default:
PrintToChat(player, Lang("HelpText", player));
break;
}
}
else
{
PrintToChat(player, Lang("HelpText", player));
}
}
[ConsoleCommand("zone.command")] // TODO: console command
void cmdConsole(ConsoleSystem.Arg arg)
{
Puts("currently only from chat");
}
void AddCommand(QueryLanguage.Parser parser, string id, BasePlayer player)
{
PrintToChat("AddCommand");
List<string> cmds = parser.ParseCommands();
if(cmds.Count == 0)
{
PrintToChat(player, Lang("ErrorEnterCommands", player));
return;
}
QueryLanguage.Parser.ExecutionAndCount executionAndCount = parser.ParseExecutionAndCount();
Methods method = Methods.Always;
switch(executionAndCount.Execution1)
{
case QueryLanguage.LexemType.Always: method = Methods.Always; break;
case QueryLanguage.LexemType.Once: method = Methods.Once; break;
}
switch(executionAndCount.Execution2)
{
case QueryLanguage.LexemType.Player: method = Methods.PerPlayer; break;
case QueryLanguage.LexemType.Day: method = Methods.PerDay; break;
}
if(executionAndCount.Execution2 == QueryLanguage.LexemType.Game && executionAndCount.Execution3 == QueryLanguage.LexemType.Day)
{
method = Methods.PerGameDay;
}
Zone zone = new Zone(id);
zone.Commands = new List<string>(cmds);
zone.Mode = parser.ParseRule() == QueryLanguage.LexemType.Exit ? Modes.OnExit : Modes.OnEnter;
zone.Amount = executionAndCount.Count;
zone.Method = method;
zone.UserGroup = parser.ParseUserGroup();
data.Add(zone);
SaveData();
PrintToChat(player, Lang("Added", player));
}
void RemoveCommand(QueryLanguage.Parser parser, string id, BasePlayer player)
{
if(data.Zones.RemoveWhere(x => x.Id == id) > 0)
{
player.ChatMessage(Lang("Removed", player));
}
else
{
player.ChatMessage(Lang("ErrorNotFound", player));
}
}
void ListCommand(QueryLanguage.Parser parser, BasePlayer player)
{
string result = string.Empty;
foreach (Zone zone in data.Zones)
{
result += zone.Id + " (" + (zone.Mode == Modes.OnEnter ? "on enter" : "on exit") + ", " + zone.Method.ToString().ToLower() + "):\n\t";
foreach(string command in zone.Commands)
{
result += command + ", ";
}
result = result.Substring(0, result.Length - 2);
}
if(string.IsNullOrEmpty(result))
{
player.ChatMessage(Lang("ErrorNotFound", player));
return;
}
player.ChatMessage(result);
}
void AddZone(BasePlayer player, string[] args, Methods method, bool onlyAmount = false, bool hasMethod = true)
{
Zone zone = new Zone(args[1]);
zone.Method = method;
int offset = 2;
if (onlyAmount)
{
zone.Amount = int.Parse(args[2]);
offset++;
}
else
{
if (hasMethod)
offset++;
if (IsDigitsOnly(args[3]))
{
zone.Amount = int.Parse(args[3]);
offset++;
}
}
string cmd = "";
for (int i = offset; i < args.Length; i++)
cmd += args[i] + " ";
zone.Add(cmd.Substring(0, cmd.Length - 1));
data.Add(zone);
SaveData();
PrintToChat(player, Lang("Added"));
}
void RemoveZone(BasePlayer player, string[] args)
{
int removed = data.Zones.RemoveWhere(x => x.Id == args[1]);
if (removed > 0)
{
SaveData();
PrintToChat(player, Lang("Removed"));
}
else
{
PrintToChat(player, Lang("ErrorNotFound"));
}
}
void PrintZoneList(BasePlayer player)
{
string message = Lang("List");
if (data.Zones.Count > 0)
{
foreach (Zone z in data.Zones)
{
message += z.Id;
switch(z.Mode)
{
case Modes.OnEnter:
message += " (on enter) ";
break;
case Modes.OnExit:
message += " ( on exit) ";
break;
}
switch(z.Method)
{
case Methods.Always:
message += "(always)";
break;
case Methods.PerPlayer:
message += "(per player)";
break;
case Methods.PerDay:
message += "(per day)";
break;
case Methods.PerGameDay:
message += "(per game day)";
break;
}
message += (z.Amount > 0 ? (" (" + z.Amount + ")") : "") + ":\n";
foreach (string s in z.Commands)
message += s + "; ";
message = message.Substring(0, message.Length - 2) + "\n";
}
message = message.Substring(0, message.Length - 1);
}
else
{
message += Lang("ErrorNotFound");
}
PrintToChat(player, message);
}
void SendHelpText(BasePlayer player)
{
if(PlayerHasPermission(player, PluginPermission))
PrintToChat(player, Lang("HelpText"));
}
// ----------------------------- UTILS -----------------------------
// -----------------------------------------------------------------
bool IsPluginExists(string name)
{
return Interface.Oxide.GetLibrary<Core.Libraries.Plugins>().Exists(name);
}
string Lang(string key, BasePlayer player = null)
{
return lang.GetMessage(key, this, player?.UserIDString);
}
void SaveData()
{
Interface.Oxide.DataFileSystem.WriteObject(Name, data);
}
bool IsDigitsOnly(string str)
{
foreach (char c in str)
if (c < '0' || c > '9')
return false;
return true;
}
string ArrayToString(string[] array)
{
string result = string.Empty;
foreach (string s in array)
{
result += s + " ";
}
return result;
}
bool PlayerHasPermission(BasePlayer player, string permissionName)
{
return player.IsAdmin || permission.UserHasPermission(player.UserIDString, permissionName);
}
// ---------------------------- PARSER -----------------------------
// -----------------------------------------------------------------
class QueryLanguage
{
/*
add --˥
remove --˧------ required
list --˩
on --------- required for next one
enter --˥------ not required
exit --˩
123456780 --------- required
execute --------- required for next two
always --˥
once --˧
per day --˧
per game — login/logout --˧------ not required
per game day --˧
per player --˩
x times --------- not required
only for x --------- user group (admin, player, etc.) (number or string) (not required)
from x:xx to y:yy --------- not required
{command} --------- required (commands in braces)
*/
public enum LexemType
{
AddCmd, RemoveCmd, ListCmd,
Text, StartBrace, EndBrace,
On, Enter, Exit,
Execute, Always, Once, Per, Day, Game, Player, Times,
Only, For,
From, To,
Unknown
}
public class Lexem
{
public LexemType Type;
public string Value;
public int Offset;
}
class LexemDefenition<T>
{
public LexemType Type;
public T Representation;
public LexemDefenition(T representation, LexemType type)
{
Representation = representation;
Type = type;
}
}
class DynamicLexemDefenition : LexemDefenition<Regex>
{
public DynamicLexemDefenition(string representation, LexemType type) : base(new Regex(representation, RegexOptions.Compiled), type) { }
}
class StaticLexemDefenition : LexemDefenition<string>
{
public StaticLexemDefenition(string representation, LexemType type) : base(representation, type) { }
}
static class LexemDefenitions
{
public static StaticLexemDefenition[] Static = new[]
{
new StaticLexemDefenition("add", LexemType.AddCmd),
new StaticLexemDefenition("remove", LexemType.RemoveCmd),
new StaticLexemDefenition("list", LexemType.ListCmd),
new StaticLexemDefenition("on", LexemType.On),
new StaticLexemDefenition("enter", LexemType.Enter),
new StaticLexemDefenition("exit", LexemType.Exit),
new StaticLexemDefenition("execute", LexemType.Execute),
new StaticLexemDefenition("per", LexemType.Per),
new StaticLexemDefenition("only", LexemType.Only),
new StaticLexemDefenition("for", LexemType.For),
new StaticLexemDefenition("times", LexemType.Times),
new StaticLexemDefenition("always", LexemType.Always),
new StaticLexemDefenition("once", LexemType.Once),
new StaticLexemDefenition("day", LexemType.Day),
new StaticLexemDefenition("game", LexemType.Game),
new StaticLexemDefenition("player", LexemType.Player),
new StaticLexemDefenition("from", LexemType.From),
new StaticLexemDefenition("to", LexemType.To),
new StaticLexemDefenition("{", LexemType.StartBrace),
new StaticLexemDefenition("}", LexemType.EndBrace)
};
public static DynamicLexemDefenition[] Dynamic = new[]
{
new DynamicLexemDefenition(@"[\s\S]", LexemType.Text)
};
}
public class Lexer
{
public IEnumerable<Lexem> Lexems { get; private set; }
string source;
int offset;
public void Parse(string src)
{
source = src;
var prepLexems = new List<Lexem>();
while (InBounds())
{
Lexem lexem = ProcessStatic() ?? ProcessDynamic();
if (lexem != null)
prepLexems.Add(lexem);
}
var lexems = new List<Lexem>();
Lexem firstTextLexem = null;
foreach (Lexem lexem in prepLexems) // Fix for text. Idk how to do it properly
{
if (lexem.Type == LexemType.Text)
{
if (firstTextLexem == null)
firstTextLexem = lexem;
else
firstTextLexem.Value += lexem.Value;
}
else
{
if (firstTextLexem != null)
{
lexems.Add(firstTextLexem);
firstTextLexem = null;
}
lexems.Add(lexem);
}
}
Lexems = lexems;
}
Lexem ProcessStatic()
{
foreach (var defenition in LexemDefenitions.Static)
{
var representation = defenition.Representation;
var length = representation.Length;
if (offset + length > source.Length || !source.Substring(offset, length).Equals(representation, StringComparison.CurrentCultureIgnoreCase))
continue;
offset += length;
return new Lexem { Type = defenition.Type, Offset = offset, Value = representation };
}
return null;
}
Lexem ProcessDynamic()
{
foreach (var defenition in LexemDefenitions.Dynamic)
{
var match = defenition.Representation.Match(source, offset);
if (!match.Success)
continue;
offset += match.Length;
return new Lexem { Type = defenition.Type, Offset = offset, Value = match.Value };
}
return null;
}
bool InBounds()
{
return offset < source.Length;
}
}
public class Parser
{
List<Lexem> lexems;
public class Time
{
public TimeSpan? From;
public TimeSpan? To;
public Time(TimeSpan? from, TimeSpan? to) { From = from; To = to; }
}
public class ExecutionAndCount
{
public LexemType Execution1 = LexemType.Unknown;
public LexemType Execution2 = LexemType.Unknown;
public LexemType Execution3 = LexemType.Unknown;
public int Count;
public ExecutionAndCount(LexemType ex1 = LexemType.Unknown, LexemType ex2 = LexemType.Unknown, LexemType ex3 = LexemType.Unknown, int count = 0)
{
Execution1 = ex1;
Execution2 = ex2;
Execution3 = ex3;
Count = count;
}
}
public Parser(List<Lexem> lexems)
{
this.lexems = lexems;
}
/// <summary>Unknown if not command</summary>
public LexemType ParseCommand()
{
Lexem lexem = lexems[0];
LexemType type = lexem.Type;
if (type == LexemType.AddCmd || type == LexemType.ListCmd || type == LexemType.RemoveCmd)
return type;
return LexemType.Unknown;
}
public LexemType ParseRule()
{
if (lexems[2].Type == LexemType.On)
{
if (lexems[4].Type == LexemType.Enter || lexems[4].Type == LexemType.Exit)
return lexems[4].Type;
}
return LexemType.Unknown;
}
/// <summary>Empty if no id</summary>
public string ParseId()
{
Lexem lexem = lexems[1];
string id = lexem.Value.Trim();
if (lexem.Type == LexemType.Text)
return id;
return string.Empty;
}
public ExecutionAndCount ParseExecutionAndCount()
{
for (int i = 2; i < lexems.Count; i++)
{
if (lexems[i].Type == LexemType.Execute)
{
LexemType executionType1 = lexems[i + 2].Type;
if (executionType1 == LexemType.Always || executionType1 == LexemType.Once)
{
int count = ParseCount(i + 2);
return new ExecutionAndCount(executionType1, LexemType.Unknown, LexemType.Unknown, count);
}
else if (executionType1 == LexemType.Per)
{
LexemType executionType2 = lexems[i + 4].Type;
if (executionType2 == LexemType.Day || executionType2 == LexemType.Player)
{
int count = ParseCount(i + 4);
return new ExecutionAndCount(LexemType.Per, executionType2, LexemType.Unknown, count);
}
else if (executionType2 == LexemType.Game)
{
if (lexems[i + 6].Type == LexemType.Day)
{
int count2 = ParseCount(i + 6);
return new ExecutionAndCount(LexemType.Per, LexemType.Game, LexemType.Day, count2);
}
int count = ParseCount(i + 4);
return new ExecutionAndCount(LexemType.Per, LexemType.Game, LexemType.Unknown, count);
}
}
else
{
int count = ParseCount(i);
return new ExecutionAndCount(LexemType.Unknown, LexemType.Unknown, LexemType.Unknown, count);
}
}
else if (lexems[i].Type == LexemType.StartBrace)
{
break;
}
}
return new ExecutionAndCount();
}
int ParseCount(int position)
{
if (lexems[position + 1].Type == LexemType.Text)
{
if (lexems[position + 2].Type == LexemType.Times)
{
int number;
int.TryParse(lexems[position + 1].Value.Trim(), out number);
return number;
}
}
return -1;
}
public List<string> ParseCommands()
{
List<string> cmds = new List<string>(1);
for (int i = 2; i < lexems.Count; i++)
{
Lexem lexemStart = lexems[i]; // Start brace
if (lexemStart.Type == LexemType.StartBrace)
{
string cmd = string.Empty;
for (int ii = i + 1; ii < lexems.Count; ii++)
{
Lexem lexemCmd = lexems[ii];
if (lexemCmd.Type != LexemType.EndBrace)
{
cmd += lexemCmd.Value;
}
else
{
i = ii;
cmds.Add(cmd.TrimStart().TrimEnd());
break;
}
}
}
}
return cmds;
}
public string ParseUserGroup()
{
for (int i = 2; i < lexems.Count; i++)
{
if (lexems[i].Type == LexemType.Only && lexems[i + 2].Type == LexemType.For)
{
if (lexems[i + 3].Type == LexemType.Text)
{
return lexems[i + 3].Value.TrimStart().TrimEnd();
}
}
else if (lexems[i].Type == LexemType.StartBrace)
{
break;
}
}
return string.Empty;
}
public Time ParseTime()
{
for (int i = 2; i < lexems.Count; i++)
{
if (lexems[i].Type == LexemType.From)
{
string from = string.Empty;
if (lexems[i + 1].Type == LexemType.Text)
{
from = lexems[i + 1].Value.Trim();
if (lexems[i + 2].Type == LexemType.To)
{
if (lexems[i + 3].Type == LexemType.Text)
{
TimeSpan timeFrom;
TimeSpan timeTo;
bool fromSuccess = TimeSpan.TryParse(from, out timeFrom);
if (!fromSuccess)
return new Time(null, null);
bool toSuccess = TimeSpan.TryParse(lexems[i + 3].Value.Trim(), out timeTo);
if (!toSuccess)
return new Time(null, null);
return new Time(timeFrom, timeTo);
}
}
}
}
}
return new Time(null, null);
}
}
}
}
}