|
| 1 | +namespace STS2RitsuLib.Compat |
| 2 | +{ |
| 3 | + /// <summary> |
| 4 | + /// Stable framework ids used by runtime interop checks. |
| 5 | + /// </summary> |
| 6 | + internal static class ExternalFrameworkIds |
| 7 | + { |
| 8 | + public const string BaseLib = "baselib"; |
| 9 | + public const string BaseLibToRitsuGenerated = "baselib-to-ritsu-generated"; |
| 10 | + public const string ModConfig = "modconfig"; |
| 11 | + } |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Central registry for external framework presence checks. |
| 15 | + /// Known frameworks are probe-based and can be extended with custom detectors. |
| 16 | + /// </summary> |
| 17 | + internal static class ExternalFrameworkRegistry |
| 18 | + { |
| 19 | + private static readonly Lock Gate = new(); |
| 20 | + private static readonly Dictionary<string, Func<bool>> CustomDetectors = []; |
| 21 | + |
| 22 | + private static readonly Dictionary<string, ProbeSpec> BuiltInProbes = new(StringComparer.OrdinalIgnoreCase) |
| 23 | + { |
| 24 | + [ExternalFrameworkIds.BaseLib] = new( |
| 25 | + ExternalFrameworkIds.BaseLib, |
| 26 | + ["BaseLib.Patches.Hooks.MaxHandSizePatch", "BaseLib.Hooks.HealthBarForecastRegistry"]), |
| 27 | + [ExternalFrameworkIds.BaseLibToRitsuGenerated] = new( |
| 28 | + ExternalFrameworkIds.BaseLibToRitsuGenerated, |
| 29 | + ["BaseLibToRitsu.Generated.ModConfigRegistry"]), |
| 30 | + [ExternalFrameworkIds.ModConfig] = new( |
| 31 | + ExternalFrameworkIds.ModConfig, |
| 32 | + ["ModConfig.ModConfigApi"]), |
| 33 | + }; |
| 34 | + |
| 35 | + private static readonly Dictionary<string, bool> PresenceCache = new(StringComparer.OrdinalIgnoreCase); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Registers a custom framework detector. The latest detector with the same id wins. |
| 39 | + /// </summary> |
| 40 | + public static void RegisterFrameworkDetector(string frameworkId, Func<bool> detector) |
| 41 | + { |
| 42 | + ArgumentException.ThrowIfNullOrWhiteSpace(frameworkId); |
| 43 | + ArgumentNullException.ThrowIfNull(detector); |
| 44 | + |
| 45 | + lock (Gate) |
| 46 | + { |
| 47 | + CustomDetectors[frameworkId] = detector; |
| 48 | + PresenceCache.Remove(frameworkId); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Returns whether the specified framework appears to be present. |
| 54 | + /// </summary> |
| 55 | + public static bool IsFrameworkPresent(string frameworkId) |
| 56 | + { |
| 57 | + ArgumentException.ThrowIfNullOrWhiteSpace(frameworkId); |
| 58 | + |
| 59 | + lock (Gate) |
| 60 | + { |
| 61 | + if (PresenceCache.TryGetValue(frameworkId, out var cached)) |
| 62 | + return cached; |
| 63 | + |
| 64 | + var detected = DetectFrameworkCore(frameworkId); |
| 65 | + PresenceCache[frameworkId] = detected; |
| 66 | + return detected; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Refreshes all known framework presence states. |
| 72 | + /// </summary> |
| 73 | + public static void RefreshKnownFrameworkPresence(string reason) |
| 74 | + { |
| 75 | + lock (Gate) |
| 76 | + { |
| 77 | + PresenceCache.Clear(); |
| 78 | + foreach (var frameworkId in BuiltInProbes.Keys) |
| 79 | + PresenceCache[frameworkId] = DetectFrameworkCore(frameworkId); |
| 80 | + foreach (var frameworkId in CustomDetectors.Keys) |
| 81 | + PresenceCache[frameworkId] = DetectFrameworkCore(frameworkId); |
| 82 | + } |
| 83 | + |
| 84 | + RitsuLibFramework.Logger.Info($"[Compat] External framework presence refreshed ({reason})."); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Resolves <paramref name="fullTypeName" /> from loaded assemblies. |
| 89 | + /// </summary> |
| 90 | + public static Type? ResolveType(string fullTypeName) |
| 91 | + { |
| 92 | + var byQualifiedName = Type.GetType(fullTypeName); |
| 93 | + if (byQualifiedName != null) |
| 94 | + return byQualifiedName; |
| 95 | + |
| 96 | + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) |
| 97 | + { |
| 98 | + Type? type = null; |
| 99 | + try |
| 100 | + { |
| 101 | + type = assembly.GetType(fullTypeName, false); |
| 102 | + } |
| 103 | + catch |
| 104 | + { |
| 105 | + // ignored |
| 106 | + } |
| 107 | + |
| 108 | + if (type != null) |
| 109 | + return type; |
| 110 | + } |
| 111 | + |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + private static bool DetectFrameworkCore(string frameworkId) |
| 116 | + { |
| 117 | + if (CustomDetectors.TryGetValue(frameworkId, out var customDetector)) |
| 118 | + try |
| 119 | + { |
| 120 | + return customDetector(); |
| 121 | + } |
| 122 | + catch (Exception ex) |
| 123 | + { |
| 124 | + RitsuLibFramework.Logger.Warn( |
| 125 | + $"[Compat] Custom framework detector '{frameworkId}' failed: {ex.Message}"); |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + if (!BuiltInProbes.TryGetValue(frameworkId, out var spec)) |
| 130 | + return false; |
| 131 | + |
| 132 | + return spec.TypeMarkers.Any(typeName => ResolveType(typeName) != null); |
| 133 | + } |
| 134 | + |
| 135 | + private readonly record struct ProbeSpec( |
| 136 | + string FrameworkId, |
| 137 | + IReadOnlyList<string> TypeMarkers); |
| 138 | + } |
| 139 | +} |
0 commit comments