Skip to content

Commit

Permalink
Normalize directory separator characters
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxEG committed May 12, 2024
1 parent 4954911 commit f751276
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Sharp.BSA.BA2/BA2Util/BA2.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SharpBSABA2.Enums;
using SharpBSABA2.Extensions;
using SharpBSABA2.Utils;
using System;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -48,7 +49,9 @@ protected override void Open(string filePath)
for (int i = 0; i < this.FileCount; i++)
try
{
this.Files[i].FullPath = BinaryReader.ReadString(BinaryReader.ReadInt16());
string fullPath = BinaryReader.ReadString(BinaryReader.ReadInt16());

this.Files[i].FullPath = PathUtils.NormalizePath(fullPath);
this.Files[i].FullPathOriginal = this.Files[i].FullPath;
}
catch (Exception ex)
Expand Down
7 changes: 3 additions & 4 deletions Sharp.BSA.BA2/BSAUtil/BSA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected override void Open(string filePath)
for (int i = 0; i < header.FileCount; i++)
{
if (hasNameTableMW)
this.Files[i].FullPath = this.BinaryReader.ReadStringTo('\0');
this.Files[i].FullPath = PathUtils.NormalizePath(this.BinaryReader.ReadStringTo('\0'));
else
{
ulong hash = this.BinaryReader.ReadUInt64();
Expand Down Expand Up @@ -237,9 +237,8 @@ protected override void Open(string filePath)
// Read name table
for (int i = 0; i < header.FileCount; i++)
{
this.Files[i].FullPath = Path.Combine(
this.Files[i].FullPath,
this.BinaryReader.ReadStringTo('\0'));
string fullPath = Path.Combine(this.Files[i].FullPath, this.BinaryReader.ReadStringTo('\0'));
this.Files[i].FullPath = PathUtils.NormalizePath(fullPath);
this.Files[i].FullPathOriginal = this.Files[i].FullPath;
}
}
Expand Down
1 change: 1 addition & 0 deletions Sharp.BSA.BA2/Sharp.BSA.BA2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\CompressionUtils.cs" />
<Compile Include="Utils\MorrowindNameTable.cs" />
<Compile Include="Utils\PathUtils.cs" />
<Compile Include="Utils\StreamUtils.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
29 changes: 29 additions & 0 deletions Sharp.BSA.BA2/Utils/PathUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.IO;

namespace SharpBSABA2.Utils
{
public static class PathUtils
{
/// <summary>
/// Returns the path with the directory separator character normalized to the platform's default.
/// </summary>
/// <param name="path">The path to normalize.</param>
public static string NormalizePath(string path) => NormalizePath(path, Path.DirectorySeparatorChar);

/// <summary>
/// Returns the path with the directory separator character normalized to the specified character.
/// </summary>
/// <param name="path">The path to normalize.</param>
/// <param name="directorySeparatorChar">The character to normalize the directory separator to.</param>
public static string NormalizePath(string path, char directorySeparatorChar)
{
// Replace forward slashes with backward slashes
path = path.Replace('/', directorySeparatorChar);

// Replace backward slashes with forward slashes
path = path.Replace('\\', directorySeparatorChar);

return path;
}
}
}

0 comments on commit f751276

Please sign in to comment.