ScriptBase.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 OpenSim 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.Lifetime;
  29. using System.Security.Permissions;
  30. using System.Threading;
  31. using System.Reflection;
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using OpenSim.Region.ScriptEngine.Interfaces;
  35. using OpenSim.Region.ScriptEngine.Shared;
  36. namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
  37. {
  38. public partial class ScriptBaseClass : MarshalByRefObject, IScript
  39. {
  40. private Dictionary<string, MethodInfo> inits = new Dictionary<string, MethodInfo>();
  41. // Object expires if we don't keep it alive
  42. // sponsor will be added on object load
  43. [SecurityPermissionAttribute(SecurityAction.Demand,
  44. Flags = SecurityPermissionFlag.Infrastructure)]
  45. public override Object InitializeLifetimeService()
  46. {
  47. ILease lease = (ILease)base.InitializeLifetimeService();
  48. if (lease.CurrentState == LeaseState.Initial)
  49. {
  50. lease.InitialLeaseTime = TimeSpan.Zero;
  51. // lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
  52. // lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
  53. // lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
  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. }
  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. MethodInfo mi = inits[api];
  102. Object[] args = new Object[1];
  103. args[0] = data;
  104. mi.Invoke(this, args);
  105. m_InitialValues = GetVars();
  106. }
  107. public Dictionary<string, object> GetVars()
  108. {
  109. Dictionary<string, object> vars = new Dictionary<string, object>();
  110. if (m_Fields == null)
  111. return vars;
  112. m_Fields.Clear();
  113. Type t = GetType();
  114. FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
  115. BindingFlags.Public |
  116. BindingFlags.Instance |
  117. BindingFlags.DeclaredOnly);
  118. foreach (FieldInfo field in fields)
  119. {
  120. m_Fields[field.Name] = field;
  121. if (field.FieldType == typeof(LSL_Types.list)) // ref type, copy
  122. {
  123. LSL_Types.list v = (LSL_Types.list)field.GetValue(this);
  124. Object[] data = new Object[v.Data.Length];
  125. Array.Copy(v.Data, 0, data, 0, v.Data.Length);
  126. LSL_Types.list c = new LSL_Types.list();
  127. c.Data = data;
  128. vars[field.Name] = c;
  129. }
  130. else if (field.FieldType == typeof(LSL_Types.LSLInteger) ||
  131. field.FieldType == typeof(LSL_Types.LSLString) ||
  132. field.FieldType == typeof(LSL_Types.LSLFloat) ||
  133. field.FieldType == typeof(Int32) ||
  134. field.FieldType == typeof(Double) ||
  135. field.FieldType == typeof(Single) ||
  136. field.FieldType == typeof(String) ||
  137. field.FieldType == typeof(Byte) ||
  138. field.FieldType == typeof(short) ||
  139. field.FieldType == typeof(LSL_Types.Vector3) ||
  140. field.FieldType == typeof(LSL_Types.Quaternion))
  141. {
  142. vars[field.Name] = field.GetValue(this);
  143. }
  144. }
  145. return vars;
  146. }
  147. public void SetVars(Dictionary<string, object> vars)
  148. {
  149. foreach (KeyValuePair<string, object> var in vars)
  150. {
  151. if (m_Fields.ContainsKey(var.Key))
  152. {
  153. if (m_Fields[var.Key].FieldType == typeof(LSL_Types.list))
  154. {
  155. LSL_Types.list v = (LSL_Types.list)m_Fields[var.Key].GetValue(this);
  156. Object[] data = ((LSL_Types.list)var.Value).Data;
  157. v.Data = new Object[data.Length];
  158. Array.Copy(data, 0, v.Data, 0, data.Length);
  159. m_Fields[var.Key].SetValue(this, v);
  160. }
  161. else if (m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLInteger) ||
  162. m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLString) ||
  163. m_Fields[var.Key].FieldType == typeof(LSL_Types.LSLFloat) ||
  164. m_Fields[var.Key].FieldType == typeof(Int32) ||
  165. m_Fields[var.Key].FieldType == typeof(Double) ||
  166. m_Fields[var.Key].FieldType == typeof(Single) ||
  167. m_Fields[var.Key].FieldType == typeof(String) ||
  168. m_Fields[var.Key].FieldType == typeof(Byte) ||
  169. m_Fields[var.Key].FieldType == typeof(short) ||
  170. m_Fields[var.Key].FieldType == typeof(LSL_Types.Vector3) ||
  171. m_Fields[var.Key].FieldType == typeof(LSL_Types.Quaternion)
  172. )
  173. {
  174. m_Fields[var.Key].SetValue(this, var.Value);
  175. }
  176. }
  177. }
  178. }
  179. public void ResetVars()
  180. {
  181. SetVars(m_InitialValues);
  182. }
  183. public void NoOp()
  184. {
  185. // Does what is says on the packet. Nowt, nada, nothing.
  186. // Required for insertion after a jump label to do what it says on the packet!
  187. // With a bit of luck the compiler may even optimize it out.
  188. }
  189. }
  190. }