diff --git a/Core/Java/ModJava.cs b/Core/Java/ModJava.cs index e02194a..18a8821 100644 --- a/Core/Java/ModJava.cs +++ b/Core/Java/ModJava.cs @@ -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 javaList) { diff --git a/Launcher/LauncherInfo.cs b/Launcher/LauncherInfo.cs index 963bac8..dbcf61a 100644 --- a/Launcher/LauncherInfo.cs +++ b/Launcher/LauncherInfo.cs @@ -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"; } } \ No newline at end of file diff --git a/Launcher/LauncherInit.cs b/Launcher/LauncherInit.cs index 62a8e56..0c973c8 100644 --- a/Launcher/LauncherInit.cs +++ b/Launcher/LauncherInit.cs @@ -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); @@ -25,29 +26,29 @@ public static void DeleteTempFolder() { /// 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"); @@ -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(); // } diff --git a/MainPage.xaml.cs b/MainPage.xaml.cs index 5432c2f..47804f7 100644 --- a/MainPage.xaml.cs +++ b/MainPage.xaml.cs @@ -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(await GetStringResponseAsync("https://bmclapi2.bangbang93.com/mc/game/version_manifest_v2.json")); //MessageBox.Show(sl.ToString()); diff --git a/Resources/DefaultSetting.xml b/Resources/DefaultSetting.xml new file mode 100644 index 0000000..90720ec --- /dev/null +++ b/Resources/DefaultSetting.xml @@ -0,0 +1,3 @@ + + null + \ No newline at end of file diff --git a/SodaCL.csproj b/SodaCL.csproj index ac967da..01bd9a3 100644 --- a/SodaCL.csproj +++ b/SodaCL.csproj @@ -22,11 +22,13 @@ 1701;1702;SYSLIB0014 + + diff --git a/Toolkits/DataTool.cs b/Toolkits/DataTool.cs index 9fcc127..84b1cf3 100644 --- a/Toolkits/DataTool.cs +++ b/Toolkits/DataTool.cs @@ -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 { + + /// + /// 释放内嵌资源至指定位置 + /// + /// 嵌入的资源,此参数写作:命名空间.文件夹名.文件名.扩展名 + /// 释放到位置 + 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; + } + } + /// + /// 将 对象 转换为 对象 /// /// 源对象 diff --git a/Toolkits/Setting.cs b/Toolkits/Setting.cs index a016cd4..ce2ccd4 100644 --- a/Toolkits/Setting.cs +++ b/Toolkits/Setting.cs @@ -1,13 +1,16 @@ -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 { @@ -15,22 +18,32 @@ namespace SodaCL.Toolkits { /// 写入 SodaCL 配置 /// public class Setting { + /// - /// ð´èë配置 + /// 写入配置 /// - + /// /// /// < param name="value"> + 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; + } + } public static void WriteSetting(string key, string value) { try { - var settings = JsonConvert.DeserializeObject(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; diff --git a/Toolkits/XmlTool.cs b/Toolkits/XmlTool.cs new file mode 100644 index 0000000..b18bf4c --- /dev/null +++ b/Toolkits/XmlTool.cs @@ -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; + } + } + + 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; + } + } + } + } +} \ No newline at end of file