コード例 #1
0
        /// <summary>
        /// Loads the mods
        /// </summary>
        internal static void LoadMods()
        {
            AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs args)
            {
                var allDlls = new DirectoryInfo(QModBaseDir).GetFiles("*.dll", SearchOption.AllDirectories);
                foreach (var dll in allDlls)
                {
                    if (args.Name.Contains(Path.GetFileNameWithoutExtension(dll.Name)))
                    {
                        FileInfo[] modjson = dll.Directory.GetFiles("mod.json", SearchOption.TopDirectoryOnly);
                        if (modjson.Length != 0)
                        {
                            try
                            {
                                if (Newtonsoft.Json.Linq.JObject.Parse(File.ReadAllText(modjson[0].FullName))
                                    .TryGetValue("Enabled", out Newtonsoft.Json.Linq.JToken isEnabled) &&
                                    !(bool)isEnabled)
                                {
                                    Console.WriteLine("Cannot resolve Assembly " + dll.Name + " - Disabled by mod.json");
                                    continue;
                                }
                            }
                            catch { /*fail silently*/ }
                        }
                        Console.WriteLine();
                        return(Assembly.LoadFrom(dll.FullName));
                    }
                }

                Console.WriteLine("Could not find assembly " + args.Name);
                return(null);
            };

            if (patched)
            {
                return;
            }

            patched = true;

            if (!Directory.Exists(QModBaseDir))
            {
                AddLog("QMods directory was not found! Creating...");
                if (QModBaseDir == "ERR")
                {
                    AddLog("There was an error creating the QMods directory");
                    AddLog("Please make sure that you ran TerraTech from Steam");
                }
                try
                {
                    Directory.CreateDirectory(QModBaseDir);
                    AddLog("QMods directory created successfully!");
                }
                catch (Exception e)
                {
                    AddLog("EXCEPTION CAUGHT!");
                    AddLog(e.Message);
                    AddLog(e.StackTrace);
                    if (e.InnerException != null)
                    {
                        AddLog("INNER EXCEPTION:");
                        AddLog(e.InnerException.Message);
                        AddLog(e.InnerException.StackTrace);
                    }
                }
                Console.WriteLine(ParseLog());
                return;
            }

            var subDirs   = Directory.GetDirectories(QModBaseDir);
            var lastMods  = new List <QMod>();
            var firstMods = new List <QMod>();
            var otherMods = new List <QMod>();

            foreach (var subDir in subDirs)
            {
                try
                {
                    var jsonFile = Path.Combine(subDir, "mod.json");

                    if (!File.Exists(jsonFile))
                    {
                        AddLog($"ERROR! No \"mod.json\" file found in folder \"{subDir}\"");
                        File.WriteAllText(jsonFile, JsonConvert.SerializeObject(new QMod()));
                        AddLog("A template file was created");
                        continue;
                    }

                    QMod mod = QMod.FromJsonFile(Path.Combine(subDir, "mod.json"));

                    if (mod == (null))
                    {
                        continue;
                    }

                    if (mod.Enable == false)
                    {
                        AddLog($"- {mod.DisplayName} is Disabled, skipping");
                        continue;
                    }

                    var modAssemblyPath = Path.Combine(subDir, mod.AssemblyName);

                    if (!File.Exists(modAssemblyPath))
                    {
                        AddLog($"ERROR! No matching dll found at \"{modAssemblyPath}\" for mod \"{mod.DisplayName}\"");
                        continue;
                    }

                    mod.LoadedAssembly  = Assembly.LoadFrom(modAssemblyPath);
                    mod.ModAssemblyPath = modAssemblyPath;

                    if (mod.Priority.Equals("Last"))
                    {
                        lastMods.Add(mod);
                        continue;
                    }
                    else if (mod.Priority.Equals("First"))
                    {
                        firstMods.Add(mod);
                        continue;
                    }
                    else
                    {
                        otherMods.Add(mod);
                        continue;
                    }
                }
                catch (Exception E)
                {
                    AddLog($"ERROR! Failed to read mod \"{subDir}\": {E.Message}\n{E.StackTrace}");
                }
            }

            // LoadBefore and LoadAfter stuff

            AddLog(" ");
            AddLog("Installed mods:");

            foreach (var mod in firstMods)
            {
                if (mod != null)
                {
                    loadedMods.Add(LoadMod(mod));
                    LogModLoadTime(mod);
                }
            }

            foreach (var mod in otherMods)
            {
                if (mod != null)
                {
                    loadedMods.Add(LoadMod(mod));
                    LogModLoadTime(mod);
                }
            }

            foreach (var mod in lastMods)
            {
                if (mod != null)
                {
                    loadedMods.Add(LoadMod(mod));
                    LogModLoadTime(mod);
                }
            }

            if (sw.IsRunning)
            {
                sw.Stop();
            }

            FlagGame();

            Console.WriteLine(ParseLog());
        }
