Skip to content

Commit 6d28bca

Browse files
authored
chore(bridge-rimchat): sync dual 1.6/1.7 LoadFolders and bilingual README (#19)
feat: add RimWorld 1.7 support and harden RimChat context bridge - Build for both 1.6 (net48) and 1.7 (net10.0) with per-version Harmony refs - Add LoadFolders.xml and release workflow that builds Core and mod for both versions - Marshal RimChat/Verse reads through RimMindAPI.MainThreadSnapshot on the main thread - Cache reflection metadata in RimChatApiShim with warn-once and fail-closed disabling - Cache RimChat detection for 6000 ticks with invalidation on tick rollback - Restrict RPG pawn lookup to active spawned colonists and gate incident listener on settings
1 parent e991154 commit 6d28bca

15 files changed

Lines changed: 334 additions & 75 deletions

‎.github/workflows/release.yml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ jobs:
2121
with:
2222
fetch-depth: 0
2323

24+
- name: Setup .NET
25+
uses: actions/setup-dotnet@v6
26+
with:
27+
dotnet-version: '10.0.x'
28+
29+
- name: Build Core Dependency
30+
run: |
31+
git clone --depth 1 https://github.com/RimWorld-RimMind-Mod/RimWorld-RimMind-Mod-Core.git ../RimMind-Core
32+
dotnet build ../RimMind-Core/Source/RimMindCore.csproj --configuration Release -p:GameVersion=1.6
33+
dotnet build ../RimMind-Core/Source/RimMindCore.csproj --configuration Release -p:GameVersion=1.7
34+
35+
- name: Build Mod (1.6 & 1.7)
36+
run: |
37+
dotnet build Source/RimMindBridgeRimChat.csproj --configuration Release -p:GameVersion=1.6
38+
dotnet build Source/RimMindBridgeRimChat.csproj --configuration Release -p:GameVersion=1.7
39+
for ver in 1.6 1.7; do
40+
mkdir -p "$ver/Assemblies"
41+
cp -f "../RimMind-Core/$ver/Assemblies/"{0_RimMindDomain.dll,1_RimMindApplication.dll,2_RimMindCore.dll,Newtonsoft.Json.dll} "$ver/Assemblies/"
42+
done
43+
2444
- name: Determine Git Range
2545
id: git-range
2646
run: |

‎AGENTS.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ RimMind 与 RimChat 的对话/动作门控、上下文拉取桥。仅编译依
1111
| 对话、玩家菜单互斥 | `Source/Bridge/DialogueGate.cs` |
1212
| 动作分类、叙事者事件门控 | `Source/Bridge/ActionGate.cs` |
1313
| 当前游戏事件冷却 | `Source/Cooldown/SharedIncidentCooldown.cs` → `GameComponent_BridgeRimChat.cs` |
14-
| RimChat 历史 → Core Context | `Source/Bridge/ContextPullBridge.cs` → `RimChatApiShim.cs` |
15-
| RimChat 启用检测 | `Source/Detection/RimChatDetector.cs` |
14+
| RimChat 历史 → Core Context | `Source/Bridge/ContextPullBridge.cs`(`RimMindAPI.MainThreadSnapshot.GetAsync` 主线程快照 + 结构失败自动停用) → `RimChatApiShim.cs` |
15+
| RimChat 启用检测 | `Source/Detection/RimChatDetector.cs`(6000-tick TTL 缓存 + 跨存档/回退 tick 自动刷新 + `InvalidateCache`) |
1616
| 设置值与存档 | `Source/Settings/BridgeRimChatSettings.cs` |
1717

1818
## 门控规则
@@ -33,8 +33,8 @@ RimMind 与 RimChat 的对话/动作门控、上下文拉取桥。仅编译依
3333
`SharedIncidentCooldown` 只定位 `Current.Game` 的组件,不持有跨游戏状态。
3434
- 存档键保留 `RimMind_BridgeRimChat_LastIncidentTick`;组件由 Verse 发现 `(Game game)` 构造。
3535
- 新游戏不继承旧游戏冷却;无游戏时冷却不生效。新增 per-game 状态必须由游戏组件拥有。
36-
- 对 RimChat 的反射统一走 `RimChatApiShim`,异常隔离;类型延迟解析并保留 NoInlining。
37-
manager 获取复用 `TryGetManagerInstance`。
36+
- `RimChatDetector.IsRimChatActive` 采用 6000-tick TTL 缓存(`CacheIntervalTicks = 6000`),当游戏 tick 回退(读档/新局 `now < _cacheTick`)或超过 6000 tick 时重新探测 `ModsConfig.IsActive("yancy.rimchat")`,支持 `InvalidateCache()` 强制失效。
37+
- 对 RimChat 的反射统一走 `RimChatApiShim`,异常隔离;类型延迟解析并保留 `NoInlining`,属性与字段元数据通过 `GetCachedProperty` / `GetCachedField`(`ConcurrentDictionary`)缓存,缺失成员通过 `WarnOnce`(`_warnedMissingMembers`)去重告警,manager 获取复用 `TryGetManagerInstance`。
3838
- 所有扩展和 Context Provider 的 owner 为 `RimMindBridgeRimChat`,不要混用带点号名称。
3939
- 设置变化由已注册委托实时读取,不重复注册 SkipCheck。
4040

@@ -47,8 +47,9 @@ RimMind 与 RimChat 的对话/动作门控、上下文拉取桥。仅编译依
4747
| `rimchat_diplomacy` | 外交会话,world-level | `pullDiplomacyHistory` |
4848
| `rimchat_rpg_history` | Pawn RPG 历史 | `pullRpgHistory` |
4949

50-
Pawn 解析复用 `TryFindPawnById`(委托 `RimMindPawnLookup.FindPawnByNumber`),覆盖所有地图与远行队中的存活殖民者,禁止无界扫描 `WorldPawns.AllPawnsAlive`。
51-
Context、门控与事件通知均经过 Core 公共边界。
50+
- **主线程快照与结构熔断**:`rimchat_diplomacy` 与 `rimchat_rpg_history` 均通过 `RimMindAPI.MainThreadSnapshot.GetAsync` 将 Verse/RimChat 状态读取封送到主线程执行,后台线程绝不直接访问 Verse 或 RimChat 集合;反射读取统一复用 `RimChatApiShim.GetCachedField`,一旦发现必需字段缺失,由 `RimChatApiShim` 触发单次告警(warn-once)并将 `_diplomacyDisabled` / `_rpgDisabled` 置位停用对应拉取(`Register()` / `Unregister()` 时重置)。
51+
- **Pawn 存活校验**:Pawn 解析复用 `TryFindPawnById`(委托 `RimMindPawnLookup.FindPawnByNumber` + `RimMindPawnLookup.IsActiveSpawnedColonist`),禁止无界扫描 `WorldPawns.AllPawnsAlive`。
52+
- Context、门控与事件通知均经过 Core 公共边界。
5253

5354
## 验证
5455

‎About/About.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<modVersion>2.0.0</modVersion>
77
<supportedVersions>
88
<li>1.6</li>
9+
<li>1.7</li>
910
</supportedVersions>
1011
<description>Coordination bridge between RimMind suite and RimChat. Handles dialogue/action dedup and context exposure. [v2.0 BREAKING CHANGES]</description>
1112
<modDependencies>

‎LoadFolders.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<loadFolders>
3+
<v1.6>
4+
<li>/</li>
5+
<li>1.6</li>
6+
</v1.6>
7+
<v1.7>
8+
<li>/</li>
9+
<li>1.7</li>
10+
</v1.7>
11+
</loadFolders>

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div align="center">
22

33
# RimMind-Bridge-RimChat 🌉
4-
### Seamless Compatibility Bridge & Mutual Exclusion Gating for RimWorld 1.6
4+
### Seamless Compatibility Bridge & Mutual Exclusion Gating for RimWorld 1.6 & 1.7
55

66
**English** | [简体中文](README_zh.md)
77

88
<p>
9-
<a href="https://rimworldgame.com/"><img src="https://img.shields.io/badge/RimWorld-1.6-brightgreen.svg" alt="RimWorld 1.6"></a>
10-
<a href="https://github.com/mcocdaa/RimWorld-RimMind-Mod-Core"><img src="https://img.shields.io/badge/Dependency-RimMind--Core-blue.svg" alt="Dependency: RimMind-Core"></a>
9+
<a href="https://rimworldgame.com/"><img src="https://img.shields.io/badge/RimWorld-1.6%20%7C%201.7-brightgreen.svg" alt="RimWorld 1.6 | 1.7"></a>
10+
<a href="https://github.com/RimWorld-RimMind-Mod/RimWorld-RimMind-Mod-Core"><img src="https://img.shields.io/badge/Dependency-RimMind--Core-blue.svg" alt="Dependency: RimMind-Core"></a>
1111
<a href="#"><img src="https://img.shields.io/badge/Unit%20Tests-10%2B%20Passing-success.svg" alt="Unit Tests"></a>
1212
<a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"></a>
1313
</p>

‎README_EN.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎README_zh.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div align="center">
22

33
# RimMind-Bridge-RimChat 🌉
4-
### 专为 RimWorld 1.6 打造的 RimChat 模组兼容桥、门控互斥与上下文同步系统
4+
### 专为 RimWorld 1.6 & 1.7 打造的 RimChat 模组兼容桥、门控互斥与上下文同步系统
55

66
[English](README.md) | **简体中文**
77

88
<p>
9-
<a href="https://rimworldgame.com/"><img src="https://img.shields.io/badge/RimWorld-1.6-brightgreen.svg" alt="RimWorld 1.6"></a>
10-
<a href="https://github.com/mcocdaa/RimWorld-RimMind-Mod-Core"><img src="https://img.shields.io/badge/核心依赖-RimMind--Core-blue.svg" alt="依赖: RimMind-Core"></a>
9+
<a href="https://rimworldgame.com/"><img src="https://img.shields.io/badge/RimWorld-1.6%20%7C%201.7-brightgreen.svg" alt="RimWorld 1.6 | 1.7"></a>
10+
<a href="https://github.com/RimWorld-RimMind-Mod/RimWorld-RimMind-Mod-Core"><img src="https://img.shields.io/badge/核心依赖-RimMind--Core-blue.svg" alt="依赖: RimMind-Core"></a>
1111
<a href="#"><img src="https://img.shields.io/badge/单元测试-10%2B%20通过-success.svg" alt="单元测试"></a>
1212
<a href="LICENSE"><img src="https://img.shields.io/badge/开源协议-MIT-yellow.svg" alt="License: MIT"></a>
1313
</p>

‎Source/Bridge/ContextPullBridge.cs‎

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ namespace RimMind.Bridge.RimChat.Bridge
1717
public static class ContextPullBridge
1818
{
1919
private const string ModId = "RimMindBridgeRimChat";
20+
private static bool _diplomacyDisabled;
21+
private static bool _rpgDisabled;
2022

2123
public static void Register()
2224
{
25+
_diplomacyDisabled = false;
26+
_rpgDisabled = false;
27+
2328
if (!RimChatDetector.IsRimChatActive) return;
2429

2530
var settings = BridgeRimChatSettings.Get();
@@ -38,13 +43,17 @@ private static void RegisterDiplomacyProvider()
3843
"rimchat_diplomacy", ContextLayer.L2_Environment, 0.4f,
3944
async (ctx, ct) =>
4045
{
41-
return BuildDiplomacyContext();
46+
if (!RimChatDetector.IsRimChatActive || _diplomacyDisabled) return null;
47+
return await RimMindAPI.MainThreadSnapshot.GetAsync<string?>(
48+
() => BuildDiplomacyContext(),
49+
null,
50+
ct: ct).ConfigureAwait(false);
4251
}, ModId, stalenessTicks: 1500, invalidationTriggers: new[] { "RimChatEvent" }));
4352
}
4453

