From 480206ff14af3d8deeb6dd8f6b7b5228a61a69ae Mon Sep 17 00:00:00 2001 From: gotmachine <24925209+gotmachine@users.noreply.github.com> Date: Mon, 25 Mar 2024 19:30:17 +0100 Subject: [PATCH] New QoL/Perf patch : OptionalMakingHistoryDLCFeatures as requested in issue #218 --- GameData/KSPCommunityFixes/Settings.cfg | 8 +++ KSPCommunityFixes/Internal/PatchSettings.cs | 28 ++++++++ KSPCommunityFixes/KSPCommunityFixes.csproj | 1 + .../QoL/OptionalMakingHistoryDLCFeatures.cs | 72 +++++++++++++++++++ README.md | 4 ++ 5 files changed, 113 insertions(+) create mode 100644 KSPCommunityFixes/QoL/OptionalMakingHistoryDLCFeatures.cs diff --git a/GameData/KSPCommunityFixes/Settings.cfg b/GameData/KSPCommunityFixes/Settings.cfg index e6d4d44..5bc50e5 100644 --- a/GameData/KSPCommunityFixes/Settings.cfg +++ b/GameData/KSPCommunityFixes/Settings.cfg @@ -284,6 +284,14 @@ KSP_COMMUNITY_FIXES // Invert the editor undo state capturing logic so part tweaks aren't lost when undoing. BetterEditorUndoRedo = true + // Allow to disable the Making History DLC mission editor and additional launch sites features + // to decrease memory usage and increase loading speed. The Making History parts will still be + // available. Can be toggled from the KSPCF in-game settings (requires a restart). + OptionalMakingHistoryDLCFeatures = true + + // Optional MM-patcheable toggle to always disable the MH features + OptionalMakingHistoryDLCFeaturesAlwaysDisable = false + // ########################## // Performance tweaks // ########################## diff --git a/KSPCommunityFixes/Internal/PatchSettings.cs b/KSPCommunityFixes/Internal/PatchSettings.cs index d75af1d..7add0b1 100644 --- a/KSPCommunityFixes/Internal/PatchSettings.cs +++ b/KSPCommunityFixes/Internal/PatchSettings.cs @@ -18,6 +18,7 @@ class PatchSettings : BasePatch private static int entryCount = 0; private static AltimeterHorizontalPosition altimeterPatch; private static DisableManeuverTool maneuverToolPatch; + private static OptionalMakingHistoryDLCFeatures disableMHPatch; protected override void ApplyPatches(List patches) { @@ -39,6 +40,10 @@ protected override void ApplyPatches(List patches) if (maneuverToolPatch != null) entryCount++; + disableMHPatch = KSPCommunityFixes.GetPatchInstance(); + if (disableMHPatch != null) + entryCount++; + if (KSPCFFastLoader.IsPatchEnabled) entryCount++; @@ -61,6 +66,22 @@ static void GameplaySettingsScreen_DrawMiniSettings_Postfix(ref DialogGUIBase[] modifiedResult[count] = new DialogGUIBox(KSPCommunityFixes.LOC_KSPCF_Title, -1f, 18f, null); count++; + if (disableMHPatch != null) + { + DialogGUIToggle toggle = new DialogGUIToggle(OptionalMakingHistoryDLCFeatures.isMHEnabled, + () => (!OptionalMakingHistoryDLCFeatures.isMHEnabled) + ? Localizer.Format("#autoLOC_6001071") //"Disabled" + : Localizer.Format("#autoLOC_6001072"), //"Enabled" + b => OptionalMakingHistoryDLCFeatures.isMHEnabled = b, 150f); + toggle.tooltipText = OptionalMakingHistoryDLCFeatures.LOC_SettingsTooltip; + toggle.OptionInteractableCondition = () => !OptionalMakingHistoryDLCFeatures.isMHDisabledFromConfig; + + modifiedResult[count] = new DialogGUIHorizontalLayout(TextAnchor.MiddleLeft, + new DialogGUILabel(() => Localizer.Format(OptionalMakingHistoryDLCFeatures.LOC_MHDLC), 150f), //"Maneuver Tool" + toggle, new DialogGUIFlexibleSpace()); + count++; + } + if (maneuverToolPatch != null) { DialogGUIToggle toggle = new DialogGUIToggle(DisableManeuverTool.enableManeuverTool, @@ -120,6 +141,13 @@ static void GameplaySettingsScreen_DrawMiniSettings_Postfix(ref DialogGUIBase[] static void GameplaySettingsScreen_ApplySettings_Postfix() { + if (disableMHPatch != null) + { + ConfigNode node = new ConfigNode(); + node.AddValue(nameof(OptionalMakingHistoryDLCFeatures.isMHEnabled), OptionalMakingHistoryDLCFeatures.isMHEnabled); + SaveData(node); + } + if (maneuverToolPatch != null) { ConfigNode node = new ConfigNode(); diff --git a/KSPCommunityFixes/KSPCommunityFixes.csproj b/KSPCommunityFixes/KSPCommunityFixes.csproj index 0b27596..1ffe9e8 100644 --- a/KSPCommunityFixes/KSPCommunityFixes.csproj +++ b/KSPCommunityFixes/KSPCommunityFixes.csproj @@ -174,6 +174,7 @@ + diff --git a/KSPCommunityFixes/QoL/OptionalMakingHistoryDLCFeatures.cs b/KSPCommunityFixes/QoL/OptionalMakingHistoryDLCFeatures.cs new file mode 100644 index 0000000..6d9c23f --- /dev/null +++ b/KSPCommunityFixes/QoL/OptionalMakingHistoryDLCFeatures.cs @@ -0,0 +1,72 @@ +using System; +using Expansions; +using HarmonyLib; +using System.Collections; +using System.Collections.Generic; +using System.IO; + +namespace KSPCommunityFixes.QoL +{ + internal class OptionalMakingHistoryDLCFeatures : BasePatch + { + private const string MH_EXPANSION_FILE = "makinghistory.kspexpansion"; + private const string SETTINGS_TOGGLE_VALUE_NAME = "OptionalMakingHistoryDLCFeaturesAlwaysDisable"; + internal static string LOC_MHDLC = "Making History features"; + internal static string LOC_SettingsTooltip = "Disable the Making History DLC mission editor and additional launch sites\nThe Making History parts will still be available\nWill reduce memory usage and increase loading speed\nChanges will take affect after restarting KSP"; + internal static bool isMHEnabled = true; + internal static bool isMHDisabledFromConfig = false; + + protected override Version VersionMin => new Version(1, 12, 3); + + protected override void ApplyPatches(List patches) + { + if (KSPCommunityFixes.SettingsNode.TryGetValue(SETTINGS_TOGGLE_VALUE_NAME, ref isMHDisabledFromConfig) && isMHDisabledFromConfig) + isMHEnabled = false; + + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(ExpansionsLoader), nameof(ExpansionsLoader.InitializeExpansion)), + this)); + } + + protected override void OnLoadData(ConfigNode node) + { + if (!isMHDisabledFromConfig) + node.TryGetValue(nameof(isMHEnabled), ref isMHEnabled); + } + + protected override bool CanApplyPatch(out string reason) + { + if (Directory.Exists(KSPExpansionsUtils.ExpansionsGameDataPath)) + { + foreach (string fileName in Directory.GetFiles(KSPExpansionsUtils.ExpansionsGameDataPath, "*" + ExpansionsLoader.expansionsMasterExtension, SearchOption.AllDirectories)) + { + if (fileName.EndsWith(MH_EXPANSION_FILE)) + { + reason = null; + return true; + } + } + } + + reason = "Making History DLC not installed"; + return false; + } + + static bool ExpansionsLoader_InitializeExpansion_Prefix(string expansionFile, ref IEnumerator __result) + { + if (!isMHEnabled && expansionFile.EndsWith(MH_EXPANSION_FILE)) + { + __result = EmptyEnumerator(); + return false; + } + + return true; + } + + static IEnumerator EmptyEnumerator() + { + yield break; + } + } +} diff --git a/README.md b/README.md index 595aa4a..7a2b19e 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ User options are available from the "ESC" in-game settings menu :