-
Notifications
You must be signed in to change notification settings - Fork 11
/
Scripting.cs
314 lines (300 loc) · 14.2 KB
/
Scripting.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
/*
Copyright 2010 MCLawl Team - Written by Valek (Modified by MCForge)
Edited for use with MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Reflection.Emit;
using System.IO;
using System.Text;
namespace MCForge
{
static class Scripting
{
private static CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp");
private static CompilerParameters parameters = new CompilerParameters();
private static CompilerResults results;
private static string sourcepath = "extra/commands/source/";
private static string dllpath = "extra/commands/dll/";
/// <summary>
/// Creates a new, empty command class.
/// </summary>
public static void CreateNew(string CmdName)
{
if (!Directory.Exists(sourcepath))
{
Directory.CreateDirectory(sourcepath);
}
using (var sw = new StreamWriter(File.Create(sourcepath + "Cmd" + CmdName + ".cs")))
{
sw.Write(
"/*" + Environment.NewLine +
"\tAuto-generated command skeleton class." + Environment.NewLine +
Environment.NewLine +
"\tUse this as a basis for custom commands implemented via the MCForge scripting framework." + Environment.NewLine +
"\tFile and class should be named a specific way. For example, /update is named 'CmdUpdate.cs' for the file, and 'CmdUpdate' for the class." + Environment.NewLine +
"*/" + Environment.NewLine +
Environment.NewLine +
"// Add any other using statements you need up here, of course." + Environment.NewLine +
"// As a note, MCForge is designed for .NET 3.5." + Environment.NewLine +
"using System;" + Environment.NewLine +
Environment.NewLine +
"namespace MCForge" + Environment.NewLine +
"{" + Environment.NewLine +
"\tpublic class " + ClassName(CmdName) + " : Command" + Environment.NewLine +
"\t{" + Environment.NewLine +
"\t\t// The command's name, in all lowercase. What you'll be putting behind the slash when using it." + Environment.NewLine +
"\t\tpublic override string name { get { return \"" + CmdName.ToLower() + "\"; } }" + Environment.NewLine +
Environment.NewLine +
"\t\t// Command's shortcut (please take care not to use an existing one, or you may have issues." + Environment.NewLine +
"\t\tpublic override string shortcut { get { return \"\"; } }" + Environment.NewLine +
Environment.NewLine +
"\t\t// Determines which submenu the command displays in under /help." + Environment.NewLine +
"\t\tpublic override string type { get { return \"other\"; } }" + Environment.NewLine +
Environment.NewLine +
"\t\t// Determines whether or not this command can be used in a museum. Block/map altering commands should be made false to avoid errors." + Environment.NewLine +
"\t\tpublic override bool museumUsable { get { return false; } }" + Environment.NewLine +
Environment.NewLine +
"\t\t// Determines the command's default rank. Valid values are:" + Environment.NewLine +
"\t\t// LevelPermission.Nobody, LevelPermission.Banned, LevelPermission.Guest" + Environment.NewLine +
"\t\t// LevelPermission.Builder, LevelPermission.AdvBuilder, LevelPermission.Operator, LevelPermission.Admin" + Environment.NewLine +
"\t\tpublic override LevelPermission defaultRank { get { return LevelPermission.Banned; } }" + Environment.NewLine +
Environment.NewLine +
"\t\t// This is where the magic happens, naturally." + Environment.NewLine +
"\t\t// p is the player object for the player executing the command. message is everything after the command invocation itself." + Environment.NewLine +
"\t\tpublic override void Use(Player p, string message)" + Environment.NewLine +
"\t\t{" + Environment.NewLine +
"\t\t\tPlayer.SendMessage(p, \"Hello World!\");" + Environment.NewLine +
"\t\t}" + Environment.NewLine +
Environment.NewLine +
"\t\t// This one controls what happens when you use /help [commandname]." + Environment.NewLine +
"\t\tpublic override void Help(Player p)" + Environment.NewLine +
"\t\t{" + Environment.NewLine +
"\t\t\tPlayer.SendMessage(p, \"/" + CmdName.ToLower() + " - Does stuff. Example command.\");" + Environment.NewLine +
"\t\t}" + Environment.NewLine +
"\t}" + Environment.NewLine +
"}");
}
}
/// <summary>
/// Compiles a written function from source into a DLL.
/// </summary>
/// <param name="commandName">Name of the command file to be compiled (without the extension)</param>
/// <returns>True on successful compile, false on failure.</returns>
public static bool Compile(string commandName)
{
string divider = new string('-', 25);
if (!File.Exists(sourcepath + "Cmd" + commandName + ".cs"))
{
bool check = File.Exists("logs/errors/compiler.log");
StreamWriter errlog = new StreamWriter("logs/errors/compiler.log", check);
if (check)
{
errlog.WriteLine();
errlog.WriteLine(divider);
errlog.WriteLine();
}
errlog.WriteLine("File not found: Cmd" + commandName + ".cs");
errlog.Dispose();
return false;
}
if (!Directory.Exists(dllpath))
{
Directory.CreateDirectory(dllpath);
}
parameters.GenerateExecutable = false;
parameters.MainClass = commandName;
parameters.OutputAssembly = dllpath + "Cmd" + commandName + ".dll";
parameters.ReferencedAssemblies.Add("MCForge_.dll");
StreamReader sr = new StreamReader(sourcepath + "cmd" + commandName + ".cs");
results = compiler.CompileAssemblyFromSource(parameters, sr.ReadToEnd().Replace("namespace MCLawl", "namespace MCForge"));
sr.Dispose();
switch (results.Errors.Count)
{
case 0:
return true;
case 1:
CompilerError error = results.Errors[0];
bool exists = (File.Exists("logs/errors/compiler.log")) ? true : false;
StringBuilder sb = new StringBuilder();
if (exists)
{
sb.AppendLine();
sb.AppendLine(divider);
sb.AppendLine();
}
sb.AppendLine("Error " + error.ErrorNumber);
sb.AppendLine("Message: " + error.ErrorText);
sb.AppendLine("Line: " + error.Line);
StreamWriter sw = new StreamWriter("logs/errors/compiler.log", exists);
sw.Write(sb.ToString());
sw.Dispose();
return false;
default:
exists = (File.Exists("logs/errors/compiler.log")) ? true : false;
sb = new StringBuilder();
bool start = true;
if(exists)
{
sb.AppendLine();
sb.AppendLine(divider);
sb.AppendLine();
}
foreach (CompilerError err in results.Errors)
{
if (!start)
{
sb.AppendLine();
sb.AppendLine(divider);
sb.AppendLine();
}
sb.AppendLine("Error #" + err.ErrorNumber);
sb.AppendLine("Message: " + err.ErrorText);
sb.AppendLine("Line: " + err.Line);
if (start)
{
start = false;
}
}
sw = new StreamWriter("logs/errors/compiler.log", exists);
sw.Write(sb.ToString());
sw.Dispose();
return false;
}
}
public static void Autoload()
{
if (!File.Exists("text/cmdautoload.txt"))
{
File.Create("text/cmdautoload.txt");
return;
}
string[] autocmds = File.ReadAllLines("text/cmdautoload.txt");
foreach (string cmd in autocmds)
{
if (cmd == "")
{
continue;
}
string error = Scripting.Load("Cmd" + cmd.ToLower());
if (error != null)
{
Server.s.Log(error);
error = null;
continue;
}
Server.s.Log("AUTOLOAD: Loaded " + cmd.ToLower() + ".dll");
}
//ScriptingVB.Autoload();
}
/// <summary>
/// Loads a command for use on the server.
/// </summary>
/// <param name="command">Name of the command to be loaded (make sure it's prefixed by Cmd before bringing it in here or you'll have problems).</param>
/// <returns>Error string on failure, null on success.</returns>
public static string Load(string command)
{
if (command.Length < 3 || command.Substring(0, 3).ToLower() != "cmd")
{
return "Invalid command name specified.";
}
try
{
//Allows unloading and deleting dlls without server restart
object instance = null;
Assembly lib = null;
using (FileStream fs = File.Open("extra/commands/dll/" + command + ".dll", FileMode.Open))
{
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[1024];
int read = 0;
while ((read = fs.Read(buffer, 0, 1024)) > 0)
ms.Write(buffer, 0, read);
lib = Assembly.Load(ms.ToArray());
ms.Close();
ms.Dispose();
}
fs.Close();
fs.Dispose();
}
try
{
foreach (Type t in lib.GetTypes())
{
if (t.BaseType == typeof(Command))
{
instance = Activator.CreateInstance(t);
Command.all.Add((Command)instance);
}
}
}
catch { }
if (instance == null)
{
Server.s.Log("The command " + command + " couldnt be loaded!");
throw new BadImageFormatException();
}
/*Assembly asm = Assembly.LoadFrom("extra/commands/dll/" + command + ".dll");
Type[] types = asm.GetTypes();
foreach(var type in types)
{
if(type.BaseType == typeof(Command))
{
object instance = Activator.CreateInstance(type);
Command.all.Add((Command)instance);
}
}
//Type type = asm.GetTypes()[0];*/
}
catch (FileNotFoundException e)
{
Server.ErrorLog(e);
return command + ".dll does not exist in the DLL folder, or is missing a dependency. Details in the error log.";
}
catch (BadImageFormatException e)
{
Server.ErrorLog(e);
return command + ".dll is not a valid assembly, or has an invalid dependency. Details in the error log.";
}
catch (PathTooLongException)
{
return "Class name is too long.";
}
catch (FileLoadException e)
{
Server.ErrorLog(e);
return command + ".dll or one of its dependencies could not be loaded. Details in the error log.";
}
catch (Exception e)
{
Server.ErrorLog(e);
return "An unknown error occured and has been logged.";
}
return null;
}
/// <summary>
/// Creates a class name from the given string.
/// </summary>
/// <param name="name">String to convert to an MCForge class name.</param>
/// <returns>Successfully generated class name.</returns>
private static string ClassName(string name)
{
char[] conv = name.ToCharArray();
conv[0] = char.ToUpper(conv[0]);
return "Cmd" + new string(conv);
}
}
}