277 lines
12 KiB
C#
277 lines
12 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Reflection;
|
||
|
|
using System.Reflection.Emit;
|
||
|
|
using HarmonyLib;
|
||
|
|
using RimWorld;
|
||
|
|
using UnityEngine;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxDefaults
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Edit window for one storage default. Work package "page" (design.md: "Defaults library and settings
|
||
|
|
/// page", how). Draws ThingFilterUI.DoThingFilterConfigWindow on a scratch StorageSettings built from the
|
||
|
|
/// game's default with the template on top (kind.LoadDefault), with the kind's parent filter; a priority
|
||
|
|
/// dropdown at the top; under the filter a dropdown for the unlisted rule (UnlistedRule.Label). Save
|
||
|
|
/// captures the scratch (kind.Capture), keeps the window's unlisted rule and any names of the original
|
||
|
|
/// template that don't resolve now (so nothing from an inactive mod is lost), and stores it with
|
||
|
|
/// DefaultsLibrary.Set. Needs defs only, not a running game: the places where the vanilla filter reads
|
||
|
|
/// the game are bridged by Patch_Listing_TreeThingFilter_MainMenu below.
|
||
|
|
/// </summary>
|
||
|
|
public class Dialog_EditStorageTemplate : Window
|
||
|
|
{
|
||
|
|
public readonly StorageKind Kind;
|
||
|
|
/// <summary>The settings the window edits: the kind's game default with the template on top, built in the constructor.</summary>
|
||
|
|
public StorageSettings Scratch;
|
||
|
|
public UnlistedRule Unlisted;
|
||
|
|
/// <summary>A copy of the template the window was opened with, or null for a new default.</summary>
|
||
|
|
public readonly StorageTemplate Original;
|
||
|
|
|
||
|
|
private readonly ThingFilterUI.UIState filterState = new ThingFilterUI.UIState();
|
||
|
|
|
||
|
|
private const float TitleHeight = 36f;
|
||
|
|
private const float RowHeight = 30f;
|
||
|
|
private const float Gap = 8f;
|
||
|
|
|
||
|
|
public Dialog_EditStorageTemplate(StorageKind kind, StorageTemplate template)
|
||
|
|
{
|
||
|
|
Kind = kind ?? throw new ArgumentNullException(nameof(kind));
|
||
|
|
Original = template?.CloneTemplate();
|
||
|
|
Scratch = new StorageSettings();
|
||
|
|
kind.LoadDefault(Scratch, template);
|
||
|
|
Unlisted = template?.unlisted ?? UnlistedRule.Game;
|
||
|
|
doCloseX = true;
|
||
|
|
forcePause = true;
|
||
|
|
absorbInputAroundWindow = true;
|
||
|
|
// Enter in the filter's search box must not close the window without saving.
|
||
|
|
closeOnAccept = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override Vector2 InitialSize => new Vector2(420f, 640f);
|
||
|
|
|
||
|
|
public override void DoWindowContents(Rect inRect)
|
||
|
|
{
|
||
|
|
Text.Font = GameFont.Medium;
|
||
|
|
Widgets.Label(new Rect(0f, 0f, inRect.width - 30f, TitleHeight), "BlaxDefaults_PageEditTitle".Translate(Kind.LabelPlural));
|
||
|
|
Text.Font = GameFont.Small;
|
||
|
|
|
||
|
|
float y = TitleHeight + 4f;
|
||
|
|
var priorityRect = new Rect(0f, y, Mathf.Min(220f, inRect.width), RowHeight);
|
||
|
|
if (Widgets.ButtonText(priorityRect, "BlaxDefaults_PagePriority".Translate(Scratch.Priority.Label().CapitalizeFirst())))
|
||
|
|
Find.WindowStack.Add(new FloatMenu(PriorityOptions()));
|
||
|
|
y += RowHeight + Gap;
|
||
|
|
|
||
|
|
float bottomHeight = RowHeight + Gap + RowHeight;
|
||
|
|
var filterRect = new Rect(0f, y, inRect.width, inRect.height - y - Gap - bottomHeight);
|
||
|
|
if (Current.Game == null && !MainMenuFilterGuard.Ready)
|
||
|
|
{
|
||
|
|
Widgets.DrawMenuSection(filterRect);
|
||
|
|
using (new TextBlock(Color.gray))
|
||
|
|
Widgets.Label(filterRect.ContractedBy(10f), "BlaxDefaults_PageFilterNeedsGame".Translate());
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
ThingFilterUI.DoThingFilterConfigWindow(filterRect, filterState, Scratch.filter, Kind.ParentFilter, TreeOpenMasks.Storage,
|
||
|
|
null, HiddenSpecialFilters(), map: Find.CurrentMap);
|
||
|
|
}
|
||
|
|
y = filterRect.yMax + Gap;
|
||
|
|
|
||
|
|
var unlistedRect = new Rect(0f, y, inRect.width, RowHeight);
|
||
|
|
if (Widgets.ButtonText(unlistedRect, "BlaxDefaults_PageUnlisted".Translate(Unlisted.Label())))
|
||
|
|
Find.WindowStack.Add(new FloatMenu(UnlistedOptions()));
|
||
|
|
TooltipHandler.TipRegion(unlistedRect, "BlaxDefaults_PageUnlistedTip".Translate(Kind.LabelPlural).Resolve());
|
||
|
|
y += RowHeight + Gap;
|
||
|
|
|
||
|
|
float half = (inRect.width - Gap) / 2f;
|
||
|
|
if (Widgets.ButtonText(new Rect(0f, y, half, RowHeight), "BlaxDefaults_PageSave".Translate()))
|
||
|
|
Save();
|
||
|
|
if (Widgets.ButtonText(new Rect(half + Gap, y, half, RowHeight), "BlaxDefaults_PageCancel".Translate()))
|
||
|
|
Close();
|
||
|
|
}
|
||
|
|
|
||
|
|
private List<FloatMenuOption> PriorityOptions()
|
||
|
|
{
|
||
|
|
var list = new List<FloatMenuOption>();
|
||
|
|
foreach (StoragePriority p in Enum.GetValues(typeof(StoragePriority)))
|
||
|
|
{
|
||
|
|
if (p == StoragePriority.Unstored)
|
||
|
|
continue;
|
||
|
|
StoragePriority local = p;
|
||
|
|
list.Add(new FloatMenuOption(local.Label().CapitalizeFirst(), () => Scratch.Priority = local));
|
||
|
|
}
|
||
|
|
return list;
|
||
|
|
}
|
||
|
|
|
||
|
|
private List<FloatMenuOption> UnlistedOptions()
|
||
|
|
{
|
||
|
|
var list = new List<FloatMenuOption>();
|
||
|
|
foreach (UnlistedRule r in Enum.GetValues(typeof(UnlistedRule)))
|
||
|
|
{
|
||
|
|
UnlistedRule local = r;
|
||
|
|
list.Add(new FloatMenuOption(local.Label().CapitalizeFirst(), () => Unlisted = local));
|
||
|
|
}
|
||
|
|
return list;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The special filters the vanilla storage tab hides (ITab_Storage.HiddenSpecialThingFilters).</summary>
|
||
|
|
private static IEnumerable<SpecialThingFilterDef> HiddenSpecialFilters()
|
||
|
|
{
|
||
|
|
if (!ModsConfig.IdeologyActive)
|
||
|
|
yield break;
|
||
|
|
if (SpecialThingFilterDefOf.AllowVegetarian != null) yield return SpecialThingFilterDefOf.AllowVegetarian;
|
||
|
|
if (SpecialThingFilterDefOf.AllowCarnivore != null) yield return SpecialThingFilterDefOf.AllowCarnivore;
|
||
|
|
if (SpecialThingFilterDefOf.AllowCannibal != null) yield return SpecialThingFilterDefOf.AllowCannibal;
|
||
|
|
if (SpecialThingFilterDefOf.AllowInsectMeat != null) yield return SpecialThingFilterDefOf.AllowInsectMeat;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// The template the window would save now: kind.Capture(Scratch) under the kind's key, with the window's
|
||
|
|
/// unlisted rule, plus every name of the original template that doesn't resolve now, in the list it was in.
|
||
|
|
/// Tests call this instead of clicking Save.
|
||
|
|
/// </summary>
|
||
|
|
public StorageTemplate BuildTemplate()
|
||
|
|
{
|
||
|
|
StorageTemplate t = Kind.Capture(Scratch);
|
||
|
|
t.unlisted = Unlisted;
|
||
|
|
t.KeepUnresolvedFrom(Original);
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Saves BuildTemplate() with DefaultsLibrary.Set and closes the window.</summary>
|
||
|
|
public void Save()
|
||
|
|
{
|
||
|
|
DefaultsLibrary.Set(BuildTemplate());
|
||
|
|
Close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Lets the vanilla item filter draw without a game, for the edit window opened from the main menu. The
|
||
|
|
/// filter tree reads the game in two places: Listing_TreeThingFilter.DoCategoryChildren reads
|
||
|
|
/// Find.HiddenItemsManager (Verse/Listing_TreeThingFilter.cs:86), and DoThingDef (:190) and two search
|
||
|
|
/// helpers (:121, :406) call QuickSearchFilter.Matches(ThingDef), which reads it too
|
||
|
|
/// (RimWorld/QuickSearchFilter.cs:55). Find.HiddenItemsManager is Current.Game.hiddenItemsManager
|
||
|
|
/// (Verse/Find.cs:182) and throws at the main menu. In a game both replacements below do exactly what
|
||
|
|
/// vanilla does; without one nothing is hidden (defaults are global, so undiscovered items are offered too).
|
||
|
|
/// Patched on the callers, because the getter and Matches are small enough to be inlined.
|
||
|
|
/// </summary>
|
||
|
|
public static class MainMenuFilterGuard
|
||
|
|
{
|
||
|
|
private static HiddenItemsManager nothingHidden;
|
||
|
|
|
||
|
|
/// <summary>Getter calls and Matches(ThingDef) calls the transpiler replaced, per patched method.</summary>
|
||
|
|
public static readonly Dictionary<string, (int getter, int matches)> Replaced = new Dictionary<string, (int, int)>();
|
||
|
|
|
||
|
|
/// <summary>True when the item filter can draw without a game: both kinds of calls were found and replaced.</summary>
|
||
|
|
public static bool Ready
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
int getter = 0, matches = 0;
|
||
|
|
foreach (var r in Replaced.Values)
|
||
|
|
{
|
||
|
|
getter += r.getter;
|
||
|
|
matches += r.matches;
|
||
|
|
}
|
||
|
|
return getter > 0 && matches > 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The manager the filter reads at the main menu: hides nothing.</summary>
|
||
|
|
public static HiddenItemsManager NothingHidden
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
if (nothingHidden == null)
|
||
|
|
{
|
||
|
|
var m = new HiddenItemsManager();
|
||
|
|
m.ClearHiddenDefs();
|
||
|
|
nothingHidden = m;
|
||
|
|
}
|
||
|
|
return nothingHidden;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Replaces Find.HiddenItemsManager: the game's manager in a game, else NothingHidden.</summary>
|
||
|
|
public static HiddenItemsManager HiddenItems()
|
||
|
|
{
|
||
|
|
Game game = Current.Game;
|
||
|
|
return game != null ? game.hiddenItemsManager : NothingHidden;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Replaces QuickSearchFilter.Matches(ThingDef): vanilla in a game, else a label match with nothing hidden.</summary>
|
||
|
|
public static bool Matches(QuickSearchFilter filter, ThingDef td)
|
||
|
|
{
|
||
|
|
if (Current.Game != null)
|
||
|
|
return filter.Matches(td);
|
||
|
|
return filter.Matches(td.label);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static readonly MethodInfo HiddenItemsGetter = AccessTools.PropertyGetter(typeof(Find), nameof(Find.HiddenItemsManager));
|
||
|
|
|
||
|
|
internal static readonly MethodInfo MatchesThingDef = AccessTools.Method(typeof(QuickSearchFilter), nameof(QuickSearchFilter.Matches), new[] { typeof(ThingDef) });
|
||
|
|
}
|
||
|
|
|
||
|
|
[HarmonyPatch]
|
||
|
|
public static class Patch_Listing_TreeThingFilter_MainMenu
|
||
|
|
{
|
||
|
|
/// <summary>Every method of the filter tree, local functions included, that calls the getter or Matches(ThingDef).</summary>
|
||
|
|
public static IEnumerable<MethodBase> TargetMethods()
|
||
|
|
{
|
||
|
|
var targets = new List<MethodBase>();
|
||
|
|
foreach (MethodInfo m in AccessTools.GetDeclaredMethods(typeof(Listing_TreeThingFilter)))
|
||
|
|
{
|
||
|
|
if (m.IsAbstract || m.ContainsGenericParameters || m.GetMethodBody() == null)
|
||
|
|
continue;
|
||
|
|
List<CodeInstruction> code;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
code = PatchProcessor.GetOriginalInstructions(m);
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
foreach (CodeInstruction ci in code)
|
||
|
|
{
|
||
|
|
if (ci.Calls(MainMenuFilterGuard.HiddenItemsGetter) || ci.Calls(MainMenuFilterGuard.MatchesThingDef))
|
||
|
|
{
|
||
|
|
targets.Add(m);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return targets;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase original)
|
||
|
|
{
|
||
|
|
MethodInfo hiddenItems = AccessTools.Method(typeof(MainMenuFilterGuard), nameof(MainMenuFilterGuard.HiddenItems));
|
||
|
|
MethodInfo matches = AccessTools.Method(typeof(MainMenuFilterGuard), nameof(MainMenuFilterGuard.Matches));
|
||
|
|
int getterCalls = 0, matchesCalls = 0;
|
||
|
|
var result = new List<CodeInstruction>();
|
||
|
|
foreach (CodeInstruction ci in instructions)
|
||
|
|
{
|
||
|
|
if (ci.Calls(MainMenuFilterGuard.HiddenItemsGetter))
|
||
|
|
{
|
||
|
|
ci.opcode = OpCodes.Call;
|
||
|
|
ci.operand = hiddenItems;
|
||
|
|
getterCalls++;
|
||
|
|
}
|
||
|
|
else if (ci.Calls(MainMenuFilterGuard.MatchesThingDef))
|
||
|
|
{
|
||
|
|
// The filter instance and the def are already on the stack: the static takes them as its two arguments.
|
||
|
|
ci.opcode = OpCodes.Call;
|
||
|
|
ci.operand = matches;
|
||
|
|
matchesCalls++;
|
||
|
|
}
|
||
|
|
result.Add(ci);
|
||
|
|
}
|
||
|
|
MainMenuFilterGuard.Replaced[original?.Name ?? "?"] = (getterCalls, matchesCalls);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|