Skip to content

Commit a137506

Browse files
committed
replace 3rd party endian bit converter with the new built-in functions
1 parent 6c02eac commit a137506

File tree

8 files changed

+45
-33
lines changed

8 files changed

+45
-33
lines changed

Ps3DiscDumper/Ps3DiscDumper.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="EndianBitConverter" Version="1.1.0" />
1312
<PackageReference Include="System.Management" Version="7.0.0" />
14-
<!-- explicit references to fix msbuild complaints -->
15-
<PackageReference Include="System.Buffers" Version="4.5.1" />
16-
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
17-
<PackageReference Include="System.Text.Json" Version="7.0.2" />
18-
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
1913
</ItemGroup>
2014

2115
<ItemGroup>

Ps3DiscDumper/Sfb/Sfb.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
using System.Collections.Generic;
2-
using System.Text;
3-
using BitConverter;
1+
using System;
2+
using System.Collections.Generic;
43

54
namespace Ps3DiscDumper.Sfb
65
{
76
public class Sfb
87
{
9-
public static int Magic = EndianBitConverter.BigEndian.ToInt32(Encoding.ASCII.GetBytes(".SFB"), 0);
8+
public static readonly int Magic = BitConverter.ToInt32(".SFB"u8);
109
public short VersionMajor;
1110
public short VersionMinor;
1211
public byte[] Unknown1; // 0x18
13-
public List<SfbKeyEntry> KeyEntries = new();
12+
public readonly List<SfbKeyEntry> KeyEntries = new();
1413
}
1514

1615
public class SfbKeyEntry

Ps3DiscDumper/Sfb/SfbReader.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
using System;
2+
using System.Buffers.Binary;
23
using System.IO;
34
using System.Text;
4-
using BitConverter;
55

66
namespace Ps3DiscDumper.Sfb
77
{
88
public static class SfbReader
99
{
10-
public static Ps3DiscDumper.Sfb.Sfb Parse(byte[] content)
10+
public static Sfb Parse(byte[] content)
1111
{
1212
if (content == null)
1313
throw new ArgumentNullException(nameof(content));
1414

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

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

21-
var result = new Ps3DiscDumper.Sfb.Sfb();
21+
var result = new Sfb();
2222
using var stream = new MemoryStream(content, false);
2323
using var reader = new BinaryReader(stream, Encoding.ASCII);
2424
reader.ReadInt32(); // magic
25-
result.VersionMajor = EndianBitConverter.BigEndian.ToInt16(reader.ReadBytes(2), 0);
26-
result.VersionMinor = EndianBitConverter.BigEndian.ToInt16(reader.ReadBytes(2), 0);
25+
result.VersionMajor = BinaryPrimitives.ReadInt16BigEndian(reader.ReadBytes(2));
26+
result.VersionMinor = BinaryPrimitives.ReadInt16BigEndian(reader.ReadBytes(2));
2727
result.Unknown1 = reader.ReadBytes(0x18);
2828
do
2929
{
@@ -32,8 +32,8 @@ public static Ps3DiscDumper.Sfb.Sfb Parse(byte[] content)
3232
if (string.IsNullOrEmpty(keyEntry.Key))
3333
break;
3434

35-
keyEntry.ValueOffset = EndianBitConverter.BigEndian.ToInt32(reader.ReadBytes(4), 0);
36-
keyEntry.ValueLength = EndianBitConverter.BigEndian.ToInt32(reader.ReadBytes(4), 0);
35+
keyEntry.ValueOffset = BinaryPrimitives.ReadInt32BigEndian(reader.ReadBytes(4));
36+
keyEntry.ValueLength = BinaryPrimitives.ReadInt32BigEndian(reader.ReadBytes(4));
3737
keyEntry.Unknown = reader.ReadInt64();
3838
result.KeyEntries.Add(keyEntry);
3939
} while (true);

Tests/BitFiddlingTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Buffers.Binary;
2+
using NUnit.Framework;
3+
4+
namespace Tests;
5+
6+
[TestFixture]
7+
public class BitFiddlingTests
8+
{
9+
[Test]
10+
public void StringToInt()
11+
{
12+
Assert.That(BinaryPrimitives.ReadInt32BigEndian(".SFB"u8), Is.EqualTo(0x2e534642));
13+
}
14+
}

Tests/IrdTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task FileStructureParseTest(string productCode, int expectedFileCou
2525
{
2626
var searchResults = await Client.GetFullListAsync(CancellationToken.None).ConfigureAwait(false);
2727
searchResults = searchResults.Where(i => i.StartsWith(productCode, StringComparison.OrdinalIgnoreCase)).ToList();
28-
Assert.That(searchResults.Count, Is.EqualTo(1));
28+
Assert.That(searchResults, Has.Count.EqualTo(1));
2929

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

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

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

93-
Assert.That(groupedStats.Count, Is.EqualTo(1));
93+
Assert.That(groupedStats, Has.Count.EqualTo(1));
9494
}
9595

9696
private static MemoryStream GetDecompressHeader(Ird ird)

Tests/Tests.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
1011
<PackageReference Include="nunit" Version="3.13.3" />
1112
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1">
1213
<PrivateAssets>all</PrivateAssets>
1314
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1415
</PackageReference>
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
16+
<PackageReference Include="NUnit.Analyzers" Version="3.6.0">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
20+
<PackageReference Include="coverlet.collector" Version="3.2.0">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
</PackageReference>
1624
</ItemGroup>
1725

1826
<ItemGroup>

UI.WinForms.Msil/UI.WinForms.Msil.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
<PackageReference Include="DotNetZip">
2626
<Version>1.16.0</Version>
2727
</PackageReference>
28-
<PackageReference Include="EndianBitConverter">
29-
<Version>1.1.0</Version>
30-
</PackageReference>
3128
<PackageReference Include="Microsoft-WindowsAPICodePack-Shell" Version="1.1.4" />
3229
<PackageReference Include="Microsoft.AspNet.WebApi.Client">
3330
<Version>5.2.9</Version>

publish.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Clear-Host
44
if ($PSVersionTable.PSVersion.Major -lt 6)
55
{
66
Write-Host 'Restarting using pwsh...'
7-
&pwsh $PSCommandPath
7+
pwsh $PSCommandPath
88
return
99
}
1010

@@ -15,13 +15,13 @@ Remove-Item -LiteralPath UI.WinForms.Msil/obj -Recurse -Force -ErrorAction Silen
1515
if (($PSVersionTable.Platform -eq 'Win32NT') -or $IsWindows)
1616
{
1717
Write-Host 'Building Windows binary...' -ForegroundColor Cyan
18-
&dotnet build -v:q -r win-x64 --self-contained -c Release UI.WinForms.Msil/UI.WinForms.Msil.csproj
19-
&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
18+
dotnet build -v:q -r win-x64 --self-contained -c Release UI.WinForms.Msil/UI.WinForms.Msil.csproj
19+
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
2020
}
2121

2222
Write-Host 'Building Linux binary...' -ForegroundColor Cyan
23-
&dotnet build -v:q -r linux-x64 --self-contained -c Release UI.Console/UI.Console.csproj
24-
&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
23+
dotnet build -v:q -r linux-x64 --self-contained -c Release UI.Console/UI.Console.csproj
24+
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
2525
if (($LASTEXITCODE -eq 0) -and (($PSVersionTable.Platform -eq 'Unix') -or $IsLinux))
2626
{
2727
chmod +x distrib/cli/lin/ps3-disc-dumper

0 commit comments

Comments
 (0)