public IEnumerator Run()
        {
            PostPatchLoader.Instance.databaseConfigs = null;

            // Wait for game database to be initialized for the 2nd time and wait for any plugins to initialize
            yield return(null);

            yield return(null);

            IEnumerable <ModListGenerator.ModAddedByAssembly> modsAddedByAssemblies = ModListGenerator.GetAdditionalModsFromStaticMethods(ModLogger.Instance);

            IEnumerable <IProtoUrlConfig> databaseConfigs = null;

            this.patchLoader = new MMPatchLoader(modsAddedByAssemblies, ModLogger.Instance, this.counter, this.timings);

            ITaskStatus patchingThreadStatus = BackgroundTask.Start(delegate
            {
                databaseConfigs = patchLoader.Run();
            });

            while (true)
            {
                yield return(null);

                Status = patchLoader.status;
                Errors = patchLoader.errors;

                if (!patchingThreadStatus.IsRunning)
                {
                    break;
                }
            }

            if (patchingThreadStatus.IsExitedWithError)
            {
                kspLogger.Exception("The patching thread threw an exception", patchingThreadStatus.Exception);
                FatalErrorHandler.HandleFatalError("The patching thread threw an exception");
                yield break;
            }

            if (databaseConfigs == null)
            {
                kspLogger.Error("The patcher returned a null collection of configs");
                FatalErrorHandler.HandleFatalError("The patcher returned a null collection of configs");
                yield break;
            }

            PostPatchLoader.Instance.databaseConfigs = databaseConfigs;
        }
示例#2
0
        private void ApplyPatches(LinkedList <IProtoUrlConfig> databaseConfigs, IPass pass)
        {
            progress.PassStarted(pass);

            foreach (IPatch patch in pass)
            {
                try
                {
                    patch.Apply(databaseConfigs, progress, logger);
                    if (patch.CountsAsPatch)
                    {
                        progress.PatchApplied();
                    }
                }
                catch (Exception e)
                {
                    progress.Exception(patch.UrlConfig, "Exception while processing node : " + patch.UrlConfig.SafeUrl(), e);
                    logger.Error("Processed node was\n" + patch.UrlConfig.PrettyPrint());
                }
            }
        }
示例#3
0
        private void ApplyPatches(IEnumerable <UrlDir.UrlFile> configFiles, IPass pass)
        {
            logger.Info(pass.Name + " pass");
            Activity = "ModuleManager " + pass.Name;

            foreach (IPatch patch in pass)
            {
                try
                {
                    foreach (UrlDir.UrlFile file in configFiles)
                    {
                        patch.Apply(file, progress, logger);
                    }
                    progress.PatchApplied();
                }
                catch (Exception e)
                {
                    progress.Exception(patch.UrlConfig, "Exception while processing node : " + patch.UrlConfig.SafeUrl(), e);
                    logger.Error("Processed node was\n" + patch.UrlConfig.PrettyPrint());
                }
            }
        }
 public void Error(UrlDir.UrlConfig url, string message)
 {
     Counter.errors.Increment();
     logger.Error(message);
     RecordErrorFile(url);
 }
