168 lines
6.5 KiB
C#
168 lines
6.5 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Runtime.CompilerServices;
|
||
|
|
using HarmonyLib;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxLaws
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Hook P: the "Court place" switch on a building (design: "Court place"). A postfix on Building.GetGizmos
|
||
|
|
/// (Verse/Building.cs:401), an iterator that yields base first; the overrides of ThingWithComps and
|
||
|
|
/// Building_Throne call base too (Verse/ThingWithComps.cs:536; RimWorld/Building_Throne.cs:79-89). Gizmos
|
||
|
|
/// are rebuilt every frame for every selected object (Verse/GizmoGridDrawer.cs:62-100), so the postfix
|
||
|
|
/// returns after one static field read while the lawbook is empty, and otherwise wraps the result only
|
||
|
|
/// for a single selected building (Find.Selector.SingleSelectedObject, RimWorld/Selector.cs:74, the gate of
|
||
|
|
/// vanilla's room stats button, Verse/Building.cs:407) of a kind that can hold court. A box-select of 200
|
||
|
|
/// walls leaves the result untouched.
|
||
|
|
/// </summary>
|
||
|
|
[HarmonyPatch(typeof(Building), nameof(Building.GetGizmos))]
|
||
|
|
public static class Patch_Building_GetGizmos
|
||
|
|
{
|
||
|
|
public const string Where = "hook P, Building.GetGizmos";
|
||
|
|
|
||
|
|
public static void Postfix(Building __instance, ref IEnumerable<Gizmo> __result)
|
||
|
|
{
|
||
|
|
if (!Watch.AnyLaw)
|
||
|
|
return;
|
||
|
|
Work(__instance, ref __result);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Off the hot path: a method with a try block is never inlined, so the check above stays small.
|
||
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||
|
|
private static void Work(Building building, ref IEnumerable<Gizmo> result)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (LawsTestHooks.CourtGizmoThrows)
|
||
|
|
throw new InvalidOperationException("test: the court place gizmo hook throws");
|
||
|
|
if (result == null || Find.Selector?.SingleSelectedObject != building || !CourtPlace.CanBe(building))
|
||
|
|
return;
|
||
|
|
result = Append(result, building);
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
HookGuard.Fail(Where, e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// C# allows no yield inside a try with a catch, so the switch is built in Make, which catches its own
|
||
|
|
// throw and gives null: a Laws error never breaks the gizmo bar. Vanilla's own gizmos pass unchanged.
|
||
|
|
private static IEnumerable<Gizmo> Append(IEnumerable<Gizmo> original, Building building)
|
||
|
|
{
|
||
|
|
foreach (Gizmo g in original)
|
||
|
|
yield return g;
|
||
|
|
Gizmo own = Make(building);
|
||
|
|
if (own != null)
|
||
|
|
yield return own;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static Gizmo Make(Building building)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return new Command_CourtPlace(building);
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
HookGuard.Fail(Where + ", the switch", e);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Hook P's second half: a postfix on Building.SpawnSetup (Verse/Building.cs:113), which every building
|
||
|
|
/// runs when it spawns, a reinstall from its crate included (RimWorld/Blueprint_Install.cs:99-112 hands
|
||
|
|
/// the same instance to GenSpawn). When the building is a stored court place, the older place spawned on
|
||
|
|
/// the same map is dropped at once (GameComponent_BlaxLaws.CourtPlaceSpawned), so a crate that arrives on
|
||
|
|
/// a map with a place resolves "the place set last wins" now and not at the next save. One field read
|
||
|
|
/// and one count while no place is stored; spawns after a load are skipped, FinalizeInit cleans those.
|
||
|
|
/// </summary>
|
||
|
|
[HarmonyPatch(typeof(Building), nameof(Building.SpawnSetup))]
|
||
|
|
public static class Patch_Building_SpawnSetup
|
||
|
|
{
|
||
|
|
public const string Where = "hook P, Building.SpawnSetup";
|
||
|
|
|
||
|
|
public static void Postfix(Building __instance, bool respawningAfterLoad)
|
||
|
|
{
|
||
|
|
if (respawningAfterLoad)
|
||
|
|
return;
|
||
|
|
Work(__instance);
|
||
|
|
}
|
||
|
|
|
||
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
||
|
|
private static void Work(Building building)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
||
|
|
if (comp == null || !comp.AnyCourtPlace)
|
||
|
|
return;
|
||
|
|
comp.CourtPlaceSpawned(building);
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
HookGuard.Fail(Where, e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// The "Court place" switch. A Command_Toggle (Verse/Command_Toggle.cs:7-60) has no hover hook, so this
|
||
|
|
/// subclass overrides Gizmo.GizmoUpdateOnMouseover (Verse/Gizmo.cs:49) to outline the court room, as
|
||
|
|
/// Gizmo_RoomStats does (Verse/Gizmo_RoomStats.cs:83-87). Built per frame and never saved, so the
|
||
|
|
/// tooltip with its room text is built only when read, on hover (Verse/Command.cs:71, 203-205).
|
||
|
|
/// </summary>
|
||
|
|
public class Command_CourtPlace : Command_Toggle
|
||
|
|
{
|
||
|
|
private readonly Building building;
|
||
|
|
|
||
|
|
public Command_CourtPlace(Building building)
|
||
|
|
{
|
||
|
|
if (LawsTestHooks.CourtSwitchThrows)
|
||
|
|
throw new InvalidOperationException("test: the court place switch throws");
|
||
|
|
this.building = building;
|
||
|
|
defaultLabel = "BlaxLaws_CourtPlace".Translate();
|
||
|
|
icon = TexButton.ShowRoomStats;
|
||
|
|
isActive = () => CourtPlace.IsPlace(building);
|
||
|
|
toggleAction = () => HookGuard.Run("Court place switch", () => CourtPlaceUi.Toggle(building));
|
||
|
|
}
|
||
|
|
|
||
|
|
public Building Building => building;
|
||
|
|
|
||
|
|
public override string Desc
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
return "BlaxLaws_CourtPlaceDesc".Translate(CourtPlaceUi.RoomText(building));
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
HookGuard.Fail("Court place tooltip", e);
|
||
|
|
return "BlaxLaws_CourtPlace".Translate();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void GizmoUpdateOnMouseover()
|
||
|
|
{
|
||
|
|
base.GizmoUpdateOnMouseover();
|
||
|
|
try
|
||
|
|
{
|
||
|
|
// Null for an outdoor or huge room, so the outline never walks thousands of cells a frame.
|
||
|
|
Room room = CourtPlace.RoomOf(building);
|
||
|
|
if (CourtPlace.IsProperRoom(room))
|
||
|
|
room.DrawFieldEdges();
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
HookGuard.Fail("Court place outline", e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|