Skip to content

Commit a657c08

Browse files
committed
feat(bridge-rimchat): fix ActionGate return check, migrate onto Core Bridge Kit, add CI test workflow
refactor: adopt shared core helpers for reflection, detection, and skip checks Replace duplicated reflection and soft-dependency logic with shared ReflectionAccessor and SoftDependencyDetector from RimMind-Core, and switch skip checks to IChannelSkipCheck with open string channels. - ActionGate: case-insensitive matching plus prefix-based classification for romance, social, diplomacy, and recruit intents to cover real mechanism and tool prefixes - DialogueGate: use shared DialogueTriggerKinds constants - ContextPullBridge: drop RimChatEvent invalidation triggers since RimChat state changes are external; rely on staleness only - Add unit test workflow and contract tests for new gate behavior
1 parent 6d28bca commit a657c08

13 files changed

Lines changed: 242 additions & 149 deletions

‎.github/workflows/test.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
paths:
7+
- 'Source/**'
8+
- 'Tests/**'
9+
- 'ArchTests/**'
10+
- '.github/workflows/test.yml'
11+
pull_request:
12+
branches: [main, master]
13+
paths:
14+
- 'Source/**'
15+
- 'Tests/**'
16+
- 'ArchTests/**'
17+
- '.github/workflows/test.yml'
18+
19+
jobs:
20+
test:
21+
uses: RimWorld-RimMind-Mod/Workflows/.github/workflows/test.yml@main
22+
secrets: inherit

