rimworld-blax-laws/Source/BlaxLaws/DlcSupport.cs

118 lines
5.5 KiB
C#
Raw Permalink Normal View History

using System;
using RimWorld;
using Verse;
namespace BlaxLaws
{
/// <summary>
/// Laws works with any DLC combination, core only included (design: "DLC support"). This class names
/// where a def comes from, says what a missing catalogue entry needs, and guards every read of DLC-only
/// pawn state behind ModsConfig.&lt;DLC&gt;Active. ModLister.Check&lt;DLC&gt; is an install check, not an
/// active check, and is never used.
/// </summary>
public static class DlcSupport
{
public const string CoreName = "Core";
public const string CorePackageId = "ludeon.rimworld";
private const string OfficialPrefix = "ludeon.rimworld.";
/// <summary>The official DLCs by name, in release order.</summary>
public static readonly string[] DlcNames = { "Royalty", "Ideology", "Biotech", "Anomaly", "Odyssey" };
// ---- where a def comes from ---------------------------------------------------------
/// <summary>"Core", the DLC's name ("Ideology") or the mod's name, as the catalogue shows it.</summary>
public static string SourceName(ModContentPack mod)
{
if (mod == null || mod.IsCoreMod)
return CoreName;
if (IsDlcPackageId(mod.PackageId))
return DlcName(mod.PackageId);
return mod.Name.NullOrEmpty() ? mod.PackageIdPlayerFacing : mod.Name;
}
/// <summary>The package id a catalogue entry saves with a row, lower case. Core for a def without a mod.</summary>
public static string SourcePackageId(ModContentPack mod) => mod?.PackageId ?? CorePackageId;
/// <summary>True for "ludeon.rimworld.royalty" and the other official DLCs, false for Core and mods.</summary>
public static bool IsDlcPackageId(string packageId) =>
!packageId.NullOrEmpty() && packageId.ToLowerInvariant().StartsWith(OfficialPrefix);
/// <summary>"ludeon.rimworld.royalty" to "Royalty".</summary>
public static string DlcName(string packageId)
{
if (packageId.NullOrEmpty())
return packageId;
string id = packageId.ToLowerInvariant();
return (id.StartsWith(OfficialPrefix) ? id.Substring(OfficialPrefix.Length) : id).CapitalizeFirst();
}
/// <summary>
/// The official DLC a saved row's source names, by package id or by name ("Ideology"), or null for
/// Core and mods.
/// </summary>
public static string DlcOf(string savedMod, string savedPackageId)
{
if (IsDlcPackageId(savedPackageId))
return DlcName(savedPackageId);
foreach (string dlc in DlcNames)
{
if (string.Equals(dlc, savedMod, StringComparison.OrdinalIgnoreCase))
return dlc;
}
return null;
}
/// <summary>Whether the official DLC of this name is active. False for an unknown name.</summary>
public static bool IsDlcActive(string dlcName)
{
switch (dlcName?.ToLowerInvariant())
{
case "royalty": return ModsConfig.RoyaltyActive;
case "ideology": return ModsConfig.IdeologyActive;
case "biotech": return ModsConfig.BiotechActive;
case "anomaly": return ModsConfig.AnomalyActive;
case "odyssey": return ModsConfig.OdysseyActive;
default: return false;
}
}
/// <summary>"Needs Ideology".</summary>
public static string NeedsText(string dlcName) => "BlaxLaws_NeedsDlc".Translate(dlcName);
/// <summary>
/// What a row whose entry is missing shows: "Needs &lt;DLC&gt;" for a DLC source, else
/// "Missing: &lt;mod&gt;" (design: "A missing entry from a DLC shows Needs &lt;DLC&gt;").
/// </summary>
public static string MissingText(string savedMod, string savedPackageId)
{
string dlc = DlcOf(savedMod, savedPackageId);
if (dlc != null)
return NeedsText(dlc);
string mod = !savedMod.NullOrEmpty() ? savedMod : !savedPackageId.NullOrEmpty() ? savedPackageId : CoreName;
return "BlaxLaws_MissingMod".Translate(mod);
}
// ---- DLC-only pawn state, always behind the active check ------------------------------
/// <summary>A slave (Ideology).</summary>
public static bool IsSlave(Pawn p) => ModsConfig.IdeologyActive && p != null && p.IsSlave;
/// <summary>A child, never beaten (Biotech).</summary>
public static bool IsChild(Pawn p) => ModsConfig.BiotechActive && p != null && p.DevelopmentalStage.Child();
/// <summary>Holds an ideoligion role, which an arrest takes (Ideology; Precept_Role.cs:582-585).</summary>
public static bool HoldsRole(Pawn p) => ModsConfig.IdeologyActive && p?.Ideo != null && p.Ideo.GetRole(p) != null;
/// <summary>A mechanitor that controls mechs, which an arrest shuts down (Biotech; Pawn_GuestTracker.cs:536-539).</summary>
public static bool IsMechanitorWithMechs(Pawn p) =>
ModsConfig.BiotechActive && p?.mechanitor != null && MechanitorUtility.IsMechanitor(p) && p.mechanitor.ControlledPawns.Count > 0;
/// <summary>
/// Laws doesn't jail this pawn on its own while "Release gives back what jail took" isn't built or is
/// off: the case waits with "Arrest now" (design: "Jail for N days", hold).
/// </summary>
public static bool HeldForArrestNow(Pawn p) => HoldsRole(p) || IsMechanitorWithMechs(p);
}
}