From 59eb9154757ff9457581f7552866bc9eb369a9c7 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Fri, 11 Sep 2020 06:54:24 -0600 Subject: [PATCH] Make extra included games explicit --- Wabbajack.Common/GameMetaData.cs | 3 +++ Wabbajack.Common/Json.cs | 8 ++++---- Wabbajack.Common/Utils.cs | 8 ++++++++ Wabbajack.Lib/CompilerSettings.cs | 19 +++++++++++++++++++ Wabbajack.Lib/MO2Compiler.cs | 19 ++++++++++--------- Wabbajack.Test/SanityTests.cs | 5 +++++ 6 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 Wabbajack.Lib/CompilerSettings.cs diff --git a/Wabbajack.Common/GameMetaData.cs b/Wabbajack.Common/GameMetaData.cs index 9ec216a9..aa9279ff 100644 --- a/Wabbajack.Common/GameMetaData.cs +++ b/Wabbajack.Common/GameMetaData.cs @@ -4,10 +4,13 @@ using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using Wabbajack.Common.StoreHandlers; namespace Wabbajack.Common { + [JsonConverter(typeof(StringEnumConverter))] public enum Game { //MO2 GAMES diff --git a/Wabbajack.Common/Json.cs b/Wabbajack.Common/Json.cs index 78c6b611..006421e9 100644 --- a/Wabbajack.Common/Json.cs +++ b/Wabbajack.Common/Json.cs @@ -51,18 +51,18 @@ namespace Wabbajack.Common File.WriteAllText(filename, JsonConvert.SerializeObject(obj, Formatting.Indented, JsonSettings)); } - public static void ToJson(this T obj, Stream stream) + public static void ToJson(this T obj, Stream stream, bool useGenericSettings = false) { using var tw = new StreamWriter(stream, Encoding.UTF8, bufferSize: 1024, leaveOpen: true); using var writer = new JsonTextWriter(tw); - var ser = JsonSerializer.Create(JsonSettings); + var ser = JsonSerializer.Create(useGenericSettings ? GenericJsonSettings : JsonSettings); ser.Serialize(writer, obj); } - public static async ValueTask ToJsonAsync(this T obj, AbsolutePath path) + public static async ValueTask ToJsonAsync(this T obj, AbsolutePath path, bool useGenericSettings = false) { await using var fs = await path.Create(); - obj.ToJson(fs); + obj.ToJson(fs, useGenericSettings); } public static string ToJson(this T obj, bool useGenericSettings = false) diff --git a/Wabbajack.Common/Utils.cs b/Wabbajack.Common/Utils.cs index a9df9d68..833bcfaa 100644 --- a/Wabbajack.Common/Utils.cs +++ b/Wabbajack.Common/Utils.cs @@ -783,6 +783,14 @@ namespace Wabbajack.Common .Build(); return d.Deserialize(new StringReader(s)); } + + public static T FromYaml(this AbsolutePath s) + { + var d = new DeserializerBuilder() + .WithNamingConvention(PascalCaseNamingConvention.Instance) + .Build(); + return d.Deserialize(new StringReader((string)s)); + } public static void LogStatus(string s) { Status(s); diff --git a/Wabbajack.Lib/CompilerSettings.cs b/Wabbajack.Lib/CompilerSettings.cs new file mode 100644 index 00000000..4de29596 --- /dev/null +++ b/Wabbajack.Lib/CompilerSettings.cs @@ -0,0 +1,19 @@ +using System.Threading.Tasks; +using Wabbajack.Common; + +namespace Wabbajack.Lib +{ + public class CompilerSettings + { + public const string FileName = "compiler_settings.json"; + + public static async Task Load(AbsolutePath folder) + { + var path = folder.Combine(FileName); + return !path.IsFile ? new CompilerSettings() : path.FromJson(); + } + + public Game[] IncludedGames { get; set; } = new Game[0]; + public string[] OtherProfiles { get; set; } = new string[0]; + } +} diff --git a/Wabbajack.Lib/MO2Compiler.cs b/Wabbajack.Lib/MO2Compiler.cs index 7b935812..c4fa92ca 100644 --- a/Wabbajack.Lib/MO2Compiler.cs +++ b/Wabbajack.Lib/MO2Compiler.cs @@ -31,11 +31,8 @@ namespace Wabbajack.Lib public override AbsolutePath GamePath { get; } public GameMetaData CompilingGame { get; } - - /// - /// All games available for sourcing during compilation (including the Compiling Game) - /// - public List AvailableGames { get; } + + public CompilerSettings Settings { get; set; } public override AbsolutePath ModListOutputFolder => ((RelativePath)"output_folder").RelativeToEntryPoint(); @@ -66,8 +63,7 @@ namespace Wabbajack.Lib CompilingGame = GameRegistry.Games.First(g => g.Value.MO2Name == mo2game).Value; GamePath = new AbsolutePath((string)MO2Ini.General.gamePath.Replace("\\\\", "\\")); ModListOutputFile = outputFile; - - AvailableGames = CompilingGame.CanSourceFrom.Cons(CompilingGame.Game).Where(g => g.MetaData().IsInstalled).ToList(); + Settings = new CompilerSettings(); } public AbsolutePath MO2DownloadsFolder @@ -91,6 +87,11 @@ namespace Wabbajack.Lib Queue.SetActiveThreadsObservable(ConstructDynamicNumThreads(await RecommendQueueSize())); UpdateTracker.Reset(); UpdateTracker.NextStep("Gathering information"); + + Utils.Log($"Loading compiler Settings"); + Settings = await CompilerSettings.Load(MO2ProfileDir); + Settings.IncludedGames = Settings.IncludedGames.Add(CompilingGame.Game); + Info("Looking for other profiles"); var otherProfilesPath = MO2ProfileDir.Combine("otherprofiles.txt"); SelectedProfiles = new HashSet(); @@ -121,7 +122,7 @@ namespace Wabbajack.Lib { MO2Folder, GamePath, MO2DownloadsFolder }; - roots.AddRange(AvailableGames.Select(g => g.MetaData().GameLocation())); + roots.AddRange(Settings.IncludedGames.Select(g => g.MetaData().GameLocation())); } else { @@ -206,7 +207,7 @@ namespace Wabbajack.Lib if (UseGamePaths) { - foreach (var ag in AvailableGames) + foreach (var ag in Settings.IncludedGames) { try { diff --git a/Wabbajack.Test/SanityTests.cs b/Wabbajack.Test/SanityTests.cs index 365c8641..b47888e3 100644 --- a/Wabbajack.Test/SanityTests.cs +++ b/Wabbajack.Test/SanityTests.cs @@ -508,6 +508,11 @@ namespace Wabbajack.Test var profile = utils.AddProfile(); var mod = await utils.AddMod(); + await new CompilerSettings() + { + IncludedGames = new []{Game.Morrowind} + }.ToJsonAsync(utils.MO2Folder.Combine("profiles", profile, CompilerSettings.FileName), true); + Game.SkyrimSpecialEdition.MetaData().CanSourceFrom = new[] {Game.Morrowind, Game.Skyrim}; // Morrowind file with different name