aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs')
-rw-r--r--OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs140
1 files changed, 140 insertions, 0 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
new file mode 100644
index 0000000..814f15a
--- /dev/null
+++ b/OpenSim/Region/Environment/Scenes/scripting/Engines/LSLEngine/LSLHandler/Engine.cs
@@ -0,0 +1,140 @@
1using System;
2using System.Reflection;
3using System.Reflection.Emit;
4using System.Threading;
5
6using OpenSim.Region.Environment.Scripting;
7
8namespace OpenSim.ScriptEngines.LSL
9{
10
11
12 public class Engine
13 {
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
52 // Create type object for the class (after defining fields and methods)
53 Type type = typeBuilder.CreateType();
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 {
67
68
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
81 typeBuilder.DefineMethodOverride(methodBuilder,
82 typeof(LSL_CLRInterface.LSLScript).GetMethod("event_state_entry"));
83
84 // Create the IL generator
85 ILGenerator il = methodBuilder.GetILGenerator();
86
87
88 /*
89 * TRY
90 */
91 il.BeginExceptionBlock();
92
93 // Push "Hello World!" string to stack
94 il.Emit(OpCodes.Ldstr, "Hello World!");
95
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
100 //il.EmitCall(OpCodes.Callvirt
101 //il.Emit(OpCodes.Call, typeof(WorldAPI).GetMethod
102 //("TestFunction"));
103
104
105 //il.ThrowException(typeof(NotSupportedException));
106
107
108 /*
109 * CATCH
110 */
111 il.BeginCatchBlock(typeof(Exception));
112
113 // Push "Hello World!" string to stack
114 il.Emit(OpCodes.Ldstr, "Something went wrong: ");
115
116 //call void [mscorlib]System.Console::WriteLine(string)
117 il.Emit(OpCodes.Call, typeof(Console).GetMethod
118 ("Write", new Type[] { typeof(string) }));
119
120 //callvirt instance string [mscorlib]System.Exception::get_Message()
121 il.Emit(OpCodes.Callvirt, typeof(Exception).GetMethod
122 ("get_Message"));
123
124 //call void [mscorlib]System.Console::WriteLine(string)
125 il.Emit(OpCodes.Call, typeof(Console).GetMethod
126 ("WriteLine", new Type[] { typeof(string) }));
127
128 /*
129 * END TRY
130 */
131 il.EndExceptionBlock();
132
133
134 // Push "Return from current method, with return value if present" to stack
135 il.Emit(OpCodes.Ret);
136
137
138 }
139 }
140}