mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-05-23 09:55:48 +08:00
新增任务节点对话框,支持选择JS脚本类型任务,优化用户操作体验
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BetterGenshinImpact.Core.Script;
|
||||
using BetterGenshinImpact.Core.Script.Project;
|
||||
using BetterGenshinImpact.ViewModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BetterGenshinImpact.ViewModel.Windows;
|
||||
|
||||
public partial class JsScriptSelectionViewModel : ViewModel
|
||||
{
|
||||
private readonly ILogger<JsScriptSelectionViewModel> _logger = App.GetLogger<JsScriptSelectionViewModel>();
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<JsScriptInfo> _jsScripts = [];
|
||||
|
||||
[ObservableProperty]
|
||||
private JsScriptInfo? _selectedScript;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _readmeContent = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _mainJsContent = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private int _selectedTabIndex = 0;
|
||||
|
||||
public JsScriptSelectionViewModel()
|
||||
{
|
||||
LoadJsScripts();
|
||||
}
|
||||
|
||||
private void LoadJsScripts()
|
||||
{
|
||||
try
|
||||
{
|
||||
var jsPath = Path.Combine(ScriptRepoUpdater.CenterRepoPath, "repo", "js");
|
||||
if (!Directory.Exists(jsPath))
|
||||
{
|
||||
_logger.LogWarning($"JS脚本目录不存在: {jsPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
var scriptDirectories = Directory.GetDirectories(jsPath);
|
||||
var scripts = new ObservableCollection<JsScriptInfo>();
|
||||
|
||||
foreach (var scriptDir in scriptDirectories)
|
||||
{
|
||||
try
|
||||
{
|
||||
var manifestPath = Path.Combine(scriptDir, "manifest.json");
|
||||
if (File.Exists(manifestPath))
|
||||
{
|
||||
var manifestContent = File.ReadAllText(manifestPath);
|
||||
var manifest = Manifest.FromJson(manifestContent);
|
||||
|
||||
var scriptInfo = new JsScriptInfo
|
||||
{
|
||||
FolderName = Path.GetFileName(scriptDir),
|
||||
FolderPath = scriptDir,
|
||||
Manifest = manifest
|
||||
};
|
||||
|
||||
scripts.Add(scriptInfo);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"加载JS脚本失败: {scriptDir}");
|
||||
}
|
||||
}
|
||||
|
||||
JsScripts = scripts;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "加载JS脚本列表失败");
|
||||
}
|
||||
}
|
||||
|
||||
partial void OnSelectedScriptChanged(JsScriptInfo? value)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
// 清空之前的内容
|
||||
ReadmeContent = string.Empty;
|
||||
MainJsContent = string.Empty;
|
||||
|
||||
// 根据当前选中的标签页加载内容
|
||||
LoadCurrentTabContent();
|
||||
}
|
||||
}
|
||||
|
||||
partial void OnSelectedTabIndexChanged(int value)
|
||||
{
|
||||
if (SelectedScript != null)
|
||||
{
|
||||
LoadCurrentTabContent();
|
||||
}
|
||||
}
|
||||
|
||||
private async void LoadCurrentTabContent()
|
||||
{
|
||||
if (SelectedScript == null) return;
|
||||
|
||||
switch (SelectedTabIndex)
|
||||
{
|
||||
case 0: // README.md
|
||||
if (string.IsNullOrEmpty(ReadmeContent))
|
||||
{
|
||||
await Task.Run(LoadReadmeContent);
|
||||
}
|
||||
break;
|
||||
case 1: // main.js
|
||||
if (string.IsNullOrEmpty(MainJsContent))
|
||||
{
|
||||
await Task.Run(LoadMainJsContent);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadReadmeContent()
|
||||
{
|
||||
if (SelectedScript == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
var readmePath = Path.Combine(SelectedScript.FolderPath, "README.md");
|
||||
if (File.Exists(readmePath))
|
||||
{
|
||||
ReadmeContent = File.ReadAllText(readmePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadmeContent = "README.md 文件不存在";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "加载README.md失败");
|
||||
ReadmeContent = $"加载README.md失败: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadMainJsContent()
|
||||
{
|
||||
if (SelectedScript == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
var mainJsPath = Path.Combine(SelectedScript.FolderPath, SelectedScript.Manifest.Main);
|
||||
if (File.Exists(mainJsPath))
|
||||
{
|
||||
MainJsContent = File.ReadAllText(mainJsPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
MainJsContent = "main.js 文件不存在";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "加载main.js失败");
|
||||
MainJsContent = $"加载main.js失败: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void RefreshScripts()
|
||||
{
|
||||
LoadJsScripts();
|
||||
}
|
||||
}
|
||||
|
||||
public class JsScriptInfo
|
||||
{
|
||||
public string FolderName { get; set; } = string.Empty;
|
||||
public string FolderPath { get; set; } = string.Empty;
|
||||
public Manifest Manifest { get; set; } = new();
|
||||
|
||||
public string DisplayName => $"{FolderName} - {Manifest.Name}";
|
||||
public string Description => Manifest.ShortDescription;
|
||||
}
|
||||
Reference in New Issue
Block a user