示例#1
0
        public void TestMultiScriptCreation()
        {
            MultiScript multiScript = CreateMultiScript("line 1", "line 2", "line 3", "line 4");

            Assert.AreEqual(4, multiScript.Scripts.Count());
            Assert.AreEqual("line 2", multiScript.Scripts.ElementAt(1).Line);
            Assert.AreEqual("line 1;line 2;line 3;line 4", GetLinesString(multiScript));
        }
示例#2
0
        public void TestMultiScriptSwap()
        {
            MultiScript multiScript = CreateMultiScript("line 1", "line 2", "line 3", "line 4");

            // Swap lines 2 and 3

            m_worldModel.UndoLogger.StartTransaction("Swap lines 2 and 3");
            multiScript.Swap(1, 2);
            m_worldModel.UndoLogger.EndTransaction();

            // Check they are swapped correctly

            Assert.AreEqual("line 1;line 3;line 2;line 4", GetLinesString(multiScript));

            // Undo - should be back to original

            m_worldModel.UndoLogger.Undo();
            Assert.AreEqual("line 1;line 2;line 3;line 4", GetLinesString(multiScript));

            // Redo - lines 2 and 3 swapped again

            m_worldModel.UndoLogger.Redo();
            Assert.AreEqual("line 1;line 3;line 2;line 4", GetLinesString(multiScript));

            // Undo - should be back to original

            m_worldModel.UndoLogger.Undo();
            Assert.AreEqual("line 1;line 2;line 3;line 4", GetLinesString(multiScript));

            // Now swap two non-consecutive elements, lines 1 and 4

            m_worldModel.UndoLogger.StartTransaction("Swap lines 1 and 4");
            multiScript.Swap(0, 3);
            m_worldModel.UndoLogger.EndTransaction();

            // Check they are swapped correctly

            Assert.AreEqual("line 4;line 2;line 3;line 1", GetLinesString(multiScript));

            // Undo - should be back to original

            m_worldModel.UndoLogger.Undo();
            Assert.AreEqual("line 1;line 2;line 3;line 4", GetLinesString(multiScript));
        }
示例#3
0
        private MultiScript CreateMultiScript(params string[] lines)
        {
            // Creates a new MultiScript with mock IScripts, with .Line returning each
            // line passed in as a parameter.

            List <IScript> source = new List <IScript>();

            foreach (string line in lines)
            {
                var script = new Mock <IScript>();
                script.Setup(s => s.Line).Returns(line);
                source.Add(script.Object);
            }

            MultiScript result = new MultiScript(m_worldModel, source.ToArray());

            result.UndoLog = m_worldModel.UndoLogger;

            return(result);
        }
示例#4
0
        public IScript CreateScript(string line, Element proc)
        {
            string              remainingScript;
            IScript             newScript;
            MultiScript         result        = new MultiScript();
            bool                finished      = false;
            IScript             lastIf        = null;
            IScript             lastFirstTime = null;
            bool                dontAdd;
            bool                addedError;
            IfScriptConstructor ifConstructor = null;

            line = Utility.RemoveSurroundingBraces(line);

            while (!finished)
            {
                line = Utility.GetScript(line, out remainingScript);
                if (line != null)
                {
                    line = line.Trim();
                    line = Utility.RemoveComments(line);
                }

                if (!string.IsNullOrEmpty(line))
                {
                    newScript  = null;
                    dontAdd    = false;
                    addedError = false;

                    if (line.StartsWith("else"))
                    {
                        if (lastIf == null)
                        {
                            AddError("Unexpected 'else' (error with parent 'if'?):" + line);
                        }
                        else
                        {
                            if (line.StartsWith("else if"))
                            {
                                ifConstructor.AddElseIf(lastIf, line, proc);
                            }
                            else
                            {
                                ifConstructor.AddElse(lastIf, line, proc);
                            }
                        }
                        dontAdd = true;
                    }
                    else if (line.StartsWith("otherwise"))
                    {
                        if (lastFirstTime == null)
                        {
                            AddError("Unexpected 'otherwise' (error with parent 'firsttime'?):" + line);
                        }
                        else
                        {
                            FirstTimeScriptConstructor.AddOtherwiseScript(lastFirstTime, line, this);
                        }
                        dontAdd = true;
                    }
                    else
                    {
                        lastIf = null;
                        IScriptConstructor constructor = GetScriptConstructor(line);

                        if (constructor != null)
                        {
                            try
                            {
                                newScript = constructor.Create(line, proc);
                                if (constructor.Keyword == "if")
                                {
                                    ifConstructor = (IfScriptConstructor)constructor;
                                    lastIf        = newScript;
                                }
                                if (constructor.Keyword == "firsttime")
                                {
                                    lastFirstTime = newScript;
                                }
                            }
                            catch (Exception ex)
                            {
                                AddError(string.Format("Error adding script '{0}': {1}", line, ex.Message));
                                addedError = true;
                            }
                        }

                        if (newScript == null)
                        {
                            // See if the script is like "myvar = 2". newScript will be null otherwise.
                            newScript = m_setConstructor.Create(line, proc);
                        }

                        if (newScript == null)
                        {
                            // See if the script calls a procedure defined by the game
                            newScript = m_procConstructor.Create(line, proc);
                        }
                    }

                    if (!dontAdd)
                    {
                        if (newScript == null)
                        {
                            if (!addedError)
                            {
                                AddError(string.Format("Unrecognised script command '{0}'", line));
                            }
                        }
                        else
                        {
                            newScript.Line = line;
                            result.Add(newScript);
                        }
                    }
                }

                line = remainingScript;
                if (string.IsNullOrEmpty(line))
                {
                    finished = true;
                }
            }

            return(result);
        }
