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

108 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RimWorld;
using Verse;
namespace BlaxCastes
{
/// <summary>A mod whose join defaults overlap with Castes: its package id, name and what it sets.</summary>
public sealed class OverlapMod
{
public readonly string PackageId;
public readonly string Name;
/// <summary>Keyed string naming the defaults that overlap.</summary>
public readonly string WhatKey;
public OverlapMod(string packageId, string name, string whatKey)
{
PackageId = packageId;
Name = name;
WhatKey = whatKey;
}
public string What => WhatKey.Translate();
}
/// <summary>
/// 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.
/// </summary>
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"),
};
/// <summary>Package ids treated as active on top of the real mod list. For tests only.</summary>
public static readonly HashSet<string> SimulatedActive = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// The mod list can't change without a restart, so the real detection runs once.
private static List<OverlapMod> detected;
/// <summary>The overlapping mods that are active, in list order.</summary>
public static List<OverlapMod> 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();
}
/// <summary>The settings page text, or null when no overlapping mod is active or the warnings are off.</summary>
public static string SettingsText()
{
if (BlaxCastesMod.Settings != null && !BlaxCastesMod.Settings.warnOverlaps)
return null;
List<OverlapMod> 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();
}
/// <summary>The message text for these mods, or null for none.</summary>
public static string MessageText(List<OverlapMod> 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"));
}
/// <summary>
/// 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.
/// </summary>
public static List<OverlapMod> ShowMessageOnce(GameComponent_BlaxCastes comp)
{
var named = new List<OverlapMod>();
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;
}
}
}