using System; using System.Collections.Generic; using System.Linq; using System.Text; using RimWorld; using Verse; namespace BlaxCastes { /// A mod whose join defaults overlap with Castes: its package id, name and what it sets. public sealed class OverlapMod { public readonly string PackageId; public readonly string Name; /// Keyed string naming the defaults that overlap. public readonly string WhatKey; public OverlapMod(string packageId, string name, string whatKey) { PackageId = packageId; Name = name; WhatKey = whatKey; } public string What => WhatKey.Translate(); } /// /// Names active mods that also set policies when a pawn joins: on the settings page and in one message /// per save. Castes applies a join one tick after it happens, so its choice wins; the warning makes the /// double work visible. No patches against those mods. Detection goes through Compat.IsActive. /// public static class OverlapWarnings { public static readonly OverlapMod[] Mods = { new OverlapMod(Compat.BetterPawnControl, "Better Pawn Control", "BlaxCastes_OverlapWhatBpc"), new OverlapMod(Compat.BetterPawnControlForked, "Better Pawn Control Forked", "BlaxCastes_OverlapWhatBpc"), new OverlapMod(Compat.Defaults1trickPwnyta, "1trickPwnyta's Defaults", "BlaxCastes_OverlapWhatDefaults"), new OverlapMod(Compat.AutomaticSlaveOutfit, "Automatic Slave Outfit", "BlaxCastes_OverlapWhatSlaveOutfit"), new OverlapMod(Compat.PrisonerDefaults, "PrisonerDefaults", "BlaxCastes_OverlapWhatPrisoner"), }; /// Package ids treated as active on top of the real mod list. For tests only. public static readonly HashSet SimulatedActive = new HashSet(StringComparer.OrdinalIgnoreCase); // The mod list can't change without a restart, so the real detection runs once. private static List detected; /// The overlapping mods that are active, in list order. public static List Active() { detected ??= Mods.Where(m => Compat.IsActive(m.PackageId)).ToList(); if (SimulatedActive.Count == 0) return detected; return Mods.Where(m => detected.Contains(m) || SimulatedActive.Contains(m.PackageId)).ToList(); } /// The settings page text, or null when no overlapping mod is active or the warnings are off. public static string SettingsText() { if (BlaxCastesMod.Settings != null && !BlaxCastesMod.Settings.warnOverlaps) return null; List active = Active(); if (active.Count == 0) return null; var sb = new StringBuilder(); sb.Append("BlaxCastes_OverlapSettingsIntro".Translate()); foreach (OverlapMod mod in active) sb.Append("\n- " + "BlaxCastes_OverlapLine".Translate(mod.Name.Named("MOD"), mod.What.Named("WHAT"))); sb.Append("\n" + "BlaxCastes_OverlapAdvice".Translate()); return sb.ToString(); } /// The message text for these mods, or null for none. public static string MessageText(List mods) { if (mods == null || mods.Count == 0) return null; string names = mods.Select(m => m.Name).ToCommaList(useAnd: true); return "BlaxCastes_OverlapMessage".Translate(names.Named("MODS")); } /// /// Posts one message naming the active overlapping mods this save has not been warned about yet, and /// remembers them in the save. Returns the mods it named. /// public static List ShowMessageOnce(GameComponent_BlaxCastes comp) { var named = new List(); if (comp == null || (BlaxCastesMod.Settings != null && !BlaxCastesMod.Settings.warnOverlaps)) return named; foreach (OverlapMod mod in Active()) { if (!comp.OverlapWarned(mod.PackageId)) named.Add(mod); } string text = MessageText(named); if (text.NullOrEmpty()) return named; foreach (OverlapMod mod in named) comp.MarkOverlapWarned(mod.PackageId); LongEventHandler.ExecuteWhenFinished(() => Messages.Message(text, MessageTypeDefOf.CautionInput, historical: true)); return named; } } }