From 8676e232dad7e16f5a2dbeb94a7beede789c8330 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 16 May 2021 19:45:34 +0200 Subject: [PATCH] Use symlinks to create a modded copy --- GCMM/MainUtils.cs | 60 +++++++++++++++++++++++++---------------------- GCMM/SymLinks.cs | 14 +++++++++++ 2 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 GCMM/SymLinks.cs diff --git a/GCMM/MainUtils.cs b/GCMM/MainUtils.cs index 54d4c5b..ea7ddf9 100644 --- a/GCMM/MainUtils.cs +++ b/GCMM/MainUtils.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; @@ -29,39 +30,41 @@ namespace GCMM private bool EnableDisableAutoPatching(bool enable) { - string launcherConfig = - Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - "Techblox Launcher", "launcher_settings.ini"); - bool gamePathSet = false; - if (File.Exists(launcherConfig)) - { - string[] lines = File.ReadLines(launcherConfig).Select(line => - { - if (line.StartsWith("133062..GAME_PATH=") && line.Contains('/')) - { - gamePathSet = true; - return $"{line.Replace("/TBMM/", "/")}{(enable ? "TBMM/" : "")}"; - } - - return line; - }).ToArray(); //Need to close the file - File.WriteAllLines(launcherConfig, lines); - } - - return gamePathSet; - } //TODO: Setting the game path might not be a good idea because of updates... + foreach (string file in Directory.EnumerateFiles(GamePath(""))) + File.Copy(file, Path.GetFileName(file), true); + var toSymLink = new List {GamePath("\\MonoBleedingEdge")}; + Directory.CreateDirectory("TechbloxPreview_Data"); + toSymLink.AddRange(Directory.EnumerateDirectories(GamePath("\\TechbloxPreview_Data")) + .Except(new[] {"Managed"})); + foreach (string file in Directory.EnumerateFiles(GamePath("\\TechbloxPreview_Data"))) + File.Copy(file, "TechbloxPreview_Data\\" + Path.GetFileName(file), true); + Directory.CreateDirectory("TechbloxPreview_Data\\Managed"); + foreach (string file in Directory.EnumerateFiles(GamePath("\\TechbloxPreview_Data\\Managed"))) + File.Copy(file, "TechbloxPreview_Data\\Managed\\" + Path.GetFileName(file), true); + return toSymLink.Select(targetName => (sourceName: targetName.Replace(GamePath("\\"), ""), targetName)) + .Where(tuple => !Directory.Exists(tuple.sourceName)) + .All(tuple => SymLinks.CreateFolderSymbolicLink(tuple.sourceName, tuple.targetName)); + } public void EnableDisableAutoPatchingWithDialog(bool enable) { - if (EnableDisableAutoPatching(enable)) + try { - DialogUtils.ShowInfo(resources.GetString("Change_launch_settings_done"), - resources.GetString("Change_launch_settings_title")); - Configuration.AutoPatch = enable ? AutoPatchingState.Enabled : AutoPatchingState.Disabled; + if (EnableDisableAutoPatching(enable)) + { + DialogUtils.ShowInfo(resources.GetString("Change_launch_settings_done"), + resources.GetString("Change_launch_settings_title")); + Configuration.AutoPatch = enable ? AutoPatchingState.Enabled : AutoPatchingState.Disabled; + } + else + DialogUtils.ShowError(resources.GetString("Change_launch_settings_error"), + resources.GetString("Change_launch_settings_title")); } - else - DialogUtils.ShowError(resources.GetString("Change_launch_settings_error"), + catch (Exception e) + { + DialogUtils.ShowError(resources.GetString("Change_launch_settings_error") + "\n\n" + e, resources.GetString("Change_launch_settings_title")); + } } public string GetGameFolder() @@ -72,7 +75,8 @@ namespace GCMM if (!File.Exists(launcherConfig)) return null; string path = File.ReadLines(launcherConfig) .FirstOrDefault(line => line.StartsWith("133062..GAME_PATH=")) - ?.Substring("133062..GAME_PATH=".Length).Replace("/TBMM/", "/") + "StandaloneWindows64"; + ?.Substring("133062..GAME_PATH=".Length).Replace("/TBMM/", "/"); + if (path != null) path = (path + "StandaloneWindows64").Replace('/', Path.DirectorySeparatorChar); if (path != null && GetExe(path) != null) return path; return null; } diff --git a/GCMM/SymLinks.cs b/GCMM/SymLinks.cs new file mode 100644 index 0000000..38a0dff --- /dev/null +++ b/GCMM/SymLinks.cs @@ -0,0 +1,14 @@ +using System.Diagnostics; + +namespace GCMM +{ + public class SymLinks + { + public static bool CreateFolderSymbolicLink(string source, string target) + { + var proc = Process.Start(new ProcessStartInfo("cmd.exe", $"/c mklink /J \"{source}\" \"{target}\"")); + proc?.WaitForExit(); + return proc?.ExitCode == 0; + } + } +} \ No newline at end of file