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