支持代码插件

This commit is contained in:
ZouJin 2023-04-02 05:31:28 +10:00
parent 508e4af3c0
commit 03f96651e4
14 changed files with 321 additions and 50 deletions

View File

@ -19,6 +19,12 @@ namespace VPet_Simulator.Core
public const int LoopMin = 5;
public const int TreeRND = 5;
/// <summary>
/// 处理说话内容
/// </summary>
public event Action<string> OnSay;
public Timer EventTimer = new Timer(15000)
{
AutoReset = true,
@ -30,6 +36,7 @@ namespace VPet_Simulator.Core
/// <param name="text">说话内容</param>
public void Say(string text, GraphCore.Helper.SayType type = GraphCore.Helper.SayType.Shining)
{
OnSay.Invoke(text);
if (type != GraphCore.Helper.SayType.None && DisplayType == GraphCore.GraphType.Default)
Display(GraphCore.Helper.Convert(type, GraphCore.Helper.AnimatType.A_Start), () =>
{
@ -41,6 +48,7 @@ namespace VPet_Simulator.Core
Dispatcher.Invoke(() => MsgBar.Show(Core.Save.Name, text, type));
}
}
public void Saying(GraphCore.Helper.SayType type)
{
Display(GraphCore.Helper.Convert(type, GraphCore.Helper.AnimatType.B_Loop), () => Saying(type));

View File

@ -3,11 +3,70 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using VPet_Simulator.Core;
namespace VPet_Simulator.Windows.Interface
{
public interface IMainWindow
{
/// <summary>
/// 是否为Steam用户
/// </summary>
bool IsSteamUser { get; }
/// <summary>
/// 游戏设置
/// </summary>
Setting Set { get; set; }
/// <summary>
/// 宠物加载器列表
/// </summary>
List<PetLoader> Pets { get; set; }
/// <summary>
/// 桌宠数据核心
/// </summary>
GameCore Core { get; set; }
/// <summary>
/// 桌宠主要部件
/// </summary>
Main Main { get; set; }
/// <summary>
/// 版本号
/// </summary>
int verison { get; }
/// <summary>
/// 版本号
/// </summary>
string Verison { get; }
/// <summary>
/// 上次点击时间 (Tick)
/// </summary>
long lastclicktime { get; set; }
/// <summary>
/// 所有三方插件
/// </summary>
List<MainPlugin> Plugins { get; }
Dispatcher Dispatcher { get; }
/// <summary>
/// 设置游戏缩放倍率
/// </summary>
/// <param name="zl">缩放倍率 范围0.1-10</param>
void SetZoomLevel(double zl);
/// <summary>
/// 保存设置
/// </summary>
void Save();
/// <summary>
/// 加载DIY内容
/// </summary>
void LoadDIY();
/// <summary>
/// 运行动作
/// </summary>
/// <param name="action">动作名称</param>
void RunAction(string action);
}
}

View File

@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace VPet_Simulator.Windows.Interface
{
public interface ITalkBox
{
/// <summary>
/// 当前UI
/// </summary>
UIElement This { get; }
}
}

View File

@ -0,0 +1,58 @@
using LinePutScript;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VPet_Simulator.Windows.Interface
{
/// <summary>
/// 这是插件的主体内容 请继承这个类
/// </summary>
public abstract class MainPlugin
{
/// <summary>
/// 主窗体, 主程序提供的各种功能和设置等 大部分参数和调用均在这里
/// </summary>
public IMainWindow MW;
/// <summary>
/// MOD插件初始化
/// </summary>
/// <param name="mainwin">主窗体</param>
/// 请不要加载游戏和玩家数据,仅用作初始化
/// 加载数据(CORE)/游戏(SAVE),请使用 StartGame
public MainPlugin(IMainWindow mainwin)
{
//此处主窗体玩家,Core等信息均为空,请不要加载游戏和玩家数据
MW = mainwin;
}
///// <summary>//TODO
///// 加载游戏主题
///// </summary>
///// <param name="theme">主题</param>
//public virtual void LoadTheme(Theme theme) { }
/// <summary>
/// 游戏开始 (可以读取Save存档) (如果玩家登出后重新开始游戏,将会被再次调用)
/// </summary>
public virtual void StartGame() { }
/// <summary>
/// 游戏结束 (可以保存或清空等,不过保存有专门的Save())
/// </summary>
public virtual void EndGame() { }
/// <summary>
/// 储存游戏 (可以写 Save.Other 储存设置和数据等)
/// </summary>
public virtual void Save() { }
/// <summary>
/// 打开代码插件设置
/// </summary>
public virtual void Setting() { }
/// <summary>
/// 重载DIY按钮, 如需添加自定义按钮可在此处添加
/// </summary>
public virtual void LoadDIY() { }
}
}

View File

@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -30,10 +31,65 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>ARM64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM64'">
<OutputPath>bin\ARM64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="LinePutScript, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\LinePutScript.1.6.1\lib\net462\LinePutScript.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -42,14 +98,22 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="IMainWindow.cs" />
<Compile Include="MainPlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Setting.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VPet-Simulator.Core\VPet-Simulator.Core.csproj">
<Project>{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}</Project>
<Name>VPet-Simulator.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -10,12 +10,19 @@ using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using VPet_Simulator.Core;
using VPet_Simulator.Windows.Interface;
using static VPet_Simulator.Core.GraphCore;
namespace VPet_Simulator.Windows
{
public class CoreMOD
{
public static List<string> LoadedDLL { get; } = new List<string>()
{
"ChatGPT.API.Framework.dll","Panuon.WPF.dll","steam_api.dll","Panuon.WPF.UI.dll","steam_api64.dll",
"LinePutScript.dll","Newtonsoft.Json.dll","Facepunch.Steamworks.Win32.dll", "Facepunch.Steamworks.Win64.dll",
"VPet-Simulator.Core.dll","VPet-Simulator.Windows.Interface.dll"
};
public static string NowLoading = null;
public string Name;
public string Author;
@ -82,6 +89,40 @@ namespace VPet_Simulator.Windows
}
}
break;
case "plugin":
Content += "代码插件\n";
SuccessLoad = false;
if (!IsPassMOD(mw))
{//不是通过模组,不加载
break;
}
foreach (FileInfo tmpfi in di.EnumerateFiles("*.dll"))
{
try
{
var path = tmpfi.FullName;
if (LoadedDLL.Contains(path))
continue;
LoadedDLL.Add(path);
Assembly dll = Assembly.LoadFrom(path);
var v = dll.GetExportedTypes();
foreach (Type exportedType in v)
{
if (exportedType.BaseType == typeof(MainPlugin))
{
mw.Plugins.Add((MainPlugin)Activator.CreateInstance(exportedType, mw));
}
}
}
catch
{
}
}
SuccessLoad = true;
break;
}
}
}

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using VPet_Simulator.Core;
using VPet_Simulator.Windows.Interface;
@ -17,17 +18,23 @@ namespace VPet_Simulator.Windows
public partial class MainWindow : IMainWindow
{
public readonly string ModPath = Environment.CurrentDirectory + @"\mod";
public readonly bool IsSteamUser;
public Setting Set;
public List<PetLoader> Pets = new List<PetLoader>();
public bool IsSteamUser { get; }
public Setting Set { get; set; }
public List<PetLoader> Pets { get; set; } = new List<PetLoader>();
public List<CoreMOD> CoreMODs = new List<CoreMOD>();
public GameCore Core = new GameCore();
public winGameSetting winSetting;
public GameCore Core { get; set; } = new GameCore();
public Main Main { get; set; }
public UIElement TalkBox;
public winGameSetting winSetting { get; set; }
public ChatGPTClient CGPTClient;
/// <summary>
/// 所有三方插件
/// </summary>
public List<MainPlugin> Plugins { get; } = new List<MainPlugin>();
/// <summary>
/// 版本号
/// </summary>
public readonly int verison = 10;
public int verison { get; } = 10;
/// <summary>
/// 版本号
/// </summary>
@ -44,6 +51,8 @@ namespace VPet_Simulator.Windows
/// </summary>
public void Save()
{
foreach (MainPlugin mp in Plugins)
mp.Save();
//游戏存档
if (Set != null)
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"\Setting.lps", Set.ToString());
@ -52,11 +61,24 @@ namespace VPet_Simulator.Windows
if (CGPTClient != null)
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"\ChatGPTSetting.json", CGPTClient.Save());
}
/// <summary>
/// 重载DIY按钮区域
/// </summary>
public void LoadDIY()
{
Main.ToolBar.MenuDIY.Items.Clear();
foreach (Sub sub in Set["diy"])
Main.ToolBar.AddMenuButton(ToolBar.MenuType.DIY, sub.Name, () => RunDIY(sub.Info));
try
{
//加载游戏创意工坊插件
foreach (MainPlugin mp in Plugins)
mp.StartGame();
}
catch (Exception e)
{
new winReport(this, "由于插件引起的自定按钮加载错误\n" + e.ToString());
}
}
public static void RunDIY(string content)
{

View File

@ -28,7 +28,6 @@ namespace VPet_Simulator.Windows
{
private NotifyIcon notifyIcon;
public System.Timers.Timer AutoSaveTimer = new System.Timers.Timer();
public ITalkBox TalkBox;
public MainWindow()
{
//判断是不是Steam用户,因为本软件会发布到Steam
@ -108,6 +107,13 @@ namespace VPet_Simulator.Windows
private void Restart_Closed(object sender, EventArgs e)
{
Save();
try
{
//关闭所有插件
foreach (MainPlugin mp in Plugins)
mp.EndGame();
}
catch { }
Main?.Dispose();
notifyIcon?.Dispose();
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
@ -127,7 +133,7 @@ namespace VPet_Simulator.Windows
new Tuple<string, Helper.SayType>("长按脑袋拖动桌宠到你喜欢的任意位置", Helper.SayType.Serious),
new Tuple<string, Helper.SayType>("欢迎加入 虚拟主播模拟器群 430081239", Helper.SayType.Shining),
};
private long lastclicktime;
public long lastclicktime { get; set; }
public void GameLoad()
{
//加载所有MOD
@ -191,14 +197,24 @@ namespace VPet_Simulator.Windows
if (!Set["CGPT"][(gbol)"enable"] && IsSteamUser)
{
TalkBox = new TalkBox(this);
Main.ToolBar.MainGrid.Children.Add(TalkBox.This);
Main.ToolBar.MainGrid.Children.Add(TalkBox);
}
else if (Set["CGPT"][(gbol)"enable"])
{
TalkBox = new TalkBoxAPI(this);
Main.ToolBar.MainGrid.Children.Add(TalkBox.This);
Main.ToolBar.MainGrid.Children.Add(TalkBox);
}
LoadingText.Content = "正在加载游戏";
try
{
//加载游戏创意工坊插件
foreach (MainPlugin mp in Plugins)
mp.StartGame();
}
catch (Exception e)
{
new winReport(this, "由于插件引起的游戏启动错误\n" + e.ToString());
}
Main.DefaultClickAction = () =>
{
if (new TimeSpan(DateTime.Now.Ticks - lastclicktime).TotalSeconds > 20)
@ -294,16 +310,23 @@ namespace VPet_Simulator.Windows
Save();
}
public Main Main;
private void Window_Closed(object sender, EventArgs e)
{
Save();
try
{
//关闭所有插件
foreach (MainPlugin mp in Plugins)
mp.EndGame();
}
catch { }
Main?.Dispose();
notifyIcon?.Dispose();
System.Environment.Exit(0);
}
//public void DEBUGValue()
//{
// Dispatcher.Invoke(() =>

View File

@ -116,7 +116,6 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Function\ITalkBox.cs" />
<Compile Include="WinDesign\TalkBoxAPI.xaml.cs">
<DependentUpon>TalkBoxAPI.xaml</DependentUpon>
</Compile>
@ -127,7 +126,6 @@
<DependentUpon>DIYViewer.xaml</DependentUpon>
</Compile>
<Compile Include="Function\CoreMOD.cs" />
<Compile Include="Function\Setting.cs" />
<Compile Include="MainWindow.cs" />
<Compile Include="WinDesign\TalkBox.xaml.cs">
<DependentUpon>TalkBox.xaml</DependentUpon>
@ -237,6 +235,10 @@
<Project>{7bd4cb1d-c8f3-4349-9bf0-cd11789130ba}</Project>
<Name>VPet-Simulator.Core</Name>
</ProjectReference>
<ProjectReference Include="..\VPet-Simulator.Windows.Interface\VPet-Simulator.Windows.Interface.csproj">
<Project>{DCAD838A-1A02-4BDF-962C-FD47C6006D28}</Project>
<Name>VPet-Simulator.Windows.Interface</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Resource Include="vpeticon.ico">

View File

@ -31,13 +31,11 @@ namespace VPet_Simulator.Windows
/// <summary>
/// MessageBar.xaml 的交互逻辑
/// </summary>
public partial class TalkBox : UserControl, ITalkBox
public partial class TalkBox : UserControl
{
Main m;
Setting set;
public UIElement This => this;
public TalkBox(MainWindow mw)
{
InitializeComponent();

View File

@ -31,13 +31,10 @@ namespace VPet_Simulator.Windows
/// <summary>
/// MessageBar.xaml 的交互逻辑
/// </summary>
public partial class TalkBoxAPI : UserControl, ITalkBox
public partial class TalkBoxAPI : UserControl
{
Main m;
MainWindow mw;
public UIElement This => this;
public TalkBoxAPI(MainWindow mw)
{
InitializeComponent();

View File

@ -525,6 +525,10 @@
BorderBrush="{DynamicResource PrimaryDarker}"
pu:ProgressBarHelper.IsPercentVisible="True"
Foreground="{DynamicResource DARKPrimary}" BorderThickness="2" />
<TextBlock x:Name="ButtonSetting" HorizontalAlignment="Left" Margin="0,2,0,0"
TextWrapping="Wrap" Text="MOD设置" VerticalAlignment="Top" FontSize="14"
Foreground="DimGray" TextDecorations="Underline" Cursor="Hand" MouseDown="ButtonSetting_MouseDown"
/>
<Button x:Name="ButtonAllow" Content="启用代码插件" HorizontalAlignment="Left"
VerticalAlignment="Top" Background="#FFFF2C2C" Foreground="White"
FontSize="12" ToolTip="启用该模组的代码内容,不能保证系统安全性" Click="ButtonAllow_Click" />

View File

@ -249,6 +249,7 @@ namespace VPet_Simulator.Windows
GameHave.Text = mod.Content.Trim('\n');
ButtonAllow.Visibility = mod.SuccessLoad ? Visibility.Collapsed : Visibility.Visible;
ButtonSetting.Visibility = ButtonAllow.Visibility;
}
private void FullScreenBox_Check(object sender, RoutedEventArgs e)
{
@ -445,7 +446,13 @@ namespace VPet_Simulator.Windows
private void ButtonAllow_Click(object sender, RoutedEventArgs e)
{
if (MessageBoxX.Show($"是否启用 {mod.Name} 的代码插件?\n一经启用,该插件将会允许访问该系统(包括外部系统)的所有数据\n如果您不确定,请先使用杀毒软件查杀检查",
$"启用 {mod.Name} 的代码插件?", MessageBoxButton.YesNo, MessageBoxIcon.Warning) == MessageBoxResult.Yes)
{
mw.Set.PassMod(mod.Name);
ShowMod((string)LabelModName.Content);
ButtonRestart.Visibility = Visibility.Visible;
}
}
private void ButtonRestart_Click(object sender, RoutedEventArgs e)
@ -698,18 +705,23 @@ namespace VPet_Simulator.Windows
BtnCGPTReSet.Content = "打开 ChatGPT API 设置";
BtnCGPTReSet.IsEnabled = true;
if (mw.TalkBox != null)
mw.Main.ToolBar.MainGrid.Children.Remove(mw.TalkBox.This);
mw.Main.ToolBar.MainGrid.Children.Remove(mw.TalkBox);
mw.TalkBox = new TalkBoxAPI(mw);
mw.Main.ToolBar.MainGrid.Children.Add(mw.TalkBox.This);
mw.Main.ToolBar.MainGrid.Children.Add(mw.TalkBox);
}
else
{
BtnCGPTReSet.Content = "初始化桌宠聊天程序";
if (mw.TalkBox != null)
mw.Main.ToolBar.MainGrid.Children.Remove(mw.TalkBox.This);
mw.Main.ToolBar.MainGrid.Children.Remove(mw.TalkBox);
mw.TalkBox = new TalkBox(mw);
mw.Main.ToolBar.MainGrid.Children.Add(mw.TalkBox.This);
mw.Main.ToolBar.MainGrid.Children.Add(mw.TalkBox);
}
}
private void ButtonSetting_MouseDown(object sender, MouseButtonEventArgs e)
{
}
}
}

View File

@ -23,10 +23,10 @@ Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x64.ActiveCfg = Debug|x64
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x64.Build.0 = Debug|x64
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x86.ActiveCfg = Debug|x86
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x86.Build.0 = Debug|x86
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x64.ActiveCfg = Debug|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x64.Build.0 = Debug|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x86.ActiveCfg = Debug|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Debug|x86.Build.0 = Debug|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Release|Any CPU.Build.0 = Release|Any CPU
{7BD4CB1D-C8F3-4349-9BF0-CD11789130BA}.Release|x64.ActiveCfg = Release|x64