Add multi-buy functionality for block purchasing
This commit is contained in:
@@ -16,6 +16,9 @@ namespace TerraTech {
|
|||||||
|
|
||||||
public static ConfigEntry<bool> debug;
|
public static ConfigEntry<bool> debug;
|
||||||
public static ConfigEntry<bool> debugBuyAll;
|
public static ConfigEntry<bool> debugBuyAll;
|
||||||
|
public static ConfigEntry<bool> debugMultiBuy;
|
||||||
|
public static ConfigEntry<int> multiBuyAmount;
|
||||||
|
|
||||||
public static ConfigEntry<float> xpMultiplier;
|
public static ConfigEntry<float> xpMultiplier;
|
||||||
public static ConfigEntry<float> moneyMultiplier;
|
public static ConfigEntry<float> moneyMultiplier;
|
||||||
public static ConfigEntry<float> heartbeatIntervalMultiplier;
|
public static ConfigEntry<float> heartbeatIntervalMultiplier;
|
||||||
@@ -27,6 +30,10 @@ namespace TerraTech {
|
|||||||
public void Awake() {
|
public void Awake() {
|
||||||
debug = Config.Bind("General", "Debug", false);
|
debug = Config.Bind("General", "Debug", false);
|
||||||
debugBuyAll = Config.Bind("General", "Debug Buy All", false);
|
debugBuyAll = Config.Bind("General", "Debug Buy All", false);
|
||||||
|
debugMultiBuy = Config.Bind("General", "Debug Multi Buy", false);
|
||||||
|
multiBuyAmount =
|
||||||
|
Config.Bind("General", "Multi Buy Amount", 10,
|
||||||
|
new ConfigDescription("Multi Buy Amount", new AcceptableValueRange<int>(1, 10000)));
|
||||||
float min = 0.01f;
|
float min = 0.01f;
|
||||||
float max = 32f;
|
float max = 32f;
|
||||||
|
|
||||||
|
71
Projects/TerraTech/TerraTech/MultiBuy.cs
Normal file
71
Projects/TerraTech/TerraTech/MultiBuy.cs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using HarmonyLib;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.EventSystems;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
namespace TerraTech {
|
||||||
|
[HarmonyPatch]
|
||||||
|
public class MultiBuy {
|
||||||
|
public static UIShopBlockSelect panel;
|
||||||
|
public static Traverse panelTraverse;
|
||||||
|
|
||||||
|
[HarmonyPostfix]
|
||||||
|
[HarmonyPatch(typeof(UIShopBlockSelect), "OnSpawn")]
|
||||||
|
public static void PostfixCreate(UIShopBlockSelect __instance) {
|
||||||
|
panel = __instance;
|
||||||
|
if (Main.debugBuyAll.Value)
|
||||||
|
Console.WriteLine("UISnapshotPanel.OnPool: {0}", __instance);
|
||||||
|
panelTraverse = Traverse.Create(__instance);
|
||||||
|
var placeButton = panelTraverse.Field("m_PurchaseBlockButton").GetValue<Button>();
|
||||||
|
placeButton.gameObject.AddComponent<MultiBuyRightClickHandler>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MultiBuyRightClickHandler : MonoBehaviour, IPointerClickHandler {
|
||||||
|
// private void Awake() {
|
||||||
|
// }
|
||||||
|
|
||||||
|
public void OnPointerClick(PointerEventData eventData) {
|
||||||
|
if (Main.debugBuyAll.Value)
|
||||||
|
Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: {0} {1}", gameObject.name,
|
||||||
|
eventData.button);
|
||||||
|
try {
|
||||||
|
if (eventData.button == PointerEventData.InputButton.Right) {
|
||||||
|
UIBlockSelectGrid grid = MultiBuy.panelTraverse.Field("m_Grid").GetValue<UIBlockSelectGrid>();
|
||||||
|
BlockTypes blockTypes;
|
||||||
|
bool ok = grid.TryGetSelection(out blockTypes);
|
||||||
|
if (!ok) {
|
||||||
|
if (Main.debugBuyAll.Value)
|
||||||
|
Console.WriteLine(
|
||||||
|
"MultiBuyRightClickHandler.OnPointerClick: Failed to get block selection from grid");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint shopBlockPoolID = MultiBuy.panelTraverse.Field("m_ShopBlockPoolID").GetValue<uint>();
|
||||||
|
|
||||||
|
MethodInfo canPurchaseMethod =
|
||||||
|
AccessTools.Method(typeof(UIShopBlockSelect), "CanPurchaseBlock", new[] { typeof(BlockTypes) });
|
||||||
|
Func<BlockTypes, bool> canPurchase = (Func<BlockTypes, bool>)Delegate.CreateDelegate(
|
||||||
|
typeof(Func<BlockTypes, bool>), MultiBuy.panel, canPurchaseMethod);
|
||||||
|
|
||||||
|
for (int i = 0; i < Main.multiBuyAmount.Value; i++) {
|
||||||
|
if (!canPurchase.Invoke(blockTypes)) {
|
||||||
|
if (Main.debugBuyAll.Value)
|
||||||
|
Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: Can purchase no more {0}",
|
||||||
|
blockTypes);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Singleton.Manager<ManPurchases>.inst.RequestPurchaseBlock(shopBlockPoolID, blockTypes, 1);
|
||||||
|
if (Main.debugBuyAll.Value)
|
||||||
|
Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: Purchased {0} block",
|
||||||
|
blockTypes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (Main.debugBuyAll.Value)
|
||||||
|
Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: Exception occurred: {0}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -61,6 +61,7 @@
|
|||||||
<Compile Include="ProjectilePatch.cs" />
|
<Compile Include="ProjectilePatch.cs" />
|
||||||
<Compile Include="TankManager.cs" />
|
<Compile Include="TankManager.cs" />
|
||||||
<Compile Include="UISnapshotPanelBuyAll.cs" />
|
<Compile Include="UISnapshotPanelBuyAll.cs" />
|
||||||
|
<Compile Include="MultiBuy.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="0Harmony">
|
<Reference Include="0Harmony">
|
||||||
|
14
Projects/TerraTech/TerraTech/log.cs
Normal file
14
Projects/TerraTech/TerraTech/log.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
static void Postfix(UIItemSelectGrid __instance) {
|
||||||
|
try {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.AppendLine("--------------------");
|
||||||
|
sb.AppendLine("void UIItemSelectGrid::Repopulate()");
|
||||||
|
sb.Append("- __instance: ").AppendLine(__instance.ToString());
|
||||||
|
foreach (var item in __instance.m_FilteredItemList) {
|
||||||
|
sb.Append("- item: ").AppendLine(item.ToString());
|
||||||
|
}
|
||||||
|
UnityExplorer.ExplorerCore.Log(sb.ToString());
|
||||||
|
} catch (System.Exception ex) {
|
||||||
|
UnityExplorer.ExplorerCore.LogWarning($"Exception in patch of void UIItemSelectGrid::Repopulate():\n{ex}");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user