示例#5
0
 private string GetLinesString(MultiScript multiScript)
 {
     return(string.Join(";", multiScript.Scripts.Select(s => s.Line)));
 }
示例#6
0
 public void AddElseIf()
 {
     IScript       newScript = new MultiScript(Controller.WorldModel);
     IElseIfScript newElseIf = m_ifScript.AddElseIf(string.Empty, newScript);
 }
示例#7
0
        public void AddElse()
        {
            IScript newScript = new MultiScript(Controller.WorldModel);

            m_ifScript.SetElse(newScript);
        }
示例#8
0
        public IScript CreateScript(string line, ScriptContext scriptContext, bool lazy, bool addExceptionsToLog = true)
        {
            MultiScript         result        = new MultiScript(m_worldModel);
            bool                finished      = false;
            IScript             lastIf        = null;
            IScript             lastComment   = null;
            IScript             lastFirstTime = null;
            IfScriptConstructor ifConstructor = null;

            if (lazy)
            {
                return(new LazyLoadScript(this, line, scriptContext));
            }

            line = Utility.RemoveSurroundingBraces(line);
            line = Utility.RemoveComments(line, m_worldModel.EditMode);

            while (!finished)
            {
                string remainingScript;
                try
                {
                    line = Utility.GetScript(line, out remainingScript);
                }
                catch (Exception ex)
                {
                    if (addExceptionsToLog)
                    {
                        AddError(string.Format("Error adding script '{0}': {1}", line, ex.Message));
                        break;
                    }
                    throw;
                }

                if (line != null)
                {
                    line = line.Trim();
                }

                if (!string.IsNullOrEmpty(line))
                {
                    IScript newScript  = null;
                    bool    dontAdd    = false;
                    bool    addedError = false;

                    if (line.StartsWith("else"))
                    {
                        if (lastIf == null)
                        {
                            AddError("Unexpected 'else' (error with parent 'if'?):" + line);
                        }
                        else
                        {
                            if (line.StartsWith("else if"))
                            {
                                ifConstructor.AddElseIf(lastIf, line, scriptContext);
                            }
                            else
                            {
                                ifConstructor.AddElse(lastIf, line, scriptContext);
                            }
                        }
                        dontAdd = true;
                    }
                    else if (line.StartsWith("otherwise"))
                    {
                        if (lastFirstTime == null)
                        {
                            AddError("Unexpected 'otherwise' (error with parent 'firsttime'?):" + line);
                        }
                        else
                        {
                            FirstTimeScriptConstructor.AddOtherwiseScript(lastFirstTime, line, this);
                        }
                        dontAdd = true;
                    }
                    else
                    {
                        IScriptConstructor constructor = GetScriptConstructor(line);

                        if (constructor is CommentScriptConstructor)
                        {
                            if (lastComment != null)
                            {
                                ((CommentScript)lastComment).AddLine(line);
                                dontAdd = true;
                            }
                            else
                            {
                                newScript   = constructor.Create(line, scriptContext);
                                lastComment = newScript;
                            }
                        }
                        else
                        {
                            lastIf        = null;
                            lastComment   = null;
                            lastFirstTime = null;

                            if (constructor != null)
                            {
                                try
                                {
                                    if (m_lazyLoadingEnabled)
                                    {
                                        newScript = new LazyLoadScript(this, constructor, line, scriptContext);
                                    }
                                    else
                                    {
                                        newScript = constructor.Create(line, scriptContext);
                                    }

                                    if (constructor.Keyword == "if")
                                    {
                                        ifConstructor = (IfScriptConstructor)constructor;
                                        lastIf        = newScript;
                                    }
                                    if (constructor.Keyword == "firsttime")
                                    {
                                        lastFirstTime = newScript;
                                    }
                                }
                                catch (Ciloci.Flee.ExpressionCompileException ex)
                                {
                                    AddError(string.Format("Error compiling expression in '{0}': {1}", line, ex.Message));
                                    addedError = true;
                                }
                                catch (Exception ex)
                                {
                                    AddError(string.Format("Error adding script '{0}': {1}", line, ex.Message));
                                    addedError = true;
                                }
                            }

                            if (!addedError)
                            {
                                if (newScript == null)
                                {
                                    // See if the script is like "myvar = 2". newScript will be null otherwise.
                                    newScript = m_setConstructor.Create(line, scriptContext);
                                }

                                if (newScript == null)
                                {
                                    // See if the script calls a procedure defined by the game
                                    newScript = m_procConstructor.Create(line, scriptContext);
                                }
                            }
                        }
                    }

                    if (!dontAdd && !addedError)
                    {
                        if (newScript == null)
                        {
                            AddError(string.Format("Unrecognised script command '{0}'", line));
                        }
                        else
                        {
                            if (!m_lazyLoadingEnabled)
                            {
                                newScript.Line = line;
                            }
                            result.Add(newScript);
                        }
                    }
                }

                line = remainingScript;
                if (string.IsNullOrEmpty(line))
                {
                    finished = true;
                }
            }

            return(result);
        }
 static void Init()
 {
     MultiScript window = (MultiScript)EditorWindow.GetWindow(typeof(MultiScript));
 }