Skip to content

Commit

Permalink
feat:设置的存取
Browse files Browse the repository at this point in the history
  • Loading branch information
HRxiaohu committed Jan 30, 2025
1 parent 43f474b commit 370591d
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Core/Java/ModJava.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static void AutoJavaFinding() {

Log(false, ModuleList.IO, LogInfo.Info, "版本: " + java.Version.ToString() + " 64 位: " + java.Is64Bit.ToString() + " 路径: " + java.DirPath.ToString());
}
RegEditor.SetKeyValue(Registry.CurrentUser, "CacheJavaList", JsonConvert.SerializeObject(javaList), RegistryValueKind.String);
Setting.WriteSetting("Java.List", JsonConvert.SerializeObject(javaList));
}

public static void SearchJavaInFolder(string targetDir, ref List<JavaModel> javaList) {
Expand Down
2 changes: 1 addition & 1 deletion Launcher/LauncherInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class LauncherInfo {

public static string SODACL_TEMP_FOLDER_PATH = Path.GetTempPath() + "SodaCL";

public static string SODACL_SETTINGS = CURRENT_DIR + @"SodaCL\Config.json";
public static string SODACL_SETTINGS = CURRENT_DIR + @"SodaCL\Config.xml";
}
}
43 changes: 22 additions & 21 deletions Launcher/LauncherInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System.Globalization;
using System.IO;
using static SodaCL.Toolkits.Logger;
using static SodaCL.Launcher.LauncherInfo;

