Esempio n. 1
0
            private void AsyncStart()
            {
                if (World.Player == null)
                {
                    return;
                }

                try
                {
                    string fullpath = Settings.GetFullPathForScript(m_Filename);
                    string ext      = Path.GetExtension(fullpath);

                    if (ext.Equals(".cs", StringComparison.InvariantCultureIgnoreCase))
                    {
                        CSharpEngine csharpEngine  = CSharpEngine.Instance;
                        bool         compileErrors = csharpEngine.CompileFromFile(fullpath, true, out StringBuilder compileMessages, out Assembly assembly);
                        if (compileMessages.Length > 0)
                        {
                            Misc.SendMessage(compileMessages.ToString());
                        }
                        if (compileErrors == true)
                        {
                            Stop();
                            return;
                        }
                        csharpEngine.Execute(assembly);
                    }
                    else if (ext.Equals(".uos", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // Using // only will be deprecated instead of //UOS
                        var text = System.IO.File.ReadAllLines(fullpath);
                        if ((text[0].Substring(0, 2) == "//") && text[0].Length < 5)
                        {
                            string message = "WARNING: // header for UOS scripts is going to be deprecated. Please use //UOS instead";
                            Misc.SendMessage(message);
                        }

                        UOSteamEngine uosteam = UOSteamEngine.Instance;
                        uosteam.Execute(fullpath);
                    }
                    else
                    {
                        DateTime lastModified = System.IO.File.GetLastWriteTime(fullpath);
                        if (FileChangeDate < lastModified)
                        {
                            ReadText(fullpath);
                            FileChangeDate = System.IO.File.GetLastWriteTime(fullpath);
                            Create(null);
                        }

                        /*Dalamar: BEGIN "fix python env" */
                        //EXECUTION OF THE SCRIPT
                        //Refactoring option, the whole block can be replaced by:
                        //
                        //m_pe.Execute(m_Text);

                        m_Source = m_Engine.CreateScriptSourceFromString(m_Text);
                        // "+": USE PythonCompilerOptions in order to initialize Python modules correctly, without it the Python env is half broken
                        PythonCompilerOptions pco = (PythonCompilerOptions)m_Engine.GetCompilerOptions(m_Scope);
                        pco.ModuleName = "__main__";
                        pco.Module    |= ModuleOptions.Initialize;
                        CompiledCode compiled = m_Source.Compile(pco);
                        Journal      journal  = m_Engine.Runtime.Globals.GetVariable("Journal") as Journal;
                        journal.Active = true;
                        compiled.Execute(m_Scope);
                        journal.Active = false;
                        // "-": DONT execute directly, unless you are not planning to import external modules.
                        //m_Source.Execute(m_Scope);

                        /*Dalamar: END*/
                    }
                }
                catch (IronPython.Runtime.Exceptions.SystemExitException)
                {
                    Stop();
                    // sys.exit - terminate the thread
                }
                catch (Exception ex)
                {
                    if (ex is System.Threading.ThreadAbortException)
                    {
                        return;
                    }

                    string display_error = ex.Message;
                    if (m_Engine != null)
                    {
                        display_error = m_Engine.GetService <ExceptionOperations>().FormatException(ex);
                    }
                    SendMessageScriptError("ERROR " + m_Filename + ":" + display_error.Replace("\n", " | "));

                    if (ScriptErrorLog) // enabled log of error
                    {
                        StringBuilder log = new StringBuilder();
                        log.Append(Environment.NewLine + "============================ START REPORT ============================ " + Environment.NewLine);

                        DateTime dt = DateTime.Now;
                        log.Append("---> Time: " + String.Format("{0:F}", dt) + Environment.NewLine);
                        log.Append(Environment.NewLine);

                        if (ex is SyntaxErrorException)
                        {
                            SyntaxErrorException se = ex as SyntaxErrorException;
                            log.Append("----> Syntax Error:" + Environment.NewLine);
                            log.Append("-> LINE: " + se.Line + Environment.NewLine);
                            log.Append("-> COLUMN: " + se.Column + Environment.NewLine);
                            log.Append("-> SEVERITY: " + se.Severity + Environment.NewLine);
                            log.Append("-> MESSAGE: " + se.Message + Environment.NewLine);
                        }
                        else
                        {
                            log.Append("----> Generic Error:" + Environment.NewLine);
                            log.Append(display_error);
                        }

                        log.Append(Environment.NewLine);
                        log.Append("============================ END REPORT ============================ ");
                        log.Append(Environment.NewLine);

                        try // For prevent crash in case of file are busy or inaccessible
                        {
                            File.AppendAllText(Assistant.Engine.RootPath + "\\" + m_Filename + ".ERROR", log.ToString());
                        }
                        catch { }
                        log.Clear();
                    }
                }
            }
Esempio n. 2
0
            private void AsyncStart()
            {
                if (World.Player == null)
                {
                    return;
                }

                try
                {
                    string fullpath = Path.Combine(Assistant.Engine.RootPath, "Scripts", m_Filename);
                    string ext      = Path.GetExtension(fullpath);
                    if (ext.Equals(".uos", StringComparison.InvariantCultureIgnoreCase))
                    {
                        UOSteamEngine uosteam = new UOSteamEngine();
                        uosteam.Execute(fullpath);
                    }
                    else
                    {
                        DateTime lastModified = System.IO.File.GetLastWriteTime(fullpath);
                        if (FileChangeDate < lastModified)
                        {
                            ReadText();
                            FileChangeDate = System.IO.File.GetLastWriteTime(fullpath);
                            Create(null);
                        }


                        /*Dalamar: BEGIN "fix python env" */
                        //EXECUTION OF THE SCRIPT
                        //Refactoring option, the whole block can be replaced by:
                        //
                        //m_pe.Execute(m_Text);

                        m_Source = m_Engine.CreateScriptSourceFromString(m_Text);
                        // "+": USE PythonCompilerOptions in order to initialize Python modules correctly, without it the Python env is half broken
                        PythonCompilerOptions pco = (PythonCompilerOptions)m_Engine.GetCompilerOptions(m_Scope);
                        pco.ModuleName = "__main__";
                        pco.Module    |= ModuleOptions.Initialize;
                        CompiledCode compiled = m_Source.Compile(pco);
                        compiled.Execute(m_Scope);

                        // "-": DONT execute directly, unless you are not planning to import external modules.
                        //m_Source.Execute(m_Scope);

                        /*Dalamar: END*/
                    }
                }
                catch (IronPython.Runtime.Exceptions.SystemExitException ex)
                {
                    Stop();
                    // sys.exit - terminate the thread
                }
                catch (Exception ex)
                {
                    if (ex is System.Threading.ThreadAbortException)
                    {
                        return;
                    }

                    string display_error = m_Engine.GetService <ExceptionOperations>().FormatException(ex);

                    SendMessageScriptError("ERROR " + m_Filename + ":" + display_error.Replace("\n", " | "));

                    if (ScriptErrorLog) // enabled log of error
                    {
                        StringBuilder log = new StringBuilder();
                        log.Append(Environment.NewLine + "============================ START REPORT ============================ " + Environment.NewLine);

                        DateTime dt = DateTime.Now;
                        log.Append("---> Time: " + String.Format("{0:F}", dt) + Environment.NewLine);
                        log.Append(Environment.NewLine);

                        if (ex is SyntaxErrorException)
                        {
                            SyntaxErrorException se = ex as SyntaxErrorException;
                            log.Append("----> Syntax Error:" + Environment.NewLine);
                            log.Append("-> LINE: " + se.Line + Environment.NewLine);
                            log.Append("-> COLUMN: " + se.Column + Environment.NewLine);
                            log.Append("-> SEVERITY: " + se.Severity + Environment.NewLine);
                            log.Append("-> MESSAGE: " + se.Message + Environment.NewLine);
                        }
                        else
                        {
                            log.Append("----> Generic Error:" + Environment.NewLine);
                            ExceptionOperations eo = m_Engine.GetService <ExceptionOperations>();
                            string error           = eo.FormatException(ex);
                            log.Append(error);
                        }

                        log.Append(Environment.NewLine);
                        log.Append("============================ END REPORT ============================ ");
                        log.Append(Environment.NewLine);

                        try // For prevent crash in case of file are busy or inaccessible
                        {
                            File.AppendAllText(Assistant.Engine.RootPath + "\\" + m_Filename + ".ERROR", log.ToString());
                        }
                        catch { }
                        log.Clear();
                    }
                }
            }