Skip to content

Commit 71f7742

Browse files
committed
Update path normalization for Windows-like behavior on Linux
Replaced LinuxDirectorySeparatorNormalizeOptions with PGFileSystemDirectorySeparatorNormalizeOptions to ensure consistent Windows-like path handling on Linux. Updated class documentation to clarify behavior. Refactored IsDirectorySeparator to use defined separator constants for improved clarity.
1 parent f27972b commit 71f7742

3 files changed

Lines changed: 10 additions & 17 deletions

File tree

src/PetroglyphTools/PG.StarWarsGame.Engine.FileSystem/IO/PetroglyphFileSystem.Normalize.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@ internal void NormalizePath(ref ValueStringBuilder stringBuilder)
1212
}
1313

1414
private static void NormalizePath(Span<char> path)
15-
{
16-
PathNormalizer.Normalize(path, path, LinuxDirectorySeparatorNormalizeOptions);
17-
18-
//if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
19-
// return;
20-
//for (var i = 0; i < path.Length; i++)
21-
//{
22-
// var c = path[i];
23-
// if (IsDirectorySeparator(c))
24-
// path[i] = '/';
25-
//}
15+
{
16+
PathNormalizer.Normalize(path, path, PGFileSystemDirectorySeparatorNormalizeOptions);
2617
}
2718
}

src/PetroglyphTools/PG.StarWarsGame.Engine.FileSystem/IO/PetroglyphFileSystem.PathEqual.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public bool PathsAreEqual(string pathA, string pathB)
1313
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1414
return _underlyingFileSystem.Path.AreEqual(pathA, pathB);
1515

16-
var normalizedA = PathNormalizer.Normalize(pathA, LinuxDirectorySeparatorNormalizeOptions);
17-
var normalizedB = PathNormalizer.Normalize(pathB, LinuxDirectorySeparatorNormalizeOptions);
16+
var normalizedA = PathNormalizer.Normalize(pathA, PGFileSystemDirectorySeparatorNormalizeOptions);
17+
var normalizedB = PathNormalizer.Normalize(pathB, PGFileSystemDirectorySeparatorNormalizeOptions);
1818

1919
var fullA = _underlyingFileSystem.Path.GetFullPath(normalizedA);
2020
var fullB = _underlyingFileSystem.Path.GetFullPath(normalizedB);

src/PetroglyphTools/PG.StarWarsGame.Engine.FileSystem/IO/PetroglyphFileSystem.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ namespace PG.StarWarsGame.Engine.IO;
1111
/// A file system abstraction for the Petroglyph game engine.
1212
/// </summary>
1313
/// <remarks>
14-
/// The file system enforces Windows behavior for all its methods independent of the current operating system.
14+
/// This file system abstraction simulates Windows-like behavior for all its public methods on Linux,
15+
/// such as correct handling of paths containing backslashes ("\"). On Windows itself the behavior is unchanged.
1516
/// </remarks>
1617
public sealed partial class PetroglyphFileSystem
1718
{
1819
private const char DirectorySeparatorChar = '/';
1920
private const char AltDirectorySeparatorChar = '\\';
2021

21-
private static readonly PathNormalizeOptions LinuxDirectorySeparatorNormalizeOptions = new()
22+
// ReSharper disable once InconsistentNaming
23+
private static readonly PathNormalizeOptions PGFileSystemDirectorySeparatorNormalizeOptions = new()
2224
{
2325
TreatBackslashAsSeparator = true, // Ensure that we treat backslashes as separators on Linux
2426
UnifyDirectorySeparators = true,
@@ -51,7 +53,7 @@ internal FileSystemStream OpenRead(string filePath)
5153

5254
private static bool IsPathRooted(ReadOnlySpan<char> path)
5355
{
54-
// The original implementation, of course, also checks for drive signatures (e.g, c:, X:).
56+
// The original implementation, obviously, also checks for drive signatures (e.g, c:, X:).
5557
// We don't expect such paths ever when running in linux mode, so we simply ignore these
5658
var length = path.Length;
5759
return length >= 1 && IsDirectorySeparator(path[0]);
@@ -89,6 +91,6 @@ private static bool IsEffectivelyEmpty(ReadOnlySpan<char> path)
8991
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9092
private static bool IsDirectorySeparator(char c)
9193
{
92-
return c is '\\' or '/';
94+
return c is DirectorySeparatorChar or AltDirectorySeparatorChar;
9395
}
9496
}

0 commit comments

Comments
 (0)