@@ -26,9 +26,8 @@ public class WorkshopService : IWorkshopService
2626 private readonly IWorkshopClient _workshopClient ;
2727 private readonly PluginSetting < bool > _migratedBuiltInPlugins ;
2828
29-
30-
3129 private bool _initialized ;
30+ private bool _mutating ;
3231
3332 public WorkshopService ( ILogger logger ,
3433 IHttpClientFactory httpClientFactory ,
@@ -173,27 +172,45 @@ public async Task NavigateToEntry(long entryId, EntryType entryType)
173172 /// <inheritdoc />
174173 public async Task < EntryInstallResult > InstallEntry ( IEntrySummary entry , IRelease release , Progress < StreamProgress > progress , CancellationToken cancellationToken )
175174 {
176- IEntryInstallationHandler handler = _factory . CreateHandler ( entry . EntryType ) ;
177- EntryInstallResult result = await handler . InstallAsync ( entry , release , progress , cancellationToken ) ;
178- if ( result . IsSuccess && result . Entry != null )
179- OnEntryInstalled ? . Invoke ( this , result . Entry ) ;
180- else
181- _logger . Warning ( "Failed to install entry {Entry}: {Message}" , entry , result . Message ) ;
182-
183- return result ;
175+ _mutating = true ;
176+
177+ try
178+ {
179+ IEntryInstallationHandler handler = _factory . CreateHandler ( entry . EntryType ) ;
180+ EntryInstallResult result = await handler . InstallAsync ( entry , release , progress , cancellationToken ) ;
181+ if ( result . IsSuccess && result . Entry != null )
182+ OnEntryInstalled ? . Invoke ( this , result . Entry ) ;
183+ else
184+ _logger . Warning ( "Failed to install entry {Entry}: {Message}" , entry , result . Message ) ;
185+
186+ return result ;
187+ }
188+ finally
189+ {
190+ _mutating = false ;
191+ }
184192 }
185193
186194 /// <inheritdoc />
187195 public async Task < EntryUninstallResult > UninstallEntry ( InstalledEntry installedEntry , CancellationToken cancellationToken )
188196 {
189- IEntryInstallationHandler handler = _factory . CreateHandler ( installedEntry . EntryType ) ;
190- EntryUninstallResult result = await handler . UninstallAsync ( installedEntry , cancellationToken ) ;
191- if ( result . IsSuccess )
192- OnEntryUninstalled ? . Invoke ( this , installedEntry ) ;
193- else
194- _logger . Warning ( "Failed to uninstall entry {EntryId}: {Message}" , installedEntry . Id , result . Message ) ;
195-
196- return result ;
197+ _mutating = true ;
198+
199+ try
200+ {
201+ IEntryInstallationHandler handler = _factory . CreateHandler ( installedEntry . EntryType ) ;
202+ EntryUninstallResult result = await handler . UninstallAsync ( installedEntry , cancellationToken ) ;
203+ if ( result . IsSuccess )
204+ OnEntryUninstalled ? . Invoke ( this , installedEntry ) ;
205+ else
206+ _logger . Warning ( "Failed to uninstall entry {EntryId}: {Message}" , installedEntry . Id , result . Message ) ;
207+
208+ return result ;
209+ }
210+ finally
211+ {
212+ _mutating = false ;
213+ }
197214 }
198215
199216 /// <inheritdoc />
@@ -316,16 +333,28 @@ private async Task MigrateBuiltInPlugins()
316333 // If already migrated, do nothing
317334 if ( _migratedBuiltInPlugins . Value )
318335 return ;
336+
337+ _mutating = true ;
319338
320- MigratingBuildInPlugins ? . Invoke ( this , EventArgs . Empty ) ;
339+ try
340+ {
341+ MigratingBuildInPlugins ? . Invoke ( this , EventArgs . Empty ) ;
321342
322- bool migrated = await BuiltInPluginsMigrator . Migrate ( this , _workshopClient , _logger , _pluginRepository ) ;
323- _migratedBuiltInPlugins . Value = migrated ;
324- _migratedBuiltInPlugins . Save ( ) ;
343+ bool migrated = await BuiltInPluginsMigrator . Migrate ( this , _workshopClient , _logger , _pluginRepository ) ;
344+ _migratedBuiltInPlugins . Value = migrated ;
345+ _migratedBuiltInPlugins . Save ( ) ;
346+ }
347+ finally
348+ {
349+ _mutating = false ;
350+ }
325351 }
326352
327353 private void ProfileServiceOnProfileRemoved ( object ? sender , ProfileConfigurationEventArgs e )
328354 {
355+ if ( _mutating )
356+ return ;
357+
329358 InstalledEntry ? entry = GetInstalledEntryByProfile ( e . ProfileConfiguration ) ;
330359 if ( entry == null )
331360 return ;
@@ -336,6 +365,9 @@ private void ProfileServiceOnProfileRemoved(object? sender, ProfileConfiguration
336365
337366 private void PluginManagementServiceOnPluginRemoved ( object ? sender , PluginEventArgs e )
338367 {
368+ if ( _mutating )
369+ return ;
370+
339371 InstalledEntry ? entry = GetInstalledEntryByPlugin ( e . Plugin ) ;
340372 if ( entry == null )
341373 return ;
@@ -349,6 +381,6 @@ private void PluginManagementServiceOnPluginRemoved(object? sender, PluginEventA
349381 public event EventHandler < InstalledEntry > ? OnEntryUninstalled ;
350382
351383 public event EventHandler < InstalledEntry > ? OnEntryInstalled ;
352-
384+
353385 public event EventHandler ? MigratingBuildInPlugins ;
354386}
0 commit comments