4554
private static string? BuildDiplomacyContext()
4655
{
47-
if (!RimChatDetector.IsRimChatActive) return null;
56+
if (!RimChatDetector.IsRimChatActive || _diplomacyDisabled) return null;
4857

4958
try
5059
{
@@ -54,18 +63,27 @@ private static void RegisterDiplomacyProvider()
5463
var sessions = RimChatApiShim.GetInstanceFieldValue(manager, "dialogueSessions") as IList;
5564
if (sessions == null || sessions.Count == 0) return null;
5665

57-
var sb = new StringBuilder("[RimChat Diplomacy]");
66+
var sb = new StringBuilder();
67+
sb.AppendLine("[RimChat Diplomacy]");
5868
int sessionCount = 0;
59-
foreach (var session in sessions)
69+
int startSessionIdx = System.Math.Max(0, sessions.Count - 3);
70+
for (int si = startSessionIdx; si < sessions.Count; si++)
6071
{
6172
if (sessionCount >= 3) break;
73+
var session = sessions[si];
74+
if (session == null) continue;
6275

63-
var factionField = session.GetType().GetField("faction",
76+
var sessionType = session.GetType();
77+
var factionField = RimChatApiShim.GetCachedField(sessionType, "faction",
6478
BindingFlags.Public | BindingFlags.Instance);
65-
var messagesField = session.GetType().GetField("messages",
79+
var messagesField = RimChatApiShim.GetCachedField(sessionType, "messages",
6680
BindingFlags.Public | BindingFlags.Instance);
6781

68-
if (factionField == null || messagesField == null) continue;
82+
if (factionField == null || messagesField == null)
83+
{
84+
_diplomacyDisabled = true;
85+
return null;
86+
}
6987

7088
var faction = factionField.GetValue(session);
7189
var messages = messagesField.GetValue(session) as IList;
@@ -81,14 +99,19 @@ private static void RegisterDiplomacyProvider()
8199
if (msgCount >= 4) break;
82100
var msg = messages[i];
83101
if (msg == null) continue;
84-
var senderField = msg.GetType().GetField("sender",
102+
var msgType = msg.GetType();
103+
var senderField = RimChatApiShim.GetCachedField(msgType, "sender",
85104
BindingFlags.Public | BindingFlags.Instance);
86-
var messageField = msg.GetType().GetField("message",
105+
var messageField = RimChatApiShim.GetCachedField(msgType, "message",
87106
BindingFlags.Public | BindingFlags.Instance);
88-
var isPlayerField = msg.GetType().GetField("isPlayer",
107+
var isPlayerField = RimChatApiShim.GetCachedField(msgType, "isPlayer",
89108
BindingFlags.Public | BindingFlags.Instance);
90109

91-
if (senderField == null || messageField == null) continue;
110+
if (senderField == null || messageField == null)
111+
{
112+
_diplomacyDisabled = true;
113+
return null;
114+
}
92115

93116
string sender = senderField.GetValue(msg)?.ToString() ?? "?";
94117
string content = messageField.GetValue(msg)?.ToString() ?? "";
@@ -116,21 +139,25 @@ private static void RegisterRpgProvider()
116139
"rimchat_rpg_history", ContextLayer.L4_History, 0.5f,
117140
async (ctx, ct) =>
118141
{
119-
if (ctx.PawnId <= 0) return null;
120-
var pawn = TryFindPawnById(ctx.PawnId);
121-
if (pawn == null) return null;
122-
return BuildRpgContext(pawn);
142+
if (ctx.PawnId <= 0 || !RimChatDetector.IsRimChatActive || _rpgDisabled) return null;
143+
return await RimMindAPI.MainThreadSnapshot.GetAsync<string?>(() =>
144+
{
145+
var pawn = TryFindPawnById(ctx.PawnId);
146+
if (pawn == null) return null;
147+
return BuildRpgContext(pawn);
148+
}, null, ct: ct).ConfigureAwait(false);
123149
}, ModId, stalenessTicks: 3000, invalidationTriggers: new[] { "RimChatEvent" }));
124150
}
125151

126152
internal static Pawn? TryFindPawnById(int pawnId)
127153
{
128-
return RimMindPawnLookup.FindPawnByNumber(pawnId);
154+
var pawn = RimMindPawnLookup.FindPawnByNumber(pawnId);
155+
return RimMindPawnLookup.IsActiveSpawnedColonist(pawn) ? pawn : null;
129156
}
130157

131158
private static string? BuildRpgContext(Pawn pawn)
132159
{
133-
if (!RimChatDetector.IsRimChatActive) return null;
160+
if (!RimChatDetector.IsRimChatActive || _rpgDisabled) return null;
134161

135162
try
136163
{
@@ -147,42 +174,54 @@ private static void RegisterRpgProvider()
147174
var archiveCandidate = entry.Value;
148175
if (archiveCandidate == null) continue;
149176

150-
var pawnLoadIdField = archiveCandidate.GetType().GetField("PawnLoadId",
177+
var pawnLoadIdField = RimChatApiShim.GetCachedField(archiveCandidate.GetType(), "PawnLoadId",
151178
BindingFlags.Public | BindingFlags.Instance);
152-
if (pawnLoadIdField != null)
179+
if (pawnLoadIdField == null)
153180
{
154-
var archivePawnIdValue = pawnLoadIdField.GetValue(archiveCandidate);
155-
if (archivePawnIdValue == null) continue;
181+
_rpgDisabled = true;
182+
return null;
183+
}
156184

157-
int archivePawnId = System.Convert.ToInt32(archivePawnIdValue);
158-
if (archivePawnId == pawn.thingIDNumber)
159-
{
160-
archive = archiveCandidate;
161-
break;
162-
}
185+
var archivePawnIdValue = pawnLoadIdField.GetValue(archiveCandidate);
186+
if (archivePawnIdValue == null) continue;
187+
188+
int archivePawnId = System.Convert.ToInt32(archivePawnIdValue);
189+
if (archivePawnId == pawn.thingIDNumber)
190+
{
191+
archive = archiveCandidate;
192+
break;
163193
}
164194
}
165195

166196
if (archive == null) return null;
167197

168-
var sessionsField = archive.GetType().GetField("Sessions",
198+
var sessionsField = RimChatApiShim.GetCachedField(archive.GetType(), "Sessions",
169199
BindingFlags.Public | BindingFlags.Instance);
170-
if (sessionsField == null) return null;
200+
if (sessionsField == null)
201+
{
202+
_rpgDisabled = true;
203+
return null;
204+
}
171205

172206
var sessions = sessionsField.GetValue(archive) as IList;
173207
if (sessions == null || sessions.Count == 0) return null;
174208

175-
var sb = new StringBuilder("[RimChat RPG History]");
209+
var sb = new StringBuilder();
210+
sb.AppendLine("[RimChat RPG History]");
176211
int sessionCount = 0;
177212
for (int si = sessions.Count - 1; si >= 0; si--)
178213
{
179214
if (sessionCount >= 2) break;
180215

181216
var session = sessions[si];
182217
if (session == null) continue;
183-
var turnsField = session.GetType().GetField("Turns",
218+
var turnsField = RimChatApiShim.GetCachedField(session.GetType(), "Turns",
184219
BindingFlags.Public | BindingFlags.Instance);
185-
if (turnsField == null) continue;
220+
if (turnsField == null)
221+
{
222+
_rpgDisabled = true;
223+
return null;
224+
}
186225

187226
var turns = turnsField.GetValue(session) as IList;
188227
if (turns == null || turns.Count == 0) continue;
@@ -195,14 +234,19 @@ private static void RegisterRpgProvider()
195234
var turn = turns[ti];
196235
if (turn == null) continue;
197236

198-
var isPlayerField = turn.GetType().GetField("IsPlayer",
237+
var turnType = turn.GetType();
238+
var isPlayerField = RimChatApiShim.GetCachedField(turnType, "IsPlayer",
199239
BindingFlags.Public | BindingFlags.Instance);
200-
var speakerField = turn.GetType().GetField("SpeakerName",
240+
var speakerField = RimChatApiShim.GetCachedField(turnType, "SpeakerName",
201241
BindingFlags.Public | BindingFlags.Instance);
202-
var textField = turn.GetType().GetField("Text",
242+
var textField = RimChatApiShim.GetCachedField(turnType, "Text",
203243
BindingFlags.Public | BindingFlags.Instance);
204244

205-
if (textField == null) continue;
245+
if (textField == null)
246+
{
247+
_rpgDisabled = true;
248+
return null;
249+
}
206250

207251
bool isPlayer = isPlayerField?.GetValue(turn) is bool playerTurn && playerTurn;
208252
string speaker = speakerField?.GetValue(turn)?.ToString() ?? "?";
@@ -232,6 +276,8 @@ private static string Truncate(string s, int maxLen)
232276

233277
public static void Unregister()
234278
{
279+
_diplomacyDisabled = false;
280+
_rpgDisabled = false;
235281
RimMindAPI.Context.ContextKeys.Unregister("rimchat_diplomacy");
236282
RimMindAPI.Context.ContextKeys.Unregister("rimchat_rpg_history");
237283
}

0 commit comments

Comments
 (0)