using System; using System.Collections.Generic; using System.Reflection; using RimWorld; using Verse; namespace BlaxDefaults { /// /// Which DLC a def needs, for saved defaults whose def is not loaded. A vanilla DefOf field carries /// [MayRequireBiotech] (or its DLC's attribute) for DLC content, such as ThingDefOf.GrowthVat; a name /// without such a field (from a removed mod) has no DLC and counts as a name from an inactive mod. /// Every feature that needs a DLC checks ModsConfig.<DLC>Active before it touches DLC content. /// public static class DlcSupport { public const string Royalty = "ludeon.rimworld.royalty"; public const string Ideology = "ludeon.rimworld.ideology"; public const string Biotech = "ludeon.rimworld.biotech"; public const string Anomaly = "ludeon.rimworld.anomaly"; public const string Odyssey = "ludeon.rimworld.odyssey"; // (def type, defName) -> lower-case DLC package id, from every [DefOf] class of the game. private static Dictionary<(Type, string), string> mayRequire; /// /// The package id of the DLC a def of this type and name needs, from the vanilla DefOf field with its /// name; null when no DefOf field names it with a MayRequire attribute. /// public static string RequiredDlc(Type defType, string defName) { if (defName.NullOrEmpty()) return null; mayRequire ??= Scan(); for (Type t = defType; t != null && typeof(Def).IsAssignableFrom(t); t = t.BaseType) { if (mayRequire.TryGetValue((t, defName), out string id)) return id; } return null; } public static string RequiredDlcOfThing(string defName) => RequiredDlc(typeof(ThingDef), defName); /// True when the DLC with this package id is loaded. public static bool IsActive(string dlcPackageId) => ModLister.GetActiveModWithIdentifier(dlcPackageId, ignorePostfix: true) != null; /// /// The DLC that a def name which does not resolve is waiting for: its package id when a vanilla DefOf /// field marks it and that DLC is off, else null (a name from an inactive mod). /// public static string MissingDlcOfThing(string defName) { string id = RequiredDlcOfThing(defName); return id != null && !IsActive(id) ? id : null; } /// "Biotech" for "ludeon.rimworld.biotech". public static string DlcName(string dlcPackageId) { switch (dlcPackageId?.ToLowerInvariant()) { case Royalty: return "Royalty"; case Ideology: return "Ideology"; case Biotech: return "Biotech"; case Anomaly: return "Anomaly"; case Odyssey: return "Odyssey"; default: return dlcPackageId; } } /// "Needs Biotech", the note on a disabled row or section. public static string NeedsLabel(string dlcPackageId) => "BlaxDefaults_NeedsDlc".Translate(DlcName(dlcPackageId)); private static Dictionary<(Type, string), string> Scan() { var d = new Dictionary<(Type, string), string>(); foreach (Type type in typeof(ThingDefOf).Assembly.GetTypes()) { if (!type.IsDefined(typeof(DefOf), false)) continue; foreach (FieldInfo f in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { if (!typeof(Def).IsAssignableFrom(f.FieldType)) continue; var attr = f.GetCustomAttribute(true); if (attr?.modId != null) d[(f.FieldType, f.Name)] = attr.modId.ToLowerInvariant(); } } return d; } } }