diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/BaseClassFactory.cs | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/BaseClassFactory.cs b/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/BaseClassFactory.cs new file mode 100644 index 0000000..2f19cae --- /dev/null +++ b/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/BaseClassFactory.cs | |||
@@ -0,0 +1,213 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.IO; | ||
4 | using System.Reflection; | ||
5 | using System.Reflection.Emit; | ||
6 | using System.Text; | ||
7 | using OpenSim.ScriptEngine.Shared; | ||
8 | |||
9 | namespace OpenSim.ScriptEngine.Components.DotNetEngine.Scheduler | ||
10 | { | ||
11 | public class BaseClassFactory | ||
12 | { | ||
13 | |||
14 | |||
15 | public static void MakeBaseClass(ScriptStructure script) | ||
16 | { | ||
17 | string asmName = "ScriptAssemblies"; | ||
18 | string ModuleID = asmName; | ||
19 | string ClassID = "Script"; | ||
20 | string moveToDir = "ScriptEngines"; | ||
21 | string asmFileName = ModuleID + "_" + ClassID + ".dll"; | ||
22 | if (!Directory.Exists(moveToDir)) | ||
23 | Directory.CreateDirectory(moveToDir); | ||
24 | |||
25 | ILGenerator ilgen; | ||
26 | AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( | ||
27 | new AssemblyName(asmName), AssemblyBuilderAccess.RunAndSave); | ||
28 | |||
29 | // The module builder | ||
30 | ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule(ModuleID, asmFileName); | ||
31 | |||
32 | // The class builder | ||
33 | TypeBuilder classBuilder = modBuilder.DefineType(ClassID, TypeAttributes.Class | TypeAttributes.Public); | ||
34 | |||
35 | // The default constructor | ||
36 | ConstructorBuilder ctorBuilder = classBuilder.DefineDefaultConstructor(MethodAttributes.Public); | ||
37 | |||
38 | |||
39 | Type[] paramsTypeArray = new Type[] {typeof (System.ParamArrayAttribute)}; | ||
40 | Type[] executeFunctionTypeArray = new Type[] {typeof (string), typeof (System.ParamArrayAttribute)}; | ||
41 | foreach (IScriptCommandProvider cp in script.RegionInfo.CommandProviders.Values) | ||
42 | { | ||
43 | Type t = cp.GetType(); | ||
44 | foreach (MethodInfo mi in t.GetMethods()) | ||
45 | { | ||
46 | MethodBuilder methodBuilder = classBuilder.DefineMethod(mi.Name, mi.Attributes, mi.GetType(), Type.EmptyTypes); | ||
47 | methodBuilder.SetParameters(paramsTypeArray); | ||
48 | //ParameterBuilder paramBuilder = methodBuilder.DefineParameter(1, ParameterAttributes.None, "args"); | ||
49 | |||
50 | ilgen = methodBuilder.GetILGenerator(); | ||
51 | //ilgen.Emit(OpCodes.Nop); | ||
52 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
53 | //ilgen.Emit(OpCodes.Ldc_I4_0); | ||
54 | //ilgen.Emit(OpCodes.Ldelem_Ref); | ||
55 | //ilgen.MarkSequencePoint(doc, 6, 1, 6, 100); | ||
56 | |||
57 | MethodInfo ExecuteFunction = typeof(ScriptAssemblies.IScript).GetMethod( | ||
58 | "ExecuteFunction", | ||
59 | executeFunctionTypeArray); | ||
60 | |||
61 | ilgen.DeclareLocal(typeof(string)); | ||
62 | ilgen.Emit(OpCodes.Nop); | ||
63 | ilgen.Emit(OpCodes.Ldstr, mi.Name); | ||
64 | ilgen.Emit(OpCodes.Stloc_0); | ||
65 | ilgen.Emit(OpCodes.Ldarg_0); | ||
66 | ilgen.Emit(OpCodes.Ldloc_0); | ||
67 | ilgen.Emit(OpCodes.Ldarg_1); | ||
68 | |||
69 | // FieldInfo testInfo = classBuilder. | ||
70 | //BindingFlags.NonPublic | BindingFlags.Instance); | ||
71 | |||
72 | //ilgen.Emit(OpCodes.Ldfld, testInfo); | ||
73 | |||
74 | //ilgen.EmitCall(OpCodes.Call, ExecuteFunction, executeFunctionTypeArray); | ||
75 | ilgen.EmitCall(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine"), executeFunctionTypeArray); | ||
76 | |||
77 | // // string.Format("Hello, {0} World!", toWhom) | ||
78 | // // | ||
79 | // ilgen.Emit(OpCodes.Ldstr, "Hello, {0} World!"); | ||
80 | // ilgen.Emit(OpCodes.Ldarg_1); | ||
81 | // ilgen.Emit(OpCodes.Call, typeof(string).GetMethod | ||
82 | //("Format", new Type[] { typeof(string), typeof(object) })); | ||
83 | |||
84 | // // Console.WriteLine("Hello, World!"); | ||
85 | // // | ||
86 | // ilgen.Emit(OpCodes.Call, typeof(Console).GetMethod | ||
87 | // ("WriteLine", new Type[] { typeof(string) })); | ||
88 | ilgen.Emit(OpCodes.Ret); | ||
89 | |||
90 | |||
91 | |||
92 | //Label eom = ilgen.DefineLabel(); | ||
93 | //ilgen.Emit(OpCodes.Br_S, eom); | ||
94 | //ilgen.MarkLabel(eom); | ||
95 | //ilgen.Emit(OpCodes.Ret); | ||
96 | //Type test = methodBuilder.SetParameters(); | ||
97 | |||
98 | |||
99 | //methodBuilder.SetParameters(typeof (object[])); | ||
100 | |||
101 | |||
102 | } | ||
103 | } | ||
104 | |||
105 | |||
106 | //// Two fields: m_firstname, m_lastname | ||
107 | //FieldBuilder fBuilderFirstName = classBuilder.DefineField("m_firstname", typeof(string), FieldAttributes.Private); | ||
108 | //FieldBuilder fBuilderLastName = classBuilder.DefineField("m_lastname", typeof(string), FieldAttributes.Private); | ||
109 | |||
110 | //// Two properties for this object: FirstName, LastName | ||
111 | //PropertyBuilder pBuilderFirstName = classBuilder.DefineProperty("FirstName", System.Reflection.PropertyAttributes.HasDefault, typeof(string), null); | ||
112 | //PropertyBuilder pBuilderLastName = classBuilder.DefineProperty("LastName", System.Reflection.PropertyAttributes.HasDefault, typeof(string), null); | ||
113 | |||
114 | //// Custom attributes for get, set accessors | ||
115 | //MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName; | ||
116 | |||
117 | //// get,set accessors for FirstName | ||
118 | //MethodBuilder mGetFirstNameBuilder = classBuilder.DefineMethod("get_FirstName", getSetAttr, typeof(string), Type.EmptyTypes); | ||
119 | |||
120 | //// Code generation | ||
121 | //ilgen = mGetFirstNameBuilder.GetILGenerator(); | ||
122 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
123 | //ilgen.Emit(OpCodes.Ldfld, fBuilderFirstName); // returning the firstname field | ||
124 | //ilgen.Emit(OpCodes.Ret); | ||
125 | |||
126 | //MethodBuilder mSetFirstNameBuilder = classBuilder.DefineMethod("set_FirstName", getSetAttr, null, new Type[] { typeof(string) }); | ||
127 | |||
128 | //// Code generation | ||
129 | //ilgen = mSetFirstNameBuilder.GetILGenerator(); | ||
130 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
131 | //ilgen.Emit(OpCodes.Ldarg_1); | ||
132 | //ilgen.Emit(OpCodes.Stfld, fBuilderFirstName); // setting the firstname field from the first argument (1) | ||
133 | //ilgen.Emit(OpCodes.Ret); | ||
134 | |||
135 | //// get,set accessors for LastName | ||
136 | //MethodBuilder mGetLastNameBuilder = classBuilder.DefineMethod("get_LastName", getSetAttr, typeof(string), Type.EmptyTypes); | ||
137 | |||
138 | //// Code generation | ||
139 | //ilgen = mGetLastNameBuilder.GetILGenerator(); | ||
140 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
141 | //ilgen.Emit(OpCodes.Ldfld, fBuilderLastName); // returning the firstname field | ||
142 | //ilgen.Emit(OpCodes.Ret); | ||
143 | |||
144 | //MethodBuilder mSetLastNameBuilder = classBuilder.DefineMethod("set_LastName", getSetAttr, null, new Type[] { typeof(string) }); | ||
145 | |||
146 | //// Code generation | ||
147 | //ilgen = mSetLastNameBuilder.GetILGenerator(); | ||
148 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
149 | //ilgen.Emit(OpCodes.Ldarg_1); | ||
150 | //ilgen.Emit(OpCodes.Stfld, fBuilderLastName); // setting the firstname field from the first argument (1) | ||
151 | //ilgen.Emit(OpCodes.Ret); | ||
152 | |||
153 | //// Assigning get/set accessors | ||
154 | //pBuilderFirstName.SetGetMethod(mGetFirstNameBuilder); | ||
155 | //pBuilderFirstName.SetSetMethod(mSetFirstNameBuilder); | ||
156 | |||
157 | //pBuilderLastName.SetGetMethod(mGetLastNameBuilder); | ||
158 | //pBuilderLastName.SetSetMethod(mSetLastNameBuilder); | ||
159 | |||
160 | //// Now, a custom method named GetFullName that concatenates FirstName and LastName properties | ||
161 | //MethodBuilder mGetFullNameBuilder = classBuilder.DefineMethod("GetFullName", MethodAttributes.Public, typeof(string), Type.EmptyTypes); | ||
162 | |||
163 | //// Code generation | ||
164 | //ilgen = mGetFullNameBuilder.GetILGenerator(); | ||
165 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
166 | //ilgen.Emit(OpCodes.Call, mGetFirstNameBuilder); // getting the firstname | ||
167 | //ilgen.Emit(OpCodes.Ldstr, " "); // an space | ||
168 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
169 | //ilgen.Emit(OpCodes.Call, mGetLastNameBuilder); // getting the lastname | ||
170 | |||
171 | //// We need the 'Concat' method from string type | ||
172 | //MethodInfo concatMethod = typeof(String).GetMethod("Concat", new Type[] { typeof(string), typeof(string), typeof(string) }); | ||
173 | |||
174 | //ilgen.Emit(OpCodes.Call, concatMethod); // calling concat and returning the result | ||
175 | //ilgen.Emit(OpCodes.Ret); | ||
176 | |||
177 | //// Another constructor that initializes firstname and lastname | ||
178 | //ConstructorBuilder ctorBuilder2 = classBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string), typeof(string) }); | ||
179 | //ctorBuilder2.DefineParameter(1, ParameterAttributes.In, "firstname"); | ||
180 | //ctorBuilder2.DefineParameter(2, ParameterAttributes.In, "lastname"); | ||
181 | |||
182 | //// Code generation | ||
183 | //ilgen = ctorBuilder2.GetILGenerator(); | ||
184 | |||
185 | //// First of all, we need to call the base constructor, | ||
186 | //// the Object's constructor in this sample | ||
187 | //Type objType = Type.GetType("System.Object"); | ||
188 | //ConstructorInfo objCtor = objType.GetConstructor(Type.EmptyTypes); | ||
189 | |||
190 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
191 | //ilgen.Emit(OpCodes.Call, objCtor); // calling the Object's constructor | ||
192 | |||
193 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
194 | //ilgen.Emit(OpCodes.Ldarg_1); | ||
195 | //ilgen.Emit(OpCodes.Call, mSetFirstNameBuilder); // setting the firstname field from the first argument (1) | ||
196 | //ilgen.Emit(OpCodes.Ldarg_0); | ||
197 | //ilgen.Emit(OpCodes.Ldarg_2); | ||
198 | //ilgen.Emit(OpCodes.Call, mSetLastNameBuilder); // setting the lastname field from the second argument (2) | ||
199 | //ilgen.Emit(OpCodes.Ret); | ||
200 | |||
201 | // Finally, create the type and save the assembly | ||
202 | classBuilder.CreateType(); | ||
203 | |||
204 | asmBuilder.Save(asmFileName); | ||
205 | string toFile = Path.Combine(moveToDir, asmFileName); | ||
206 | if (File.Exists(toFile)) | ||
207 | File.Delete(toFile); | ||
208 | File.Move(asmFileName, toFile); | ||
209 | |||
210 | string a = ""; | ||
211 | } | ||
212 | } | ||
213 | } \ No newline at end of file | ||