ScriptManager.cs 6.8 KB

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