1
0

ScriptBase.cs 8.7 KB

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