コード例 #2
0
ファイル: Patcher.cs プロジェクト: nesrak1/QModManager
        internal static void StartLoadingMods()
        {
            Logger.Info("Started loading mods");

            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                FileInfo[] allDlls = new DirectoryInfo(QModBaseDir).GetFiles("*.dll", SearchOption.AllDirectories);
                foreach (FileInfo dll in allDlls)
                {
                    if (args.Name.Contains(Path.GetFileNameWithoutExtension(dll.Name)))
                    {
                        return(Assembly.LoadFrom(dll.FullName));
                    }
                }

                return(null);
            };

            Logger.Debug("Added AssemblyResolve event");

            if (!Directory.Exists(QModBaseDir))
            {
                Logger.Info("QMods directory was not found! Creating...");

                return;
            }

            string[] subDirs = Directory.GetDirectories(QModBaseDir);

            foreach (string subDir in subDirs)
            {
                if (Directory.GetFiles(subDir, "*.dll", SearchOption.TopDirectoryOnly).Length < 1)
                {
                    continue;
                }

                string folderName = new DirectoryInfo(subDir).Name;
                string jsonFile   = Path.Combine(subDir, "mod.json");

                if (!File.Exists(jsonFile))
                {
                    Logger.Error($"No \"mod.json\" file found for mod located in folder \"{subDir}\". A template file will be created");
                    File.WriteAllText(jsonFile, JsonConvert.SerializeObject(new QMod()));
                    erroredMods.Add(QMod.CreateFakeQMod(folderName));
                    continue;
                }

                QMod mod = QMod.FromJsonFile(Path.Combine(subDir, "mod.json"));

                if (!QMod.QModValid(mod, folderName))
                {
                    erroredMods.Add(QMod.CreateFakeQMod(folderName));

                    continue;
                }

                if (mod.Enable == false)
                {
                    Logger.Info($"Mod \"{mod.DisplayName}\" is disabled via config, skipping...");

                    continue;
                }

                string modAssemblyPath = Path.Combine(subDir, mod.AssemblyName);

                if (!File.Exists(modAssemblyPath))
                {
                    Logger.Error($"No matching dll found at \"{modAssemblyPath}\" for mod \"{mod.DisplayName}\"");
                    erroredMods.Add(mod);

                    continue;
                }

                mod.LoadedAssembly  = Assembly.LoadFrom(modAssemblyPath);
                mod.ModAssemblyPath = modAssemblyPath;
                //mod.MessageReceivers = GetMessageRecievers(mod.LoadedAssembly);

                foundMods.Add(mod);
            }

            // Add the found mods into the sortedMods list
            sortedMods.AddRange(foundMods);

            // Disable mods that are not for the detected game
            // (Disable Subnautica mods if Below Zero is detected and disable Below Zero mods if Subnautica is detected)
            DisableNonApplicableMods();

            // Remove mods with duplicate mod ids if any are found
            RemoveDuplicateModIDs();

            // Sort the mods based on their LoadBefore and LoadAfter properties
            // If any mods break (i.e., a loop is found), they are removed from the list so that they aren't loaded
            // And are outputted into the log.
            SortMods();

            // Check if all the mods' dependencies are present
            // If a mod's dependecies aren't present, that mods isn't loaded and it is outputted in the log.
            CheckForDependencies();

            // Finally, load all the mods after sorting and checking for dependencies.
            // If anything goes wrong during loading, it is outputted in the log.
            LoadAllMods();
        }