using System; using System.Collections.Generic; using RimWorld; using Verse; namespace BlaxCastes { /// /// The Caste column in three tables. The XML patches put it in the Assign table right before /// HostilityResponse (Patches/BlaxCastes_Assign.xml), in the Schedule table right after the timetable /// (the tab draws its time assignment selector over the top of the header left of the timetable, where /// the column's buttons would sit), and in the Work table right before the copy and paste column, so /// just left of the work columns (Patches/BlaxCastes_ScheduleWork.xml). At startup this checks the /// placement, and removes the column again when the showAssignColumn setting is off, so turning the /// setting on or off takes effect after a restart. /// [StaticConstructorOnStartup] public static class CasteColumns { /// The Assign table's anchor: the Caste column goes right before it. public const string AnchorColumn = "HostilityResponse"; /// The Schedule table's column the Caste column goes right after. public const string ScheduleAfter = "Timetable"; /// The Schedule table's anchors without the timetable: the column goes right before the first one found. public static readonly string[] ScheduleAnchors = { "AllowedArea" }; /// The Work table's anchor. Without it the column goes before the first work column. public static readonly string[] WorkAnchors = { "CopyPasteWorkPriorities" }; static CasteColumns() { try { bool show = BlaxCastesMod.Settings == null || BlaxCastesMod.Settings.showAssignColumn; var notPlaced = new List(); foreach (PawnTableDef table in Tables) { if (show && table?.columns != null && Column != null && !table.columns.Contains(Column)) notPlaced.Add(table.defName); } ApplyAll(show); if (notPlaced.Count > 0) Log.Warning("[Blax Castes] The XML patches did not place the Caste column in " + string.Join(", ", notPlaced) + "; it was added in code instead."); } catch (Exception e) { Log.Error("[Blax Castes] Setting up the Caste column failed: " + e); } } /// The Assign tab's table. public static PawnTableDef Table => PawnTableDefOf.Assign; /// The Schedule tab's table (defName Restrict). public static PawnTableDef ScheduleTable => PawnTableDefOf.Restrict; public static PawnTableDef WorkTable => PawnTableDefOf.Work; public static IEnumerable Tables { get { yield return Table; yield return ScheduleTable; yield return WorkTable; } } public static PawnColumnDef Column => DefDatabase.GetNamedSilentFail(PawnColumnWorker_BlaxCaste.DefName); /// /// The Assign table only. Off: removes the column. 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 tabs pick the change up the next time they open. /// public static bool Apply(bool show) => ApplyTo(Table, show, AnchorColumn); /// All three tables. Returns true if any changed. public static bool ApplyAll(bool show) { bool changed = Apply(show); changed |= ApplyToSchedule(show); changed |= ApplyTo(WorkTable, show, WorkAnchors); return changed; } /// The Schedule table: right after the timetable, else as ApplyTo with ScheduleAnchors. public static bool ApplyToSchedule(bool show) { PawnTableDef table = ScheduleTable; PawnColumnDef column = Column; if (table?.columns == null || column == null) return false; if (show && !table.columns.Contains(column)) { int timetable = table.columns.FindIndex(c => c != null && c.defName == ScheduleAfter); if (timetable >= 0) { table.columns.Insert(timetable + 1, column); return true; } } return ApplyTo(table, show, ScheduleAnchors); } /// /// Off: removes the column from the table. On: adds it right before the first anchor found, else /// before the first work column, else after the first column, unless it is already there. /// public static bool ApplyTo(PawnTableDef table, bool show, params string[] anchors) { 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 = -1; foreach (string anchor in anchors) { at = table.columns.FindIndex(c => c != null && c.defName == anchor); if (at >= 0) break; } if (at < 0) at = table.columns.FindIndex(c => c?.workType != null); if (at < 0) at = Math.Min(1, table.columns.Count); table.columns.Insert(at, column); return true; } } }