Skip to content

Commit

Permalink
✨ 自动检查更新
Browse files Browse the repository at this point in the history
  • Loading branch information
DaiYu-233 committed Aug 27, 2024
1 parent e72f1d0 commit 33dcd02
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 244 deletions.
2 changes: 2 additions & 0 deletions YMCL.Main/Public/Classes/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Setting
{
public string Language { get; set; } = "Unset";
public string MinecraftFolder { get; set; }
public string SkipUpdateVersion { get; set; } = string.Empty;
public bool EnableAutoCheckUpdate { get; set; } = true;
public double MaximumDownloadThread { get; set; } = 64;
public bool IsCompleteJavaInitialize { get; set; }
public bool IsCompleteMinecraftFolderInitialize { get; set; }
Expand Down
1 change: 1 addition & 0 deletions YMCL.Main/Public/Const.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public abstract class String
public static string PluginDataPath { get; } = Path.Combine(UserDataRootPath, "YMCL.Plugin.DaiYu");
public static string PluginFolderPath { get; } = Path.Combine(UserDataRootPath, "Plugin");
public static string TempFolderPath { get; } = Path.Combine(UserDataRootPath, "Temp");
public static string UpdateFolderPath { get; } = Path.Combine(UserDataRootPath, "Update");
public static string VersionSettingFileName { get; } = "YMCLSetting.DaiYu";

public static string GithubUpdateApiUrl { get; } =
Expand Down
18 changes: 18 additions & 0 deletions YMCL.Main/Public/Langs/MainLang.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions YMCL.Main/Public/Langs/MainLang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,10 @@ sudo apt install vlc</value>
<data name="DeskLyricSize" xml:space="preserve">
<value>桌面歌词大小</value>
</data>
<data name="AutoUpdate" xml:space="preserve">
<value>自动更新</value>
</data>
<data name="SkipThisVersion" xml:space="preserve">
<value>跳过此版本</value>
</data>
</root>
8 changes: 4 additions & 4 deletions YMCL.Main/Public/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public static void RestartApp()
Environment.Exit(0);
}

public static async Task<(bool, string)> CheckUpdateAsync()
public static async Task<(bool, string, string)> CheckUpdateAsync()
{
try
{
Expand All @@ -303,12 +303,12 @@ public static void RestartApp()
httpClient.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.54");
var githubApiJson = JArray.Parse(await httpClient.GetStringAsync(Const.String.GithubUpdateApiUrl));
var apiVersion = (string)githubApiJson[0]["name"];
return (apiVersion != version, $"{apiVersion!}\n\n{(string)githubApiJson[0]["html_url"]}");
var apiVersion = (string)githubApiJson[0]["name"]!;
return (apiVersion != version, apiVersion, $"{apiVersion!}\n\n{(string)githubApiJson[0]["html_url"]}");
}
catch
{
return (false, string.Empty);
return (false, string.Empty, string.Empty);
}
}

Expand Down
11 changes: 6 additions & 5 deletions YMCL.Main/Public/Styles/DarkTheme.axaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush Color="#2c2c2c" x:Key="1x" />
<SolidColorBrush Color="#262626" x:Key="2x" />
<SolidColorBrush Color="#ffffff" x:Key="TextColor" />
<SolidColorBrush Color="#242424" x:Key="TitleBarColor" />
<SolidColorBrush Color="#7e7e7e" x:Key="GrayColor" />
<SolidColorBrush x:Key="1x" Color="#2c2c2c" />
<SolidColorBrush x:Key="2x" Color="#262626" />
<SolidColorBrush x:Key="TextColor" Color="#ffffff" />
<SolidColorBrush x:Key="TitleBarColor" Color="#242424" />
<SolidColorBrush x:Key="GrayColor" Color="#7e7e7e" />
<SolidColorBrush x:Key="BorderBrushColor" Color="#e1e9f0" />
</ResourceDictionary>
11 changes: 6 additions & 5 deletions YMCL.Main/Public/Styles/LightTheme.axaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush Color="#FFE9F6FF" x:Key="1x" />
<SolidColorBrush Color="#f6fafd" x:Key="2x" />
<SolidColorBrush Color="#333333" x:Key="TextColor" />
<SolidColorBrush Color="#e7f5ff" x:Key="TitleBarColor" />
<SolidColorBrush Color="#7e7e7e" x:Key="GrayColor" />
<SolidColorBrush x:Key="1x" Color="#FFE9F6FF" />
<SolidColorBrush x:Key="2x" Color="#f6fafd" />
<SolidColorBrush x:Key="TextColor" Color="#333333" />
<SolidColorBrush x:Key="TitleBarColor" Color="#e7f5ff" />
<SolidColorBrush x:Key="GrayColor" Color="#7e7e7e" />
<SolidColorBrush x:Key="BorderBrushColor" Color="#e1e9f0" />
</ResourceDictionary>
1 change: 1 addition & 0 deletions YMCL.Main/Views/Initialize/InitializeWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private void Init()
Method.IO.TryCreateFolder(Const.String.UserDataRootPath);
Method.IO.TryCreateFolder(Const.String.PluginFolderPath);
Method.IO.TryCreateFolder(Const.String.TempFolderPath);
Method.IO.TryCreateFolder(Const.String.UpdateFolderPath);
if (!File.Exists(Const.String.SettingDataPath))
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(new Setting(), Formatting.Indented));
if (!File.Exists(Const.String.MinecraftFolderDataPath) || JsonConvert
Expand Down
11 changes: 8 additions & 3 deletions YMCL.Main/Views/Main/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ private void EventBinding()
else if (Const.Data.Platform == Platform.Linux)
{
setting.IsAlreadyWrittenIntoTheUrlScheme = true;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
File.WriteAllText(Const.String.SettingDataPath,
JsonConvert.SerializeObject(setting, Formatting.Indented));
}
}

