Skip to content

Commit

Permalink
Consolidate platform-specific path comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 14, 2024
1 parent 3e56d8e commit dc2f945
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Core/CKANPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static string ToRelative(string path, string root)
Properties.Resources.PathUtilsNotAbsolute, path));
}

if (!path.StartsWith(root, StringComparison.CurrentCultureIgnoreCase))
if (!path.StartsWith(root, Platform.PathComparison))
{
throw new PathErrorKraken(path, string.Format(
Properties.Resources.PathUtilsNotInside, path, root));
Expand Down
4 changes: 1 addition & 3 deletions Core/Extensions/IOExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ private static bool StringArrayStartsWith(string[] child, string[] parent)
// Only child is allowed to have extra pieces
return false;
}
var opt = Platform.IsWindows ? StringComparison.InvariantCultureIgnoreCase
: StringComparison.InvariantCulture;
for (int i = 0; i < parent.Length; ++i)
{
if (!parent[i].Equals(child[i], opt))
if (!parent[i].Equals(child[i], Platform.PathComparison))
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public string DllPathToIdentifier(string relative_path)
{
var paths = Enumerable.Repeat(game.PrimaryModDirectoryRelative, 1)
.Concat(game.AlternateModDirectoriesRelative);
if (!paths.Any(p => relative_path.StartsWith($"{p}/", StringComparison.CurrentCultureIgnoreCase)))
if (!paths.Any(p => relative_path.StartsWith($"{p}/", Platform.PathComparison)))
{
// DLLs only live in the primary or alternate mod directories
return null;
Expand Down
18 changes: 5 additions & 13 deletions Core/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,7 @@ private void Uninstall(string identifier, ref HashSet<string> possibleConfigOnly
var modFiles = instMod.Files.ToArray();

// We need case insensitive path matching on Windows
var directoriesToDelete = Platform.IsWindows
? new HashSet<string>(StringComparer.OrdinalIgnoreCase)
: new HashSet<string>();
var directoriesToDelete = new HashSet<string>(Platform.PathComparer);

// Files that Windows refused to delete due to locking (probably)
var undeletableFiles = new List<string>();
Expand Down Expand Up @@ -890,9 +888,7 @@ private void Uninstall(string identifier, ref HashSet<string> possibleConfigOnly
string.Join(", ", notRemovable));
if (possibleConfigOnlyDirs == null)
{
possibleConfigOnlyDirs = Platform.IsWindows
? new HashSet<string>(StringComparer.OrdinalIgnoreCase)
: new HashSet<string>();
possibleConfigOnlyDirs = new HashSet<string>(Platform.PathComparer);
}
possibleConfigOnlyDirs.Add(directory);
}
Expand Down Expand Up @@ -945,9 +941,7 @@ public HashSet<string> AddParentDirectories(HashSet<string> directories)
{
if (directories == null || directories.Count == 0)
{
return Platform.IsWindows
? new HashSet<string>(StringComparer.OrdinalIgnoreCase)
: new HashSet<string>();
return new HashSet<string>(Platform.PathComparer);
}

var gameDir = CKANPathUtils.NormalizePath(ksp.GameDir());
Expand All @@ -959,9 +953,7 @@ public HashSet<string> AddParentDirectories(HashSet<string> directories)
.Distinct()
.SelectMany(dir =>
{
var results = Platform.IsWindows
? new HashSet<string>(StringComparer.OrdinalIgnoreCase)
: new HashSet<string>();
var results = new HashSet<string>(Platform.PathComparer);
// Adding in the DirectorySeparatorChar fixes attempts on Windows
// to parse "X:" which resolves to Environment.CurrentDirectory
var dirInfo = new DirectoryInfo(
Expand All @@ -974,7 +966,7 @@ public HashSet<string> AddParentDirectories(HashSet<string> directories)
return results;
}

if (!dir.StartsWith(gameDir, StringComparison.CurrentCultureIgnoreCase))
if (!dir.StartsWith(gameDir, Platform.PathComparison))
{
dir = CKANPathUtils.ToAbsolute(dir, gameDir);
}
Expand Down
8 changes: 8 additions & 0 deletions Core/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public static class Platform
IsUnix
&& !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISPLAY"));

public static readonly StringComparer PathComparer =
IsWindows ? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal;

public static readonly StringComparison PathComparison =
IsWindows ? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;

public static bool IsAdministrator()
{
if (File.Exists("/.dockerenv"))
Expand Down
10 changes: 3 additions & 7 deletions Core/Registry/InstalledModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ public InstalledModule(GameInstance ksp, CkanModule module, IEnumerable<string>
install_time = DateTime.Now;
source_module = module;
// We need case insensitive path matching on Windows
installed_files = Platform.IsWindows
? new Dictionary<string, InstalledModuleFile>(StringComparer.OrdinalIgnoreCase)
: new Dictionary<string, InstalledModuleFile>();
installed_files = new Dictionary<string, InstalledModuleFile>(Platform.PathComparer);
auto_installed = autoInstalled;

if (ksp != null)
Expand Down Expand Up @@ -156,7 +154,7 @@ private void DeSerialisationFixes(StreamingContext context)
{
// We need case insensitive path matching on Windows
installed_files = new Dictionary<string, InstalledModuleFile>(installed_files,
StringComparer.OrdinalIgnoreCase);
Platform.PathComparer);
}
}

Expand All @@ -168,9 +166,7 @@ private void DeSerialisationFixes(StreamingContext context)
public void Renormalise(GameInstance ksp)
{
// We need case insensitive path matching on Windows
var normalised_installed_files = Platform.IsWindows
? new Dictionary<string, InstalledModuleFile>(StringComparer.OrdinalIgnoreCase)
: new Dictionary<string, InstalledModuleFile>();
var normalised_installed_files = new Dictionary<string, InstalledModuleFile>(Platform.PathComparer);

foreach (KeyValuePair<string, InstalledModuleFile> tuple in installed_files)
{
Expand Down
14 changes: 4 additions & 10 deletions Core/Registry/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ private void DeSerialisationFixes(StreamingContext context)
log.Warn("Older registry format detected, normalising paths...");

// We need case insensitive path matching on Windows
var normalised_installed_files = Platform.IsWindows
? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
: new Dictionary<string, string>();
var normalised_installed_files = new Dictionary<string, string>(Platform.PathComparer);

foreach (KeyValuePair<string, string> tuple in installed_files)
{
Expand Down Expand Up @@ -208,7 +206,7 @@ private void DeSerialisationFixes(StreamingContext context)
{
// We need case insensitive path matching on Windows
// (already done when replacing this object in the above block, hence the 'else')
installed_files = new Dictionary<string, string>(installed_files, StringComparer.OrdinalIgnoreCase);
installed_files = new Dictionary<string, string>(installed_files, Platform.PathComparer);
}

// Fix control lock, which previously was indexed with an invalid identifier.
Expand Down Expand Up @@ -285,9 +283,7 @@ private void DeSerialisationFixes(StreamingContext context)
public void ReindexInstalled()
{
// We need case insensitive path matching on Windows
installed_files = Platform.IsWindows
? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
: new Dictionary<string, string>();
installed_files = new Dictionary<string, string>(Platform.PathComparer);

foreach (InstalledModule module in installed_modules.Values)
{
Expand Down Expand Up @@ -851,9 +847,7 @@ public void RegisterModule(CkanModule mod,

// We always work with relative files, so let's get some!
var relativeFiles = absoluteFiles.Select(x => inst.ToRelativeGameDir(x))
.ToHashSet(Platform.IsWindows
? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal);
.ToHashSet(Platform.PathComparer);

// For now, it's always cool if a module wants to register a directory.
// We have to flip back to absolute paths to actually test this.
Expand Down
6 changes: 1 addition & 5 deletions Core/SteamLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public SteamLibrary()
}

public IEnumerable<Uri> GameAppURLs(DirectoryInfo gameDir)
=> Games.Where(g => gameDir.FullName.Equals(g.GameDir.FullName, compareOpt))
=> Games.Where(g => gameDir.FullName.Equals(g.GameDir.FullName, Platform.PathComparison))
.Select(g => g.LaunchUrl);

public readonly GameBase[] Games;
Expand Down Expand Up @@ -87,10 +87,6 @@ private static string[] SteamPaths

private static readonly string[] appRelPaths = new string[] { "SteamApps", "steamapps" };

private static readonly StringComparison compareOpt
= Platform.IsWindows ? StringComparison.InvariantCultureIgnoreCase
: StringComparison.InvariantCulture;

private static readonly ILog log = LogManager.GetLogger(typeof(SteamLibrary));
}

Expand Down
4 changes: 1 addition & 3 deletions GUI/Dialogs/ManageGameInstancesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ private void ImportFromSteamMenuItem_Click(object sender, EventArgs e)
{
var currentDirs = _manager.Instances.Values
.Select(inst => inst.GameDir())
.ToHashSet(Platform.IsWindows
? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal);
.ToHashSet(Platform.PathComparer);
var toAdd = _manager.FindDefaultInstances()
.Where(inst => !currentDirs.Contains(inst.GameDir()));
foreach (var inst in toAdd)
Expand Down
1 change: 0 additions & 1 deletion GUI/Main/MainAutoUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Autofac;

using CKAN.Configuration;
using CKAN.Versioning;

// Don't warn if we use our own obsolete properties
#pragma warning disable 0618
Expand Down

0 comments on commit dc2f945

Please sign in to comment.