diff --git a/DebugNotIncluded/DebugNotIncludedPatches.cs b/DebugNotIncluded/DebugNotIncludedPatches.cs index f20e31ea..e4f64bc8 100644 --- a/DebugNotIncluded/DebugNotIncludedPatches.cs +++ b/DebugNotIncluded/DebugNotIncludedPatches.cs @@ -26,7 +26,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.Reflection; using System.Reflection.Emit; using UnityEngine; diff --git a/NoWasteWant/FreshnessControl.cs b/NoWasteWant/FreshnessControl.cs index 583b3382..fdf51bc7 100644 --- a/NoWasteWant/FreshnessControl.cs +++ b/NoWasteWant/FreshnessControl.cs @@ -27,9 +27,7 @@ namespace PeterHan.NoWasteWant { [SerializationConfig(MemberSerialization.OptIn)] public class FreshnessControl : KMonoBehaviour, ISim4000ms, ISingleSliderControl { public float MinFreshness { - get { - return minFreshness; - } + get => minFreshness; set { minFreshness = value; DropStaleItems(); @@ -61,15 +59,12 @@ public FreshnessControl() { public void DropStaleItems() { if (storage != null && minFreshness > 0.0f) { var toDrop = ListPool.Allocate(); - try { - foreach (var item in storage.items) - if (item != null && !IsAcceptable(item)) - toDrop.Add(item); - foreach (var item in toDrop) - storage.Drop(item, false); - } finally { - toDrop.Recycle(); - } + foreach (var item in storage.items) + if (item != null && !IsAcceptable(item)) + toDrop.Add(item); + foreach (var item in toDrop) + storage.Drop(item, false); + toDrop.Recycle(); } } diff --git a/NoWasteWant/NoWasteWant.csproj b/NoWasteWant/NoWasteWant.csproj index 51217c58..113c13d4 100644 --- a/NoWasteWant/NoWasteWant.csproj +++ b/NoWasteWant/NoWasteWant.csproj @@ -2,7 +2,7 @@ Waste Not, Want Not - 1.3.0.0 + 1.4.0.0 PeterHan.NoWasteWant Encourages Duplicants to eat older food and adds other tools for managing food storage. 1.1.0.0 diff --git a/NoWasteWant/NoWasteWantPatches.cs b/NoWasteWant/NoWasteWantPatches.cs index 57d7b628..d311e34a 100644 --- a/NoWasteWant/NoWasteWantPatches.cs +++ b/NoWasteWant/NoWasteWantPatches.cs @@ -34,7 +34,9 @@ namespace PeterHan.NoWasteWant { /// Patches which will be applied via annotations for Waste Not, Want Not. /// public sealed class NoWasteWantPatches : KMod.UserMod2 { - private static TagBits EDIBLE_BITS; + private static readonly Tag[] EDIBLE_BITS = { + GameTags.CookingIngredient, GameTags.Edible + }; /// /// The maximum mass of food in kilograms that will rot. @@ -63,14 +65,7 @@ internal static IEnumerable FixEfficientSupply( public static void AddFreshnessControl(GameObject go) { go.AddOrGet(); } - - [PLibMethod(RunAt.AfterDbInit)] - internal static void AfterDbInit() { - // For compatibility with Not Enough Tags - EDIBLE_BITS.SetTag(GameTags.CookingIngredient); - EDIBLE_BITS.SetTag(GameTags.Edible); - } - + public override void OnLoad(Harmony harmony) { base.OnLoad(harmony); PUtil.InitLibrary(); @@ -133,16 +128,22 @@ private static IEnumerable TranspileNegateLast( /// Applied to FetchManager to ban fetching stale items to refrigerators. /// [HarmonyPatch] - public static class FetchManager_IsFetchablePickup_Patch { + public static class FetchManager_IsFetchablePickupExclude_Patch { // Why can we not use byref types in attributes... - internal static MethodBase TargetMethod() { + internal static IEnumerable TargetMethods() { var refTagBits = typeof(TagBits).MakeByRefType(); - var newMethod = typeof(FetchManager).GetMethodSafe("IsFetchablePickup_Exclude", + var excMethod = typeof(FetchManager).GetMethodSafe("IsFetchablePickup_Exclude", true, typeof(KPrefabID), typeof(Storage), typeof(float), typeof(HashSet), typeof(Tag), typeof(Storage)); - return newMethod ?? typeof(FetchManager).GetMethodSafe(nameof(FetchManager. + if (excMethod != null) + yield return excMethod; + // TODO Remove when versions prior to U44-535211 no longer need to be supported + // and convert the other one to an attribute patch + var oldMethod = typeof(FetchManager).GetMethodSafe(nameof(FetchManager. IsFetchablePickup), true, typeof(KPrefabID), typeof(Storage), typeof(float), refTagBits, refTagBits, refTagBits, typeof(Storage)); + if (oldMethod != null) + yield return oldMethod; } /// @@ -151,11 +152,41 @@ internal static MethodBase TargetMethod() { internal static void Postfix(KPrefabID pickup_id, Storage destination, ref bool __result) { if (__result && pickup_id != null && destination != null && pickup_id. - HasAnyTags(ref EDIBLE_BITS)) { - var freshness = destination.gameObject.GetComponentSafe(); - if (freshness != null) - __result = freshness.IsAcceptable(pickup_id.gameObject); - } + HasAnyTags(EDIBLE_BITS) && destination.TryGetComponent( + out FreshnessControl freshness)) + __result = freshness.IsAcceptable(pickup_id.gameObject); + } + } + + /// + /// Applied to FetchManager to ban fetching stale items to refrigerators. + /// + [HarmonyPatch] + public static class FetchManager_IsFetchablePickup_Patch { + /// + /// The target method to patch. + /// + private static readonly MethodBase TARGET = typeof(FetchManager).GetMethodSafe( + nameof(FetchManager.IsFetchablePickup), true, typeof(Pickupable), + typeof(FetchChore), typeof(Storage)); + + internal static bool Prepare() { + return TARGET != null; + } + + internal static MethodBase TargetMethod() { + return TARGET; + } + + /// + /// Applied after IsFetchablePickup runs. + /// + internal static void Postfix(Pickupable pickup, Storage destination, + ref bool __result) { + if (__result && pickup != null && destination != null && pickup. + KPrefabID.HasAnyTags(EDIBLE_BITS) && destination.TryGetComponent( + out FreshnessControl freshness)) + __result = freshness.IsAcceptable(pickup.gameObject); } } diff --git a/SandboxTools/SandboxTools.csproj b/SandboxTools/SandboxTools.csproj index 6625e9d3..ad2d14b6 100644 --- a/SandboxTools/SandboxTools.csproj +++ b/SandboxTools/SandboxTools.csproj @@ -2,7 +2,7 @@ Sandbox Tools - 3.6.0.0 + 3.7.0.0 PeterHan.SandboxTools Improves the Sandbox Mode tools with numerous small tweaks. 3.1.0.0 diff --git a/SandboxTools/SandboxToolsPatches.cs b/SandboxTools/SandboxToolsPatches.cs index 758a7e9d..059eacc0 100644 --- a/SandboxTools/SandboxToolsPatches.cs +++ b/SandboxTools/SandboxToolsPatches.cs @@ -139,9 +139,12 @@ internal static void Prefix(BuildingDef __instance, IList selected_elements settings.InstantBuild))) { if (__instance.PrefabID == MassiveHeatSinkConfig.ID) { // Special case the AETN to iron (it uses niobium otherwise) - selected_elements.Clear(); - selected_elements.Add(ElementLoader.FindElementByHash(SimHashes.Iron). - tag); + var iron = ElementLoader.FindElementByHash(SimHashes.Iron).tag; + if (selected_elements.Count == 1 && selected_elements[0] != iron && + !selected_elements.IsReadOnly) { + selected_elements.Clear(); + selected_elements.Add(iron); + } } else if (selected_elements.Count > 0) { // Lower temperature to at least the element's melt point - 1 K var pe = ElementLoader.GetElement(selected_elements[0]);