// Create a file for an option group from given data. internal static void CreateOptionGroup(DirectoryInfo baseFolder, SelectType type, string name, int priority, int index, string desc, IEnumerable <ISubMod> subMods) { switch (type) { case SelectType.Multi: { var group = new MultiModGroup() { Name = name, Description = desc, Priority = priority, }; group.PrioritizedOptions.AddRange(subMods.OfType <SubMod>().Select((s, idx) => (s, idx))); IModGroup.Save(group, baseFolder, index); break; } case SelectType.Single: { var group = new SingleModGroup() { Name = name, Description = desc, Priority = priority, }; group.OptionData.AddRange(subMods.OfType <SubMod>()); IModGroup.Save(group, baseFolder, index); break; } } }
private static bool MigrateV0ToV1(Mod mod, JObject json) { if (mod.FileVersion > 0) { return(false); } var swaps = json["FileSwaps"]?.ToObject <Dictionary <Utf8GamePath, FullPath> >() ?? new Dictionary <Utf8GamePath, FullPath>(); var groups = json["Groups"]?.ToObject <Dictionary <string, OptionGroupV0> >() ?? new Dictionary <string, OptionGroupV0>(); var priority = 1; var seenMetaFiles = new HashSet <FullPath>(); foreach (var group in groups.Values) { ConvertGroup(mod, group, ref priority, seenMetaFiles); } foreach (var unusedFile in mod.FindUnusedFiles().Where(f => !seenMetaFiles.Contains(f))) { if (unusedFile.ToGamePath(mod.ModPath, out var gamePath) && !mod._default.FileData.TryAdd(gamePath, unusedFile)) { PluginLog.Error($"Could not add {gamePath} because it already points to {mod._default.FileData[ gamePath ]}."); } } mod._default.FileSwapData.Clear(); mod._default.FileSwapData.EnsureCapacity(swaps.Count); foreach (var(gamePath, swapPath) in swaps) { mod._default.FileSwapData.Add(gamePath, swapPath); } mod._default.IncorporateMetaChanges(mod.ModPath, true); foreach (var(group, index) in mod.Groups.WithIndex()) { IModGroup.Save(group, mod.ModPath, index); } // Delete meta files. foreach (var file in seenMetaFiles.Where(f => f.Exists)) { try { File.Delete(file.FullName); } catch (Exception e) { PluginLog.Warning($"Could not delete meta file {file.FullName} during migration:\n{e}"); } } // Delete old meta files. var oldMetaFile = Path.Combine(mod.ModPath.FullName, "metadata_manipulations.json"); if (File.Exists(oldMetaFile)) { try { File.Delete(oldMetaFile); } catch (Exception e) { PluginLog.Warning($"Could not delete old meta file {oldMetaFile} during migration:\n{e}"); } } mod.FileVersion = 1; mod.SaveDefaultMod(); mod.SaveMeta(); return(true); }