‎Source/Bridge/ActionGate.cs‎

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using RimMind.Bridge.RimChat.Cooldown;
34
using RimMind.Bridge.RimChat.Detection;
@@ -8,44 +9,120 @@ namespace RimMind.Bridge.RimChat.Bridge
89
{
910
public static class ActionGate
1011
{
11-
private static readonly HashSet<string> DiplomacyActions = new HashSet<string>
12+
private static readonly HashSet<string> DiplomacyActions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
1213
{
1314
"adjust_faction",
1415
"trigger_incident",
16+
"world.faction",
17+
"diplomacy",
18+
"world.storyteller",
19+
"storyteller.incident",
1520
};
1621

17-
private static readonly HashSet<string> SocialActions = new HashSet<string>
22+
private static readonly HashSet<string> SocialActions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
1823
{
1924
"romance_attempt",
2025
"romance_breakup",
26+
"pawn.interaction",
27+
"social_relax",
28+
"give_item",
29+
"actions_social_interact",
30+
"social_interact",
2131
};
2232

23-
private static readonly HashSet<string> RecruitActions = new HashSet<string>
33+
private static readonly HashSet<string> RecruitActions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
2434
{
2535
"recruit_agree",
36+
"recruit",
37+
"actions_recruit",
38+
"pawn.recruit",
39+
"prisoner.recruit",
2640
};
2741

2842
public static bool ShouldSkipAction(string? intentId)
2943
{
30-
if (intentId is null || intentId.Length == 0) return false;
44+
if (string.IsNullOrWhiteSpace(intentId)) return false;
3145
if (!RimChatDetector.IsRimChatActive) return false;
3246

3347
var settings = BridgeRimChatSettings.Get();
3448
if (!settings.enableActionGate) return false;
3549
if (settings.forceRimMindActions) return false;
3650

37-
if (settings.skipDiplomacyActions && DiplomacyActions.Contains(intentId))
51+
if (settings.skipDiplomacyActions && IsDiplomacyAction(intentId))
3852
return true;
3953

40-
if (settings.skipSocialActions && SocialActions.Contains(intentId))
54+
if (settings.skipSocialActions && IsSocialAction(intentId))
4155
return true;
4256

43-
if (settings.skipRecruitAgree && RecruitActions.Contains(intentId))
57+
if (settings.skipRecruitAgree && IsRecruitAction(intentId))
4458
return true;
4559

4660
return false;
4761
}
4862

63+
public static bool IsRomanceAction(string? intentId)
64+
{
65+
if (string.IsNullOrWhiteSpace(intentId)) return false;
66+
string normalized = intentId!.Trim();
67+
if (normalized.StartsWith("romance_", StringComparison.OrdinalIgnoreCase))
68+
return true;
69+
if (MatchesPrefix(normalized, "pawn.interaction")
70+
&& (normalized.IndexOf("romance", StringComparison.OrdinalIgnoreCase) >= 0
71+
|| normalized.EndsWith(".romance_attempt", StringComparison.OrdinalIgnoreCase)
72+
|| normalized.EndsWith(".romance_breakup", StringComparison.OrdinalIgnoreCase)))
73+
{
74+
return true;
75+
}
76+
return normalized.IndexOf("romance", StringComparison.OrdinalIgnoreCase) >= 0;
77+
}
78+
79+
public static bool IsSocialAction(string? intentId)
80+
{
81+
if (string.IsNullOrWhiteSpace(intentId)) return false;
82+
string normalized = intentId!.Trim();
83+
if (IsRomanceAction(normalized)) return true;
84+
if (SocialActions.Contains(normalized)) return true;
85+
return MatchesPrefix(normalized, "pawn.interaction")
86+
|| normalized.StartsWith("pawn_interaction", StringComparison.OrdinalIgnoreCase)
87+
|| normalized.StartsWith("social.", StringComparison.OrdinalIgnoreCase);
88+
}
89+
90+
public static bool IsDiplomacyAction(string? intentId)
91+
{
92+
if (string.IsNullOrWhiteSpace(intentId)) return false;
93+
string normalized = intentId!.Trim();
94+
if (DiplomacyActions.Contains(normalized)) return true;
95+
return MatchesPrefix(normalized, "world.faction")
96+
|| normalized.StartsWith("faction.", StringComparison.OrdinalIgnoreCase)
97+
|| normalized.StartsWith("faction_", StringComparison.OrdinalIgnoreCase)
98+
|| MatchesPrefix(normalized, "diplomacy")
99+
|| normalized.IndexOf("diplomacy", StringComparison.OrdinalIgnoreCase) >= 0
100+
|| MatchesPrefix(normalized, "world.storyteller")
101+
|| MatchesPrefix(normalized, "storyteller.incident")
102+
|| normalized.StartsWith("incident.", StringComparison.OrdinalIgnoreCase);
103+
}
104+
105+
public static bool IsRecruitAction(string? intentId)
106+
{
107+
if (string.IsNullOrWhiteSpace(intentId)) return false;
108+
string normalized = intentId!.Trim();
109+
if (RecruitActions.Contains(normalized)) return true;
110+
return MatchesPrefix(normalized, "recruit")
111+
|| normalized.StartsWith("recruit_", StringComparison.OrdinalIgnoreCase)
112+
|| MatchesPrefix(normalized, "pawn.recruit")
113+
|| MatchesPrefix(normalized, "prisoner.recruit")
114+
|| normalized.IndexOf("recruit", StringComparison.OrdinalIgnoreCase) >= 0;
115+
}
116+
117+
private static bool MatchesPrefix(string value, string prefix)
118+
{
119+
if (string.Equals(value, prefix, StringComparison.OrdinalIgnoreCase))
120+
return true;
121+
return value.Length > prefix.Length
122+
&& value.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
123+
&& (value[prefix.Length] == '.' || value[prefix.Length] == ':' || value[prefix.Length] == '_');
124+
}
125+
49126
public static bool ShouldSkipStorytellerIncident()
50127
{
51128
if (!RimChatDetector.IsRimChatActive) return false;

‎Source/Bridge/ContextPullBridge.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static void RegisterDiplomacyProvider()
4848
() => BuildDiplomacyContext(),
4949
null,
5050
ct: ct).ConfigureAwait(false);
51-
}, ModId, stalenessTicks: 1500, invalidationTriggers: new[] { "RimChatEvent" }));
51+
}, ModId, stalenessTicks: 1500)); // RimChat state changes are external (no bus event): staleness only
5252
}
5353

5454
private static string? BuildDiplomacyContext()
@@ -146,7 +146,7 @@ private static void RegisterRpgProvider()
146146
if (pawn == null) return null;
147147
return BuildRpgContext(pawn);
148148
}, null, ct: ct).ConfigureAwait(false);
149-
}, ModId, stalenessTicks: 3000, invalidationTriggers: new[] { "RimChatEvent" }));
149+
}, ModId, stalenessTicks: 3000)); // RimChat state changes are external (no bus event): staleness only
150150
}
151151

152152
internal static Pawn? TryFindPawnById(int pawnId)

‎Source/Bridge/DialogueGate.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using RimMind.Application.Common.Constants;
12
using RimMind.Bridge.RimChat.Detection;
23
using RimMind.Bridge.RimChat.Settings;
34
using Verse;
@@ -14,9 +15,9 @@ public static bool ShouldSkipDialogue(Pawn? pawn, string? triggerType)
1415