示例#5
0
        public IEnumerator Run()
        {
            PostPatchLoader.Instance.databaseConfigs = null;

            if (!Directory.Exists(logsDirPath))
            {
                Directory.CreateDirectory(logsDirPath);
            }

            kspLogger.Info("Patching started on a new thread, all output will be directed to " + logPath);

            MessageQueue <ILogMessage> mmLogQueue = new MessageQueue <ILogMessage>();
            QueueLogRunner             logRunner  = new QueueLogRunner(mmLogQueue);
            ITaskStatus loggingThreadStatus       = BackgroundTask.Start(delegate
            {
                using (StreamLogger streamLogger = new StreamLogger(new FileStream(logPath, FileMode.Create)))
                {
                    logRunner.Run(streamLogger);
                    streamLogger.Info("Done!");
                }
            });

            // Wait for game database to be initialized for the 2nd time and wait for any plugins to initialize
            yield return(null);

            yield return(null);

            IBasicLogger mmLogger = new QueueLogger(mmLogQueue);

            IEnumerable <ModListGenerator.ModAddedByAssembly> modsAddedByAssemblies = ModListGenerator.GetAdditionalModsFromStaticMethods(mmLogger);

            IEnumerable <IProtoUrlConfig> databaseConfigs = null;

            MMPatchLoader patchLoader = new MMPatchLoader(modsAddedByAssemblies, mmLogger);

            ITaskStatus patchingThreadStatus = BackgroundTask.Start(delegate
            {
                databaseConfigs = patchLoader.Run();
            });

            while (true)
            {
                yield return(null);

                if (!patchingThreadStatus.IsRunning)
                {
                    logRunner.RequestStop();
                }

                Status = patchLoader.status;
                Errors = patchLoader.errors;

                if (!patchingThreadStatus.IsRunning && !loggingThreadStatus.IsRunning)
                {
                    break;
                }
            }

            if (patchingThreadStatus.IsExitedWithError)
            {
                kspLogger.Exception("The patching thread threw an exception", patchingThreadStatus.Exception);
                FatalErrorHandler.HandleFatalError("The patching thread threw an exception");
            }

            if (loggingThreadStatus.IsExitedWithError)
            {
                kspLogger.Exception("The logging thread threw an exception", loggingThreadStatus.Exception);
                FatalErrorHandler.HandleFatalError("The logging thread threw an exception");
            }

            if (databaseConfigs == null)
            {
                kspLogger.Error("The patcher returned a null collection of configs");
                FatalErrorHandler.HandleFatalError("The patcher returned a null collection of configs");
                yield break;
            }

            PostPatchLoader.Instance.databaseConfigs = databaseConfigs;
        }
示例#6
0
        private void ApplyPatches(string stage, IEnumerable <UrlDir.UrlConfig> patches)
        {
            logger.Info(stage + " pass");
            Activity = "ModuleManager " + stage;

            foreach (UrlDir.UrlConfig mod in patches)
            {
                try
                {
                    string  name = mod.type.RemoveWS();
                    Command cmd  = CommandParser.Parse(name, out string tmp);

                    if (cmd == Command.Insert)
                    {
                        logger.Warning("Warning - Encountered insert node that should not exist at this stage: " + mod.SafeUrl());
                        continue;
                    }
                    else if (cmd != Command.Edit && cmd != Command.Copy && cmd != Command.Delete)
                    {
                        logger.Warning("Invalid command encountered on a patch: " + mod.SafeUrl());
                        continue;
                    }

                    string       upperName = name.ToUpper();
                    PatchContext context   = new PatchContext(mod, databaseRoot, logger, progress);
                    char[]       sep       = { '[', ']' };
                    string       condition = "";

                    if (upperName.Contains(":HAS["))
                    {
                        int start = upperName.IndexOf(":HAS[");
                        condition = name.Substring(start + 5, name.LastIndexOf(']') - start - 5);
                        name      = name.Substring(0, start);
                    }

                    string[] splits   = name.Split(sep, 3);
                    string[] patterns = splits.Length > 1 ? splits[1].Split(',', '|') : null;
                    string   type     = splits[0].Substring(1);

                    bool loop = mod.config.HasNode("MM_PATCH_LOOP");

                    foreach (UrlDir.UrlFile file in allConfigFiles)
                    {
                        if (cmd == Command.Edit)
                        {
                            foreach (UrlDir.UrlConfig url in file.configs)
                            {
                                if (!IsMatch(url, type, patterns, condition))
                                {
                                    continue;
                                }
                                if (loop)
                                {
                                    logger.Info("Looping on " + mod.SafeUrl() + " to " + url.SafeUrl());
                                }

                                do
                                {
                                    progress.ApplyingUpdate(url, mod);
                                    url.config = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context);
                                } while (loop && IsMatch(url, type, patterns, condition));

                                if (loop)
                                {
                                    url.config.RemoveNodes("MM_PATCH_LOOP");
                                }
                            }
                        }
                        else if (cmd == Command.Copy)
                        {
                            // Avoid checking the new configs we are creating
                            int count = file.configs.Count;
                            for (int i = 0; i < count; i++)
                            {
                                UrlDir.UrlConfig url = file.configs[i];
                                if (!IsMatch(url, type, patterns, condition))
                                {
                                    continue;
                                }

                                ConfigNode clone = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context);
                                if (url.config.HasValue("name") && url.config.GetValue("name") == clone.GetValue("name"))
                                {
                                    progress.Error(mod, $"Error - when applying copy {mod.SafeUrl()} to {url.SafeUrl()} - the copy needs to have a different name than the parent (use @name = xxx)");
                                }
                                else
                                {
                                    progress.ApplyingCopy(url, mod);
                                    file.AddConfig(clone);
                                }
                            }
                        }
                        else if (cmd == Command.Delete)
                        {
                            int i = 0;
                            while (i < file.configs.Count)
                            {
                                UrlDir.UrlConfig url = file.configs[i];

                                if (IsMatch(url, type, patterns, condition))
                                {
                                    progress.ApplyingDelete(url, mod);
                                    file.configs.RemoveAt(i);
                                }
                                else
                                {
                                    i++;
                                }
                            }
                        }
                        else
                        {
                            throw new NotImplementedException("This code should not be reachable");
                        }
                    }
                    progress.PatchApplied();
                }
                catch (Exception e)
                {
                    progress.Exception(mod, "Exception while processing node : " + mod.SafeUrl(), e);

                    try
                    {
                        logger.Error("Processed node was\n" + mod.PrettyPrint());
                    }
                    catch (Exception ex2)
                    {
                        logger.Exception("Exception while attempting to print a node", ex2);
                    }
                }
            }
        }