await Task.Delay(200);
_ = Const.Window.main.settingPage.launcherSettingPage.AutoUpdate();
};
Activated += (_, _) =>
{
Expand Down Expand Up @@ -188,7 +192,7 @@ private void EventBinding()
public void LoadWindow()
{
Method.IO.ClearFolder(Const.String.TempFolderPath);

SystemDecorations = SystemDecorations.Full;

var setting = Const.Data.Setting;
Expand Down Expand Up @@ -221,7 +225,8 @@ public void LoadWindow()
if (setting.CustomHomePage == CustomHomePageWay.Local)
try
{
var c = (Control)AvaloniaRuntimeXamlLoader.Load(File.ReadAllText(Const.String.CustomHomePageXamlDataPath));
var c = (Control)AvaloniaRuntimeXamlLoader.Load(
File.ReadAllText(Const.String.CustomHomePageXamlDataPath));
launchPage.CustomPageRoot.Child = c;
}
catch (Exception ex)
Expand Down
22 changes: 18 additions & 4 deletions YMCL.Main/Views/Main/Pages/Launch/LaunchPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public partial class LaunchPage : UserControl
private bool _firstOpenVersionList = true;
private bool _firstOpenVersionSetting = true;
private bool _shouldCloseVersuionList;
private bool _isSelectioningVersionFolder = false;

private List<string> minecraftFolders =
JsonConvert.DeserializeObject<List<string>>(File.ReadAllText(Const.String.MinecraftFolderDataPath));
Expand Down Expand Up @@ -80,7 +81,8 @@ private void BindingEvent()
}
else
{
LaunchConsoleRoot.Opacity = (double)Application.Current.Resources["Opacity"]!;;
LaunchConsoleRoot.Opacity = (double)Application.Current.Resources["Opacity"]!;
;
LaunchConsoleRoot.IsVisible = true;
}
};
Expand All @@ -103,6 +105,7 @@ private void BindingEvent()
MinecraftFolderComboBox.SelectedItem.ToString() == setting.MinecraftFolder) return;
setting.MinecraftFolder = MinecraftFolderComboBox.SelectedItem.ToString();
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
_isSelectioningVersionFolder = true;
_shouldCloseVersuionList = false;
LoadVersions();
VersionListView.SelectedIndex = 0;
Expand Down Expand Up @@ -165,7 +168,16 @@ private void BindingEvent()
}
};
CloseVersionListBtn.Click += (s, e) => { CloseVersionList(); };
VersionListView.PointerEntered += (s, e) => { _shouldCloseVersuionList = true; };
VersionListView.PointerEntered += async (s, e) =>
{
if (_isSelectioningVersionFolder)
{
await Task.Delay(200);
_isSelectioningVersionFolder = false;
}

_shouldCloseVersuionList = true;
};
VersionListView.SelectionChanged += async (s, e) =>
{
if (VersionListView.SelectedItem != null)
Expand All @@ -176,7 +188,8 @@ private void BindingEvent()
setting.Version = "BedRock";
else
setting.Version = (VersionListView.SelectedItem as GameEntry).Id;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
File.WriteAllText(Const.String.SettingDataPath,
JsonConvert.SerializeObject(setting, Formatting.Indented));
}

