Skip to content

Commit

Permalink
[KKS] Fix custom non-restockable shop items always having only 1 in s…
Browse files Browse the repository at this point in the history
…tock
  • Loading branch information
ManlyMarco committed Dec 8, 2021
1 parent d252b84 commit 5c23d18
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/KKSAPI/MainGame/StoreApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public static void ApplyHooksIfNeeded()
var h = new Harmony(nameof(StoreApi) + "_Hooks");
h.PatchMoveNext(AccessTools.Method(typeof(ShopView), nameof(ShopView.Start)), transpiler: new HarmonyMethod(typeof(StoreHooks), nameof(ShopInitTpl)));
h.PatchMoveNext(AccessTools.Method(typeof(ShopView), nameof(ShopView.Buy)), transpiler: new HarmonyMethod(typeof(StoreHooks), nameof(OnBuyTpl)));
h.Patch(AccessTools.Method(typeof(ShopView), nameof(ShopView.Conditions)), prefix: new HarmonyMethod(typeof(StoreHooks), nameof(ConditionCheckPrefix)));
}

public static IEnumerable<CodeInstruction> ShopInitTpl(IEnumerable<CodeInstruction> instructions)
Expand Down Expand Up @@ -328,6 +329,18 @@ private static void OnBuyHook(ShopItem itemBought)
}
}
}

private static bool ConditionCheckPrefix(ref int __result, ShopView __instance, ShopInfo.Param param)
{
// If not a custom item and not set to be non-restockable, use stock game code
if (param.ID < MinimumItemId || param.InitType != -1) return true;

// If custom item is set as restockable then base game code will only allow 1 of this item
// to be bought and then set it as sold out, so we need to patch around that here
__instance.player.buyNumTable.TryGetValue(param.ID, out var bought);
__result = param.Num - bought;
return false;
}
}
}
}

0 comments on commit 5c23d18

Please sign in to comment.