@@ -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