1
0

ScriptBase.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 virtual void StateChange(string newState)
  111. {
  112. }
  113. public void Close()
  114. {
  115. // m_sponser.Close();
  116. }
  117. public Dictionary<string, object> GetVars()
  118. {
  119. Dictionary<string, object> vars = new Dictionary<string, object>();
  120. if (m_Fields == null)
  121. return vars;
  122. m_Fields.Clear();
  123. Type t = GetType();
  124. FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
  125. BindingFlags.Public |
  126. BindingFlags.Instance |
  127. BindingFlags.DeclaredOnly);
  128. foreach (FieldInfo field in fields)
  129. {
  130. m_Fields[field.Name] = field;
  131. if (field.FieldType == typeof(LSL_Types.list)) // ref type, copy
  132. {
  133. LSL_Types.list v = (LSL_Types.list)field.GetValue(this);
  134. Object[] data = new Object[v.Data.Length];
  135. Array.Copy(v.Data, 0, data, 0, v.Data.Length);
  136. LSL_Types.list c = new LSL_Types.list();
  137. c.Data = data;
  138. vars[field.Name] = c;
  139. }
  140. else if (field.FieldType == typeof(LSL_Types.LSLInteger) ||
  141. field.FieldType == typeof(LSL_Types.LSLString) ||
  142. field.FieldType == typeof(LSL_Types.LSLFloat) ||
  143. field.FieldType == typeof(Int32) ||
  144. field.FieldType == typeof(Double) ||
  145. field.FieldType == typeof(Single) ||
  146. field.FieldType == typeof(String) ||
  147. field.FieldType == typeof(Byte) ||
  148. field.FieldType == typeof(short) ||
  149. field.FieldType == typeof(LSL_Types.Vector3) ||
  150. field.FieldType == typeof(LSL_Types.Quaternion))
  151. {
  152. vars[field.Name] = field.GetValue(this);
  153. }
  154. }
  155. return vars;
  156. }
  157. public void SetVars(Dictionary<string, object> vars)
  158. {
  159. foreach (KeyValuePair<string, object> var in vars)
  160. {
  161. if (m_Fields.ContainsKey(var.Key))
  162. {
  163. if (m_Fields[var.Key].FieldType == typeof(LSL_Types.list))
  164. {
  165. LSL_Types.list v = (LSL_Types.list)m_Fields[var.Key].GetValue(this);
  166. Object[] data = ((LSL_Types.list)var.Value).Data;
  167. v.Data = new Object[data.Length];
  168. Array.Copy(data, 0, v.Data, 0, data.Length);
  169. m_Fields[var.Key].SetValue(this, v);
  170. }
  171. else if (m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLInteger) ||
  172. m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLString) ||
  173. m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLFloat) ||
  174. m_Fields[var.Key].FieldType == typeof(Int32) ||
  175. m_Fields[var.Key].FieldType == typeof(Double) ||
  176. m_Fields[var.Key].FieldType == typeof(Single) ||
  177. m_Fields[var.Key].FieldType == typeof(String) ||
  178. m_Fields[var.Key].FieldType == typeof(Byte) ||
  179. m_Fields[var.Key].FieldType == typeof(short) ||
  180. m_Fields[var.Key].FieldType == typeof(LSL_Types.Vector3) ||
  181. m_Fields[var.Key].FieldType == typeof(LSL_Types.Quaternion)
  182. )
  183. {
  184. m_Fields[var.Key].SetValue(this, var.Value);
  185. }
  186. }
  187. }
  188. }
  189. public void ResetVars()
  190. {
  191. SetVars(m_InitialValues);
  192. }
  193. public void NoOp()
  194. {
  195. // Does what is says on the packet. Nowt, nada, nothing.
  196. // Required for insertion after a jump label to do what it says on the packet!
  197. // With a bit of luck the compiler may even optimize it out.
  198. }
  199. }
  200. }