using System; using HarmonyLib; using RimWorld; using Verse; namespace BlaxDefaults { /// /// New storage buildings with a storage blueprint start from their kind's default. Work package "hooks" /// (design.md: "New storage buildings start from your default"). The postfix runs after vanilla copied the /// def default in Blueprint_Storage.PostMake; the blueprint hands its settings to the frame, and /// Frame.CompleteConstruction copies them onto the building. Auto-rebuild of a Building_Storage copies the /// old settings onto the new blueprint after PostMake, so it keeps them; a rebuilt bookcase starts from the /// default. Changes made on the blueprint before it is built still win. PostMake runs inside /// ThingMaker.MakeThing, before the blueprint has a faction or a map; loading a save never calls it. /// public static class StorageBuildingHooks { /// When StorageOn and the blueprint's BuildDef is covered and has a saved template: apply it onto blueprint.settings. public static void OnBlueprintMade(Blueprint_Storage blueprint) { if (!BlaxDefaultsMod.StorageOn || blueprint?.settings == null) return; ThingDef def = blueprint.BuildDef; if (def == null || !StorageKinds.IsCovered(def)) return; StorageTemplate template = DefaultsLibrary.Get(DefaultsKeys.ForStorage(def)); if (template == null) return; template.ApplyTo(blueprint.settings, StorageKinds.ParentFilterFor(def)); } } [HarmonyPatch(typeof(Blueprint_Storage), nameof(Blueprint_Storage.PostMake))] public static class Patch_Blueprint_Storage_PostMake { public static void Postfix(Blueprint_Storage __instance) { try { StorageBuildingHooks.OnBlueprintMade(__instance); } catch (Exception e) { HookGuard.Report("Blueprint_Storage.PostMake", e); } } } }