namespace SodaCL.Launcher {

public static class LauncherInit {

public static void DeleteTempFolder() {
if (Directory.GetFiles(LauncherInfo.SODACL_TEMP_FOLDER_PATH).Length > 0) {
var tempDir = new DirectoryInfo(LauncherInfo.SODACL_TEMP_FOLDER_PATH);
if (Directory.GetFiles(SODACL_TEMP_FOLDER_PATH).Length > 0) {
var tempDir = new DirectoryInfo(SODACL_TEMP_FOLDER_PATH);
var tempFiles = tempDir.GetFiles();
foreach (var files in tempFiles) {
File.Delete(files.FullName);
Expand All @@ -25,29 +26,29 @@ public static void DeleteTempFolder() {
/// </summary>
public static void Setup() {
try {
if (!Directory.Exists(LauncherInfo.SODACL_FOLDER_PATH))
Directory.CreateDirectory(LauncherInfo.SODACL_FOLDER_PATH);
if (!Directory.Exists(SODACL_FOLDER_PATH))
Directory.CreateDirectory(SODACL_FOLDER_PATH);

if (!Directory.Exists(LauncherInfo.SODA_MC_DIR))
Directory.CreateDirectory(LauncherInfo.SODA_MC_DIR);
if (!Directory.Exists(SODA_MC_DIR))
Directory.CreateDirectory(SODA_MC_DIR);

if (!Directory.Exists(LauncherInfo.SODACL_LOG_FOLDER_PATH))
Directory.CreateDirectory(LauncherInfo.SODACL_LOG_FOLDER_PATH);
if (!Directory.Exists(SODACL_LOG_FOLDER_PATH))
Directory.CreateDirectory(SODACL_LOG_FOLDER_PATH);

//if (!Directory.Exists(LauncherInfo.sodaCLFontsForderPath))
// Directory.CreateDirectory(LauncherInfo.sodaCLFontsForderPath);
//if (!Directory.Exists(sodaCLFontsForderPath))
// Directory.CreateDirectory(sodaCLFontsForderPath);

if (!Directory.Exists(LauncherInfo.SODA_MC_VERSIONS_DIR))
Directory.CreateDirectory(LauncherInfo.SODA_MC_VERSIONS_DIR);
if (!Directory.Exists(SODA_MC_VERSIONS_DIR))
Directory.CreateDirectory(SODA_MC_VERSIONS_DIR);

if (!Directory.Exists(LauncherInfo.APP_DATA_DIR))
Directory.CreateDirectory(LauncherInfo.APP_DATA_DIR);
if (!Directory.Exists(APP_DATA_DIR))
Directory.CreateDirectory(APP_DATA_DIR);

if (!File.Exists(LauncherInfo.SODACL_SETTINGS))
File.Create(LauncherInfo.SODACL_SETTINGS);
if (!File.Exists(SODACL_SETTINGS))
DataTool.ExtractFile("SodaCL.Resources.DefaultSetting.xml", SODACL_SETTINGS);

if (!Directory.Exists(LauncherInfo.SODACL_TEMP_FOLDER_PATH))
Directory.CreateDirectory(LauncherInfo.SODACL_TEMP_FOLDER_PATH);
if (!Directory.Exists(SODACL_TEMP_FOLDER_PATH))
Directory.CreateDirectory(SODACL_TEMP_FOLDER_PATH);

if (Registry.CurrentUser.OpenSubKey(@"Software\SodaCL") == null)
RegEditor.CreateSubKey(Registry.CurrentUser, @"Software\SodaCL");
Expand All @@ -64,15 +65,15 @@ public static void Setup() {
//TODO:做不做捏?
//public static async Task InitMiSansFonts()
//{
// if (new DirectoryInfo(LauncherInfo.sodaCLFontsForderPath).GetFiles().Length != 11)
// if (new DirectoryInfo(sodaCLFontsForderPath).GetFiles().Length != 11)
// {
// try
// {
// var zipPath = LauncherInfo.sodaCLFontsForderPath + "\\MiSans.zip";
// var zipPath = sodaCLFontsForderPath + "\\MiSans.zip";
// var md = new FileDownloader("https://cdn.cnbj1.fds.api.mi-img.com/vipmlmodel/font/MiSans/MiSans.zip", zipPath);
// md.DownloaderProgressFinished += (sender, e) =>
// {
// ZipFile.ExtractToDirectory(zipPath, LauncherInfo.sodaCLFontsForderPath);
// ZipFile.ExtractToDirectory(zipPath, sodaCLFontsForderPath);
// };
// await md.Start();
// }
Expand Down
2 changes: 1 addition & 1 deletion MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void LogFolderOpenerButtonClick(object sender, RoutedEventArgs e) {

private void StartBtn_Click(object sender, RoutedEventArgs e) {
//MinecraftLaunch.LaunchGame();
var dE = new SodaDialog(SodaDialog.DialogType.Error, null, null, null, true);
var dE = new SodaDialog(SodaDialog.DialogType.Error, null, null, null);
//var sl = JsonConvert.DeserializeObject<AssetModel>(await GetStringResponseAsync("https://bmclapi2.bangbang93.com/mc/game/version_manifest_v2.json"));
//MessageBox.Show(sl.ToString());

Expand Down
3 changes: 3 additions & 0 deletions Resources/DefaultSetting.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Settings>
<Java.List>null</Java.List>
</Settings>
2 changes: 2 additions & 0 deletions SodaCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
<NoWarn>1701;1702;SYSLIB0014</NoWarn>
</PropertyGroup>
<ItemGroup>
<None Remove="Resources\DefaultSetting.xml" />
<None Remove="Resources\Images\Dev.ico" />
<None Remove="Resources\Images\Launcher-Bg.jpg" />
<None Remove="Resources\Images\Release.ico" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\DefaultSetting.xml" />
<Resource Include="Token.xaml" />
<Resource Include="Resources\Images\Dev.ico" />
<Resource Include="Resources\Images\Launcher-Bg.jpg" />
Expand Down
33 changes: 32 additions & 1 deletion Toolkits/DataTool.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
using System.Collections;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Media;

namespace SodaCL.Toolkits {

public class DataTool {

/// <summary>
/// 释放内嵌资源至指定位置
/// </summary>
/// <param name="resource">嵌入的资源,此参数写作:命名空间.文件夹名.文件名.扩展名</param>
/// <param name="path">释放到位置</param>
public static void ExtractFile(string resource, string path) {
try {
var assembly = Assembly.GetExecutingAssembly();
var res = assembly.GetManifestResourceStream(resource);
var input = new BufferedStream(res);
var output = new FileStream(path, FileMode.Create);
byte[] data = new byte[1024];
int lengthEachRead;
while ((lengthEachRead = input.Read(data, 0, data.Length)) > 0) {
output.Write(data, 0, lengthEachRead);
}
output.Flush();
output.Close();
}
catch (Exception ex) {
Logger.Log(false, Logger.ModuleList.IO, Logger.LogInfo.Error, "释放资源文件失败。", ex);
throw ex;

Check warning on line 33 in Toolkits/DataTool.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)
}
}

/// <summary>

/// 将 <see cref="SolidColorBrush"/> 对象 转换为 <see cref="Color"/> 对象
/// </summary>
/// <param name="targetBrush">源对象</param>
Expand Down
33 changes: 23 additions & 10 deletions Toolkits/Setting.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
using Newtonsoft.Json;
using SodaCL.Controls.Dialogs;
using SodaCL.Controls.Dialogs;
using SodaCL.Models.Launcher.Toolkits;
using System;
using System.Collections.Generic;
using System.IO;
using System.Configuration;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SodaCL.Launcher.LauncherInfo;
using SodaCL.Launcher;
using System.Diagnostics;

namespace SodaCL.Toolkits {

/// <summary>
/// 写入 SodaCL 配置
/// </summary>
public class Setting {

/// <summary>
/// ð´èë配置
/// 写入配置
/// </summary>

///
/// <private param name = "key" ></ param >
/// < param name="value"></param>
public static void SettingInit() {
try {
DataTool.ExtractFile("SodaCL.Resources.DefaultSetting.xml", SODACL_SETTINGS);
var xml = new XmlTool(SODACL_SETTINGS);
xml.CreateNode("Settings");
}
catch (Exception ex) {
throw ex;

Check warning on line 35 in Toolkits/Setting.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)
}
}

public static void WriteSetting(string key, string value) {
try {
var settings = JsonConvert.DeserializeObject<SodaSetting>(File.ReadAllText(SODACL_SETTINGS));
if (settings == null) {
throw new Exception();
}
var xml = new XmlTool(SODACL_SETTINGS);
xml.SetNodeValue(key, value);
}
catch {
var dE = new SodaDialog(SodaDialog.DialogType.Error, null, null, "读取配置文件出错,程序自动退出。");
catch (Exception ex) {
SettingInit();
var dE = new SodaDialog(SodaDialog.DialogType.Error, null, null, $"读取配置文件出错,程序自动退出。\r错误信息:{ex}");
dE.CloseEvent += () => {
App.Current.Shutdown();
return null;
Expand Down
52 changes: 52 additions & 0 deletions Toolkits/XmlTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace SodaCL.Toolkits {

internal class XmlTool {
private XmlNodeList _nodes;
private XmlDocument doc;

public XmlTool(string path) {
try {
doc = new XmlDocument();
doc.Load(path);
_nodes = doc.DocumentElement.ChildNodes;
}
catch (XmlException ex) {
Logger.Log(false, Logger.ModuleList.IO, Logger.LogInfo.Error, null, ex);
throw ex;

Check warning on line 22 in Toolkits/XmlTool.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)
}
}

public void CreateNode(string nodeName) {
var root = doc.CreateElement(nodeName);
doc.AppendChild(root);
}

public string GetNodeValue(string nodeName) {
foreach (var node in _nodes) {
if (node is XmlElement element) {
if (element.Name == nodeName) {
return element.InnerText;
}
}
}
return null;
}

public void SetNodeValue(string nodeName, string value) {
foreach (XmlNode node in _nodes) {
if (node.Name == nodeName) {
node.InnerText = value;
doc.Save(Launcher.LauncherInfo.SODACL_SETTINGS);
return;
}
}
}
}
}

0 comments on commit 370591d

Please sign in to comment.