1
0

XMRInstMain.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.Threading;
  29. using System.Reflection;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Runtime.Remoting.Lifetime;
  33. using System.Security.Policy;
  34. using System.IO;
  35. using System.Xml;
  36. using System.Text;
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.ScriptEngine.Interfaces;
  40. using OpenSim.Region.ScriptEngine.Shared;
  41. using OpenSim.Region.ScriptEngine.Shared.Api;
  42. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  43. using OpenSim.Region.ScriptEngine.Yengine;
  44. using OpenSim.Region.Framework.Scenes;
  45. using log4net;
  46. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  47. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  48. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  49. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  50. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  51. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  52. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  53. // This class exists in the main app domain
  54. //
  55. namespace OpenSim.Region.ScriptEngine.Yengine
  56. {
  57. /**
  58. * @brief Which queue it is in as far as running is concerned,
  59. * ie, m_StartQueue, m_YieldQueue, m_SleepQueue, etc.
  60. * Allowed transitions:
  61. * Starts in CONSTRUCT when constructed
  62. * CONSTRUCT->ONSTARTQ : only by thread that constructed and compiled it
  63. * IDLE->ONSTARTQ,RESETTING : by any thread but must have m_QueueLock when transitioning
  64. * ONSTARTQ->RUNNING,RESETTING : only by thread that removed it from m_StartQueue
  65. * ONYIELDQ->RUNNING,RESETTING : only by thread that removed it from m_YieldQueue
  66. * ONSLEEPQ->REMDFROMSLPQ : by any thread but must have m_SleepQueue when transitioning
  67. * REMDFROMSLPQ->ONYIELDQ,RESETTING : only by thread that removed it from m_SleepQueue
  68. * RUNNING->whatever1 : only by thread that transitioned it to RUNNING
  69. * whatever1 = IDLE,ONSLEEPQ,ONYIELDQ,ONSTARTQ,SUSPENDED,FINISHED
  70. * FINSHED->whatever2 : only by thread that transitioned it to FINISHED
  71. * whatever2 = IDLE,ONSTARTQ,DISPOSED
  72. * SUSPENDED->ONSTARTQ : by any thread (NOT YET IMPLEMENTED, should be under some kind of lock?)
  73. * RESETTING->ONSTARTQ : only by the thread that transitioned it to RESETTING
  74. */
  75. public enum XMRInstState
  76. {
  77. CONSTRUCT, // it is being constructed
  78. IDLE, // nothing happening (finished last event and m_EventQueue is empty)
  79. ONSTARTQ, // inserted on m_Engine.m_StartQueue
  80. RUNNING, // currently being executed by RunOne()
  81. ONSLEEPQ, // inserted on m_Engine.m_SleepQueue
  82. REMDFROMSLPQ, // removed from m_SleepQueue but not yet on m_YieldQueue
  83. ONYIELDQ, // inserted on m_Engine.m_YieldQueue
  84. FINISHED, // just finished handling an event
  85. SUSPENDED, // m_SuspendCount > 0
  86. RESETTING, // being reset via external call
  87. DISPOSED // has been disposed
  88. }
  89. public partial class XMRInstance: XMRInstAbstract, IDisposable
  90. {
  91. /******************************************************************\
  92. * This module contains the instance variables for XMRInstance. *
  93. \******************************************************************/
  94. public const int MAXEVENTQUEUE = 64;
  95. public static readonly DetectParams[] zeroDetectParams = new DetectParams[0];
  96. public static readonly object[] zeroObjectArray = new object[0];
  97. public static readonly ILog m_log =
  98. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  99. public XMRInstance m_NextInst; // used by XMRInstQueue
  100. public XMRInstance m_PrevInst;
  101. // For a given m_Item.AssetID, do we have the compiled object code and where
  102. // is it?
  103. public static object m_CompileLock = new object();
  104. private static Dictionary<string, ScriptObjCode> m_CompiledScriptObjCode = new Dictionary<string, ScriptObjCode>();
  105. public XMRInstState m_IState;
  106. public bool m_ForceRecomp = false;
  107. public SceneObjectPart m_Part = null;
  108. public uint m_LocalID = 0;
  109. public TaskInventoryItem m_Item = null;
  110. public UUID m_ItemID;
  111. public UUID m_PartUUID;
  112. private string m_CameFrom;
  113. private string m_ScriptObjCodeKey;
  114. private Yengine m_Engine = null;
  115. private string m_ScriptBasePath;
  116. private string m_StateFileName;
  117. public string m_SourceCode;
  118. public bool m_PostOnRez;
  119. private DetectParams[] m_DetectParams = null;
  120. public int m_StartParam = 0;
  121. public StateSource m_StateSource;
  122. public string m_DescName;
  123. private bool[] m_HaveEventHandlers;
  124. public int m_StackSize;
  125. public int m_HeapSize;
  126. private ArrayList m_CompilerErrors;
  127. private DateTime m_LastRanAt = DateTime.MinValue;
  128. private string m_RunOnePhase = "hasn't run";
  129. private string m_CheckRunPhase = "hasn't checked";
  130. public int m_InstEHEvent = 0; // number of events dequeued (StartEventHandler called)
  131. public int m_InstEHSlice = 0; // number of times handler timesliced (ResumeEx called)
  132. public double m_CPUTime = 0; // accumulated CPU time (milliseconds)
  133. public double m_SliceStart = 0; // when did current exec start
  134. // If code needs to have both m_QueueLock and m_RunLock,
  135. // be sure to lock m_RunLock first then m_QueueLock, as
  136. // that is the order used in RunOne().
  137. // These locks are currently separated to allow the script
  138. // to call API routines that queue events back to the script.
  139. // If we just had one lock, then the queuing would deadlock.
  140. // guards m_DetachQuantum, m_EventQueue, m_EventCounts, m_Running, m_Suspended
  141. public Object m_QueueLock = new Object();
  142. // true iff allowed to accept new events
  143. public bool m_Running = true;
  144. // queue of events that haven't been acted upon yet
  145. public LinkedList<EventParams> m_EventQueue = new LinkedList<EventParams>();
  146. // number of events of each code currently in m_EventQueue.
  147. private int[] m_EventCounts = new int[(int)ScriptEventCode.Size];
  148. // locked whilst running on the microthread stack (or about to run on it or just ran on it)
  149. private Object m_RunLock = new Object();
  150. // script won't step while > 0. bus-atomic updates only.
  151. private int m_SuspendCount = 0;
  152. // don't run any of script until this time
  153. // or until one of these events are queued
  154. public DateTime m_SleepUntil = DateTime.MinValue;
  155. public int m_SleepEventMask1 = 0;
  156. public int m_SleepEventMask2 = 0;
  157. private XMRLSL_Api m_XMRLSLApi;
  158. /*
  159. * Makes sure migration data version is same on both ends.
  160. */
  161. public static byte migrationVersion = 11;
  162. // Incremented each time script gets reset.
  163. public int m_ResetCount = 0;
  164. // Scripts start suspended now. This means that event queues will
  165. // accept events, but will not actually run them until the core
  166. // tells it it's OK. This is needed to prevent loss of link messages
  167. // in complex objects, where no event can be allowed to run until
  168. // all possible link message receivers' queues are established.
  169. // Guarded by m_QueueLock.
  170. public bool m_Suspended = true;
  171. // We really don't want to save state for a script that hasn't had
  172. // a chance to run, because it's state will be blank. That would
  173. // cause attachment state loss.
  174. public bool m_HasRun = false;
  175. // When llDie is executed within the attach(NULL_KEY) event of
  176. // a script being detached to inventory, the DeleteSceneObject call
  177. // it causes will delete the script instances before their state can
  178. // be saved. Therefore, the instance needs to know that it's being
  179. // detached to inventory, rather than to ground.
  180. // Also, the attach(NULL_KEY) event needs to run with priority, and
  181. // it also needs to have a limited quantum.
  182. // If this is nonzero, we're detaching to inventory.
  183. // Guarded by m_QueueLock.
  184. private int m_DetachQuantum = 0;
  185. // Finally, we need to wait until the quantum is done, or the script
  186. // suspends itself. This should be efficient, so we use an event
  187. // for it instead of spinning busy.
  188. // It's born ready, but will be reset when the detach is posted.
  189. // It will then be set again on suspend/completion
  190. private ManualResetEvent m_DetachReady = new ManualResetEvent(true);
  191. // llmineventdelay support
  192. double m_minEventDelay = 0.0;
  193. double m_nextEventTime = 0.0;
  194. private static readonly Dictionary<string, ScriptEventCode> m_eventCodeMap = new Dictionary<string, ScriptEventCode>()
  195. {
  196. {"attach", ScriptEventCode.attach},
  197. {"at_rot_target", ScriptEventCode.at_rot_target},
  198. {"at_target", ScriptEventCode.at_target},
  199. {"collision", ScriptEventCode.collision},
  200. {"collision_end", ScriptEventCode.collision_end},
  201. {"collision_start", ScriptEventCode.collision_start},
  202. {"control", ScriptEventCode.control},
  203. {"dataserver", ScriptEventCode.dataserver},
  204. {"email", ScriptEventCode.email},
  205. {"http_response", ScriptEventCode.http_response},
  206. {"land_collision", ScriptEventCode.land_collision},
  207. {"land_collision_end", ScriptEventCode.land_collision_end},
  208. {"land_collision_start", ScriptEventCode.land_collision_start},
  209. {"listen", ScriptEventCode.listen},
  210. {"money", ScriptEventCode.money},
  211. {"moving_end", ScriptEventCode.moving_end},
  212. {"moving_start", ScriptEventCode.moving_start},
  213. {"not_at_rot_target", ScriptEventCode.not_at_rot_target},
  214. {"not_at_target", ScriptEventCode.not_at_target},
  215. {"remote_data", ScriptEventCode.remote_data},
  216. {"run_time_permissions", ScriptEventCode.run_time_permissions},
  217. {"state_entry", ScriptEventCode.state_entry},
  218. {"state_exit", ScriptEventCode.state_exit},
  219. {"timer", ScriptEventCode.timer},
  220. {"touch", ScriptEventCode.touch},
  221. {"touch_end", ScriptEventCode.touch_end},
  222. {"touch_start", ScriptEventCode.touch_start},
  223. {"transaction_result", ScriptEventCode.transaction_result},
  224. {"object_rez", ScriptEventCode.object_rez},
  225. {"changed", ScriptEventCode.changed},
  226. {"link_message", ScriptEventCode.link_message},
  227. {"no_sensor", ScriptEventCode.no_sensor},
  228. {"on_rez", ScriptEventCode.on_rez},
  229. {"sensor", ScriptEventCode.sensor},
  230. {"http_request", ScriptEventCode.http_request},
  231. {"path_update", ScriptEventCode.path_update}
  232. };
  233. }
  234. }