add js script page

This commit is contained in:
辉鸭蛋
2024-08-17 16:02:54 +08:00
parent b47ce6f529
commit c6680ced12
9 changed files with 249 additions and 15 deletions

View File

@@ -87,7 +87,7 @@ public partial class App : Application
services.AddView<HotKeyPage, HotKeyPageViewModel>();
services.AddView<NotificationSettingsPage, NotificationSettingsPageViewModel>();
services.AddView<KeyMouseRecordPage, KeyMouseRecordPageViewModel>();
// services.AddView<DispatcherPage, DispatcherPageViewModel>();
services.AddView<JsListPage, JsListViewModel>();
// My Services
services.AddSingleton<TaskTriggerDispatcher>();

View File

@@ -11,8 +11,6 @@ public partial class ScriptGroupProject : ObservableObject
[ObservableProperty]
private int _index;
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string FolderName { get; set; } = string.Empty;
@@ -48,7 +46,6 @@ public partial class ScriptGroupProject : ObservableObject
public ScriptGroupProject(ScriptProject project)
{
Id = project.Manifest.Id;
Name = project.Manifest.Name;
FolderName = project.FolderName;
Status = "Enabled";

View File

@@ -10,8 +10,6 @@ namespace BetterGenshinImpact.Core.Script.Project;
public class Manifest
{
public int ManifestVersion { get; set; } = 1;
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Version { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
@@ -28,11 +26,6 @@ public class Manifest
public void Validate(string path)
{
if (string.IsNullOrWhiteSpace(Id))
{
throw new Exception("manifest.json: id is not supported.");
}
if (string.IsNullOrWhiteSpace(Name))
{
throw new Exception("manifest.json: name is required.");

View File

@@ -1,6 +1,5 @@
{
"manifest_version": 1,
"id": "bgi_crystalfly",
"name": "自动采集晶蝶",
"version": "1.0",
"description": "BetterGI自带脚本自动采集离传送点较近的晶蝶。",

View File

@@ -96,7 +96,7 @@
<!-- WebAsset24 | DocumentJs16 | Script16 -->
<ui:NavigationViewItem Content="脚本"
NavigationCacheMode="Enabled"
TargetPageType="{x:Type pages:ScriptControlPage}">
TargetPageType="{x:Type pages:JsListPage}">
<ui:NavigationViewItem.Icon>
<ui:SymbolIcon Symbol="DocumentJs16" />
</ui:NavigationViewItem.Icon>

View File

@@ -0,0 +1,116 @@
<Page x:Class="BetterGenshinImpact.View.Pages.JsListPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:pages="clr-namespace:BetterGenshinImpact.ViewModel.Pages"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Title="JsListPage"
d:DataContext="{d:DesignInstance Type=pages:JsListViewModel}"
d:DesignHeight="600"
d:DesignWidth="800"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
FontFamily="{StaticResource TextThemeFontFamily}"
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
mc:Ignorable="d">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/View/Controls/Style/ListViewEx.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
<Grid Margin="42,16,42,12">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="400" />
</Grid.RowDefinitions>
<ui:TextBlock Grid.Row="0"
Margin="0,0,0,8"
FontTypography="BodyStrong"
Text="自定义 Javascript 脚本(开发中功能)" />
<ui:TextBlock Grid.Row="1"
Margin="0,0,0,8"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="可以通过 Javascript 调用 BetterGI 在原神中的各项能力"
TextWrapping="Wrap" />
<StackPanel Grid.Row="2" Orientation="Horizontal">
<ui:Button Command="{Binding OpenScriptsFolderCommand}"
Content="打开脚本目录"
Icon="{ui:SymbolIcon FolderOpen24}" />
</StackPanel>
<Separator Grid.Row="3"
Height="10"
Opacity="0" />
<Grid Grid.Row="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="80" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Grid x:Name="Col1" Grid.Column="0" />
<Grid x:Name="Col2" Grid.Column="1" />
<Grid x:Name="Col3" Grid.Column="2" />
<Grid x:Name="Col4" Grid.Column="3" />
<Grid x:Name="Col5" Grid.Column="4" />
</Grid>
<ui:ListView Grid.Row="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding ScriptItems}"
SelectionMode="Single">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderDarkStyle}">
<GridViewColumn Width="{Binding ElementName=Col1, Path=ActualWidth}"
DisplayMemberBinding="{Binding FolderName}"
Header="目录" />
<GridViewColumn Width="{Binding ElementName=Col2, Path=ActualWidth}"
DisplayMemberBinding="{Binding Manifest.Name}"
Header="名称" />
<GridViewColumn Width="{Binding ElementName=Col3, Path=ActualWidth}"
DisplayMemberBinding="{Binding Manifest.Version}"
Header="版本" />
<GridViewColumn Width="{Binding ElementName=Col4, Path=ActualWidth}" Header="操作">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ui:Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:JsListPage}}, Path=DataContext.StartRunCommand}"
CommandParameter="{Binding ProjectPath}"
Content="执行脚本"
Icon="{ui:SymbolIcon Play24}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding OpenScriptProjectFolderCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"
Header="打开目录" />
</ContextMenu>
</ListBox.ContextMenu>
<ListView.Style>
<Style TargetType="{x:Type ListView}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
</ListView.Style>
</ui:ListView>
</Grid>
</Page>

View File

@@ -0,0 +1,19 @@
using BetterGenshinImpact.ViewModel;
using BetterGenshinImpact.ViewModel.Pages;
using System.Windows.Controls;
namespace BetterGenshinImpact.View.Pages;
/// <summary>
/// JsListPage.xaml 的交互逻辑
/// </summary>
public partial class JsListPage : Page
{
private JsListViewModel ViewModel { get; }
public JsListPage(JsListViewModel viewModel)
{
DataContext = ViewModel = viewModel;
InitializeComponent();
}
}

View File

@@ -0,0 +1,112 @@
using System;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using BetterGenshinImpact.Core.Script.Project;
using Wpf.Ui;
using Wpf.Ui.Controls;
using static Vanara.PInvoke.Gdi32;
using Wpf.Ui.Violeta.Controls;
using CommunityToolkit.Mvvm.Input;
using BetterGenshinImpact.GameTask.Model.Enum;
using BetterGenshinImpact.GameTask;
using System.Threading.Tasks;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class JsListViewModel : ObservableObject, INavigationAware, IViewModel
{
private readonly ILogger<JsListViewModel> _logger = App.GetLogger<JsListViewModel>();
private readonly string scriptPath = Global.Absolute("Script");
[ObservableProperty]
private ObservableCollection<ScriptProject> _scriptItems = [];
private ISnackbarService _snackbarService;
public JsListViewModel(ISnackbarService snackbarService)
{
_snackbarService = snackbarService;
}
private void InitScriptListViewData()
{
_scriptItems.Clear();
var directoryInfos = LoadScriptFolder(scriptPath);
foreach (var f in directoryInfos)
{
try
{
_scriptItems.Add(new ScriptProject(f.Name));
}
catch (Exception e)
{
Toast.Warning($"脚本 {f.Name} 载入失败:{e.Message}");
}
}
}
private IEnumerable<DirectoryInfo> LoadScriptFolder(string folder)
{
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
var di = new DirectoryInfo(folder);
return di.GetDirectories();
}
public void OnNavigatedTo()
{
InitScriptListViewData();
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public void OnOpenScriptsFolder()
{
Process.Start("explorer.exe", scriptPath);
}
[RelayCommand]
public void OnOpenScriptProjectFolder(ScriptProject? item)
{
if (item == null)
{
return;
}
Process.Start("explorer.exe", item.ProjectPath);
}
[RelayCommand]
public async Task OnStartRun(string name)
{
_logger.LogInformation("重放开始:{Name}", name);
try
{
// var s = await File.ReadAllTextAsync(Path.Combine(scriptPath, name));
//
// await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage)
// .RunAsync(async () => await KeyMouseMacroPlayer.PlayMacro(s, CancellationContext.Instance.Cts.Token));
}
catch (Exception e)
{
_logger.LogError(e, "重放脚本时发生异常");
}
finally
{
_logger.LogInformation("重放结束:{Name}", name);
}
}
}

View File

@@ -1,7 +1,6 @@
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Core.Recorder;
using BetterGenshinImpact.Core.Script;
using BetterGenshinImpact.Core.Script.Group;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.GameTask.Model.Enum;
using BetterGenshinImpact.Model;
@@ -19,7 +18,6 @@ using System.Threading.Tasks;
using Wpf.Ui;
using Wpf.Ui.Controls;
using Wpf.Ui.Violeta.Controls;
using MessageBox = System.Windows.MessageBox;
namespace BetterGenshinImpact.ViewModel.Pages;