ScriptManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Reflection;
  29. using log4net;
  30. using libsecondlife;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Environment.Scenes;
  33. using OpenSim.Region.ScriptEngine.Common;
  34. using OpenSim.Region.ScriptEngine.Common.ScriptEngineBase;
  35. namespace OpenSim.Region.ScriptEngine.DotNetEngine
  36. {
  37. public class ScriptManager : Common.ScriptEngineBase.ScriptManager
  38. {
  39. public ScriptManager(Common.ScriptEngineBase.ScriptEngine scriptEngine)
  40. : base(scriptEngine)
  41. {
  42. base.m_scriptEngine = scriptEngine;
  43. }
  44. private Compiler.LSL.Compiler LSLCompiler;
  45. private static readonly ILog m_log
  46. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. public override void Initialize()
  48. {
  49. // Create our compiler
  50. LSLCompiler = new Compiler.LSL.Compiler(m_scriptEngine);
  51. }
  52. // KEEP TRACK OF SCRIPTS <int id, whatever script>
  53. //internal Dictionary<uint, Dictionary<LLUUID, LSL_BaseClass>> Scripts = new Dictionary<uint, Dictionary<LLUUID, LSL_BaseClass>>();
  54. // LOAD SCRIPT
  55. // UNLOAD SCRIPT
  56. // PROVIDE SCRIPT WITH ITS INTERFACE TO OpenSim
  57. public override void _StartScript(uint localID, LLUUID itemID, string Script)
  58. {
  59. m_scriptEngine.Log.Debug("[" + m_scriptEngine.ScriptEngineName + "]: ScriptManager StartScript: localID: " + localID + ", itemID: " + itemID);
  60. //IScriptHost root = host.GetRoot();
  61. // We will initialize and start the script.
  62. // It will be up to the script itself to hook up the correct events.
  63. string CompiledScriptFile = String.Empty;
  64. SceneObjectPart m_host = World.GetSceneObjectPart(localID);
  65. // Xantor 20080525: I need assetID here to see if we already compiled this one previously
  66. LLUUID assetID = LLUUID.Zero;
  67. TaskInventoryItem taskInventoryItem = new TaskInventoryItem();
  68. if (m_host.TaskInventory.TryGetValue(itemID,out taskInventoryItem))
  69. assetID = taskInventoryItem.AssetID;
  70. try
  71. {
  72. // Xantor 20080525 see if we already compiled this script this session, stop incessant recompiling on
  73. // scriptreset, spawning of objects with embedded scripts etc.
  74. if (scriptList.TryGetValue(assetID, out CompiledScriptFile))
  75. {
  76. m_log.InfoFormat("[SCRIPT]: Found existing compile of assetID {0}: {1}", assetID, CompiledScriptFile);
  77. }
  78. else
  79. {
  80. // Compile (We assume LSL)
  81. CompiledScriptFile = LSLCompiler.PerformScriptCompile(Script);
  82. // Xantor 20080525 Save compiled scriptfile for later use
  83. m_log.InfoFormat("[SCRIPT]: Compiled assetID {0}: {1}", assetID, CompiledScriptFile);
  84. scriptList.Add(assetID, CompiledScriptFile);
  85. }
  86. //#if DEBUG
  87. //long before;
  88. //before = GC.GetTotalMemory(true); // This force a garbage collect that freezes some windows plateforms
  89. //#endif
  90. IScript CompiledScript;
  91. CompiledScript = m_scriptEngine.m_AppDomainManager.LoadScript(CompiledScriptFile);
  92. //#if DEBUG
  93. //m_scriptEngine.Log.DebugFormat("[" + m_scriptEngine.ScriptEngineName + "]: Script " + itemID + " occupies {0} bytes", GC.GetTotalMemory(true) - before);
  94. //#endif
  95. CompiledScript.Source = Script;
  96. // Add it to our script memstruct
  97. m_scriptEngine.m_ScriptManager.SetScript(localID, itemID, CompiledScript);
  98. // We need to give (untrusted) assembly a private instance of BuiltIns
  99. // this private copy will contain Read-Only FullitemID so that it can bring that on to the server whenever needed.
  100. BuilIn_Commands LSLB = new BuilIn_Commands(m_scriptEngine, m_host, localID, itemID);
  101. // Start the script - giving it BuiltIns
  102. CompiledScript.Start(LSLB);
  103. // Fire the first start-event
  104. int eventFlags = m_scriptEngine.m_ScriptManager.GetStateEventFlags(localID, itemID);
  105. m_host.SetScriptEvents(itemID, eventFlags);
  106. m_scriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "state_entry", EventQueueManager.llDetectNull, new object[] { });
  107. }
  108. catch (Exception e) // LEGIT: User Scripting
  109. {
  110. //m_scriptEngine.Log.Error("[ScriptEngine]: Error compiling script: " + e.ToString());
  111. try
  112. {
  113. // DISPLAY ERROR INWORLD
  114. string text = "Error compiling script:\r\n" + e.Message.ToString();
  115. if (text.Length > 1500)
  116. text = text.Substring(0, 1500);
  117. World.SimChat(Helpers.StringToField(text), ChatTypeEnum.DebugChannel, 2147483647,
  118. m_host.AbsolutePosition, m_host.Name, m_host.UUID, false);
  119. }
  120. catch (Exception e2) // LEGIT: User Scripting
  121. {
  122. m_scriptEngine.Log.Error("[" + m_scriptEngine.ScriptEngineName + "]: Error displaying error in-world: " + e2.ToString());
  123. m_scriptEngine.Log.Error("[" + m_scriptEngine.ScriptEngineName + "]: " +
  124. "Errormessage: Error compiling script:\r\n" + e.Message.ToString());
  125. }
  126. }
  127. }
  128. public override void _StopScript(uint localID, LLUUID itemID)
  129. {
  130. // Stop script
  131. #if DEBUG
  132. m_scriptEngine.Log.Debug("[" + m_scriptEngine.ScriptEngineName + "]: Stop script localID: " + localID + " LLUID: " + itemID.ToString());
  133. #endif
  134. // Stop long command on script
  135. m_scriptEngine.m_ASYNCLSLCommandManager.RemoveScript(localID, itemID);
  136. IScript LSLBC = GetScript(localID, itemID);
  137. if (LSLBC == null)
  138. return;
  139. // TEMP: First serialize it
  140. //GetSerializedScript(localID, itemID);
  141. try
  142. {
  143. // Get AppDomain
  144. AppDomain ad = LSLBC.Exec.GetAppDomain();
  145. // Tell script not to accept new requests
  146. m_scriptEngine.m_ScriptManager.GetScript(localID, itemID).Exec.StopScript();
  147. // Remove from internal structure
  148. m_scriptEngine.m_ScriptManager.RemoveScript(localID, itemID);
  149. // Tell AppDomain that we have stopped script
  150. m_scriptEngine.m_AppDomainManager.StopScript(ad);
  151. }
  152. catch (Exception e) // LEGIT: User Scripting
  153. {
  154. m_scriptEngine.Log.Error("[" + m_scriptEngine.ScriptEngineName + "]: Exception stopping script localID: " + localID + " LLUID: " + itemID.ToString() +
  155. ": " + e.ToString());
  156. }
  157. }
  158. }
  159. }