Patcher.Rules.RuleRunner.Run C# (CSharp) Method

Run() public method

public Run ( ) : void
return void
        public void Run()
        {
            // No source form
            if (rule.From == FormKind.Any)
            {
                Log.Fine("Non-query rule without source form");

                // Only select and inserts are valid in a non-query rule
                DoSelect(null);
                DoInserts(null);
            }
            // Single source form
            else if (rule.WhereEditorId != null)
            {
                Log.Fine("Looking up form with Editor ID = '" + rule.WhereEditorId + "'");

                if (engine.Context.Forms.Contains(rule.WhereEditorId))
                {
                    // Single form that is linked to the specified Editor ID
                    // Make sure the FormType matches and the form has been loaded
                    var form = engine.Context.Forms[rule.WhereEditorId];
                    if (form.FormKind == rule.From && form.IsLoaded)
                    {
                        var sourceProxy = engine.ProxyProvider.CreateFormProxy(form, ProxyMode.Source);
                        var updateProxy = engine.ProxyProvider.CreateFormProxy(rule.From, ProxyMode.Target);
                        DoActions(sourceProxy, updateProxy);
                    }
                    else
                    {
                        Log.Fine("Form with Editor ID = '" + rule.WhereEditorId + "' found (" + form + ") but it is a different type or not loaded");
                    }
                }
                else
                {
                    Log.Fine("Form with Editor ID = '" + rule.WhereEditorId + "' not found");
                }
            }
            // List of source forms
            else
            {
                Log.Fine("Querying all {0} forms", rule.From);
                var forms = engine.Context.Forms.OfKind(rule.From).Where(f => f.IsLoaded && !f.IsOverriden).ToArray();
                Log.Fine("Found {0} forms of type {1}", forms.Length, rule.From);

                // Prepare readonly source proxy
                var sourceProxy = engine.ProxyProvider.CreateFormProxy(rule.From, ProxyMode.Source);
                // Prepare update proxy (same type as the source proxy)
                var updateProxy = engine.ProxyProvider.CreateFormProxy(rule.From, ProxyMode.Target);

                // All loaded forms of specified type
                foreach (var form in forms)
                {
                    // Assign each form to the reusable readonly source proxy
                    sourceProxy.WithForm(form);

                    // Apply condition to all forms if specified
                    if (rule.Where == null || rule.Where.Method(sourceProxy))
                    {
                        // Perform all available actions
                        DoActions(sourceProxy, updateProxy);
                    }
                }
            }
        }

Usage Example

Example #1
0
        public void Run()
        {
            int totalRulesToRun = rules.SelectMany(p => p.Value).Count();

            if (totalRulesToRun == 0)
            {
                Log.Warning("No rules loaded, nothing to do.");
                return;
            }

            using (var progress = Display.StartProgress("Executing rules"))
            {
                int run = 0;
                foreach (var pluginFileName in rules.Keys)
                {
                    foreach (var rule in rules[pluginFileName])
                    {
                        Log.Info("Executing rule {0}", rule);
                        progress.Update(run++, totalRulesToRun, "{0}", rule);

                        // Run rule
                        var runner = new RuleRunner(this, rule);
                        try
                        {
                            runner.Run();
                        }
#if !DEBUG
                        catch (Exception ex)
                        {
                            if (ex is CompiledRuleAssertException)
                            {
                                Log.Error("Assertion failed while executing rule {0} with message: {1}", rule, ex.Message);
                            }
                            else
                            {
                                Log.Error("Error occured while executing rule {0} with message: {1}", rule, ex.Message);
                            }
                            Log.Fine(ex.ToString());

                            // Determine were the exception occured
                            var stackTrace = new StackTrace(ex, true);
                            var frame      = stackTrace.GetFrames().Where(f => f.GetMethod().DeclaringType.Namespace == "Patcher.Rules.Compiled.Generated").FirstOrDefault();
                            if (frame != null)
                            {
                                Display.ShowProblems("Runtime Error", ex.ToString(), new Problem()
                                {
                                    Message  = string.Format("{0}: {1}", ex.GetType().FullName, ex.Message),
                                    File     = DataFile.GetRelativePath(frame.GetFileName()),
                                    Line     = frame.GetFileLineNumber(),
                                    Solution = RuleRunner.GetRuntimeErrorHint(ex)
                                });
                            }

                            var choice = Display.Choice("Continue executing rules?", ChoiceOption.Ok, ChoiceOption.Cancel);
                            if (choice == ChoiceOption.Cancel)
                            {
                                Log.Warning("Rule execution has been aborted.");
                                throw new UserAbortException("Rule execution has been aborted by the user.");
                            }
                            else
                            {
                                Log.Warning("The last rule has not been fully applied.");
                                continue;
                            }
                        }
#endif
                        finally
                        {
                            Display.ClearProblems();
                        }

                        Log.Info("Rule completed with {0} updates and {1} inserts", runner.Updated, runner.Created);
                    }

                    // After all rules of a plugin were run
                    // Clear tags
                    Tags.Clear();
                }
            }
        }
All Usage Examples Of Patcher.Rules.RuleRunner::Run