ScriptStructure.cs 5.4 KB

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