aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs120
1 files changed, 120 insertions, 0 deletions
diff --git a/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs b/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs
new file mode 100644
index 0000000..e2e1cad
--- /dev/null
+++ b/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs
@@ -0,0 +1,120 @@
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using Microsoft.CSharp;
6using System.CodeDom.Compiler;
7using System.Reflection;
8
9namespace OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL
10{
11
12 public class Compiler
13 {
14 private LSL2CSConverter LSL_Converter = new LSL2CSConverter();
15 private CSharpCodeProvider codeProvider = new CSharpCodeProvider();
16 private static UInt64 scriptCompileCounter = 0;
17 //private ICodeCompiler icc = codeProvider.CreateCompiler();
18 public string CompileFromFile(string LSOFileName)
19 {
20 switch (System.IO.Path.GetExtension(LSOFileName).ToLower())
21 {
22 case ".txt":
23 case ".lsl":
24 Common.SendToDebug("Source code is LSL, converting to CS");
25 return CompileFromLSLText(File.ReadAllText(LSOFileName));
26 case ".cs":
27 Common.SendToDebug("Source code is CS");
28 return CompileFromCSText(File.ReadAllText(LSOFileName));
29 default:
30 throw new Exception("Unknown script type.");
31 }
32 }
33 /// <summary>
34 /// Converts script from LSL to CS and calls CompileFromCSText
35 /// </summary>
36 /// <param name="Script">LSL script</param>
37 /// <returns>Filename to .dll assembly</returns>
38 public string CompileFromLSLText(string Script)
39 {
40 if (Script.Substring(0, 4).ToLower() == "//c#")
41 {
42 return CompileFromCSText( Script );
43 }
44 else
45 {
46 return CompileFromCSText(LSL_Converter.Convert(Script));
47 }
48 }
49 /// <summary>
50 /// Compile CS script to .Net assembly (.dll)
51 /// </summary>
52 /// <param name="Script">CS script</param>
53 /// <returns>Filename to .dll assembly</returns>
54 public string CompileFromCSText(string Script)
55 {
56
57
58 // Output assembly name
59 scriptCompileCounter++;
60 string OutFile = Path.Combine("ScriptEngines", "Script_" + scriptCompileCounter + ".dll");
61 try
62 {
63 System.IO.File.Delete(OutFile);
64 }
65 catch (Exception e)
66 {
67 Console.WriteLine("Exception attempting to delete old compiled script: " + e.ToString());
68 }
69 //string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll");
70
71 // DEBUG - write source to disk
72 try
73 {
74 File.WriteAllText(Path.Combine("ScriptEngines", "debug_" + Path.GetFileNameWithoutExtension(OutFile) + ".cs"), Script);
75 }
76 catch { }
77
78 // Do actual compile
79 System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
80 parameters.IncludeDebugInformation = true;
81 // Add all available assemblies
82 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
83 {
84 //Console.WriteLine("Adding assembly: " + asm.Location);
85 //parameters.ReferencedAssemblies.Add(asm.Location);
86 }
87
88 string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
89 string rootPathSE = Path.GetDirectoryName(this.GetType().Assembly.Location);
90 //Console.WriteLine("Assembly location: " + rootPath);
91 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Common.dll"));
92 parameters.ReferencedAssemblies.Add(Path.Combine(rootPathSE, "OpenSim.Grid.ScriptEngine.DotNetEngine.dll"));
93
94 //parameters.ReferencedAssemblies.Add("OpenSim.Region.Environment");
95 parameters.GenerateExecutable = false;
96 parameters.OutputAssembly = OutFile;
97 parameters.IncludeDebugInformation = false;
98 CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Script);
99
100 // Go through errors
101 // TODO: Return errors to user somehow
102 if (results.Errors.Count > 0)
103 {
104
105 string errtext = "";
106 foreach (CompilerError CompErr in results.Errors)
107 {
108 errtext += "Line number " + (CompErr.Line - 1) +
109 ", Error Number: " + CompErr.ErrorNumber +
110 ", '" + CompErr.ErrorText + "'\r\n";
111 }
112 throw new Exception(errtext);
113 }
114
115
116 return OutFile;
117 }
118
119 }
120}