ScriptBase.cs 9.0 KB

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