Skip to content

Commit 578bd29

Browse files
committed
Merge branch 'develop'
2 parents 3cc321d + 7bd7ef8 commit 578bd29

21 files changed

+177
-85
lines changed

Changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 3.14.3 (2024/05/16)
2+
- 优化插件开发
3+
- 允许插件使用log功能
4+
- 当增加或删除插件不在需要重启软件
5+
- 优化代理设置提示
6+
17
# 3.14.2 (2024/05/13)
28
- 优化当多个任务同时开启日志时界面卡顿的问题
39

M3u8Downloader_H.Core/DownloadClient.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public class DownloadClient
2525
private Uri _url;
2626
private readonly IEnumerable<KeyValuePair<string, string>>? _header;
2727
private readonly IPluginManager? pluginManager;
28+
private readonly ILog _log;
2829
private M3u8FileInfoClient? m3U8FileInfoClient;
2930
private M3uDownloaderClient? m3UDownloaderClient;
3031
private M3uCombinerClient? m3UCombinerClient;
3132
private IM3u8UriManager? m3U8UriManager;
3233

33-
public ILog? Log { get; set; } = default!;
3434
public string M3uContent { get; set; } = default!;
3535
public M3UFileInfo M3u8FileInfo { get; set; } = default!;
3636
public M3UKeyInfo M3UKeyInfo { get; set; } = default!;
@@ -42,7 +42,7 @@ private IM3u8UriManager M3U8UriManager
4242
{
4343
get
4444
{
45-
m3U8UriManager ??= M3u8UriManagerFactory.CreateM3u8UriManager(pluginManager?.M3U8UriProvider, httpClient, _header);
45+
m3U8UriManager ??= M3u8UriManagerFactory.CreateM3u8UriManager(pluginManager?.M3U8UriProvider, _header);
4646
return m3U8UriManager;
4747
}
4848

@@ -54,7 +54,7 @@ private IM3UFileInfoMananger M3uFileReader
5454
{
5555
m3U8FileInfoClient ??= new M3u8FileInfoClient(httpClient, pluginManager);
5656
m3U8FileInfoClient.M3UFileReader.TimeOuts = TimeSpan.FromSeconds(Settings.Timeouts);
57-
m3U8FileInfoClient.M3UFileReader.Log = Log;
57+
m3U8FileInfoClient.M3UFileReader.Log = _log;
5858
return m3U8FileInfoClient.M3UFileReader;
5959
}
6060
}
@@ -70,7 +70,7 @@ public IDownloaderSource Downloader
7070
m3UDownloaderClient.Downloader.M3UFileInfo = M3u8FileInfo;
7171
m3UDownloaderClient.Downloader.Headers = _header;
7272
m3UDownloaderClient.Downloader.DownloadParams = DownloadParams;
73-
m3UDownloaderClient.Downloader.Log = Log;
73+
m3UDownloaderClient.Downloader.Log = _log;
7474
}
7575

7676
return m3UDownloaderClient.Downloader;
@@ -85,18 +85,19 @@ public M3uCombinerClient Merger
8585
{
8686
DownloadParams = DownloadParams,
8787
Settings = Settings,
88-
Log = Log
88+
Log = _log
8989
};
9090
return m3UCombinerClient;
9191
}
9292
}
9393

94-
public DownloadClient(HttpClient httpClient,Uri url,IEnumerable<KeyValuePair<string,string>>? header, IPluginBuilder? pluginBuilder)
94+
public DownloadClient(HttpClient httpClient,Uri url,IEnumerable<KeyValuePair<string,string>>? header, ILog log, Type? pluginType)
9595
{
9696
this.httpClient = httpClient;
9797
_url = url;
9898
_header = header;
99-
pluginManager = PluginManger.CreatePluginMangaer(pluginBuilder);
99+
_log = log;
100+
pluginManager = PluginManger.CreatePluginMangaer(pluginType, httpClient, log);
100101
}
101102

