Files
ArachnaeSwarm/Source/ArachnaeSwarm/Building_Comps/WULA_MutiFuelSpawner/CompRefuelableNutrition_WithKey.cs
2025-09-17 12:54:07 +08:00

70 lines
2.8 KiB
C#

using RimWorld;
using Verse;
using System.Reflection;
using HarmonyLib;
namespace ArachnaeSwarm
{
public class CompProperties_RefuelableNutrition_WithKey : CompProperties_RefuelableNutrition
{
public string saveKeysPrefix;
public CompProperties_RefuelableNutrition_WithKey()
{
compClass = typeof(CompRefuelableNutrition_WithKey);
}
}
public class CompRefuelableNutrition_WithKey : CompRefuelableNutrition, IFuelSource
{
public new CompProperties_RefuelableNutrition_WithKey Props => (CompProperties_RefuelableNutrition_WithKey)props;
public override void PostExposeData()
{
string prefix = Props.saveKeysPrefix;
if (prefix.NullOrEmpty())
{
Log.ErrorOnce($"CompRefuelableNutrition_WithKey on {parent.def.defName} has a null or empty saveKeysPrefix. Defaulting to standard save.", GetHashCode());
base.PostExposeData();
return;
}
// --- Accessing private fields from CompRefuelable (base of CompRefuelableNutrition) ---
FieldInfo fuelField = AccessTools.Field(typeof(CompRefuelable), "fuel");
FieldInfo configuredTargetFuelLevelField = AccessTools.Field(typeof(CompRefuelable), "configuredTargetFuelLevel");
FieldInfo allowAutoRefuelField = AccessTools.Field(typeof(CompRefuelable), "allowAutoRefuel");
// Get current values
float currentFuel = (float)fuelField.GetValue(this);
float currentConfiguredLevel = (float)configuredTargetFuelLevelField.GetValue(this);
bool currentAllowAuto = (bool)allowAutoRefuelField.GetValue(this);
// Scribe with prefix
Scribe_Values.Look(ref currentFuel, prefix + "_fuel", 0f);
Scribe_Values.Look(ref currentConfiguredLevel, prefix + "_configuredTargetFuelLevel", -1f);
Scribe_Values.Look(ref currentAllowAuto, prefix + "_allowAutoRefuel", true);
// Set values back if loading
if (Scribe.mode == LoadSaveMode.LoadingVars)
{
fuelField.SetValue(this, currentFuel);
configuredTargetFuelLevelField.SetValue(this, currentConfiguredLevel);
allowAutoRefuelField.SetValue(this, currentAllowAuto);
}
// --- Accessing private fields from CompRefuelableNutrition ---
// (Assuming there are any. If not, this part is not needed)
// Example:
// FieldInfo someOtherField = AccessTools.Field(typeof(CompRefuelableNutrition), "someOtherPrivateField");
// ... and so on
}
public new void Notify_UsedThisTick()
{
if (Props.consumeFuelOnlyWhenUsed)
{
ConsumeFuel(Props.fuelConsumptionRate / 60000f);
}
}
}
}