Exemplo n.º 1
0
        private void Initialise()
        {
            if (m_script != null)
            {
                return;
            }

            try
            {
                if (m_scriptConstructor == null)
                {
                    m_script = m_scriptFactory.CreateScript(m_scriptString, m_scriptContext, false, false);
                }
                else
                {
                    m_script      = m_scriptConstructor.Create(m_scriptString, m_scriptContext);
                    m_script.Line = m_scriptString;
                }
            }
            catch
            {
                if (!m_worldModel.EditMode)
                {
                    throw;
                }

                m_script = new FailedScript(m_scriptString);
                if (m_scriptConstructor == null)
                {
                    m_script = new MultiScript(m_scriptFactory.WorldModel, m_script);
                }
            }
            m_script.Parent = m_parent;
            m_scriptString  = null;
        }
Exemplo n.º 2
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            string  callback       = Utility.GetScript(script.Substring(Keyword.Length).Trim());
            IScript callbackScript = ScriptFactory.CreateScript(callback);

            return(new OnReadyScript(scriptContext, ScriptFactory, callbackScript));
        }
Exemplo n.º 3
0
        public IScript Create(string script, Element proc)
        {
            string  callback       = Utility.GetScript(script.Substring(8).Trim());
            IScript callbackScript = ScriptFactory.CreateScript(callback);

            return(new OnReadyScript(ScriptFactory, callbackScript));
        }
        public IScript Create(string script, Element proc)
        {
            // Get script after "firsttime" keyword
            script = script.Substring(9).Trim();
            string  firstTime       = Utility.GetScript(script);
            IScript firstTimeScript = ScriptFactory.CreateScript(firstTime);

            return(new FirstTimeScript(firstTimeScript));
        }
Exemplo n.º 5
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            // Get script after "firsttime" keyword
            script = script.Substring(9).Trim();
            string  firstTime       = Utility.GetScript(script);
            IScript firstTimeScript = ScriptFactory.CreateScript(firstTime);

            return(new FirstTimeScript(WorldModel, ScriptFactory, firstTimeScript));
        }
Exemplo n.º 6
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            string  afterExpr;
            string  param      = Utility.GetParameter(script, out afterExpr);
            string  loop       = Utility.GetScript(afterExpr);
            IScript loopScript = ScriptFactory.CreateScript(loop);

            return(new WhileScript(scriptContext, ScriptFactory, new Expression <bool>(param, scriptContext), loopScript));
        }
Exemplo n.º 7
0
        public IScript Create(string script, Element proc)
        {
            string  afterExpr;
            string  param      = Utility.GetParameter(script, out afterExpr);
            string  loop       = Utility.GetScript(afterExpr);
            IScript loopScript = ScriptFactory.CreateScript(loop);

            return(new WhileScript(new Expression(param, GameLoader), loopScript));
        }
Exemplo n.º 8
0
        public IScript Create(string script, Element proc)
        {
            string afterExpr;
            string expr = Utility.GetParameter(script, out afterExpr);
            string then = Utility.GetScript(afterExpr);

            IScript thenScript = ScriptFactory.CreateScript(then, proc);

            return(new IfScript(new Expression(expr, GameLoader), thenScript));
        }
Exemplo n.º 9
0
        public void LoadCode(string code)
        {
            var newScript     = (IMultiScript)ScriptFactory.CreateScript(code);
            var newScriptList = new List <IScript>(newScript.Scripts);

            if (base.UndoLog != null)
            {
                base.UndoLog.AddUndoAction(new UndoMultiScriptLoadCode(this, m_scripts, newScriptList));
            }
            ReplaceScripts(newScriptList);
        }
Exemplo n.º 10
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            List <IFunction <object> > paramExpressions = null;
            string procName, afterParameter;

            string  param = Utility.GetParameter(script, out afterParameter);
            IScript paramScript = null;

            // Handle functions of the form
            //    SomeFunction (parameter) { script }
            if (afterParameter != null)
            {
                afterParameter = afterParameter.Trim();
                if (afterParameter.Length > 0)
                {
                    string paramScriptString = Utility.GetScript(afterParameter);
                    paramScript = ScriptFactory.CreateScript(paramScriptString);
                }
            }

            if (param == null && paramScript == null)
            {
                procName = script;
            }
            else
            {
                if (param != null)
                {
                    List <string> parameters = Utility.SplitParameter(param);
                    procName         = script.Substring(0, script.IndexOf('(')).Trim();
                    paramExpressions = new List <IFunction <object> >();
                    if (param.Trim().Length > 0)
                    {
                        foreach (string s in parameters)
                        {
                            paramExpressions.Add(new Expression <object>(s, scriptContext));
                        }
                    }
                }
                else
                {
                    procName = script.Substring(0, script.IndexOfAny(new char[] { '{', ' ' }));
                }
            }

            if (!WorldModel.EditMode && WorldModel.Procedure(procName) == null)
            {
                throw new Exception(string.Format("Function not found: '{0}'", procName));
            }
            else
            {
                return(new FunctionCallScript(WorldModel, procName, paramExpressions, paramScript));
            }
        }
