100 lines
4.5 KiB
C#
100 lines
4.5 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Reflection;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxCastes
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Castes works with any combination of DLCs. This class answers two questions for that:
|
||
|
|
/// - Which official DLC a stored def name belongs to while that DLC is off. Such a def is not in the
|
||
|
|
/// game, so its DLC is read from the [MayRequire...] attribute of the vanilla DefOf field with its
|
||
|
|
/// name: TimeAssignmentDefOf.Meditate is Royalty, WorkTypeDefOf.Childcare Biotech, DarkStudy Anomaly
|
||
|
|
/// and Fishing Odyssey. A caste keeps those names without a warning, and they work again once the
|
||
|
|
/// DLC is back (a save made with the DLC, loaded without it). Names from a removed mod have no such
|
||
|
|
/// field and are still dropped with a warning.
|
||
|
|
/// - Which text to show without Ideology: keys that name slave events or the Slave caste have a
|
||
|
|
/// "NoIdeology" variant.
|
||
|
|
/// </summary>
|
||
|
|
public static class DlcSupport
|
||
|
|
{
|
||
|
|
private const string OfficialPrefix = "ludeon.rimworld.";
|
||
|
|
|
||
|
|
private static Dictionary<(Type, string), string> owners;
|
||
|
|
|
||
|
|
/// <summary>The package id (lower case) of the official DLC a vanilla DefOf field of this name and def type requires, or null.</summary>
|
||
|
|
public static string OwnerOf(Type defType, string defName)
|
||
|
|
{
|
||
|
|
if (defType == null || defName.NullOrEmpty())
|
||
|
|
return null;
|
||
|
|
owners ??= ReadOwners();
|
||
|
|
return owners.TryGetValue((defType, defName), out string id) ? id : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// The name of the official DLC the def needs ("Royalty"), while that DLC is off. Null when the DLC is
|
||
|
|
/// on, or when the def belongs to no DLC (the core game or a mod).
|
||
|
|
/// </summary>
|
||
|
|
public static string InactiveDlcFor(Type defType, string defName)
|
||
|
|
{
|
||
|
|
string id = OwnerOf(defType, defName);
|
||
|
|
return id == null || ModsConfig.IsActive(id) ? null : DlcName(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <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 key's "NoIdeology" variant while Ideology is off and the variant exists. Used for texts that name
|
||
|
|
/// slave events (enslaved, freed) or the Slave caste, which cannot happen without Ideology. CanTranslate
|
||
|
|
/// asks the active language only, and Castes ships English only, so the default language counts too:
|
||
|
|
/// a game in another language shows the English variant rather than the text about slaves.
|
||
|
|
/// </summary>
|
||
|
|
public static string IdeologyKey(string key)
|
||
|
|
{
|
||
|
|
if (ModsConfig.IdeologyActive || key.NullOrEmpty())
|
||
|
|
return key;
|
||
|
|
string variant = key + "NoIdeology";
|
||
|
|
bool exists = variant.CanTranslate() || (LanguageDatabase.defaultLanguage?.HaveTextForKey(variant) ?? false);
|
||
|
|
return exists ? variant : key;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Every Def field of the vanilla DefOf classes that carries a MayRequire attribute for an official DLC.</summary>
|
||
|
|
private static Dictionary<(Type, string), string> ReadOwners()
|
||
|
|
{
|
||
|
|
var result = new Dictionary<(Type, string), string>();
|
||
|
|
Type[] types;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
types = typeof(DefOf).Assembly.GetTypes();
|
||
|
|
}
|
||
|
|
catch (ReflectionTypeLoadException e)
|
||
|
|
{
|
||
|
|
types = e.Types.Where(t => t != null).ToArray();
|
||
|
|
}
|
||
|
|
foreach (Type type in types)
|
||
|
|
{
|
||
|
|
if (!type.IsDefined(typeof(DefOf), false))
|
||
|
|
continue;
|
||
|
|
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Static))
|
||
|
|
{
|
||
|
|
if (!typeof(Def).IsAssignableFrom(field.FieldType))
|
||
|
|
continue;
|
||
|
|
string id = field.GetCustomAttribute<MayRequireAttribute>(inherit: true)?.modId?.ToLowerInvariant();
|
||
|
|
if (id != null && id.StartsWith(OfficialPrefix))
|
||
|
|
result[(field.FieldType, field.Name)] = id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|