示例#7
0
        private IEnumerator Run()
        {
            Stopwatch waitTimer = new Stopwatch();

            waitTimer.Start();

            progressTitle = "ModuleManager: Waiting for patching to finish";

            while (databaseConfigs == null)
            {
                yield return(null);
            }

            waitTimer.Stop();
            logger.Info("Waited " + ((float)waitTimer.ElapsedMilliseconds / 1000).ToString("F3") + "s for patching to finish");

            Stopwatch postPatchTimer = new Stopwatch();

            postPatchTimer.Start();

            progressTitle = "ModuleManager: Applying patched game database";
            logger.Info("Applying patched game database");

            foreach (UrlDir.UrlFile file in GameDatabase.Instance.root.AllConfigFiles)
            {
                file.configs.Clear();
            }

            foreach (IProtoUrlConfig protoConfig in databaseConfigs)
            {
                protoConfig.UrlFile.AddConfig(protoConfig.Node);
            }

            databaseConfigs = null;

            yield return(null);

#if false
            // Using an dedicated external log is nice. Dumping it into KSP.log breaking the known formats is not.
            if (File.Exists(logPath))
            {
                progressTitle = "ModuleManager: Dumping log to KSP log";
                logger.Info("Dumping ModuleManager log to main log");
                logger.Info("\n#### BEGIN MODULEMANAGER LOG ####\n\n\n" + File.ReadAllText(logPath) + "\n\n\n#### END MODULEMANAGER LOG ####");
            }
            else
            {
                logger.Error("ModuleManager log does not exist: " + logPath);
            }
#endif
            yield return(null);

#if DEBUG
            InGameTestRunner testRunner = new InGameTestRunner(logger);
            testRunner.RunTestCases(GameDatabase.Instance.root);
#endif

            yield return(null);

            progressTitle = "ModuleManager: Reloading things";

            logger.Info("Reloading resources definitions");
            PartResourceLibrary.Instance.LoadDefinitions();

            logger.Info("Reloading Trait configs");
            GameDatabase.Instance.ExperienceConfigs.LoadTraitConfigs();

            logger.Info("Reloading Part Upgrades");
            PartUpgradeManager.Handler.FillUpgrades();

            yield return(null);

            progressTitle = "ModuleManager: Running post patch callbacks";
            logger.Info("Running post patch callbacks");

            foreach (ModuleManagerPostPatchCallback callback in postPatchCallbacks)
            {
                try
                {
                    callback();
                }
                catch (Exception e)
                {
                    logger.Exception("Exception while running a post patch callback", e);
                }
                yield return(null);
            }
            yield return(null);

            // Call all "public static void ModuleManagerPostLoad()" on all class
            foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    foreach (Type type in ass.GetTypes())
                    {
                        MethodInfo method = type.GetMethod("ModuleManagerPostLoad", BindingFlags.Public | BindingFlags.Static);

                        if (method != null && method.GetParameters().Length == 0)
                        {
                            try
                            {
                                logger.Info("Calling " + ass.GetName().Name + "." + type.Name + "." + method.Name + "()");
                                method.Invoke(null, null);
                            }
                            catch (Exception e)
                            {
                                logger.Exception("Exception while calling " + ass.GetName().Name + "." + type.Name + "." + method.Name + "()", e);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.Exception("Post run call threw an exception in loading " + ass.FullName, e);
                }
            }

            yield return(null);

            // Call "public void ModuleManagerPostLoad()" on all active MonoBehaviour instance
            foreach (MonoBehaviour obj in FindObjectsOfType <MonoBehaviour>())
            {
                MethodInfo method = obj.GetType().GetMethod("ModuleManagerPostLoad", BindingFlags.Public | BindingFlags.Instance);

                if (method != null && method.GetParameters().Length == 0)
                {
                    try
                    {
                        logger.Info("Calling " + obj.GetType().Name + "." + method.Name + "()");
                        method.Invoke(obj, null);
                    }
                    catch (Exception e)
                    {
                        logger.Exception("Exception while calling " + obj.GetType().Name + "." + method.Name + "() :", e);
                    }
                }
            }

            yield return(null);

            if (ModuleManager.dumpPostPatch)
            {
                ModuleManager.OutputAllConfigs();
            }

            postPatchTimer.Stop();
            logger.Info("Post patch ran in " + ((float)postPatchTimer.ElapsedMilliseconds / 1000).ToString("F3") + "s");

            ready = true;
        }
示例#8
0
        public static IEnumerable <ModAddedByAssembly> GetAdditionalModsFromStaticMethods(IBasicLogger logger)
        {
            List <ModAddedByAssembly> result = new List <ModAddedByAssembly>();

            foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    foreach (Type type in ass.GetTypes())
                    {
                        MethodInfo method = type.GetMethod("ModuleManagerAddToModList", BindingFlags.Public | BindingFlags.Static);

                        if (method != null && method.GetParameters().Length == 0 && typeof(IEnumerable <string>).IsAssignableFrom(method.ReturnType))
                        {
                            string methodName = $"{ass.GetName().Name}.{type.Name}.{method.Name}()";
                            try
                            {
                                logger.Info("Calling " + methodName);
                                IEnumerable <string> modsToAdd = (IEnumerable <string>)method.Invoke(null, null);

                                if (modsToAdd == null)
                                {
                                    logger.Error("ModuleManagerAddToModList returned null: " + methodName);
                                    continue;
                                }

                                foreach (string mod in modsToAdd)
                                {
                                    result.Add(new ModAddedByAssembly(mod, ass.GetName().Name));
                                }
                            }
                            catch (Exception e)
                            {
                                logger.Exception("Exception while calling " + methodName, e);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.Exception("Add to mod list threw an exception in loading " + ass.FullName, e);
                }
            }

            foreach (MonoBehaviour obj in UnityEngine.Object.FindObjectsOfType <MonoBehaviour>())
            {
                MethodInfo method = obj.GetType().GetMethod("ModuleManagerAddToModList", BindingFlags.Public | BindingFlags.Instance);

                if (method != null && method.GetParameters().Length == 0 && typeof(IEnumerable <string>).IsAssignableFrom(method.ReturnType))
                {
                    string methodName = $"{obj.GetType().Name}.{method.Name}()";
                    try
                    {
                        logger.Info("Calling " + methodName);
                        IEnumerable <string> modsToAdd = (IEnumerable <string>)method.Invoke(obj, null);

                        if (modsToAdd == null)
                        {
                            logger.Error("ModuleManagerAddToModList returned null: " + methodName);
                            continue;
                        }

                        foreach (string mod in modsToAdd)
                        {
                            result.Add(new ModAddedByAssembly(mod, obj.GetType().Assembly.GetName().Name));
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Exception("Exception while calling " + methodName, e);
                    }
                }
            }

            return(result);
        }
 public void TestError()
 {
     logger.Error("You have made a grave mistake");
     logger.AssertError("You have made a grave mistake");
 }
 public void TestError()
 {
     logger.Error("You have made a grave mistake");
     logger.Received().Log(LogType.Error, "You have made a grave mistake");
 }