102103
public async Task GetM3u8Uri(CancellationToken cancellationToken)
@@ -122,7 +123,7 @@ public async Task GetM3U8FileInfo(CancellationToken cancellationToken)
122123
{
123124
M3u8FileInfo = await M3uFileReader.GetM3u8FileInfo(_url, _header, cancellationToken);
124125
}
125-
Log?.Info("获取视频流{0}个", M3u8FileInfo.MediaFiles.Count);
126+
_log.Info("获取视频流{0}个", M3u8FileInfo.MediaFiles.Count);
126127
if (M3UKeyInfo is not null)
127128
M3u8FileInfo.Key = M3UKeyInfo;
128129

M3u8Downloader_H.Core/M3u8Downloader_H.Core.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<Nullable>enable</Nullable>
6-
<Version>3.14.2.0</Version>
6+
<Version>3.14.3.0</Version>
77
<Authors>Harlan</Authors>
8-
<AssemblyVersion>3.14.2.0</AssemblyVersion>
8+
<AssemblyVersion>3.14.3.0</AssemblyVersion>
99
</PropertyGroup>
1010

1111
<PropertyGroup Condition="'$(Configuration)'=='Release'">

M3u8Downloader_H.Core/M3u8UriManagers/M3u8UriManagerFactory.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace M3u8Downloader_H.Core.M3u8UriManagers
66
{
77
internal class M3u8UriManagerFactory
88
{
9-
public static IM3u8UriManager CreateM3u8UriManager(IM3u8UriProvider? iM3U8UriProvider,HttpClient httpClient, IEnumerable<KeyValuePair<string, string>>? headers)
9+
public static IM3u8UriManager CreateM3u8UriManager(IM3u8UriProvider? iM3U8UriProvider,IEnumerable<KeyValuePair<string, string>>? headers)
1010
{
1111
if (iM3U8UriProvider is not null)
12-
return new PluginM3u8UriManager(iM3U8UriProvider, httpClient, headers);
12+
return new PluginM3u8UriManager(iM3U8UriProvider, headers);
1313
else
1414
return new M3u8UriManager();
1515

M3u8Downloader_H.Core/M3u8UriManagers/PluginM3u8UriManager.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ namespace M3u8Downloader_H.Core.M3u8UriManagers
1010
internal class PluginM3u8UriManager : IM3u8UriManager
1111
{
1212
private readonly IM3u8UriProvider m3U8UriProvider;
13-
private readonly HttpClient httpClient;
1413
private readonly IEnumerable<KeyValuePair<string, string>>? headers;
1514
public bool Completed { get; set; } = false;
1615

17-
public PluginM3u8UriManager(IM3u8UriProvider m3U8UriProvider,HttpClient httpClient, IEnumerable<KeyValuePair<string, string>>? headers)
16+
public PluginM3u8UriManager(IM3u8UriProvider m3U8UriProvider,IEnumerable<KeyValuePair<string, string>>? headers)
1817
{
1918
this.m3U8UriProvider = m3U8UriProvider;
20-
this.httpClient = httpClient;
2119
this.headers = headers;
2220
}
2321

@@ -27,7 +25,7 @@ public Task<Uri> GetM3u8UriAsync(Uri uri, int reserve0, CancellationToken cancel
2725
{
2826
try
2927
{
30-
var url = m3U8UriProvider.GetM3u8UriAsync(httpClient, uri, headers, cancellationToken);
28+
var url = m3U8UriProvider.GetM3u8UriAsync(uri, headers, cancellationToken);
3129
Completed = true;
3230
return url;
3331
}

M3u8Downloader_H.Downloader/M3uDownloaders/PluginM3u8Downloader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public PluginM3u8Downloader(IDownloadService downloadService, M3UFileInfo m3UFi
2020

2121
public override async ValueTask Initialization(CancellationToken cancellationToken)
2222
{
23-
await _pluginDownload.Initialize(HttpClient,Headers, m3UFileInfo, cancellationToken);
23+
await _pluginDownload.Initialize(Headers, m3UFileInfo, cancellationToken);
2424
}
2525

2626
protected override Stream DownloadAfter(Stream stream, string contentType, CancellationToken cancellationToken)

M3u8Downloader_H.M3U8/M3UFileReaderManangers/PluginM3UFileReaderManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public PluginM3UFileReaderManager(IM3u8FileInfoStreamService m3U8FileInfoService
2020
}
2121

2222
protected override async Task<(Uri?, Stream)> GetM3u8FileStreamAsync(Uri uri, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default)
23-
=> await m3U8FileInfoService.GetM3u8FileStreamAsync(httpClient, uri, headers, cancellationToken);
23+
=> await m3U8FileInfoService.GetM3u8FileStreamAsync(uri, headers, cancellationToken);
2424
}
2525
}

M3u8Downloader_H.M3U8/M3u8Downloader_H.M3U8.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<Nullable>enable</Nullable>
6-
<AssemblyVersion>3.14.2.0</AssemblyVersion>
7-
<Version>3.14.2.0</Version>
6+
<AssemblyVersion>3.14.3.0</AssemblyVersion>
7+
<Version>3.14.3.0</Version>
88
</PropertyGroup>
99

1010
<PropertyGroup Condition="'$(Configuration)'=='Release'">

M3u8Downloader_H.Plugin.Abstractions/IDownloadService.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ public interface IDownloadService
77
/// <summary>
88
/// 初始化函数,在没有进行任何下载之前,第一个调用的函数
99
/// </summary>
10-
/// <param name="httpClient">http实例</param>
1110
/// <param name="headers">附带的请求头,如果有的话</param>
1211
/// <param name="m3UFileInfo">m3u8的数据</param>
1312
/// <param name="cancellationToken">取消的token</param>
1413
/// <returns>没有返回内容</returns>
15-
Task Initialize(HttpClient httpClient, IEnumerable<KeyValuePair<string, string>>? headers, M3UFileInfo m3UFileInfo, CancellationToken cancellationToken);
14+
Task Initialize(IEnumerable<KeyValuePair<string, string>>? headers, M3UFileInfo m3UFileInfo, CancellationToken cancellationToken);
1615

1716

1817
/// <summary>

M3u8Downloader_H.Plugin.Abstractions/IM3u8FileInfoStreamService.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ public interface IM3u8FileInfoStreamService
1010
/// 如果需要默认得处理函数可以使用下面得方式,同时引入M3u8Downloader_H.Common这个dll
1111
/// httpClient.GetStreamAndUriAsync(uri, headers, cancellationToken);
1212
/// </summary>
13-
/// <param name="httpClient">http实例</param>
1413
/// <param name="uri">请求地址</param>
1514
/// <param name="headers">请求头</param>
1615
/// <param name="cancellationToken">取消的令牌</param>
1716
/// <returns>返回得到得m3u8数据流</returns>
18-
Task<(Uri?,Stream)> GetM3u8FileStreamAsync(HttpClient httpClient, Uri uri, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default);
17+
Task<(Uri?,Stream)> GetM3u8FileStreamAsync(Uri uri, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default);
1918
}
2019
}

M3u8Downloader_H.Plugin.Abstractions/IM3u8UriProvider.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ public interface IM3u8UriProvider
55
/// <summary>
66
/// 获取m3u8得数据流,此函数可以做解析网站得操作,只要最终返回m3u8得数据流即可
77
/// </summary>
8-
/// <param name="httpClient">http实例</param>
98
/// <param name="uri">请求地址</param>
109
/// <param name="headers">请求头</param>
1110
/// <param name="cancellationToken">取消的令牌</param>
1211
/// <returns>返回m3u8得地址</returns>
13-
Task<Uri> GetM3u8UriAsync(HttpClient httpClient, Uri uri, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default);
12+
Task<Uri> GetM3u8UriAsync(Uri uri, IEnumerable<KeyValuePair<string, string>>? headers, CancellationToken cancellationToken = default);
1413
}
1514
}

M3u8Downloader_H.Plugin.Abstractions/IPluginBuilder.cs

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22
{
33
public interface IPluginBuilder
44
{
5+
// 修改后的插件将在构造函数中添加两个参数
6+
// HttpClient , Ilog
7+
// 两个参数的位置无所谓 需要用哪个就在构造函数中添加上
8+
// 构造函数必须时继承自IPluginBuilder接口的构造函数,
9+
// 如果这两个参数都不需要可以不写构造函数
10+
/* 例子1:
11+
public class Class1 : IPluginBuilder
12+
{
13+
public Class1(HttpClient httpClient)
14+
{
15+
this.httpClient = httpClient;
16+
}
17+
}
18+
例子2:
19+
public class Class1 : IPluginBuilder
20+
{
21+
public Class1(HttpClient httpClient,ILog log)
22+
{
23+
this.httpClient = httpClient;
24+
this.log = log;
25+
}
26+
}
27+
例子3:
28+
public class Class1 : IPluginBuilder
29+
{
30+
public Class1(ILog log)
31+
{
32+
this.log = log;
33+
}
34+
}
35+
*/
536
/// <summary>
637
/// 创建获取m3u8 uri的类
738
/// 如果你不需要处理,只要return null 即可

M3u8Downloader_H.Plugin/M3u8Downloader_H.Plugin.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<Version>1.0.1.0</Version>
7+
<Version>1.0.2.0</Version>
88
</PropertyGroup>
99

1010
<PropertyGroup Condition="'$(Configuration)'=='Release'">

M3u8Downloader_H.Plugin/PluginClient.cs

+49-10
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,91 @@ namespace M3u8Downloader_H.Plugin.PluginClients
55
{
66
public partial class PluginClient
77
{
8+
private static string _filterStr = "M3u8Downloader_H.*.plugin.dll";
9+
private static string _pluginKeyRegex = @"M3u8Downloader_H\.(.*?)\.plugin";
810
private readonly Dictionary<string, Type> _pluginDict = new();
11+
private readonly FileSystemWatcher watcher = new FileSystemWatcher();
912
public IEnumerable<string> Keys => _pluginDict.Keys;
1013

14+
public string PluginPath { get; set; } = default!;
15+
1116
private PluginClient()
1217
{
1318

1419
}
1520

16-
public void Load(string path)
21+
public void Init()
22+
{
23+
watcher.Path = PluginPath;
24+
watcher.Filter = _filterStr;
25+
watcher.NotifyFilter = NotifyFilters.FileName;
26+
watcher.Created += OnCreated;
27+
watcher.Deleted += OnDeleted;
28+
}
29+
30+
private void OnDeleted(object sender, FileSystemEventArgs e)
31+
{
32+
if (string.IsNullOrEmpty(e.Name)) return;
33+
34+
string key = e.Name!.Normalize(_pluginKeyRegex);
35+
if (_pluginDict.ContainsKey(key))
36+
{
37+
_pluginDict.Remove(key);
38+
}
39+
}
40+
41+
private void OnCreated(object sender, FileSystemEventArgs e)
42+
{
43+
if (string.IsNullOrEmpty(e.Name)) return;
44+
45+
LoadFile(e.FullPath, e.Name!);
46+
}
47+
48+
public void Load()
1749
{
1850
try
1951
{
20-
DirectoryInfo directoryInfo = new(path);
21-
foreach (var item in directoryInfo.EnumerateFiles("M3u8Downloader_H.*.plugin.dll", SearchOption.TopDirectoryOnly))
52+
DirectoryInfo directoryInfo = new(PluginPath);
53+
foreach (var item in directoryInfo.EnumerateFiles(_filterStr, SearchOption.TopDirectoryOnly))
2254
{
23-
Type? type = LoadLibrary(item.FullName);
24-
string key = item.Name.Normalize(@"M3u8Downloader_H\.(.*?)\.plugin");
25-
if (type is not null && !string.IsNullOrWhiteSpace(key))
26-
_pluginDict.Add(key, type);
55+
LoadFile(item.FullName, item.Name);
2756
}
57+
watcher.EnableRaisingEvents = true;
2858
}
2959
catch (DirectoryNotFoundException)
3060
{
3161

3262
}
3363
}
3464

65+
private void LoadFile(string fullPath,string fileName)
66+
{
67+
Type? type = LoadLibrary(fullPath);
68+
string key = fileName.Normalize(_pluginKeyRegex);
69+
if (type is not null && !string.IsNullOrWhiteSpace(key))
70+
_pluginDict.Add(key, type);
71+
}
72+
73+
3574
private static Type? LoadLibrary(string path)
3675
{
3776
try
3877
{
39-
Type[] exportTypes = Assembly.LoadFrom(path).GetExportedTypes();
78+
Type[] exportTypes = Assembly.LoadFile(path).GetExportedTypes();
4079
return exportTypes.FirstOrDefault(i => typeof(IPluginBuilder).IsAssignableFrom(i));
4180
}catch(FileLoadException)
4281
{
4382
return null;
4483
}
4584
}
4685

47-
public IPluginBuilder? CreatePluginBuilder(string? key)
86+
public Type? GetPluginType(string? key)
4887
{
4988
if (string.IsNullOrWhiteSpace(key))
5089
return null;
5190

5291
if (_pluginDict.TryGetValue(key, out Type? type))
53-
return (IPluginBuilder?)Activator.CreateInstance(type);
92+
return type;
5493
return null;
5594
}
5695
}

M3u8Downloader_H.Plugin/PluginManagers/PluginManger.cs

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using M3u8Downloader_H.Plugin.AttributeReaderManagers;
1+
using M3u8Downloader_H.Common.Interfaces;
2+
using M3u8Downloader_H.Plugin.AttributeReaderManagers;
3+
using Newtonsoft.Json.Linq;
4+
using System.Reflection;
25

36
namespace M3u8Downloader_H.Plugin.PluginManagers
47
{
@@ -35,11 +38,38 @@ private void Build()
3538

3639
public partial class PluginManger
3740
{
38-
public static PluginManger? CreatePluginMangaer(IPluginBuilder? pluginBuilder)
41+
public static PluginManger? CreatePluginMangaer(Type? type,HttpClient httpClient,ILog log)
3942
{
40-
if (pluginBuilder is null)
43+
if (type is null)
4144
return null;
4245

46+
ConstructorInfo constructor = type.GetConstructors()[0];
47+
ParameterInfo[] parameterInfos = constructor.GetParameters();
48+
49+
50+
51+
IPluginBuilder pluginBuilder;
52+
if (parameterInfos.Length == 0)
53+
pluginBuilder = (IPluginBuilder)constructor.Invoke(null);
54+
else
55+
{
56+
object[] argsArray = new object[parameterInfos.Length];
57+
for (int i = 0; i < parameterInfos.Length; i++)
58+
{
59+
if (parameterInfos[i].ParameterType == typeof(HttpClient))
60+
{
61+
argsArray[i] = httpClient;
62+
}else if (parameterInfos[i].ParameterType == typeof(ILog))
63+
{
64+
argsArray[i] = log;
65+
}else
66+
{
67+
argsArray[i] = default!;
68+
}
69+
}
70+
pluginBuilder = (IPluginBuilder)constructor.Invoke(argsArray);
71+
}
72+
4373
PluginManger pluginManger = new(pluginBuilder);
4474
pluginManger.Build();
4575
return pluginManger;

0 commit comments

Comments
 (0)