11using System ;
2- using System . Collections . Concurrent ;
32using System . Reflection ;
43using HarmonyLib ;
4+ using RimMind . Application . Features . Bridges ;
55using RimMind . Domain . ValueObjects ;
6- using Verse ;
76
87namespace 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