worldbox-observer-mod/IdleSpectator/Events/Patches/MetaEventPatches.cs
DazedAnon 03729664be Implement building completion events and enhance earthquake handling.
- Update building event patches to include completion notifications
- Introduce earthquake event handling for ongoing seismic activity
- Modify event catalog to reflect changes in ambient interest settings
- Add new harness scenarios for library asset label validation
- Increment version to 0.28.17 in mod.json
2026-07-17 17:13:43 -05:00

182 lines
5.4 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());
}
/// <summary>
/// Ongoing quake hold. Age change is already covered by <see cref="PostfixStartNextAge"/>
/// (called from <c>WorldAgeManager.update</c> when progress completes).
/// </summary>
[HarmonyPatch(typeof(Earthquake), nameof(Earthquake.startQuake))]
[HarmonyPostfix]
public static void PostfixStartQuake(WorldTile pTile)
{
if (!Earthquake.isQuakeActive())
{
return;
}
MetaInterestFeed.EmitEarthquake(pTile);
}
[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";
}
}