2008-06-25 13:19:42 +00:00
/ *
* Copyright ( c ) Contributors , http : //opensimulator.org/
* See CONTRIBUTORS . TXT for a full list of copyright holders .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
* * Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* * Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
2009-06-01 06:37:14 +00:00
* * Neither the name of the OpenSimulator Project nor the
2008-06-25 13:19:42 +00:00
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission .
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ` ` AS IS ' ' AND ANY
* EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
* ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
* LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
* /
2008-05-30 12:27:06 +00:00
using System ;
using System.Collections ;
using System.Collections.Generic ;
2012-02-01 00:07:06 +00:00
using System.Globalization ;
using System.IO ;
2012-12-05 23:27:50 +00:00
using System.Linq ;
2012-02-01 00:07:06 +00:00
using System.Reflection ;
2008-11-19 06:25:34 +00:00
using System.Security ;
2008-05-30 12:27:06 +00:00
using System.Security.Policy ;
2012-02-01 00:07:06 +00:00
using System.Text ;
using System.Threading ;
2008-05-30 12:27:06 +00:00
using System.Xml ;
2008-09-06 07:52:41 +00:00
using OpenMetaverse ;
2008-10-17 01:59:43 +00:00
using OpenMetaverse.StructuredData ;
2008-05-30 12:27:06 +00:00
using log4net ;
using Nini.Config ;
using Amib.Threading ;
2014-12-30 20:53:31 +00:00
using Mono.Addins ;
2008-05-30 12:27:06 +00:00
using OpenSim.Framework ;
2011-10-19 19:09:02 +00:00
using OpenSim.Framework.Console ;
2009-02-06 16:55:34 +00:00
using OpenSim.Region.Framework.Scenes ;
using OpenSim.Region.Framework.Interfaces ;
2013-01-23 02:28:27 +00:00
using OpenSim.Region.ScriptEngine.Interfaces ;
2008-06-24 21:09:49 +00:00
using OpenSim.Region.ScriptEngine.Shared ;
using OpenSim.Region.ScriptEngine.Shared.CodeTools ;
2008-08-27 22:38:36 +00:00
using OpenSim.Region.ScriptEngine.Shared.Instance ;
2012-03-06 23:21:17 +00:00
using OpenSim.Region.ScriptEngine.Shared.Api ;
using OpenSim.Region.ScriptEngine.Shared.Api.Plugins ;
2013-01-23 02:28:27 +00:00
using OpenSim.Region.ScriptEngine.Shared.ScriptBase ;
2013-01-17 23:39:09 +00:00
using OpenSim.Region.ScriptEngine.XEngine.ScriptBase ;
2012-03-06 23:21:17 +00:00
using Timer = OpenSim . Region . ScriptEngine . Shared . Api . Plugins . Timer ;
2008-05-30 12:27:06 +00:00
2009-10-29 13:42:40 +00:00
using ScriptCompileQueue = OpenSim . Framework . LocklessQueue < object [ ] > ;
2008-05-30 12:27:06 +00:00
namespace OpenSim.Region.ScriptEngine.XEngine
{
2014-12-30 20:53:31 +00:00
[Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XEngine")]
2009-04-15 18:51:17 +00:00
public class XEngine : INonSharedRegionModule , IScriptModule , IScriptEngine
2008-05-30 12:27:06 +00:00
{
private static readonly ILog m_log = LogManager . GetLogger ( MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
2012-06-19 23:10:19 +00:00
/// <summary>
/// Control the printing of certain debug messages.
/// </summary>
/// <remarks>
/// If DebugLevel >= 1, then we log every time that a script is started.
/// </remarks>
2014-12-03 20:48:17 +00:00
public int DebugLevel { get ; set ; }
2012-06-19 23:10:19 +00:00
2015-01-27 18:58:11 +00:00
/// <summary>
/// A parameter to allow us to notify the log if at least one script has a compilation that is not compatible
/// with ScriptStopStrategy.
/// </summary>
2017-09-04 17:28:31 +00:00
public bool HaveNotifiedLogOfScriptStopMismatch { get ; private set ; }
2015-01-27 18:58:11 +00:00
2008-05-30 12:27:06 +00:00
private SmartThreadPool m_ThreadPool ;
private int m_MaxScriptQueue ;
private Scene m_Scene ;
2009-04-15 18:51:17 +00:00
private IConfig m_ScriptConfig = null ;
2009-06-06 16:39:28 +00:00
private IConfigSource m_ConfigSource = null ;
2008-09-27 05:31:43 +00:00
private ICompiler m_Compiler ;
2008-09-21 02:52:12 +00:00
private int m_MinThreads ;
2012-07-11 20:55:18 +00:00
private int m_MaxThreads ;
/// <summary>
/// Amount of time to delay before starting.
/// </summary>
private int m_StartDelay ;
2014-07-16 21:58:38 +00:00
/// <summary>
/// Are we stopping scripts co-operatively by inserting checks in them at C# compile time (true) or aborting
/// their threads (false)?
/// </summary>
2014-07-10 23:03:02 +00:00
private bool m_coopTermination ;
2008-09-21 02:52:12 +00:00
private int m_IdleTimeout ;
private int m_StackSize ;
private int m_SleepTime ;
private int m_SaveTime ;
private ThreadPriority m_Prio ;
2008-09-23 03:36:16 +00:00
private bool m_Enabled = false ;
2008-12-28 17:22:31 +00:00
private bool m_InitialStartup = true ;
2009-01-28 09:22:12 +00:00
private int m_ScriptFailCount ; // Number of script fails since compile queue was last empty
private string m_ScriptErrorMessage ;
2009-10-26 14:50:25 +00:00
private bool m_AppDomainLoading ;
2020-07-27 15:13:09 +00:00
private bool m_AttachmentsDomainLoading ;
2009-12-22 09:24:01 +00:00
private Dictionary < UUID , ArrayList > m_ScriptErrors =
new Dictionary < UUID , ArrayList > ( ) ;
2008-06-30 11:57:47 +00:00
2009-10-26 14:50:25 +00:00
// disable warning: need to keep a reference to XEngine.EventManager
// alive to avoid it being garbage collected
2008-06-28 03:45:52 +00:00
#pragma warning disable 414
private EventManager m_EventManager ;
#pragma warning restore 414
2009-04-10 21:08:33 +00:00
private IXmlRpcRouter m_XmlRpcRouter ;
2008-05-30 12:27:06 +00:00
private int m_EventLimit ;
private bool m_KillTimedOutScripts ;
2012-12-13 21:02:55 +00:00
/// <summary>
/// Number of milliseconds we will wait for a script event to complete on script stop before we forcibly abort
/// its thread.
/// </summary>
/// <remarks>
/// It appears that if a script thread is aborted whilst it is holding ReaderWriterLockSlim (possibly the write
/// lock) then the lock is not properly released. This causes mono 2.6, 2.10 and possibly
/// later to crash, sometimes with symptoms such as a leap to 100% script usage and a vm thead dump showing
2017-01-05 19:07:37 +00:00
/// all threads waiting on release of ReaderWriterLockSlim write thread which none of the threads listed
2012-12-13 21:02:55 +00:00
/// actually hold.
2017-01-05 19:07:37 +00:00
///
2012-12-13 21:02:55 +00:00
/// Pausing for event completion reduces the risk of this happening. However, it may be that aborting threads
/// is not a mono issue per se but rather a risky activity in itself in an AppDomain that is not immediately
/// shutting down.
/// </remarks>
private int m_WaitForEventCompletionOnScriptStop = 1000 ;
2010-09-25 20:22:18 +00:00
private string m_ScriptEnginesPath = null ;
2008-05-30 12:27:06 +00:00
2012-07-17 13:00:42 +00:00
private ExpiringCache < UUID , bool > m_runFlags = new ExpiringCache < UUID , bool > ( ) ;
2011-11-17 21:03:08 +00:00
/// <summary>
/// Is the entire simulator in the process of shutting down?
/// </summary>
private bool m_SimulatorShuttingDown ;
2008-05-30 12:27:06 +00:00
private static List < XEngine > m_ScriptEngines =
new List < XEngine > ( ) ;
// Maps the local id to the script inventory items in it
2009-01-19 17:11:57 +00:00
private Dictionary < uint , List < UUID > > m_PrimObjects =
new Dictionary < uint , List < UUID > > ( ) ;
2008-05-30 12:27:06 +00:00
2008-09-06 07:52:41 +00:00
// Maps the UUID above to the script instance
2008-05-30 12:27:06 +00:00
2008-09-06 07:52:41 +00:00
private Dictionary < UUID , IScriptInstance > m_Scripts =
new Dictionary < UUID , IScriptInstance > ( ) ;
2008-05-30 12:27:06 +00:00
// Maps the asset ID to the assembly
2008-09-06 07:52:41 +00:00
private Dictionary < UUID , string > m_Assemblies =
new Dictionary < UUID , string > ( ) ;
2008-05-30 12:27:06 +00:00
2009-02-05 16:12:51 +00:00
private Dictionary < string , int > m_AddingAssemblies =
new Dictionary < string , int > ( ) ;
2008-05-30 12:27:06 +00:00
// This will list AppDomains by script asset
2008-09-06 07:52:41 +00:00
private Dictionary < UUID , AppDomain > m_AppDomains =
new Dictionary < UUID , AppDomain > ( ) ;
2008-05-30 12:27:06 +00:00
// List the scripts running in each appdomain
2009-01-19 17:11:57 +00:00
private Dictionary < UUID , List < UUID > > m_DomainScripts =
new Dictionary < UUID , List < UUID > > ( ) ;
2008-05-30 12:27:06 +00:00
2009-10-29 13:42:40 +00:00
private ScriptCompileQueue m_CompileQueue = new ScriptCompileQueue ( ) ;
2008-06-28 16:08:12 +00:00
IWorkItemResult m_CurrentCompile = null ;
2017-01-07 00:25:59 +00:00
private Dictionary < UUID , ScriptCompileInfo > m_CompileDict = new Dictionary < UUID , ScriptCompileInfo > ( ) ;
2008-06-28 16:08:12 +00:00
2013-01-10 00:32:22 +00:00
private ScriptEngineConsoleCommands m_consoleCommands ;
2008-05-30 12:27:06 +00:00
public string ScriptEngineName
{
get { return "XEngine" ; }
}
2013-01-23 02:28:27 +00:00
public string ScriptClassName { get ; private set ; }
2013-01-17 23:39:09 +00:00
public string ScriptBaseClassName { get ; private set ; }
public ParameterInfo [ ] ScriptBaseClassParameters { get ; private set ; }
public string [ ] ScriptReferencedAssemblies { get ; private set ; }
2008-05-30 12:27:06 +00:00
public Scene World
{
get { return m_Scene ; }
}
public static List < XEngine > ScriptEngines
{
get { return m_ScriptEngines ; }
}
2009-04-10 21:08:33 +00:00
public IScriptModule ScriptModule
{
get { return this ; }
}
2008-06-27 23:03:39 +00:00
// private struct RezScriptParms
// {
// uint LocalID;
2008-09-06 07:52:41 +00:00
// UUID ItemID;
2008-06-27 23:03:39 +00:00
// string Script;
// }
2008-05-30 12:27:06 +00:00
2008-06-24 21:09:49 +00:00
public IConfig Config
2008-05-30 12:27:06 +00:00
{
get { return m_ScriptConfig ; }
}
2010-09-25 20:22:18 +00:00
public string ScriptEnginePath
{
get { return m_ScriptEnginesPath ; }
}
2009-06-06 16:39:28 +00:00
public IConfigSource ConfigSource
{
get { return m_ConfigSource ; }
}
2017-01-07 00:25:59 +00:00
private class ScriptCompileInfo
{
public List < EventParams > eventList = new List < EventParams > ( ) ;
}
2012-03-15 00:20:47 +00:00
/// <summary>
/// Event fired after the script engine has finished removing a script.
/// </summary>
2009-04-10 19:07:41 +00:00
public event ScriptRemoved OnScriptRemoved ;
2012-03-15 00:20:47 +00:00
/// <summary>
/// Event fired after the script engine has finished removing a script from an object.
/// </summary>
2009-04-10 19:07:41 +00:00
public event ObjectRemoved OnObjectRemoved ;
2009-04-15 18:51:17 +00:00
public void Initialise ( IConfigSource configSource )
2008-05-30 12:27:06 +00:00
{
2009-04-15 18:51:17 +00:00
if ( configSource . Configs [ "XEngine" ] = = null )
return ;
2008-05-30 12:27:06 +00:00
m_ScriptConfig = configSource . Configs [ "XEngine" ] ;
2009-06-06 16:39:28 +00:00
m_ConfigSource = configSource ;
2013-01-17 23:39:09 +00:00
2015-01-27 21:04:11 +00:00
string rawScriptStopStrategy = m_ScriptConfig . GetString ( "ScriptStopStrategy" , "co-op" ) ;
2013-01-23 23:34:15 +00:00
m_log . InfoFormat ( "[XEngine]: Script stop strategy is {0}" , rawScriptStopStrategy ) ;
if ( rawScriptStopStrategy = = "co-op" )
2013-01-23 02:28:27 +00:00
{
2014-07-10 23:03:02 +00:00
m_coopTermination = true ;
2013-01-23 02:28:27 +00:00
ScriptClassName = "XEngineScript" ;
ScriptBaseClassName = typeof ( XEngineScriptBase ) . FullName ;
ScriptBaseClassParameters = typeof ( XEngineScriptBase ) . GetConstructor ( new Type [ ] { typeof ( WaitHandle ) } ) . GetParameters ( ) ;
ScriptReferencedAssemblies = new string [ ] { Path . GetFileName ( typeof ( XEngineScriptBase ) . Assembly . Location ) } ;
}
else
{
ScriptClassName = "Script" ;
ScriptBaseClassName = typeof ( ScriptBaseClass ) . FullName ;
}
2013-01-17 23:39:09 +00:00
2013-01-22 01:00:53 +00:00
// Console.WriteLine("ASSEMBLY NAME: {0}", ScriptReferencedAssemblies[0]);
2009-04-15 18:51:17 +00:00
}
public void AddRegion ( Scene scene )
{
if ( m_ScriptConfig = = null )
return ;
2013-01-23 23:34:15 +00:00
2009-01-28 09:22:12 +00:00
m_ScriptFailCount = 0 ;
m_ScriptErrorMessage = String . Empty ;
2008-05-30 12:27:06 +00:00
2008-09-21 21:47:00 +00:00
m_Enabled = m_ScriptConfig . GetBoolean ( "Enabled" , true ) ;
if ( ! m_Enabled )
return ;
AppDomain . CurrentDomain . AssemblyResolve + =
OnAssemblyResolve ;
m_Scene = scene ;
2012-06-19 23:10:19 +00:00
m_log . InfoFormat ( "[XEngine]: Initializing scripts in region {0}" , m_Scene . RegionInfo . RegionName ) ;
2008-09-21 21:47:00 +00:00
2008-09-21 02:52:12 +00:00
m_MinThreads = m_ScriptConfig . GetInt ( "MinThreads" , 2 ) ;
m_MaxThreads = m_ScriptConfig . GetInt ( "MaxThreads" , 100 ) ;
m_IdleTimeout = m_ScriptConfig . GetInt ( "IdleTimeout" , 60 ) ;
2008-05-30 12:27:06 +00:00
string priority = m_ScriptConfig . GetString ( "Priority" , "BelowNormal" ) ;
2012-07-11 20:55:18 +00:00
m_StartDelay = m_ScriptConfig . GetInt ( "StartDelay" , 15000 ) ;
2009-01-19 17:11:57 +00:00
m_MaxScriptQueue = m_ScriptConfig . GetInt ( "MaxScriptEventQueue" , 300 ) ;
2008-09-21 02:52:12 +00:00
m_StackSize = m_ScriptConfig . GetInt ( "ThreadStackSize" , 262144 ) ;
m_SleepTime = m_ScriptConfig . GetInt ( "MaintenanceInterval" , 10 ) * 1000 ;
2016-11-03 12:58:43 +00:00
m_AppDomainLoading = m_ScriptConfig . GetBoolean ( "AppDomainLoading" , false ) ;
2020-07-27 15:13:09 +00:00
m_AttachmentsDomainLoading = m_ScriptConfig . GetBoolean ( "AttachmentsDomainLoading" , false ) ;
2008-05-30 12:27:06 +00:00
m_EventLimit = m_ScriptConfig . GetInt ( "EventLimit" , 30 ) ;
2008-09-02 12:07:23 +00:00
m_KillTimedOutScripts = m_ScriptConfig . GetBoolean ( "KillTimedOutScripts" , false ) ;
2008-09-21 02:52:12 +00:00
m_SaveTime = m_ScriptConfig . GetInt ( "SaveInterval" , 120 ) * 1000 ;
2017-01-05 19:07:37 +00:00
m_WaitForEventCompletionOnScriptStop
2012-12-13 21:02:55 +00:00
= m_ScriptConfig . GetInt ( "WaitForEventCompletionOnScriptStop" , m_WaitForEventCompletionOnScriptStop ) ;
2010-09-25 20:22:18 +00:00
m_ScriptEnginesPath = m_ScriptConfig . GetString ( "ScriptEnginesPath" , "ScriptEngines" ) ;
2008-05-30 12:27:06 +00:00
2008-09-21 02:52:12 +00:00
m_Prio = ThreadPriority . BelowNormal ;
2008-06-04 09:59:27 +00:00
switch ( priority )
2008-05-30 12:27:06 +00:00
{
2008-09-16 16:37:16 +00:00
case "Lowest" :
2008-09-21 02:52:12 +00:00
m_Prio = ThreadPriority . Lowest ;
2008-09-16 16:37:16 +00:00
break ;
case "BelowNormal" :
2008-09-21 02:52:12 +00:00
m_Prio = ThreadPriority . BelowNormal ;
2008-09-16 16:37:16 +00:00
break ;
case "Normal" :
2008-09-21 02:52:12 +00:00
m_Prio = ThreadPriority . Normal ;
2008-09-16 16:37:16 +00:00
break ;
case "AboveNormal" :
2008-09-21 02:52:12 +00:00
m_Prio = ThreadPriority . AboveNormal ;
2008-09-16 16:37:16 +00:00
break ;
case "Highest" :
2008-09-21 02:52:12 +00:00
m_Prio = ThreadPriority . Highest ;
2008-09-16 16:37:16 +00:00
break ;
default :
m_log . ErrorFormat ( "[XEngine] Invalid thread priority: '{0}'. Assuming BelowNormal" , priority ) ;
break ;
2008-05-30 12:27:06 +00:00
}
2008-06-04 09:59:27 +00:00
lock ( m_ScriptEngines )
2008-05-30 12:27:06 +00:00
{
m_ScriptEngines . Add ( this ) ;
}
2008-09-21 02:52:12 +00:00
// Needs to be here so we can queue the scripts that need starting
/ /
m_Scene . EventManager . OnRezScript + = OnRezScript ;
2008-05-30 12:27:06 +00:00
2008-09-21 02:52:12 +00:00
// Complete basic setup of the thread pool
/ /
SetupEngine ( m_MinThreads , m_MaxThreads , m_IdleTimeout , m_Prio ,
m_MaxScriptQueue , m_StackSize ) ;
2008-09-25 17:26:32 +00:00
m_Scene . StackModuleInterface < IScriptModule > ( this ) ;
2009-04-10 21:08:33 +00:00
m_XmlRpcRouter = m_Scene . RequestModuleInterface < IXmlRpcRouter > ( ) ;
if ( m_XmlRpcRouter ! = null )
{
OnScriptRemoved + = m_XmlRpcRouter . ScriptRemoved ;
OnObjectRemoved + = m_XmlRpcRouter . ObjectRemoved ;
}
2011-10-19 19:09:02 +00:00
2013-01-10 00:32:22 +00:00
m_consoleCommands = new ScriptEngineConsoleCommands ( this ) ;
m_consoleCommands . RegisterCommands ( ) ;
2012-02-01 00:07:06 +00:00
MainConsole . Instance . Commands . AddCommand (
2012-03-08 01:51:37 +00:00
"Scripts" , false , "xengine status" , "xengine status" , "Show status information" ,
2012-02-01 00:07:06 +00:00
"Show status information on the script engine." ,
HandleShowStatus ) ;
2011-10-19 19:09:02 +00:00
MainConsole . Instance . Commands . AddCommand (
2013-01-09 00:01:48 +00:00
"Scripts" , false , "scripts show" , "scripts show [<script-item-uuid>+]" , "Show script information" ,
2012-12-12 23:13:34 +00:00
"Show information on all scripts known to the script engine.\n"
2013-01-09 00:01:48 +00:00
+ "If one or more <script-item-uuid>s are given then only information on that script will be shown." ,
2012-01-14 00:44:19 +00:00
HandleShowScripts ) ;
2011-10-19 19:09:02 +00:00
MainConsole . Instance . Commands . AddCommand (
2013-01-09 00:01:48 +00:00
"Scripts" , false , "show scripts" , "show scripts [<script-item-uuid>+]" , "Show script information" ,
2011-10-19 19:09:02 +00:00
"Synonym for scripts show command" , HandleShowScripts ) ;
2011-10-19 19:53:24 +00:00
MainConsole . Instance . Commands . AddCommand (
2013-01-09 00:01:48 +00:00
"Scripts" , false , "scripts suspend" , "scripts suspend [<script-item-uuid>+]" , "Suspends all running scripts" ,
2011-10-19 19:53:24 +00:00
"Suspends all currently running scripts. This only suspends event delivery, it will not suspend a"
2011-10-19 20:34:55 +00:00
+ " script that is currently processing an event.\n"
2011-10-19 21:30:31 +00:00
+ "Suspended scripts will continue to accumulate events but won't process them.\n"
2013-01-09 00:01:48 +00:00
+ "If one or more <script-item-uuid>s are given then only that script will be suspended. Otherwise, all suitable scripts are suspended." ,
2011-10-19 21:30:31 +00:00
( module , cmdparams ) = > HandleScriptsAction ( cmdparams , HandleSuspendScript ) ) ;
2011-10-19 19:53:24 +00:00
MainConsole . Instance . Commands . AddCommand (
2013-01-09 00:01:48 +00:00
"Scripts" , false , "scripts resume" , "scripts resume [<script-item-uuid>+]" , "Resumes all suspended scripts" ,
2011-10-19 20:34:55 +00:00
"Resumes all currently suspended scripts.\n"
2012-12-12 23:13:34 +00:00
+ "Resumed scripts will process all events accumulated whilst suspended.\n"
2013-01-09 00:01:48 +00:00
+ "If one or more <script-item-uuid>s are given then only that script will be resumed. Otherwise, all suitable scripts are resumed." ,
2011-10-19 21:30:31 +00:00
( module , cmdparams ) = > HandleScriptsAction ( cmdparams , HandleResumeScript ) ) ;
2011-10-19 20:40:28 +00:00
MainConsole . Instance . Commands . AddCommand (
2013-01-09 00:01:48 +00:00
"Scripts" , false , "scripts stop" , "scripts stop [<script-item-uuid>+]" , "Stops all running scripts" ,
2012-12-12 23:13:34 +00:00
"Stops all running scripts.\n"
2013-01-09 00:01:48 +00:00
+ "If one or more <script-item-uuid>s are given then only that script will be stopped. Otherwise, all suitable scripts are stopped." ,
2011-10-19 21:30:31 +00:00
( module , cmdparams ) = > HandleScriptsAction ( cmdparams , HandleStopScript ) ) ;
2011-10-19 20:40:28 +00:00
MainConsole . Instance . Commands . AddCommand (
2013-01-09 00:01:48 +00:00
"Scripts" , false , "scripts start" , "scripts start [<script-item-uuid>+]" , "Starts all stopped scripts" ,
2012-12-12 23:13:34 +00:00
"Starts all stopped scripts.\n"
2013-01-09 00:01:48 +00:00
+ "If one or more <script-item-uuid>s are given then only that script will be started. Otherwise, all suitable scripts are started." ,
2011-10-19 21:30:31 +00:00
( module , cmdparams ) = > HandleScriptsAction ( cmdparams , HandleStartScript ) ) ;
2012-06-19 23:10:19 +00:00
2012-12-12 23:13:34 +00:00
MainConsole . Instance . Commands . AddCommand (
2014-07-10 23:18:39 +00:00
"Debug" , false , "debug scripts log" , "debug scripts log <item-id> <log-level>" , "Extra debug logging for a particular script." ,
2012-12-12 23:13:34 +00:00
"Activates or deactivates extra debug logging for the given script.\n"
+ "Level == 0, deactivate extra debug logging.\n"
+ "Level >= 1, log state changes.\n"
+ "Level >= 2, log event invocations.\n" ,
HandleDebugScriptLogCommand ) ;
2014-12-03 20:48:17 +00:00
MainConsole . Instance . Commands . AddCommand (
"Debug" , false , "debug xengine log" , "debug xengine log [<level>]" ,
"Turn on detailed xengine debugging." ,
"If level <= 0, then no extra logging is done.\n"
+ "If level >= 1, then we log every time that a script is started." ,
HandleDebugLevelCommand ) ;
2011-10-19 19:09:02 +00:00
}
2012-12-12 23:13:34 +00:00
private void HandleDebugScriptLogCommand ( string module , string [ ] args )
{
if ( ! ( MainConsole . Instance . ConsoleScene = = null | | MainConsole . Instance . ConsoleScene = = m_Scene ) )
return ;
if ( args . Length ! = 5 )
{
MainConsole . Instance . Output ( "Usage: debug script log <item-id> <log-level>" ) ;
return ;
}
UUID itemId ;
if ( ! ConsoleUtil . TryParseConsoleUuid ( MainConsole . Instance , args [ 3 ] , out itemId ) )
return ;
int newLevel ;
if ( ! ConsoleUtil . TryParseConsoleInt ( MainConsole . Instance , args [ 4 ] , out newLevel ) )
return ;
IScriptInstance si ;
lock ( m_Scripts )
{
// XXX: We can't give the user feedback on a bad item id because this may apply to a different script
// engine
if ( ! m_Scripts . TryGetValue ( itemId , out si ) )
return ;
}
si . DebugLevel = newLevel ;
2019-10-22 12:04:01 +00:00
MainConsole . Instance . Output ( "Set debug level of {0} {1} to {2}" , si . ScriptName , si . ItemID , newLevel ) ;
2012-12-12 23:13:34 +00:00
}
2012-06-19 23:10:19 +00:00
/// <summary>
/// Change debug level
/// </summary>
/// <param name="module"></param>
/// <param name="args"></param>
2014-12-03 20:48:17 +00:00
private void HandleDebugLevelCommand ( string module , string [ ] args )
{
2015-07-27 05:54:18 +00:00
if ( args . Length > = 4 )
2014-12-03 20:48:17 +00:00
{
int newDebug ;
if ( ConsoleUtil . TryParseConsoleNaturalInt ( MainConsole . Instance , args [ 3 ] , out newDebug ) )
{
DebugLevel = newDebug ;
2019-10-22 12:04:01 +00:00
MainConsole . Instance . Output ( "Debug level set to {0} in XEngine for region {1}" , newDebug , m_Scene . Name ) ;
2014-12-03 20:48:17 +00:00
}
}
else if ( args . Length = = 3 )
{
2019-10-22 12:04:01 +00:00
MainConsole . Instance . Output ( "Current debug level is {0}" , DebugLevel ) ;
2014-12-03 20:48:17 +00:00
}
else
{
MainConsole . Instance . Output ( "Usage: debug xengine log <level>" ) ;
}
}
2012-06-19 23:10:19 +00:00
2011-10-19 21:30:31 +00:00
/// <summary>
/// Parse the raw item id into a script instance from the command params if it's present.
/// </summary>
/// <param name="cmdparams"></param>
/// <param name="instance"></param>
2012-12-05 23:27:50 +00:00
/// <param name="comparer">Basis on which to sort output. Can be null if no sort needs to take place</param>
2011-10-19 21:30:31 +00:00
private void HandleScriptsAction ( string [ ] cmdparams , Action < IScriptInstance > action )
2012-12-05 23:27:50 +00:00
{
HandleScriptsAction < object > ( cmdparams , action , null ) ;
}
/// <summary>
/// Parse the raw item id into a script instance from the command params if it's present.
/// </summary>
/// <param name="cmdparams"></param>
/// <param name="instance"></param>
/// <param name="keySelector">Basis on which to sort output. Can be null if no sort needs to take place</param>
private void HandleScriptsAction < TKey > (
2013-05-01 18:01:43 +00:00
string [ ] cmdparams , Action < IScriptInstance > action , System . Func < IScriptInstance , TKey > keySelector )
2011-10-19 19:53:24 +00:00
{
2012-02-01 00:10:07 +00:00
if ( ! ( MainConsole . Instance . ConsoleScene = = null | | MainConsole . Instance . ConsoleScene = = m_Scene ) )
return ;
2011-10-19 19:53:24 +00:00
lock ( m_Scripts )
{
2011-10-19 21:30:31 +00:00
string rawItemId ;
UUID itemId = UUID . Zero ;
2017-01-05 19:07:37 +00:00
2011-10-19 21:30:31 +00:00
if ( cmdparams . Length = = 2 )
2011-10-19 19:53:24 +00:00
{
2012-12-05 23:27:50 +00:00
IEnumerable < IScriptInstance > scripts = m_Scripts . Values ;
if ( keySelector ! = null )
scripts = scripts . OrderBy < IScriptInstance , TKey > ( keySelector ) ;
foreach ( IScriptInstance instance in scripts )
2011-10-19 21:30:31 +00:00
action ( instance ) ;
2011-10-19 19:53:24 +00:00
2011-10-19 21:30:31 +00:00
return ;
}
2017-01-05 19:07:37 +00:00
2013-01-09 00:01:48 +00:00
for ( int i = 2 ; i < cmdparams . Length ; i + + )
2011-10-19 21:30:31 +00:00
{
2013-01-09 00:01:48 +00:00
rawItemId = cmdparams [ i ] ;
2017-01-05 19:07:37 +00:00
2013-01-09 00:01:48 +00:00
if ( ! UUID . TryParse ( rawItemId , out itemId ) )
2011-10-19 21:30:31 +00:00
{
2019-10-22 12:04:01 +00:00
MainConsole . Instance . Output ( "ERROR: {0} is not a valid UUID" , rawItemId ) ;
2013-01-09 00:01:48 +00:00
continue ;
2011-10-19 21:30:31 +00:00
}
2017-01-05 19:07:37 +00:00
2013-01-09 00:01:48 +00:00
if ( itemId ! = UUID . Zero )
2011-10-19 21:30:31 +00:00
{
2013-01-09 00:01:48 +00:00
IScriptInstance instance = GetInstance ( itemId ) ;
if ( instance = = null )
{
// Commented out for now since this will cause false reports on simulators with more than
// one scene where the current command line set region is 'root' (which causes commands to
// go to both regions... (sigh)
// MainConsole.Instance.OutputFormat("Error - No item found with id {0}", itemId);
continue ;
}
else
{
action ( instance ) ;
}
2011-10-19 19:53:24 +00:00
}
}
}
}
2012-02-01 00:07:06 +00:00
private void HandleShowStatus ( string module , string [ ] cmdparams )
{
2012-02-01 00:10:07 +00:00
if ( ! ( MainConsole . Instance . ConsoleScene = = null | | MainConsole . Instance . ConsoleScene = = m_Scene ) )
return ;
2013-04-02 00:28:16 +00:00
MainConsole . Instance . Output ( GetStatusReport ( ) ) ;
2012-03-16 03:32:14 +00:00
}
public string GetStatusReport ( )
{
2012-02-01 00:07:06 +00:00
StringBuilder sb = new StringBuilder ( ) ;
sb . AppendFormat ( "Status of XEngine instance for {0}\n" , m_Scene . RegionInfo . RegionName ) ;
2012-12-05 23:41:50 +00:00
long scriptsLoaded , eventsQueued = 0 , eventsProcessed = 0 ;
2012-02-01 00:07:06 +00:00
lock ( m_Scripts )
2012-12-05 23:41:50 +00:00
{
scriptsLoaded = m_Scripts . Count ;
foreach ( IScriptInstance si in m_Scripts . Values )
{
eventsQueued + = si . EventsQueued ;
eventsProcessed + = si . EventsProcessed ;
}
}
2012-02-01 00:07:06 +00:00
2012-12-05 23:41:50 +00:00
sb . AppendFormat ( "Scripts loaded : {0}\n" , scriptsLoaded ) ;
2012-02-01 00:07:06 +00:00
sb . AppendFormat ( "Scripts waiting for load : {0}\n" , m_CompileQueue . Count ) ;
2012-03-12 21:16:05 +00:00
sb . AppendFormat ( "Max threads : {0}\n" , m_ThreadPool . MaxThreads ) ;
sb . AppendFormat ( "Min threads : {0}\n" , m_ThreadPool . MinThreads ) ;
2012-02-01 00:07:06 +00:00
sb . AppendFormat ( "Allocated threads : {0}\n" , m_ThreadPool . ActiveThreads ) ;
sb . AppendFormat ( "In use threads : {0}\n" , m_ThreadPool . InUseThreads ) ;
sb . AppendFormat ( "Work items waiting : {0}\n" , m_ThreadPool . WaitingCallbacks ) ;
// sb.AppendFormat("Assemblies loaded : {0}\n", m_Assemblies.Count);
2012-12-05 23:41:50 +00:00
sb . AppendFormat ( "Events queued : {0}\n" , eventsQueued ) ;
sb . AppendFormat ( "Events processed : {0}\n" , eventsProcessed ) ;
2012-02-01 00:07:06 +00:00
2012-03-06 23:21:17 +00:00
SensorRepeat sr = AsyncCommandManager . GetSensorRepeatPlugin ( this ) ;
2012-03-19 21:43:23 +00:00
sb . AppendFormat ( "Sensors : {0}\n" , sr ! = null ? sr . SensorsCount : 0 ) ;
2012-03-06 23:21:17 +00:00
Dataserver ds = AsyncCommandManager . GetDataserverPlugin ( this ) ;
2012-03-19 21:43:23 +00:00
sb . AppendFormat ( "Dataserver requests : {0}\n" , ds ! = null ? ds . DataserverRequestsCount : 0 ) ;
2012-03-06 23:21:17 +00:00
Timer t = AsyncCommandManager . GetTimerPlugin ( this ) ;
2012-03-19 21:43:23 +00:00
sb . AppendFormat ( "Timers : {0}\n" , t ! = null ? t . TimersCount : 0 ) ;
2012-03-06 23:21:17 +00:00
Listener l = AsyncCommandManager . GetListenerPlugin ( this ) ;
2012-03-19 21:43:23 +00:00
sb . AppendFormat ( "Listeners : {0}\n" , l ! = null ? l . ListenerCount : 0 ) ;
2012-03-06 23:21:17 +00:00
2012-03-16 03:32:14 +00:00
return sb . ToString ( ) ;
2012-02-01 00:07:06 +00:00
}
2012-01-14 00:44:19 +00:00
public void HandleShowScripts ( string module , string [ ] cmdparams )
{
2012-02-01 00:10:07 +00:00
if ( ! ( MainConsole . Instance . ConsoleScene = = null | | MainConsole . Instance . ConsoleScene = = m_Scene ) )
return ;
2012-01-14 00:44:19 +00:00
if ( cmdparams . Length = = 2 )
{
lock ( m_Scripts )
{
2019-08-20 22:28:59 +00:00
MainConsole . Instance . Output (
2019-10-22 12:04:01 +00:00
"Showing {0} scripts in {1}" , m_Scripts . Count , m_Scene . RegionInfo . RegionName ) ;
2012-01-14 00:44:19 +00:00
}
}
2012-12-05 23:27:50 +00:00
HandleScriptsAction < long > ( cmdparams , HandleShowScript , si = > si . EventsProcessed ) ;
2012-01-14 00:44:19 +00:00
}
private void HandleShowScript ( IScriptInstance instance )
{
SceneObjectPart sop = m_Scene . GetSceneObjectPart ( instance . ObjectID ) ;
string status ;
if ( instance . ShuttingDown )
{
status = "shutting down" ;
}
else if ( instance . Suspended )
{
status = "suspended" ;
}
else if ( ! instance . Running )
{
status = "stopped" ;
}
else
{
status = "running" ;
}
2012-02-01 00:27:42 +00:00
StringBuilder sb = new StringBuilder ( ) ;
2012-02-01 00:17:02 +00:00
2012-02-01 00:27:42 +00:00
sb . AppendFormat ( "Script name : {0}\n" , instance . ScriptName ) ;
sb . AppendFormat ( "Status : {0}\n" , status ) ;
2012-12-05 23:33:48 +00:00
sb . AppendFormat ( "Queued events : {0}\n" , instance . EventsQueued ) ;
2012-12-05 22:33:28 +00:00
sb . AppendFormat ( "Processed events : {0}\n" , instance . EventsProcessed ) ;
2012-02-01 00:27:42 +00:00
sb . AppendFormat ( "Item UUID : {0}\n" , instance . ItemID ) ;
2012-12-12 22:06:09 +00:00
sb . AppendFormat ( "Asset UUID : {0}\n" , instance . AssetID ) ;
2012-02-01 00:27:42 +00:00
sb . AppendFormat ( "Containing part name: {0}\n" , instance . PrimName ) ;
sb . AppendFormat ( "Containing part UUID: {0}\n" , instance . ObjectID ) ;
sb . AppendFormat ( "Position : {0}\n" , sop . AbsolutePosition ) ;
2013-04-02 00:28:16 +00:00
MainConsole . Instance . Output ( sb . ToString ( ) ) ;
2012-01-14 00:44:19 +00:00
}
2011-10-19 21:30:31 +00:00
private void HandleSuspendScript ( IScriptInstance instance )
2011-10-19 19:53:24 +00:00
{
2011-10-19 21:30:31 +00:00
if ( ! instance . Suspended )
2011-10-19 19:53:24 +00:00
{
2011-10-19 21:30:31 +00:00
instance . Suspend ( ) ;
2011-10-19 19:53:24 +00:00
2011-10-19 21:30:31 +00:00
SceneObjectPart sop = m_Scene . GetSceneObjectPart ( instance . ObjectID ) ;
2019-08-20 22:28:59 +00:00
MainConsole . Instance . Output (
2011-10-19 21:30:31 +00:00
"Suspended {0}.{1}, item UUID {2}, prim UUID {3} @ {4}" ,
instance . PrimName , instance . ScriptName , instance . ItemID , instance . ObjectID , sop . AbsolutePosition ) ;
2011-10-19 20:40:28 +00:00
}
}
2011-10-19 21:30:31 +00:00
private void HandleResumeScript ( IScriptInstance instance )
2011-10-19 20:40:28 +00:00
{
2011-10-19 21:30:31 +00:00
if ( instance . Suspended )
2011-10-19 20:40:28 +00:00
{
2011-10-19 21:30:31 +00:00
instance . Resume ( ) ;
2011-10-19 20:40:28 +00:00
2011-10-19 21:30:31 +00:00
SceneObjectPart sop = m_Scene . GetSceneObjectPart ( instance . ObjectID ) ;
2019-08-20 22:28:59 +00:00
MainConsole . Instance . Output (
2011-10-19 21:30:31 +00:00
"Resumed {0}.{1}, item UUID {2}, prim UUID {3} @ {4}" ,
instance . PrimName , instance . ScriptName , instance . ItemID , instance . ObjectID , sop . AbsolutePosition ) ;
2011-10-19 20:40:28 +00:00
}
}
2011-10-19 21:30:31 +00:00
private void HandleStartScript ( IScriptInstance instance )
2011-10-19 20:40:28 +00:00
{
2011-10-19 21:30:31 +00:00
if ( ! instance . Running )
2011-10-19 20:40:28 +00:00
{
2011-10-19 21:30:31 +00:00
instance . Start ( ) ;
2011-10-19 20:40:28 +00:00
2011-10-19 21:30:31 +00:00
SceneObjectPart sop = m_Scene . GetSceneObjectPart ( instance . ObjectID ) ;
2019-08-20 22:28:59 +00:00
MainConsole . Instance . Output (
2011-10-19 21:30:31 +00:00
"Started {0}.{1}, item UUID {2}, prim UUID {3} @ {4}" ,
instance . PrimName , instance . ScriptName , instance . ItemID , instance . ObjectID , sop . AbsolutePosition ) ;
}
}
private void HandleStopScript ( IScriptInstance instance )
{
if ( instance . Running )
{
2015-08-07 13:35:32 +00:00
instance . StayStopped = true ; // the script was stopped explicitly
2011-10-19 21:30:31 +00:00
instance . Stop ( 0 ) ;
SceneObjectPart sop = m_Scene . GetSceneObjectPart ( instance . ObjectID ) ;
2019-08-20 22:28:59 +00:00
MainConsole . Instance . Output (
2011-10-19 21:30:31 +00:00
"Stopped {0}.{1}, item UUID {2}, prim UUID {3} @ {4}" ,
instance . PrimName , instance . ScriptName , instance . ItemID , instance . ObjectID , sop . AbsolutePosition ) ;
2011-10-19 19:53:24 +00:00
}
}
2009-04-15 18:51:17 +00:00
public void RemoveRegion ( Scene scene )
{
2011-10-14 00:06:43 +00:00
if ( ! m_Enabled )
return ;
2015-09-02 18:54:53 +00:00
lock ( m_Scripts )
2009-04-15 18:51:17 +00:00
{
2015-09-02 18:54:53 +00:00
m_log . InfoFormat (
"[XEngine]: Shutting down {0} scripts in {1}" , m_Scripts . Count , m_Scene . RegionInfo . RegionName ) ;
foreach ( IScriptInstance instance in m_Scripts . Values )
2009-04-15 18:51:17 +00:00
{
// Force a final state save
/ /
2014-12-10 00:25:27 +00:00
try
2009-04-15 18:51:17 +00:00
{
2015-01-16 22:44:54 +00:00
if ( instance . StatePersistedHere )
instance . SaveState ( ) ;
2014-12-10 00:25:27 +00:00
}
catch ( Exception e )
{
m_log . Error (
string . Format (
"[XEngine]: Failed final state save for script {0}.{1}, item UUID {2}, prim UUID {3} in {4}. Exception " ,
instance . PrimName , instance . ScriptName , instance . ItemID , instance . ObjectID , World . Name )
, e ) ;
2009-04-15 18:51:17 +00:00
}
// Clear the event queue and abort the instance thread
/ /
2015-01-16 23:55:00 +00:00
instance . Stop ( 0 , true ) ;
2009-04-15 18:51:17 +00:00
2015-09-02 18:54:53 +00:00
// Release events, timer, etc
/ /
instance . DestroyScriptInstance ( ) ;
// Unload scripts and app domains.
// Must be done explicitly because they have infinite
// lifetime.
// However, don't bother to do this if the simulator is shutting
// down since it takes a long time with many scripts.
if ( ! m_SimulatorShuttingDown )
2009-04-15 18:51:17 +00:00
{
2015-09-02 18:54:53 +00:00
m_DomainScripts [ instance . AppDomain ] . Remove ( instance . ItemID ) ;
if ( m_DomainScripts [ instance . AppDomain ] . Count = = 0 )
{
m_DomainScripts . Remove ( instance . AppDomain ) ;
UnloadAppDomain ( instance . AppDomain ) ;
}
2009-04-15 18:51:17 +00:00
}
}
2011-11-17 21:03:08 +00:00
2015-09-02 18:54:53 +00:00
m_Scripts . Clear ( ) ;
m_PrimObjects . Clear ( ) ;
m_Assemblies . Clear ( ) ;
m_DomainScripts . Clear ( ) ;
2009-04-15 18:51:17 +00:00
}
lock ( m_ScriptEngines )
{
m_ScriptEngines . Remove ( this ) ;
}
}
public void RegionLoaded ( Scene scene )
2008-09-21 02:52:12 +00:00
{
2008-09-21 21:47:00 +00:00
if ( ! m_Enabled )
return ;
2008-09-21 02:52:12 +00:00
m_EventManager = new EventManager ( this ) ;
2008-05-30 12:27:06 +00:00
m_Compiler = new Compiler ( this ) ;
m_Scene . EventManager . OnRemoveScript + = OnRemoveScript ;
m_Scene . EventManager . OnScriptReset + = OnScriptReset ;
2008-06-28 16:08:12 +00:00
m_Scene . EventManager . OnStartScript + = OnStartScript ;
m_Scene . EventManager . OnStopScript + = OnStopScript ;
2008-09-25 05:13:44 +00:00
m_Scene . EventManager . OnGetScriptRunning + = OnGetScriptRunning ;
2008-09-07 22:01:25 +00:00
m_Scene . EventManager . OnShutdown + = OnShutdown ;
2008-05-30 12:27:06 +00:00
2012-07-18 21:24:52 +00:00
// If region ready has been triggered, then the region had no scripts to compile and completed its other
// work.
2012-07-25 20:00:59 +00:00
m_Scene . EventManager . OnRegionReadyStatusChange + = s = > { if ( s . Ready ) m_InitialStartup = false ; } ;
2012-07-18 21:24:52 +00:00
2008-09-21 02:52:12 +00:00
if ( m_SleepTime > 0 )
2008-05-30 12:27:06 +00:00
{
2008-09-09 01:26:48 +00:00
m_ThreadPool . QueueWorkItem ( new WorkItemCallback ( this . DoMaintenance ) ,
2009-01-19 17:11:57 +00:00
new Object [ ] { m_SleepTime } ) ;
2008-05-30 12:27:06 +00:00
}
2008-09-21 02:52:12 +00:00
if ( m_SaveTime > 0 )
2008-05-30 12:27:06 +00:00
{
2008-09-09 01:26:48 +00:00
m_ThreadPool . QueueWorkItem ( new WorkItemCallback ( this . DoBackup ) ,
2008-09-21 02:52:12 +00:00
new Object [ ] { m_SaveTime } ) ;
2008-05-30 12:27:06 +00:00
}
2011-09-05 09:45:02 +00:00
}
2008-06-28 16:08:12 +00:00
2011-09-05 09:45:02 +00:00
public void StartProcessing ( )
{
2008-05-30 12:27:06 +00:00
m_ThreadPool . Start ( ) ;
}
public void Close ( )
{
2011-10-14 00:06:43 +00:00
if ( ! m_Enabled )
return ;
2017-01-05 19:07:37 +00:00
2008-06-04 09:59:27 +00:00
lock ( m_ScriptEngines )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
if ( m_ScriptEngines . Contains ( this ) )
2008-05-30 12:27:06 +00:00
m_ScriptEngines . Remove ( this ) ;
}
2017-05-23 02:26:32 +00:00
lock ( m_Scripts )
m_ThreadPool . Shutdown ( ) ;
2008-05-30 12:27:06 +00:00
}
public object DoBackup ( object o )
{
Object [ ] p = ( Object [ ] ) o ;
int saveTime = ( int ) p [ 0 ] ;
2008-09-07 22:01:25 +00:00
if ( saveTime > 0 )
System . Threading . Thread . Sleep ( saveTime ) ;
2008-05-30 12:27:06 +00:00
2009-01-19 17:11:57 +00:00
// m_log.Debug("[XEngine] Backing up script states");
2008-05-30 12:27:06 +00:00
2008-08-27 22:38:36 +00:00
List < IScriptInstance > instances = new List < IScriptInstance > ( ) ;
2008-05-30 12:27:06 +00:00
2008-06-04 09:59:27 +00:00
lock ( m_Scripts )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
foreach ( IScriptInstance instance in m_Scripts . Values )
2015-01-16 22:44:54 +00:00
{
if ( instance . StatePersistedHere )
{
// m_log.DebugFormat(
// "[XEngine]: Adding script {0}.{1}, item UUID {2}, prim UUID {3} in {4} for state persistence",
// instance.PrimName, instance.ScriptName, instance.ItemID, instance.ObjectID, World.Name);
instances . Add ( instance ) ;
}
}
2008-05-30 12:27:06 +00:00
}
2008-08-27 22:38:36 +00:00
foreach ( IScriptInstance i in instances )
2008-05-30 12:27:06 +00:00
{
2012-08-23 22:13:53 +00:00
try
{
2014-12-10 00:25:27 +00:00
i . SaveState ( ) ;
2012-08-23 22:13:53 +00:00
}
catch ( Exception e )
{
m_log . Error (
string . Format (
"[XEngine]: Failed to save state of script {0}.{1}, item UUID {2}, prim UUID {3} in {4}. Exception " ,
i . PrimName , i . ScriptName , i . ItemID , i . ObjectID , World . Name )
, e ) ;
}
2008-05-30 12:27:06 +00:00
}
2008-09-07 22:01:25 +00:00
if ( saveTime > 0 )
2008-09-09 01:26:48 +00:00
m_ThreadPool . QueueWorkItem ( new WorkItemCallback ( this . DoBackup ) ,
2008-09-07 22:01:25 +00:00
new Object [ ] { saveTime } ) ;
2008-05-30 12:27:06 +00:00
return 0 ;
}
2011-05-24 02:17:12 +00:00
public void SaveAllState ( )
2008-05-30 12:27:06 +00:00
{
2015-01-17 00:05:13 +00:00
DoBackup ( new object [ ] { 0 } ) ;
}
public object DoMaintenance ( object p )
{
object [ ] parms = ( object [ ] ) p ;
int sleepTime = ( int ) parms [ 0 ] ;
2008-08-27 22:38:36 +00:00
foreach ( IScriptInstance inst in m_Scripts . Values )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
if ( inst . EventTime ( ) > m_EventLimit )
2008-05-30 12:27:06 +00:00
{
inst . Stop ( 100 ) ;
2008-06-04 09:59:27 +00:00
if ( ! m_KillTimedOutScripts )
2008-05-30 12:27:06 +00:00
inst . Start ( ) ;
}
}
System . Threading . Thread . Sleep ( sleepTime ) ;
2008-09-09 01:26:48 +00:00
m_ThreadPool . QueueWorkItem ( new WorkItemCallback ( this . DoMaintenance ) ,
2009-01-19 17:11:57 +00:00
new Object [ ] { sleepTime } ) ;
2008-05-30 12:27:06 +00:00
return 0 ;
}
2010-09-25 20:22:18 +00:00
public Type ReplaceableInterface
2009-07-10 20:17:13 +00:00
{
get { return null ; }
}
2008-05-30 12:27:06 +00:00
public string Name
{
get { return "XEngine" ; }
}
2008-11-09 19:30:40 +00:00
public void OnRezScript ( uint localID , UUID itemID , string script , int startParam , bool postOnRez , string engine , int stateSource )
2008-06-28 16:08:12 +00:00
{
2012-02-07 17:44:37 +00:00
// m_log.DebugFormat(
// "[XEngine]: OnRezScript event triggered for script {0}, startParam {1}, postOnRez {2}, engine {3}, stateSource {4}, script\n{5}",
// itemID, startParam, postOnRez, engine, stateSource, script);
2009-04-10 21:44:27 +00:00
if ( script . StartsWith ( "//MRM:" ) )
return ;
2008-09-25 17:26:32 +00:00
List < IScriptModule > engines = new List < IScriptModule > ( m_Scene . RequestModuleInterfaces < IScriptModule > ( ) ) ;
List < string > names = new List < string > ( ) ;
foreach ( IScriptModule m in engines )
names . Add ( m . ScriptEngineName ) ;
2008-09-22 02:11:40 +00:00
int lineEnd = script . IndexOf ( '\n' ) ;
2008-10-03 21:44:33 +00:00
if ( lineEnd > 1 )
2008-09-21 22:16:17 +00:00
{
2008-09-22 02:11:40 +00:00
string firstline = script . Substring ( 0 , lineEnd ) . Trim ( ) ;
int colon = firstline . IndexOf ( ':' ) ;
if ( firstline . Length > 2 & & firstline . Substring ( 0 , 2 ) = = "//" & & colon ! = - 1 )
2008-09-21 22:16:17 +00:00
{
2012-06-27 23:37:23 +00:00
string engineName = firstline . Substring ( 2 , colon - 2 ) ;
2008-09-25 17:26:32 +00:00
if ( names . Contains ( engineName ) )
{
engine = engineName ;
2012-06-27 23:37:23 +00:00
script = "//" + script . Substring ( colon + 1 ) ;
2008-09-25 17:26:32 +00:00
}
2008-09-26 17:59:36 +00:00
else
{
if ( engine = = ScriptEngineName )
{
2012-06-27 23:58:36 +00:00
// If we are falling back on XEngine as the default engine, then only complain to the user
2012-06-28 20:30:36 +00:00
// if a script language has been explicitly set and it's one that we recognize or there are
// no non-whitespace characters after the colon.
/ /
// If the script is
2012-06-27 23:58:36 +00:00
// explicitly not allowed or the script is not in LSL then the user will be informed by a later compiler message.
/ /
2012-06-28 20:30:36 +00:00
// If the colon ends the line then we'll risk the false positive as this is more likely
// to signal a real scriptengine line where the user wants to use the default compile language.
/ /
2012-06-27 23:58:36 +00:00
// This avoids the overwhelming number of false positives where we're in this code because
// there's a colon in a comment in the first line of a script for entirely
// unrelated reasons (e.g. vim settings).
/ /
// TODO: A better fix would be to deprecate simple : detection and look for some less likely
// string to begin the comment (like #! in unix shell scripts).
2012-06-28 20:30:36 +00:00
bool warnRunningInXEngine = false ;
string restOfFirstLine = firstline . Substring ( colon + 1 ) ;
2012-06-27 23:58:36 +00:00
// FIXME: These are hardcoded because they are currently hardcoded in Compiler.cs
2012-06-28 20:30:36 +00:00
if ( restOfFirstLine . StartsWith ( "c#" )
| | restOfFirstLine . StartsWith ( "vb" )
| | restOfFirstLine . StartsWith ( "lsl" )
| | restOfFirstLine . Length = = 0 )
warnRunningInXEngine = true ;
if ( warnRunningInXEngine )
2008-09-26 17:59:36 +00:00
{
2012-06-27 23:58:36 +00:00
SceneObjectPart part =
m_Scene . GetSceneObjectPart (
localID ) ;
2017-01-05 19:07:37 +00:00
2012-06-27 23:58:36 +00:00
TaskInventoryItem item =
part . Inventory . GetInventoryItem ( itemID ) ;
2017-01-05 19:07:37 +00:00
ScenePresence presence =
2012-06-27 23:58:36 +00:00
m_Scene . GetScenePresence (
item . OwnerID ) ;
2017-01-05 19:07:37 +00:00
2012-06-27 23:58:36 +00:00
if ( presence ! = null )
{
presence . ControllingClient . SendAgentAlertMessage (
"Selected engine unavailable. " +
"Running script on " +
ScriptEngineName ,
false ) ;
}
2008-09-26 17:59:36 +00:00
}
}
}
2008-09-21 22:16:17 +00:00
}
}
2008-09-25 17:54:44 +00:00
if ( engine ! = ScriptEngineName )
2008-09-21 21:47:00 +00:00
return ;
2009-01-19 17:11:57 +00:00
Object [ ] parms = new Object [ ] { localID , itemID , script , startParam , postOnRez , ( StateSource ) stateSource } ;
2008-06-28 16:08:12 +00:00
2017-01-07 00:25:59 +00:00
if ( stateSource = = ( int ) StateSource . ScriptedRez )
2008-06-28 16:08:12 +00:00
{
2010-07-19 00:10:57 +00:00
lock ( m_CompileDict )
{
2017-01-07 09:02:50 +00:00
// m_log.DebugFormat("[XENGINE]: Set compile dict for {0}", itemID);
2017-01-07 00:25:59 +00:00
m_CompileDict [ itemID ] = new ScriptCompileInfo ( ) ;
2010-07-19 00:10:57 +00:00
}
2010-06-30 20:25:39 +00:00
2008-11-13 19:00:46 +00:00
DoOnRezScript ( parms ) ;
2008-06-28 16:08:12 +00:00
}
2008-11-13 19:00:46 +00:00
else
2008-06-28 16:08:12 +00:00
{
2010-07-19 00:10:57 +00:00
lock ( m_CompileDict )
2017-01-07 00:25:59 +00:00
m_CompileDict [ itemID ] = new ScriptCompileInfo ( ) ;
2017-01-07 09:02:50 +00:00
// m_log.DebugFormat("[XENGINE]: Set compile dict for {0} delayed", itemID);
2014-12-09 23:59:19 +00:00
// This must occur after the m_CompileDict so that an existing compile thread cannot hit the check
// in DoOnRezScript() before m_CompileDict has been updated.
m_CompileQueue . Enqueue ( parms ) ;
2008-11-13 19:00:46 +00:00
2012-02-07 17:44:37 +00:00
// m_log.DebugFormat("[XEngine]: Added script {0} to compile queue", itemID);
2016-08-30 07:56:35 +00:00
// NOTE: Although we use a lockless queue, the lock here
// is required. It ensures that there are never two
// compile threads running, which, due to a race
// conndition, might otherwise happen
/ /
lock ( m_CompileQueue )
2009-10-29 13:42:40 +00:00
{
2016-08-30 07:56:35 +00:00
if ( m_CurrentCompile = = null )
m_CurrentCompile = m_ThreadPool . QueueWorkItem ( DoOnRezScriptQueue , null ) ;
2008-06-28 16:08:12 +00:00
}
}
}
public Object DoOnRezScriptQueue ( Object dummy )
2008-05-30 12:27:06 +00:00
{
2014-11-29 01:42:52 +00:00
try
2008-12-28 17:22:31 +00:00
{
2014-11-29 01:42:52 +00:00
if ( m_InitialStartup )
{
// This delay exists to stop mono problems where script compilation and startup would stop the sim
// working properly for the session.
System . Threading . Thread . Sleep ( m_StartDelay ) ;
2012-08-24 23:42:32 +00:00
2014-11-29 01:42:52 +00:00
m_log . InfoFormat ( "[XEngine]: Performing initial script startup on {0}" , m_Scene . Name ) ;
}
2008-12-28 17:22:31 +00:00
2014-11-29 01:42:52 +00:00
object [ ] o ;
2012-06-19 23:10:19 +00:00
2014-11-29 01:42:52 +00:00
int scriptsStarted = 0 ;
2012-06-19 23:10:19 +00:00
2014-11-29 01:42:52 +00:00
while ( m_CompileQueue . Dequeue ( out o ) )
2012-06-19 23:10:19 +00:00
{
2015-01-26 23:32:51 +00:00
try
2014-11-29 01:42:52 +00:00
{
2015-01-26 23:32:51 +00:00
if ( DoOnRezScript ( o ) )
{
scriptsStarted + + ;
2012-06-19 23:10:19 +00:00
2015-01-26 23:32:51 +00:00
if ( m_InitialStartup )
if ( scriptsStarted % 50 = = 0 )
m_log . InfoFormat (
"[XEngine]: Started {0} scripts in {1}" , scriptsStarted , m_Scene . Name ) ;
}
}
2017-05-25 05:05:02 +00:00
catch ( System . Threading . ThreadAbortException ) { }
2015-01-26 23:32:51 +00:00
catch ( Exception e )
{
m_log . Error (
string . Format (
2017-01-05 19:07:37 +00:00
"[XEngine]: Failure in DoOnRezScriptQueue() for item {0} in {1}. Continuing. Exception " ,
o [ 1 ] , m_Scene . Name ) ,
2015-01-26 23:32:51 +00:00
e ) ;
2014-11-29 01:42:52 +00:00
}
2012-06-19 23:10:19 +00:00
}
2009-11-03 20:23:50 +00:00
2014-11-29 01:42:52 +00:00
if ( m_InitialStartup )
m_log . InfoFormat (
"[XEngine]: Completed starting {0} scripts on {1}" , scriptsStarted , m_Scene . Name ) ;
2014-11-29 01:33:14 +00:00
2014-11-29 01:42:52 +00:00
}
catch ( Exception e )
{
2014-11-29 15:04:16 +00:00
m_log . Error (
string . Format ( "[XEngine]: Failure in DoOnRezScriptQueue() in {0}. Exception " , m_Scene . Name ) , e ) ;
2014-11-29 01:42:52 +00:00
}
finally
{
2017-01-05 19:07:37 +00:00
// FIXME: On failure we must trigger this even if the compile queue is not actually empty so that the
2014-11-29 01:42:52 +00:00
// RegionReadyModule is not forever waiting. This event really needs a different name.
m_Scene . EventManager . TriggerEmptyScriptCompileQueue ( m_ScriptFailCount ,
m_ScriptErrorMessage ) ;
m_ScriptFailCount = 0 ;
m_InitialStartup = false ;
// NOTE: Despite having a lockless queue, this lock is required
// to make sure there is never no compile thread while there
// are still scripts to compile. This could otherwise happen
// due to a race condition
/ /
lock ( m_CompileQueue )
2014-12-10 00:04:10 +00:00
{
2014-11-29 01:42:52 +00:00
m_CurrentCompile = null ;
2014-12-10 00:04:10 +00:00
2017-01-05 19:07:37 +00:00
// This is to avoid a situation where the m_CompileQueue while loop above could complete but
// OnRezScript() place a new script on the queue and check m_CurrentCompile = null before we hit
2014-12-10 00:04:10 +00:00
// this section.
if ( m_CompileQueue . Count > 0 )
m_CurrentCompile = m_ThreadPool . QueueWorkItem ( DoOnRezScriptQueue , null ) ;
}
2014-11-29 01:42:52 +00:00
}
2012-06-22 00:57:14 +00:00
2008-06-28 16:08:12 +00:00
return null ;
2008-05-30 12:27:06 +00:00
}
2009-10-29 13:42:40 +00:00
private bool DoOnRezScript ( object [ ] parms )
2008-05-30 12:27:06 +00:00
{
2009-10-29 13:42:40 +00:00
Object [ ] p = parms ;
2008-05-30 12:27:06 +00:00
uint localID = ( uint ) p [ 0 ] ;
2008-09-06 07:52:41 +00:00
UUID itemID = ( UUID ) p [ 1 ] ;
2009-01-19 17:11:57 +00:00
string script = ( string ) p [ 2 ] ;
2008-06-28 16:08:12 +00:00
int startParam = ( int ) p [ 3 ] ;
bool postOnRez = ( bool ) p [ 4 ] ;
2008-11-09 19:30:40 +00:00
StateSource stateSource = ( StateSource ) p [ 5 ] ;
2008-05-30 12:27:06 +00:00
2012-02-07 17:44:37 +00:00
// m_log.DebugFormat("[XEngine]: DoOnRezScript called for script {0}", itemID);
2010-09-12 17:43:49 +00:00
lock ( m_CompileDict )
2010-07-19 00:10:57 +00:00
{
if ( ! m_CompileDict . ContainsKey ( itemID ) )
return false ;
}
2010-06-09 02:51:43 +00:00
2008-05-30 12:27:06 +00:00
// Get the asset ID of the script, so we can check if we
// already have it.
2008-09-19 18:50:19 +00:00
// We must look for the part outside the m_Scripts lock because GetSceneObjectPart later triggers the
// m_parts lock on SOG. At the same time, a scene object that is being deleted will take the m_parts lock
2009-09-30 16:00:09 +00:00
// and then later on try to take the m_scripts lock in this class when it calls OnRemoveScript()
2008-05-30 12:27:06 +00:00
SceneObjectPart part = m_Scene . GetSceneObjectPart ( localID ) ;
2008-06-04 09:59:27 +00:00
if ( part = = null )
2008-09-19 18:50:19 +00:00
{
2011-09-12 21:44:14 +00:00
m_log . ErrorFormat ( "[Script]: SceneObjectPart with localID {0} unavailable. Script NOT started." , localID ) ;
2009-01-30 09:03:23 +00:00
m_ScriptErrorMessage + = "SceneObjectPart unavailable. Script NOT started.\n" ;
m_ScriptFailCount + + ;
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
2008-05-30 12:27:06 +00:00
return false ;
2009-09-30 16:00:09 +00:00
}
2008-05-30 12:27:06 +00:00
2008-11-21 21:16:42 +00:00
TaskInventoryItem item = part . Inventory . GetInventoryItem ( itemID ) ;
2008-06-04 09:59:27 +00:00
if ( item = = null )
2009-01-30 09:03:23 +00:00
{
m_ScriptErrorMessage + = "Can't find script inventory item.\n" ;
m_ScriptFailCount + + ;
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
2008-05-30 12:27:06 +00:00
return false ;
2009-01-30 09:03:23 +00:00
}
2008-06-04 09:59:27 +00:00
2014-12-03 20:48:17 +00:00
if ( DebugLevel > 0 )
m_log . DebugFormat (
"[XEngine]: Loading script {0}.{1}, item UUID {2}, prim UUID {3} @ {4}.{5}" ,
part . ParentGroup . RootPart . Name , item . Name , itemID , part . UUID ,
part . ParentGroup . RootPart . AbsolutePosition , part . ParentGroup . Scene . RegionInfo . RegionName ) ;
2008-05-30 12:27:06 +00:00
2012-08-17 21:30:01 +00:00
UUID assetID = item . AssetID ;
2008-05-30 12:27:06 +00:00
2008-09-25 17:26:32 +00:00
ScenePresence presence = m_Scene . GetScenePresence ( item . OwnerID ) ;
2014-07-10 23:03:02 +00:00
string assemblyPath = "" ;
2008-09-28 20:29:56 +00:00
2012-12-05 21:51:08 +00:00
Culture . SetCurrentCulture ( ) ;
2008-09-28 20:29:56 +00:00
2009-03-22 11:57:00 +00:00
Dictionary < KeyValuePair < int , int > , KeyValuePair < int , int > > linemap ;
2010-01-03 21:10:45 +00:00
lock ( m_ScriptErrors )
2008-05-30 12:27:06 +00:00
{
2009-12-22 09:24:01 +00:00
try
2009-02-05 16:12:51 +00:00
{
2012-02-07 17:44:37 +00:00
lock ( m_AddingAssemblies )
2009-12-22 09:24:01 +00:00
{
2014-07-10 23:03:02 +00:00
m_Compiler . PerformScriptCompile ( script , assetID . ToString ( ) , item . OwnerID , out assemblyPath , out linemap ) ;
2014-12-10 00:25:27 +00:00
// m_log.DebugFormat(
2017-01-05 19:07:37 +00:00
// "[XENGINE]: Found assembly path {0} onrez {1} in {2}",
2014-12-10 00:25:27 +00:00
// assemblyPath, item.ItemID, World.Name);
2014-07-10 23:03:02 +00:00
if ( ! m_AddingAssemblies . ContainsKey ( assemblyPath ) ) {
m_AddingAssemblies [ assemblyPath ] = 1 ;
2009-12-22 09:24:01 +00:00
} else {
2014-07-10 23:03:02 +00:00
m_AddingAssemblies [ assemblyPath ] + + ;
2009-12-22 09:24:01 +00:00
}
2009-02-05 16:12:51 +00:00
}
2009-02-23 02:43:51 +00:00
2009-12-22 09:24:01 +00:00
string [ ] warnings = m_Compiler . GetWarnings ( ) ;
2009-02-23 02:43:51 +00:00
2009-12-22 09:24:01 +00:00
if ( warnings ! = null & & warnings . Length ! = 0 )
2009-02-23 02:43:51 +00:00
{
2009-12-22 09:24:01 +00:00
foreach ( string warning in warnings )
2009-02-23 02:43:51 +00:00
{
2009-12-22 09:24:01 +00:00
if ( ! m_ScriptErrors . ContainsKey ( itemID ) )
m_ScriptErrors [ itemID ] = new ArrayList ( ) ;
m_ScriptErrors [ itemID ] . Add ( warning ) ;
// try
// {
// // DISPLAY WARNING INWORLD
// string text = "Warning:\n" + warning;
// if (text.Length > 1000)
// text = text.Substring(0, 1000);
// if (!ShowScriptSaveResponse(item.OwnerID,
// assetID, text, true))
// {
// if (presence != null && (!postOnRez))
// presence.ControllingClient.SendAgentAlertMessage("Script saved with warnings, check debug window!", false);
/ /
// World.SimChat(Utils.StringToBytes(text),
// ChatTypeEnum.DebugChannel, 2147483647,
// part.AbsolutePosition,
// part.Name, part.UUID, false);
// }
// }
// catch (Exception e2) // LEGIT: User Scripting
// {
// m_log.Error("[XEngine]: " +
// "Error displaying warning in-world: " +
// e2.ToString());
// m_log.Error("[XEngine]: " +
// "Warning:\r\n" +
// warning);
// }
2009-02-23 02:43:51 +00:00
}
}
}
2009-12-22 09:24:01 +00:00
catch ( Exception e )
2008-05-30 12:27:06 +00:00
{
2013-01-17 23:39:09 +00:00
// m_log.ErrorFormat(
2017-01-05 19:07:37 +00:00
// "[XEngine]: Exception when rezzing script with item ID {0}, {1}{2}",
2013-01-17 23:39:09 +00:00
// itemID, e.Message, e.StackTrace);
2012-02-07 17:44:37 +00:00
2009-12-22 09:24:01 +00:00
// try
// {
if ( ! m_ScriptErrors . ContainsKey ( itemID ) )
m_ScriptErrors [ itemID ] = new ArrayList ( ) ;
// DISPLAY ERROR INWORLD
// m_ScriptErrorMessage += "Failed to compile script in object: '" + part.ParentGroup.RootPart.Name + "' Script name: '" + item.Name + "' Error message: " + e.Message.ToString();
/ /
m_ScriptFailCount + + ;
m_ScriptErrors [ itemID ] . Add ( e . Message . ToString ( ) ) ;
// string text = "Error compiling script '" + item.Name + "':\n" + e.Message.ToString();
// if (text.Length > 1000)
// text = text.Substring(0, 1000);
// if (!ShowScriptSaveResponse(item.OwnerID,
// assetID, text, false))
// {
// if (presence != null && (!postOnRez))
// presence.ControllingClient.SendAgentAlertMessage("Script saved with errors, check debug window!", false);
// World.SimChat(Utils.StringToBytes(text),
// ChatTypeEnum.DebugChannel, 2147483647,
// part.AbsolutePosition,
// part.Name, part.UUID, false);
// }
// }
// catch (Exception e2) // LEGIT: User Scripting
// {
// m_log.Error("[XEngine]: "+
// "Error displaying error in-world: " +
// e2.ToString());
// m_log.Error("[XEngine]: " +
// "Errormessage: Error compiling script:\r\n" +
// e.Message.ToString());
// }
2008-05-30 12:27:06 +00:00
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
2009-12-22 09:24:01 +00:00
return false ;
}
2008-05-30 12:27:06 +00:00
}
2010-07-20 11:23:39 +00:00
ScriptInstance instance = null ;
2015-09-02 18:54:53 +00:00
lock ( m_Scripts )
2008-05-30 12:27:06 +00:00
{
2015-09-02 18:54:53 +00:00
// Create the object record
2020-07-27 15:13:09 +00:00
if ( ( ! m_Scripts . ContainsKey ( itemID ) ) | | ( m_Scripts [ itemID ] . AssetID ! = assetID ) )
2015-09-02 18:54:53 +00:00
{
2008-05-30 12:27:06 +00:00
2020-07-27 15:13:09 +00:00
bool attachDomains = m_AttachmentsDomainLoading & & part . ParentGroup . IsAttachmentCheckFull ( ) ;
2016-08-31 11:03:06 +00:00
UUID appDomain = part . ParentGroup . RootPart . UUID ;
2008-06-04 09:59:27 +00:00
2015-09-02 18:54:53 +00:00
if ( ! m_AppDomains . ContainsKey ( appDomain ) )
2008-05-30 12:27:06 +00:00
{
2015-09-02 18:54:53 +00:00
try
{
2016-08-30 07:56:35 +00:00
AppDomain sandbox ;
2020-07-27 15:13:09 +00:00
if ( m_AppDomainLoading | | attachDomains )
2016-08-30 07:56:35 +00:00
{
AppDomainSetup appSetup = new AppDomainSetup ( ) ;
appSetup . PrivateBinPath = Path . Combine (
2015-09-02 18:54:53 +00:00
m_ScriptEnginesPath ,
m_Scene . RegionInfo . RegionID . ToString ( ) ) ;
2008-05-30 12:27:06 +00:00
2016-08-30 07:56:35 +00:00
Evidence baseEvidence = AppDomain . CurrentDomain . Evidence ;
Evidence evidence = new Evidence ( baseEvidence ) ;
2008-05-30 12:27:06 +00:00
2015-09-02 18:54:53 +00:00
sandbox = AppDomain . CreateDomain (
m_Scene . RegionInfo . RegionID . ToString ( ) ,
evidence , appSetup ) ;
sandbox . AssemblyResolve + =
2012-04-18 19:01:41 +00:00
new ResolveEventHandler (
AssemblyResolver . OnAssemblyResolve ) ;
}
2009-10-26 14:50:25 +00:00
else
2012-04-18 19:01:41 +00:00
{
2015-09-02 18:54:53 +00:00
sandbox = AppDomain . CurrentDomain ;
2012-04-18 19:01:41 +00:00
}
2017-01-05 19:07:37 +00:00
2015-09-02 18:54:53 +00:00
//PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel();
//AllMembershipCondition sandboxMembershipCondition = new AllMembershipCondition();
//PermissionSet sandboxPermissionSet = sandboxPolicy.GetNamedPermissionSet("Internet");
//PolicyStatement sandboxPolicyStatement = new PolicyStatement(sandboxPermissionSet);
//CodeGroup sandboxCodeGroup = new UnionCodeGroup(sandboxMembershipCondition, sandboxPolicyStatement);
//sandboxPolicy.RootCodeGroup = sandboxCodeGroup;
//sandbox.SetAppDomainPolicy(sandboxPolicy);
2017-01-05 19:07:37 +00:00
2015-09-02 18:54:53 +00:00
m_AppDomains [ appDomain ] = sandbox ;
m_DomainScripts [ appDomain ] = new List < UUID > ( ) ;
2008-05-30 12:27:06 +00:00
}
2015-09-02 18:54:53 +00:00
catch ( Exception e )
2008-05-30 12:27:06 +00:00
{
2008-09-02 12:07:23 +00:00
m_log . ErrorFormat ( "[XEngine] Exception creating app domain:\n {0}" , e . ToString ( ) ) ;
2009-01-30 09:03:23 +00:00
m_ScriptErrorMessage + = "Exception creating app domain:\n" ;
m_ScriptFailCount + + ;
2017-01-05 19:07:37 +00:00
lock ( m_AddingAssemblies )
2009-02-05 16:12:51 +00:00
{
2014-07-10 23:03:02 +00:00
m_AddingAssemblies [ assemblyPath ] - - ;
2009-02-05 16:12:51 +00:00
}
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
2008-05-30 12:27:06 +00:00
return false ;
}
2015-09-02 18:54:53 +00:00
}
2020-07-27 15:13:09 +00:00
2015-09-02 18:54:53 +00:00
m_DomainScripts [ appDomain ] . Add ( itemID ) ;
2008-05-30 12:27:06 +00:00
2015-01-26 23:16:06 +00:00
IScript scriptObj = null ;
2015-01-27 18:58:11 +00:00
EventWaitHandle coopSleepHandle ;
2015-01-26 23:16:06 +00:00
bool coopTerminationForThisScript ;
// Set up assembly name to point to the appropriate scriptEngines directory
AssemblyName assemblyName = new AssemblyName ( Path . GetFileNameWithoutExtension ( assemblyPath ) ) ;
assemblyName . CodeBase = Path . GetDirectoryName ( assemblyPath ) ;
2014-07-10 23:03:02 +00:00
if ( m_coopTermination )
{
2015-01-26 23:16:06 +00:00
try
{
coopSleepHandle = new XEngineEventWaitHandle ( false , EventResetMode . AutoReset ) ;
2017-01-05 19:07:37 +00:00
scriptObj
2015-01-26 23:16:06 +00:00
= ( IScript ) m_AppDomains [ appDomain ] . CreateInstanceAndUnwrap (
assemblyName . FullName ,
"SecondLife.XEngineScript" ,
false ,
BindingFlags . Default ,
null ,
2017-01-05 19:07:37 +00:00
new object [ ] { coopSleepHandle } ,
2015-01-26 23:16:06 +00:00
null ,
null ) ;
coopTerminationForThisScript = true ;
}
2015-01-27 18:58:11 +00:00
catch ( TypeLoadException )
2015-01-26 23:16:06 +00:00
{
coopSleepHandle = null ;
2014-07-10 23:03:02 +00:00
2015-01-26 23:16:06 +00:00
try
{
2017-01-05 19:07:37 +00:00
scriptObj
2015-01-26 23:16:06 +00:00
= ( IScript ) m_AppDomains [ appDomain ] . CreateInstanceAndUnwrap (
assemblyName . FullName ,
"SecondLife.Script" ,
false ,
BindingFlags . Default ,
null ,
null ,
null ,
null ) ;
}
catch ( Exception e2 )
{
m_log . Error (
string . Format (
2017-01-05 19:07:37 +00:00
"[XENGINE]: Could not load previous SecondLife.Script from assembly {0} in {1}. Not starting. Exception " ,
2015-01-27 18:58:11 +00:00
assemblyName . FullName , World . Name ) ,
2015-01-26 23:16:06 +00:00
e2 ) ;
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
2015-01-27 18:58:11 +00:00
return false ;
2015-01-26 23:16:06 +00:00
}
coopTerminationForThisScript = false ;
}
2014-07-10 23:03:02 +00:00
}
else
{
2015-01-26 23:16:06 +00:00
try
{
2017-01-05 19:07:37 +00:00
scriptObj
2015-01-26 23:16:06 +00:00
= ( IScript ) m_AppDomains [ appDomain ] . CreateInstanceAndUnwrap (
assemblyName . FullName ,
"SecondLife.Script" ,
false ,
BindingFlags . Default ,
null ,
null ,
null ,
null ) ;
2015-01-27 18:58:11 +00:00
coopSleepHandle = null ;
2015-01-26 23:16:06 +00:00
coopTerminationForThisScript = false ;
}
2015-01-27 18:58:11 +00:00
catch ( TypeLoadException )
2015-01-26 23:16:06 +00:00
{
coopSleepHandle = new XEngineEventWaitHandle ( false , EventResetMode . AutoReset ) ;
try
{
2017-01-05 19:07:37 +00:00
scriptObj
2015-01-26 23:16:06 +00:00
= ( IScript ) m_AppDomains [ appDomain ] . CreateInstanceAndUnwrap (
assemblyName . FullName ,
"SecondLife.XEngineScript" ,
false ,
BindingFlags . Default ,
null ,
2015-01-27 18:58:11 +00:00
new object [ ] { coopSleepHandle } ,
2015-01-26 23:16:06 +00:00
null ,
null ) ;
}
catch ( Exception e2 )
{
m_log . Error (
string . Format (
2017-01-05 19:07:37 +00:00
"[XENGINE]: Could not load previous SecondLife.XEngineScript from assembly {0} in {1}. Not starting. Exception " ,
2015-01-26 23:16:06 +00:00
assemblyName . FullName , World . Name ) ,
e2 ) ;
2014-07-10 23:03:02 +00:00
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
2015-01-27 18:58:11 +00:00
return false ;
2015-01-26 23:16:06 +00:00
}
coopTerminationForThisScript = true ;
}
2014-07-10 23:03:02 +00:00
}
2017-09-04 17:28:31 +00:00
if ( m_coopTermination ! = coopTerminationForThisScript & & ! HaveNotifiedLogOfScriptStopMismatch )
2014-12-03 21:08:43 +00:00
{
2015-01-27 18:58:11 +00:00
// Notify the log that there is at least one script compile that doesn't match the
// ScriptStopStrategy. Operator has to manually delete old DLLs - we can't do this on Windows
// once the assembly has been loaded evne if the instantiation of a class was unsuccessful.
m_log . WarnFormat (
"[XEngine]: At least one existing compiled script DLL in {0} has {1} as ScriptStopStrategy whereas config setting is {2}."
+ "\nContinuing with script compiled strategy but to remove this message please set [XEngine] DeleteScriptsOnStartup = true for one simulator session to remove old script DLLs (script state will not be lost)." ,
2015-01-29 18:28:17 +00:00
World . Name , coopTerminationForThisScript ? "co-op" : "abort" , m_coopTermination ? "co-op" : "abort" ) ;
2015-01-27 18:58:11 +00:00
2017-09-04 17:28:31 +00:00
HaveNotifiedLogOfScriptStopMismatch = true ;
2014-12-03 21:08:43 +00:00
}
2014-07-10 23:03:02 +00:00
2009-01-28 09:52:09 +00:00
instance = new ScriptInstance ( this , part ,
2013-01-15 21:13:22 +00:00
item ,
startParam , postOnRez ,
m_MaxScriptQueue ) ;
2016-08-30 07:56:35 +00:00
if ( ! instance . Load ( scriptObj , coopSleepHandle , assemblyPath ,
2015-01-26 23:16:06 +00:00
Path . Combine ( ScriptEnginePath , World . RegionInfo . RegionID . ToString ( ) ) , stateSource , coopTerminationForThisScript ) )
2017-01-07 00:25:59 +00:00
{
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
return false ;
}
2012-06-19 23:10:19 +00:00
2015-09-02 18:54:53 +00:00
// if (DebugLevel >= 1)
// m_log.DebugFormat(
// "[XEngine] Loaded script {0}.{1}, item UUID {2}, prim UUID {3} @ {4}.{5}",
// part.ParentGroup.RootPart.Name, item.Name, itemID, part.UUID,
// part.ParentGroup.RootPart.AbsolutePosition, part.ParentGroup.Scene.RegionInfo.RegionName);
2008-05-30 12:27:06 +00:00
2015-09-02 18:54:53 +00:00
if ( presence ! = null )
2009-10-17 21:36:44 +00:00
{
2015-09-02 18:54:53 +00:00
ShowScriptSaveResponse ( item . OwnerID ,
assetID , "Compile successful" , true ) ;
2009-10-17 21:36:44 +00:00
}
2015-09-02 18:54:53 +00:00
instance . AppDomain = appDomain ;
instance . LineMap = linemap ;
2008-05-30 12:27:06 +00:00
2015-09-02 18:54:53 +00:00
m_Scripts [ itemID ] = instance ;
2008-05-30 12:27:06 +00:00
}
2009-11-27 17:29:03 +00:00
}
2015-09-02 18:54:53 +00:00
2010-07-20 11:23:39 +00:00
lock ( m_PrimObjects )
{
if ( ! m_PrimObjects . ContainsKey ( localID ) )
m_PrimObjects [ localID ] = new List < UUID > ( ) ;
2009-01-28 09:52:09 +00:00
2010-07-20 11:23:39 +00:00
if ( ! m_PrimObjects [ localID ] . Contains ( itemID ) )
m_PrimObjects [ localID ] . Add ( itemID ) ;
}
2010-07-19 22:20:34 +00:00
2017-01-05 19:07:37 +00:00
lock ( m_AddingAssemblies )
2010-07-20 11:23:39 +00:00
{
2016-08-31 08:15:08 +00:00
if ( ! m_Assemblies . ContainsKey ( assetID ) )
m_Assemblies [ assetID ] = assemblyPath ;
2014-07-10 23:03:02 +00:00
m_AddingAssemblies [ assemblyPath ] - - ;
2010-07-19 23:22:29 +00:00
}
2010-07-20 11:23:39 +00:00
2017-01-05 19:07:37 +00:00
if ( instance ! = null )
2017-01-07 00:25:59 +00:00
{
2010-07-20 11:23:39 +00:00
instance . Init ( ) ;
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
{
foreach ( EventParams pp in m_CompileDict [ itemID ] . eventList )
instance . PostEvent ( pp ) ;
}
}
lock ( m_CompileDict )
m_CompileDict . Remove ( itemID ) ;
2010-07-20 11:23:39 +00:00
2012-07-17 13:00:42 +00:00
bool runIt ;
if ( m_runFlags . TryGetValue ( itemID , out runIt ) )
{
if ( ! runIt )
StopScript ( itemID ) ;
m_runFlags . Remove ( itemID ) ;
}
2008-05-30 12:27:06 +00:00
return true ;
}
2008-09-06 07:52:41 +00:00
public void OnRemoveScript ( uint localID , UUID itemID )
2008-05-30 12:27:06 +00:00
{
2010-06-09 02:51:43 +00:00
// If it's not yet been compiled, make sure we don't try
2010-07-19 00:10:57 +00:00
lock ( m_CompileDict )
{
if ( m_CompileDict . ContainsKey ( itemID ) )
m_CompileDict . Remove ( itemID ) ;
}
2010-06-09 02:51:43 +00:00
2015-09-02 18:54:53 +00:00
IScriptInstance instance = null ;
2008-05-30 12:27:06 +00:00
2015-09-02 18:54:53 +00:00
lock ( m_Scripts )
{
// Do we even have it?
if ( ! m_Scripts . ContainsKey ( itemID ) )
return ;
2012-03-15 00:06:52 +00:00
2015-09-02 18:54:53 +00:00
instance = m_Scripts [ itemID ] ;
m_Scripts . Remove ( itemID ) ;
}
2008-05-30 12:27:06 +00:00
2015-09-02 18:54:53 +00:00
instance . Stop ( m_WaitForEventCompletionOnScriptStop , true ) ;
2012-03-15 00:20:47 +00:00
2010-07-19 20:59:11 +00:00
lock ( m_PrimObjects )
{
// Remove the script from it's prim
if ( m_PrimObjects . ContainsKey ( localID ) )
2008-05-30 12:27:06 +00:00
{
2010-07-19 20:59:11 +00:00
// Remove inventory item record
if ( m_PrimObjects [ localID ] . Contains ( itemID ) )
m_PrimObjects [ localID ] . Remove ( itemID ) ;
2008-12-17 15:30:26 +00:00
2010-07-19 20:59:11 +00:00
// If there are no more scripts, remove prim
if ( m_PrimObjects [ localID ] . Count = = 0 )
m_PrimObjects . Remove ( localID ) ;
2008-05-30 12:27:06 +00:00
}
2010-07-19 20:59:11 +00:00
}
2008-05-30 12:27:06 +00:00
2015-01-16 22:44:54 +00:00
if ( instance . StatePersistedHere )
2017-01-05 19:07:37 +00:00
instance . RemoveState ( ) ;
2015-01-16 22:44:54 +00:00
2010-07-19 20:59:11 +00:00
instance . DestroyScriptInstance ( ) ;
2008-05-30 12:27:06 +00:00
2015-09-02 18:54:53 +00:00
m_DomainScripts [ instance . AppDomain ] . Remove ( instance . ItemID ) ;
if ( m_DomainScripts [ instance . AppDomain ] . Count = = 0 )
2010-07-19 20:59:11 +00:00
{
2015-09-02 18:54:53 +00:00
m_DomainScripts . Remove ( instance . AppDomain ) ;
UnloadAppDomain ( instance . AppDomain ) ;
2010-07-19 20:59:11 +00:00
}
2008-05-30 12:27:06 +00:00
2020-07-27 15:13:09 +00:00
OnObjectRemoved ? . Invoke ( instance . ObjectID ) ;
OnScriptRemoved ? . Invoke ( itemID ) ;
2008-05-30 12:27:06 +00:00
}
2008-09-06 07:52:41 +00:00
public void OnScriptReset ( uint localID , UUID itemID )
2008-05-30 12:27:06 +00:00
{
ResetScript ( itemID ) ;
}
2008-09-06 07:52:41 +00:00
public void OnStartScript ( uint localID , UUID itemID )
2008-06-28 16:08:12 +00:00
{
StartScript ( itemID ) ;
}
2008-09-06 07:52:41 +00:00
public void OnStopScript ( uint localID , UUID itemID )
2008-06-28 16:08:12 +00:00
{
StopScript ( itemID ) ;
}
2008-05-30 12:27:06 +00:00
private void CleanAssemblies ( )
{
2008-09-06 07:52:41 +00:00
List < UUID > assetIDList = new List < UUID > ( m_Assemblies . Keys ) ;
2008-05-30 12:27:06 +00:00
2008-08-27 22:38:36 +00:00
foreach ( IScriptInstance i in m_Scripts . Values )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
if ( assetIDList . Contains ( i . AssetID ) )
2008-05-30 12:27:06 +00:00
assetIDList . Remove ( i . AssetID ) ;
}
2009-02-05 16:12:51 +00:00
lock ( m_AddingAssemblies )
2008-05-30 12:27:06 +00:00
{
2009-02-05 16:12:51 +00:00
foreach ( UUID assetID in assetIDList )
2008-05-30 12:27:06 +00:00
{
2009-02-05 16:12:51 +00:00
// Do not remove assembly files if another instance of the script
// is currently initialising
if ( ! m_AddingAssemblies . ContainsKey ( m_Assemblies [ assetID ] )
2017-01-05 19:07:37 +00:00
| | m_AddingAssemblies [ m_Assemblies [ assetID ] ] = = 0 )
2009-02-05 16:12:51 +00:00
{
// m_log.DebugFormat("[XEngine] Removing unreferenced assembly {0}", m_Assemblies[assetID]);
try
{
if ( File . Exists ( m_Assemblies [ assetID ] ) )
File . Delete ( m_Assemblies [ assetID ] ) ;
2017-01-05 19:07:37 +00:00
2009-02-18 22:32:25 +00:00
if ( File . Exists ( m_Assemblies [ assetID ] + ".text" ) )
File . Delete ( m_Assemblies [ assetID ] + ".text" ) ;
2017-01-05 19:07:37 +00:00
2009-02-05 16:12:51 +00:00
if ( File . Exists ( m_Assemblies [ assetID ] + ".mdb" ) )
File . Delete ( m_Assemblies [ assetID ] + ".mdb" ) ;
2017-01-05 19:07:37 +00:00
2009-03-26 15:06:20 +00:00
if ( File . Exists ( m_Assemblies [ assetID ] + ".map" ) )
File . Delete ( m_Assemblies [ assetID ] + ".map" ) ;
2009-02-05 16:12:51 +00:00
}
catch ( Exception )
{
}
m_Assemblies . Remove ( assetID ) ;
}
2008-05-30 12:27:06 +00:00
}
}
}
2008-09-06 07:52:41 +00:00
private void UnloadAppDomain ( UUID id )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
if ( m_AppDomains . ContainsKey ( id ) )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
AppDomain domain = m_AppDomains [ id ] ;
2008-05-30 12:27:06 +00:00
m_AppDomains . Remove ( id ) ;
2009-10-26 14:50:25 +00:00
if ( domain ! = AppDomain . CurrentDomain )
AppDomain . Unload ( domain ) ;
2008-05-30 12:27:06 +00:00
domain = null ;
2009-10-26 14:50:25 +00:00
// m_log.DebugFormat("[XEngine] Unloaded app domain {0}", id.ToString());
2008-05-30 12:27:06 +00:00
}
}
/ /
// Start processing
/ /
2008-09-21 02:52:12 +00:00
private void SetupEngine ( int minThreads , int maxThreads ,
2008-06-04 09:59:27 +00:00
int idleTimeout , ThreadPriority threadPriority ,
int maxScriptQueue , int stackSize )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
m_MaxScriptQueue = maxScriptQueue ;
2008-05-30 12:27:06 +00:00
STPStartInfo startInfo = new STPStartInfo ( ) ;
2013-01-04 06:43:05 +00:00
startInfo . ThreadPoolName = "XEngine" ;
startInfo . IdleTimeout = idleTimeout * 1000 ; // convert to seconds as stated in .ini
2008-05-30 12:27:06 +00:00
startInfo . MaxWorkerThreads = maxThreads ;
startInfo . MinWorkerThreads = minThreads ;
2010-09-25 20:22:18 +00:00
startInfo . ThreadPriority = threadPriority ; ;
2013-05-01 18:01:43 +00:00
startInfo . MaxStackSize = stackSize ;
2008-05-30 12:27:06 +00:00
startInfo . StartSuspended = true ;
2008-06-04 09:59:27 +00:00
2008-05-30 12:27:06 +00:00
m_ThreadPool = new SmartThreadPool ( startInfo ) ;
}
/ /
// Used by script instances to queue event handler jobs
/ /
2008-08-28 00:37:22 +00:00
public IScriptWorkItem QueueEventHandler ( object parms )
2008-05-30 12:27:06 +00:00
{
2008-09-02 12:07:23 +00:00
return new XWorkItem ( m_ThreadPool . QueueWorkItem (
2008-09-09 01:26:48 +00:00
new WorkItemCallback ( this . ProcessEventHandler ) ,
2008-09-02 12:07:23 +00:00
parms ) ) ;
2009-09-30 16:00:09 +00:00
}
2017-01-05 19:07:37 +00:00
2008-09-16 16:37:16 +00:00
/// <summary>
/// Process a previously posted script event.
/// </summary>
/// <param name="parms"></param>
/// <returns></returns>
2008-05-30 12:27:06 +00:00
private object ProcessEventHandler ( object parms )
{
2012-12-05 21:37:41 +00:00
Culture . SetCurrentCulture ( ) ;
2008-06-28 17:47:06 +00:00
2009-01-19 17:11:57 +00:00
IScriptInstance instance = ( ScriptInstance ) parms ;
2017-01-05 19:07:37 +00:00
2014-12-10 00:25:27 +00:00
// m_log.DebugFormat("[XEngine]: Processing event for {0}", instance);
2008-06-04 09:59:27 +00:00
2008-05-30 12:27:06 +00:00
return instance . EventProcessor ( ) ;
}
2008-09-16 16:37:16 +00:00
/// <summary>
/// Post event to an entire prim
/// </summary>
/// <param name="localID"></param>
/// <param name="p"></param>
/// <returns></returns>
2008-06-24 21:09:49 +00:00
public bool PostObjectEvent ( uint localID , EventParams p )
2008-05-30 12:27:06 +00:00
{
bool result = false ;
2010-07-20 11:23:39 +00:00
List < UUID > uuids = null ;
2008-12-17 15:30:26 +00:00
lock ( m_PrimObjects )
2008-05-30 12:27:06 +00:00
{
2008-12-17 15:30:26 +00:00
if ( ! m_PrimObjects . ContainsKey ( localID ) )
return false ;
2010-07-20 11:23:39 +00:00
uuids = m_PrimObjects [ localID ] ;
2012-06-22 01:23:25 +00:00
foreach ( UUID itemID in uuids )
2010-07-20 11:23:39 +00:00
{
2012-06-22 01:23:25 +00:00
IScriptInstance instance = null ;
try
{
if ( m_Scripts . ContainsKey ( itemID ) )
instance = m_Scripts [ itemID ] ;
}
catch { /* ignore race conditions */ }
2017-01-05 19:07:37 +00:00
2012-06-22 01:23:25 +00:00
if ( instance ! = null )
{
instance . PostEvent ( p ) ;
result = true ;
}
2017-01-07 00:25:59 +00:00
else
{
lock ( m_CompileDict )
{
if ( m_CompileDict . ContainsKey ( itemID ) )
{
m_CompileDict [ itemID ] . eventList . Add ( p ) ;
result = true ;
}
}
}
2008-05-30 12:27:06 +00:00
}
}
2017-01-05 19:07:37 +00:00
2008-05-30 12:27:06 +00:00
return result ;
}
2019-11-11 12:51:09 +00:00
public void CancelScriptEvent ( UUID itemID , string eventName )
{
}
2008-09-16 16:37:16 +00:00
/// <summary>
/// Post an event to a single script
/// </summary>
/// <param name="itemID"></param>
/// <param name="p"></param>
/// <returns></returns>
2008-09-06 07:52:41 +00:00
public bool PostScriptEvent ( UUID itemID , EventParams p )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
if ( m_Scripts . ContainsKey ( itemID ) )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = m_Scripts [ itemID ] ;
2008-06-04 09:59:27 +00:00
if ( instance ! = null )
2008-05-30 12:27:06 +00:00
instance . PostEvent ( p ) ;
return true ;
}
2017-01-07 00:25:59 +00:00
lock ( m_CompileDict )
{
if ( m_CompileDict . ContainsKey ( itemID ) )
{
m_CompileDict [ itemID ] . eventList . Add ( p ) ;
return true ;
}
}
2008-05-30 12:27:06 +00:00
return false ;
}
2009-04-10 21:08:33 +00:00
public bool PostScriptEvent ( UUID itemID , string name , Object [ ] p )
{
2009-04-12 02:42:05 +00:00
Object [ ] lsl_p = new Object [ p . Length ] ;
for ( int i = 0 ; i < p . Length ; i + + )
{
if ( p [ i ] is int )
lsl_p [ i ] = new LSL_Types . LSLInteger ( ( int ) p [ i ] ) ;
else if ( p [ i ] is string )
lsl_p [ i ] = new LSL_Types . LSLString ( ( string ) p [ i ] ) ;
else if ( p [ i ] is Vector3 )
2012-08-18 00:17:01 +00:00
lsl_p [ i ] = new LSL_Types . Vector3 ( ( Vector3 ) p [ i ] ) ;
2009-04-12 02:42:05 +00:00
else if ( p [ i ] is Quaternion )
2012-08-18 14:10:44 +00:00
lsl_p [ i ] = new LSL_Types . Quaternion ( ( Quaternion ) p [ i ] ) ;
2009-04-12 02:42:05 +00:00
else if ( p [ i ] is float )
lsl_p [ i ] = new LSL_Types . LSLFloat ( ( float ) p [ i ] ) ;
else
lsl_p [ i ] = p [ i ] ;
}
return PostScriptEvent ( itemID , new EventParams ( name , lsl_p , new DetectParams [ 0 ] ) ) ;
2009-04-10 21:08:33 +00:00
}
2009-04-11 16:51:27 +00:00
public bool PostObjectEvent ( UUID itemID , string name , Object [ ] p )
{
SceneObjectPart part = m_Scene . GetSceneObjectPart ( itemID ) ;
if ( part = = null )
return false ;
2009-04-12 02:42:05 +00:00
Object [ ] lsl_p = new Object [ p . Length ] ;
for ( int i = 0 ; i < p . Length ; i + + )
{
if ( p [ i ] is int )
lsl_p [ i ] = new LSL_Types . LSLInteger ( ( int ) p [ i ] ) ;
else if ( p [ i ] is string )
lsl_p [ i ] = new LSL_Types . LSLString ( ( string ) p [ i ] ) ;
else if ( p [ i ] is Vector3 )
2012-08-18 00:17:01 +00:00
lsl_p [ i ] = new LSL_Types . Vector3 ( ( Vector3 ) p [ i ] ) ;
2009-04-12 02:42:05 +00:00
else if ( p [ i ] is Quaternion )
2012-08-18 14:10:44 +00:00
lsl_p [ i ] = new LSL_Types . Quaternion ( ( Quaternion ) p [ i ] ) ;
2009-04-12 02:42:05 +00:00
else if ( p [ i ] is float )
lsl_p [ i ] = new LSL_Types . LSLFloat ( ( float ) p [ i ] ) ;
else
lsl_p [ i ] = p [ i ] ;
}
return PostObjectEvent ( part . LocalId , new EventParams ( name , lsl_p , new DetectParams [ 0 ] ) ) ;
2009-04-11 16:51:27 +00:00
}
2008-05-30 12:27:06 +00:00
public Assembly OnAssemblyResolve ( object sender ,
2008-06-04 09:59:27 +00:00
ResolveEventArgs args )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
if ( ! ( sender is System . AppDomain ) )
2008-05-30 12:27:06 +00:00
return null ;
2010-09-25 20:22:18 +00:00
string [ ] pathList = new string [ ] { "bin" , m_ScriptEnginesPath ,
Path . Combine ( m_ScriptEnginesPath ,
2008-06-04 09:59:27 +00:00
m_Scene . RegionInfo . RegionID . ToString ( ) ) } ;
2008-09-09 01:26:48 +00:00
2008-05-30 12:27:06 +00:00
string assemblyName = args . Name ;
2008-06-04 09:59:27 +00:00
if ( assemblyName . IndexOf ( "," ) ! = - 1 )
assemblyName = args . Name . Substring ( 0 , args . Name . IndexOf ( "," ) ) ;
2008-05-30 12:27:06 +00:00
foreach ( string s in pathList )
{
2008-06-04 09:59:27 +00:00
string path = Path . Combine ( Directory . GetCurrentDirectory ( ) ,
2009-01-19 17:11:57 +00:00
Path . Combine ( s , assemblyName ) ) + ".dll" ;
2008-05-30 12:27:06 +00:00
2012-02-07 17:44:37 +00:00
// Console.WriteLine("[XEngine]: Trying to resolve {0}", path);
2008-06-04 09:59:27 +00:00
if ( File . Exists ( path ) )
2008-05-30 12:27:06 +00:00
return Assembly . LoadFrom ( path ) ;
}
return null ;
}
2008-09-06 07:52:41 +00:00
private IScriptInstance GetInstance ( UUID itemID )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance ;
2015-09-02 18:54:53 +00:00
lock ( m_Scripts )
2008-05-30 12:27:06 +00:00
{
2015-09-02 18:54:53 +00:00
if ( ! m_Scripts . ContainsKey ( itemID ) )
return null ;
instance = m_Scripts [ itemID ] ;
2008-05-30 12:27:06 +00:00
}
return instance ;
}
2016-11-13 19:25:32 +00:00
public void SetScriptState ( UUID itemID , bool running , bool self )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2008-06-04 09:59:27 +00:00
if ( instance ! = null )
2008-05-30 12:27:06 +00:00
{
2008-06-04 09:59:27 +00:00
if ( running )
2016-11-13 19:25:32 +00:00
instance . Start ( ) ;
2008-05-30 12:27:06 +00:00
else
2016-11-13 19:25:32 +00:00
{
if ( self )
{
instance . Running = false ;
throw new EventAbortException ( ) ;
}
else
instance . Stop ( 100 ) ;
}
2008-05-30 12:27:06 +00:00
}
}
2008-09-06 07:52:41 +00:00
public bool GetScriptState ( UUID itemID )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2013-12-06 13:12:35 +00:00
return instance ! = null & & instance . Running ;
2008-05-30 12:27:06 +00:00
}
2008-09-06 07:52:41 +00:00
public void ApiResetScript ( UUID itemID )
2008-07-02 13:31:39 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2008-07-02 13:31:39 +00:00
if ( instance ! = null )
instance . ApiResetScript ( ) ;
2015-04-20 18:55:00 +00:00
// Send the new number of threads that are in use by the thread
// pool, I believe that by adding them to the locations where the
// script is changing states that I will catch all changes to the
// thread pool
m_Scene . setThreadCount ( m_ThreadPool . InUseThreads ) ;
2008-07-02 13:31:39 +00:00
}
2008-09-06 07:52:41 +00:00
public void ResetScript ( UUID itemID )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2008-06-04 09:59:27 +00:00
if ( instance ! = null )
2012-12-17 21:37:02 +00:00
instance . ResetScript ( m_WaitForEventCompletionOnScriptStop ) ;
2015-04-20 18:55:00 +00:00
// Send the new number of threads that are in use by the thread
// pool, I believe that by adding them to the locations where the
// script is changing states that I will catch all changes to the
// thread pool
m_Scene . setThreadCount ( m_ThreadPool . InUseThreads ) ;
2008-05-30 12:27:06 +00:00
}
2008-09-06 07:52:41 +00:00
public void StartScript ( UUID itemID )
2008-06-28 16:08:12 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2008-06-28 16:08:12 +00:00
if ( instance ! = null )
instance . Start ( ) ;
2012-07-17 13:00:42 +00:00
else
m_runFlags . AddOrUpdate ( itemID , true , 240 ) ;
2015-04-20 18:55:00 +00:00
// Send the new number of threads that are in use by the thread
// pool, I believe that by adding them to the locations where the
// script is changing states that I will catch all changes to the
// thread pool
m_Scene . setThreadCount ( m_ThreadPool . InUseThreads ) ;
2008-06-28 16:08:12 +00:00
}
2008-09-06 07:52:41 +00:00
public void StopScript ( UUID itemID )
2008-06-28 16:08:12 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2012-12-13 21:02:55 +00:00
2008-06-28 16:08:12 +00:00
if ( instance ! = null )
2013-01-16 02:07:43 +00:00
{
2015-08-07 13:35:32 +00:00
lock ( instance . EventQueue )
instance . StayStopped = true ; // the script was stopped explicitly
2012-12-13 21:02:55 +00:00
instance . Stop ( m_WaitForEventCompletionOnScriptStop ) ;
2013-01-16 02:07:43 +00:00
}
2012-07-17 13:00:42 +00:00
else
2013-01-16 02:07:43 +00:00
{
// m_log.DebugFormat("[XENGINE]: Could not find script with ID {0} to stop in {1}", itemID, World.Name);
2012-07-17 13:00:42 +00:00
m_runFlags . AddOrUpdate ( itemID , false , 240 ) ;
2013-01-16 02:07:43 +00:00
}
2015-04-20 18:55:00 +00:00
// Send the new number of threads that are in use by the thread
// pool, I believe that by adding them to the locations where the
// script is changing states that I will catch all changes to the
// thread pool
m_Scene . setThreadCount ( m_ThreadPool . InUseThreads ) ;
2008-06-28 16:08:12 +00:00
}
2008-09-06 07:52:41 +00:00
public DetectParams GetDetectParams ( UUID itemID , int idx )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2013-12-06 13:12:35 +00:00
return instance ! = null ? instance . GetDetectParams ( idx ) : null ;
2008-05-30 12:27:06 +00:00
}
2008-12-21 19:04:06 +00:00
public void SetMinEventDelay ( UUID itemID , double delay )
{
IScriptInstance instance = GetInstance ( itemID ) ;
if ( instance ! = null )
instance . MinEventDelay = delay ;
}
2008-09-06 07:52:41 +00:00
public UUID GetDetectID ( UUID itemID , int idx )
2008-05-30 12:27:06 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2013-12-06 13:12:35 +00:00
return instance ! = null ? instance . GetDetectID ( idx ) : UUID . Zero ;
2008-05-30 12:27:06 +00:00
}
2008-06-07 22:37:48 +00:00
2009-12-22 15:45:56 +00:00
public void SetState ( UUID itemID , string newState )
{
IScriptInstance instance = GetInstance ( itemID ) ;
if ( instance = = null )
return ;
instance . SetState ( newState ) ;
}
2008-09-06 07:52:41 +00:00
public int GetStartParameter ( UUID itemID )
2008-06-28 16:08:12 +00:00
{
2008-08-27 22:38:36 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2013-12-06 13:12:35 +00:00
return instance = = null ? 0 : instance . StartParam ;
2008-06-28 16:08:12 +00:00
}
2008-09-07 22:01:25 +00:00
public void OnShutdown ( )
{
2011-11-17 21:03:08 +00:00
m_SimulatorShuttingDown = true ;
2008-09-07 22:01:25 +00:00
List < IScriptInstance > instances = new List < IScriptInstance > ( ) ;
2015-09-02 18:54:53 +00:00
lock ( m_Scripts )
{
foreach ( IScriptInstance instance in m_Scripts . Values )
2008-09-07 22:01:25 +00:00
instances . Add ( instance ) ;
2015-09-02 18:54:53 +00:00
}
2008-09-07 22:01:25 +00:00
foreach ( IScriptInstance i in instances )
{
2008-09-08 02:40:20 +00:00
// Stop the script, even forcibly if needed. Then flag
// it as shutting down and restore the previous run state
// for serialization, so the scripts don't come back
// dead after region restart
/ /
bool prevRunning = i . Running ;
2008-09-07 22:01:25 +00:00
i . Stop ( 50 ) ;
2008-09-08 02:40:20 +00:00
i . ShuttingDown = true ;
i . Running = prevRunning ;
2008-09-07 22:01:25 +00:00
}
2009-01-19 17:11:57 +00:00
DoBackup ( new Object [ ] { 0 } ) ;
2008-09-07 22:01:25 +00:00
}
2008-09-13 14:40:42 +00:00
public IScriptApi GetApi ( UUID itemID , string name )
{
IScriptInstance instance = GetInstance ( itemID ) ;
2013-12-06 13:12:35 +00:00
return instance = = null ? null : instance . GetApi ( name ) ;
2008-09-13 14:40:42 +00:00
}
2008-09-25 05:13:44 +00:00
public void OnGetScriptRunning ( IClientAPI controllingClient , UUID objectID , UUID itemID )
{
IScriptInstance instance = GetInstance ( itemID ) ;
if ( instance = = null )
return ;
2008-10-16 21:46:03 +00:00
IEventQueue eq = World . RequestModuleInterface < IEventQueue > ( ) ;
if ( eq = = null )
{
controllingClient . SendScriptRunningReply ( objectID , itemID ,
GetScriptState ( itemID ) ) ;
}
else
{
2019-01-25 21:21:23 +00:00
eq . ScriptRunningEvent ( objectID , itemID , GetScriptState ( itemID ) , controllingClient . AgentId ) ;
2008-10-16 21:46:03 +00:00
}
2008-09-25 05:13:44 +00:00
}
2008-11-08 02:24:34 +00:00
public string GetXMLState ( UUID itemID )
{
2012-07-11 20:43:35 +00:00
// m_log.DebugFormat("[XEngine]: Getting XML state for script instance {0}", itemID);
2011-08-24 19:49:23 +00:00
2008-11-08 02:24:34 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
if ( instance = = null )
2011-08-24 19:49:23 +00:00
{
2012-07-11 20:43:35 +00:00
// m_log.DebugFormat("[XEngine]: Found no script instance for {0}, returning empty string", itemID);
2008-12-06 14:55:18 +00:00
return "" ;
2011-08-24 19:49:23 +00:00
}
2009-11-26 12:08:20 +00:00
string xml = instance . GetXMLState ( ) ;
XmlDocument sdoc = new XmlDocument ( ) ;
2018-01-17 07:17:24 +00:00
2010-06-29 20:50:52 +00:00
bool loadedState = true ;
try
{
sdoc . LoadXml ( xml ) ;
}
2010-08-23 21:24:23 +00:00
catch ( System . Xml . XmlException )
2010-06-29 20:50:52 +00:00
{
loadedState = false ;
}
XmlNodeList rootL = null ;
XmlNode rootNode = null ;
if ( loadedState )
{
rootL = sdoc . GetElementsByTagName ( "ScriptState" ) ;
rootNode = rootL [ 0 ] ;
}
2009-11-26 12:08:20 +00:00
// Create <State UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">
XmlDocument doc = new XmlDocument ( ) ;
XmlElement stateData = doc . CreateElement ( "" , "State" , "" ) ;
XmlAttribute stateID = doc . CreateAttribute ( "" , "UUID" , "" ) ;
stateID . Value = itemID . ToString ( ) ;
stateData . Attributes . Append ( stateID ) ;
XmlAttribute assetID = doc . CreateAttribute ( "" , "Asset" , "" ) ;
assetID . Value = instance . AssetID . ToString ( ) ;
stateData . Attributes . Append ( assetID ) ;
2009-12-21 06:29:23 +00:00
XmlAttribute engineName = doc . CreateAttribute ( "" , "Engine" , "" ) ;
engineName . Value = ScriptEngineName ;
stateData . Attributes . Append ( engineName ) ;
2009-11-26 12:08:20 +00:00
doc . AppendChild ( stateData ) ;
2010-06-29 20:50:52 +00:00
XmlNode xmlstate = null ;
2009-11-26 12:08:20 +00:00
// Add <ScriptState>...</ScriptState>
2010-06-29 20:50:52 +00:00
if ( loadedState )
{
xmlstate = doc . ImportNode ( rootNode , true ) ;
}
else
{
xmlstate = doc . CreateElement ( "" , "ScriptState" , "" ) ;
}
2009-11-26 12:08:20 +00:00
stateData . AppendChild ( xmlstate ) ;
string assemName = instance . GetAssemblyName ( ) ;
string fn = Path . GetFileName ( assemName ) ;
string assem = String . Empty ;
2017-05-27 04:41:40 +00:00
string assemNameText = assemName + ".text" ;
2009-11-26 12:08:20 +00:00
2017-05-27 04:41:40 +00:00
if ( File . Exists ( assemNameText ) )
2009-11-26 12:08:20 +00:00
{
2017-05-27 04:41:40 +00:00
FileInfo tfi = new FileInfo ( assemNameText ) ;
2009-11-26 12:08:20 +00:00
if ( tfi ! = null )
{
Byte [ ] tdata = new Byte [ tfi . Length ] ;
try
{
2017-05-27 04:41:40 +00:00
using ( FileStream tfs = File . Open ( assemNameText ,
2010-07-09 16:58:16 +00:00
FileMode . Open , FileAccess . Read ) )
{
tfs . Read ( tdata , 0 , tdata . Length ) ;
}
2009-11-26 12:08:20 +00:00
2012-07-11 21:54:22 +00:00
assem = Encoding . ASCII . GetString ( tdata ) ;
2009-11-26 12:08:20 +00:00
}
catch ( Exception e )
{
2012-06-19 23:19:50 +00:00
m_log . ErrorFormat (
"[XEngine]: Unable to open script textfile {0}{1}, reason: {2}" ,
assemName , ".text" , e . Message ) ;
2009-11-26 12:08:20 +00:00
}
}
}
else
{
FileInfo fi = new FileInfo ( assemName ) ;
if ( fi ! = null )
{
Byte [ ] data = new Byte [ fi . Length ] ;
try
{
2010-07-09 16:58:16 +00:00
using ( FileStream fs = File . Open ( assemName , FileMode . Open , FileAccess . Read ) )
{
fs . Read ( data , 0 , data . Length ) ;
}
2009-11-26 12:08:20 +00:00
assem = System . Convert . ToBase64String ( data ) ;
}
catch ( Exception e )
{
2012-06-19 23:19:50 +00:00
m_log . ErrorFormat (
"[XEngine]: Unable to open script assembly {0}, reason: {1}" , assemName , e . Message ) ;
2009-11-26 12:08:20 +00:00
}
}
}
2009-11-26 17:03:09 +00:00
string map = String . Empty ;
if ( File . Exists ( fn + ".map" ) )
{
2010-07-09 16:58:16 +00:00
using ( FileStream mfs = File . Open ( fn + ".map" , FileMode . Open , FileAccess . Read ) )
{
using ( StreamReader msr = new StreamReader ( mfs ) )
{
map = msr . ReadToEnd ( ) ;
}
}
2009-11-26 17:03:09 +00:00
}
2009-11-26 12:08:20 +00:00
XmlElement assemblyData = doc . CreateElement ( "" , "Assembly" , "" ) ;
XmlAttribute assemblyName = doc . CreateAttribute ( "" , "Filename" , "" ) ;
assemblyName . Value = fn ;
assemblyData . Attributes . Append ( assemblyName ) ;
assemblyData . InnerText = assem ;
stateData . AppendChild ( assemblyData ) ;
2009-11-26 17:03:09 +00:00
XmlElement mapData = doc . CreateElement ( "" , "LineMap" , "" ) ;
XmlAttribute mapName = doc . CreateAttribute ( "" , "Filename" , "" ) ;
2009-08-04 02:17:13 +00:00
2009-11-26 17:03:09 +00:00
mapName . Value = fn + ".map" ;
mapData . Attributes . Append ( mapName ) ;
2009-08-04 02:17:13 +00:00
2009-11-26 17:03:09 +00:00
mapData . InnerText = map ;
stateData . AppendChild ( mapData ) ;
2011-08-24 19:49:23 +00:00
2019-01-25 21:21:23 +00:00
// m_log.DebugFormat("[XEngine]: Got XML state for {0}", itemID);
2011-08-24 19:49:23 +00:00
2009-11-26 17:03:09 +00:00
return doc . InnerXml ;
2009-08-04 02:17:13 +00:00
}
2009-10-17 21:36:44 +00:00
private bool ShowScriptSaveResponse ( UUID ownerID , UUID assetID , string text , bool compiled )
{
return false ;
}
2009-11-26 17:03:09 +00:00
2009-12-21 10:26:52 +00:00
public bool SetXMLState ( UUID itemID , string xml )
2009-11-26 17:03:09 +00:00
{
2012-06-19 00:53:50 +00:00
// m_log.DebugFormat("[XEngine]: Writing state for script item with ID {0}", itemID);
2009-11-26 17:03:09 +00:00
if ( xml = = String . Empty )
2009-12-21 10:26:52 +00:00
return false ;
2009-11-26 17:03:09 +00:00
XmlDocument doc = new XmlDocument ( ) ;
try
{
doc . LoadXml ( xml ) ;
}
catch ( Exception )
{
m_log . Error ( "[XEngine]: Exception decoding XML data from region transfer" ) ;
2009-12-21 10:26:52 +00:00
return false ;
2009-11-26 17:03:09 +00:00
}
XmlNodeList rootL = doc . GetElementsByTagName ( "State" ) ;
if ( rootL . Count < 1 )
2009-12-21 10:26:52 +00:00
return false ;
2009-11-26 17:03:09 +00:00
XmlElement rootE = ( XmlElement ) rootL [ 0 ] ;
2009-12-21 06:29:23 +00:00
if ( rootE . GetAttribute ( "Engine" ) ! = ScriptEngineName )
2009-12-21 10:26:52 +00:00
return false ;
2009-12-21 06:29:23 +00:00
2009-12-21 06:41:10 +00:00
// On rez from inventory, that ID will have changed. It was only
// advisory anyway. So we don't check it anymore.
/ /
// if (rootE.GetAttribute("UUID") != itemID.ToString())
// return;
2009-11-26 17:03:09 +00:00
XmlNodeList stateL = rootE . GetElementsByTagName ( "ScriptState" ) ;
if ( stateL . Count ! = 1 )
2009-12-21 10:26:52 +00:00
return false ;
2009-11-26 17:03:09 +00:00
XmlElement stateE = ( XmlElement ) stateL [ 0 ] ;
if ( World . m_trustBinaries )
{
XmlNodeList assemL = rootE . GetElementsByTagName ( "Assembly" ) ;
if ( assemL . Count ! = 1 )
2009-12-21 10:26:52 +00:00
return false ;
2009-11-26 17:03:09 +00:00
XmlElement assemE = ( XmlElement ) assemL [ 0 ] ;
string fn = assemE . GetAttribute ( "Filename" ) ;
string base64 = assemE . InnerText ;
2010-09-25 20:22:18 +00:00
string path = Path . Combine ( m_ScriptEnginesPath , World . RegionInfo . RegionID . ToString ( ) ) ;
2009-11-26 17:03:09 +00:00
path = Path . Combine ( path , fn ) ;
if ( ! File . Exists ( path ) )
{
Byte [ ] filedata = Convert . FromBase64String ( base64 ) ;
2010-07-09 16:58:16 +00:00
try
{
using ( FileStream fs = File . Create ( path ) )
{
2012-06-19 00:53:50 +00:00
// m_log.DebugFormat("[XEngine]: Writing assembly file {0}", path);
2010-07-09 16:58:16 +00:00
fs . Write ( filedata , 0 , filedata . Length ) ;
}
}
catch ( IOException ex )
{
// if there already exists a file at that location, it may be locked.
2012-06-19 00:53:50 +00:00
m_log . ErrorFormat ( "[XEngine]: Error whilst writing assembly file {0}, {1}" , path , ex . Message ) ;
2010-07-09 16:58:16 +00:00
}
2012-04-23 12:31:45 +00:00
string textpath = path + ".text" ;
2010-07-09 16:58:16 +00:00
try
{
2012-04-23 12:31:45 +00:00
using ( FileStream fs = File . Create ( textpath ) )
2010-07-09 16:58:16 +00:00
{
using ( StreamWriter sw = new StreamWriter ( fs ) )
{
2012-06-19 00:53:50 +00:00
// m_log.DebugFormat("[XEngine]: Writing .text file {0}", textpath);
2010-07-09 16:58:16 +00:00
sw . Write ( base64 ) ;
}
}
}
catch ( IOException ex )
{
// if there already exists a file at that location, it may be locked.
2012-06-19 00:53:50 +00:00
m_log . ErrorFormat ( "[XEngine]: Error whilst writing .text file {0}, {1}" , textpath , ex . Message ) ;
2010-07-09 16:58:16 +00:00
}
2009-11-26 17:03:09 +00:00
}
2012-06-19 01:11:46 +00:00
XmlNodeList mapL = rootE . GetElementsByTagName ( "LineMap" ) ;
if ( mapL . Count > 0 )
{
XmlElement mapE = ( XmlElement ) mapL [ 0 ] ;
string mappath = Path . Combine ( m_ScriptEnginesPath , World . RegionInfo . RegionID . ToString ( ) ) ;
mappath = Path . Combine ( mappath , mapE . GetAttribute ( "Filename" ) ) ;
try
{
using ( FileStream mfs = File . Create ( mappath ) )
{
using ( StreamWriter msw = new StreamWriter ( mfs ) )
{
// m_log.DebugFormat("[XEngine]: Writing linemap file {0}", mappath);
msw . Write ( mapE . InnerText ) ;
}
}
}
catch ( IOException ex )
{
// if there already exists a file at that location, it may be locked.
2014-12-03 20:35:49 +00:00
m_log . Error (
string . Format ( "[XEngine]: Linemap file {0} could not be written. Exception " , mappath ) , ex ) ;
2012-06-19 01:11:46 +00:00
}
}
2009-11-26 17:03:09 +00:00
}
2010-09-25 20:22:18 +00:00
string statepath = Path . Combine ( m_ScriptEnginesPath , World . RegionInfo . RegionID . ToString ( ) ) ;
2009-11-26 17:03:09 +00:00
statepath = Path . Combine ( statepath , itemID . ToString ( ) + ".state" ) ;
2010-07-09 16:58:16 +00:00
try
{
using ( FileStream sfs = File . Create ( statepath ) )
{
using ( StreamWriter ssw = new StreamWriter ( sfs ) )
{
2012-06-19 00:53:50 +00:00
// m_log.DebugFormat("[XEngine]: Writing state file {0}", statepath);
2010-07-09 16:58:16 +00:00
ssw . Write ( stateE . OuterXml ) ;
}
}
}
catch ( IOException ex )
{
// if there already exists a file at that location, it may be locked.
2012-06-19 00:53:50 +00:00
m_log . ErrorFormat ( "[XEngine]: Error whilst writing state file {0}, {1}" , statepath , ex . Message ) ;
2010-07-09 16:58:16 +00:00
}
2009-11-26 17:03:09 +00:00
2014-12-10 00:25:27 +00:00
// m_log.DebugFormat(
// "[XEngine]: Wrote state for script item with ID {0} at {1} in {2}", itemID, statepath, m_Scene.Name);
2009-12-21 10:26:52 +00:00
return true ;
2009-11-26 17:03:09 +00:00
}
2009-12-22 05:25:32 +00:00
public ArrayList GetScriptErrors ( UUID itemID )
{
2009-12-22 09:24:01 +00:00
System . Threading . Thread . Sleep ( 1000 ) ;
lock ( m_ScriptErrors )
{
if ( m_ScriptErrors . ContainsKey ( itemID ) )
{
ArrayList ret = m_ScriptErrors [ itemID ] ;
m_ScriptErrors . Remove ( itemID ) ;
return ret ;
}
return new ArrayList ( ) ;
}
2009-12-22 05:25:32 +00:00
}
2010-04-19 05:29:26 +00:00
2012-03-16 00:34:30 +00:00
public Dictionary < uint , float > GetObjectScriptsExecutionTimes ( )
{
Dictionary < uint , float > topScripts = new Dictionary < uint , float > ( ) ;
lock ( m_Scripts )
{
foreach ( IScriptInstance si in m_Scripts . Values )
{
if ( ! topScripts . ContainsKey ( si . LocalID ) )
2012-03-16 01:31:53 +00:00
topScripts [ si . RootLocalID ] = 0 ;
2012-03-16 00:34:30 +00:00
2015-07-27 09:16:21 +00:00
topScripts [ si . RootLocalID ] + = GetExectionTime ( si ) ;
2012-04-10 20:25:05 +00:00
}
}
2012-03-16 00:34:30 +00:00
2012-04-10 20:25:05 +00:00
return topScripts ;
}
2012-03-16 00:34:30 +00:00
2012-04-10 20:25:05 +00:00
public float GetScriptExecutionTime ( List < UUID > itemIDs )
{
if ( itemIDs = = null | | itemIDs . Count = = 0 )
{
return 0.0f ;
}
float time = 0.0f ;
IScriptInstance si ;
// Calculate the time for all scripts that this engine is executing
// Ignore any others
foreach ( UUID id in itemIDs )
{
si = GetInstance ( id ) ;
if ( si ! = null & & si . Running )
{
2015-07-27 09:16:21 +00:00
time + = GetExectionTime ( si ) ;
2012-03-16 00:34:30 +00:00
}
}
2012-04-10 20:25:05 +00:00
return time ;
}
2012-03-16 00:34:30 +00:00
2015-07-27 09:16:21 +00:00
private float GetExectionTime ( IScriptInstance si )
2012-04-10 20:25:05 +00:00
{
2015-07-27 09:16:21 +00:00
return ( float ) si . ExecutionTime . GetSumTime ( ) . TotalMilliseconds ;
2012-03-16 00:34:30 +00:00
}
2020-05-25 13:41:59 +00:00
public bool SuspendScript ( UUID itemID )
2010-04-19 05:29:26 +00:00
{
2020-05-25 13:41:59 +00:00
// m_log.DebugFormat("[XEngine]: Received request to suspend script with ID {0}", itemID);
m_Scene . setThreadCount ( m_ThreadPool . InUseThreads ) ;
2010-04-19 05:29:26 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2020-05-25 13:41:59 +00:00
if ( instance = = null )
return false ;
2015-04-20 18:55:00 +00:00
2020-05-25 13:41:59 +00:00
instance . Suspend ( ) ;
return true ;
2010-04-19 05:29:26 +00:00
}
2020-05-25 13:41:59 +00:00
public bool ResumeScript ( UUID itemID )
2010-04-19 05:29:26 +00:00
{
2020-05-25 13:41:59 +00:00
// m_log.DebugFormat("[XEngine]: Received request to resume script with ID {0}", itemID);
m_Scene . setThreadCount ( m_ThreadPool . InUseThreads ) ;
2012-02-07 17:44:37 +00:00
2010-04-19 05:29:26 +00:00
IScriptInstance instance = GetInstance ( itemID ) ;
2011-10-19 21:30:31 +00:00
if ( instance ! = null )
2020-05-25 13:41:59 +00:00
{
2011-10-19 21:30:31 +00:00
instance . Resume ( ) ;
2020-05-25 13:41:59 +00:00
return true ;
}
return false ;
2010-04-19 05:29:26 +00:00
}
2012-07-01 17:30:59 +00:00
public bool HasScript ( UUID itemID , out bool running )
{
running = true ;
IScriptInstance instance = GetInstance ( itemID ) ;
if ( instance = = null )
return false ;
running = instance . Running ;
return true ;
}
2015-08-17 19:46:30 +00:00
public void SleepScript ( UUID itemID , int delay )
{
IScriptInstance instance = GetInstance ( itemID ) ;
if ( instance = = null )
return ;
instance . ExecutionTimer . Stop ( ) ;
try
{
if ( instance . CoopWaitHandle ! = null )
{
if ( instance . CoopWaitHandle . WaitOne ( delay ) )
throw new ScriptCoopStopException ( ) ;
}
else
{
Thread . Sleep ( delay ) ;
}
}
finally
{
instance . ExecutionTimer . Start ( ) ;
}
}
2008-05-30 12:27:06 +00:00
}
2012-04-18 19:01:41 +00:00
}