Patcher.Rules.RuleCompiler.CompileAll C# (CSharp) Method

CompileAll() public method

public CompileAll ( ) : bool
return bool
        public bool CompileAll()
        {
            if (units.Count == 0)
            {
                Log.Warning("No rules form plugin {0}. Nothing to compile.", pluginFileName);
                return false;
            }

            // Collect sources into a list, checking whether any has been updated
            bool anyUpdated = false;
            List<string> sources = new List<string>();
            foreach (var unit in units)
            {
                if (unit.Updated)
                    anyUpdated = true;

                // Get full path to source fine
                sources.Add(engine.Context.DataFileProvider.GetDataFile(FileMode.Open, unit.Source).FullPath);
            }

            if (!anyUpdated)
            {
                // None of the source files have been updated
                // so the cached rules assembly can be used, if it still exists
                // and appears to be valid
                var cachedAssemblyFile = engine.Context.DataFileProvider.GetDataFile(FileMode.Open, generatedAssemblyPath);
                if (cachedAssemblyFile.Exists())
                {
                    //if (ValidateCachedAssemblyVersion(cachedAssemblyFile.FullPath))
                    {
                        try
                        {
                            // Load cached assembly into the domain
                            Assembly assembly = Assembly.LoadFrom(cachedAssemblyFile.FullPath);

                            LoadMethodsFromAssembly(assembly);
                            Log.Info("Using cached assembly containing compiled rules for plugin {0}.", pluginFileName);
                            return true;
                         }
                        catch (Exception ex)
                        {
                            Log.Error("Something is wrong with cached compiled rules.");
                            Log.Error("If this problem persists please delete this file: {0}", cachedAssemblyFile.FullPath);
                            Log.Error("Then run this program again.");
                            throw new InvalidProgramException("Cached assembly containing compiled rules for plugin " + pluginFileName + " could not be loaded: " + ex.Message);
                        }
                    }
                    //else
                    //{
                    //    Log.Fine("Cached assembly containing compiled rules for plugin {0} is no longer valid and needs to be recompiled.", pluginFileName);
                    //}
                }
                else
                {
                    Log.Fine("Cached assembly containing compiled rules for plugin {0} could not be found.", pluginFileName);
                }
            }

            //// Create version.cs
            //CodeBuilder builder = new CodeBuilder(NamespaceName, VersionClassName, "//\n// Source file for version tracking.\n//\n\n");
            //builder.WriteCode(string.Format("public static readonly string {0} = \"{1}\";\n", VersionFieldName, Program.GetProgramVersionInfo()));
            //string versionCode = builder.ToString();
            //string versionFilePath = Path.Combine(cachePath, "version.cs");
            //var versionFile = engine.Context.DataFileProvider.GetDataFile(FileMode.Create, versionFilePath);
            //using (var stream = versionFile.Open())
            //{
            //    using (var writer = new StreamWriter(stream))
            //    {
            //        writer.Write(versionCode);
            //    }
            //}
            //sources.Add(versionFile.FullPath);

            // Compile all sources
            Log.Info("Compiling rules for plugin {0}.", pluginFileName);

            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            // Following 2 needed for dynamic type
            parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
            parameters.ReferencedAssemblies.Add("System.Core.dll");
            // Extracted embedded resource with proxies and helpers
            parameters.ReferencedAssemblies.Add(engine.Context.DataFileProvider.GetDataFile(FileMode.Open, RuleEngine.CompiledRulesAssemblyPath).FullPath);
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = false;
            parameters.IncludeDebugInformation = true;
            parameters.OutputAssembly = engine.Context.DataFileProvider.GetDataFile(FileMode.Create, generatedAssemblyPath).FullPath;
            CompilerResults results = provider.CompileAssemblyFromFile(parameters, sources.ToArray());

            // Version file is not needed to be cached
            // new one will always be created when needed
            //versionFile.Delete();

            if (results.Errors.HasErrors)
            {
                SortedSet<string> sourcesWithErrors = new SortedSet<string>();

                foreach (CompilerError error in results.Errors)
                {
                    //Log.Error(string.Format("Compiler {0} in file `{1}` at line {2:000}: {3}", error.IsWarning ? "warning" : "error", error.FileName, error.Line, error.ErrorText));
                    if (!error.IsWarning)
                        Log.Error(error.ToString());

                    // Collect filenames of sources with errors
                    if (!sourcesWithErrors.Contains(error.FileName))
                        sourcesWithErrors.Add(error.FileName);
                }

                // Alter the sources that failed to ensure recompilation
                // even if the source does not change
                foreach (var path in sourcesWithErrors)
                {
                    File.AppendAllText(path, " ");
                }

                throw new CompilerException("Error(s) occured during rule compilation", results.Errors);
            }

            LoadMethodsFromAssembly(results.CompiledAssembly);

            return true;
        }

Usage Example

