Use symlinks to create a modded copy

This commit is contained in:
Norbi Peti 2021-05-16 19:45:34 +02:00
parent a7d26ebdb8
commit 8676e232da
2 changed files with 46 additions and 28 deletions

View file

@ -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<string> {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;
}

14
GCMM/SymLinks.cs Normal file
View file

@ -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;
}
}
}