ScriptManager.cs 6.8 KB

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