ScriptBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Threading;
  30. using System.Reflection;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using OpenSim.Region.ScriptEngine.Interfaces;
  34. using OpenSim.Region.ScriptEngine.Shared;
  35. namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
  36. {
  37. public partial class ScriptBaseClass : MarshalByRefObject, IScript
  38. {
  39. private Dictionary<string,MethodInfo> inits = new Dictionary<string,MethodInfo>();
  40. //
  41. // Never expire this object
  42. //
  43. public override Object InitializeLifetimeService()
  44. {
  45. ILease lease = (ILease)base.InitializeLifetimeService();
  46. if (lease.CurrentState == LeaseState.Initial)
  47. {
  48. lease.InitialLeaseTime = TimeSpan.Zero;
  49. }
  50. return lease;
  51. }
  52. public ScriptBaseClass()
  53. {
  54. MethodInfo[] myArrayMethodInfo = GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance);
  55. foreach (MethodInfo mi in myArrayMethodInfo)
  56. {
  57. if (mi.Name.Length > 7 && mi.Name.Substring(0, 7) == "ApiType")
  58. {
  59. string type=mi.Name.Substring(7);
  60. inits[type]=mi;
  61. }
  62. }
  63. }
  64. public string[] GetApis()
  65. {
  66. string[] apis = new string[inits.Count];
  67. inits.Keys.CopyTo(apis, 0);
  68. return apis;
  69. }
  70. public void InitApi(string api, IScriptApi data)
  71. {
  72. if (!inits.ContainsKey(api))
  73. return;
  74. MethodInfo mi = inits[api];
  75. Object[] args = new Object[1];
  76. args[0] = data;
  77. mi.Invoke(this, args);
  78. }
  79. private Dictionary<string, object> m_InitialValues =
  80. new Dictionary<string, object>();
  81. private Dictionary<string, FieldInfo> m_Fields =
  82. new Dictionary<string, FieldInfo>();
  83. public Dictionary<string, object> GetVars()
  84. {
  85. Dictionary<string, object> vars = new Dictionary<string, object>();
  86. if (m_Fields == null)
  87. return vars;
  88. m_Fields.Clear();
  89. Type t = GetType();
  90. FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
  91. BindingFlags.Public |
  92. BindingFlags.Instance |
  93. BindingFlags.DeclaredOnly);
  94. foreach (FieldInfo field in fields)
  95. {
  96. m_Fields[field.Name]=field;
  97. vars[field.Name]=field.GetValue(this);
  98. }
  99. return vars;
  100. }
  101. public void SetVars(Dictionary<string, object> vars)
  102. {
  103. foreach (KeyValuePair<string, object> var in vars)
  104. {
  105. if (m_Fields.ContainsKey(var.Key))
  106. {
  107. m_Fields[var.Key].SetValue(this, var.Value);
  108. }
  109. }
  110. }
  111. public void ResetVars()
  112. {
  113. SetVars(m_InitialValues);
  114. }
  115. }
  116. }