1516
return triggerType switch
1617
{
17-
"Chitchat" => settings.enableChitchatGate,
18-
"Auto" => settings.enableAutoGate,
19-
"PlayerInput" => ShouldSkipPlayerInput(settings),
18+
DialogueTriggerKinds.Chitchat => settings.enableChitchatGate,
19+
DialogueTriggerKinds.Auto => settings.enableAutoGate,
20+
DialogueTriggerKinds.PlayerInput => ShouldSkipPlayerInput(settings),
2021
_ => false
2122
};
2223
}

‎Source/Bridge/RimChatApiShim.cs‎

Lines changed: 24 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Reflection;
43
using HarmonyLib;
4+
using RimMind.Application.Features.Bridges;
55
using RimMind.Domain.ValueObjects;
6-
using Verse;
76

87
namespace RimMind.Bridge.RimChat.Bridge
98
{
@@ -13,40 +12,26 @@ public static class RimChatApiShim
1312
private const string DiplomacyManagerTypeName = "RimChat.DiplomacySystem.GameComponent_DiplomacyManager";
1413
private const string RpgArchiveManagerTypeName = "RimChat.Memory.RpgNpcDialogueArchiveManager";
1514

15+
internal static readonly ReflectionAccessor Accessor = new ReflectionAccessor(
16+
typeName => AccessTools.TypeByName(typeName),
17+
msg => RimMindErrors.Warn(msg),
18+
"[RimMind-Bridge-RimChat]");
19+
1620
private static Type? _apiType;
1721
private static Type? _diplomacyManagerType;
1822
private static Type? _rpgArchiveManagerType;
1923
private static bool _resolved;
2024

21-
private static readonly ConcurrentDictionary<(Type Type, string Name, BindingFlags Flags), PropertyInfo?> _propertyCache =
22-
new ConcurrentDictionary<(Type Type, string Name, BindingFlags Flags), PropertyInfo?>();
23-
24-
private static readonly ConcurrentDictionary<(Type Type, string Name, BindingFlags Flags), FieldInfo?> _fieldCache =
25-
new ConcurrentDictionary<(Type Type, string Name, BindingFlags Flags), FieldInfo?>();
26-
27-
private static readonly ConcurrentDictionary<string, byte> _warnedMissingMembers =
28-
new ConcurrentDictionary<string, byte>(StringComparer.Ordinal);
29-
3025
internal static void ConfigureTypesForTesting(
3126
Type? apiType,
3227
Type? diplomacyManagerType,
3328
Type? rpgArchiveManagerType)
3429
{
30+
Accessor.ResetForTesting();
3531
_apiType = apiType;
3632
_diplomacyManagerType = diplomacyManagerType;
3733
_rpgArchiveManagerType = rpgArchiveManagerType;
3834
_resolved = true;
39-
_propertyCache.Clear();
40-
_fieldCache.Clear();
41-
_warnedMissingMembers.Clear();
42-
}
43-
44-
private static void WarnOnce(string key, string message)
45-
{
46-
if (_warnedMissingMembers.TryAdd(key, 1))
47-
{
48-
RimMindErrors.Warn(message);
49-
}
5035
}
5136

5237
private static void EnsureResolved()
@@ -62,9 +47,9 @@ private static void ResolveTypes()
6247
{
6348
try
6449
{
65-
_apiType = AccessTools.TypeByName(ApiTypeName);
66-
_diplomacyManagerType = AccessTools.TypeByName(DiplomacyManagerTypeName);
67-
_rpgArchiveManagerType = AccessTools.TypeByName(RpgArchiveManagerTypeName);
50+
_apiType = Accessor.GetCachedType(ApiTypeName);
51+
_diplomacyManagerType = Accessor.GetCachedType(DiplomacyManagerTypeName);
52+
_rpgArchiveManagerType = Accessor.GetCachedType(RpgArchiveManagerTypeName);
6853
}
6954
catch (Exception ex)
7055
{
@@ -87,101 +72,32 @@ public static Type? RpgArchiveManagerType
8772
get { EnsureResolved(); return _rpgArchiveManagerType; }
8873
}
8974

90-
public static PropertyInfo? GetCachedProperty(Type type, string propertyName,
75+
public static PropertyInfo? GetCachedProperty(
76+
Type type,
77+
string propertyName,
9178
BindingFlags flags = BindingFlags.Public | BindingFlags.Static)
92-
{
93-
if (type == null || string.IsNullOrEmpty(propertyName)) return null;
94-
var prop = _propertyCache.GetOrAdd(
95-
(type, propertyName, flags),
96-
static k => k.Type.GetProperty(k.Name, k.Flags));
97-
if (prop == null)
98-
{
99-
WarnOnce(
100-
$"prop:{type.FullName}:{propertyName}:{flags}",
101-
$"[RimMind-Bridge-RimChat] Property '{propertyName}' not found on {type.Name}.");
102-
}
103-
return prop;
104-
}
79+
=> Accessor.GetCachedProperty(type, propertyName, flags);
10580

106-
public static FieldInfo? GetCachedField(Type type, string fieldName,
81+
public static FieldInfo? GetCachedField(
82+
Type type,
83+
string fieldName,
10784
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
108-
{
109-
if (type == null || string.IsNullOrEmpty(fieldName)) return null;
110-
var field = _fieldCache.GetOrAdd(
111-
(type, fieldName, flags),
112-
static k => k.Type.GetField(k.Name, k.Flags));
113-
if (field == null)
114-
{
115-
WarnOnce(
116-
$"field:{type.FullName}:{fieldName}:{flags}",
117-
$"[RimMind-Bridge-RimChat] Field '{fieldName}' not found on {type.Name}.");
118-
}
119-
return field;
120-
}
85+
=> Accessor.GetCachedField(type, fieldName, flags);
12186

12287
public static object? GetStaticPropertyValue(Type type, string propertyName)
123-
{
124-
if (type == null)
125-
{
126-
RimMindErrors.Warn("[RimMind-Bridge-RimChat] GetStaticPropertyValue: type is null.");
127-
return null;
128-
}
88+
=> Accessor.GetStaticPropertyValue(type, propertyName);
12989

130-
try
131-
{
132-
var prop = GetCachedProperty(type, propertyName, BindingFlags.Public | BindingFlags.Static);
133-
if (prop == null) return null;
134-
return prop.GetValue(null);
135-
}
136-
catch (Exception ex)
137-
{
138-
RimMindErrors.Warn($"[RimMind-Bridge-RimChat] GetStaticPropertyValue({type.Name}.{propertyName}) failed: {ex.Message}");
139-
return null;
140-
}
141-
}
142-
143-
public static object? GetInstanceFieldValue(object instance, string fieldName,
90+
public static object? GetInstanceFieldValue(
91+
object instance,
92+
string fieldName,
14493
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance)
145-
{
146-
if (instance == null)
147-
{
148-
RimMindErrors.Warn("[RimMind-Bridge-RimChat] GetInstanceFieldValue: instance is null.");
149-
return null;
150-
}
151-
152-
try
153-
{
154-
var field = GetCachedField(instance.GetType(), fieldName, flags);
155-
if (field == null) return null;
156-
return field.GetValue(instance);
157-
}
158-
catch (Exception ex)
159-
{
160-
RimMindErrors.Warn($"[RimMind-Bridge-RimChat] GetInstanceFieldValue({instance.GetType().Name}.{fieldName}) failed: {ex.Message}");
161-
return null;
162-
}
163-
}
94+
=> Accessor.GetInstanceFieldValue(instance, fieldName, flags);
16495

16596
/// <summary>
16697
/// 获取 RimChat manager 的单例实例(反射读取静态 "Instance" 属性)。
16798
/// 统一 BuildDiplomacyContext / BuildRpgContext 的 manager 获取逻辑。
16899
/// </summary>
169100
public static object? TryGetManagerInstance(Type? managerType, string instancePropertyName = "Instance")
170-
{
171-
if (managerType == null)
172-
{
173-
RimMindErrors.Warn($"[RimMind-Bridge-RimChat] TryGetManagerInstance: managerType is null.");
174-
return null;
175-
}
176-
177-
var instance = GetStaticPropertyValue(managerType, instancePropertyName);
178-
if (instance == null)
179-
{
180-
RimMindErrors.Warn($"[RimMind-Bridge-RimChat] TryGetManagerInstance: instance is null on {managerType.Name}.");
181-
return null;
182-
}
183-
184-
return instance;
185-
}
101+
=> Accessor.TryGetSingletonInstance(managerType, instancePropertyName);
186102
}
187103
}

0 commit comments

Comments
 (0)