diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs | 316 |
1 files changed, 202 insertions, 114 deletions
diff --git a/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs b/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs index 012a00e..5cd1f71 100644 --- a/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs +++ b/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs | |||
@@ -1,140 +1,228 @@ | |||
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 */ | ||
1 | using System; | 29 | using System; |
2 | using System.Reflection; | 30 | using System.Reflection; |
3 | using System.Reflection.Emit; | 31 | using System.Reflection.Emit; |
4 | using System.Threading; | 32 | using System.Threading; |
5 | 33 | ||
6 | using OpenSim.Region.Scripting; | 34 | namespace OpenSim.Region.Scripting.LSL |
7 | |||
8 | namespace OpenSim.ScriptEngines.LSL | ||
9 | { | ||
10 | |||
11 | |||
12 | public class Engine | ||
13 | { | 35 | { |
14 | public void Start(ScriptInfo WorldAPI) | ||
15 | { | ||
16 | |||
17 | |||
18 | |||
19 | // Create Assembly Name | ||
20 | AssemblyName asmName = new AssemblyName(); | ||
21 | asmName.Name = "TestAssembly"; | ||
22 | |||
23 | // Create Assembly | ||
24 | AssemblyBuilder asmBuilder = | ||
25 | Thread.GetDomain().DefineDynamicAssembly | ||
26 | (asmName, AssemblyBuilderAccess.RunAndSave); | ||
27 | |||
28 | // Create a module (and save to disk) | ||
29 | ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule | ||
30 | (asmName.Name, asmName.Name + ".dll"); | ||
31 | |||
32 | // Create a Class (/Type) | ||
33 | TypeBuilder typeBuilder = modBuilder.DefineType( | ||
34 | "MyClass", | ||
35 | TypeAttributes.Public, | ||
36 | typeof(object), | ||
37 | new Type[] { typeof(LSL_CLRInterface.LSLScript) }); | ||
38 | |||
39 | |||
40 | |||
41 | /* | ||
42 | * Generate the IL itself | ||
43 | */ | ||
44 | |||
45 | GenerateIL(WorldAPI, typeBuilder); | ||
46 | |||
47 | |||
48 | /* | ||
49 | * Done generating, create a type and run it. | ||
50 | */ | ||
51 | 36 | ||
52 | // Create type object for the class (after defining fields and methods) | 37 | |
53 | Type type = typeBuilder.CreateType(); | 38 | public class Engine |
54 | |||
55 | asmBuilder.Save("TestAssembly.dll"); | ||
56 | |||
57 | // Create an instance we can play with | ||
58 | //LSLScript hello = (LSLScript)Activator.CreateInstance(type); | ||
59 | LSL_CLRInterface.LSLScript MyScript = (LSL_CLRInterface.LSLScript)Activator.CreateInstance(type); | ||
60 | |||
61 | // Play with it | ||
62 | MyScript.event_state_entry("Test"); | ||
63 | } | ||
64 | |||
65 | private void GenerateIL(ScriptInfo WorldAPI, TypeBuilder typeBuilder) | ||
66 | { | 39 | { |
40 | //private string LSO_FileName = @"LSO\AdditionTest.lso"; | ||
41 | private string LSO_FileName;// = @"LSO\CloseToDefault.lso"; | ||
42 | AppDomain appDomain; | ||
67 | 43 | ||
44 | public void Start(string FileName) | ||
45 | { | ||
46 | LSO_FileName = FileName; | ||
68 | 47 | ||
69 | // For debug | ||
70 | LSO_Parser LSOP = new LSO_Parser(); | ||
71 | LSOP.ParseFile("LSO\\CloseToDefault.lso", WorldAPI, ref typeBuilder); | ||
72 | return; | ||
73 | |||
74 | |||
75 | // Override a Method / Function | ||
76 | MethodBuilder methodBuilder = typeBuilder.DefineMethod("event_state_entry", | ||
77 | MethodAttributes.Private | MethodAttributes.Virtual, | ||
78 | typeof(void), | ||
79 | new Type[] { typeof(object) }); | ||
80 | 48 | ||
81 | typeBuilder.DefineMethodOverride(methodBuilder, | 49 | //appDomain = AppDomain.CreateDomain("AlternateAppDomain"); |
82 | typeof(LSL_CLRInterface.LSLScript).GetMethod("event_state_entry")); | 50 | appDomain = Thread.GetDomain(); |
83 | |||
84 | // Create the IL generator | ||
85 | ILGenerator il = methodBuilder.GetILGenerator(); | ||
86 | 51 | ||
52 | // Create Assembly Name | ||
53 | AssemblyName asmName = new AssemblyName(); | ||
54 | asmName.Name = System.IO.Path.GetFileNameWithoutExtension(LSO_FileName); | ||
55 | //asmName.Name = "TestAssembly"; | ||
87 | 56 | ||
88 | /* | 57 | string DLL_FileName = asmName.Name + ".dll"; |
89 | * TRY | 58 | string DLL_FileName_WithPath = System.IO.Path.GetDirectoryName(FileName) + @"\" + DLL_FileName; |
90 | */ | ||
91 | il.BeginExceptionBlock(); | ||
92 | 59 | ||
93 | // Push "Hello World!" string to stack | 60 | Common.SendToLog("LSO File Name: " + System.IO.Path.GetFileName(FileName)); |
94 | il.Emit(OpCodes.Ldstr, "Hello World!"); | 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(""); | ||
95 | 65 | ||
96 | // Push Console.WriteLine command to stack ... Console.WriteLine("Hello World!"); | ||
97 | il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
98 | ("WriteLine", new Type[] { typeof(string) })); | ||
99 | 66 | ||
100 | //il.EmitCall(OpCodes.Callvirt | 67 | |
101 | //il.Emit(OpCodes.Call, typeof(WorldAPI).GetMethod | 68 | // Create Assembly |
102 | //("TestFunction")); | 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 \"" + asmName.Name + "\""); | ||
84 | // Create a Class (/Type) | ||
85 | TypeBuilder typeBuilder = modBuilder.DefineType( | ||
86 | "LSL_ScriptObject", | ||
87 | TypeAttributes.Public | TypeAttributes.BeforeFieldInit); | ||
88 | //, | ||
89 | // typeof()); | ||
90 | //, typeof(LSL_BuiltIn_Commands_Interface)); | ||
91 | //, | ||
92 | // typeof(object), | ||
93 | // new Type[] { typeof(LSL_CLRInterface.LSLScript) }); | ||
94 | |||
95 | |||
96 | if (Common.IL_CreateConstructor) | ||
97 | IL_CREATE_CONSTRUCTOR(typeBuilder); | ||
98 | |||
103 | 99 | ||
100 | /* | ||
101 | * Generate the IL itself | ||
102 | */ | ||
104 | 103 | ||
105 | //il.ThrowException(typeof(NotSupportedException)); | 104 | LSO_Parser LSOP = new LSO_Parser(); |
105 | LSOP.ParseFile(LSO_FileName, typeBuilder); | ||
106 | 106 | ||
107 | 107 | /* | |
108 | /* | 108 | * Done generating. Create a type and run it. |
109 | * CATCH | 109 | */ |
110 | */ | ||
111 | il.BeginCatchBlock(typeof(Exception)); | ||
112 | 110 | ||
113 | // Push "Hello World!" string to stack | ||
114 | il.Emit(OpCodes.Ldstr, "Something went wrong: "); | ||
115 | 111 | ||
116 | //call void [mscorlib]System.Console::WriteLine(string) | ||
117 | il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
118 | ("Write", new Type[] { typeof(string) })); | ||
119 | 112 | ||
120 | //callvirt instance string [mscorlib]System.Exception::get_Message() | 113 | Common.SendToLog("Attempting to compile assembly..."); |
121 | il.Emit(OpCodes.Callvirt, typeof(Exception).GetMethod | 114 | // Compile it |
122 | ("get_Message")); | 115 | Type type = typeBuilder.CreateType(); |
123 | 116 | Common.SendToLog("Compilation successful!"); | |
124 | //call void [mscorlib]System.Console::WriteLine(string) | ||
125 | il.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
126 | ("WriteLine", new Type[] { typeof(string) })); | ||
127 | 117 | ||
128 | /* | 118 | Common.SendToLog("Saving assembly: " + DLL_FileName); |
129 | * END TRY | 119 | asmBuilder.Save(DLL_FileName); |
130 | */ | ||
131 | il.EndExceptionBlock(); | ||
132 | 120 | ||
133 | 121 | ||
134 | // Push "Return from current method, with return value if present" to stack | 122 | Common.SendToLog("Creating an instance of new assembly..."); |
135 | il.Emit(OpCodes.Ret); | 123 | // Create an instance we can play with |
124 | //LSLScript hello = (LSLScript)Activator.CreateInstance(type); | ||
125 | //LSL_CLRInterface.LSLScript MyScript = (LSL_CLRInterface.LSLScript)Activator.CreateInstance(type); | ||
126 | object MyScript = (object)Activator.CreateInstance(type); | ||
136 | 127 | ||
137 | 128 | ||
129 | Common.SendToLog(""); | ||
130 | |||
131 | System.Reflection.MemberInfo[] Members = type.GetMembers(); | ||
132 | |||
133 | Common.SendToLog("Members of assembly " + type.ToString () + ":"); | ||
134 | foreach (MemberInfo member in Members ) | ||
135 | Common.SendToLog(member.ToString()); | ||
136 | |||
137 | |||
138 | // Play with it | ||
139 | //MyScript.event_state_entry("Test"); | ||
140 | object[] args = { null } ; | ||
141 | //System.Collections.Generic.List<string> Functions = (System.Collections.Generic.List<string>)type.InvokeMember("GetFunctions", BindingFlags.InvokeMethod, null, MyScript, null); | ||
142 | |||
143 | string[] ret = { }; | ||
144 | if (Common.IL_CreateFunctionList) | ||
145 | ret = (string[])type.InvokeMember("GetFunctions", BindingFlags.InvokeMethod, null, MyScript, null); | ||
146 | |||
147 | foreach (string s in ret) | ||
148 | { | ||
149 | Common.SendToLog(""); | ||
150 | Common.SendToLog("*** Executing LSL Server Event: " + s); | ||
151 | //object test = type.GetMember(s); | ||
152 | //object runner = type.InvokeMember(s, BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, MyScript, args); | ||
153 | //runner(); | ||
154 | //objBooks_Late = type.InvokeMember(s, BindingFlags.CreateInstance, null, objApp_Late, null); | ||
155 | type.InvokeMember(s, BindingFlags.InvokeMethod, null, MyScript, new object[] { "Test" }); | ||
156 | |||
157 | } | ||
158 | |||
159 | } | ||
160 | |||
161 | |||
162 | private static void IL_CREATE_CONSTRUCTOR(TypeBuilder typeBuilder) | ||
163 | { | ||
164 | |||
165 | |||
166 | Common.SendToDebug("IL_CREATE_CONSTRUCTOR()"); | ||
167 | //ConstructorBuilder constructor = typeBuilder.DefineConstructor( | ||
168 | // MethodAttributes.Public, | ||
169 | // CallingConventions.Standard, | ||
170 | // new Type[0]); | ||
171 | ConstructorBuilder constructor = typeBuilder.DefineConstructor( | ||
172 | MethodAttributes.Public | | ||
173 | MethodAttributes.SpecialName | | ||
174 | MethodAttributes.RTSpecialName, | ||
175 | CallingConventions.Standard, | ||
176 | new Type[0]); | ||
177 | |||
178 | //Define the reflection ConstructorInfor for System.Object | ||
179 | ConstructorInfo conObj = typeof(object).GetConstructor(new Type[0]); | ||
180 | |||
181 | //call constructor of base object | ||
182 | ILGenerator il = constructor.GetILGenerator(); | ||
183 | |||
184 | Common.SendToDebug("IL_CREATE_CONSTRUCTOR: Creating global: UInt32 State = 0;"); | ||
185 | string FieldName; | ||
186 | // Create state object | ||
187 | FieldName = "State"; | ||
188 | FieldBuilder State_fb = typeBuilder.DefineField( | ||
189 | FieldName, | ||
190 | typeof(UInt32), | ||
191 | FieldAttributes.Public); | ||
192 | il.Emit(OpCodes.Ldarg_0); | ||
193 | il.Emit(OpCodes.Ldc_I4, 0); | ||
194 | il.Emit(OpCodes.Stfld, State_fb); | ||
195 | |||
196 | |||
197 | Common.SendToDebug("IL_CREATE_CONSTRUCTOR: Creating global: LSL_BuiltIn_Commands_TestImplementation LSL_BuiltIns = New LSL_BuiltIn_Commands_TestImplementation();"); | ||
198 | //Type objType1 = typeof(object); | ||
199 | Type objType1 = typeof(LSL_BuiltIn_Commands_TestImplementation); | ||
200 | |||
201 | FieldName = "LSL_BuiltIns"; | ||
202 | FieldBuilder LSL_BuiltIns_fb = typeBuilder.DefineField( | ||
203 | FieldName, | ||
204 | objType1, | ||
205 | FieldAttributes.Public); | ||
206 | |||
207 | //LSL_BuiltIn_Commands_TestImplementation _ti = new LSL_BuiltIn_Commands_TestImplementation(); | ||
208 | il.Emit(OpCodes.Ldarg_0); | ||
209 | //il.Emit(OpCodes.Ldstr, "Test 123"); | ||
210 | il.Emit(OpCodes.Newobj, objType1.GetConstructor(new Type[] { })); | ||
211 | il.Emit(OpCodes.Stfld, LSL_BuiltIns_fb); | ||
212 | |||
213 | il.Emit(OpCodes.Ldarg_0); | ||
214 | il.Emit(OpCodes.Call, conObj); | ||
215 | |||
216 | |||
217 | |||
218 | ////il.Emit(OpCodes.Newobj, typeof(UInt32)); | ||
219 | //il.Emit(OpCodes.Starg_0); | ||
220 | //// Create LSL function library | ||
221 | //FieldBuilder LSL_BuiltIns_fb = typeBuilder.DefineField("LSL_BuiltIns", typeof(LSL_BuiltIn_Commands_Interface), FieldAttributes.Public); | ||
222 | //il.Emit(OpCodes.Newobj, typeof(LSL_BuiltIn_Commands_Interface)); | ||
223 | //il.Emit(OpCodes.Stloc_1); | ||
224 | |||
225 | il.Emit(OpCodes.Ret); | ||
226 | } | ||
138 | } | 227 | } |
139 | } | 228 | } |
140 | } | ||