From d9270f2624a8c6b565df4659cf7f0b4436915714 Mon Sep 17 00:00:00 2001 From: valters-tomsons Date: Sun, 3 Dec 2023 16:08:04 +0200 Subject: [PATCH] Re-introduce patch for settings saving --- .../Patches/LauncherConfigPatcher.cs | 97 ++++++++++++++++++- src/DayZLauncher.UnixPatcher/Program.cs | 2 + 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/DayZLauncher.UnixPatcher/Patches/LauncherConfigPatcher.cs b/src/DayZLauncher.UnixPatcher/Patches/LauncherConfigPatcher.cs index 75d2b48..6ea6174 100644 --- a/src/DayZLauncher.UnixPatcher/Patches/LauncherConfigPatcher.cs +++ b/src/DayZLauncher.UnixPatcher/Patches/LauncherConfigPatcher.cs @@ -1,3 +1,6 @@ +using System.Text; +using System.Xml; + namespace DayZLauncher.UnixPatcher.Patches; public static class LauncherConfigPatcher @@ -28,6 +31,98 @@ public static void ApplySettingsBandAid(string gamePath) } var symLinkPath = bohemiaPath[0..^1]; - File.CreateSymbolicLink(symLinkPath, bohemiaPath); + if (!Directory.Exists(symLinkPath)) + { + File.CreateSymbolicLink(symLinkPath, bohemiaPath); + } + } + + public static async Task PatchLauncherConfigFile(string gamePath) + { + var configFilePath = $"{gamePath}/DayZLauncher.exe.config"; + string? newXml = string.Empty; + + if (!File.Exists(configFilePath)) + { + Common.WriteLine("Failed to find DayZLauncher.exe.config!"); + return; + } + + using (var configStream = new FileStream(configFilePath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, true)) + { + var configXml = new XmlDocument(); + configXml.Load(configStream); + await configStream.FlushAsync(); + + var sectionGroup = configXml.SelectSingleNode("//sectionGroup[@name='userSettings']"); + sectionGroup?.ParentNode?.RemoveChild(sectionGroup); + + var userSettings = configXml.SelectSingleNode("//userSettings"); + userSettings?.ParentNode?.RemoveChild(userSettings); + + newXml = new string(configXml.OuterXml); + } + + newXml = IndentXml(newXml); + await File.WriteAllTextAsync(configFilePath, newXml); + } + + public static void RemoveOldUserConfig(string gamePath) + { + var prefixPath = $"{gamePath}/../../compatdata/221100/pfx"; + + if (!Directory.Exists(prefixPath)) + { + var systemPrefix = Common.TryGetGamePrefixFromSystem(); + if (systemPrefix is null) + { + Common.WriteLine($"Failed to find game prefix at: '{prefixPath}'", ConsoleColor.Red); + Common.WriteLine(""" + You should manually delete the following file for launcher to work!!! + > C:/users/steamuser/AppData/Local/Bohemia Interactive/DayZ Launcher_*/*/user.config + """, + ConsoleColor.Yellow); + return; + } + + prefixPath = systemPrefix; + } + + Common.WriteLine("Proton prefix found!"); + + var bohemiaPath = $"{prefixPath}/drive_c/users/steamuser/AppData/Local/Bohemia Interactive a.s."; + if (!Directory.Exists(bohemiaPath)) + { + Common.WriteLine("Could not find config settings folder, nothing to patch"); + return; + } + + var configFiles = Directory.EnumerateFiles(bohemiaPath, "user.config", SearchOption.AllDirectories); + foreach (var file in configFiles) + { + Common.WriteLine($"Deleting settings file: {file}", ConsoleColor.Green); + File.Delete(file); + } + } + + private static string IndentXml(string xml) + { + var doc = new XmlDocument(); + doc.LoadXml(xml); + + var settings = new XmlWriterSettings + { + Indent = true, + IndentChars = " ", + NewLineChars = "\r\n", + NewLineHandling = NewLineHandling.Replace, + Encoding = Encoding.UTF8 + }; + + var sb = new StringBuilder(); + using var xmlWriter = XmlWriter.Create(sb, settings); + doc.Save(xmlWriter); + var xmlString = sb.ToString(); + return xmlString.Replace("utf-16", "utf-8"); } } diff --git a/src/DayZLauncher.UnixPatcher/Program.cs b/src/DayZLauncher.UnixPatcher/Program.cs index e2f7c02..e64d6b5 100644 --- a/src/DayZLauncher.UnixPatcher/Program.cs +++ b/src/DayZLauncher.UnixPatcher/Program.cs @@ -61,6 +61,8 @@ try { LauncherConfigPatcher.ApplySettingsBandAid(userInput); + await LauncherConfigPatcher.PatchLauncherConfigFile(userInput); + LauncherConfigPatcher.RemoveOldUserConfig(userInput); Common.WriteLine("Settings fix applied!", ConsoleColor.Green); } catch