-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedSettings.axaml.cs
116 lines (110 loc) · 4.02 KB
/
AdvancedSettings.axaml.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Runtime.InteropServices.JavaScript;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using MsBox.Avalonia;
using MsBox.Avalonia.Dto;
using MsBox.Avalonia.Models;
using SkiaSharp;
namespace MoSpeedUI;
public partial class AdvancedSettings : Window
{
public AdvancedSettings()
{
this.Width = 800;
Height = 250;
InitializeComponent();
if (!string.IsNullOrWhiteSpace(MainWindow.CompileConfig.ExtendedArguments))
{
AdvBox.Text = MainWindow.CompileConfig.ExtendedArguments;
}
this.Closing += ((sender, args) => { MainWindow.CompileConfig.ExtendedArguments = AdvBox?.Text!; });
}
private void Button_OnClick(object? s, RoutedEventArgs e)
{
MainWindow.CompileConfig.ExtendedArguments = AdvBox?.Text!;
Close();
}
private void WikiBtn_OnClick(object? sender, RoutedEventArgs e)
{
Process p = new();
p.StartInfo.FileName = Lang.Resources.WikiUrl;
p.StartInfo.UseShellExecute = true;
p.Start();
}
private async void UpdateBtn_OnClick(object? sender, RoutedEventArgs e)
{
UpdateBtn.IsEnabled = false;
string tmpDir = Path.Join(Path.GetTempPath(), Path.GetRandomFileName());
string basePath = Shared.AppConfiguration.MoSpeedPath.Replace("\\dist", "\\").Replace("/dist", "/");
Directory.CreateDirectory(tmpDir);
string outputPath = Path.Join(tmpDir, "mospeed.zip");
try
{
using var client = new HttpClient();
using var response =
await client.GetAsync("https://github.com/EgonOlsen71/basicv2/archive/refs/heads/master.zip",
HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
await using var contentStream = await response.Content.ReadAsStreamAsync();
await using var fileStream =
new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
var buffer = new byte[8192];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead));
totalBytesRead += bytesRead;
}
fileStream.Close();
await Task.Run(() =>
{
ZipFile.ExtractToDirectory(Path.Join(tmpDir, "mospeed.zip"),
tmpDir, true);
});
try
{
Directory.Delete(basePath, true);
}
catch{}
Shared.CopyFilesRecursively(Path.Join(tmpDir, "basicv2-master"),
basePath);
}
catch (Exception ex)
{
Console.WriteLine(ex + "//" + ex.Message);
var box = MessageBoxManager.GetMessageBoxCustom(new MessageBoxCustomParams
{
ContentMessage = Lang.Resources.MSDownloadError + $" {ex.Message}",
ButtonDefinitions = new List<ButtonDefinition>
{
new ButtonDefinition { Name = "Ok" }
},
Icon = MsBox.Avalonia.Enums.Icon.Error
});
await box.ShowAsPopupAsync(this);
return;
}
finally
{
UpdateBtn.IsEnabled = true;
}
var sBox = MessageBoxManager.GetMessageBoxCustom(new MessageBoxCustomParams
{
ContentMessage = Lang.Resources.MSDownloadSuccess + $" {Shared.AppConfiguration.MoSpeedPath}",
ButtonDefinitions = new List<ButtonDefinition>
{
new ButtonDefinition { Name = "Ok" }
},
Icon = MsBox.Avalonia.Enums.Icon.Success
});
await sBox.ShowAsPopupAsync(this);
}
}