/// <summary>
 /// Adds a script to the to do list of the source manager.
 /// Will do nothing if already included
 /// </summary>
 /// <param name="script"></param>
 public void AddToTodo(ISourceScript script)
 {
     if (!IsIncluded(script))
     {
         this.Log(DebugLevel.LOGS, Verbosity.LEVEL3, "Adding Script to Todo List: {0}", Path.GetFileName(script.GetFilePath()));
         AddFile(script, false);
         _doneState.Add(ProcessStage.QUEUED);
     }
 }
        /// <summary>
        /// Sets the processing state of the script to done
        /// it will not be returned by the NextItem property.
        /// </summary>
        /// <param name="script"></param>
        public void SetState(ISourceScript script, ProcessStage stage)
        {
            if (IsIncluded(script))
            {
                _doneState[IndexOfFile(script.GetKey())] = stage;

                this.Log(DebugLevel.LOGS, Verbosity.LEVEL3, "Finished Script: {0}", Path.GetFileName(script.GetFilePath()));
            }
        }
        /// <summary>
        /// Fixes the order of the file tree if a script was being loaded and is now referenced (again)
        /// by removing it from the lower position and readding it at the top
        /// </summary>
        /// <param name="script"></param>
        public void FixOrder(ISourceScript script)
        {
            this.Log(DebugLevel.LOGS, Verbosity.LEVEL3, "Fixing Build Order of file: {0}", Path.GetFileName(script.GetFilePath()));
            int idx = IndexOfFile(script.GetKey());
            var a   = _sources[idx];
            var ab  = _doneState[idx];

            _doneState.RemoveAt(idx);
            _doneState.Add(ab);
            _sources.RemoveAt(idx);
            AddFile(a, true);
        }
        public bool FullScriptStage(ISourceScript file, ISourceManager todo, IDefinitions defs)
        {
            this.Log(DebugLevel.LOGS, Verbosity.LEVEL4, "Starting Condition Solver passes on file: {0}", Path.GetFileName(file.GetFilePath()));
            bool          ret             = true;
            int           openIf          = 0;
            bool          foundConditions = false;
            bool          elseIsValid     = false;
            bool          expectEndOrIf   = false;
            List <string> lastPass        = file.GetSource().ToList();
            List <string> solvedFile      = new List <string>();
            int           passCount       = 0;

            do
            {
                passCount++;
                this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Starting Condition Solver pass: {0}", passCount);

                foundConditions = false;
                elseIsValid     = false;
                for (int i = 0; i < lastPass.Count; i++)
                {
                    string line = lastPass[i].TrimStart();
                    if (IsKeyWord(line, StartCondition))
                    {
                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Found a {0} Statement", StartCondition);
                        bool r = EvaluateConditional(line, defs);
                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Evaluation: {0}", r);
                        elseIsValid = !r;
                        int size = GetBlockSize(lastPass, i);
                        if (r)
                        {
                            solvedFile.AddRange(lastPass.SubArray(i + 1, size));
                            this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Adding Branch To Solved File.");
                        }

                        openIf++;
                        i += size;
                        foundConditions = true;
                        expectEndOrIf   = false;
                    }
                    else if (elseIsValid && IsKeyWord(line, ElseIfCondition))
                    {
                        if (!expectEndOrIf && openIf > 0)
                        {
                            this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Found a {0} Statement", ElseIfCondition);
                            bool r = EvaluateConditional(line, defs);
                            this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Evaluation: {0}", r);
                            elseIsValid = !r;
                            int size = GetBlockSize(lastPass, i);
                            if (r)
                            {
                                solvedFile.AddRange(lastPass.SubArray(i + 1, size));
                                this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Adding Branch To Solved File.");
                            }

                            i += size;
                            foundConditions = true;
                        }
                        else if (expectEndOrIf)
                        {
                            this.Log(DebugLevel.ERRORS, Verbosity.LEVEL1, "A {0} can not be followed by an {1}", ElseCondition, ElseIfCondition);
                            ret = false;
                            break;
                        }
                        else
                        {
                            this.Log(DebugLevel.ERRORS, Verbosity.LEVEL1, "A {0} should be preceeded by a {1}", ElseIfCondition, StartCondition);
                            ret = false;
                            break;
                        }
                    }
                    else if (IsKeyWord(line, ElseCondition))
                    {
                        if (openIf > 0)
                        {
                            this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Found a {0} Statement", ElseCondition);
                            var size = GetBlockSize(lastPass, i);
                            if (elseIsValid)
                            {
                                solvedFile.AddRange(lastPass.SubArray(i + 1, size));
                                this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Adding Branch To Solved File.");
                            }
                            else
                            {
                                this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Ignored since a previous condition was true");
                            }
                            i += size;
                            foundConditions = true;
                            expectEndOrIf   = true;
                        }
                        else
                        {
                            this.Log(DebugLevel.ERRORS, Verbosity.LEVEL1, "A {0} should be preceeded by a {1}", ElseCondition, StartCondition);
                            ret = false;
                            break;
                        }
                    }
                    else if (IsKeyWord(line, EndCondition))
                    {
                        if (openIf > 0)
                        {
                            expectEndOrIf = false;
                            openIf--;
                        }
                        else
                        {
                            ret = false;

                            this.Log(DebugLevel.ERRORS, Verbosity.LEVEL1, "A {0} should be preceeded by a {1}", EndCondition, StartCondition);
                            break;
                        }
                    }
                    else if (EnableDefine &&
                             line.StartsWith(DefineKeyword))
                    {
                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Found a {0} Statement", DefineKeyword);;
                        defs.Set(Utils.SplitAndRemoveFirst(line, Separator));
                        solvedFile.Add(lastPass[i]);
                    }
                    else if (EnableUndefine &&
                             line.StartsWith(UndefineKeyword))
                    {
                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Found a {0} Statement", UndefineKeyword);
                        defs.Unset(Utils.SplitAndRemoveFirst(line, Separator));
                        solvedFile.Add(lastPass[i]);
                    }
                    else
                    {
                        solvedFile.Add(lastPass[i]);
                    }
                }

                if (ret)
                {
                    lastPass = solvedFile;
                }
                else
                {
                    break;
                }
                solvedFile = new List <string>();
            } while (foundConditions);

            file.SetSource(lastPass.ToArray());


            this.Log(DebugLevel.LOGS, Verbosity.LEVEL4, "Conditional Solver Finished");

            return(ret);
        }
        public bool FullScriptStage(ISourceScript script, ISourceManager sourceManager, IDefinitions defs)
        {
            this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Disovering Include Statments...");
            List <string> source      = script.GetSource().ToList();
            string        currentPath = Path.GetDirectoryName(Path.GetFullPath(script.GetFilePath()));

            for (int i = source.Count - 1; i >= 0; i--)
            {
                if (Utils.IsStatement(source[i], IncludeInlineKeyword))
                {
                    this.Log(DebugLevel.LOGS, Verbosity.LEVEL6, "Found Inline Include Statement...");
                    string[] args = Utils.SplitAndRemoveFirst(source[i], Separator);
                    if (args.Length == 0)
                    {
                        this.Log(DebugLevel.WARNINGS, Verbosity.LEVEL1, "No File Specified");
                        continue;
                    }

                    if (Utils.FileExistsRelativeTo(currentPath, args[0]))
                    {
                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL6, "Replacing Inline Keyword with file content");
                        source.RemoveAt(i);

                        source.InsertRange(i, File.ReadAllLines(args[0]));
                    }
                    else
                    {
                        this.Log(DebugLevel.WARNINGS, Verbosity.LEVEL1, "File does not exist: {0}", args[0]);
                    }
                }
            }
            script.SetSource(source.ToArray());


            string[] incs = Utils.FindStatements(source.ToArray(), IncludeKeyword);


            foreach (var includes in incs)
            {
                this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Processing Statement: {0}", includes);
                bool tmp = GetISourceScript(sourceManager, includes, currentPath, out List <ISourceScript> sources);
                if (tmp)
                {
                    foreach (var sourceScript in sources)
                    {
                        this.Log(DebugLevel.LOGS, Verbosity.LEVEL6, "Processing Include: {0}", Path.GetFileName(sourceScript.GetFilePath()));

                        if (!sourceManager.IsIncluded(sourceScript))
                        {
                            sourceManager.AddToTodo(sourceScript);
                        }
                        else
                        {
                            sourceManager.FixOrder(sourceScript);
                        }
                    }
                }
                else
                {
                    /*if (path != "")*/
                    return(false); //We crash if we didnt find the file. but if the user forgets to specify the path we will just log the error
                }
            }

            this.Log(DebugLevel.LOGS, Verbosity.LEVEL5, "Inclusion of Files Finished");
            return(true);
        }
