BaseClassFactory.cs 12 KB

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