Example #1
0
        public ModInfo FindOrCreateModSettings(ResourceMod mod)
        {
            var settings = FindModSettings(mod.ModBasePath.Name);

            if (settings != null)
            {
                settings.Mod = mod;
                return(settings);
            }

            return(AddModSettings(mod));
        }
Example #2
0
        public ModInfo AddModSettings(ResourceMod mod)
        {
            var entry = new ModInfo
            {
                Priority   = ModSettings.Count,
                FolderName = mod.ModBasePath.Name,
                Enabled    = true,
                Mod        = mod
            };

#if DEBUG
            PluginLog.Information("creating mod settings {ModName}", entry.FolderName);
#endif

            ModSettings.Add(entry);
            return(entry);
        }
Example #3
0
        public void Load(bool invertOrder = false)
        {
            // find the collection json
            var collectionPath = Path.Combine(_basePath.FullName, "collection.json");

            if (File.Exists(collectionPath))
            {
                try
                {
                    ModSettings = JsonConvert.DeserializeObject <List <ModInfo> >(File.ReadAllText(collectionPath));
                    ModSettings = ModSettings.OrderBy(x => x.Priority).ToList();
                }
                catch (Exception e)
                {
                    PluginLog.Error($"failed to read log collection information, failed path: {collectionPath}, err: {e.Message}");
                }
            }

#if DEBUG
            if (ModSettings != null)
            {
                foreach (var ms in ModSettings)
                {
                    PluginLog.Information(
                        "mod: {ModName} Enabled: {Enabled} Priority: {Priority}",
                        ms.FolderName, ms.Enabled, ms.Priority
                        );
                }
            }
#endif

            ModSettings ??= new();
            var foundMods = new List <string>();

            foreach (var modDir in _basePath.EnumerateDirectories())
            {
                var metaFile = modDir.EnumerateFiles().FirstOrDefault(f => f.Name == "meta.json");

                if (metaFile == null)
                {
                    PluginLog.LogError("mod meta is missing for resource mod: {ResourceModLocation}", modDir);
                    continue;
                }

                var meta = JsonConvert.DeserializeObject <ModMeta>(File.ReadAllText(metaFile.FullName));

                var mod = new ResourceMod
                {
                    Meta        = meta,
                    ModBasePath = modDir
                };

                var modEntry = FindOrCreateModSettings(mod);
                foundMods.Add(modDir.Name);
                mod.RefreshModFiles();
            }

            // remove any mods from the collection we didn't find
            ModSettings = ModSettings.Where(
                x =>
                foundMods.Any(
                    fm => string.Equals(x.FolderName, fm, StringComparison.InvariantCultureIgnoreCase)
                    )
                ).ToList();

            // if anything gets removed above, the priority ordering gets f****d, so we need to resort and reindex them otherwise BAD THINGS HAPPEN
            ModSettings = ModSettings.OrderBy(x => x.Priority).ToList();
            var p = 0;
            foreach (var modSetting in ModSettings)
            {
                modSetting.Priority = p++;
            }

            // reorder the resourcemods list so we can just directly iterate
            EnabledMods = GetOrderedAndEnabledModList(invertOrder).ToArray();

            // write the collection metadata back to disk
            Save();
        }