Skip to content

Commit

Permalink
#891 初步实现外置插件功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ywmoyue committed Oct 29, 2024
1 parent 91344e3 commit c4152b2
Show file tree
Hide file tree
Showing 14 changed files with 522 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/BiliLite.UWP/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ private async void InitBili()
App.Current.Resources["ImageCornerRadius"] = new CornerRadius(SettingService.GetValue<double>(SettingConstants.UI.IMAGE_CORNER_RADIUS, 0));
await AppHelper.SetRegions();
await InitDb();

try
{
var downloadService = ServiceProvider.GetRequiredService<DownloadService>();
Expand All @@ -217,6 +218,9 @@ private async void InitBili()
logger.Error("初始化加载下载视频错误", ex);
}
VideoPlayHistoryHelper.LoadABPlayHistories(true);

var pluginService = ServiceProvider.GetRequiredService<PluginService>();
await pluginService.Start();
}

private async Task InitDb()
Expand Down
7 changes: 7 additions & 0 deletions src/BiliLite.UWP/BiliLite.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,19 @@
</Compile>
<Compile Include="Services\BiliLiteDbContext.cs" />
<Compile Include="Services\DownloadService.cs" />
<Compile Include="Services\PluginService.cs" />
<Compile Include="Services\SqlMigrateService.cs" />
<Compile Include="ViewModels\Common\MainPageViewModel.cs" />
<Compile Include="ViewModels\Common\PieControlViewModel.cs" />
<Compile Include="ViewModels\Plugins\WebSocketPluginViewModel.cs" />
<Compile Include="ViewModels\Settings\CDNServerItemViewModel.cs" />
<Compile Include="Pages\ISavablePage.cs" />
<Compile Include="Services\Biz\MediaListService.cs" />
<Compile Include="Services\ContentFilterService.cs" />
<Compile Include="Services\PlaySpeedMenuService.cs" />
<Compile Include="ViewModels\Comment\SendCommentDialogViewModel.cs" />
<Compile Include="ViewModels\Rank\RankRegionViewModel.cs" />
<Compile Include="ViewModels\Settings\DevSettingsControlViewModel.cs" />
<Compile Include="ViewModels\Settings\EditPlaySpeedMenuViewModel.cs" />
<Compile Include="ViewModels\Settings\FilterRuleViewModel.cs" />
<Compile Include="ViewModels\Settings\FilterSettingsViewModel.cs" />
Expand Down Expand Up @@ -1549,6 +1552,9 @@
<PackageReference Include="Tomlyn">
<Version>0.17.0</Version>
</PackageReference>
<PackageReference Include="WebSocket4Net">
<Version>0.15.2</Version>
</PackageReference>
<PackageReference Include="Win2D.uwp">
<Version>1.26.0</Version>
</PackageReference>
Expand All @@ -1575,6 +1581,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Assets\Tools\" />
<Folder Include="Models\Plugins\" />
<Folder Include="Models\Requests\Api\Grpc\" />
<Folder Include="Modules\User\SendDynamic\" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/BiliLite.UWP/Controls/PlayerControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ private async void DanmuTimer_Tick(object sender, object e)

private void PositionTimer_Tick(object sender, object e)
{
PluginCenter.BroadcastPosition(this, Player.Position);
if (!m_autoSkipOpEdFlag) return;
if (CurrentPlayItem == null) return;
if (CurrentPlayItem.EpisodeSkip == null) return;
Expand Down Expand Up @@ -1834,7 +1835,7 @@ double Brightness
{
_brightness = value;
BrightnessShield.Opacity = value;
if(!lockBrightness)
if (!lockBrightness)
SettingService.SetValue<double>(SettingConstants.Player.PLAYER_BRIGHTNESS, _brightness);
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/BiliLite.UWP/Controls/Settings/DevSettingsControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
xmlns:controls1="using:Microsoft.UI.Xaml.Controls"
xmlns:common="using:BiliLite.Models.Common"
xmlns:font="using:FontAwesome5"
xmlns:plugins="using:BiliLite.ViewModels.Plugins"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
Expand Down Expand Up @@ -66,6 +67,12 @@
</ComboBox>
</controls:SettingsCard>

<controls:SettingsExpander Header="外置插件">
<controls:SettingsExpander.Items>
<Button Click="BtnSettingPlugin_OnClick">设置</Button>
</controls:SettingsExpander.Items>
</controls:SettingsExpander>

<controls:SettingsCard Header="迁移数据库"
Description="开发者调试用">
<controls:SettingsCard.HeaderIcon>
Expand Down Expand Up @@ -133,5 +140,45 @@
DisplayMemberPath="Name">
</ComboBox>
</controls:SettingsCard>

<ContentDialog x:Name="PluginsDialog"
Title="插件管理"
PrimaryButtonText="确认"
CloseButtonText="取消"
MinWidth="600">
<StackPanel Orientation="Vertical" Width="500">
<StackPanel>
<Button Click="BtnImportPluginInfo_OnClick">导入插件信息</Button>
</StackPanel>
<ScrollViewer Visibility="{x:Bind m_viewModel.ShowPluginList,Mode=OneWay}">
<ListView ItemsSource="{x:Bind m_viewModel.Plugins,Mode=OneWay}"
SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="plugins:WebSocketPluginViewModel">
<controls:SettingsExpander Header="{x:Bind Name,Mode=OneWay}">
<controls:SettingsExpander.Items>
<controls:SettingsCard Header="名称">
<TextBlock Text="{x:Bind Name}"></TextBlock>
</controls:SettingsCard>
<controls:SettingsCard Header="WS地址">
<TextBlock Text="{x:Bind WebSocketUrl,Mode=OneWay}"></TextBlock>
</controls:SettingsCard>
<controls:SettingsCard Header="Http地址">
<TextBlock Text="{x:Bind CheckUrl,Mode=OneWay}"></TextBlock>
</controls:SettingsCard>
<controls:SettingsCard Header="唤醒协议">
<TextBlock Text="{x:Bind WakeProto,Mode=OneWay}"></TextBlock>
</controls:SettingsCard>
<controls:SettingsCard Header="删除">
<Button Click="BtnDeletePlugin_OnClick">确认删除</Button>
</controls:SettingsCard>
</controls:SettingsExpander.Items>
</controls:SettingsExpander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
</StackPanel>
</ContentDialog>
</StackPanel>
</UserControl>
41 changes: 41 additions & 0 deletions src/BiliLite.UWP/Controls/Settings/DevSettingsControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand All @@ -7,6 +8,13 @@
using BiliLite.Services;
using Microsoft.Extensions.DependencyInjection;
using Windows.ApplicationModel.Core;
using BiliLite.ViewModels.Settings;
using Windows.Storage.Pickers;
using Windows.Storage;
using BiliLite.ViewModels.Plugins;
using Microsoft.Toolkit.Uwp.Helpers;
using Newtonsoft.Json;
using IMapper = AutoMapper.IMapper;

//https://go.microsoft.com/fwlink/?LinkId=234236 上介绍了“用户控件”项模板

Expand All @@ -15,9 +23,17 @@ namespace BiliLite.Controls.Settings
public sealed partial class DevSettingsControl : UserControl
{
private static readonly ILogger _logger = GlobalLogger.FromCurrentType();
private readonly DevSettingsControlViewModel m_viewModel;
private readonly PluginService m_pluginService;
private readonly IMapper m_mapper;

public DevSettingsControl()
{
m_viewModel = App.ServiceProvider.GetRequiredService<DevSettingsControlViewModel>();
m_mapper = App.ServiceProvider.GetRequiredService<IMapper>();
m_pluginService = App.ServiceProvider.GetRequiredService<PluginService>();
m_viewModel.Plugins =
m_mapper.Map<ObservableCollection<WebSocketPluginViewModel>>(m_pluginService.GetPlugins());
this.InitializeComponent();
LoadDev();
}
Expand Down Expand Up @@ -217,5 +233,30 @@ private async void BtnMigrateDb_OnClick(object sender, RoutedEventArgs e)
var migrateService = App.ServiceProvider.GetRequiredService<SqlMigrateService>();
await migrateService.ExcuteAllMigrationScripts();
}

private async void BtnSettingPlugin_OnClick(object sender, RoutedEventArgs e)
{
await PluginsDialog.ShowAsync();
}

private async void BtnImportPluginInfo_OnClick(object sender, RoutedEventArgs e)
{
var filePicker = new FileOpenPicker();
filePicker.FileTypeFilter.Add(".json");
var file = await filePicker.PickSingleFileAsync();
if (file == null) return;
using var openFile = await file.OpenAsync(FileAccessMode.Read);
var text = await openFile.ReadTextAsync();
var plugin = JsonConvert.DeserializeObject<WebSocketPlugin>(text);
await m_pluginService.AddPlugin(plugin);
m_viewModel.AddPlugin(m_mapper.Map<WebSocketPluginViewModel>(plugin));
}

private async void BtnDeletePlugin_OnClick(object sender, RoutedEventArgs e)
{
if (!(sender is Button { DataContext: WebSocketPluginViewModel plugin })) return;
await m_pluginService.RemovePlugin(plugin.Name);
m_viewModel.RemovePlugin(plugin);
}
}
}
3 changes: 3 additions & 0 deletions src/BiliLite.UWP/Extensions/MapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using BiliLite.ViewModels.Comment;
using BiliLite.ViewModels.Download;
using BiliLite.ViewModels.Home;
using BiliLite.ViewModels.Plugins;
using BiliLite.ViewModels.Season;
using BiliLite.ViewModels.Settings;
using BiliLite.ViewModels.User;
Expand All @@ -46,6 +47,8 @@ public static IServiceCollection AddMapper(this IServiceCollection services)
{
var mapper = new Mapper(new MapperConfiguration(expression =>
{
expression.CreateMap<WebSocketPlugin, WebSocketPluginViewModel>()
.ReverseMap();
expression.CreateMap<DownloadedItemDTO, DownloadedItem>()
.ReverseMap();
expression.CreateMap<DownloadedSubItemDTO, DownloadedSubItem>()
Expand Down
1 change: 1 addition & 0 deletions src/BiliLite.UWP/Extensions/ViewModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static IServiceCollection AddViewModels(this IServiceCollection services)
services.AddTransient<PlaySettingsControlViewModel>();
services.AddTransient<VideoDanmakuSettingsControlViewModel>();
services.AddTransient<ShortcutKeySettingsControlViewModel>();
services.AddTransient<DevSettingsControlViewModel>();
services.AddTransient<MainPageViewModel>();
return services;
}
Expand Down
2 changes: 2 additions & 0 deletions src/BiliLite.UWP/Models/Common/SettingConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,8 @@ public class Other
public const string DEFAULT_REQUEST_BUILD = "75900200";

public const string SQL_DB_VERSION = "SqlDbVersion";

public const string PLUGIN_LIST = "PluginList";
}
}
}
5 changes: 5 additions & 0 deletions src/BiliLite.UWP/Pages/BasePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public void StopHighRateSpeedPlay()
Player.StopHighRateSpeedPlay();
}

public void SetPosition(double position)
{
Player.SetPosition(position);
}

public void PositionBack()
{
Player.PositionBack();
Expand Down
Loading

0 comments on commit c4152b2

Please sign in to comment.