forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cs
More file actions
126 lines (107 loc) · 3.39 KB
/
Command.cs
File metadata and controls
126 lines (107 loc) · 3.39 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
namespace kOS
{
public class CommandAttribute : Attribute
{
public string[] Values { get; set; }
public CommandAttribute(params string[] values) { this.Values = values; }
public override String ToString()
{
return String.Join(",", Values);
}
}
public static class CommandRegistry
{
public static Dictionary<String, Type> Bindings = new Dictionary<string, Type>();
static CommandRegistry()
{
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
{
CommandAttribute attr = (CommandAttribute)t.GetCustomAttributes(typeof(CommandAttribute), true).FirstOrDefault();
if (attr != null)
{
foreach (String s in attr.Values)
{
Bindings.Add(Utils.BuildRegex(s), t);
}
}
}
}
}
public class Command : ExecutionContext
{
public float Time;
public float WaitTime = 0;
public String Input;
public Match RegexMatch;
public String InstanceName;
public Command(Match regexMatch, ExecutionContext context) : base(context)
{
Input = regexMatch.ToString();
this.RegexMatch = regexMatch;
}
public Command(String input, ExecutionContext context) : base(context)
{
this.Input = input;
}
public virtual void Verify()
{
}
public virtual void Evaluate()
{
}
public static Command Get(String input, ExecutionContext context, int line)
{
try
{
Command retCommand = Get(input, context);
retCommand.Line = line;
return retCommand;
}
catch (kOSException e)
{
e.LineNumber = line;
throw e;
}
}
public static Command Get(String input, ExecutionContext context)
{
input = input.Trim();//.Replace("\n", " ");
foreach (var kvp in CommandRegistry.Bindings)
{
Match match = Regex.Match(input, kvp.Key, RegexOptions.IgnoreCase);
if (match.Success)
{
var command = (Command)Activator.CreateInstance(kvp.Value, match, context);
return command;
}
}
throw new kOSException("Syntax Error.", context);
}
public virtual void Refresh()
{
this.State = ExecutionState.NEW;
}
public override void Lock(Command command)
{
if (ParentContext != null) ParentContext.Lock(command);
}
public override void Lock(string name, Expression expression)
{
if (ParentContext != null) ParentContext.Lock(name, expression);
}
public override void Unlock(Command command)
{
if (ParentContext != null) ParentContext.Unlock(command);
}
public override void Unlock(string name)
{
if (ParentContext != null) ParentContext.Unlock(name);
}
}
}