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