Exemplo n.º 11
0
        private List <Tuple <List <IFunction>, IScript> > ProcessCases(string cases, out IScript defaultScript, Element proc)
        {
            bool   finished = false;
            string remainingCases;
            string afterExpr;
            var    result = new List <Tuple <List <IFunction>, IScript> >();

            defaultScript = null;

            cases = Utility.RemoveSurroundingBraces(cases);

            while (!finished)
            {
                cases = Utility.GetScript(cases, out remainingCases);
                if (cases != null)
                {
                    cases = cases.Trim();
                }

                if (!string.IsNullOrEmpty(cases))
                {
                    if (cases.StartsWith("case"))
                    {
                        string  expr       = Utility.GetParameter(cases, out afterExpr);
                        string  caseScript = Utility.GetScript(afterExpr);
                        IScript script     = ScriptFactory.CreateScript(caseScript, proc);

                        var matchList   = Utility.SplitParameter(expr);
                        var expressions = matchList.Select(match => new Expression(match, GameLoader)).Cast <IFunction>().ToList();

                        result.Add(Tuple.Create(expressions, script));
                    }
                    else if (cases.StartsWith("default"))
                    {
                        defaultScript = ScriptFactory.CreateScript(cases.Substring(8).Trim());
                    }
                    else
                    {
                        throw new Exception(string.Format("Invalid inside switch block: '{0}'", cases));
                    }
                }

                cases = remainingCases;
                if (string.IsNullOrEmpty(cases))
                {
                    finished = true;
                }
            }

            return(result);
        }
Exemplo n.º 12
0
        public IScript Create(string script, Element proc)
        {
            bool isScript = false;
            int  offset   = 0;
            int  eqPos;

            // hide text within string expressions
            string obscuredScript = Utility.ObscureStrings(script);
            int    bracePos       = obscuredScript.IndexOf('{');

            if (bracePos != -1)
            {
                // only want to look for = and => before any other scripts which may
                // be defined on the same line, for example procedure calls of type
                //     MyProcedureCall (5) { some other script }

                obscuredScript = obscuredScript.Substring(0, bracePos);
            }

            eqPos = obscuredScript.IndexOf("=>");
            if (eqPos != -1)
            {
                isScript = true;
                offset   = 1;
            }
            else
            {
                eqPos = obscuredScript.IndexOf('=');
            }

            if (eqPos != -1)
            {
                string appliesTo = script.Substring(0, eqPos);
                string value     = script.Substring(eqPos + 1 + offset).Trim();

                string    variable;
                IFunction expr = GetAppliesTo(appliesTo, out variable);

                if (!isScript)
                {
                    return(new SetExpressionScript(this, expr, variable, new Expression(value, GameLoader), GameLoader));
                }
                else
                {
                    return(new SetScriptScript(this, expr, variable, ScriptFactory.CreateScript(value), GameLoader));
                }
            }

            return(null);
        }
Exemplo n.º 13
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            string afterExpr;
            string param    = Utility.GetParameter(script, out afterExpr);
            string callback = Utility.GetScript(afterExpr);

            string[] parameters = Utility.SplitParameter(param).ToArray();
            if (parameters.Count() != 1)
            {
                throw new Exception(string.Format("'ask' script should have 1 parameter: 'ask ({0})'", param));
            }
            IScript callbackScript = ScriptFactory.CreateScript(callback);

            return(new AskScript(scriptContext, ScriptFactory, new Expression <string>(parameters[0], scriptContext), callbackScript));
        }
