aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/ScriptEngine/Components/DotNetEngine/Compilers/CILCompiler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/ScriptEngine/Components/DotNetEngine/Compilers/CILCompiler.cs')
-rw-r--r--OpenSim/ScriptEngine/Components/DotNetEngine/Compilers/CILCompiler.cs187
1 files changed, 0 insertions, 187 deletions
diff --git a/OpenSim/ScriptEngine/Components/DotNetEngine/Compilers/CILCompiler.cs b/OpenSim/ScriptEngine/Components/DotNetEngine/Compilers/CILCompiler.cs
deleted file mode 100644
index 3d2d9d2..0000000
--- a/OpenSim/ScriptEngine/Components/DotNetEngine/Compilers/CILCompiler.cs
+++ /dev/null
@@ -1,187 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28using System.CodeDom.Compiler;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
32using System.Text.RegularExpressions;
33using log4net;
34using OpenSim.ScriptEngine.Shared;
35using ScriptAssemblies;
36
37namespace OpenSim.ScriptEngine.Components.DotNetEngine.Compilers
38{
39 public abstract class CILCompiler
40 {
41 internal static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
42 private string ScriptEnginesPath = "ScriptEngines";
43 private string Name { get { return "SECS.DotNetEngine.CILCompiler"; } }
44 private string m_scriptAssemblyName;
45 internal string ScriptAssemblyName { get { return m_scriptAssemblyName; } set { m_scriptAssemblyName = value; } }
46
47 // Default inherit
48 protected string ScriptInheritFrom = typeof(ScriptAssemblies.ScriptBase).Name;
49 private readonly System.Security.Cryptography.MD5CryptoServiceProvider MD5Sum = new System.Security.Cryptography.MD5CryptoServiceProvider();
50 protected CodeDomProvider CompileProvider;
51
52 //private string[] AppDomainAssemblies = new string[] { "OpenSim.Region.ScriptEngine.Shared.dll", "OpenSim.Region.ScriptEngine.Shared.Script.dll", "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll" };
53 private readonly string[] AppDomainAssemblies = new string[] {
54 Assembly.GetAssembly(typeof(Int32)).Location,
55 "OpenSim.ScriptEngine.Shared.dll",
56 "OpenSim.ScriptEngine.Shared.Script.dll",
57 Path.Combine("ScriptEngines", "OpenSim.Region.ScriptEngine.Shared.dll")
58 };
59
60 public abstract string PreProcessScript(ref string script);
61
62 public CILCompiler()
63 {
64 }
65
66 private static readonly Regex FileNameFixer = new Regex(@"[^a-zA-Z0-9\.\-]", RegexOptions.Compiled | RegexOptions.Singleline);
67 public string Compile(ScriptMetaData data, ref string _script)
68 {
69 // Add "using", "inherit", default constructor, etc around script.
70 string script = PreProcessScript(ref _script);
71
72 // Get filename based on content
73 string md5Sum = System.Convert.ToBase64String(
74 MD5Sum.ComputeHash(
75 System.Text.Encoding.ASCII.GetBytes(script)
76 ));
77 // Unique name for this assembly
78 ScriptAssemblyName = "SECS_Script_" + FileNameFixer.Replace(md5Sum, "_");
79
80 string OutFile = Path.Combine(ScriptEnginesPath, ScriptAssemblyName + ".dll");
81
82 // Make sure target dir exist
83 if (!Directory.Exists(ScriptEnginesPath))
84 try { Directory.CreateDirectory(ScriptEnginesPath); }
85 catch { }
86
87 // Already exist? No point in recompiling
88 if (File.Exists(OutFile))
89 return OutFile;
90
91 //
92 // Dump source code
93 //
94 string dumpFile = OutFile + ".txt";
95 try
96 {
97 if (File.Exists(dumpFile))
98 File.Delete(dumpFile);
99 File.WriteAllText(dumpFile, script);
100 }
101 catch (Exception e)
102 {
103 m_log.DebugFormat("[{0}] Exception trying to dump script source code to file \"{1}\": {2}", Name, dumpFile, e.ToString());
104 }
105
106 //
107 // COMPILE
108 //
109
110 CompilerParameters parameters = new CompilerParameters();
111 parameters.IncludeDebugInformation = true;
112 //string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
113
114 foreach (string file in AppDomainAssemblies)
115 {
116 parameters.ReferencedAssemblies.Add(file);
117 m_log.DebugFormat("[{0}] Adding reference for compile: \"{1}\".", Name, file);
118 }
119 //lock (commandProvider)
120 //{
121 // foreach (string key in commandProvider.Keys)
122 // {
123 // IScriptCommandProvider cp = commandProvider[key];
124 // string
125 // file = cp.GetType().Assembly.Location;
126 // parameters.ReferencedAssemblies.Add(file);
127 // m_log.DebugFormat("[{0}] Loading command provider assembly \"{1}\" into AppDomain: \"{2}\".", Name,
128 // key, file);
129 // }
130 //}
131
132 parameters.GenerateExecutable = false;
133 parameters.OutputAssembly = OutFile;
134 parameters.IncludeDebugInformation = true;
135 //parameters.WarningLevel = 1; // Should be 4?
136 parameters.TreatWarningsAsErrors = false;
137
138 // Do compile
139 CompilerResults results = CompileProvider.CompileAssemblyFromSource(parameters, script);
140
141
142 //
143 // WARNINGS AND ERRORS
144 //
145 //TODO
146 int display = 5;
147 if (results.Errors.Count > 0)
148 {
149 string errtext = String.Empty;
150 foreach (CompilerError CompErr in results.Errors)
151 {
152 // Show 5 errors max
153 //
154 if (display <= 0)
155 break;
156 display--;
157
158 string severity = "Error";
159 if (CompErr.IsWarning)
160 severity = "Warning";
161
162 //TODO: Implement
163 KeyValuePair<int, int> lslPos = new KeyValuePair<int, int>();
164
165 //lslPos = "NOT IMPLEMENTED";// FindErrorPosition(CompErr.Line, CompErr.Column);
166
167 string text = CompErr.ErrorText;
168
169 // The Second Life viewer's script editor begins
170 // countingn lines and columns at 0, so we subtract 1.
171 errtext += String.Format("Line ({0},{1}): {4} {2}: {3}\n",
172 lslPos.Key - 1, lslPos.Value - 1,
173 CompErr.ErrorNumber, text, severity);
174 }
175
176 if (!File.Exists(OutFile))
177 {
178 throw new Exception(errtext);
179 }
180 }
181
182 // TODO: Process errors
183 return OutFile;
184 }
185
186 }
187}