aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/Scripting/Minimodule/MRMModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/OptionalModules/Scripting/Minimodule/MRMModule.cs')
-rw-r--r--OpenSim/Region/OptionalModules/Scripting/Minimodule/MRMModule.cs470
1 files changed, 235 insertions, 235 deletions
diff --git a/OpenSim/Region/OptionalModules/Scripting/Minimodule/MRMModule.cs b/OpenSim/Region/OptionalModules/Scripting/Minimodule/MRMModule.cs
index 86a3240..1536445 100644
--- a/OpenSim/Region/OptionalModules/Scripting/Minimodule/MRMModule.cs
+++ b/OpenSim/Region/OptionalModules/Scripting/Minimodule/MRMModule.cs
@@ -1,235 +1,235 @@
1using System; 1using System;
2using System.CodeDom.Compiler; 2using System.CodeDom.Compiler;
3using System.Collections.Generic; 3using System.Collections.Generic;
4using System.IO; 4using System.IO;
5using System.Reflection; 5using System.Reflection;
6using System.Text; 6using System.Text;
7using log4net; 7using log4net;
8using Microsoft.CSharp; 8using Microsoft.CSharp;
9using Nini.Config; 9using Nini.Config;
10using OpenMetaverse; 10using OpenMetaverse;
11using OpenSim.Region.Framework.Interfaces; 11using OpenSim.Region.Framework.Interfaces;
12using OpenSim.Region.Framework.Scenes; 12using OpenSim.Region.Framework.Scenes;
13 13
14namespace OpenSim.Region.OptionalModules.Scripting.Minimodule 14namespace OpenSim.Region.OptionalModules.Scripting.Minimodule
15{ 15{
16 public class MRMModule : IRegionModule 16 public class MRMModule : IRegionModule
17 { 17 {
18 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 18 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
19 private Scene m_scene; 19 private Scene m_scene;
20 20
21 private readonly Dictionary<UUID,MRMBase> m_scripts = new Dictionary<UUID, MRMBase>(); 21 private readonly Dictionary<UUID,MRMBase> m_scripts = new Dictionary<UUID, MRMBase>();
22 22
23 private static readonly CSharpCodeProvider CScodeProvider = new CSharpCodeProvider(); 23 private static readonly CSharpCodeProvider CScodeProvider = new CSharpCodeProvider();
24 24
25 public void Initialise(Scene scene, IConfigSource source) 25 public void Initialise(Scene scene, IConfigSource source)
26 { 26 {
27 if (source.Configs["MRM"] != null) 27 if (source.Configs["MRM"] != null)
28 { 28 {
29 if (source.Configs["MRM"].GetBoolean("Enabled", false)) 29 if (source.Configs["MRM"].GetBoolean("Enabled", false))
30 { 30 {
31 m_log.Info("[MRM] Enabling MRM Module"); 31 m_log.Info("[MRM] Enabling MRM Module");
32 m_scene = scene; 32 m_scene = scene;
33 scene.EventManager.OnRezScript += EventManager_OnRezScript; 33 scene.EventManager.OnRezScript += EventManager_OnRezScript;
34 } 34 }
35 else 35 else
36 { 36 {
37 m_log.Info("[MRM] Disabled MRM Module (Express)"); 37 m_log.Info("[MRM] Disabled MRM Module (Express)");
38 } 38 }
39 } 39 }
40 else 40 else
41 { 41 {
42 m_log.Info("[MRM] Disabled MRM Module (Omission)"); 42 m_log.Info("[MRM] Disabled MRM Module (Omission)");
43 } 43 }
44 } 44 }
45 45
46 void EventManager_OnRezScript(uint localID, UUID itemID, string script, int startParam, bool postOnRez, string engine, int stateSource) 46 void EventManager_OnRezScript(uint localID, UUID itemID, string script, int startParam, bool postOnRez, string engine, int stateSource)
47 { 47 {
48 if (script.StartsWith("//MiniMod:C#")) 48 if (script.StartsWith("//MiniMod:C#"))
49 { 49 {
50 m_log.Info("[MRM] Found C# MRM"); 50 m_log.Info("[MRM] Found C# MRM");
51 IWorld m_world = new World(m_scene); 51 IWorld m_world = new World(m_scene);
52 IHost m_host = new Host(new SOPObject(m_scene, localID)); 52 IHost m_host = new Host(new SOPObject(m_scene, localID));
53 53
54 MRMBase mmb = (MRMBase) AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap( 54 MRMBase mmb = (MRMBase) AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap(
55 CompileFromDotNetText(script, itemID.ToString()), 55 CompileFromDotNetText(script, itemID.ToString()),
56 "OpenSim.MiniModule"); 56 "OpenSim.MiniModule");
57 m_log.Info("[MRM] Created MRM Instance"); 57 m_log.Info("[MRM] Created MRM Instance");
58 mmb.InitMiniModule(m_world, m_host); 58 mmb.InitMiniModule(m_world, m_host);
59 m_scripts[itemID] = mmb; 59 m_scripts[itemID] = mmb;
60 60
61 m_log.Info("[MRM] Starting MRM"); 61 m_log.Info("[MRM] Starting MRM");
62 mmb.Start(); 62 mmb.Start();
63 } 63 }
64 } 64 }
65 65
66 public void PostInitialise() 66 public void PostInitialise()
67 { 67 {
68 68
69 } 69 }
70 70
71 public void Close() 71 public void Close()
72 { 72 {
73 foreach (KeyValuePair<UUID, MRMBase> pair in m_scripts) 73 foreach (KeyValuePair<UUID, MRMBase> pair in m_scripts)
74 { 74 {
75 pair.Value.Stop(); 75 pair.Value.Stop();
76 } 76 }
77 } 77 }
78 78
79 public string Name 79 public string Name
80 { 80 {
81 get { return "MiniRegionModule"; } 81 get { return "MiniRegionModule"; }
82 } 82 }
83 83
84 public bool IsSharedModule 84 public bool IsSharedModule
85 { 85 {
86 get { return false; } 86 get { return false; }
87 } 87 }
88 88
89 /// <summary> 89 /// <summary>
90 /// Stolen from ScriptEngine Common 90 /// Stolen from ScriptEngine Common
91 /// </summary> 91 /// </summary>
92 /// <param name="Script"></param> 92 /// <param name="Script"></param>
93 /// <param name="uuid">Unique ID for this module</param> 93 /// <param name="uuid">Unique ID for this module</param>
94 /// <returns></returns> 94 /// <returns></returns>
95 internal string CompileFromDotNetText(string Script, string uuid) 95 internal string CompileFromDotNetText(string Script, string uuid)
96 { 96 {
97 const string ext = ".cs"; 97 const string ext = ".cs";
98 const string FilePrefix = "MiniModule"; 98 const string FilePrefix = "MiniModule";
99 99
100 // Output assembly name 100 // Output assembly name
101 string OutFile = Path.Combine("MiniModules", Path.Combine( 101 string OutFile = Path.Combine("MiniModules", Path.Combine(
102 m_scene.RegionInfo.RegionID.ToString(), 102 m_scene.RegionInfo.RegionID.ToString(),
103 FilePrefix + "_compiled_" + uuid + ".dll")); 103 FilePrefix + "_compiled_" + uuid + ".dll"));
104 104
105 // Create Directories for Assemblies 105 // Create Directories for Assemblies
106 if (!Directory.Exists("MiniModules")) 106 if (!Directory.Exists("MiniModules"))
107 Directory.CreateDirectory("MiniModules"); 107 Directory.CreateDirectory("MiniModules");
108 string tmp = Path.Combine("MiniModules", m_scene.RegionInfo.RegionID.ToString()); 108 string tmp = Path.Combine("MiniModules", m_scene.RegionInfo.RegionID.ToString());
109 if (!Directory.Exists(tmp)) 109 if (!Directory.Exists(tmp))
110 Directory.CreateDirectory(tmp); 110 Directory.CreateDirectory(tmp);
111 111
112 try 112 try
113 { 113 {
114 File.Delete(OutFile); 114 File.Delete(OutFile);
115 } 115 }
116 catch (IOException e) 116 catch (IOException e)
117 { 117 {
118 throw new Exception("Unable to delete old existing " + 118 throw new Exception("Unable to delete old existing " +
119 "script-file before writing new. Compile aborted: " + 119 "script-file before writing new. Compile aborted: " +
120 e); 120 e);
121 } 121 }
122 122
123 // DEBUG - write source to disk 123 // DEBUG - write source to disk
124 string srcFileName = FilePrefix + "_source_" + 124 string srcFileName = FilePrefix + "_source_" +
125 Path.GetFileNameWithoutExtension(OutFile) + ext; 125 Path.GetFileNameWithoutExtension(OutFile) + ext;
126 try 126 try
127 { 127 {
128 File.WriteAllText(Path.Combine(Path.Combine( 128 File.WriteAllText(Path.Combine(Path.Combine(
129 "MiniModules", 129 "MiniModules",
130 m_scene.RegionInfo.RegionID.ToString()), 130 m_scene.RegionInfo.RegionID.ToString()),
131 srcFileName), Script); 131 srcFileName), Script);
132 } 132 }
133 catch (Exception ex) //NOTLEGIT - Should be just FileIOException 133 catch (Exception ex) //NOTLEGIT - Should be just FileIOException
134 { 134 {
135 m_log.Error("[Compiler]: Exception while " + 135 m_log.Error("[Compiler]: Exception while " +
136 "trying to write script source to file \"" + 136 "trying to write script source to file \"" +
137 srcFileName + "\": " + ex.ToString()); 137 srcFileName + "\": " + ex.ToString());
138 } 138 }
139 139
140 // Do actual compile 140 // Do actual compile
141 CompilerParameters parameters = new CompilerParameters(); 141 CompilerParameters parameters = new CompilerParameters();
142 142
143 parameters.IncludeDebugInformation = true; 143 parameters.IncludeDebugInformation = true;
144 144
145 string rootPath = 145 string rootPath =
146 Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory); 146 Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
147 147
148 148
149 // TODO: Add Libraries 149 // TODO: Add Libraries
150 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, 150 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
151 "OpenSim.Region.OptionalModules.dll")); 151 "OpenSim.Region.OptionalModules.dll"));
152 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, 152 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
153 "log4net.dll")); 153 "log4net.dll"));
154 154
155 parameters.GenerateExecutable = false; 155 parameters.GenerateExecutable = false;
156 parameters.OutputAssembly = OutFile; 156 parameters.OutputAssembly = OutFile;
157 parameters.IncludeDebugInformation = true; 157 parameters.IncludeDebugInformation = true;
158 parameters.TreatWarningsAsErrors = false; 158 parameters.TreatWarningsAsErrors = false;
159 159
160 CompilerResults results = CScodeProvider.CompileAssemblyFromSource( 160 CompilerResults results = CScodeProvider.CompileAssemblyFromSource(
161 parameters, Script); 161 parameters, Script);
162 162
163 int display = 5; 163 int display = 5;
164 if (results.Errors.Count > 0) 164 if (results.Errors.Count > 0)
165 { 165 {
166 string errtext = String.Empty; 166 string errtext = String.Empty;
167 foreach (CompilerError CompErr in results.Errors) 167 foreach (CompilerError CompErr in results.Errors)
168 { 168 {
169 // Show 5 errors max 169 // Show 5 errors max
170 // 170 //
171 if (display <= 0) 171 if (display <= 0)
172 break; 172 break;
173 display--; 173 display--;
174 174
175 string severity = "Error"; 175 string severity = "Error";
176 if (CompErr.IsWarning) 176 if (CompErr.IsWarning)
177 { 177 {
178 severity = "Warning"; 178 severity = "Warning";
179 } 179 }
180 180
181 string text = CompErr.ErrorText; 181 string text = CompErr.ErrorText;
182 182
183 // The Second Life viewer's script editor begins 183 // The Second Life viewer's script editor begins
184 // countingn lines and columns at 0, so we subtract 1. 184 // countingn lines and columns at 0, so we subtract 1.
185 errtext += String.Format("Line ({0},{1}): {4} {2}: {3}\n", 185 errtext += String.Format("Line ({0},{1}): {4} {2}: {3}\n",
186 CompErr.Line - 1, CompErr.Column - 1, 186 CompErr.Line - 1, CompErr.Column - 1,
187 CompErr.ErrorNumber, text, severity); 187 CompErr.ErrorNumber, text, severity);
188 } 188 }
189 189
190 if (!File.Exists(OutFile)) 190 if (!File.Exists(OutFile))
191 { 191 {
192 throw new Exception(errtext); 192 throw new Exception(errtext);
193 } 193 }
194 } 194 }
195 195
196 if (!File.Exists(OutFile)) 196 if (!File.Exists(OutFile))
197 { 197 {
198 string errtext = String.Empty; 198 string errtext = String.Empty;
199 errtext += "No compile error. But not able to locate compiled file."; 199 errtext += "No compile error. But not able to locate compiled file.";
200 throw new Exception(errtext); 200 throw new Exception(errtext);
201 } 201 }
202 202
203 FileInfo fi = new FileInfo(OutFile); 203 FileInfo fi = new FileInfo(OutFile);
204 204
205 Byte[] data = new Byte[fi.Length]; 205 Byte[] data = new Byte[fi.Length];
206 206
207 try 207 try
208 { 208 {
209 FileStream fs = File.Open(OutFile, FileMode.Open, FileAccess.Read); 209 FileStream fs = File.Open(OutFile, FileMode.Open, FileAccess.Read);
210 fs.Read(data, 0, data.Length); 210 fs.Read(data, 0, data.Length);
211 fs.Close(); 211 fs.Close();
212 } 212 }
213 catch (IOException) 213 catch (IOException)
214 { 214 {
215 string errtext = String.Empty; 215 string errtext = String.Empty;
216 errtext += "No compile error. But not able to open file."; 216 errtext += "No compile error. But not able to open file.";
217 throw new Exception(errtext); 217 throw new Exception(errtext);
218 } 218 }
219 219
220 // Convert to base64 220 // Convert to base64
221 // 221 //
222 string filetext = Convert.ToBase64String(data); 222 string filetext = Convert.ToBase64String(data);
223 223
224 ASCIIEncoding enc = new ASCIIEncoding(); 224 ASCIIEncoding enc = new ASCIIEncoding();
225 225
226 Byte[] buf = enc.GetBytes(filetext); 226 Byte[] buf = enc.GetBytes(filetext);
227 227
228 FileStream sfs = File.Create(OutFile + ".cil.b64"); 228 FileStream sfs = File.Create(OutFile + ".cil.b64");
229 sfs.Write(buf, 0, buf.Length); 229 sfs.Write(buf, 0, buf.Length);
230 sfs.Close(); 230 sfs.Close();
231 231
232 return OutFile; 232 return OutFile;
233 } 233 }
234 } 234 }
235} 235}