ScriptBase.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public ScriptBaseClass()
  41. {
  42. MethodInfo[] myArrayMethodInfo = GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance);
  43. foreach (MethodInfo mi in myArrayMethodInfo)
  44. {
  45. if (mi.Name.Length > 7 && mi.Name.Substring(0, 7) == "ApiType")
  46. {
  47. string type=mi.Name.Substring(7);
  48. inits[type]=mi;
  49. }
  50. }
  51. }
  52. public string[] GetApis()
  53. {
  54. string[] apis = new string[inits.Count];
  55. inits.Keys.CopyTo(apis, 0);
  56. return apis;
  57. }
  58. public void InitApi(string api, IScriptApi data)
  59. {
  60. if (!inits.ContainsKey(api))
  61. return;
  62. MethodInfo mi = inits[api];
  63. Object[] args = new Object[1];
  64. args[0] = data;
  65. mi.Invoke(this, args);
  66. }
  67. private Dictionary<string, object> m_InitialValues =
  68. new Dictionary<string, object>();
  69. private Dictionary<string, FieldInfo> m_Fields =
  70. new Dictionary<string, FieldInfo>();
  71. public Dictionary<string, object> GetVars()
  72. {
  73. Dictionary<string, object> vars = new Dictionary<string, object>();
  74. if (m_Fields == null)
  75. return vars;
  76. m_Fields.Clear();
  77. Type t = GetType();
  78. FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
  79. BindingFlags.Public |
  80. BindingFlags.Instance |
  81. BindingFlags.DeclaredOnly);
  82. foreach (FieldInfo field in fields)
  83. {
  84. m_Fields[field.Name]=field;
  85. vars[field.Name]=field.GetValue(this);
  86. }
  87. return vars;
  88. }
  89. public void SetVars(Dictionary<string, object> vars)
  90. {
  91. foreach (KeyValuePair<string, object> var in vars)
  92. {
  93. if (m_Fields.ContainsKey(var.Key))
  94. {
  95. m_Fields[var.Key].SetValue(this, var.Value);
  96. }
  97. }
  98. }
  99. public void ResetVars()
  100. {
  101. SetVars(m_InitialValues);
  102. }
  103. }
  104. }