-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFile.cs
More file actions
136 lines (117 loc) · 4.85 KB
/
MainFile.cs
File metadata and controls
136 lines (117 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System.Reflection;
using Godot;
using HarmonyLib;
using MegaCrit.Sts2.Core.Modding;
using MegaCrit.Sts2.Core.Nodes;
using MegaCrit.Sts2.Core.Nodes.Screens.MainMenu;
using YuWanCard.Badges;
using YuWanCard.Characters;
using YuWanCard.Config;
using YuWanCard.Core.Badges;
using YuWanCard.Core.Interop;
using YuWanCard.Core.Multiplayer;
using YuWanCard.Core.RightClick;
using YuWanCard.Core.Transcendence;
using YuWanCard.Multiplayer;
using YuWanCard.Utils;
using YuWanCard.Hextech;
namespace YuWanCard;
[ModInitializer(nameof(Initialize))]
public partial class MainFile : Node
{
public const string ModId = "YuWanCard";
public static MegaCrit.Sts2.Core.Logging.Logger Logger { get; } =
new(ModId, MegaCrit.Sts2.Core.Logging.LogType.Generic);
public static YuWanCardConfig? Config { get; private set; }
public static void Initialize()
{
ModLifecycle.Publish(ModLifecyclePhase.Initializing);
var patcher = new ModPatcher(ModId);
// Phase 1: Bulk Harmony patches (auto-discovered via [HarmonyPatch] attributes)
// Uses PatchAllSafe for per-class try/catch — essential for Android/Mono AOT compatibility.
// Exclude patches that must be applied conditionally by platform.
var manualPatches = new HashSet<string>
{
nameof(Core.Patches.YuWanDailyRunModifierFilterPatch),
nameof(Core.Patches.ProgressStateEncounterStatsPatch)
};
patcher.PatchAllSafe(Assembly.GetExecutingAssembly(), manualPatches);
ModLifecycle.Publish(ModLifecyclePhase.PatchesApplied);
// Phase 2: Platform-conditional patches (wrapped to survive mobile)
patcher.ApplySingle(
h => Core.Patches.AutoSlayCharacterPatch.ApplyPatch(h), "AutoSlayCharacter");
patcher.ApplySingle(
h => Core.Patches.AutoSlayOptionsPatch.ApplyPatch(h), "AutoSlayOptions");
patcher.ApplySingle(
h => Core.Patches.CustomEnergyIconPatches.Apply(h), "CustomEnergyIcons");
patcher.ApplySingle(
h => ModInteropProcessor.Process(h, Assembly.GetExecutingAssembly()), "ModInterop");
patcher.ApplySingle(
HextechRuntimeCompat.TryInstall, "HextechRuntimeCompat");
// Desktop-only patches — skip on Android to avoid triggering NDailyRunScreen
// static constructor which has a known NRE bug on Mono AOT
if (!IsMobilePlatform())
{
patcher.ApplySingle(
h => h.CreateClassProcessor(typeof(Core.Patches.YuWanDailyRunModifierFilterPatch)).Patch(),
"YuWanDailyRunModifierFilter");
patcher.ApplySingle(
h => h.CreateClassProcessor(typeof(Core.Patches.ProgressStateEncounterStatsPatch)).Patch(),
"ProgressStateEncounterStats");
}
// Phase 3: Content discovery — scan for [Pool] and registration attributes
ModLifecycle.Publish(ModLifecyclePhase.ContentRegistering);
ContentRegistry.RegisterAll(Assembly.GetExecutingAssembly());
SavedPropertyRegistration.RegisterAssembly(Assembly.GetExecutingAssembly());
TranscendenceRegistry.RegisterDefaults();
CustomBadgeRegistry.Register((run, playerId) => new PigTycoonBadge(run, playerId));
CustomBadgeRegistry.Register((run, playerId) => new WerewolfBadge(run, playerId));
ModLifecycle.Publish(ModLifecyclePhase.ContentRegistered);
// Phase 4: Config, scene conversions, multiplayer, assets
Config = new YuWanCardConfig();
ConfigRegistrar.TryDeferredRegister();
NodeFactory.Init();
Pig.RegisterScenes();
TeammatePayMessageHandler.Register();
SavedPropertySyncMessageHandler.Register();
YuWanRightClickMessageHandler.Register();
AssetPreloader.Preload();
CloudAnalyticsService.Initialize();
ModLifecycle.Publish(ModLifecyclePhase.Initialized);
Logger.Info("YuWanCard initialized");
}
/// <summary>
/// Returns true on Android/iOS to gate patches that access types with
/// broken static constructors on Mono AOT.
/// </summary>
private static bool IsMobilePlatform()
{
try
{
var osName = Godot.OS.GetName();
return osName == "Android" || osName == "iOS";
}
catch
{
return false; // Assume desktop if we can't detect
}
}
}
[HarmonyPatch(typeof(NMainMenu), nameof(NMainMenu._Ready))]
public static class NMainMenu_ConfigRegisterPatch
{
public static void Postfix()
{
ConfigRegistrar.TryDeferredRegister();
HextechRuntimeCompat.TryInstallIfAvailable();
}
}
[HarmonyPatch(typeof(NGame), nameof(NGame._Ready))]
public static class NGame_Ready_ConfigPreloadPatch
{
public static void Prefix()
{
ConfigRegistrar.TryDeferredRegister();
HextechRuntimeCompat.TryInstallIfAvailable();
}
}