going to stop reading remaining soil uses
This commit is contained in:
@@ -4,11 +4,13 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using HarmonyLib;
|
||||
using MelonLoader;
|
||||
using Il2CppInterop.Runtime;
|
||||
using Il2CppScheduleOne.Growing;
|
||||
using Il2CppScheduleOne.ItemFramework;
|
||||
using Il2CppScheduleOne.ObjectScripts;
|
||||
using UnityEngine;
|
||||
|
||||
[assembly: MelonInfo(typeof(AbsorbentSoil.AbsorbentSoilMod), "Absorbent Soil", "0.1.1", "AttilaG")]
|
||||
[assembly: MelonInfo(typeof(AbsorbentSoil.AbsorbentSoilMod), "Absorbent Soil", "0.1.0", "AttilaG")]
|
||||
[assembly: MelonGame(null, "Schedule I")]
|
||||
|
||||
namespace AbsorbentSoil
|
||||
@@ -22,15 +24,16 @@ namespace AbsorbentSoil
|
||||
MelonLogger.Msg("Absorbent Soil loaded. Additive retention is active for long-life soils.");
|
||||
}
|
||||
|
||||
// This keeps slot/session bleed low if the player backs out and loads another save without restarting.
|
||||
// It does NOT persist anything to disk. It only tracks pots currently seen in the running game session.
|
||||
public override void OnSceneWasLoaded(int buildIndex, string sceneName)
|
||||
{
|
||||
// Prevent save-slot/session bleed if the player returns to menu and loads another save.
|
||||
if (!string.IsNullOrWhiteSpace(sceneName) &&
|
||||
(sceneName.IndexOf("menu", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
sceneName.IndexOf("main", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
sceneName.IndexOf("load", StringComparison.OrdinalIgnoreCase) >= 0))
|
||||
{
|
||||
AdditiveMemory.ClearAll();
|
||||
AdditiveMemory.Clear();
|
||||
MelonLogger.Msg($"Cleared additive memory on scene load: {sceneName}");
|
||||
}
|
||||
}
|
||||
@@ -73,27 +76,28 @@ namespace AbsorbentSoil
|
||||
: Array.Empty<string>();
|
||||
}
|
||||
|
||||
public static bool HasStored(Pot pot)
|
||||
{
|
||||
if (pot == null)
|
||||
return false;
|
||||
|
||||
string key = PotKeyHelper.GetPotKey(pot);
|
||||
return !string.IsNullOrWhiteSpace(key) &&
|
||||
AdditivesByPotKey.TryGetValue(key, out HashSet<string> set) &&
|
||||
set.Count > 0;
|
||||
}
|
||||
|
||||
public static void Forget(Pot pot)
|
||||
{
|
||||
if (pot == null)
|
||||
return;
|
||||
|
||||
Forget(PotKeyHelper.GetPotKey(pot));
|
||||
string key = PotKeyHelper.GetPotKey(pot);
|
||||
if (!string.IsNullOrWhiteSpace(key))
|
||||
AdditivesByPotKey.Remove(key);
|
||||
}
|
||||
|
||||
public static void Forget(string potKey)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(potKey))
|
||||
return;
|
||||
|
||||
if (AdditivesByPotKey.Remove(potKey) && AbsorbentSoilMod.VerboseLogging)
|
||||
MelonLogger.Msg($"Cleared retained additives for pot '{potKey}'.");
|
||||
}
|
||||
|
||||
public static void ClearAll()
|
||||
{
|
||||
AdditivesByPotKey.Clear();
|
||||
}
|
||||
public static void Clear() => AdditivesByPotKey.Clear();
|
||||
}
|
||||
|
||||
internal static class PotKeyHelper
|
||||
@@ -103,10 +107,13 @@ namespace AbsorbentSoil
|
||||
if (pot == null)
|
||||
return string.Empty;
|
||||
|
||||
// Most Schedule I buildable/grid items have a GUID inherited from a base class.
|
||||
// Use reflection so this survives minor EA API shifts.
|
||||
object guid = ReadMember(pot, "GUID") ?? ReadMember(pot, "Guid") ?? ReadMember(pot, "guid");
|
||||
if (guid != null && !string.IsNullOrWhiteSpace(guid.ToString()))
|
||||
return guid.ToString();
|
||||
|
||||
// Fallback for testing only. Not stable across reloads, but better than hard failing.
|
||||
return $"scene:{pot.GetInstanceID()}";
|
||||
}
|
||||
|
||||
@@ -116,21 +123,17 @@ namespace AbsorbentSoil
|
||||
return null;
|
||||
|
||||
Type type = instance.GetType();
|
||||
while (type != null)
|
||||
|
||||
PropertyInfo prop = AccessTools.Property(type, name);
|
||||
if (prop != null)
|
||||
{
|
||||
PropertyInfo prop = AccessTools.Property(type, name);
|
||||
if (prop != null)
|
||||
{
|
||||
try { return prop.GetValue(instance); } catch { }
|
||||
}
|
||||
try { return prop.GetValue(instance); } catch { }
|
||||
}
|
||||
|
||||
FieldInfo field = AccessTools.Field(type, name);
|
||||
if (field != null)
|
||||
{
|
||||
try { return field.GetValue(instance); } catch { }
|
||||
}
|
||||
|
||||
type = type.BaseType;
|
||||
FieldInfo field = AccessTools.Field(type, name);
|
||||
if (field != null)
|
||||
{
|
||||
try { return field.GetValue(instance); } catch { }
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -142,28 +145,30 @@ namespace AbsorbentSoil
|
||||
private static readonly FieldInfo RemainingSoilUsesField =
|
||||
AccessTools.Field(typeof(GrowContainer), "_remainingSoilUses");
|
||||
|
||||
public static bool IsRetainingSoil(Pot pot)
|
||||
{
|
||||
if (pot == null || string.IsNullOrWhiteSpace(pot.SoilID))
|
||||
return false;
|
||||
|
||||
string soilId = pot.SoilID.ToLowerInvariant();
|
||||
|
||||
return soilId.Contains("longlifesoil") ||
|
||||
soilId.Contains("extralonglifesoil");
|
||||
}
|
||||
|
||||
public static int GetRemainingSoilUses(Pot pot)
|
||||
{
|
||||
if (pot == null || RemainingSoilUsesField == null)
|
||||
return 0;
|
||||
|
||||
return (int)RemainingSoilUsesField.GetValue(pot);
|
||||
try
|
||||
{
|
||||
return (int)RemainingSoilUsesField.GetValue(pot);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CanRetainAdditives(Pot pot)
|
||||
public static bool CanCaptureNewAdditives(Pot pot)
|
||||
{
|
||||
return IsRetainingSoil(pot) && GetRemainingSoilUses(pot) > 0;
|
||||
// Normal soil should have 1 use, long-life should have more than 1.
|
||||
return GetRemainingSoilUses(pot) > 1;
|
||||
}
|
||||
|
||||
public static bool CanReapplyRetainedAdditives(Pot pot)
|
||||
{
|
||||
return GetRemainingSoilUses(pot) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +176,6 @@ namespace AbsorbentSoil
|
||||
internal static class Pot_ApplyAdditive_Patch
|
||||
{
|
||||
private static bool _suppressCapture;
|
||||
private static readonly MethodInfo ApplyAdditiveMethod = AccessTools.Method(typeof(Pot), "ApplyAdditive");
|
||||
|
||||
public static void Postfix(Pot __instance, AdditiveDefinition __result, string additiveID, bool isInitialApplication)
|
||||
{
|
||||
@@ -183,7 +187,7 @@ namespace AbsorbentSoil
|
||||
if (__instance == null || __result == null || string.IsNullOrWhiteSpace(additiveID))
|
||||
return;
|
||||
|
||||
if (!SoilHelper.IsRetainingSoil(__instance))
|
||||
if (!SoilHelper.CanCaptureNewAdditives(__instance))
|
||||
return;
|
||||
|
||||
AdditiveMemory.Remember(__instance, additiveID);
|
||||
@@ -199,7 +203,8 @@ namespace AbsorbentSoil
|
||||
if (pot == null || string.IsNullOrWhiteSpace(additiveID))
|
||||
return;
|
||||
|
||||
if (ApplyAdditiveMethod == null)
|
||||
MethodInfo applyAdditive = AccessTools.Method(typeof(Pot), "ApplyAdditive");
|
||||
if (applyAdditive == null)
|
||||
{
|
||||
MelonLogger.Warning("Could not find Pot.ApplyAdditive via reflection.");
|
||||
return;
|
||||
@@ -208,7 +213,7 @@ namespace AbsorbentSoil
|
||||
try
|
||||
{
|
||||
_suppressCapture = true;
|
||||
ApplyAdditiveMethod.Invoke(pot, new object[] { additiveID, true });
|
||||
applyAdditive.Invoke(pot, new object[] { additiveID, true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -232,12 +237,8 @@ namespace AbsorbentSoil
|
||||
return;
|
||||
|
||||
Pot pot = __instance.Pot;
|
||||
|
||||
if (!SoilHelper.CanRetainAdditives(pot))
|
||||
{
|
||||
AdditiveMemory.Forget(pot);
|
||||
if (!SoilHelper.CanReapplyRetainedAdditives(pot))
|
||||
return;
|
||||
}
|
||||
|
||||
IReadOnlyList<string> additiveIds = AdditiveMemory.Get(pot);
|
||||
if (additiveIds.Count == 0)
|
||||
@@ -258,15 +259,33 @@ namespace AbsorbentSoil
|
||||
[HarmonyPatch(typeof(Pot), "OnPlantFullyHarvested")]
|
||||
internal static class Pot_OnPlantFullyHarvested_Patch
|
||||
{
|
||||
private static void Postfix(Pot __instance)
|
||||
public static void Postfix(Pot __instance)
|
||||
{
|
||||
if (__instance == null)
|
||||
return;
|
||||
// Intentionally do nothing. This patch exists as a reminder that retained additives should survive harvest.
|
||||
// Do NOT clear AdditiveMemory here, because long-life/extra-long-life soil should carry additives forward.
|
||||
}
|
||||
}
|
||||
|
||||
if (!SoilHelper.CanRetainAdditives(__instance))
|
||||
[HarmonyPatch(typeof(GrowContainer), "SetRemainingSoilUses")]
|
||||
internal static class GrowContainer_SetRemainingSoilUses_Patch
|
||||
{
|
||||
private static void Postfix(GrowContainer __instance, int uses)
|
||||
{
|
||||
try
|
||||
{
|
||||
AdditiveMemory.Forget(__instance);
|
||||
MelonLogger.Msg("[Absorbent Soil] Cleared retained additives because soil has no retaining uses left.");
|
||||
if (__instance == null || uses > 0)
|
||||
return;
|
||||
|
||||
Pot pot = __instance.TryCast<Pot>();
|
||||
if (pot == null)
|
||||
return;
|
||||
|
||||
AdditiveMemory.Forget(pot);
|
||||
MelonLogger.Msg("[Absorbent Soil] Cleared retained additives because soil uses reached zero.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MelonLogger.Warning($"SetRemainingSoilUses postfix failed: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -276,6 +295,7 @@ namespace AbsorbentSoil
|
||||
{
|
||||
public static void Prefix(Pot __instance)
|
||||
{
|
||||
// If the pot itself is destroyed/sold, stop tracking its additives.
|
||||
try { AdditiveMemory.Forget(__instance); }
|
||||
catch { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user