|
| 1 | +using MegaCrit.Sts2.Core.DevConsole; |
| 2 | +using MegaCrit.Sts2.Core.DevConsole.ConsoleCommands; |
| 3 | +using STS2RitsuLib.CardPiles; |
| 4 | +using STS2RitsuLib.CardTags; |
| 5 | +using STS2RitsuLib.Content; |
| 6 | +using STS2RitsuLib.Keywords; |
| 7 | +using STS2RitsuLib.Patching.Models; |
| 8 | +using STS2RitsuLib.TopBar; |
| 9 | + |
| 10 | +namespace STS2RitsuLib.Diagnostics.Patches |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Enables shorthand autocomplete only for IDs owned by ritsulib-registered content. |
| 14 | + /// </summary> |
| 15 | + public sealed class DevConsoleAutocompleteQualifiedIdPatch : IPatchMethod |
| 16 | + { |
| 17 | + private static readonly Lazy<OwnedIdCatalog> OwnedCatalog = new(BuildOwnedIdCatalog); |
| 18 | + |
| 19 | + /// <inheritdoc /> |
| 20 | + public static string PatchId => "dev_console_autocomplete_qualified_id"; |
| 21 | + |
| 22 | + /// <inheritdoc /> |
| 23 | + public static bool IsCritical => false; |
| 24 | + |
| 25 | + /// <inheritdoc /> |
| 26 | + public static string Description => |
| 27 | + "DevConsole autocomplete: shorthand match for ritsulib-owned ids only"; |
| 28 | + |
| 29 | + /// <inheritdoc /> |
| 30 | + public static ModPatchTarget[] GetTargets() |
| 31 | + { |
| 32 | + return [new(typeof(AbstractConsoleCmd), nameof(AbstractConsoleCmd.CompleteArgument), true)]; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Installs a matcher only when no other patch already provided one. |
| 37 | + /// </summary> |
| 38 | + public static void Prefix(ref Func<string, string, bool>? matchPredicate) |
| 39 | + { |
| 40 | + if (matchPredicate != null) |
| 41 | + return; |
| 42 | + |
| 43 | + matchPredicate = TailAwareOwnedIdMatch; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// De-duplicates completion candidates while preserving existing order from upstream patches. |
| 48 | + /// </summary> |
| 49 | + // ReSharper disable once InconsistentNaming |
| 50 | + public static void Postfix(ref CompletionResult __result, string partialArg) |
| 51 | + { |
| 52 | + __result.Candidates = __result.Candidates |
| 53 | + .Distinct(StringComparer.OrdinalIgnoreCase) |
| 54 | + .ToList(); |
| 55 | + } |
| 56 | + |
| 57 | + private static bool TailAwareOwnedIdMatch(string candidate, string partial) |
| 58 | + { |
| 59 | + if (string.IsNullOrWhiteSpace(partial)) |
| 60 | + return true; |
| 61 | + |
| 62 | + var token = partial.Trim(); |
| 63 | + if (candidate.StartsWith(token, StringComparison.OrdinalIgnoreCase)) |
| 64 | + return true; |
| 65 | + |
| 66 | + if (!TryGetOwnedTail(candidate, out var tail)) |
| 67 | + return false; |
| 68 | + |
| 69 | + return tail.StartsWith(token, StringComparison.OrdinalIgnoreCase) || |
| 70 | + tail.Contains(token, StringComparison.OrdinalIgnoreCase); |
| 71 | + } |
| 72 | + |
| 73 | + private static bool TryGetOwnedTail(string candidate, out string tail) |
| 74 | + { |
| 75 | + tail = string.Empty; |
| 76 | + if (string.IsNullOrWhiteSpace(candidate)) |
| 77 | + return false; |
| 78 | + |
| 79 | + var ownership = OwnedCatalog.Value.IdOwnerMap; |
| 80 | + if (!ownership.TryGetValue(candidate.Trim(), out var ownerModId)) |
| 81 | + return false; |
| 82 | + |
| 83 | + var modPrefix = GetNormalizedModStem(ownerModId) + "_"; |
| 84 | + if (!candidate.StartsWith(modPrefix, StringComparison.OrdinalIgnoreCase)) |
| 85 | + return false; |
| 86 | + |
| 87 | + if (candidate.Length <= modPrefix.Length) |
| 88 | + return false; |
| 89 | + |
| 90 | + tail = candidate[modPrefix.Length..]; |
| 91 | + return tail.Contains('_'); |
| 92 | + } |
| 93 | + |
| 94 | + private static OwnedIdCatalog BuildOwnedIdCatalog() |
| 95 | + { |
| 96 | + var owned = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 97 | + |
| 98 | + foreach (var s in ModContentRegistry.GetRegisteredTypeSnapshots()) |
| 99 | + { |
| 100 | + if (s.ModelDbId != null) |
| 101 | + TryAddOwned(owned, s.ModelDbId.Entry, s.ModId); |
| 102 | + |
| 103 | + if (!string.IsNullOrWhiteSpace(s.ExpectedPublicEntry)) |
| 104 | + TryAddOwned(owned, s.ExpectedPublicEntry, s.ModId); |
| 105 | + } |
| 106 | + |
| 107 | + foreach (var d in ModKeywordRegistry.GetDefinitionsSnapshot()) |
| 108 | + TryAddOwned(owned, d.Id, d.ModId); |
| 109 | + |
| 110 | + foreach (var d in ModCardTagRegistry.GetDefinitionsSnapshot()) |
| 111 | + TryAddOwned(owned, d.Id, d.ModId); |
| 112 | + |
| 113 | + foreach (var d in ModCardPileRegistry.GetDefinitionsSnapshot()) |
| 114 | + TryAddOwned(owned, d.Id, d.ModId); |
| 115 | + |
| 116 | + foreach (var d in ModTopBarButtonRegistry.GetDefinitionsSnapshot()) |
| 117 | + TryAddOwned(owned, d.Id, d.ModId); |
| 118 | + |
| 119 | + return new(owned); |
| 120 | + } |
| 121 | + |
| 122 | + private static void TryAddOwned(Dictionary<string, string> owned, string? id, string? modId) |
| 123 | + { |
| 124 | + if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(modId)) |
| 125 | + return; |
| 126 | + |
| 127 | + var normalizedId = id.Trim(); |
| 128 | + owned.TryAdd(normalizedId, modId); |
| 129 | + } |
| 130 | + |
| 131 | + private static string GetNormalizedModStem(string modId) |
| 132 | + { |
| 133 | + return ModContentRegistry.NormalizePublicStem(modId); |
| 134 | + } |
| 135 | + |
| 136 | + private sealed record OwnedIdCatalog(Dictionary<string, string> IdOwnerMap); |
| 137 | + } |
| 138 | +} |
0 commit comments