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 fa11972 commit a64137c
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 156 deletions.
5 changes: 3 additions & 2 deletions YMCL.Main/Public/Const.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public abstract class Const
{
public abstract class Data
{
public static JavaEntry AutoJava = new() { JavaPath = "Auto", JavaVersion = "All" };
public static List<UrlImageDataListEntry> UrlImageDataList = new();
public static Setting Setting { get; set; } = null;
public static JavaEntry AutoJava { get; set; } = new() { JavaPath = "Auto", JavaVersion = "All" };
public static List<UrlImageDataListEntry> UrlImageDataList { get; set; } = new();
public static Platform Platform { get; set; }
}

Expand Down
2 changes: 1 addition & 1 deletion YMCL.Main/Public/Controls/ImageIcon/ImageIcon.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Reload()
}
private void ImageIcon_Loaded(object? sender, RoutedEventArgs e)
{
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
IBrush? brush;
if (Color == null)
brush = setting.Theme == Public.Theme.Light ? Brush.Parse("#333333") : Brush.Parse("#ffffff");
Expand Down
2 changes: 1 addition & 1 deletion YMCL.Main/Public/Controls/WindowTask/WindowTask.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public WindowTask(string name, bool valueProgress = true)
};
Loaded += (_, _) =>
{
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (setting.WindowTitleBarStyle == WindowTitleBarStyle.System)
{
TitleBar.IsVisible = false;
Expand Down
2 changes: 1 addition & 1 deletion YMCL.Main/Public/Langs/LangHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void ChangedCulture(string name)

public static string GetText(string name, string culture = "")
{
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
CultureInfo cultureInfo;
if (culture == "")
{
Expand Down
20 changes: 9 additions & 11 deletions YMCL.Main/Public/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static void ToggleTheme(Theme theme)

public static void SetWindowBackGroundImg()
{
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (setting.EnableCustomBackGroundImg && !string.IsNullOrEmpty(setting.WindowBackGroundImgData))
{
Application.Current.Resources["Opacity"] = 0.875;
Expand Down Expand Up @@ -352,9 +352,7 @@ public static async Task<bool> UpdateAppAsync()
return false;
}

var setting =
JsonConvert.DeserializeObject<Setting>(
File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
var trueUrl = url;
if (setting.EnableCustomUpdateUrl)
{
Expand Down Expand Up @@ -448,7 +446,7 @@ public static async Task<List<FolderInfo>> OpenFolderPicker(TopLevel topLevel =
FolderPickerOpenOptions options = null)
{
var isPrimaryButtonClick = false;
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (setting.OpenFileWay == OpenFileWay.FileSelectWindow)
{
if (options != null && topLevel != null)
Expand Down Expand Up @@ -501,7 +499,7 @@ public static async Task<List<FileInfo>> OpenFilePicker(TopLevel topLevel = null
{
var title = p_title == null ? MainLang.InputFilePath : p_title;
var isPrimaryButtonClick = false;
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (setting.OpenFileWay == OpenFileWay.FileSelectWindow)
{
if (options != null && topLevel != null)
Expand Down Expand Up @@ -559,7 +557,7 @@ public static async Task<List<FileInfo>> OpenFilePicker(TopLevel topLevel = null
public static async Task<string> SaveFilePicker(TopLevel topLevel = null, FilePickerSaveOptions options = null)
{
var isPrimaryButtonClick = false;
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (setting.OpenFileWay == OpenFileWay.FileSelectWindow)
{
if (options != null && topLevel != null)
Expand Down Expand Up @@ -994,7 +992,7 @@ public static async Task<bool> InstallClientUsingMinecraftLaunchAsync(string ver
return false;
}

var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
var resolver = new GameResolver(setting.MinecraftFolder);
var vanlliaInstaller = new VanlliaInstaller(resolver, versionId, MirrorDownloadManager.Bmcl);
if (Directory.Exists(Path.Combine(setting.MinecraftFolder, "versions", customId)))
Expand Down Expand Up @@ -1322,7 +1320,7 @@ public static async Task<bool> LaunchClientUsingMinecraftLaunchAsync(string p_id
double l_maxMem = -1;
var l_enableIndependencyCore = true;

var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (string.IsNullOrEmpty(p_id))
{
if (Const.Window.main.launchPage.VersionListView.SelectedItem as GameEntry != null)
Expand Down Expand Up @@ -1713,7 +1711,7 @@ public static async Task<bool> LaunchClientUsingStarLightAsync(string p_id = "",
double l_maxMem = -1;
var l_enableIndependencyCore = true;

var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (string.IsNullOrEmpty(p_id))
{
if (Const.Window.main.launchPage.VersionListView.SelectedItem as GameEntry != null)
Expand Down Expand Up @@ -2103,7 +2101,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>
public static async Task<bool> ImportModPackFromLocal(string path, bool confirmBox = true,
string p_customId = null)
{
var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
var customId = string.Empty;
while (true)
{
Expand Down
3 changes: 1 addition & 2 deletions YMCL.Main/Views/DeskLyric/DeskLyric.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public async void Toggle()
}
else
{
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
LyricText.Foreground = new SolidColorBrush(setting.DeskLyricColor);
LyricText.TextAlignment = setting.DeskLyricAlignment;
_isOpen = true;
Expand Down
14 changes: 3 additions & 11 deletions YMCL.Main/Views/Initialize/InitializeWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ private void Init()
Method.IO.TryCreateFolder(Const.String.TempFolderPath);
if (!File.Exists(Const.String.SettingDataPath))
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(new Setting(), Formatting.Indented));
if (File.Exists(Path.Combine(Const.String.UserDataRootPath, "YMCL.Update.Helper.win.exe")))
File.Delete(Path.Combine(Const.String.UserDataRootPath, "YMCL.Update.Helper.win.exe"));
if (File.Exists(Path.Combine(Const.String.UserDataRootPath, "YMCL.Update.Helper.linux")))
File.Delete(Path.Combine(Const.String.UserDataRootPath, "YMCL.Update.Helper.linux"));
if (File.Exists(Path.Combine(Const.String.UserDataRootPath, "Update.exe")))
File.Delete(Path.Combine(Const.String.UserDataRootPath, "Update.exe"));
if (File.Exists(Path.Combine(Const.String.UserDataRootPath, "Update")))
File.Delete(Path.Combine(Const.String.UserDataRootPath, "Update"));
if (!File.Exists(Const.String.MinecraftFolderDataPath) || JsonConvert
.DeserializeObject<List<string>>(File.ReadAllText(Const.String.MinecraftFolderDataPath)).Count == 0)
{
Expand All @@ -77,7 +69,7 @@ private void Init()
setting1.MinecraftFolder = path;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting1, Formatting.Indented));
}

Const.Data.Setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
if (!File.Exists(Const.String.JavaDataPath))
File.WriteAllText(Const.String.JavaDataPath,
JsonConvert.SerializeObject(new List<JavaEntry>(), Formatting.Indented));
Expand Down Expand Up @@ -125,7 +117,7 @@ private void Init()
}
}

var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (setting.Language == null || setting.Language == "zh-CN")
LangHelper.Current.ChangedCulture("");
else
Expand Down Expand Up @@ -186,7 +178,7 @@ protected override void OnLoaded(RoutedEventArgs e)
{
YMCL.Main.Views.Initialize.Pages.Main.Main mianPage = new();
Frame.Content = mianPage;
var setting = JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
mianPage.WindowTitleBarStyleListBox.SelectedIndex = setting.WindowTitleBarStyle switch
{
WindowTitleBarStyle.System => 0,
Expand Down
39 changes: 12 additions & 27 deletions YMCL.Main/Views/Initialize/Pages/Main/Main.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,14 @@ public Main()
break;
}

var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
setting.Language = lang;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
Method.Ui.RestartApp();
};
WindowTitleBarStyleListBox.SelectionChanged += (_, e) =>
{
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (WindowTitleBarStyleListBox.SelectedIndex == 0)
{
Const.Window.initialize.TitleBar.IsVisible = false;
Expand Down Expand Up @@ -258,9 +256,7 @@ public Main()
Name = textBox.Text
});
AccountListBox.Items.Clear();
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(
File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
_accountList.ForEach(x =>
{
SkinResolver SkinResolver = new(Convert.FromBase64String(x.Skin));
Expand Down Expand Up @@ -389,9 +385,7 @@ await authenticator.DeviceFlowAuthAsync(device =>
Skin = Method.Value.BytesToBase64(bytes)
});
AccountListBox.Items.Clear();
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(
File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
_accountList.ForEach(x =>
{
SkinResolver SkinResolver = new(Convert.FromBase64String(x.Skin));
Expand Down Expand Up @@ -468,8 +462,7 @@ private async void UpdatePage()
break;
}

var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
setting.Language = lang;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
}
Expand All @@ -478,8 +471,7 @@ private async void UpdatePage()
{
MinecraftFolderRoot.IsVisible = true;
MinecraftFolderRoot.Opacity = (double)Application.Current.Resources["Opacity"]!;
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
setting.WindowTitleBarStyle = WindowTitleBarStyleListBox.SelectedIndex == 0
? WindowTitleBarStyle.System
: WindowTitleBarStyle.Ymcl;
Expand All @@ -490,8 +482,7 @@ private async void UpdatePage()
{
JavaRuntimeRoot.IsVisible = true;
JavaRuntimeRoot.Opacity = (double)Application.Current.Resources["Opacity"]!;
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
setting.IsCompleteMinecraftFolderInitialize = true;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
}
Expand All @@ -500,16 +491,14 @@ private async void UpdatePage()
{
AccountRoot.IsVisible = true;
AccountRoot.Opacity = (double)Application.Current.Resources["Opacity"]!;
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
setting.IsCompleteJavaInitialize = true;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
}

if (_page == 6)
{
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
setting.IsCompleteAccountInitialize = true;
File.WriteAllText(Const.String.SettingDataPath, JsonConvert.SerializeObject(setting, Formatting.Indented));
Method.Ui.RestartApp();
Expand All @@ -518,7 +507,7 @@ private async void UpdatePage()

private void OnLoaded(object? sender, RoutedEventArgs e)
{
var setting = JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
_mcFolderList.ForEach(x => MinecraftFolderListBox.Items.Add(x));
_javaList.ForEach(x => JavaRuntimeListBox.Items.Add(x));
_accountList.ForEach(x =>
Expand Down Expand Up @@ -703,9 +692,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>
Skin = Method.Value.BytesToBase64(bytes)
});
AccountListBox.Items.Clear();
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(
File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
_accountList.ForEach(x =>
{
SkinResolver SkinResolver = new(Convert.FromBase64String(x.Skin));
Expand Down Expand Up @@ -734,9 +721,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>
Name = account.Name
});
AccountListBox.Items.Clear();
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(
File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
_accountList.ForEach(x =>
{
SkinResolver SkinResolver = new(Convert.FromBase64String(x.Skin));
Expand Down
5 changes: 2 additions & 3 deletions YMCL.Main/Views/Main/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ private void EventBinding()
if (!Const.Window.main._firstLoad) return;
Const.Window.main._firstLoad = false;
Method.Ui.CheckLauncher();
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
if (!setting.IsAlreadyWrittenIntoTheUrlScheme)
{
if (Const.Data.Platform == Platform.Windows)
Expand Down Expand Up @@ -192,7 +191,7 @@ public void LoadWindow()

SystemDecorations = SystemDecorations.Full;

var setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
FrameView.Content = launchPage;
titleBarStyle = setting.WindowTitleBarStyle;
switch (setting.WindowTitleBarStyle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,7 @@ private async void ModFileSelectionChanged(object? s, SelectionChangedEventArgs
{
var fN = item.DisplayName;
if (Path.GetExtension(fN) != ".zip") fN += ".zip";
var setting =
JsonConvert.DeserializeObject<Public.Classes.Setting>(
System.IO.File.ReadAllText(Const.String.SettingDataPath));
var setting = Const.Data.Setting;
while (true)
{
var textBox = new TextBox
Expand Down
Loading

0 comments on commit a64137c

Please sign in to comment.