ScriptStructure.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using OpenMetaverse;
  32. using OpenSim.Region.ScriptEngine.Interfaces;
  33. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  34. using OpenSim.ScriptEngine.Shared;
  35. namespace OpenSim.ScriptEngine.Shared
  36. {
  37. public struct ScriptStructure
  38. {
  39. public RegionInfoStructure RegionInfo;
  40. public ScriptMetaData ScriptMetaData;
  41. public ScriptAssemblies.IScript ScriptObject;
  42. public string State;
  43. public bool Running;
  44. public bool Disabled;
  45. public string Source;
  46. public int StartParam;
  47. public AppDomain AppDomain;
  48. public Dictionary<string, IScriptApi> Apis;
  49. public Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> LineMap;
  50. public uint LocalID;
  51. public UUID ItemID;
  52. public string AssemblyFileName;
  53. public string ScriptID { get { return LocalID.ToString() + "." + ItemID.ToString(); } }
  54. public string Name { get { return "Script:" + ScriptID; } }
  55. private bool Initialized;
  56. private Dictionary<string, Delegate> InternalFunctions;
  57. public string AssemblyName;
  58. public void ExecuteEvent(EventParams p)
  59. {
  60. ExecuteMethod(p, true);
  61. }
  62. public void ExecuteMethod(EventParams p)
  63. {
  64. ExecuteMethod(p, false);
  65. }
  66. private void ExecuteMethod(EventParams p, bool isEvent)
  67. {
  68. // First time initialization?
  69. if (!Initialized)
  70. {
  71. Initialized = true;
  72. CacheInternalFunctions();
  73. }
  74. lock (InternalFunctions)
  75. {
  76. // Make function name
  77. string FunctionName;
  78. if (isEvent)
  79. FunctionName = State + "_event_" + p.EventName;
  80. else
  81. FunctionName = p.EventName;
  82. // Check if this function exist
  83. if (!InternalFunctions.ContainsKey(FunctionName))
  84. {
  85. // TODO: Send message in-world
  86. //RegionInfo.Scene.
  87. RegionInfo.Logger.ErrorFormat("[{0}] Script function \"{1}\" was not found.", Name, FunctionName);
  88. return;
  89. }
  90. // Execute script function
  91. try
  92. {
  93. InternalFunctions[FunctionName].DynamicInvoke(p.Params);
  94. }
  95. catch (Exception e)
  96. {
  97. RegionInfo.Logger.ErrorFormat("[{0}] Execute \"{1}\" failed: {2}", Name, FunctionName, e.ToString());
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// Cache functions into a dictionary with delegates. Should be faster than reflection.
  103. /// </summary>
  104. private void CacheInternalFunctions()
  105. {
  106. Type scriptObjectType = ScriptObject.GetType();
  107. InternalFunctions = new Dictionary<string, Delegate>();
  108. MethodInfo[] methods = scriptObjectType.GetMethods();
  109. lock (InternalFunctions)
  110. {
  111. // Read all methods into a dictionary
  112. foreach (MethodInfo mi in methods)
  113. {
  114. // TODO: We don't support overloading
  115. if (!InternalFunctions.ContainsKey(mi.Name))
  116. InternalFunctions.Add(mi.Name, Delegate.CreateDelegate(scriptObjectType, ScriptObject, mi));
  117. else
  118. RegionInfo.Logger.ErrorFormat("[{0}] Error: Script function \"{1}\" is already added. We do not support overloading.",
  119. Name, mi.Name);
  120. }
  121. }
  122. }
  123. }
  124. }