Skip to content

Commit 3327fa0

Browse files
committed
feat(logging): enhance version logging with compatibility branch support
- Replaced direct version logging with a new method, `BuildVersionLogText`, to include compatibility branch information when applicable. - Removed unused `RunHistoryMissingModelDbGetByIdPatch` registration from `PatcherSetup`. - Refactored `RunHistoryMissingModelDbGetByIdTranspilerPatch` to streamline the transpilation process for missing model fallbacks, improving clarity and maintainability.
1 parent 309634f commit 3327fa0

3 files changed

Lines changed: 60 additions & 69 deletions

File tree

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.Reflection;
2+
using System.Reflection.Emit;
3+
using HarmonyLib;
14
using MegaCrit.Sts2.Core.Models;
25
using MegaCrit.Sts2.Core.Nodes.Screens.RunHistoryScreen;
36
using MegaCrit.Sts2.Core.Rooms;
@@ -7,35 +10,26 @@
710

811
namespace STS2RitsuLib.Lifecycle.Patches
912
{
10-
internal static class RunHistoryMissingModelScope
11-
{
12-
[ThreadStatic] private static int _depth;
13-
14-
internal static bool IsActive => _depth > 0;
15-
16-
internal static void Enter()
17-
{
18-
_depth++;
19-
}
20-
21-
internal static void Exit()
22-
{
23-
if (_depth > 0)
24-
_depth--;
25-
}
26-
}
27-
2813
/// <summary>
29-
/// Creates an execution scope for run-history UI methods that may read missing mod models.
14+
/// Replaces <c>ModelDb.GetById&lt;CharacterModel&gt;</c> and <c>GetById&lt;ActModel&gt;</c> in run-history UI with
15+
/// <see cref="RunHistoryMissingModelSupport" /> so missing mod content does not throw.
3016
/// </summary>
3117
public class RunHistoryMissingModelDbGetByIdTranspilerPatch : IPatchMethod
3218
{
19+
private static readonly MethodInfo CharacterFallback =
20+
AccessTools.DeclaredMethod(typeof(RunHistoryMissingModelSupport),
21+
nameof(RunHistoryMissingModelSupport.CharacterForRunHistory));
22+
23+
private static readonly MethodInfo ActFallback =
24+
AccessTools.DeclaredMethod(typeof(RunHistoryMissingModelSupport),
25+
nameof(RunHistoryMissingModelSupport.ActForRunHistory));
26+
3327
/// <inheritdoc />
3428
public static string PatchId => "run_history_missing_model_db_getbyid_transpile";
3529

3630
/// <inheritdoc />
3731
public static string Description =>
38-
"Create run-history scope for missing-model fallbacks";
32+
"Transpile run-history methods to use Character/Act fallbacks when ModelDb has no entry";
3933

4034
/// <inheritdoc />
4135
public static bool IsCritical => false;
@@ -58,61 +52,41 @@ public static ModPatchTarget[] GetTargets()
5852
}
5953

6054
/// <summary>
61-
/// Enters run-history missing-model support scope.
62-
/// </summary>
63-
public static void Prefix()
64-
{
65-
RunHistoryMissingModelScope.Enter();
66-
}
67-
68-
/// <summary>
69-
/// Exits run-history scope even if the target method throws.
55+
/// Harmony transpiler: redirect ModelDb lookups to RitsuLib fallbacks.
7056
/// </summary>
71-
public static void Finalizer()
57+
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
7258
{
73-
RunHistoryMissingModelScope.Exit();
74-
}
75-
}
76-
77-
/// <summary>
78-
/// Uses run-history-specific fallbacks for missing character/act lookups.
79-
/// </summary>
80-
public class RunHistoryMissingModelDbGetByIdPatch : IPatchMethod
81-
{
82-
/// <inheritdoc />
83-
public static string PatchId => "run_history_missing_model_db_getbyid";
84-
85-
/// <inheritdoc />
86-
public static string Description =>
87-
"Use Character/Act fallbacks in run-history scope when ModelDb.GetById has no entry";
88-
89-
/// <inheritdoc />
90-
public static bool IsCritical => false;
91-
92-
/// <inheritdoc />
93-
public static ModPatchTarget[] GetTargets()
94-
{
95-
return [new(typeof(ModelDb), nameof(ModelDb.GetById), [typeof(ModelId)])];
59+
foreach (var code in instructions)
60+
{
61+
if (code.operand is MethodInfo called)
62+
{
63+
if (IsModelDbGetByIdFor(called, typeof(CharacterModel)))
64+
{
65+
code.opcode = OpCodes.Call;
66+
code.operand = CharacterFallback;
67+
}
68+
else if (IsModelDbGetByIdFor(called, typeof(ActModel)))
69+
{
70+
code.opcode = OpCodes.Call;
71+
code.operand = ActFallback;
72+
}
73+
}
74+
75+
yield return code;
76+
}
9677
}
9778

98-
/// <summary>
99-
/// Replaces vanilla GetById throws with run-history fallback models.
100-
/// </summary>
101-
// ReSharper disable once InconsistentNaming
102-
public static bool Prefix<T>(ModelId id, ref T __result) where T : AbstractModel
79+
private static bool IsModelDbGetByIdFor(MethodInfo mi, Type typeArg)
10380
{
104-
if (!RunHistoryMissingModelScope.IsActive)
105-
return true;
81+
if (!mi.IsGenericMethod || mi.DeclaringType != typeof(ModelDb))
82+
return false;
10683

107-
if (typeof(T) == typeof(CharacterModel))
108-
{
109-
__result = (T)(AbstractModel)RunHistoryMissingModelSupport.CharacterForRunHistory(id);
84+
var def = mi.GetGenericMethodDefinition();
85+
if (def.Name != nameof(ModelDb.GetById))
11086
return false;
111-
}
11287

113-
if (typeof(T) != typeof(ActModel)) return true;
114-
__result = (T)(AbstractModel)RunHistoryMissingModelSupport.ActForRunHistory(id);
115-
return false;
88+
var args = mi.GetGenericArguments();
89+
return args.Length == 1 && args[0] == typeArg;
11690
}
11791
}
11892
}

