Skip to content

Commit

Permalink
适配器错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Jul 24, 2024
1 parent 7985b8b commit aa39649
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
37 changes: 25 additions & 12 deletions NonsPlayer.Core/Services/AdapterService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Diagnostics;
using System.Reflection;
using NonsPlayer.Core.Contracts.Adapters;
using NonsPlayer.Core.Contracts.Services;
using NonsPlayer.Core.Models;
Expand All @@ -12,7 +13,8 @@ public class AdapterService
private Dictionary<string, IAdapter> _disabledAdapters = new();
private List<string> _disabledAdaptersStrings = new();
public static AdapterService Instance { get; } = new();

public delegate void AdapterEventHandler(string name);
public event AdapterEventHandler? AdapterLoadFailed;

public void Init()
{
Expand All @@ -28,14 +30,24 @@ public void LoadAdapters(string directory)

foreach (var file in dllFiles)
{
var (name, assembly) = LoadSingleAdapter(file);
if (_disabledAdaptersStrings.Contains(assembly.GetMetadata().Platform))

try
{
_disabledAdapters.Add(name, assembly);
continue;
}
var (name, assembly) = LoadSingleAdapter(file);
if (name == null) continue;
if (assembly == null) continue;

if (_disabledAdaptersStrings.Contains(assembly.GetMetadata().Platform))
{
_disabledAdapters.Add(name, assembly);
continue;
}

_adapters.Add(name, assembly);
_adapters.Add(name, assembly);
}
catch(NullReferenceException ex)
{
}
}
}

Expand All @@ -53,7 +65,7 @@ public bool DisableAdapter(AdapterMetadata metadata)
return DisableAdapter(metadata.Platform);
}

public Tuple<string, IAdapter>? LoadSingleAdapter(string file)
public Tuple<string?, IAdapter?>? LoadSingleAdapter(string file)
{
try
{
Expand All @@ -67,13 +79,14 @@ public bool DisableAdapter(AdapterMetadata metadata)
return new Tuple<string, IAdapter>(adapter.GetMetadata().Name, adapter);
}
}
AdapterLoadFailed?.Invoke(file);
return new Tuple<string?, IAdapter?>(null, null);

return null;
}
catch (Exception ex)
{
ExceptionService.Instance.Throw(ex, $"Error loading plugin from '{file}': {ex.Message}");
return null;
AdapterLoadFailed?.Invoke(file);
return new Tuple<string?, IAdapter?>(null, null);
}
}

Expand Down
12 changes: 8 additions & 4 deletions NonsPlayer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public App()
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
services.AddSingleton<IFileService, FileService>();
services.AddSingleton<VersionService>();
services.AddSingleton<UpdateService>();
services.AddSingleton<UpdateClient>();
Expand All @@ -59,9 +60,6 @@ public App()
services.AddSingleton<SMTCService>();
services.AddSingleton<RadioService>();
// Core Services
services.AddSingleton<IFileService, FileService>();
#region Views and ViewModels
services.AddTransient<PlaylistDetailViewModel>();
Expand Down Expand Up @@ -116,7 +114,7 @@ public App()
services.AddTransient<RecentlyPlayItemCardViewModel>();
services.AddTransient<RecommendedPlaylistCardViewModel>();
services.AddTransient<RadioCardViewModel>();
services.AddTransient<LoginCardViewModel>();
#endregion
Expand All @@ -131,12 +129,18 @@ public App()

GetService<IAppNotificationService>().Initialize();
ConfigManager.Instance.LoadConfig();
AdapterService.Instance.AdapterLoadFailed += OnAdapterLoadFailed;
AdapterService.Instance.Init();
UnhandledException += App_UnhandledException;
GetService<PlayCounterService>().Init();
GetService<SMTCService>().Init();
}

private void OnAdapterLoadFailed(string name)
{
ExceptionService.Instance.Throw($"Failed load adapter: {name}");
}

// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
Expand Down

0 comments on commit aa39649

Please sign in to comment.