Skip to content

Commit

Permalink
Set the working directory to the location of the executable path if i…
Browse files Browse the repository at this point in the history
…t does not already match. Resolves #223.
  • Loading branch information
RevZero committed Sep 10, 2021
1 parent 94e5960 commit 0737054
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions xcom2-launcher/xcom2-launcher/Classes/Helper/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,23 @@ public static string GetRtfEscapedString(string s)
return sb.ToString();
}

public static bool CompareDirectories(string dir1, string dir2)
{
if (dir1 == null || dir2 == null)
{
return false;
}

// Trim/ignore trailing directory separators for the comparison
dir1 = dir1.TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar);
dir2 = dir2.TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar);

// Create DirectoryInfo to get a consistent directory style for comparison
var dirInfo1 = new DirectoryInfo(dir1);
var dirInfo2 = new DirectoryInfo(dir2);
var result = string.Compare(dirInfo1.FullName, dirInfo2.FullName, StringComparison.InvariantCultureIgnoreCase);

return result == 0;
}
}
}
22 changes: 22 additions & 0 deletions xcom2-launcher/xcom2-launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ static Program()
#endif

Log.Info($"Application started (AML {GitVersionInfo.FullSemVer} {GitVersionInfo.Sha})");
Log.Info($"Executable location: '{Application.ExecutablePath}'");

// If AML stores files to the "working directory", we actually want them to end up in the executable folder.
// So if the working directory does not match the executable path, we change it manually.
try
{
var exeDir = Path.GetDirectoryName(Application.ExecutablePath);
var workingDir = Directory.GetCurrentDirectory();

if (!Tools.CompareDirectories(exeDir, workingDir))
{
Log.Info($"Changing current working directory from '{workingDir}' to executable path '{exeDir}'");
Directory.SetCurrentDirectory(exeDir!);
}
}
catch (Exception ex)
{
Log.Error("Failed changing working directory", ex);
}
}

/// <summary>
Expand Down Expand Up @@ -330,6 +349,9 @@ public static Settings InitializeSettings()
MessageBox.Show(@"Could not detect path to installation directory. Please select it manually from the settings.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

Log.Info($"Game: {settings.Game}");
Log.Info($"GamePath: {settings.GamePath}");

// Make sure, that all mod paths have a trailing backslash
var pathsWithMissingTrailingBackSlash = settings.ModPaths.Where(m => !m.EndsWith(@"\")).ToList();
for (var i = 0; i < pathsWithMissingTrailingBackSlash.Count; i++)
Expand Down

0 comments on commit 0737054

Please sign in to comment.