Exemplo n.º 14
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            string afterExpr;
            string param = Utility.GetParameter(script, out afterExpr);
            string loop  = Utility.GetScript(afterExpr);

            string[] parameters = Utility.SplitParameter(param).ToArray();
            if (parameters.Count() != 2)
            {
                throw new Exception(string.Format("'foreach' script should have 2 parameters: 'foreach ({0})'", param));
            }
            IScript loopScript = ScriptFactory.CreateScript(loop);

            return(new ForEachScript(scriptContext, parameters[0], new ExpressionGeneric(parameters[1], scriptContext), loopScript));
        }
Exemplo n.º 15
0
        public IScript Create(string script, Element proc)
        {
            string afterExpr;
            string param    = Utility.GetParameter(script, out afterExpr);
            string callback = Utility.GetScript(afterExpr);

            string[] parameters = Utility.SplitParameter(param).ToArray();
            if (parameters.Count() != 3)
            {
                throw new Exception(string.Format("'show menu' script should have 3 parameters: 'show menu ({0})'", param));
            }
            IScript callbackScript = ScriptFactory.CreateScript(callback);

            return(new ShowMenuScript(ScriptFactory, new Expression(parameters[0], GameLoader), new Expression(parameters[1], GameLoader), new Expression(parameters[2], GameLoader), callbackScript));
        }
        public IScript Create(string script, Element proc)
        {
            List <IFunction> paramExpressions = null;
            string           procName, afterParameter;

            string  param = Utility.GetParameter(script, out afterParameter);
            IScript paramScript = null;

            // Handle functions of the form
            //    SomeFunction (parameter) { script }
            if (afterParameter != null)
            {
                afterParameter = afterParameter.Trim();
                if (afterParameter.Length > 0)
                {
                    string paramScriptString = Utility.GetScript(afterParameter);
                    paramScript = ScriptFactory.CreateScript(paramScriptString);
                }
            }

            if (param == null && paramScript == null)
            {
                procName = script;
            }
            else
            {
                if (param != null)
                {
                    List <string> parameters = Utility.SplitParameter(param);
                    procName         = script.Substring(0, script.IndexOf('(')).Trim();
                    paramExpressions = new List <IFunction>();
                    if (param.Trim().Length > 0)
                    {
                        foreach (string s in parameters)
                        {
                            paramExpressions.Add(new Expression(s, GameLoader));
                        }
                    }
                }
                else
                {
                    procName = script.Substring(0, script.IndexOfAny(new char[] { '{', ' ' }));
                }
            }

            return(new FunctionCallScript(GameLoader, procName, paramExpressions, paramScript));
        }
Exemplo n.º 17
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            string afterExpr;
            string expr = Utility.GetParameter(script, out afterExpr);

            if (afterExpr.StartsWith(")"))
            {
                // We have a mismatch of brackets in the expression
                throw new Exception("Too many ')'");
            }

            string then = Utility.GetScript(afterExpr);

            IScript thenScript = ScriptFactory.CreateScript(then, scriptContext);

            return(new IfScript(new Expression <bool>(expr, scriptContext), thenScript, scriptContext));
        }
Exemplo n.º 18
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            string afterExpr;
            string param = Utility.GetParameter(script, out afterExpr);
            string loop  = Utility.GetScript(afterExpr);

            string[] parameters = Utility.SplitParameter(param).ToArray();
            IScript  loopScript = ScriptFactory.CreateScript(loop);

            if (parameters.Count() == 3)
            {
                return(new ForScript(scriptContext, ScriptFactory, parameters[0], new Expression <int>(parameters[1], scriptContext), new Expression <int>(parameters[2], scriptContext), loopScript));
            }
            else if (parameters.Count() == 4)
            {
                return(new ForScript(scriptContext, ScriptFactory, parameters[0], new Expression <int>(parameters[1], scriptContext), new Expression <int>(parameters[2], scriptContext), new Expression <int>(parameters[3], scriptContext), loopScript));
            }
            else
            {
                throw new Exception(string.Format("'for' script should have 3 or 4 parameters: 'for ({0})'", param));
            }
        }
