From a764bb324722b28bd4e6756330608afd4cd5be0c Mon Sep 17 00:00:00 2001 From: siimav Date: Mon, 19 Aug 2024 17:11:24 +0300 Subject: [PATCH] Harmony-patch ROHS config can afford query to factor in unlock credit. Also change other Harmony patches to use the same format. --- Source/RP0/Harmony/CustomBarnKit.cs | 11 ++++++++- Source/RP0/Harmony/KSCSwitcher.cs | 7 +++--- Source/RP0/Harmony/ROHeatshields.cs | 37 +++++++++++++++++++++++++++++ Source/RP0/RP0.csproj | 1 + 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 Source/RP0/Harmony/ROHeatshields.cs diff --git a/Source/RP0/Harmony/CustomBarnKit.cs b/Source/RP0/Harmony/CustomBarnKit.cs index 74c69fb4d84..c2bdf7c1d0a 100644 --- a/Source/RP0/Harmony/CustomBarnKit.cs +++ b/Source/RP0/Harmony/CustomBarnKit.cs @@ -1,4 +1,5 @@ using HarmonyLib; +using System; using System.Reflection; namespace RP0.Harmony @@ -6,7 +7,15 @@ namespace RP0.Harmony [HarmonyPatch] internal class PatchCustomBarnKit_LoadUpgradesPrices { - static MethodBase TargetMethod() => AccessTools.TypeByName("CustomBarnKit.CustomBarnKit").GetMethod("LoadUpgradesPrices", AccessTools.all); + internal static readonly Type _type = AccessTools.TypeByName("CustomBarnKit.CustomBarnKit"); + + internal static MethodBase TargetMethod() => AccessTools.Method(_type, "LoadUpgradesPrices"); + + [HarmonyPrepare] + internal static bool Prepare() + { + return _type != null; + } [HarmonyPrefix] internal static void Prefix_LoadUpgradesPrices(ref bool ___varLoaded, out bool __state) diff --git a/Source/RP0/Harmony/KSCSwitcher.cs b/Source/RP0/Harmony/KSCSwitcher.cs index 87de274a1c6..7caea3fbfdb 100644 --- a/Source/RP0/Harmony/KSCSwitcher.cs +++ b/Source/RP0/Harmony/KSCSwitcher.cs @@ -1,4 +1,5 @@ using HarmonyLib; +using System; using System.Reflection; namespace RP0.Harmony @@ -6,14 +7,14 @@ namespace RP0.Harmony [HarmonyPatch] internal class PatchKSCSwitcher { - internal static readonly System.Type KSCSwitcherType = AccessTools.TypeByName("regexKSP.KSCSwitcher"); + internal static readonly System.Type _type = AccessTools.TypeByName("regexKSP.KSCSwitcher"); - internal static MethodBase TargetMethod() => KSCSwitcherType == null ? null : AccessTools.Method(KSCSwitcherType, "SetSite", new System.Type[] { typeof(ConfigNode) }); + internal static MethodBase TargetMethod() => AccessTools.Method(_type, "SetSite", new Type[] { typeof(ConfigNode) }); [HarmonyPrepare] internal static bool Prepare() { - return KSCSwitcherType != null; + return _type != null; } [HarmonyPostfix] diff --git a/Source/RP0/Harmony/ROHeatshields.cs b/Source/RP0/Harmony/ROHeatshields.cs new file mode 100644 index 00000000000..a4a7f3bed78 --- /dev/null +++ b/Source/RP0/Harmony/ROHeatshields.cs @@ -0,0 +1,37 @@ +using HarmonyLib; +using System; +using System.Reflection; + +namespace RP0.Harmony +{ + [HarmonyPatch] + internal class ROHSPartModulePatcher + { + internal static readonly Type _type = AccessTools.TypeByName("ROHeatshields.ModuleROHeatshield"); + internal static MethodInfo _mi; + + internal static MethodBase TargetMethod() => _mi; + + [HarmonyPrepare] + internal static bool Prepare() + { + if (_type != null) + { + _mi ??= AccessTools.Method(_type, "CanAffordEntryCost", new Type[] { typeof(float) }); + } + return _mi != null; + } + + [HarmonyPrefix] + internal static bool Prefix_CanAffordEntryCost(float cost, ref bool __result) + { + var cmq = CurrencyModifierQueryRP0.RunQuery(TransactionReasonsRP0.PartOrUpgradeUnlock, -cost, 0d, 0d); + double postCMQcost = -cmq.GetTotal(CurrencyRP0.Funds, false); + double credit = UnlockCreditHandler.Instance.TotalCredit; + cmq.AddPostDelta(CurrencyRP0.Funds, credit, true); + __result = cmq.CanAfford(); + + return false; + } + } +} diff --git a/Source/RP0/RP0.csproj b/Source/RP0/RP0.csproj index 999de57c968..cd560a76ac6 100644 --- a/Source/RP0/RP0.csproj +++ b/Source/RP0/RP0.csproj @@ -50,6 +50,7 @@ +