ScriptManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 : OpenSim.Region.ScriptEngine.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 ScriptSource = String.Empty;
  60. SceneObjectPart m_host = World.GetSceneObjectPart(localID);
  61. try
  62. {
  63. // Compile (We assume LSL)
  64. ScriptSource = 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(ScriptSource);
  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. m_scriptEngine.m_EventQueueManager.AddToScriptQueue(localID, itemID, "state_entry", EventQueueManager.llDetectNull, new object[] { });
  84. }
  85. catch (Exception e) // LEGIT: User Scripting
  86. {
  87. //m_scriptEngine.Log.Error("[ScriptEngine]: Error compiling script: " + e.ToString());
  88. try
  89. {
  90. // DISPLAY ERROR INWORLD
  91. string text = "Error compiling script:\r\n" + e.Message.ToString();
  92. if (text.Length > 1500)
  93. text = text.Substring(0, 1500);
  94. World.SimChat(Helpers.StringToField(text), ChatTypeEnum.Say, 0, m_host.AbsolutePosition,
  95. m_host.Name, m_host.UUID);
  96. }
  97. catch (Exception e2) // LEGIT: User Scripting
  98. {
  99. m_scriptEngine.Log.Error("[" + m_scriptEngine.ScriptEngineName + "]: Error displaying error in-world: " + e2.ToString());
  100. m_scriptEngine.Log.Error("[" + m_scriptEngine.ScriptEngineName + "]: " +
  101. "Errormessage: Error compiling script:\r\n" + e.Message.ToString());
  102. }
  103. }
  104. }
  105. public override void _StopScript(uint localID, LLUUID itemID)
  106. {
  107. // Stop script
  108. #if DEBUG
  109. m_scriptEngine.Log.Debug("[" + m_scriptEngine.ScriptEngineName + "]: Stop script localID: " + localID + " LLUID: " + itemID.ToString());
  110. #endif
  111. // Stop long command on script
  112. m_scriptEngine.m_ASYNCLSLCommandManager.RemoveScript(localID, itemID);
  113. IScript LSLBC = GetScript(localID, itemID);
  114. if (LSLBC == null)
  115. return;
  116. // TEMP: First serialize it
  117. //GetSerializedScript(localID, itemID);
  118. try
  119. {
  120. // Get AppDomain
  121. AppDomain ad = LSLBC.Exec.GetAppDomain();
  122. // Tell script not to accept new requests
  123. m_scriptEngine.m_ScriptManager.GetScript(localID, itemID).Exec.StopScript();
  124. // Remove from internal structure
  125. m_scriptEngine.m_ScriptManager.RemoveScript(localID, itemID);
  126. // Tell AppDomain that we have stopped script
  127. m_scriptEngine.m_AppDomainManager.StopScript(ad);
  128. }
  129. catch (Exception e) // LEGIT: User Scripting
  130. {
  131. m_scriptEngine.Log.Error("[" + m_scriptEngine.ScriptEngineName + "]: Exception stopping script localID: " + localID + " LLUID: " + itemID.ToString() +
  132. ": " + e.ToString());
  133. }
  134. }
  135. }
  136. }