Exemplo n.º 1
0
        static void test_KingstarGold_Main(string[] args)
        {
            ApiManager.QueuePath = @"C:\Program Files\SmartQuant Ltd\OpenQuant 2014\QuantBox_Queue.dll";
            api = ApiManager.CreateApi(@"C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\KingstarGold\QuantBox_KingstarGold.dll");

            api.Server.BrokerID = "";
            api.Server.Address  = "tcp://124.74.239.38:18961";

            api.User.UserID   = "9843010200773696";
            api.User.Password = "******";

            api.OnConnectionStatus   = OnConnectionStatus;
            api.OnRtnDepthMarketData = OnRtnDepthMarketData;

            api.Connect();

            Console.ReadKey();
            api.Subscribe("IF1411", "");

            Console.ReadKey();

            ApiManager.ReleaseApi(api);

            Console.ReadKey();

            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void test_CTP_Main(string[] args)
        {
            ApiManager.QueuePath = @"C:\Program Files\SmartQuant Ltd\OpenQuant 2014\QuantBox_Queue.dll";
            api = ApiManager.CreateApi(@"C:\Program Files\SmartQuant Ltd\OpenQuant 2014\XAPI\CTP\QuantBox_CTP_Trade.dll");

            api.Server.BrokerID = "1017";
            api.Server.Address  = "tcp://ctpmn1-front1.citicsf.com:51205";

            api.User.UserID   = "00000015";
            api.User.Password = "******";

            api.OnConnectionStatus   = OnConnectionStatus;
            api.OnRtnDepthMarketData = OnRtnDepthMarketData;

            api.Connect();

            Thread.Sleep(5 * 1000);

            ApiManager.ReleaseApi(api);



            //api.Dispose();
            //queue.Dispose();
        }
Exemplo n.º 3
0
        static void test_Linux_Main(string[] args)
        {
            //Queue queue = new Queue(@"libQuantBox_Queue.so");
            //Queue queue2 = new Queue(@"libQuantBox_Queue.so");

            //ApiManager.QueuePath = @"/home/hetao/works/QuantBox_XAPI/bin/Debug/libQuantBox_Queue.so";
            //XApi api = ApiManager.CreateApi(@"/home/hetao/works/QuantBox_XAPI/bin/Debug/libQuantBox_CTP_Quote.so");
            //XApi api2 = ApiManager.CreateApi(@"/home/hetao/works/QuantBox_XAPI/bin/Debug/libQuantBox_CTP_Trade.so");

            ApiManager.QueuePath = @"libQuantBox_Queue.so";
            XApi api  = ApiManager.CreateApi(@"libQuantBox_CTP_Quote.so");
            XApi api2 = ApiManager.CreateApi(@"libQuantBox_CTP_Trade.so");

            api.Server.BrokerID = "1017";
            api.Server.Address  = "tcp://ctpmn1-front1.citicsf.com:51213";

            api.User.UserID   = "00000015";
            api.User.Password = "******";

            api.OnConnectionStatus   = OnConnectionStatus;
            api.OnRtnDepthMarketData = OnRtnDepthMarketData;

            api2.Server.BrokerID = "1017";
            api2.Server.Address  = "tcp://ctpmn1-front1.citicsf.com:51205";
            api2.Server.PrivateTopicResumeType = ResumeType.Quick;

            api2.User.UserID   = "00000015";
            api2.User.Password = "******";


            api2.OnConnectionStatus     = OnConnectionStatus2;
            api2.OnRspQryInstrument     = OnRspQryInstrument;
            api2.OnRspQryTradingAccount = OnRspQryTradingAccount;
            api2.OnRspQrySettlementInfo = OnRspQrySettlementInfo;
            api2.OnRtnOrder             = OnRtnOrder;
            api2.OnRtnError             = OnRtnError;
            api2.OnRtnTrade             = OnRtnTrade;

            api.Connect();
            api2.Connect();

            api.Subscribe("IF1412", "");

            Console.ReadKey();

            Thread.Sleep(10000);
            Console.WriteLine(123);
            ApiManager.ReleaseApi(api);
            ApiManager.ReleaseApi(api2);
        }
Exemplo n.º 4
0
        /****************************************************************************\
        *  The only method of interest to outside this module is the Initializer.  *
        *                                                                          *
        *  The rest of this module contains support routines for the Initializer.  *
        \****************************************************************************/

        /**
         * @brief Initializer, loads script in memory and all ready for running.
         * @param engine = YEngine instance this is part of
         * @param scriptBasePath = directory name where files are
         * @param stackSize = number of bytes to allocate for stacks
         * @param errors = return compiler errors in this array
         * @param forceRecomp = force recompile
         * Throws exception if any error, so it was successful if it returns.
         */
        public void Initialize(Yengine engine, string scriptBasePath,
                               int stackSize, int heapSize, ArrayList errors)
        {
            if (stackSize < 16384)
            {
                stackSize = 16384;
            }
            if (heapSize < 16384)
            {
                heapSize = 16384;
            }

            // Save all call parameters in instance vars for easy access.
            m_Engine         = engine;
            m_ScriptBasePath = scriptBasePath;
            m_StackSize      = stackSize;
            m_StackLeft      = stackSize;
            m_HeapSize       = heapSize;
            m_localsHeapUsed = 0;
            m_arraysHeapUsed = 0;
            m_CompilerErrors = errors;
            m_StateFileName  = GetStateFileName(scriptBasePath, m_ItemID);

            // Not in any XMRInstQueue.
            m_NextInst = this;
            m_PrevInst = this;

            // Set up list of API calls it has available.
            // This also gets the API modules ready to accept setup data, such as
            // active listeners being restored.
            IScriptApi scriptApi;
            ApiManager am = new ApiManager();

            foreach (string api in am.GetApis())
            {
                // Instantiate the API for this script instance.
                if (api != "LSL")
                {
                    scriptApi = am.CreateApi(api);
                }
                else
                {
                    scriptApi = m_XMRLSLApi = new XMRLSL_Api();
                }

                // Connect it up to the instance.
                InitScriptApi(engine, api, scriptApi);
            }

            m_XMRLSLApi.InitXMRLSLApi(this);

            // Get object loaded, compiling script and reading .state file as
            // necessary to restore the state.
            suspendOnCheckRunHold = true;
            InstantiateScript();
            m_SourceCode = null;
            if (m_ObjCode == null)
            {
                throw new ArgumentNullException("m_ObjCode");
            }
            if (m_ObjCode.scriptEventHandlerTable == null)
            {
                throw new ArgumentNullException("m_ObjCode.scriptEventHandlerTable");
            }

            suspendOnCheckRunHold = false;
            suspendOnCheckRunTemp = false;
        }
        public ScriptInstance(IScriptEngine engine, SceneObjectPart part,
                              UUID itemID, UUID assetID, string assembly,
                              AppDomain dom, string primName, string scriptName,
                              int startParam, bool postOnRez, StateSource stateSource,
                              int maxScriptQueue)
        {
            State      = "default";
            EventQueue = new Queue(32);

            Engine           = engine;
            LocalID          = part.LocalId;
            ObjectID         = part.UUID;
            RootLocalID      = part.ParentGroup.LocalId;
            RootObjectID     = part.ParentGroup.UUID;
            ItemID           = itemID;
            AssetID          = assetID;
            PrimName         = primName;
            ScriptName       = scriptName;
            m_Assembly       = assembly;
            StartParam       = startParam;
            m_MaxScriptQueue = maxScriptQueue;
            m_stateSource    = stateSource;
            m_postOnRez      = postOnRez;
            m_AttachedAvatar = part.ParentGroup.AttachedAvatar;
            m_RegionID       = part.ParentGroup.Scene.RegionInfo.RegionID;

            if (part != null)
            {
                lock (part.TaskInventory)
                {
                    if (part.TaskInventory.ContainsKey(ItemID))
                    {
                        ScriptTask = part.TaskInventory[ItemID];
                    }
                }
            }

            ApiManager am = new ApiManager();

            foreach (string api in am.GetApis())
            {
                m_Apis[api] = am.CreateApi(api);
                m_Apis[api].Initialize(engine, part, LocalID, itemID);
            }

            try
            {
                if (dom != System.AppDomain.CurrentDomain)
                {
                    m_Script = (IScript)dom.CreateInstanceAndUnwrap(
                        Path.GetFileNameWithoutExtension(assembly),
                        "SecondLife.Script");
                }
                else
                {
                    m_Script = (IScript)Assembly.Load(
                        Path.GetFileNameWithoutExtension(assembly)).CreateInstance(
                        "SecondLife.Script");
                }

                //ILease lease = (ILease)RemotingServices.GetLifetimeService(m_Script as ScriptBaseClass);
                //RemotingServices.GetLifetimeService(m_Script as ScriptBaseClass);
//                lease.Register(this);
            }
            catch (Exception e)
            {
                m_log.ErrorFormat(
                    "[SCRIPT INSTANCE]: Error loading assembly {0}.  Exception {1}{2}",
                    assembly, e.Message, e.StackTrace);
            }

            try
            {
                foreach (KeyValuePair <string, IScriptApi> kv in m_Apis)
                {
                    m_Script.InitApi(kv.Key, kv.Value);
                }

//                // m_log.Debug("[Script] Script instance created");

                part.SetScriptEvents(ItemID,
                                     (int)m_Script.GetStateEventFlags(State));
            }
            catch (Exception e)
            {
                m_log.ErrorFormat(
                    "[SCRIPT INSTANCE]: Error loading script instance from assembly {0}.  Exception {1}{2}",
                    assembly, e.Message, e.StackTrace);

                return;
            }

            m_SaveState = true;

            string savedState = Path.Combine(Path.GetDirectoryName(assembly),
                                             ItemID.ToString() + ".state");

            if (File.Exists(savedState))
            {
                string xml = String.Empty;

                try
                {
                    FileInfo fi   = new FileInfo(savedState);
                    int      size = (int)fi.Length;
                    if (size < 512000)
                    {
                        using (FileStream fs = File.Open(savedState,
                                                         FileMode.Open, FileAccess.Read, FileShare.None))
                        {
                            System.Text.UTF8Encoding enc =
                                new System.Text.UTF8Encoding();

                            Byte[] data = new Byte[size];
                            fs.Read(data, 0, size);

                            xml = enc.GetString(data);

                            ScriptSerializer.Deserialize(xml, this);

                            AsyncCommandManager.CreateFromData(Engine,
                                                               LocalID, ItemID, ObjectID,
                                                               PluginData);

//                            m_log.DebugFormat("[Script] Successfully retrieved state for script {0}.{1}", PrimName, m_ScriptName);

                            part.SetScriptEvents(ItemID,
                                                 (int)m_Script.GetStateEventFlags(State));

                            Running = false;

                            if (ShuttingDown)
                            {
                                m_startOnInit = false;
                            }

                            // we get new rez events on sim restart, too
                            // but if there is state, then we fire the change
                            // event

                            // We loaded state, don't force a re-save
                            m_SaveState             = false;
                            m_startedFromSavedState = true;
                        }
                    }
                    else
                    {
                        m_log.ErrorFormat(
                            "[SCRIPT INSTANCE]: Unable to load script state from assembly {0}: Memory limit exceeded",
                            assembly);
                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat(
                        "[SCRIPT INSTANCE]: Unable to load script state from assembly {0}.  XML is {1}.  Exception {2}{3}",
                        assembly, xml, e.Message, e.StackTrace);
                }
            }
//            else
//            {
//                ScenePresence presence = Engine.World.GetScenePresence(part.OwnerID);

//                if (presence != null && (!postOnRez))
//                    presence.ControllingClient.SendAgentAlertMessage("Compile successful", false);

//            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Load the script from an assembly into an AppDomain.
        /// </summary>
        /// <param name='dom'></param>
        /// <param name='assembly'></param>
        /// <param name='stateSource'></param>
        /// <returns>false if load failed, true if suceeded</returns>
        public bool Load(AppDomain dom, string assembly, StateSource stateSource)
        {
            m_Assembly    = assembly;
            m_stateSource = stateSource;

            ApiManager am = new ApiManager();

            foreach (string api in am.GetApis())
            {
                m_Apis[api] = am.CreateApi(api);
                m_Apis[api].Initialize(Engine, Part, ScriptTask, m_coopSleepHandle);
            }

            try
            {
                object[] constructorParams;

                Assembly scriptAssembly = dom.Load(Path.GetFileNameWithoutExtension(assembly));
                Type     scriptType     = scriptAssembly.GetType("SecondLife.XEngineScript");

                if (scriptType != null)
                {
                    constructorParams = new object[] { m_coopSleepHandle };
                }
                else
                {
                    m_log.ErrorFormat(
                        "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  You must remove all existing {6}* script DLL files before using enabling co-op termination"
                        + ", either by setting DeleteScriptsOnStartup = true in [XEngine] for one run"
                        + " or by deleting these files manually.",
                        ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, assembly);

                    return(false);
                }

//                m_log.DebugFormat(
//                    "[SCRIPT INSTANCE]: Looking to load {0} from assembly {1} in {2}",
//                    scriptType.FullName, Path.GetFileNameWithoutExtension(assembly), Engine.World.Name);

                if (dom != System.AppDomain.CurrentDomain)
                {
                    m_Script
                        = (IScript)dom.CreateInstanceAndUnwrap(
                              Path.GetFileNameWithoutExtension(assembly),
                              scriptType.FullName,
                              false,
                              BindingFlags.Default,
                              null,
                              constructorParams,
                              null,
                              null);
                }
                else
                {
                    m_Script
                        = (IScript)scriptAssembly.CreateInstance(
                              scriptType.FullName,
                              false,
                              BindingFlags.Default,
                              null,
                              constructorParams,
                              null,
                              null);
                }

                //ILease lease = (ILease)RemotingServices.GetLifetimeService(m_Script as ScriptBaseClass);
                //RemotingServices.GetLifetimeService(m_Script as ScriptBaseClass);
//                lease.Register(this);
            }
            catch (Exception e)
            {
                m_log.ErrorFormat(
                    "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  Error loading assembly {6}.  Exception {7}{8}",
                    ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, assembly, e.Message, e.StackTrace);

                return(false);
            }

            try
            {
                foreach (KeyValuePair <string, IScriptApi> kv in m_Apis)
                {
                    m_Script.InitApi(kv.Key, kv.Value);
                }

//                // m_log.Debug("[Script] Script instance created");

                Part.SetScriptEvents(ItemID,
                                     (int)m_Script.GetStateEventFlags(State));
            }
            catch (Exception e)
            {
                m_log.ErrorFormat(
                    "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  Error initializing script instance.  Exception {6}{7}",
                    ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, e.Message, e.StackTrace);

                return(false);
            }

            m_SaveState = true;

            string savedState = Path.Combine(Path.GetDirectoryName(assembly),
                                             ItemID.ToString() + ".state");

            if (File.Exists(savedState))
            {
                string xml = String.Empty;

                try
                {
                    FileInfo fi   = new FileInfo(savedState);
                    int      size = (int)fi.Length;
                    if (size < 512000)
                    {
                        using (FileStream fs = File.Open(savedState,
                                                         FileMode.Open, FileAccess.Read, FileShare.None))
                        {
                            Byte[] data = new Byte[size];
                            fs.Read(data, 0, size);

                            xml = Encoding.UTF8.GetString(data);

                            ScriptSerializer.Deserialize(xml, this);

                            AsyncCommandManager.CreateFromData(Engine,
                                                               LocalID, ItemID, ObjectID,
                                                               PluginData);

//                            m_log.DebugFormat("[Script] Successfully retrieved state for script {0}.{1}", PrimName, m_ScriptName);

                            Part.SetScriptEvents(ItemID,
                                                 (int)m_Script.GetStateEventFlags(State));

                            if (!Running)
                            {
                                m_startOnInit = false;
                            }

                            Running = false;

                            // we get new rez events on sim restart, too
                            // but if there is state, then we fire the change
                            // event

                            // We loaded state, don't force a re-save
                            m_SaveState             = false;
                            m_startedFromSavedState = true;
                        }
                    }
                    else
                    {
                        m_log.WarnFormat(
                            "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  Unable to load script state file {6}.  Memory limit exceeded.",
                            ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, savedState);
                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat(
                        "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  Unable to load script state file {6}.  XML is {7}.  Exception {8}{9}",
                        ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, savedState, xml, e.Message, e.StackTrace);
                }
            }
//            else
//            {
//                ScenePresence presence = Engine.World.GetScenePresence(part.OwnerID);

//                if (presence != null && (!postOnRez))
//                    presence.ControllingClient.SendAgentAlertMessage("Compile successful", false);

//            }

            return(true);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Load the script from an assembly into an AppDomain.
        /// </summary>
        /// <param name='dom'></param>
        /// <param name='assembly'></param>
        /// <param name='dataPath'>
        /// Path for all script associated data (state, etc.).  In a multi-region set up
        /// with all scripts loading into the same AppDomain this may not be the same place as the DLL itself.
        /// </param>
        /// <param name='stateSource'></param>
        /// <returns>false if load failed, true if suceeded</returns>
        public bool Load(
            IScript script, EventWaitHandle coopSleepHandle, string assemblyPath,
            string dataPath, StateSource stateSource, bool coopTermination)
        {
            m_Script          = script;
            m_coopSleepHandle = coopSleepHandle;
            m_assemblyPath    = assemblyPath;
            m_dataPath        = dataPath;
            m_stateSource     = stateSource;
            m_coopTermination = coopTermination;

            ApiManager am = new ApiManager();

            foreach (string api in am.GetApis())
            {
                m_Apis[api] = am.CreateApi(api);
                m_Apis[api].Initialize(Engine, Part, ScriptTask, m_coopSleepHandle);
            }

            try
            {
                foreach (KeyValuePair <string, IScriptApi> kv in m_Apis)
                {
                    m_Script.InitApi(kv.Key, kv.Value);
                }

                //                // m_log.Debug("[Script] Script instance created");

                Part.SetScriptEvents(ItemID, (int)m_Script.GetStateEventFlags(State));
            }
            catch (Exception e)
            {
                m_log.ErrorFormat(
                    "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  Error initializing script instance.  Exception {6}{7}",
                    ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, e.Message, e.StackTrace);

                return(false);
            }

            // For attachments, XEngine saves the state into a .state file when XEngine.SetXMLState() is called.
            string savedState = Path.Combine(m_dataPath, ItemID.ToString() + ".state");

            if (File.Exists(savedState))
            {
                //                m_log.DebugFormat(
                //                    "[SCRIPT INSTANCE]: Found state for script {0} for {1} ({2}) at {3} in {4}",
                //                    ItemID, savedState, Part.Name, Part.ParentGroup.Name, Part.ParentGroup.Scene.Name);

                string xml = String.Empty;

                try
                {
                    FileInfo fi   = new FileInfo(savedState);
                    int      size = (int)fi.Length;
                    if (size < 512000)
                    {
                        using (FileStream fs = File.Open(savedState,
                                                         FileMode.Open, FileAccess.Read, FileShare.None))
                        {
                            Byte[] data = new Byte[size];
                            fs.Read(data, 0, size);

                            xml = Encoding.UTF8.GetString(data);

                            ScriptSerializer.Deserialize(xml, this);

                            AsyncCommandManager.CreateFromData(Engine,
                                                               LocalID, ItemID, ObjectID,
                                                               PluginData);

                            //                            m_log.DebugFormat("[Script] Successfully retrieved state for script {0}.{1}", PrimName, m_ScriptName);

                            Part.SetScriptEvents(ItemID,
                                                 (int)m_Script.GetStateEventFlags(State));

                            if (!Running)
                            {
                                m_startOnInit = false;
                            }

                            Running = false;

                            // we get new rez events on sim restart, too
                            // but if there is state, then we fire the change
                            // event

                            // We loaded state, don't force a re-save
                            m_SaveState             = false;
                            m_startedFromSavedState = true;
                        }

                        // If this script is in an attachment then we no longer need the state file.
                        if (!StatePersistedHere)
                        {
                            RemoveState();
                        }
                    }
                    //                    else
                    //                    {
                    //                        m_log.WarnFormat(
                    //                            "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  Unable to load script state file {6}.  Memory limit exceeded.",
                    //                            ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, savedState);
                    //                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat(
                        "[SCRIPT INSTANCE]: Not starting script {0} (id {1}) in part {2} (id {3}) in object {4} in {5}.  Unable to load script state file {6}.  XML is {7}.  Exception {8}{9}",
                        ScriptTask.Name, ScriptTask.ItemID, Part.Name, Part.UUID, Part.ParentGroup.Name, Engine.World.Name, savedState, xml, e.Message, e.StackTrace);
                }
            }
            //            else
            //            {
            //                m_log.DebugFormat(
            //                    "[SCRIPT INSTANCE]: Did not find state for script {0} for {1} ({2}) at {3} in {4}",
            //                    ItemID, savedState, Part.Name, Part.ParentGroup.Name, Part.ParentGroup.Scene.Name);
            //            }

            return(true);
        }
Exemplo n.º 8
0
        public void _StartScript(uint localID, UUID itemID, string Script,
                                 int startParam, bool postOnRez)
        {
            m_log.DebugFormat(
                "[{0}]: ScriptManager StartScript: localID: {1}, itemID: {2}",
                m_scriptEngine.ScriptEngineName, localID, itemID);

            // We will initialize and start the script.
            // It will be up to the script itself to hook up the correct events.
            string CompiledScriptFile = String.Empty;

            SceneObjectPart m_host = World.GetSceneObjectPart(localID);

            if (null == m_host)
            {
                m_log.ErrorFormat(
                    "[{0}]: Could not find scene object part corresponding " +
                    "to localID {1} to start script",
                    m_scriptEngine.ScriptEngineName, localID);

                return;
            }

            UUID assetID = UUID.Zero;
            TaskInventoryItem taskInventoryItem = new TaskInventoryItem();

            if (m_host.TaskInventory.TryGetValue(itemID, out taskInventoryItem))
            {
                assetID = taskInventoryItem.AssetID;
            }

            ScenePresence presence =
                World.GetScenePresence(taskInventoryItem.OwnerID);

            CultureInfo USCulture = new CultureInfo("en-US");

            Thread.CurrentThread.CurrentCulture = USCulture;

            try
            {
                // Compile (We assume LSL)
                CompiledScriptFile =
                    LSLCompiler.PerformScriptCompile(Script,
                                                     assetID.ToString());

                if (presence != null && (!postOnRez))
                {
                    presence.ControllingClient.SendAgentAlertMessage(
                        "Compile successful", false);
                }

                m_log.InfoFormat("[SCRIPT]: Compiled assetID {0}: {1}",
                                 assetID, CompiledScriptFile);

                InstanceData id = new InstanceData();

                IScript CompiledScript;
                CompiledScript =
                    m_scriptEngine.m_AppDomainManager.LoadScript(
                        CompiledScriptFile, out id.AppDomain);
                //Register the sponsor
//                ISponsor scriptSponsor = new ScriptSponsor();
//                ILease lease = (ILease)RemotingServices.GetLifetimeService(CompiledScript as MarshalByRefObject);
//                lease.Register(scriptSponsor);
//                id.ScriptSponsor = scriptSponsor;

                id.LineMap    = LSLCompiler.LineMap();
                id.Script     = CompiledScript;
                id.Source     = Script;
                id.StartParam = startParam;
                id.State      = "default";
                id.Running    = true;
                id.Disabled   = false;

                // Add it to our script memstruct
                m_scriptEngine.m_ScriptManager.SetScript(localID, itemID, id);

                id.Apis = new Dictionary <string, IScriptApi>();

                ApiManager am = new ApiManager();

                foreach (string api in am.GetApis())
                {
                    id.Apis[api] = am.CreateApi(api);
                    id.Apis[api].Initialize(m_scriptEngine, m_host,
                                            localID, itemID);
                }

                foreach (KeyValuePair <string, IScriptApi> kv in id.Apis)
                {
                    CompiledScript.InitApi(kv.Key, kv.Value);
                }

                // Fire the first start-event
                int eventFlags =
                    m_scriptEngine.m_ScriptManager.GetStateEventFlags(
                        localID, itemID);

                m_host.SetScriptEvents(itemID, eventFlags);

                m_scriptEngine.m_EventQueueManager.AddToScriptQueue(
                    localID, itemID, "state_entry", new DetectParams[0],
                    new object[] { });

                if (postOnRez)
                {
                    m_scriptEngine.m_EventQueueManager.AddToScriptQueue(
                        localID, itemID, "on_rez", new DetectParams[0],
                        new object[] { new LSL_Types.LSLInteger(startParam) });
                }

                string[] warnings = LSLCompiler.GetWarnings();

                if (warnings != null && warnings.Length != 0)
                {
                    if (presence != null && (!postOnRez))
                    {
                        presence.ControllingClient.SendAgentAlertMessage(
                            "Script saved with warnings, check debug window!",
                            false);
                    }

                    foreach (string warning in warnings)
                    {
                        try
                        {
                            // DISPLAY WARNING INWORLD
                            string text = "Warning:\n" + warning;
                            if (text.Length > 1100)
                            {
                                text = text.Substring(0, 1099);
                            }

                            World.SimChat(Utils.StringToBytes(text),
                                          ChatTypeEnum.DebugChannel, 2147483647,
                                          m_host.AbsolutePosition, m_host.Name, m_host.UUID,
                                          false);
                        }
                        catch (Exception e2) // LEGIT: User Scripting
                        {
                            m_log.Error("[" +
                                        m_scriptEngine.ScriptEngineName +
                                        "]: Error displaying warning in-world: " +
                                        e2.ToString());
                            m_log.Error("[" +
                                        m_scriptEngine.ScriptEngineName + "]: " +
                                        "Warning:\r\n" +
                                        warning);
                        }
                    }
                }
            }
            catch (Exception e) // LEGIT: User Scripting
            {
                if (presence != null && (!postOnRez))
                {
                    presence.ControllingClient.SendAgentAlertMessage(
                        "Script saved with errors, check debug window!",
                        false);
                }
                try
                {
                    // DISPLAY ERROR INWORLD
                    string text = "Error compiling script:\n" +
                                  e.Message.ToString();
                    if (text.Length > 1100)
                    {
                        text = text.Substring(0, 1099);
                    }

                    World.SimChat(Utils.StringToBytes(text),
                                  ChatTypeEnum.DebugChannel, 2147483647,
                                  m_host.AbsolutePosition, m_host.Name, m_host.UUID,
                                  false);
                }
                catch (Exception e2) // LEGIT: User Scripting
                {
                    m_log.Error("[" +
                                m_scriptEngine.ScriptEngineName +
                                "]: Error displaying error in-world: " +
                                e2.ToString());
                    m_log.Error("[" +
                                m_scriptEngine.ScriptEngineName + "]: " +
                                "Errormessage: Error compiling script:\r\n" +
                                e2.Message.ToString());
                }
            }
        }
Exemplo n.º 9
0
        public ScriptInstance(IScriptEngine engine, SceneObjectPart part,
                              UUID itemID, UUID assetID, string assembly,
                              AppDomain dom, string primName, string scriptName,
                              int startParam, bool postOnRez, StateSource stateSource,
                              int maxScriptQueue)
        {
            m_Engine = engine;

            m_LocalID        = part.LocalId;
            m_ObjectID       = part.UUID;
            m_ItemID         = itemID;
            m_AssetID        = assetID;
            m_PrimName       = primName;
            m_ScriptName     = scriptName;
            m_Assembly       = assembly;
            m_StartParam     = startParam;
            m_MaxScriptQueue = maxScriptQueue;
            m_stateSource    = stateSource;
            m_postOnRez      = postOnRez;
            m_RegionID       = part.ParentGroup.Scene.RegionInfo.RegionID;

            if (part != null)
            {
                lock (part.TaskInventory)
                {
                    if (part.TaskInventory.ContainsKey(m_ItemID))
                    {
                        m_thisScriptTask = part.TaskInventory[m_ItemID];
                    }
                }
            }

            ApiManager am = new ApiManager();

            foreach (string api in am.GetApis())
            {
                m_Apis[api] = am.CreateApi(api);
                m_Apis[api].Initialize(engine, part, m_LocalID, itemID);
            }

            try
            {
                m_Script = (IScript)dom.CreateInstanceAndUnwrap(
                    Path.GetFileNameWithoutExtension(assembly),
                    "SecondLife.Script");

                // Add a sponsor to the script
//                ISponsor scriptSponsor = new ScriptSponsor();
//                ILease lease = (ILease)RemotingServices.GetLifetimeService(m_Script as MarshalByRefObject);
//                lease.Register(scriptSponsor);
                //m_ScriptSponsor = scriptSponsor;
            }
            catch (Exception e)
            {
                m_log.ErrorFormat("[Script] Error loading assembly {0}\n" + e.ToString(), assembly);
            }

            try
            {
                foreach (KeyValuePair <string, IScriptApi> kv in m_Apis)
                {
                    m_Script.InitApi(kv.Key, kv.Value);
                }

//                m_log.Debug("[Script] Script instance created");

                part.SetScriptEvents(m_ItemID,
                                     (int)m_Script.GetStateEventFlags(State));
            }
            catch (Exception e)
            {
                m_log.Error("[Script] Error loading script instance\n" + e.ToString());
                return;
            }

            m_SaveState = true;

            string savedState = Path.Combine(Path.GetDirectoryName(assembly),
                                             m_ItemID.ToString() + ".state");

            if (File.Exists(savedState))
            {
                string xml = String.Empty;

                try
                {
                    FileInfo fi   = new FileInfo(savedState);
                    int      size = (int)fi.Length;
                    if (size < 512000)
                    {
                        using (FileStream fs = File.Open(savedState,
                                                         FileMode.Open, FileAccess.Read, FileShare.None))
                        {
                            System.Text.ASCIIEncoding enc =
                                new System.Text.ASCIIEncoding();

                            Byte[] data = new Byte[size];
                            fs.Read(data, 0, size);

                            xml = enc.GetString(data);

                            ScriptSerializer.Deserialize(xml, this);

                            AsyncCommandManager.CreateFromData(m_Engine,
                                                               m_LocalID, m_ItemID, m_ObjectID,
                                                               PluginData);

//                            m_log.DebugFormat("[Script] Successfully retrieved state for script {0}.{1}", m_PrimName, m_ScriptName);

                            part.SetScriptEvents(m_ItemID,
                                                 (int)m_Script.GetStateEventFlags(State));

                            if (m_RunEvents && (!m_ShuttingDown))
                            {
                                m_RunEvents = false;
                            }
                            else
                            {
                                m_RunEvents   = false;
                                m_startOnInit = false;
                            }

                            // we get new rez events on sim restart, too
                            // but if there is state, then we fire the change
                            // event

                            // We loaded state, don't force a re-save
                            m_SaveState             = false;
                            m_startedFromSavedState = true;
                        }
                    }
                    else
                    {
                        m_log.Error("[Script] Unable to load script state: Memory limit exceeded");
                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat("[Script] Unable to load script state from xml: {0}\n" + e.ToString(), xml);
                }
            }
            else
            {
                ScenePresence presence = m_Engine.World.GetScenePresence(part.OwnerID);

                if (presence != null && (!postOnRez))
                {
                    presence.ControllingClient.SendAgentAlertMessage("Compile successful", false);
                }

//                m_log.ErrorFormat("[Script] Unable to load script state, file not found");
            }
        }