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