diff --git a/AbsorbentSoil/Patches/PlantAdditiveAppliedPatch.cs b/AbsorbentSoil/Patches/PlantAdditiveAppliedPatch.cs index 4a7957d..c9440d5 100644 --- a/AbsorbentSoil/Patches/PlantAdditiveAppliedPatch.cs +++ b/AbsorbentSoil/Patches/PlantAdditiveAppliedPatch.cs @@ -2,32 +2,42 @@ using System; using HarmonyLib; using Il2CppScheduleOne.Growing; using Il2CppScheduleOne.ItemFramework; +using Il2CppScheduleOne.ObjectScripts; using MelonLoader; namespace AbsorbentSoil { [HarmonyPatch(typeof(Plant), nameof(Plant.AdditiveApplied))] - internal static class Plant_AdditiveApplied_Patch + public static class PlantAdditiveAppliedPatch { public static void Postfix(Plant __instance, AdditiveDefinition additive, bool isInitialApplication) { - try + if (__instance == null || additive == null) + return; + + // Important cleanup: + // Initial applications are caused by load/init/reapply behavior. + // Do NOT treat them as a fresh additive pour. + if (isInitialApplication) { - MelonLogger.Msg($"Plant.AdditiveApplied fired. Plant={__instance?.name}, Pot={__instance?.Pot?.name}, Additive={additive?.ID}, Initial={isInitialApplication}"); + if (AbsorbentSoilMod.VerboseLogging) + MelonLogger.Msg($"[Absorbent Soil] Ignored initial additive fire: {additive.ID}"); - if (__instance == null || __instance.Pot == null || additive == null) - return; - - string additiveId = additive.ID; - if (string.IsNullOrWhiteSpace(additiveId)) - return; - - AdditiveMemory.Remember(__instance.Pot, additiveId); - } - catch (Exception ex) - { - MelonLogger.Warning($"Plant.AdditiveApplied postfix failed: {ex}"); + return; } + + Pot pot = __instance.Pot; + if (pot == null) + return; + + string potKey = PotKeyHelper.GetPotKey(pot); + if (string.IsNullOrWhiteSpace(potKey)) + return; + + AdditiveMemory.Remember(pot, additive.ID); + + if (AbsorbentSoilMod.VerboseLogging) + MelonLogger.Msg($"[Absorbent Soil] Remembered player additive '{additive.ID}' for pot '{potKey}'"); } } } diff --git a/AbsorbentSoil/Patches/PlantInitializePatch.cs b/AbsorbentSoil/Patches/PlantInitializePatch.cs index 39b1f56..379dc94 100644 --- a/AbsorbentSoil/Patches/PlantInitializePatch.cs +++ b/AbsorbentSoil/Patches/PlantInitializePatch.cs @@ -3,34 +3,46 @@ using HarmonyLib; using Il2CppScheduleOne.Growing; using Il2CppScheduleOne.ObjectScripts; using MelonLoader; +using System.Collections.Generic; namespace AbsorbentSoil { [HarmonyPatch(typeof(Plant), nameof(Plant.Initialize))] - internal static class Plant_Initialize_Patch + public static class PlantInitializePatch { + private static readonly HashSet ReappliedPlants = new HashSet(); + public static void Postfix(Plant __instance) { - try + if (__instance == null) + return; + + int plantId = __instance.GetInstanceID(); + + if (ReappliedPlants.Contains(plantId)) + return; + + ReappliedPlants.Add(plantId); + + Pot pot = __instance.Pot; + if (pot == null) + return; + + string potKey = PotKeyHelper.GetPotKey(pot); + if (string.IsNullOrWhiteSpace(potKey)) + return; + + var additives = AdditiveMemory.Get(pot); + + if (additives == null || additives.Count == 0) + return; + + if (AbsorbentSoilMod.VerboseLogging) + MelonLogger.Msg($"[Absorbent Soil] Reapplying {additives.Count} remembered additive(s) to plant in pot '{potKey}'"); + + foreach (string additiveId in additives) { - PersistenceStore.EnsureLoaded(); - - MelonLogger.Msg($"Plant.Initialize fired. Plant={__instance?.name}, Pot={__instance?.Pot?.name}"); - - if (__instance == null || __instance.Pot == null) - return; - - Pot actualPot = __instance.Pot; - var additiveIds = AdditiveMemory.Get(actualPot); - - MelonLogger.Msg($"Plant.Initialize pot key={PotKeyHelper.GetPotKey(actualPot)}, remembered additives={additiveIds.Count}"); - - foreach (string additiveId in additiveIds) - Pot_ApplyAdditive_Patch.ReapplyWithoutRecapture(actualPot, additiveId); - } - catch (Exception ex) - { - MelonLogger.Warning($"Plant.Initialize postfix failed: {ex}"); + pot.ApplyAdditive(additiveId, true); } } } diff --git a/AbsorbentSoil/bin/Release/net6.0/AbsorbentSoil.pdb b/AbsorbentSoil/bin/Release/net6.0/AbsorbentSoil.pdb index b55dff9..909799c 100644 Binary files a/AbsorbentSoil/bin/Release/net6.0/AbsorbentSoil.pdb and b/AbsorbentSoil/bin/Release/net6.0/AbsorbentSoil.pdb differ diff --git a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfo.cs b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfo.cs index f285866..175fbe1 100644 --- a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfo.cs +++ b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("AbsorbentSoil")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Release")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+162a1ce7951370e4cbd8c674c284b30f8a85cbd4")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b21db135dbb267f14ba001c06d5e767684e16748")] [assembly: System.Reflection.AssemblyProductAttribute("AbsorbentSoil")] [assembly: System.Reflection.AssemblyTitleAttribute("AbsorbentSoil")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfoInputs.cache b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfoInputs.cache index 73a7919..f77f843 100644 --- a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfoInputs.cache +++ b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.AssemblyInfoInputs.cache @@ -1 +1 @@ -67d62e0901b35175e30da93a43a11d964fe004e1a9681a2e3903abe5b9ef6961 +b9ad6f2e98c8850d94d764bf380f030dbfc6c348e959950e4fa42b574c0504d4 diff --git a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.dll b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.dll index f598550..ffc449b 100644 Binary files a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.dll and b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.dll differ diff --git a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.pdb b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.pdb index b55dff9..909799c 100644 Binary files a/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.pdb and b/AbsorbentSoil/obj/Release/net6.0/AbsorbentSoil.pdb differ diff --git a/AbsorbentSoil/obj/Release/net6.0/ref/AbsorbentSoil.dll b/AbsorbentSoil/obj/Release/net6.0/ref/AbsorbentSoil.dll index d279ece..8d7cb45 100644 Binary files a/AbsorbentSoil/obj/Release/net6.0/ref/AbsorbentSoil.dll and b/AbsorbentSoil/obj/Release/net6.0/ref/AbsorbentSoil.dll differ diff --git a/AbsorbentSoil/obj/Release/net6.0/refint/AbsorbentSoil.dll b/AbsorbentSoil/obj/Release/net6.0/refint/AbsorbentSoil.dll index d279ece..8d7cb45 100644 Binary files a/AbsorbentSoil/obj/Release/net6.0/refint/AbsorbentSoil.dll and b/AbsorbentSoil/obj/Release/net6.0/refint/AbsorbentSoil.dll differ