diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/LSOEngine/LSO/Engine.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/LSOEngine/LSO/Engine.cs | 293 |
1 files changed, 293 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/LSOEngine/LSO/Engine.cs b/OpenSim/Region/ScriptEngine/LSOEngine/LSO/Engine.cs new file mode 100644 index 0000000..27acc9c --- /dev/null +++ b/OpenSim/Region/ScriptEngine/LSOEngine/LSO/Engine.cs | |||
@@ -0,0 +1,293 @@ | |||
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 OpenSim 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 | * | ||
27 | */ | ||
28 | /* Original code: Tedd Hansen */ | ||
29 | using System; | ||
30 | using System.IO; | ||
31 | using System.Reflection; | ||
32 | using System.Reflection.Emit; | ||
33 | using System.Text; | ||
34 | using System.Threading; | ||
35 | using OpenSim.Region.ScriptEngine.Common; | ||
36 | using OpenSim.Region.ScriptEngine.LSOEngine.LSO; | ||
37 | |||
38 | namespace OpenSim.Region.ScriptEngine.LSOEngine.LSO | ||
39 | { | ||
40 | public class Engine | ||
41 | { | ||
42 | //private string LSO_FileName = @"LSO\AdditionTest.lso"; | ||
43 | private string LSO_FileName; // = @"LSO\CloseToDefault.lso"; | ||
44 | private AppDomain appDomain; | ||
45 | |||
46 | public string Compile(string LSOFileName) | ||
47 | { | ||
48 | LSO_FileName = LSOFileName; | ||
49 | |||
50 | |||
51 | //appDomain = AppDomain.CreateDomain("AlternateAppDomain"); | ||
52 | appDomain = Thread.GetDomain(); | ||
53 | |||
54 | // Create Assembly Name | ||
55 | AssemblyName asmName = new AssemblyName(); | ||
56 | asmName.Name = Path.GetFileNameWithoutExtension(LSO_FileName); | ||
57 | //asmName.Name = "TestAssembly"; | ||
58 | |||
59 | string DLL_FileName = asmName.Name + ".dll"; | ||
60 | string DLL_FileName_WithPath = Path.GetDirectoryName(LSO_FileName) + @"\" + DLL_FileName; | ||
61 | |||
62 | LSOEngine.LSO.Common.SendToLog("LSO File Name: " + Path.GetFileName(LSO_FileName)); | ||
63 | LSOEngine.LSO.Common.SendToLog("Assembly name: " + asmName.Name); | ||
64 | LSOEngine.LSO.Common.SendToLog("Assembly File Name: " + asmName.Name + ".dll"); | ||
65 | LSOEngine.LSO.Common.SendToLog("Starting processing of LSL ByteCode..."); | ||
66 | LSOEngine.LSO.Common.SendToLog(""); | ||
67 | |||
68 | |||
69 | // Create Assembly | ||
70 | AssemblyBuilder asmBuilder = appDomain.DefineDynamicAssembly( | ||
71 | asmName, | ||
72 | AssemblyBuilderAccess.RunAndSave | ||
73 | ); | ||
74 | //// Create Assembly | ||
75 | //AssemblyBuilder asmBuilder = | ||
76 | // Thread.GetDomain().DefineDynamicAssembly | ||
77 | //(asmName, AssemblyBuilderAccess.RunAndSave); | ||
78 | |||
79 | // Create a module (and save to disk) | ||
80 | ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule | ||
81 | (asmName.Name, | ||
82 | DLL_FileName); | ||
83 | |||
84 | //Common.SendToDebug("asmName.Name is still \"" + asmName.Name + "\""); | ||
85 | // Create a Class (/Type) | ||
86 | TypeBuilder typeBuilder = modBuilder.DefineType( | ||
87 | "LSL_ScriptObject", | ||
88 | TypeAttributes.Public | TypeAttributes.BeforeFieldInit, | ||
89 | typeof (LSL_BaseClass)); | ||
90 | //, | ||
91 | // typeof()); | ||
92 | //, typeof(LSL_BuiltIn_Commands_Interface)); | ||
93 | //, | ||
94 | // typeof(object), | ||
95 | // new Type[] { typeof(LSL_CLRInterface.LSLScript) }); | ||
96 | |||
97 | |||
98 | /* | ||
99 | * Generate the IL itself | ||
100 | */ | ||
101 | |||
102 | LSO_Parser LSOP = new LSO_Parser(LSO_FileName, typeBuilder); | ||
103 | LSOP.OpenFile(); | ||
104 | LSOP.Parse(); | ||
105 | |||
106 | // Constructor has to be created AFTER LSO_Parser because of accumulated variables | ||
107 | if (LSOEngine.LSO.Common.IL_CreateConstructor) | ||
108 | IL_CREATE_CONSTRUCTOR(typeBuilder, LSOP); | ||
109 | |||
110 | LSOP.CloseFile(); | ||
111 | /* | ||
112 | * Done generating. Create a type and run it. | ||
113 | */ | ||
114 | |||
115 | |||
116 | LSOEngine.LSO.Common.SendToLog("Attempting to compile assembly..."); | ||
117 | // Compile it | ||
118 | Type type = typeBuilder.CreateType(); | ||
119 | LSOEngine.LSO.Common.SendToLog("Compilation successful!"); | ||
120 | |||
121 | LSOEngine.LSO.Common.SendToLog("Saving assembly: " + DLL_FileName); | ||
122 | asmBuilder.Save(DLL_FileName); | ||
123 | |||
124 | LSOEngine.LSO.Common.SendToLog("Returning assembly filename: " + DLL_FileName); | ||
125 | |||
126 | |||
127 | return DLL_FileName; | ||
128 | |||
129 | |||
130 | //Common.SendToLog("Creating an instance of new assembly..."); | ||
131 | //// Create an instance we can play with | ||
132 | ////LSLScript hello = (LSLScript)Activator.CreateInstance(type); | ||
133 | ////LSL_CLRInterface.LSLScript MyScript = (LSL_CLRInterface.LSLScript)Activator.CreateInstance(type); | ||
134 | //object MyScript = (object)Activator.CreateInstance(type); | ||
135 | |||
136 | |||
137 | //System.Reflection.MemberInfo[] Members = type.GetMembers(); | ||
138 | |||
139 | //Common.SendToLog("Members of assembly " + type.ToString() + ":"); | ||
140 | //foreach (MemberInfo member in Members) | ||
141 | // Common.SendToLog(member.ToString()); | ||
142 | |||
143 | |||
144 | //// Play with it | ||
145 | ////MyScript.event_state_entry("Test"); | ||
146 | //object[] args = { null }; | ||
147 | ////System.Collections.Generic.List<string> Functions = (System.Collections.Generic.List<string>)type.InvokeMember("GetFunctions", BindingFlags.InvokeMethod, null, MyScript, null); | ||
148 | |||
149 | //string[] ret = { }; | ||
150 | //if (Common.IL_CreateFunctionList) | ||
151 | // ret = (string[])type.InvokeMember("GetFunctions", BindingFlags.InvokeMethod, null, MyScript, null); | ||
152 | |||
153 | //foreach (string s in ret) | ||
154 | //{ | ||
155 | // Common.SendToLog(""); | ||
156 | // Common.SendToLog("*** Executing LSL Server Event: " + s); | ||
157 | // //object test = type.GetMember(s); | ||
158 | // //object runner = type.InvokeMember(s, BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, MyScript, args); | ||
159 | // //runner(); | ||
160 | // //objBooks_Late = type.InvokeMember(s, BindingFlags.CreateInstance, null, objApp_Late, null); | ||
161 | // type.InvokeMember(s, BindingFlags.InvokeMethod, null, MyScript, new object[] { "Test" }); | ||
162 | |||
163 | //} | ||
164 | } | ||
165 | |||
166 | |||
167 | private static void IL_CREATE_CONSTRUCTOR(TypeBuilder typeBuilder, LSO_Parser LSOP) | ||
168 | { | ||
169 | LSOEngine.LSO.Common.SendToDebug("IL_CREATE_CONSTRUCTOR()"); | ||
170 | //ConstructorBuilder constructor = typeBuilder.DefineConstructor( | ||
171 | // MethodAttributes.Public, | ||
172 | // CallingConventions.Standard, | ||
173 | // new Type[0]); | ||
174 | ConstructorBuilder constructor = typeBuilder.DefineConstructor( | ||
175 | MethodAttributes.Public | | ||
176 | MethodAttributes.SpecialName | | ||
177 | MethodAttributes.RTSpecialName, | ||
178 | CallingConventions.Standard, | ||
179 | new Type[0]); | ||
180 | |||
181 | //Define the reflection ConstructorInfor for System.Object | ||
182 | ConstructorInfo conObj = typeof (LSL_BaseClass).GetConstructor(new Type[0]); | ||
183 | |||
184 | //call constructor of base object | ||
185 | ILGenerator il = constructor.GetILGenerator(); | ||
186 | |||
187 | il.Emit(OpCodes.Ldarg_0); | ||
188 | il.Emit(OpCodes.Call, conObj); | ||
189 | |||
190 | |||
191 | //Common.SendToDebug("IL_CREATE_CONSTRUCTOR: Creating global: UInt32 State = 0;"); | ||
192 | //string FieldName; | ||
193 | //// Create state object | ||
194 | //FieldName = "State"; | ||
195 | //FieldBuilder State_fb = typeBuilder.DefineField( | ||
196 | // FieldName, | ||
197 | // typeof(UInt32), | ||
198 | // FieldAttributes.Public); | ||
199 | //il.Emit(OpCodes.Ldarg_0); | ||
200 | //il.Emit(OpCodes.Ldc_I4, 0); | ||
201 | //il.Emit(OpCodes.Stfld, State_fb); | ||
202 | |||
203 | |||
204 | //Common.SendToDebug("IL_CREATE_CONSTRUCTOR: Creating global: LSL_BuiltIn_Commands_TestImplementation LSL_BuiltIns = New LSL_BuiltIn_Commands_TestImplementation();"); | ||
205 | ////Type objType1 = typeof(object); | ||
206 | //Type objType1 = typeof(LSL_BuiltIn_Commands_TestImplementation); | ||
207 | |||
208 | //FieldName = "LSL_BuiltIns"; | ||
209 | //FieldBuilder LSL_BuiltIns_fb = typeBuilder.DefineField( | ||
210 | // FieldName, | ||
211 | // objType1, | ||
212 | // FieldAttributes.Public); | ||
213 | |||
214 | ////LSL_BuiltIn_Commands_TestImplementation _ti = new LSL_BuiltIn_Commands_TestImplementation(); | ||
215 | //il.Emit(OpCodes.Ldarg_0); | ||
216 | ////il.Emit(OpCodes.Ldstr, "Test 123"); | ||
217 | //il.Emit(OpCodes.Newobj, objType1.GetConstructor(new Type[] { })); | ||
218 | //il.Emit(OpCodes.Stfld, LSL_BuiltIns_fb); | ||
219 | |||
220 | foreach (UInt32 pos in LSOP.StaticBlocks.Keys) | ||
221 | { | ||
222 | LSO_Struct.StaticBlock sb; | ||
223 | LSOP.StaticBlocks.TryGetValue(pos, out sb); | ||
224 | |||
225 | if (sb.ObjectType > 0 && sb.ObjectType < 8) | ||
226 | { | ||
227 | // We don't want void or null's | ||
228 | |||
229 | il.Emit(OpCodes.Ldarg_0); | ||
230 | // Push position to stack | ||
231 | il.Emit(OpCodes.Ldc_I4, pos); | ||
232 | //il.Emit(OpCodes.Box, typeof(UInt32)); | ||
233 | |||
234 | |||
235 | Type datatype = null; | ||
236 | |||
237 | // Push data to stack | ||
238 | LSOEngine.LSO.Common.SendToDebug("Adding to static (" + pos + ") type: " + | ||
239 | ((LSO_Enums.Variable_Type_Codes) sb.ObjectType).ToString() + " (" + sb.ObjectType + | ||
240 | ")"); | ||
241 | switch ((LSO_Enums.Variable_Type_Codes) sb.ObjectType) | ||
242 | { | ||
243 | case LSO_Enums.Variable_Type_Codes.Float: | ||
244 | case LSO_Enums.Variable_Type_Codes.Integer: | ||
245 | //UInt32 | ||
246 | il.Emit(OpCodes.Ldc_I4, BitConverter.ToUInt32(sb.BlockVariable, 0)); | ||
247 | datatype = typeof (UInt32); | ||
248 | il.Emit(OpCodes.Box, datatype); | ||
249 | break; | ||
250 | case LSO_Enums.Variable_Type_Codes.String: | ||
251 | case LSO_Enums.Variable_Type_Codes.Key: | ||
252 | //String | ||
253 | LSO_Struct.HeapBlock hb = | ||
254 | LSOP.GetHeap(LSOP.myHeader.HR + BitConverter.ToUInt32(sb.BlockVariable, 0) - 1); | ||
255 | il.Emit(OpCodes.Ldstr, Encoding.UTF8.GetString(hb.Data)); | ||
256 | datatype = typeof (string); | ||
257 | break; | ||
258 | case LSO_Enums.Variable_Type_Codes.Vector: | ||
259 | datatype = typeof (LSO_Enums.Vector); | ||
260 | //TODO: Not implemented | ||
261 | break; | ||
262 | case LSO_Enums.Variable_Type_Codes.Rotation: | ||
263 | //Object | ||
264 | //TODO: Not implemented | ||
265 | datatype = typeof (LSO_Enums.Rotation); | ||
266 | break; | ||
267 | default: | ||
268 | datatype = typeof (object); | ||
269 | break; | ||
270 | } | ||
271 | |||
272 | |||
273 | // Make call | ||
274 | il.Emit(OpCodes.Call, | ||
275 | typeof (LSL_BaseClass).GetMethod("AddToStatic", new Type[] {typeof (UInt32), datatype})); | ||
276 | } | ||
277 | } | ||
278 | |||
279 | |||
280 | ////il.Emit(OpCodes.Newobj, typeof(UInt32)); | ||
281 | //il.Emit(OpCodes.Starg_0); | ||
282 | //// Create LSL function library | ||
283 | //FieldBuilder LSL_BuiltIns_fb = typeBuilder.DefineField("LSL_BuiltIns", typeof(LSL_BuiltIn_Commands_Interface), FieldAttributes.Public); | ||
284 | //il.Emit(OpCodes.Newobj, typeof(LSL_BuiltIn_Commands_Interface)); | ||
285 | //il.Emit(OpCodes.Stloc_1); | ||
286 | |||
287 | il.Emit(OpCodes.Ret); | ||
288 | } | ||
289 | |||
290 | |||
291 | // End of class | ||
292 | } | ||
293 | } \ No newline at end of file | ||