ScriptBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Runtime.Remoting;
  29. using System.Runtime.Remoting.Lifetime;
  30. using System.Security.Permissions;
  31. using System.Threading;
  32. using System.Reflection;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using OpenSim.Region.ScriptEngine.Interfaces;
  36. using OpenSim.Region.ScriptEngine.Shared;
  37. using OpenSim.Region.ScriptEngine.Shared.Api.Runtime;
  38. namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
  39. {
  40. public partial class ScriptBaseClass : MarshalByRefObject, IScript
  41. {
  42. private Dictionary<string, MethodInfo> inits = new Dictionary<string, MethodInfo>();
  43. // private ScriptSponsor m_sponser;
  44. public override Object InitializeLifetimeService()
  45. {
  46. ILease lease = (ILease)base.InitializeLifetimeService();
  47. if (lease.CurrentState == LeaseState.Initial)
  48. {
  49. // Infinite
  50. lease.InitialLeaseTime = TimeSpan.FromMinutes(0);
  51. // lease.RenewOnCallTime = TimeSpan.FromSeconds(10.0);
  52. // lease.SponsorshipTimeout = TimeSpan.FromMinutes(1.0);
  53. }
  54. return lease;
  55. }
  56. #if DEBUG
  57. // For tracing GC while debugging
  58. public static bool GCDummy = false;
  59. ~ScriptBaseClass()
  60. {
  61. GCDummy = true;
  62. }
  63. #endif
  64. public ScriptBaseClass()
  65. {
  66. m_Executor = new Executor(this);
  67. MethodInfo[] myArrayMethodInfo = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
  68. foreach (MethodInfo mi in myArrayMethodInfo)
  69. {
  70. if (mi.Name.Length > 7 && mi.Name.Substring(0, 7) == "ApiType")
  71. {
  72. string type = mi.Name.Substring(7);
  73. inits[type] = mi;
  74. }
  75. }
  76. // m_sponser = new ScriptSponsor();
  77. }
  78. private Executor m_Executor = null;
  79. public int GetStateEventFlags(string state)
  80. {
  81. return (int)m_Executor.GetStateEventFlags(state);
  82. }
  83. public void ExecuteEvent(string state, string FunctionName, object[] args)
  84. {
  85. m_Executor.ExecuteEvent(state, FunctionName, args);
  86. }
  87. public string[] GetApis()
  88. {
  89. string[] apis = new string[inits.Count];
  90. inits.Keys.CopyTo(apis, 0);
  91. return apis;
  92. }
  93. private Dictionary<string, object> m_InitialValues =
  94. new Dictionary<string, object>();
  95. private Dictionary<string, FieldInfo> m_Fields =
  96. new Dictionary<string, FieldInfo>();
  97. public void InitApi(string api, IScriptApi data)
  98. {
  99. if (!inits.ContainsKey(api))
  100. return;
  101. //ILease lease = (ILease)RemotingServices.GetLifetimeService(data as MarshalByRefObject);
  102. RemotingServices.GetLifetimeService(data as MarshalByRefObject);
  103. // lease.Register(m_sponser);
  104. MethodInfo mi = inits[api];
  105. Object[] args = new Object[1];
  106. args[0] = data;
  107. mi.Invoke(this, args);
  108. m_InitialValues = GetVars();
  109. }
  110. public void Close()
  111. {
  112. // m_sponser.Close();
  113. }
  114. public Dictionary<string, object> GetVars()
  115. {
  116. Dictionary<string, object> vars = new Dictionary<string, object>();
  117. if (m_Fields == null)
  118. return vars;
  119. m_Fields.Clear();
  120. Type t = GetType();
  121. FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
  122. BindingFlags.Public |
  123. BindingFlags.Instance |
  124. BindingFlags.DeclaredOnly);
  125. foreach (FieldInfo field in fields)
  126. {
  127. m_Fields[field.Name] = field;
  128. if (field.FieldType == typeof(LSL_Types.list)) // ref type, copy
  129. {
  130. LSL_Types.list v = (LSL_Types.list)field.GetValue(this);
  131. Object[] data = new Object[v.Data.Length];
  132. Array.Copy(v.Data, 0, data, 0, v.Data.Length);
  133. LSL_Types.list c = new LSL_Types.list();
  134. c.Data = data;
  135. vars[field.Name] = c;
  136. }
  137. else if (field.FieldType == typeof(LSL_Types.LSLInteger) ||
  138. field.FieldType == typeof(LSL_Types.LSLString) ||
  139. field.FieldType == typeof(LSL_Types.LSLFloat) ||
  140. field.FieldType == typeof(Int32) ||
  141. field.FieldType == typeof(Double) ||
  142. field.FieldType == typeof(Single) ||
  143. field.FieldType == typeof(String) ||
  144. field.FieldType == typeof(Byte) ||
  145. field.FieldType == typeof(short) ||
  146. field.FieldType == typeof(LSL_Types.Vector3) ||
  147. field.FieldType == typeof(LSL_Types.Quaternion))
  148. {
  149. vars[field.Name] = field.GetValue(this);
  150. }
  151. }
  152. return vars;
  153. }
  154. public void SetVars(Dictionary<string, object> vars)
  155. {
  156. foreach (KeyValuePair<string, object> var in vars)
  157. {
  158. if (m_Fields.ContainsKey(var.Key))
  159. {
  160. if (m_Fields[var.Key].FieldType == typeof(LSL_Types.list))
  161. {
  162. LSL_Types.list v = (LSL_Types.list)m_Fields[var.Key].GetValue(this);
  163. Object[] data = ((LSL_Types.list)var.Value).Data;
  164. v.Data = new Object[data.Length];
  165. Array.Copy(data, 0, v.Data, 0, data.Length);
  166. m_Fields[var.Key].SetValue(this, v);
  167. }
  168. else if (m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLInteger) ||
  169. m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLString) ||
  170. m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLFloat) ||
  171. m_Fields[var.Key].FieldType == typeof(Int32) ||
  172. m_Fields[var.Key].FieldType == typeof(Double) ||
  173. m_Fields[var.Key].FieldType == typeof(Single) ||
  174. m_Fields[var.Key].FieldType == typeof(String) ||
  175. m_Fields[var.Key].FieldType == typeof(Byte) ||
  176. m_Fields[var.Key].FieldType == typeof(short) ||
  177. m_Fields[var.Key].FieldType == typeof(LSL_Types.Vector3) ||
  178. m_Fields[var.Key].FieldType == typeof(LSL_Types.Quaternion)
  179. )
  180. {
  181. m_Fields[var.Key].SetValue(this, var.Value);
  182. }
  183. }
  184. }
  185. }
  186. public void ResetVars()
  187. {
  188. SetVars(m_InitialValues);
  189. }
  190. public void NoOp()
  191. {
  192. // Does what is says on the packet. Nowt, nada, nothing.
  193. // Required for insertion after a jump label to do what it says on the packet!
  194. // With a bit of luck the compiler may even optimize it out.
  195. }
  196. }
  197. }