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