Ejemplo n.º 1
0
        internal static IEnumerator <ProgressReport> LoadMoadsLoop()
        {
            // Only want to run this function once -- it could get submitted a few times
            if (hasLoadedMods)
            {
                yield break;
            }

            stopwatch.Start();

            string sliderText = "Loading Mods";

            Log("");
            LogWithDate("Pre-loading mods...");
            yield return(new ProgressReport(0, sliderText, "Pre-loading mods..."));

            // find all sub-directories that have a mod.json file
            var modDirectories = Directory.GetDirectories(ModsDirectory)
                                 .Where(x => File.Exists(Path.Combine(x, MOD_JSON_NAME))).ToArray();

            if (modDirectories.Length == 0)
            {
                hasLoadedMods = true;
                Log("No ModTek-compatable mods found.");
                yield break;
            }

            // create ModDef objects for each mod.json file
            var modDefs = new Dictionary <string, ModDef>();

            foreach (var modDirectory in modDirectories)
            {
                ModDef modDef;
                var    modDefPath = Path.Combine(modDirectory, MOD_JSON_NAME);

                try
                {
                    modDef = ModDef.CreateFromPath(modDefPath);
                }
                catch (Exception e)
                {
                    Log($"Caught exception while parsing {MOD_JSON_NAME} at path {modDefPath}");
                    Log($"\t{e.Message}");
                    continue;
                }

                if (!modDef.Enabled)
                {
                    Log($"Will not load {modDef.Name} because it's disabled.");
                    continue;
                }

                if (modDefs.ContainsKey(modDef.Name))
                {
                    Log($"Already loaded a mod named {modDef.Name}. Skipping load from {modDef.Directory}.");
                    continue;
                }

                modDefs.Add(modDef.Name, modDef);
            }

            PropagateConflictsForward(modDefs);
            modLoadOrder = GetLoadOrder(modDefs, out var willNotLoad);

            int modLoaded = 0;

            // lists guarentee order
            foreach (var modName in modLoadOrder)
            {
                var modDef = modDefs[modName];
                yield return(new ProgressReport(
                                 (float)modLoaded++ / (float)modLoadOrder.Count,
                                 sliderText,
                                 string.Format("Loading Mod: {0} {1}", modDef.Name, modDef.Version)
                                 ));

                try
                {
                    LoadMod(modDef);
                }
                catch (Exception e)
                {
                    Log($"Tried to load mod: {modDef.Name}, but something went wrong. Make sure all of your JSON is correct!");
                    Log($"\t{e.Message}");
                }
            }

            foreach (var modDef in willNotLoad)
            {
                Log($"Will not load {modDef}. It's lacking a dependancy or a conflict loaded before it.");
            }

            stopwatch.Stop();
            Log("");
            LogWithDate($"Done pre-load mods. Elapsed running time: {stopwatch.Elapsed.TotalSeconds} seconds\n");
            Log("----------\n");

            // write out harmony summary
            PrintHarmonySummary(HarmonySummaryPath);

            // write out load order
            File.WriteAllText(LoadOrderPath, JsonConvert.SerializeObject(modLoadOrder, Formatting.Indented));

            hasLoadedMods = true;

            yield break;
        }
Ejemplo n.º 2
0
        internal static IEnumerator <ProgressReport> LoadMoadsLoop()
        {
            stopwatch.Start();

            Log("");
            yield return(new ProgressReport(1, "Initializing Mods", ""));

            // find all sub-directories that have a mod.json file
            var modDirectories = Directory.GetDirectories(ModsDirectory)
                                 .Where(x => File.Exists(Path.Combine(x, MOD_JSON_NAME))).ToArray();

            if (modDirectories.Length == 0)
            {
                Log("No ModTek-compatable mods found.");
                yield break;
            }

            // create ModDef objects for each mod.json file
            var modDefs = new Dictionary <string, ModDef>();

            foreach (var modDirectory in modDirectories)
            {
                ModDef modDef;
                var    modDefPath = Path.Combine(modDirectory, MOD_JSON_NAME);

                try
                {
                    modDef = ModDef.CreateFromPath(modDefPath);
                }
                catch (Exception e)
                {
                    Log($"Caught exception while parsing {MOD_JSON_NAME} at path {modDefPath}");
                    Log($"\t{e.Message}");
                    continue;
                }

                if (!modDef.Enabled)
                {
                    Log($"Will not load {modDef.Name} because it's disabled.");
                    continue;
                }

                if (modDefs.ContainsKey(modDef.Name))
                {
                    Log($"Already loaded a mod named {modDef.Name}. Skipping load from {modDef.Directory}.");
                    continue;
                }

                modDefs.Add(modDef.Name, modDef);
            }

            Log("");
            modLoadOrder = GetLoadOrder(modDefs, out var willNotLoad);
            foreach (var modName in willNotLoad)
            {
                Log($"Will not load {modName} because it's lacking a dependancy or a conflict loaded before it.");
            }
            Log("");

            // lists guarentee order
            var modLoaded = 0;

            foreach (var modName in modLoadOrder)
            {
                var modDef = modDefs[modName];
                yield return(new ProgressReport(modLoaded++ / ((float)modLoadOrder.Count), "Initializing Mods", $"{modDef.Name} {modDef.Version}"));

                try
                {
                    LoadMod(modDef);
                }
                catch (Exception e)
                {
                    Log($"Tried to load mod: {modDef.Name}, but something went wrong. Make sure all of your JSON is correct!");
                    Log($"\t{e.Message}");
                }
            }

            PrintHarmonySummary(HarmonySummaryPath);
            WriteJsonFile(LoadOrderPath, modLoadOrder);
            stopwatch.Stop();

            yield break;
        }