-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSimulateViewModel.cs
More file actions
161 lines (141 loc) · 5.95 KB
/
Copy pathSimulateViewModel.cs
File metadata and controls
161 lines (141 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GeneralUpdate.Tools.Configuration;
using GeneralUpdate.Tools.Models;
using GeneralUpdate.Tools.Services;
namespace GeneralUpdate.Tools.ViewModels;
public partial class SimulateViewModel : ViewModelBase
{
private readonly LocalizationService _loc = LocalizationService.Instance;
private readonly SimulationService _sim = new();
private readonly ReportGeneratorService _report = new();
private readonly AppConfig _config;
public SimulateConfigModel Config { get; } = new();
[ObservableProperty] private bool _isRunning;
[ObservableProperty] private string _status;
[ObservableProperty] private string _startButtonText;
[ObservableProperty] private ObservableCollection<string> _log = new();
public ObservableCollection<PlatformItem> Platforms { get; } = new()
{
new(1, "Windows"),
new(2, "Linux")
};
public ObservableCollection<AppTypeItem> AppTypes { get; } = new()
{
new(1, "ClientApp"),
new(2, "UpgradeApp")
};
public SimulateViewModel(AppConfig config)
{
_config = config;
// Restore path memory
Config.AppDirectory = config.LastSimulateAppDir;
Config.PatchFilePath = config.LastSimulatePatchFile;
// Restore simulation config
Config.ServerPort = int.TryParse(config.SimulationServerPort, out var port) ? port : 5000;
Config.Platform = config.SimulationPlatformType == "Linux" ? 2 : 1;
Config.AppType = config.SimulationAppType == "UpgradeApp" ? 2 : 1;
_status = _loc["Patch.Ready"];
_startButtonText = _loc["Sim.Start"];
}
public int PlatformIndex
{
get => Config.Platform == 2 ? 1 : 0;
set => Config.Platform = value == 1 ? 2 : 1;
}
public int AppTypeIndex
{
get => Config.AppType == 2 ? 1 : 0;
set => Config.AppType = value == 1 ? 2 : 1;
}
async Task<string?> PickFolder(string title)
{
var tl = Avalonia.Controls.TopLevel.GetTopLevel(
(Avalonia.Application.Current?.ApplicationLifetime as Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime)?.MainWindow);
if (tl == null) return null;
var r = await tl.StorageProvider.OpenFolderPickerAsync(
new Avalonia.Platform.Storage.FolderPickerOpenOptions { Title = title, AllowMultiple = false });
return r.Count > 0 ? r[0].Path.LocalPath : null;
}
async Task<string?> PickFile(string title)
{
var tl = Avalonia.Controls.TopLevel.GetTopLevel(
(Avalonia.Application.Current?.ApplicationLifetime as Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime)?.MainWindow);
if (tl == null) return null;
var r = await tl.StorageProvider.OpenFilePickerAsync(
new Avalonia.Platform.Storage.FilePickerOpenOptions { Title = title, AllowMultiple = false });
return r.Count > 0 ? r[0].Path.LocalPath : null;
}
[RelayCommand]
async Task SelectAppDir()
{
var p = await PickFolder(_loc["Sim.SelectAppDir"]);
if (p != null)
{
Config.AppDirectory = p;
_config.LastSimulateAppDir = p;
ConfigService.SafeFireAndForgetSave(ConfigServiceSingleton.Instance);
}
}
[RelayCommand]
async Task SelectPatch()
{
var p = await PickFile(_loc["Sim.SelectPatch"]);
if (p != null)
{
Config.PatchFilePath = p;
_config.LastSimulatePatchFile = p;
ConfigService.SafeFireAndForgetSave(ConfigServiceSingleton.Instance);
}
}
[RelayCommand]
async Task StartSimulation()
{
if (string.IsNullOrWhiteSpace(Config.AppDirectory)) { Status = _loc["Sim.ValidateDirs"]; return; }
if (string.IsNullOrWhiteSpace(Config.PatchFilePath)) { Status = _loc["Sim.ValidateDirs"]; return; }
if (!SemverValidator.IsValid(Config.CurrentVersion))
{
Status = _loc.T("Sim.InvalidVersion", Config.CurrentVersion);
return;
}
if (!SemverValidator.IsValid(Config.TargetVersion))
{
Status = _loc.T("Sim.InvalidVersion", Config.TargetVersion);
return;
}
// Persist simulation settings
_config.SimulationServerPort = Config.ServerPort.ToString();
_config.SimulationPlatformType = Config.Platform == 2 ? "Linux" : "Windows";
_config.SimulationAppType = Config.AppType == 2 ? "UpgradeApp" : "ClientApp";
ConfigService.SafeFireAndForgetSave(ConfigServiceSingleton.Instance);
IsRunning = true; StartButtonText = "⏳ Running..."; Log.Clear(); Status = _loc["Sim.Starting"];
try
{
var progress = new Progress<string>(L);
var result = await _sim.RunAsync(Config, progress);
if (result.Success)
Status = _loc.T("Sim.Completed", result.Elapsed.TotalSeconds);
else
Status = _loc.T("Sim.Failed", result.ErrorMessage ?? "unknown");
L($"Result: {(result.Success ? "PASS" : "FAIL")}");
foreach (var note in result.Notes) L($" Note: {note}");
var reportPath = await _report.GenerateAsync(Config, result, Config.AppDirectory);
L(_loc.T("Sim.Report", reportPath));
}
catch (Exception ex)
{
Status = $"Error: {ex.Message}";
L($"FATAL: {ex}");
var failResult = new SimulationResult { Success = false, ErrorMessage = ex.Message };
var reportPath = await _report.GenerateAsync(Config, failResult, Config.AppDirectory);
L(_loc.T("Sim.Report", reportPath));
}
finally { IsRunning = false; StartButtonText = _loc["Sim.Start"]; }
}
void L(string msg) => Log.Add($"[{DateTime.Now:HH:mm:ss}] {msg}");
}