示例#1
0
        public void Load()
        {
            Log.Info("Loading rules");

            using (var progress = Display.StartProgress("Loading rules"))
            {
                long total   = context.Plugins.Count;
                long current = 0;

                foreach (var pluginFileName in context.Plugins.Select(p => p.FileName))
                {
                    bool retry;
                    do
                    {
                        retry = false;
                        try
                        {
                            var compiler = new RuleCompiler(this, pluginFileName);

                            string path  = Path.Combine(Program.ProgramFolder, Program.ProgramRulesFolder, RulesFolder, pluginFileName);
                            var    files = Context.DataFileProvider.FindDataFiles(path, "*.rules").ToArray();

                            total += files.Length;

                            foreach (var file in files)
                            {
                                using (var stream = file.Open())
                                {
                                    bool isDebugModeEnabled = DebugAll ||
                                                              pluginFileName.Equals(DebugPluginFileName, StringComparison.OrdinalIgnoreCase) &&
                                                              (DebugRuleFileName == null || Path.GetFileName(file.Name).Equals(DebugRuleFileName, StringComparison.OrdinalIgnoreCase));

                                    int count = 0;
                                    using (RuleReader reader = new RuleReader(stream))
                                    {
                                        foreach (var entry in reader.ReadRules())
                                        {
                                            if (entry.Select == null && entry.Update == null && entry.Inserts.Count() == 0)
                                            {
                                                Log.Warning("Rule {0} in file {1} ignored because it lacks any operation", entry.Name, pluginFileName);
                                                continue;
                                            }

                                            var metadata = new RuleMetadata()
                                            {
                                                PluginFileName = pluginFileName,
                                                RuleFileName   = Path.GetFileName(file.Name),
                                                Name           = entry.Name,
                                                Description    = entry.Description,
                                            };

                                            Log.Fine("Loading rule {0}\\{1}@{2}", metadata.PluginFileName, metadata.RuleFileName, metadata.Name);

                                            try
                                            {
                                                compiler.Add(entry, metadata, isDebugModeEnabled);
                                            }
                                            catch (IllegalTokenException ex)
                                            {
                                                Display.ShowProblems("Illegal Token", ex.ToString(), new Problem()
                                                {
                                                    Message  = ex.Message,
                                                    File     = file.GetRelativePath(),
                                                    Solution = string.Format("Please avoid using the following tokens in rule code: {0}",
                                                                             string.Join(", ", RuleCompiler.GetIllegalCodeTokens()))
                                                });
                                                throw;
                                            }

                                            count++;
                                        }
                                    }
                                    Log.Fine("Loaded {0} rule(s) from file {1}", count, stream.Name);
                                }

                                progress.Update(current, total, string.Format("{0}\\{1}", pluginFileName, Path.GetFileName(file.Name)));
                            }

                            if (compiler.HasRules)
                            {
                                try
                                {
                                    compiler.CompileAll();
                                }
                                catch (CompilerException ex)
                                {
                                    StringBuilder  text     = new StringBuilder();
                                    List <Problem> problems = new List <Problem>();
                                    foreach (System.CodeDom.Compiler.CompilerError error in ex.Errors)
                                    {
                                        if (!error.IsWarning)
                                        {
                                            text.AppendLine(error.ToString());

                                            problems.Add(new Problem()
                                            {
                                                Message  = string.Format("{0}: {1}", error.ErrorNumber, error.ErrorText),
                                                File     = DataFile.GetRelativePath(error.FileName),
                                                Line     = error.Line,
                                                Column   = error.Column,
                                                Solution = RuleCompiler.GetCompilerErrorHint(error)
                                            });
                                        }
                                    }
                                    text.Append(ex.ToString());

                                    Display.ShowProblems("Compiler Error(s)", text.ToString(), problems.ToArray());

                                    throw ex;
                                }

                                rules.Add(pluginFileName, new List <IRule>(compiler.CompiledRules));
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error("Error occured while loading rules for plugin {0} with message: {1}", pluginFileName, ex.Message);
                            Log.Fine(ex.ToString());

                            // Depending on the kind of error, offer a choice to reload rules for this plugin
                            // Rules can be reloaded only if illegal tokens have been detected or when compilation has failed
                            ChoiceOption result = ChoiceOption.Cancel;
                            if (ex.GetType() == typeof(IllegalTokenException) || ex.GetType() == typeof(CompilerException))
                            {
                                result = Display.Choice(string.Format("Try loading rules for plugin {0} again?", pluginFileName), ChoiceOption.Yes, ChoiceOption.Continue, ChoiceOption.Cancel);
                            }
                            else
                            {
                                result = Display.Choice("Continue loading rules?", ChoiceOption.Continue, ChoiceOption.Cancel);
                            }

                            if (result == ChoiceOption.Yes)
                            {
                                Log.Info("Rules for plugin {0} will be reloaded.", pluginFileName);
                                retry = true;
                            }
                            else if (result == ChoiceOption.Continue)
                            {
                                Log.Warning("Rules for plugin {0} skipped because an error occured: {1} ", pluginFileName, ex.Message);
                            }
                            else
                            {
                                Log.Warning("Rule loading has been aborted.");
                                throw new UserAbortException("Rule loading has been aborted by the user.");
                            }
                        }
                        finally
                        {
                            Display.ClearProblems();
                        }
                    } while (retry);
                }

                current++;
            }
        }
All Usage Examples Of Patcher.Rules.RuleCompiler::CompileAll