RitsuLibFramework.PatcherSetup.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ private static void RegisterLifecyclePatches()
8383
patcher.RegisterPatch<NContinueRunInfoShowInfoModelNotFoundPatch>();
8484
patcher.RegisterPatch<NRunHistoryRefreshAndSelectRunSuppressRethrowPatch>();
8585
patcher.RegisterPatch<RunHistoryMissingModelDbGetByIdTranspilerPatch>();
86-
patcher.RegisterPatch<RunHistoryMissingModelDbGetByIdPatch>();
8786
patcher.RegisterPatch<NMultiplayerLoadGameScreenBeginRunMissingCharacterPatch>();
8887
patcher.RegisterPatch<NMultiplayerTestCharacterPaginatorAllCharactersPatch>();
8988
patcher.RegisterPatch<NCustomRunLoadScreenBeginRunMissingCharacterPatch>();

RitsuLibFramework.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static void Initialize()
166166

167167
Logger.Info($"Framework ID: {Const.ModId}");
168168
Logger.Info($"Framework Name: {Const.Name}");
169-
Logger.Info($"Version: {Const.Version}");
169+
Logger.Info(BuildVersionLogText());
170170
Logger.Info("Initializing shared framework...");
171171
ModTypeDiscoveryHub.EnsureBuiltInContributorsRegistered();
172172
RitsuLibSettingsStore.Initialize();
@@ -219,6 +219,24 @@ public static void Initialize()
219219
}
220220
}
221221

222+
private static string BuildVersionLogText()
223+
{
224+
var compatBranchLabel = GetCompatBranchLabel();
225+
if (string.IsNullOrWhiteSpace(compatBranchLabel))
226+
return $"Version: {Const.Version}";
227+
228+
return $"Version: {Const.Version} [兼容分支: {compatBranchLabel}]";
229+
}
230+
231+
private static string? GetCompatBranchLabel()
232+
{
233+
#if STS2_V_0_103_2
234+
return "0.103.2";
235+
#else
236+
return null;
237+
#endif
238+
}
239+
222240
/// <summary>
223241
/// Ensures profile-bound services (<c>ProfileManager</c>, profile-scoped <c>ModDataStore</c>) are initialized once.
224242
/// </summary>

0 commit comments

Comments
 (0)