forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptCompiler.cs
More file actions
67 lines (55 loc) · 1.96 KB
/
ScriptCompiler.cs
File metadata and controls
67 lines (55 loc) · 1.96 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
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using log4net;
using Microsoft.CSharp;
namespace L2dotNET.scripting
{
/// <summary>
/// Idea L2cemu
/// </summary>
public class ScriptCompiler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ScriptCompiler));
private readonly CSharpCodeProvider _provider;
private static volatile ScriptCompiler _instance;
private static readonly object SyncRoot = new object();
public static ScriptCompiler Instance
{
get
{
if (_instance != null)
return _instance;
lock (SyncRoot)
{
if (_instance == null)
_instance = new ScriptCompiler();
}
return _instance;
}
}
public ScriptCompiler()
{
_provider = new CSharpCodeProvider();
}
public object[] CompileFolder(string path)
{
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("L2dotNET.GameService.exe");
cp.GenerateInMemory = true;
cp.CompilerOptions = "/t:library";
List<object> objectList = new List<object>();
foreach (string fname in Directory.GetFiles(path, "*.cs"))
{
FileInfo info = new FileInfo(fname);
CompilerResults result = _provider.CompileAssemblyFromFile(cp, fname);
if (result.Errors.Count > 0)
Log.Error($"ScriptCompiler: Failed to compile {fname}.");
else
objectList.Add(result.CompiledAssembly.CreateInstance(Path.GetFileNameWithoutExtension(info.Name)));
}
Log.Info($"Script Compiler: Compiled {objectList.Count} scripted quests.");
return objectList.ToArray();
}
}
}