-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirective.cs
More file actions
215 lines (182 loc) · 6.08 KB
/
Directive.cs
File metadata and controls
215 lines (182 loc) · 6.08 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
namespace Rift.ModernRift.Core
{
public class Directive
{
public Action directive;
public List<string> triggers = new();
public bool isProtected = false;
public Directive(Action directive)
{
this.directive = directive;
}
public Directive(Action directive, bool isProtected)
{
this.directive = directive;
this.isProtected = isProtected;
}
public void AddTrigger(string trigger)
{
triggers.Add(trigger);
}
public void OverrideTriggers(List<string> triggers)
{
this.triggers = triggers;
}
public void Execute()
{
directive();
}
public bool ReceiveMessage(string message)
{
bool result = false;
foreach(string trigger in triggers)
{
if(message.ToLower() == trigger.ToLower())
{
Execute();
result = true;
}
}
return result;
}
public void Add()
{
MessageHandler.AddDirective(this);
}
}
public class ConsoleDirective : Directive {
string content;
public ConsoleDirective(Func<string> content) : base(() => { Console.WriteLine(content.Invoke()); })
{
this.content = content.Invoke();
}
public ConsoleDirective(Func<string> content, bool isProtected) : base(() => { Console.WriteLine(content.Invoke()); }, isProtected)
{
this.content = content.Invoke();
}
}
public class InlineConsoleDirective : Directive
{
string content;
public InlineConsoleDirective(Func<string> content) : base(() => { Console.Write(content.Invoke()); }) {
this.content = content.Invoke();
}
public InlineConsoleDirective(Func<string> content, bool isProtected) : base(() => { Console.Write(content.Invoke()); }, isProtected)
{
this.content = content.Invoke();
}
}
public class ConsoleFromFileDirective : Directive
{
string path;
List<Func<object>> parameters = new();
bool usingParameters = false;
public ConsoleFromFileDirective(string path) : base(() => { Console.WriteLine(GetValue(path)); })
{
this.path = path;
}
public ConsoleFromFileDirective(string path, bool isProtected) : base(() => { Console.WriteLine(GetValue(path)); }, isProtected)
{
this.path = path;
}
public static string GetValue(string path)
{
try
{
return File.ReadAllText(path.ToString());
}
catch (Exception inner)
{
throw new FilePathInvalidException("The file path \"" + path.ToString() + "\" was not a valid file.", inner);
}
}
public void AddParameter(Func<object> parameter)
{
parameters.Add(parameter);
}
public void RemoveParameter(int index)
{
parameters.RemoveAt(index);
}
public void RemoveParameter(Func<object> parameter)
{
parameters.Remove(parameter);
}
public void ToggleUseParameters()
{
if (!usingParameters)
{
usingParameters = true;
} else
{
usingParameters = false;
}
if (usingParameters)
{
directive = () =>
{
List<object> list = new List<object>();
foreach(Func<object> parameter in parameters)
{
list.Add(parameter.Invoke());
}
Console.WriteLine(GetValue(path), list.ToArray());
};
} else
{
directive = () =>
{
Console.WriteLine(GetValue(path));
};
}
}
}
public sealed class QuitDirective : Directive
{
public QuitDirective() : base(() => { Engine.Stop(); }, true) { }
}
public sealed class HelpDirective : Directive
{
public static bool enabled = true;
public HelpDirective() : base(() => { if (enabled) Console.WriteLine(HelpData()); }) { }
public HelpDirective(bool isProtected) : base(() => { if (enabled) Console.WriteLine(HelpData()); }, isProtected) { }
private static string HelpData()
{
string result = "Game created with the ModernRift Game Engine. For more information, visit the RiftEngine GitHub profile.\n";
List<string> names = new List<string>();
List<string> descriptions = new List<string>();
Dictionary<string, string> items = new();
foreach (Command c in Engine.GameReference.Commands)
{
names.Add(c.Get());
descriptions.Add(c.GetDescription());
items.Add(c.Get(), c.GetDescription());
}
int maxLength = 0;
foreach(string n in items.Keys)
{
if(n.Length > maxLength) maxLength = n.Length;
}
foreach (string n in items.Keys)
{
string name = n;
for (int i = 0; i < maxLength - n.Length + 1; i++)
{
name += " ";
}
result += name + "| " + items[n] + "\n";
}
return result;
}
}
public sealed class ClearDirective : Directive
{
public static bool enabled = true;
public ClearDirective() : base(() => { if(enabled) Console.Clear(); }) { }
public ClearDirective(bool isProtected) : base(() => { if (enabled) Console.Clear(); }, isProtected) { }
}
public sealed class SaveGameStateDirective : Directive
{
public SaveGameStateDirective() : base(() => { }) { }
}
}