11using Celeste . Mod . CelesteNet . Client . Components ;
2+ using Celeste . Mod . CelesteNet . DataTypes ;
23using Celeste . Mod . CollabUtils2 . UI ;
34using Celeste . Mod . UI ;
45using Mono . Cecil ;
@@ -38,7 +39,7 @@ internal static void OnInitialize() {
3839 }
3940
4041 if ( Everest . Loader . DependencyLoaded ( new EverestModuleMetadata ( ) { Name = "CelesteNet.Client" , Version = new Version ( 2 , 0 , 0 ) } ) ) {
41- adjustCollabIcon ( ) ;
42+ setupAdjustCollabIcon ( ) ;
4243 }
4344 }
4445
@@ -147,6 +148,8 @@ internal static void Load() {
147148 hookOnOuiJournalPoemLines = new ILHook ( typeof ( OuiJournalPoem ) . GetNestedType ( "PoemLine" , BindingFlags . NonPublic ) . GetMethod ( "Render" ) , modJournalPoemHeartColors ) ;
148149 hookOnOuiFileSelectSlotGolden = new ILHook ( typeof ( OuiFileSelectSlot ) . GetMethod ( "get_Golden" , BindingFlags . NonPublic | BindingFlags . Instance ) , modSelectSlotCollectedStrawberries ) ;
149150 hookOnOuiFileSelectSlotRender = new ILHook ( typeof ( OuiFileSelectSlot ) . GetMethod ( "orig_Render" ) , modOuiFileSelectSlotRender ) ;
151+
152+ On . Monocle . Commands . ProcessMethod += onCommandLookupInMethod ;
150153 }
151154
152155 internal static void Unload ( ) {
@@ -172,10 +175,36 @@ internal static void Unload() {
172175 hookOnOuiJournalPoemLines ? . Dispose ( ) ;
173176 hookOnOuiFileSelectSlotGolden ? . Dispose ( ) ;
174177 hookOnOuiFileSelectSlotRender ? . Dispose ( ) ;
178+
179+ On . Monocle . Commands . ProcessMethod -= onCommandLookupInMethod ;
180+
181+ if ( Everest . Loader . DependencyLoaded ( new EverestModuleMetadata ( ) { Name = "CelesteNet.Client" , Version = new Version ( 2 , 0 , 0 ) } ) ) {
182+ teardownAdjustCollabIcon ( ) ;
183+ }
184+ }
185+
186+ // TODO: this hook should be moved to Everest, I'm probably not the only one wanting to do optional dependencies like that
187+
188+ private static void onCommandLookupInMethod ( On . Monocle . Commands . orig_ProcessMethod orig , Monocle . Commands self , MethodInfo method ) {
189+ try {
190+ orig ( self , method ) ;
191+ } catch ( Exception e ) {
192+ // we probably met a method with some missing optional dependency, so just skip it.
193+ Logger . Log ( LogLevel . Warn , "commands" , "Could not look for custom commands in method " + method . Name ) ;
194+ e . LogDetailed ( ) ;
195+ }
196+ }
197+
198+ private static void setupAdjustCollabIcon ( ) {
199+ CelesteNetPlayerListComponent . OnGetState += CelesteNet . adjustCollabIcon ;
175200 }
176201
177- private static void adjustCollabIcon ( ) {
178- CelesteNetPlayerListComponent . OnGetState += ( blob , state ) => {
202+ private static void teardownAdjustCollabIcon ( ) {
203+ CelesteNetPlayerListComponent . OnGetState -= CelesteNet . adjustCollabIcon ;
204+ }
205+
206+ private class CelesteNet {
207+ internal static void adjustCollabIcon ( CelesteNetPlayerListComponent . BlobPlayer blob , DataPlayerState state ) {
179208 // if we are in a collab map, change the icon displayed in the CelesteNet player list to the lobby icon.
180209 AreaData data = AreaData . Get ( state . SID ) ;
181210 if ( data != null ) {
@@ -184,7 +213,7 @@ private static void adjustCollabIcon() {
184213 blob . Location . Icon = AreaData . Get ( lobbySID ) ? . Icon ?? blob . Location . Icon ;
185214 }
186215 }
187- } ;
216+ }
188217 }
189218
190219 public static void OnSessionCreated ( ) {
0 commit comments