Exemplo n.º 19
0
        private static void Main(string[] args)
        {
            if (args.Length > 1)
            {
                Console.WriteLine("Usage: FiveTool [script path]");
                return;
            }

            var scriptPath  = args.Length > 0 ? args[0] : null;
            var configPath  = Config.DefaultPath;
            var consoleMode = scriptPath == null;

            if (!LoadConfig(configPath))
            {
                Console.Error.WriteLine($"Failed to load configuration data from {configPath}! Using defaults.");
            }

            if (consoleMode)
            {
                Console.ForegroundColor = ConsoleColor.White;
                var version = Assembly.GetExecutingAssembly().GetName().Version;
                Console.WriteLine("FiveTool [{0}.{1}.{2}]", version.Major, version.Minor, version.Revision);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Please submit bug reports, pull requests, and feature requests to");
                Console.WriteLine("<https://github.com/Shockfire/FiveTool>.");
                Console.WriteLine();
            }

            if (EnsureGameRootIsSet())
            {
                if (consoleMode)
                {
                    Console.WriteLine($"Using game files in {Config.Current.GameRoot}.");
                    Console.Write("You can use ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("ChooseGameFolder()");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(" to change this folder at any time.");
                    Console.WriteLine();
                }
            }
            else
            {
                Console.Error.WriteLine("You must set a valid game folder in order to use FiveTool.");
                if (consoleMode)
                {
                    Console.WriteLine("Press any key to quit...");
                    Console.ReadKey(true);
                }
                return;
            }

            if (consoleMode)
            {
                Console.WriteLine("Starting interactive Lua (MoonSharp) shell.");
                Console.Write("Use ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Help()");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write(" for help and ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Exit()");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(" to quit.");
                Console.WriteLine();
            }

            ScriptFactory.Initialize();
            var script = ScriptFactory.CreateScript();

            // If a script file was passed in, run it
            if (scriptPath != null)
            {
                try
                {
                    script.DoFile(scriptPath);
                }
                catch (InterpreterException e)
                {
                    Console.Error.WriteLine("Error: " + e.DecoratedMessage);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Error: " + e.Message);
                }
            }
            if (!consoleMode)
            {
                return;
            }

            var done = false;

            script.Globals["Exit"] = (Action)(() => { done = true; });

            while (!done)
            {
                try
                {
                    var func   = ReadFunction(script);
                    var result = func.Function.Call();
                    if (result.IsNotVoid())
                    {
                        // Dump without recursion to display the value in a friendly format
                        DumpBuiltIns.Dump(script, result, 0);
                        Console.WriteLine();
                    }
                }
                catch (InterpreterException e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(e.DecoratedMessage);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 20
0
 /// <summary>
 ///     Reads a script source and builds the corresponding <see cref="IScript" /> object;
 /// </summary>
 /// <param name="source">Text of the script</param>
 /// <returns>The corresponding <see cref="IScript" /></returns>
 public IScript LoadScript(string source)
 {
     return(ScriptFactory.CreateScript(ReaderFactory.MakeReader(source), DefaultScope));
 }
Exemplo n.º 21
0
        private Dictionary <IFunctionGeneric, IScript> ProcessCases(string cases, out IScript defaultScript, ScriptContext scriptContext)
        {
            bool   finished = false;
            string remainingCases;
            string afterExpr;
            Dictionary <IFunctionGeneric, IScript> result = new Dictionary <IFunctionGeneric, IScript>();

            defaultScript = null;

            cases = Utility.RemoveSurroundingBraces(cases);

            while (!finished)
            {
                cases = Utility.GetScript(cases, out remainingCases);
                if (cases != null)
                {
                    cases = cases.Trim();
                }

                if (!string.IsNullOrEmpty(cases))
                {
                    if (cases.StartsWith("case"))
                    {
                        string  expr       = Utility.GetParameter(cases, out afterExpr);
                        string  caseScript = Utility.GetScript(afterExpr);
                        IScript script     = ScriptFactory.CreateScript(caseScript, scriptContext);

                        // Case expression can have multiple values separated by commas. In Edit mode,
                        // just load this as one expression for editing.

                        if (!scriptContext.WorldModel.EditMode)
                        {
                            var matchList = Utility.SplitParameter(expr);
                            foreach (var match in matchList)
                            {
                                result.Add(new ExpressionGeneric(match, scriptContext), script);
                            }
                        }
                        else
                        {
                            result.Add(new ExpressionGeneric(expr, scriptContext), script);
                        }
                    }
                    else if (cases.StartsWith("default"))
                    {
                        defaultScript = ScriptFactory.CreateScript(cases.Substring(8).Trim());
                    }
                    else
                    {
                        throw new Exception(string.Format("Invalid inside switch block: '{0}'", cases));
                    }
                }

                cases = remainingCases;
                if (string.IsNullOrEmpty(cases))
                {
                    finished = true;
                }
            }

            return(result);
        }
Exemplo n.º 22
0
 private IScript GetElse(string elseScript, Element proc)
 {
     elseScript = Utility.GetTextAfter(elseScript, "else");
     return(ScriptFactory.CreateScript(elseScript, proc));
 }
Exemplo n.º 23
0
        public IScript Create(string script, ScriptContext scriptContext)
        {
            bool isScript = false;
            int  offset   = 0;
            int  eqPos;

            // hide text within string expressions
            string obscuredScript = Utility.ObscureStrings(script);
            int    bracePos       = obscuredScript.IndexOf('{');

            if (bracePos != -1)
            {
                // only want to look for = and => before any other scripts which may
                // be defined on the same line, for example procedure calls of type
                //     MyProcedureCall (5) { some other script }

                obscuredScript = obscuredScript.Substring(0, bracePos);
            }

            eqPos = obscuredScript.IndexOf("=>");
            if (eqPos != -1)
            {
                isScript = true;
                offset   = 1;
            }
            else
            {
                eqPos = obscuredScript.IndexOf('=');
            }

            if (eqPos != -1)
            {
                string appliesTo = script.Substring(0, eqPos);
                string value     = script.Substring(eqPos + 1 + offset).Trim();

                string variable;
                IFunction <Element> expr = GetAppliesTo(scriptContext, appliesTo, out variable);

                if (!WorldModel.EditMode && WorldModel.Version >= WorldModelVersion.v530)
                {
                    if (!Utility.IsValidAttributeName(variable))
                    {
                        string error = string.Format("Invalid {0} name '{1}' in '{2}'",
                                                     expr == null ? "variable" : "attribute",
                                                     variable,
                                                     script);
                        throw new Exception(error);
                    }
                }

                if (!isScript)
                {
                    return(new SetExpressionScript(this, scriptContext, expr, variable, new Expression <object>(value, scriptContext)));
                }
                else
                {
                    return(new SetScriptScript(this, scriptContext, expr, variable, ScriptFactory.CreateScript(value)));
                }
            }

            return(null);
        }
Exemplo n.º 24
0
 private IScript GetElse(string elseScript, ScriptContext scriptContext)
 {
     elseScript = Utility.GetTextAfter(elseScript, "else");
     return(ScriptFactory.CreateScript(elseScript, scriptContext));
 }
Exemplo n.º 25
0
        private static void Main(string[] args)
        {
            if (args.Length > 1)
            {
                Console.WriteLine("Usage: FiveTool [script path]");
                return;
            }

            var scriptPath = args.Length > 0 ? args[0] : null;

            if (scriptPath == null)
            {
                Console.ForegroundColor = ConsoleColor.White;
                var version = Assembly.GetExecutingAssembly().GetName().Version;
                Console.WriteLine("FiveTool [{0}.{1}.{2}]", version.Major, version.Minor, version.Revision);
                Console.WriteLine();

                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Please submit bug reports, pull requests, and feature requests to");
                Console.WriteLine("<https://github.com/Shockfire/FiveTool>.");
                Console.WriteLine();

                Console.WriteLine("Starting interactive Lua (MoonSharp) shell.");
                Console.Write("Use ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Help()");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write(" for help and ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Exit()");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(" to quit.");
                Console.WriteLine();
            }

            ScriptFactory.Initialize();
            var script = ScriptFactory.CreateScript();

            // If a script file was passed in, run it and return
            if (scriptPath != null)
            {
                try
                {
                    script.DoFile(scriptPath);
                }
                catch (InterpreterException e)
                {
                    Console.Error.WriteLine("Error: " + e.DecoratedMessage);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Error: " + e.Message);
                }
                return;
            }

            var done = false;

            script.Globals["Exit"] = (Action)(() => { done = true; });

            while (!done)
            {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write("lua> ");
                Console.ForegroundColor = ConsoleColor.White;
                var line = Console.ReadLine();
                Console.ForegroundColor = ConsoleColor.Gray;
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                try
                {
                    // HACK: Stick a return statement in front of the line first, and then try without it if there's a syntax error
                    // Otherwise users would need to manually type "return" in front of most lines
                    DynValue result;
                    try
                    {
                        result = script.DoString("return " + line);
                    }
                    catch (SyntaxErrorException)
                    {
                        result = script.DoString(line);
                    }
                    if (result.IsNotVoid())
                    {
                        // Dump without recursion to display the value in a friendly format
                        DumpBuiltIns.Dump(script, result, 0);
                        Console.WriteLine();
                    }
                }
                catch (InterpreterException e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(e.DecoratedMessage);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine();
            }
        }