Example #1
0
 public IMutableField Clone()
 {
     if (m_script != null) return m_script.Clone();
     LazyLoadScript result = new LazyLoadScript(m_scriptFactory, m_scriptString, m_scriptContext);
     result.Line = m_scriptString;
     result.Parent = m_parent;
     return result;
 }
Example #2
0
        public IMutableField Clone()
        {
            if (m_script != null)
            {
                return(m_script.Clone());
            }
            LazyLoadScript result = new LazyLoadScript(m_scriptFactory, m_scriptString, m_scriptContext);

            result.Line   = m_scriptString;
            result.Parent = m_parent;
            return(result);
        }
Example #3
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;
        }