Skip to content

Commit

Permalink
Harmony-patch ROHS config can afford query to factor in unlock credit.
Browse files Browse the repository at this point in the history
Also change other Harmony patches to use the same format.
  • Loading branch information
siimav committed Aug 19, 2024
1 parent 12339d7 commit a764bb3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Source/RP0/Harmony/CustomBarnKit.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using HarmonyLib;
using System;
using System.Reflection;

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)
Expand Down
7 changes: 4 additions & 3 deletions Source/RP0/Harmony/KSCSwitcher.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using HarmonyLib;
using System;
using System.Reflection;

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]
Expand Down
37 changes: 37 additions & 0 deletions Source/RP0/Harmony/ROHeatshields.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions Source/RP0/RP0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Harmony\FlightInputHandler.cs" />
<Compile Include="Harmony\ModuleRCS.cs" />
<Compile Include="Harmony\RealAntennas.cs" />
<Compile Include="Harmony\ROHeatshields.cs" />
<Compile Include="ModIntegrations\KSCSwitcherInterop.cs" />
<Compile Include="ModIntegrations\TFInterop.cs" />
<Compile Include="Programs\ProgramModifier.cs" />
Expand Down

0 comments on commit a764bb3

Please sign in to comment.