Exemple #6
0
        private bool RunFullScriptStage(List <AbstractPlugin> _fullScriptStage, ProcessStage stage, ISourceScript script, ISourceManager sourceManager, IDefinitions defTable)
        {
            foreach (var abstractPlugin in _fullScriptStage)
            {
                bool ret = true;
                this.Log(DebugLevel.LOGS, Verbosity.LEVEL3, "Running Plugin: {0}: {1} on file {2}", abstractPlugin, stage, Path.GetFileName(script.GetFilePath()));
                if (stage == ProcessStage.ON_LOAD_STAGE)
                {
                    ret = abstractPlugin.OnLoad_FullScriptStage(script, sourceManager, defTable);
                }
                else if (stage == ProcessStage.ON_MAIN)
                {
                    ret = abstractPlugin.OnMain_FullScriptStage(script, sourceManager, defTable);
                }

                if (!ret)
                {
                    this.Log(DebugLevel.ERRORS, Verbosity.LEVEL1, "Processing was aborted by Plugin: {0}", abstractPlugin);
                    return(false);
                }
            }

            return(true);
        }
Exemple #7
0
        /// <summary>
        /// Processes the file with the settings, definitions and the source manager specified.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="settings"></param>
        /// <param name="defs"></param>
        /// <returns>Returns a list of files that can be compiled in reverse order</returns>
        public ISourceScript[] Process(string[] files, Settings settings = null, IDefinitions defs = null)
        {
            string dir = Directory.GetCurrentDirectory();

            defs = defs ?? new Definitions();
            SourceManager sm = new SourceManager(_plugins);

            InitializePlugins(settings, defs, sm);

            this.Log(DebugLevel.LOGS, Verbosity.LEVEL1, "Starting Processing of Files: {0}", files.Unpack(", "));
            foreach (var file in files)
            {
                string f = Path.GetFullPath(file);
                Directory.SetCurrentDirectory(Path.GetDirectoryName(f));

                sm.SetLock(false);
                sm.CreateScript(out ISourceScript sss, _sep, f, f, new Dictionary <string, object>());
                sm.SetLock(true);
                List <ISourceScript> all = new List <ISourceScript>();
                sm.AddToTodo(sss);
            }

            ISourceScript ss = sm.NextItem;

            do
            {
                if (!(ss as SourceScript).IsSourceLoaded)
                {
                    RunStages(ProcessStage.ON_LOAD_STAGE, ss, sm, defs);
                }

                this.Log(DebugLevel.PROGRESS, Verbosity.LEVEL1, "Remaining Files: {0}", sm.GetTodoCount());
                this.Log(DebugLevel.LOGS, Verbosity.LEVEL2, "Selecting File: {0}", Path.GetFileName(ss.GetFilePath()));
                //RUN MAIN
                sm.SetLock(false);
                RunStages(ProcessStage.ON_MAIN, ss, sm, defs);
                sm.SetLock(true);
                sm.SetState(ss, ProcessStage.ON_FINISH_UP);
                ss = sm.NextItem;
            } while (ss != null);


            Directory.SetCurrentDirectory(dir);
            ISourceScript[] ret = sm.GetList().ToArray();
            this.Log(DebugLevel.LOGS, Verbosity.LEVEL1, "Finishing Up...");
            foreach (var finishedScript in ret)
            {
                this.Log(DebugLevel.LOGS, Verbosity.LEVEL2, "Selecting File: {0}", Path.GetFileName(finishedScript.GetFilePath()));
                RunStages(ProcessStage.ON_FINISH_UP, finishedScript, sm, defs);
            }
            this.Log(DebugLevel.LOGS, Verbosity.LEVEL1, "Finished Processing Files.");
            return(ret);
        }