rimworld-blax-castes/Source/BlaxCastes/AssignColumn.cs

61 lines
2.4 KiB
C#
Raw Normal View History

using System;
using RimWorld;
using Verse;
namespace BlaxCastes
{
/// <summary>
/// The Caste column in the Assign table. Patches/BlaxCastes_Assign.xml puts it right before
/// HostilityResponse; at startup this removes it again when the showAssignColumn setting is off,
/// so turning the setting on or off takes effect after a restart.
/// </summary>
[StaticConstructorOnStartup]
public static class AssignColumn
{
public const string AnchorColumn = "HostilityResponse";
static AssignColumn()
{
try
{
bool show = BlaxCastesMod.Settings == null || BlaxCastesMod.Settings.showAssignColumn;
PawnTableDef table = Table;
bool missing = show && table?.columns != null && Column != null && !table.columns.Contains(Column);
Apply(show);
if (missing)
Log.Warning("[Blax Castes] The XML patch did not place the Caste column in the Assign table; it was added in code instead.");
}
catch (Exception e)
{
Log.Error("[Blax Castes] Setting up the Caste column failed: " + e);
}
}
public static PawnTableDef Table => PawnTableDefOf.Assign;
public static PawnColumnDef Column => DefDatabase<PawnColumnDef>.GetNamedSilentFail(PawnColumnWorker_BlaxCaste.DefName);
/// <summary>
/// Off: removes the column from the Assign table def. On: adds it back right before
/// HostilityResponse (or after the first column if that is gone) unless it is already there.
/// Returns true if the table changed. Open Assign tabs pick the change up the next time they open.
/// </summary>
public static bool Apply(bool show)
{
PawnTableDef table = Table;
PawnColumnDef column = Column;
if (table?.columns == null || column == null)
return false;
if (!show)
return table.columns.RemoveAll(c => c == column) > 0;
if (table.columns.Contains(column))
return false;
int at = table.columns.FindIndex(c => c != null && c.defName == AnchorColumn);
if (at < 0)
at = Math.Min(1, table.columns.Count);
table.columns.Insert(at, column);
return true;
}
}
}