@@ -59,7 +59,7 @@ public static class PluginManager
5959 /// </summary>
6060 public static void Save ( )
6161 {
62- foreach ( var pluginPair in GetAllInitializedPlugins ( ) )
62+ foreach ( var pluginPair in GetAllInitializedPlugins ( false ) )
6363 {
6464 var savable = pluginPair . Plugin as ISavable ;
6565 try
@@ -78,7 +78,8 @@ public static void Save()
7878
7979 public static async ValueTask DisposePluginsAsync ( )
8080 {
81- foreach ( var pluginPair in GetAllInitializedPlugins ( ) )
81+ // Still call dispose for all plugins even if initialization failed, so that we can clean up resources
82+ foreach ( var pluginPair in GetAllInitializedPlugins ( true ) )
8283 {
8384 await DisposePluginAsync ( pluginPair ) ;
8485 }
@@ -106,7 +107,7 @@ private static async Task DisposePluginAsync(PluginPair pluginPair)
106107
107108 public static async Task ReloadDataAsync ( )
108109 {
109- await Task . WhenAll ( [ .. GetAllInitializedPlugins ( ) . Select ( plugin => plugin . Plugin switch
110+ await Task . WhenAll ( [ .. GetAllInitializedPlugins ( false ) . Select ( plugin => plugin . Plugin switch
110111 {
111112 IReloadable p => Task . Run ( p . ReloadData ) ,
112113 IAsyncReloadable p => p . ReloadDataAsync ( ) ,
@@ -120,7 +121,7 @@ public static async Task ReloadDataAsync()
120121
121122 public static async Task OpenExternalPreviewAsync ( string path , bool sendFailToast = true )
122123 {
123- await Task . WhenAll ( [ .. GetAllInitializedPlugins ( ) . Select ( plugin => plugin . Plugin switch
124+ await Task . WhenAll ( [ .. GetAllInitializedPlugins ( false ) . Select ( plugin => plugin . Plugin switch
124125 {
125126 IAsyncExternalPreview p => p . OpenPreviewAsync ( path , sendFailToast ) ,
126127 _ => Task . CompletedTask ,
@@ -129,7 +130,7 @@ public static async Task OpenExternalPreviewAsync(string path, bool sendFailToas
129130
130131 public static async Task CloseExternalPreviewAsync ( )
131132 {
132- await Task . WhenAll ( [ .. GetAllInitializedPlugins ( ) . Select ( plugin => plugin . Plugin switch
133+ await Task . WhenAll ( [ .. GetAllInitializedPlugins ( false ) . Select ( plugin => plugin . Plugin switch
133134 {
134135 IAsyncExternalPreview p => p . ClosePreviewAsync ( ) ,
135136 _ => Task . CompletedTask ,
@@ -138,7 +139,7 @@ public static async Task CloseExternalPreviewAsync()
138139
139140 public static async Task SwitchExternalPreviewAsync ( string path , bool sendFailToast = true )
140141 {
141- await Task . WhenAll ( [ .. GetAllInitializedPlugins ( ) . Select ( plugin => plugin . Plugin switch
142+ await Task . WhenAll ( [ .. GetAllInitializedPlugins ( false ) . Select ( plugin => plugin . Plugin switch
142143 {
143144 IAsyncExternalPreview p => p . SwitchPreviewAsync ( path , sendFailToast ) ,
144145 _ => Task . CompletedTask ,
@@ -523,9 +524,17 @@ public static List<PluginPair> GetAllLoadedPlugins()
523524 return [ .. _allLoadedPlugins ] ;
524525 }
525526
526- public static List < PluginPair > GetAllInitializedPlugins ( )
527+ public static List < PluginPair > GetAllInitializedPlugins ( bool containFailed )
527528 {
528- return [ .. _allInitializedPlugins . Values ] ;
529+ if ( containFailed )
530+ {
531+ return [ .. _allInitializedPlugins . Values ] ;
532+ }
533+ else
534+ {
535+ return [ .. _allInitializedPlugins . Values
536+ . Where ( p => ! _initFailedPlugins . ContainsKey ( p . Metadata . ID ) ) ] ;
537+ }
529538 }
530539
531540 public static List < PluginPair > GetGlobalPlugins ( )
@@ -695,9 +704,10 @@ private static bool SameOrLesserPluginVersionExists(string metadataPath)
695704 if ( ! Version . TryParse ( newMetadata . Version , out var newVersion ) )
696705 return true ; // If version is not valid, we assume it is lesser than any existing version
697706
698- return GetAllInitializedPlugins ( ) . Any ( x => x . Metadata . ID == newMetadata . ID
699- && Version . TryParse ( x . Metadata . Version , out var version )
700- && newVersion <= version ) ;
707+ // Get all plugins even if initialization failed so that we can check if the plugin with the same ID exists
708+ return GetAllInitializedPlugins ( true ) . Any ( x => x . Metadata . ID == newMetadata . ID
709+ && Version . TryParse ( x . Metadata . Version , out var version )
710+ && newVersion <= version ) ;
701711 }
702712
703713 #endregion
@@ -839,7 +849,7 @@ internal static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, boo
839849 // If we want to remove plugin from AllPlugins,
840850 // we need to dispose them so that they can release file handles
841851 // which can help FL to delete the plugin settings & cache folders successfully
842- var pluginPairs = GetAllInitializedPlugins ( ) . Where ( p => p . Metadata . ID == plugin . ID ) . ToList ( ) ;
852+ var pluginPairs = GetAllInitializedPlugins ( true ) . Where ( p => p . Metadata . ID == plugin . ID ) . ToList ( ) ;
843853 foreach ( var pluginPair in pluginPairs )
844854 {
845855 await DisposePluginAsync ( pluginPair ) ;
@@ -885,11 +895,12 @@ internal static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, boo
885895 }
886896 Settings . RemovePluginSettings ( plugin . ID ) ;
887897 _allInitializedPlugins . TryRemove ( plugin . ID , out var item ) ;
888- _globalPlugins . TryRemove ( plugin . ID , out var item1 ) ;
898+ _initFailedPlugins . TryRemove ( plugin . ID , out var item1 ) ;
899+ _globalPlugins . TryRemove ( plugin . ID , out var item2 ) ;
889900 var keysToRemove = _nonGlobalPlugins . Where ( p => p . Value . Metadata . ID == plugin . ID ) . Select ( p => p . Key ) . ToList ( ) ;
890901 foreach ( var key in keysToRemove )
891902 {
892- _nonGlobalPlugins . Remove ( key , out var item2 ) ;
903+ _nonGlobalPlugins . Remove ( key , out var item3 ) ;
893904 }
894905 }
895906
0 commit comments