|
| 1 | +using System.Reflection; |
| 2 | +using HarmonyLib; |
| 3 | +using MegaCrit.Sts2.addons.mega_text; |
| 4 | +using MegaCrit.Sts2.Core.Models; |
| 5 | +using MegaCrit.Sts2.Core.Nodes.Orbs; |
| 6 | +using MegaCrit.Sts2.Core.Random; |
| 7 | +using STS2RitsuLib.Patching.Models; |
| 8 | + |
| 9 | +namespace STS2RitsuLib.Scaffolding.Content.Patches |
| 10 | +{ |
| 11 | + internal static class ModOrbRandomPoolPolicyRuntime |
| 12 | + { |
| 13 | + private static readonly FieldInfo? VanillaValidOrbsField = AccessTools.Field(typeof(OrbModel), "_validOrbs"); |
| 14 | + |
| 15 | + public static bool HasModdedCandidates() |
| 16 | + { |
| 17 | + var vanillaIds = GetVanillaRandomOrbIds(); |
| 18 | + return vanillaIds.Length != 0 && ModelDb.Orbs.Any(orb => ShouldAddFromModelDbOrbs(vanillaIds, orb)); |
| 19 | + } |
| 20 | + |
| 21 | + public static bool TryGetRandomOrb(Rng rng, out OrbModel orb) |
| 22 | + { |
| 23 | + var vanillaIds = GetVanillaRandomOrbIds(); |
| 24 | + if (vanillaIds.Length == 0) |
| 25 | + { |
| 26 | + orb = null!; |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + var candidates = BuildCandidates(vanillaIds); |
| 31 | + var selected = rng.NextItem(candidates); |
| 32 | + if (selected == null) |
| 33 | + { |
| 34 | + orb = null!; |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + orb = selected; |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + private static ModelId[] GetVanillaRandomOrbIds() |
| 43 | + { |
| 44 | + return VanillaValidOrbsField?.GetValue(null) as ModelId[] ?? []; |
| 45 | + } |
| 46 | + |
| 47 | + private static OrbModel[] BuildCandidates(IReadOnlyCollection<ModelId> vanillaIds) |
| 48 | + { |
| 49 | + return vanillaIds |
| 50 | + .Select(static id => ModelDb.GetByIdOrNull<OrbModel>(id)) |
| 51 | + .OfType<OrbModel>() |
| 52 | + .Concat(ModelDb.Orbs.Where(orb => ShouldAddFromModelDbOrbs(vanillaIds, orb))) |
| 53 | + .DistinctBy(static orb => orb.Id) |
| 54 | + .ToArray(); |
| 55 | + } |
| 56 | + |
| 57 | + private static bool ShouldAddFromModelDbOrbs(IReadOnlyCollection<ModelId> vanillaIds, OrbModel orb) |
| 58 | + { |
| 59 | + return !vanillaIds.Contains(orb.Id) && |
| 60 | + orb is IModOrbRandomPoolPolicy { AllowInRandomOrbPool: true }; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Replaces <see cref="OrbModel.GetRandomOrb" /> only when registered mod orbs opt in to the random pool. |
| 66 | + /// 仅当已注册 mod 充能球选择加入随机池时替换 <see cref="OrbModel.GetRandomOrb" />。 |
| 67 | + /// </summary> |
| 68 | + public sealed class OrbModelRandomPoolPolicyPatch : IPatchMethod |
| 69 | + { |
| 70 | + /// <inheritdoc /> |
| 71 | + public static string PatchId => "orb_model_random_pool_policy"; |
| 72 | + |
| 73 | + /// <inheritdoc /> |
| 74 | + public static bool IsCritical => false; |
| 75 | + |
| 76 | + /// <inheritdoc /> |
| 77 | + public static string Description => "Include opt-in mod orbs in OrbModel.GetRandomOrb"; |
| 78 | + |
| 79 | + /// <inheritdoc /> |
| 80 | + public static ModPatchTarget[] GetTargets() |
| 81 | + { |
| 82 | + return [new(typeof(OrbModel), nameof(OrbModel.GetRandomOrb), [typeof(Rng)])]; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Uses the vanilla random pool plus registered <see cref="IModOrbRandomPoolPolicy" /> opt-in candidates. |
| 87 | + /// 使用原版随机池,加上已注册且通过 <see cref="IModOrbRandomPoolPolicy" /> 选择加入的候选项。 |
| 88 | + /// </summary> |
| 89 | + [HarmonyAfter(Const.BaseLibHarmonyId, Const.FrameworkContentRegistryHarmonyId)] |
| 90 | + [HarmonyPriority(Priority.Last)] |
| 91 | + // ReSharper disable once InconsistentNaming |
| 92 | + public static bool Prefix(Rng rng, ref OrbModel __result) |
| 93 | + { |
| 94 | + if (!ModOrbRandomPoolPolicyRuntime.HasModdedCandidates()) |
| 95 | + return true; |
| 96 | + |
| 97 | + if (!ModOrbRandomPoolPolicyRuntime.TryGetRandomOrb(rng, out var orb)) |
| 98 | + return true; |
| 99 | + |
| 100 | + __result = orb; |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + internal static class ModOrbValueDisplayPolicyRuntime |
| 106 | + { |
| 107 | + private static readonly FieldInfo? PassiveLabelField = AccessTools.Field(typeof(NOrb), "_passiveLabel"); |
| 108 | + private static readonly FieldInfo? EvokeLabelField = AccessTools.Field(typeof(NOrb), "_evokeLabel"); |
| 109 | + |
| 110 | + public static void Apply(NOrb node, bool isEvoking) |
| 111 | + { |
| 112 | + if (node.Model is not IModOrbValueDisplayPolicy policy || |
| 113 | + policy.ValueDisplayMode == ModOrbValueDisplayMode.Vanilla) |
| 114 | + return; |
| 115 | + |
| 116 | + var passiveLabel = PassiveLabelField?.GetValue(node) as MegaLabel; |
| 117 | + var evokeLabel = EvokeLabelField?.GetValue(node) as MegaLabel; |
| 118 | + if (passiveLabel == null || evokeLabel == null) |
| 119 | + return; |
| 120 | + |
| 121 | + var (showPassive, showEvoke) = policy.ValueDisplayMode switch |
| 122 | + { |
| 123 | + ModOrbValueDisplayMode.Hidden => (false, false), |
| 124 | + ModOrbValueDisplayMode.Contextual => (!isEvoking, isEvoking), |
| 125 | + ModOrbValueDisplayMode.SinglePassive => (true, false), |
| 126 | + ModOrbValueDisplayMode.SingleEvoke => (false, true), |
| 127 | + ModOrbValueDisplayMode.Both => (true, true), |
| 128 | + _ => (passiveLabel.Visible, evokeLabel.Visible), |
| 129 | + }; |
| 130 | + |
| 131 | + passiveLabel.Visible = showPassive; |
| 132 | + evokeLabel.Visible = showEvoke; |
| 133 | + if (showPassive) |
| 134 | + passiveLabel.SetTextAutoSize(policy.PassiveValueDisplayText); |
| 135 | + if (showEvoke) |
| 136 | + evokeLabel.SetTextAutoSize(policy.EvokeValueDisplayText); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Applies <see cref="IModOrbValueDisplayPolicy" /> after vanilla orb visual refresh. |
| 142 | + /// 在原版充能球视觉刷新之后应用 <see cref="IModOrbValueDisplayPolicy" />。 |
| 143 | + /// </summary> |
| 144 | + public sealed class NOrbValueDisplayPolicyPatch : IPatchMethod |
| 145 | + { |
| 146 | + /// <inheritdoc /> |
| 147 | + public static string PatchId => "norb_value_display_policy"; |
| 148 | + |
| 149 | + /// <inheritdoc /> |
| 150 | + public static bool IsCritical => false; |
| 151 | + |
| 152 | + /// <inheritdoc /> |
| 153 | + public static string Description => "Allow mod orbs to control passive/evoke value labels"; |
| 154 | + |
| 155 | + /// <inheritdoc /> |
| 156 | + public static ModPatchTarget[] GetTargets() |
| 157 | + { |
| 158 | + return [new(typeof(NOrb), nameof(NOrb.UpdateVisuals), [typeof(bool)])]; |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Reconciles labels with the mod orb's requested display mode. |
| 163 | + /// 按 mod 充能球请求的显示模式同步标签。 |
| 164 | + /// </summary> |
| 165 | + [HarmonyAfter(Const.BaseLibHarmonyId)] |
| 166 | + [HarmonyPriority(Priority.Last)] |
| 167 | + // ReSharper disable once InconsistentNaming |
| 168 | + public static void Postfix(NOrb __instance, bool isEvoking) |
| 169 | + { |
| 170 | + ModOrbValueDisplayPolicyRuntime.Apply(__instance, isEvoking); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments