feat: 优化插件加载与项目结构

This commit is contained in:
denglihong2007
2025-07-26 23:55:24 +08:00
parent 3ee22e02da
commit 5ecc71e4de
16 changed files with 38 additions and 285 deletions

View File

@@ -6,6 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
<PackageId>CRSim.Core</PackageId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

View File

@@ -49,13 +49,12 @@ public class PluginService : IPluginService
PluginManifest manifest = new();
try
{
Console.WriteLine(manifestYaml);
manifest = JsonSerializer.Deserialize(manifestYaml,JsonContextWithCamelCase.Default.PluginManifest);
Console.WriteLine("成功解析YAML文件: " + manifestPath);
Console.WriteLine("成功解析JSON文件: " + manifestPath);
}
catch (Exception ex)
{
Console.WriteLine("YAML解析失败");
Console.WriteLine("JSON解析失败");
Console.WriteLine(ex.ToString());
continue;
}
@@ -91,8 +90,9 @@ public class PluginService : IPluginService
{
Console.WriteLine("尝试加载插件: " + manifest.Id);
var fullPath = Path.GetFullPath(Path.Combine(pluginDir, manifest.EntranceAssembly));
var asm = Assembly.LoadFrom(fullPath);
var entrance = asm.ExportedTypes.FirstOrDefault(x =>
var loadContext = new PluginLoadContext(fullPath);
var assembly = loadContext.LoadFromAssemblyPath(fullPath);
var entrance = assembly.ExportedTypes.FirstOrDefault(x =>
x.BaseType == typeof(PluginBase) ||
x.GetCustomAttributes().FirstOrDefault(a => a.GetType() == typeof(PluginEntrance)) != null);

View File

@@ -0,0 +1,21 @@
using System.Reflection;
using System.Runtime.Loader;
namespace CRSim.Core.Utils
{
internal class PluginLoadContext(string pluginPath) : AssemblyLoadContext
{
private readonly AssemblyDependencyResolver _resolver = new(pluginPath);
protected override Assembly Load(AssemblyName assemblyName)
{
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}
return null;
}
}
}

View File

@@ -1,33 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<RootNamespace>CRSim.ExamplePlugin</RootNamespace>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<EnableDynamicLoading>True</EnableDynamicLoading>
<UseWPF>true</UseWPF>
<Platforms>AnyCPU;x64</Platforms>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CRSim.Core\CRSim.Core.csproj">
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
<ProjectReference Include="..\CRSim.ScreenSimulator\CRSim.ScreenSimulator.csproj">
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Update="icon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="manifest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="style.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -1,21 +0,0 @@
using CRSim.Core.Abstractions;
using CRSim.Core.Attributes;
using CRSim.ExamplePlugin.ViewModels;
using CRSim.ExamplePlugin.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
namespace CRSim.ExamplePlugin;
[PluginEntrance]
public class Plugin : PluginBase
{
public override Type ViewModel => typeof(ScreenViewModel);
public override Type View => typeof(ScreenView);
public override void Initialize(HostBuilderContext context, IServiceCollection services)
{
services.AddTransient(ViewModel);
services.AddTransient(View);
}
}

View File

@@ -1,11 +0,0 @@
{
"profiles": {
"CRSim 插件": {
"commandName": "Executable",
"executablePath": "$(CRSim_DebugBinaryFile)",
"commandLineArgs": "-epp $(TargetDir) -debug",
"workingDirectory": "$(CRSim_DebugBinaryDirectory)",
"nativeDebugging": true
}
}
}

View File

@@ -1,17 +0,0 @@
using CRSim.Core.Models;
using CRSim.Core.Abstractions;
using CRSim.ScreenSimulator.ViewModels;
namespace CRSim.ExamplePlugin.ViewModels
{
public class ScreenViewModel : BaseScreenViewModel
{
public ScreenViewModel(ITimeService timeService, ISettingsService settingsService)
: base(timeService, settingsService)
{
Text = "成都铁路客服电话028-12306";
ItemsPerPage = 12;
ScreenCount = 1;
StationType = StationType.Departure;
}
}
}

View File

@@ -1,134 +0,0 @@
<Page x:Class="CRSim.ExamplePlugin.Views.ScreenView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CRSim.ExamplePlugin.Views"
mc:Ignorable="d"
xmlns:converters="clr-namespace:CRSim.ScreenSimulator.Converters;assembly=CRSim.ScreenSimulator"
FontFamily="Microsoft YaHei"
xmlns:views="clr-namespace:CRSim.ScreenSimulator.Views;assembly=CRSim.ScreenSimulator">
<Page.Resources>
<Style x:Key="HeaderTextBlockStyle" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Width" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, Path=ActualWidth}"/>
</Style>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="#D75B41"/>
</Style>
<converters:ListToStringConverter x:Key="ListToStringConverter" Separator="" />
<converters:EmptyToVisibilityConverter x:Key="EmptyToVisibilityConverter" />
<converters:DateTimeToStringConverter x:Key="DateTimeToStringConverter" Format="HH:mm" />
<converters:DateTimeToStringConverter x:Key="DateTimeToStringTimeConverter" Format="HH:mm:ss" />
<converters:DateTimeToStringConverter x:Key="DateTimeToStringDateConverter" Format="yyyy/MM/dd" />
<converters:TrainStateConverter x:Key="TrainStateConverter" WaitingText="正点"/>
</Page.Resources>
<StackPanel Orientation="Horizontal">
<views:BaseDataGrid ItemsSource="{Binding ScreenA}" RowHeight="28" Foreground="#D75B41" FontWeight="Bold">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding TrainNumber}" MinWidth="55">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="车次" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Origin}" MinWidth="100">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="始发站" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Terminal}" MinWidth="100">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="终到站" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding DepartureTime, Converter={StaticResource DateTimeToStringConverter}}" MinWidth="50">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="开点" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="{Binding TrainNumber,Converter={StaticResource EmptyToVisibilityConverter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding WaitingArea}" MinWidth="80">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="候车区" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding TicketChecks, Converter={StaticResource ListToStringConverter}}" MinWidth="70">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="检票口" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Platform}" MinWidth="70">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="站台" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn MinWidth="90">
<DataGridTextColumn.Binding>
<MultiBinding Mode="OneWay" Converter="{StaticResource TrainStateConverter}">
<Binding Path="ArrivalTime" />
<Binding Path="DepartureTime" />
<Binding Path="State" />
</MultiBinding>
</DataGridTextColumn.Binding>
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="状态" Style="{StaticResource HeaderTextBlockStyle}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="#D75B41"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="Black"/>
</Style>
</DataGrid.RowStyle>
</views:BaseDataGrid>
<Grid Width="350" Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding CurrentTime,Converter={StaticResource DateTimeToStringDateConverter}}"
Foreground="#D75B41" FontSize="44" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock Grid.Row="1" Text="{Binding CurrentTime,Converter={StaticResource DateTimeToStringTimeConverter}}"
Foreground="#D75B41" FontSize="44" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock Grid.Row="3" Text="{Binding Text}" Foreground="#D75B41"
FontWeight="Bold" FontSize="16" TextAlignment="Center" Margin="2"/>
</Grid>
</StackPanel>
</Page>

View File

@@ -1,16 +0,0 @@
using CRSim.ExamplePlugin.ViewModels;
using System.Windows.Controls;
namespace CRSim.ExamplePlugin.Views
{
public partial class ScreenView : Page
{
public ScreenViewModel ViewModel { get; }
public ScreenView(ScreenViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
DataContext = viewModel;
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

View File

@@ -1,11 +0,0 @@
{
"entranceAssembly": "CRSim.ExamplePlugin.dll",
"name": "示例插件",
"id": "CRSim.ExamplePlugin",
"url": "https://crsim.tech",
"version": "0.0.0.0",
"apiVersion": "3.0.0.0",
"author": "电排骨",
"type": "ScreenStyle",
"description": "这是一个示例插件。"
}

View File

@@ -1,5 +0,0 @@
{
"parameters": [ "station", "text" ],
"region": "广元",
"type": "车站大屏"
}

View File

@@ -1,19 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Title>CRSim Plugin SDK</Title>
</PropertyGroup>
<PropertyGroup>
<Description>用于开发应用 CRSim 插件的 SDK。</Description>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CRSim.Core\CRSim.Core.csproj" />
<ProjectReference Include="..\CRSim.ScreenSimulator\CRSim.ScreenSimulator.csproj" />
</ItemGroup>
</Project>

View File

@@ -6,7 +6,9 @@
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<Platforms>AnyCPU;x64</Platforms>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>enable</ImplicitUsings>
<PackageId>CRSim.ScreenSimulator</PackageId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />

View File

@@ -7,12 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRSim.Core", "CRSim.Core\CR
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRSim", "CRSim\CRSim.csproj", "{9C334D96-6CE7-4137-9CA8-2969E4C67C0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRSim.ExamplePlugin", "CRSim.ExamplePlugin\CRSim.ExamplePlugin.csproj", "{FDD66ED3-4F7C-4A81-BE0F-F5E504BFCB2E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRSim.ScreenSimulator", "CRSim.ScreenSimulator\CRSim.ScreenSimulator.csproj", "{94927020-7AD7-4F6F-A456-F34A8E0BC40A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRSim.PluginSdk", "CRSim.PluginSdk\CRSim.PluginSdk.csproj", "{2216DFBB-8EE2-4487-9772-7CAB0BF3A5C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@@ -29,18 +25,10 @@ Global
{9C334D96-6CE7-4137-9CA8-2969E4C67C0F}.Release|x64.ActiveCfg = Release|x64
{9C334D96-6CE7-4137-9CA8-2969E4C67C0F}.Release|x64.Build.0 = Release|x64
{9C334D96-6CE7-4137-9CA8-2969E4C67C0F}.Release|x64.Deploy.0 = Release|x64
{FDD66ED3-4F7C-4A81-BE0F-F5E504BFCB2E}.Debug|x64.ActiveCfg = Debug|x64
{FDD66ED3-4F7C-4A81-BE0F-F5E504BFCB2E}.Debug|x64.Build.0 = Debug|x64
{FDD66ED3-4F7C-4A81-BE0F-F5E504BFCB2E}.Release|x64.ActiveCfg = Release|x64
{FDD66ED3-4F7C-4A81-BE0F-F5E504BFCB2E}.Release|x64.Build.0 = Release|x64
{94927020-7AD7-4F6F-A456-F34A8E0BC40A}.Debug|x64.ActiveCfg = Debug|x64
{94927020-7AD7-4F6F-A456-F34A8E0BC40A}.Debug|x64.Build.0 = Debug|x64
{94927020-7AD7-4F6F-A456-F34A8E0BC40A}.Release|x64.ActiveCfg = Release|x64
{94927020-7AD7-4F6F-A456-F34A8E0BC40A}.Release|x64.Build.0 = Release|x64
{2216DFBB-8EE2-4487-9772-7CAB0BF3A5C6}.Debug|x64.ActiveCfg = Debug|Any CPU
{2216DFBB-8EE2-4487-9772-7CAB0BF3A5C6}.Debug|x64.Build.0 = Debug|Any CPU
{2216DFBB-8EE2-4487-9772-7CAB0BF3A5C6}.Release|x64.ActiveCfg = Release|Any CPU
{2216DFBB-8EE2-4487-9772-7CAB0BF3A5C6}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

7
Directory.Build.props Normal file
View File

@@ -0,0 +1,7 @@
<Project>
<PropertyGroup>
<Version>2.3.103.0</Version>
<Authors>电排骨</Authors>
<RepositoryUrl>https://github.com/denglihong2007/CRSim</RepositoryUrl>
</PropertyGroup>
</Project>