Replace TechBoosterManager with ModuleFuelTankManager

This commit is contained in:
2025-02-24 18:48:57 +01:00
parent 4f51f8599e
commit d0ffe1951b
5 changed files with 64 additions and 88 deletions

View File

@@ -41,7 +41,7 @@ namespace TerraTech {
TankBeamManager.Setup(Config);
ModuleWeaponManager.Setup(Config);
ModuleHeartManager.Setup(Config);
TechBoosterManager.Setup(Config);
ModuleFuelTankManager.Setup(Config);
xpMultiplier =
Config.Bind("General", "XP Multiplier", 1f,

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using BepInEx.Configuration;
using HarmonyLib;
namespace TerraTech {
[HarmonyPatch]
public class ModuleFuelTankManager {
private static readonly MultipliedObjectManager<ModuleFuelTank> manager =
new MultipliedObjectManager<ModuleFuelTank>(ConfigureFuelTank);
public static ConfigEntry<bool> playerOnly;
public static ConfigEntry<float> fuelCapacityMultiplier;
public static ConfigEntry<float> fuelRefillMultiplier;
public static void Setup(ConfigFile config) {
float min = 0.01f;
float max = 32f;
playerOnly = config.Bind("FuelTank", "Player Only", false, new ConfigDescription("Player Only"));
playerOnly.SettingChanged += (sender, args) => DoPatch();
fuelCapacityMultiplier = config.Bind(
"FuelTank", "Fuel Capacity Multiplier", 1f,
new ConfigDescription("Fuel Capacity Multiplier", new AcceptableValueRange<float>(min, max)));
fuelCapacityMultiplier.SettingChanged += (sender, args) => DoPatch();
fuelRefillMultiplier =
config.Bind("FuelTank", "Fuel Refill Multiplier", 1f,
new ConfigDescription("Fuel Refill Multiplier", new AcceptableValueRange<float>(min, max)));
fuelRefillMultiplier.SettingChanged += (sender, args) => DoPatch();
}
private static void ConfigureFuelTank(MultipliedObject<ModuleFuelTank> obj) {
obj.AddField(new FieldConfiguration<float, float>("m_Capacity", fuelCapacityMultiplier, ShouldApply));
obj.AddField(new FieldConfiguration<float, float>("m_RefillRate", fuelRefillMultiplier, ShouldApply));
}
private static readonly Func<object, bool> ShouldApply = obj => {
if (!playerOnly.Value)
return true;
return CykUtil.IsObjectPlayerTank(obj);
};
[HarmonyPrefix]
[HarmonyPatch(typeof(ModuleFuelTank), "OnAttached")]
static void PostfixCreate(ModuleFuelTank __instance) {
manager.OnObjectAttached(__instance);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ModuleFuelTank), "OnDetaching")]
static void PostfixDestroy(ModuleFuelTank __instance) {
manager.OnObjectDetached(__instance);
}
public static void DoPatch() {
manager.ApplyAll();
}
}
}

View File

@@ -1,85 +0,0 @@
using System;
using System.Collections.Generic;
using BepInEx.Configuration;
using HarmonyLib;
namespace TerraTech {
[HarmonyPatch]
public class TechBoosterManager {
private struct TechBoosterData {
public float m_FuelCapacity;
public float m_FuelRefill;
}
private static Dictionary<TechBooster, TechBoosterData> techBoosterData =
new Dictionary<TechBooster, TechBoosterData>();
public static ConfigEntry<bool> playerOnly;
public static ConfigEntry<float> fuelCapacityMultiplier;
public static ConfigEntry<float> fuelRefillMultiplier;
public static void Setup(ConfigFile config) {
float min = 0.01f;
float max = 32f;
playerOnly = config.Bind("TechBooster", "Player Only", false, new ConfigDescription("Player Only"));
fuelCapacityMultiplier = config.Bind(
"TechBooster", "Fuel Capacity Multiplier", 1f,
new ConfigDescription("Fuel Capacity Multiplier", new AcceptableValueRange<float>(min, max)));
fuelRefillMultiplier =
config.Bind("TechBooster", "Fuel Refill Multiplier", 1f,
new ConfigDescription("Fuel Refill Multiplier", new AcceptableValueRange<float>(min, max)));
}
private static readonly Func<object, bool> ShouldApply = obj => {
if (!playerOnly.Value)
return true;
return CykUtil.IsObjectPlayerTank(obj);
};
[HarmonyPrefix]
[HarmonyPatch(typeof(TechBooster), "AddFuelTank")]
static void PostfixCreate(TechBooster __instance, ref ModuleFuelTank tank) {
var trav = Traverse.Create(__instance);
var fuelCapacityField = trav.Field("m_FuelCapacity");
var fuelRefillField = trav.Field("m_FuelRefill");
TechBoosterData boosterData;
bool ok = techBoosterData.TryGetValue(__instance, out boosterData);
if (ok) {
boosterData.m_FuelCapacity = fuelCapacityField.GetValue<float>() + tank.Capacity;
boosterData.m_FuelRefill = fuelRefillField.GetValue<float>() + tank.RefillRate;
} else {
boosterData = new TechBoosterData { m_FuelCapacity = fuelCapacityField.GetValue<float>(),
m_FuelRefill = fuelRefillField.GetValue<float>() };
techBoosterData[__instance] = boosterData;
}
fuelCapacityField.SetValue(fuelCapacityField.GetValue<float>() * fuelCapacityMultiplier.Value);
fuelRefillField.SetValue(fuelRefillField.GetValue<float>() * fuelRefillMultiplier.Value);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(TechBooster), "RemoveFuelTank")]
static void PostfixDestroy(TechBooster __instance, ref ModuleFuelTank tank) {
var trav = Traverse.Create(__instance);
var fuelCapacityField = trav.Field("m_FuelCapacity");
var fuelRefillField = trav.Field("m_FuelRefill");
TechBoosterData boosterData;
bool ok = techBoosterData.TryGetValue(__instance, out boosterData);
if (ok) {
boosterData.m_FuelCapacity -= tank.Capacity;
boosterData.m_FuelRefill -= tank.RefillRate;
} else {
boosterData = new TechBoosterData { m_FuelCapacity = fuelCapacityField.GetValue<float>(),
m_FuelRefill = fuelRefillField.GetValue<float>() };
techBoosterData[__instance] = boosterData;
}
fuelCapacityField.SetValue(fuelCapacityField.GetValue<float>() * fuelCapacityMultiplier.Value);
fuelRefillField.SetValue(fuelRefillField.GetValue<float>() * fuelRefillMultiplier.Value);
}
}
}

View File

@@ -57,7 +57,7 @@
<Compile Include="ModuleWeaponManager.cs" />
<Compile Include="CykUtil.cs" />
<Compile Include="ModuleHeartManager.cs" />
<Compile Include="TechBoosterManager.cs" />
<Compile Include="ModuleFuelTankManager.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">

View File

@@ -1 +1 @@
clang-format -i TerraTech/*.cs
wgo -dir TerraTech echo "Formatters" :: clang-format -i TerraTech/*.cs