ScriptManager.cs 7.3 KB

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