await Task.Delay(100);
Expand Down Expand Up @@ -399,7 +412,8 @@ private void LoadAccounts()
{
AccountComboBox.SelectedItem = AccountComboBox.Items[0];
setting.AccountSelectionIndex = 0;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
File.WriteAllText(Const.String.SettingDataPath,
JsonConvert.SerializeObject(setting, Formatting.Indented));
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
<TextBlock
Name="MaximumDownloadThreadText"
Width="65"
Margin="6.5,0,6.5,0"
Margin="3,0,3,0"
VerticalAlignment="Center"
DockPanel.Dock="Right"
FontFamily="{DynamicResource Font}"
Expand All @@ -139,6 +139,29 @@
Message="{x:Static properties:MainLang.DownloadThreadTooBigTip}"
Opacity="{DynamicResource Opacity}"
Severity="Warning" />
<Border
Height="40"
Padding="6.5"
Background="{DynamicResource 1x}"
CornerRadius="5"
Opacity="{DynamicResource Opacity}">
<DockPanel>
<TextBlock
Margin="10,0,20,0"
VerticalAlignment="Center"
FontFamily="{DynamicResource Font}"
FontSize="14"
Text="{x:Static properties:MainLang.AutoUpdate}" />
<ToggleSwitch
Name="AutoUpdateSwitch"
Height="40"
Margin="0,-10,10,0"
HorizontalAlignment="Right"
FontFamily="{DynamicResource Font}"
OffContent="{x:Static properties:MainLang.Off}"
OnContent="{x:Static properties:MainLang.On}" />
</DockPanel>
</Border>
</StackPanel>
</ScrollViewer>
</Border>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,28 @@ private void BindingEvent()
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
Const.String.MusicApiUrl = MusicApiTextBox.Text;
};
AutoUpdateSwitch.Click += (s, e) =>
{
var setting = Const.Data.Setting;
if (AutoUpdateSwitch.IsChecked != setting.ShowGameOutput)
{
setting.EnableAutoCheckUpdate = (bool)AutoUpdateSwitch.IsChecked!;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
}
};
SizeChanged += (_, _) =>
{
if (CustomUpdateUrlTextBox.IsVisible)
CustomUpdateUrlEnableComboBox.Width = 150;
else
CustomUpdateUrlEnableComboBox.Width = CustomUpdateUrlRoot.Bounds.Width - 2 * 6.5 -
CustomUpdateUrlLabel.Bounds.Width - 30;
try
{
if (CustomUpdateUrlTextBox.IsVisible)
CustomUpdateUrlEnableComboBox.Width = 150;
else
CustomUpdateUrlEnableComboBox.Width = CustomUpdateUrlRoot.Bounds.Width - 2 * 6.5 -
CustomUpdateUrlLabel.Bounds.Width - 30;
}
catch
{
}
};
DownloadSourceComboBox.SelectionChanged += (s, e) =>
{
Expand Down Expand Up @@ -101,6 +116,7 @@ private void ControlProperty()
MaximumDownloadThreadSlider.Value = setting.MaximumDownloadThread;
DownloadThreadWarning.IsVisible = MaximumDownloadThreadSlider.Value > 100;
CustomUpdateUrlEnableComboBox.SelectedIndex = setting.EnableCustomUpdateUrl ? 1 : 0;
AutoUpdateSwitch.IsChecked = setting.EnableAutoCheckUpdate;
CustomUpdateUrlTextBox.Text = setting.CustomUpdateUrl;
Const.String.MusicApiUrl = setting.MusicApi;
MusicApiTextBox.Text = setting.MusicApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using FluentAvalonia.UI.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YMCL.Main.Public;
using YMCL.Main.Public.Controls.WindowTask;
using YMCL.Main.Public.Langs;
using Application = Avalonia.Application;

namespace YMCL.Main.Views.Main.Pages.Setting.Pages.Launcher;

Expand All @@ -28,6 +20,28 @@ public LauncherSettingPage()
BindingEvent();
}

public async Task AutoUpdate()
{
if (!Const.Data.Setting.EnableAutoCheckUpdate) return;
var updateAvailable = await Method.Ui.CheckUpdateAsync();
if (!updateAvailable.Item1) return;
if (Const.Data.Setting.SkipUpdateVersion == updateAvailable.Item2) return;
var dialog = await Method.Ui.ShowDialogAsync(MainLang.FoundNewVersion, updateAvailable.Item3
, b_cancel: MainLang.Cancel, b_secondary: MainLang.SkipThisVersion,
b_primary: MainLang.Ok);
if (dialog == ContentDialogResult.Primary)
{
var updateAppAsync = await Method.Ui.UpdateAppAsync();
if (!updateAppAsync) Method.Ui.Toast(MainLang.UpdateFail);
}
else if (dialog == ContentDialogResult.Secondary)
{
Const.Data.Setting.SkipUpdateVersion = updateAvailable.Item2;
File.WriteAllText(Const.String.SettingDataPath,
JsonConvert.SerializeObject(Const.Data.Setting, Formatting.Indented));
}
}

private void BindingEvent()
{
Loaded += (s, e) =>
Expand All @@ -47,7 +61,7 @@ private void BindingEvent()
CheckUpdateBtn.Content = ring;
ring.Height = 17;
ring.Width = 17;
var (checkUpdateAsyncStatus, checkUpdateAsyncMsg) = await Method.Ui.CheckUpdateAsync();
var (checkUpdateAsyncStatus, _, checkUpdateAsyncMsg) = await Method.Ui.CheckUpdateAsync();
CheckUpdateBtn.IsEnabled = true;
CheckUpdateBtn.Content = MainLang.CheckUpdate;
if (!checkUpdateAsyncStatus)
Expand All @@ -62,10 +76,7 @@ private void BindingEvent()
if (dialog == ContentDialogResult.Primary)
{
var updateAppAsync = await Method.Ui.UpdateAppAsync();
if (!updateAppAsync)
{
Method.Ui.Toast(MainLang.UpdateFail);
}
if (!updateAppAsync) Method.Ui.Toast(MainLang.UpdateFail);
}
};
}
Expand Down
Loading

0 comments on commit 33dcd02

Please sign in to comment.