rimworld-blax-defaults/Source/BlaxDefaults/StorageBuildingHooks.cs

50 lines
2.1 KiB
C#
Raw Normal View History

using System;
using HarmonyLib;
using RimWorld;
using Verse;
namespace BlaxDefaults
{
/// <summary>
/// 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.
/// </summary>
public static class StorageBuildingHooks
{
/// <summary>When StorageOn and the blueprint's BuildDef is covered and has a saved template: apply it onto blueprint.settings.</summary>
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<StorageTemplate>(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);
}
}
}
}