166 lines
5 KiB
C#
166 lines
5 KiB
C#
using HarmonyLib;
|
|
|
|
namespace IdleSpectator;
|
|
|
|
/// <summary>Era + meta genesis patches from mutation discovery.</summary>
|
|
[HarmonyPatch]
|
|
public static class MetaEventPatches
|
|
{
|
|
[HarmonyPatch(typeof(WorldAgeManager), nameof(WorldAgeManager.startNextAge))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixStartNextAge()
|
|
{
|
|
MetaInterestFeed.EmitEra(ReadCurrentEraId());
|
|
}
|
|
|
|
[HarmonyPatch(typeof(CultureManager), nameof(CultureManager.newCulture))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewCulture(Culture __result)
|
|
{
|
|
EmitNew("new_culture", "Culture", 68f, __result, "culture");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(ReligionManager), nameof(ReligionManager.newReligion))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewReligion(Religion __result)
|
|
{
|
|
EmitNew("new_religion", "Culture", 72f, __result, "religion");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(LanguageManager), nameof(LanguageManager.newLanguage))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewLanguage(Language __result)
|
|
{
|
|
EmitNew("new_language", "Culture", 60f, __result, "language");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(ClanManager), nameof(ClanManager.newClan))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewClan(Clan __result)
|
|
{
|
|
EmitNew("new_clan", "Politics", 70f, __result, "clan");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(ArmyManager), nameof(ArmyManager.newArmy))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewArmy(Army __result)
|
|
{
|
|
EmitNew("new_army", "Politics", 66f, __result, "army");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(AllianceManager), nameof(AllianceManager.newAlliance))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewAlliance(Alliance __result)
|
|
{
|
|
EmitNew("new_alliance", "Politics", 78f, __result, "alliance");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(CityManager), nameof(CityManager.newCity))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewCity(City __result)
|
|
{
|
|
EmitNew("new_city", "Politics", 74f, __result, "city");
|
|
}
|
|
|
|
[HarmonyPatch(typeof(KingdomManager), nameof(KingdomManager.makeNewCivKingdom))]
|
|
[HarmonyPostfix]
|
|
public static void PostfixNewCivKingdom(Kingdom __result, Actor pActor)
|
|
{
|
|
if (__result == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Actor anchor = pActor != null && pActor.isAlive() ? pActor : TryReadActor(__result);
|
|
if (anchor == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
MetaInterestFeed.EmitMeta(
|
|
"new_kingdom",
|
|
"Politics",
|
|
82f,
|
|
anchor,
|
|
EventReason.MetaNew(anchor, "kingdom"));
|
|
}
|
|
|
|
private static void EmitNew(
|
|
string eventId,
|
|
string category,
|
|
float strength,
|
|
object meta,
|
|
string kind)
|
|
{
|
|
Actor anchor = TryReadActor(meta);
|
|
if (anchor == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string label = EventReason.MetaNew(anchor, kind);
|
|
MetaInterestFeed.EmitMeta(eventId, category, strength, anchor, label);
|
|
}
|
|
|
|
private static Actor TryReadActor(object meta)
|
|
{
|
|
if (meta == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
foreach (string name in new[] { "founder", "creator", "leader", "king", "actor", "unit" })
|
|
{
|
|
object value = meta.GetType().GetField(name)?.GetValue(meta)
|
|
?? meta.GetType().GetProperty(name)?.GetValue(meta, null);
|
|
if (value is Actor actor && actor.isAlive())
|
|
{
|
|
return actor;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static string ReadCurrentEraId()
|
|
{
|
|
try
|
|
{
|
|
object world = World.world;
|
|
object ageManager = world?.GetType().GetField("era_manager")?.GetValue(world)
|
|
?? world?.GetType().GetProperty("era_manager")?.GetValue(world, null)
|
|
?? world?.GetType().GetField("age_manager")?.GetValue(world)
|
|
?? MapBox.instance?.GetType().GetField("era_manager")?.GetValue(MapBox.instance);
|
|
if (ageManager == null)
|
|
{
|
|
return "age_unknown";
|
|
}
|
|
|
|
object asset = ageManager.GetType().GetMethod("getCurrentAge")?.Invoke(ageManager, null)
|
|
?? ageManager.GetType().GetProperty("current_age")?.GetValue(ageManager, null)
|
|
?? ageManager.GetType().GetField("current_age")?.GetValue(ageManager);
|
|
if (asset != null)
|
|
{
|
|
object id = asset.GetType().GetField("id")?.GetValue(asset)
|
|
?? asset.GetType().GetProperty("id")?.GetValue(asset, null);
|
|
if (id != null)
|
|
{
|
|
return id.ToString();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
|
|
return "age_unknown";
|
|
}
|
|
}
|