Esempio n. 1
0
        private IScriptConstructor GetScriptConstructor(string line)
        {
            IScriptConstructor constructor = null;
            int strength = 0;

            foreach (IScriptConstructor c in m_scriptConstructors.Values)
            {
                if (line.StartsWith(c.Keyword))
                {
                    // The line must start with the script keyword, and then the following
                    // character must be a non-word character. For example "msgfunction" is not
                    // a match for "msg".

                    if (line.Length == c.Keyword.Length || s_nonWordCharacterRegex.IsMatch(line.Substring(c.Keyword.Length)) || c is JSScriptConstructor)
                    {
                        if (c.Keyword.Length > strength)
                        {
                            constructor = c;
                            strength    = c.Keyword.Length;
                        }
                    }
                }
            }
            return(constructor);
        }
Esempio n. 2
0
 private void AddConstructor(IScriptConstructor constructor)
 {
     if (constructor.Keyword != null)
     {
         m_scriptConstructors.Add(constructor.Keyword, InitScriptConstructor(constructor));
     }
 }
Esempio n. 3
0
 private void AddConstructor(IScriptConstructor constructor)
 {
     if (constructor.Keyword != null)
     {
         m_scriptConstructors.Add(constructor.Keyword, InitScriptConstructor(constructor));
     }
 }
Esempio n. 4
0
 public LazyLoadScript(ScriptFactory scriptFactory, IScriptConstructor scriptConstructor, string scriptString, ScriptContext scriptContext)
 {
     m_scriptFactory     = scriptFactory;
     m_scriptConstructor = scriptConstructor;
     m_scriptString      = scriptString;
     m_scriptContext     = scriptContext;
     m_worldModel        = scriptConstructor.WorldModel;
 }
Esempio n. 5
0
 public LazyLoadScript(ScriptFactory scriptFactory, IScriptConstructor scriptConstructor, string scriptString, ScriptContext scriptContext)
 {
     m_scriptFactory = scriptFactory;
     m_scriptConstructor = scriptConstructor;
     m_scriptString = scriptString;
     m_scriptContext = scriptContext;
     m_worldModel = scriptConstructor.WorldModel;
 }
Esempio n. 6
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);
        }
Esempio n. 7
0
 private IScriptConstructor InitScriptConstructor(IScriptConstructor constructor)
 {
     constructor.GameLoader    = m_gameLoader;
     constructor.ScriptFactory = this;
     return(constructor);
 }
Esempio n. 8
0
 private IScriptConstructor InitScriptConstructor(IScriptConstructor constructor)
 {
     constructor.WorldModel = m_worldModel;
     constructor.ScriptFactory = this;
     return constructor;
 }
Esempio n. 9
0
 private IScriptConstructor InitScriptConstructor(IScriptConstructor constructor)
 {
     constructor.GameLoader = m_gameLoader;
     constructor.ScriptFactory = this;
     return constructor;
 }
Esempio n. 10
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);
        }
Esempio n. 11
0
 private IScriptConstructor InitScriptConstructor(IScriptConstructor constructor)
 {
     constructor.WorldModel    = m_worldModel;
     constructor.ScriptFactory = this;
     return(constructor);
 }