Skip to content

Commit

Permalink
replace 3rd party endian bit converter with the new built-in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
13xforever committed Feb 24, 2023
1 parent 6c02eac commit a137506
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 33 deletions.
6 changes: 0 additions & 6 deletions Ps3DiscDumper/Ps3DiscDumper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EndianBitConverter" Version="1.1.0" />
<PackageReference Include="System.Management" Version="7.0.0" />
<!-- explicit references to fix msbuild complaints -->
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 4 additions & 5 deletions Ps3DiscDumper/Sfb/Sfb.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Collections.Generic;
using System.Text;
using BitConverter;
using System;
using System.Collections.Generic;

namespace Ps3DiscDumper.Sfb
{
public class Sfb
{
public static int Magic = EndianBitConverter.BigEndian.ToInt32(Encoding.ASCII.GetBytes(".SFB"), 0);
public static readonly int Magic = BitConverter.ToInt32(".SFB"u8);
public short VersionMajor;
public short VersionMinor;
public byte[] Unknown1; // 0x18
public List<SfbKeyEntry> KeyEntries = new();
public readonly List<SfbKeyEntry> KeyEntries = new();
}

public class SfbKeyEntry
Expand Down
16 changes: 8 additions & 8 deletions Ps3DiscDumper/Sfb/SfbReader.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using System;
using System.Buffers.Binary;
using System.IO;
using System.Text;
using BitConverter;

namespace Ps3DiscDumper.Sfb
{
public static class SfbReader
{
public static Ps3DiscDumper.Sfb.Sfb Parse(byte[] content)
public static Sfb Parse(byte[] content)
{
if (content == null)
throw new ArgumentNullException(nameof(content));

if (content.Length < 200)
throw new ArgumentException("Data is too small to be a valid SFB structure", nameof(content));

if (EndianBitConverter.BigEndian.ToInt32(content, 0) != Ps3DiscDumper.Sfb.Sfb.Magic)
if (BitConverter.ToInt32(content.AsSpan(0, 4)) != Sfb.Magic)
throw new ArgumentException("Specified file is not a valid SFB file", nameof(content));

var result = new Ps3DiscDumper.Sfb.Sfb();
var result = new Sfb();
using var stream = new MemoryStream(content, false);
using var reader = new BinaryReader(stream, Encoding.ASCII);
reader.ReadInt32(); // magic
result.VersionMajor = EndianBitConverter.BigEndian.ToInt16(reader.ReadBytes(2), 0);
result.VersionMinor = EndianBitConverter.BigEndian.ToInt16(reader.ReadBytes(2), 0);
result.VersionMajor = BinaryPrimitives.ReadInt16BigEndian(reader.ReadBytes(2));
result.VersionMinor = BinaryPrimitives.ReadInt16BigEndian(reader.ReadBytes(2));
result.Unknown1 = reader.ReadBytes(0x18);
do
{
Expand All @@ -32,8 +32,8 @@ public static Ps3DiscDumper.Sfb.Sfb Parse(byte[] content)
if (string.IsNullOrEmpty(keyEntry.Key))
break;

keyEntry.ValueOffset = EndianBitConverter.BigEndian.ToInt32(reader.ReadBytes(4), 0);
keyEntry.ValueLength = EndianBitConverter.BigEndian.ToInt32(reader.ReadBytes(4), 0);
keyEntry.ValueOffset = BinaryPrimitives.ReadInt32BigEndian(reader.ReadBytes(4));
keyEntry.ValueLength = BinaryPrimitives.ReadInt32BigEndian(reader.ReadBytes(4));
keyEntry.Unknown = reader.ReadInt64();
result.KeyEntries.Add(keyEntry);
} while (true);
Expand Down
14 changes: 14 additions & 0 deletions Tests/BitFiddlingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Buffers.Binary;
using NUnit.Framework;

namespace Tests;

[TestFixture]
public class BitFiddlingTests
{
[Test]
public void StringToInt()
{
Assert.That(BinaryPrimitives.ReadInt32BigEndian(".SFB"u8), Is.EqualTo(0x2e534642));
}
}
10 changes: 5 additions & 5 deletions Tests/IrdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task FileStructureParseTest(string productCode, int expectedFileCou
{
var searchResults = await Client.GetFullListAsync(CancellationToken.None).ConfigureAwait(false);
searchResults = searchResults.Where(i => i.StartsWith(productCode, StringComparison.OrdinalIgnoreCase)).ToList();
Assert.That(searchResults.Count, Is.EqualTo(1));
Assert.That(searchResults, Has.Count.EqualTo(1));

var ird = await Client.DownloadAsync(searchResults[0], "ird", CancellationToken.None).ConfigureAwait(false);
Assert.That(ird, Is.Not.Null);
Expand All @@ -34,7 +34,7 @@ public async Task FileStructureParseTest(string productCode, int expectedFileCou
await using var decompressedStream = GetDecompressHeader(ird);
var reader = new CDReader(decompressedStream, true, true);
var (files, _) = reader.GetFilesystemStructure();
Assert.That(files.Count, Is.EqualTo(expectedFileCount));
Assert.That(files, Has.Count.EqualTo(expectedFileCount));
}

[TestCase("BLUS31604", "0A37A83C")]
Expand All @@ -43,7 +43,7 @@ public async Task DiscDecryptionKeyTest(string productCode, string expectedKey)
{
var searchResults = await Client.GetFullListAsync(CancellationToken.None).ConfigureAwait(false);
searchResults = searchResults.Where(i => i.StartsWith(productCode, StringComparison.OrdinalIgnoreCase)).ToList();
Assert.That(searchResults.Count, Is.EqualTo(1));
Assert.That(searchResults, Has.Count.EqualTo(1));

var ird = await Client.DownloadAsync(searchResults[0], "ird", CancellationToken.None).ConfigureAwait(false);
Assert.That(ird, Is.Not.Null);
Expand All @@ -65,7 +65,7 @@ public void KeyEncryptionTest()
[Test, Explicit("Requires custom data")]
public async Task TocSizeTest()
{
var path = @"E:\FakeCDs\PS3 Games\ird";
const string path = @"E:\FakeCDs\PS3 Games\ird";
var result = new List<(string filename, long size)>();
foreach (var f in Directory.EnumerateFiles(path, "*.ird", SearchOption.TopDirectoryOnly))
{
Expand All @@ -90,7 +90,7 @@ group t by t.size into g
foreach (var s in groupedStats)
Console.WriteLine($"{s.count} items of size {s.size}");

Assert.That(groupedStats.Count, Is.EqualTo(1));
Assert.That(groupedStats, Has.Count.EqualTo(1));
}

private static MemoryStream GetDecompressHeader(Ird ird)
Expand Down
10 changes: 9 additions & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions UI.WinForms.Msil/UI.WinForms.Msil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
<PackageReference Include="DotNetZip">
<Version>1.16.0</Version>
</PackageReference>
<PackageReference Include="EndianBitConverter">
<Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft-WindowsAPICodePack-Shell" Version="1.1.4" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client">
<Version>5.2.9</Version>
Expand Down
10 changes: 5 additions & 5 deletions publish.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Clear-Host
if ($PSVersionTable.PSVersion.Major -lt 6)
{
Write-Host 'Restarting using pwsh...'
&pwsh $PSCommandPath
pwsh $PSCommandPath
return
}

Expand All @@ -15,13 +15,13 @@ Remove-Item -LiteralPath UI.WinForms.Msil/obj -Recurse -Force -ErrorAction Silen
if (($PSVersionTable.Platform -eq 'Win32NT') -or $IsWindows)
{
Write-Host 'Building Windows binary...' -ForegroundColor Cyan
&dotnet build -v:q -r win-x64 --self-contained -c Release UI.WinForms.Msil/UI.WinForms.Msil.csproj
&dotnet publish -v:q -r win-x64 --self-contained -c Release -o distrib/gui/win/ UI.WinForms.Msil/UI.WinForms.Msil.csproj /p:PublishTrimmed=false /p:PublishSingleFile=true
dotnet build -v:q -r win-x64 --self-contained -c Release UI.WinForms.Msil/UI.WinForms.Msil.csproj
dotnet publish -v:q -r win-x64 --self-contained -c Release -o distrib/gui/win/ UI.WinForms.Msil/UI.WinForms.Msil.csproj /p:PublishTrimmed=false /p:PublishSingleFile=true
}

Write-Host 'Building Linux binary...' -ForegroundColor Cyan
&dotnet build -v:q -r linux-x64 --self-contained -c Release UI.Console/UI.Console.csproj
&dotnet publish -v:q -r linux-x64 --self-contained -c Release -o distrib/cli/lin/ UI.Console/UI.Console.csproj /p:PublishTrimmed=false /p:PublishSingleFile=true
dotnet build -v:q -r linux-x64 --self-contained -c Release UI.Console/UI.Console.csproj
dotnet publish -v:q -r linux-x64 --self-contained -c Release -o distrib/cli/lin/ UI.Console/UI.Console.csproj /p:PublishTrimmed=false /p:PublishSingleFile=true
if (($LASTEXITCODE -eq 0) -and (($PSVersionTable.Platform -eq 'Unix') -or $IsLinux))
{
chmod +x distrib/cli/lin/ps3-disc-dumper
Expand Down

0 comments on commit a137506

Please sign in to comment.