From a13750666344e30b00097581a5bd52bbef6a77d6 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Fri, 24 Feb 2023 11:32:00 +0500 Subject: [PATCH] replace 3rd party endian bit converter with the new built-in functions --- Ps3DiscDumper/Ps3DiscDumper.csproj | 6 ------ Ps3DiscDumper/Sfb/Sfb.cs | 9 ++++----- Ps3DiscDumper/Sfb/SfbReader.cs | 16 ++++++++-------- Tests/BitFiddlingTests.cs | 14 ++++++++++++++ Tests/IrdTests.cs | 10 +++++----- Tests/Tests.csproj | 10 +++++++++- UI.WinForms.Msil/UI.WinForms.Msil.csproj | 3 --- publish.ps1 | 10 +++++----- 8 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 Tests/BitFiddlingTests.cs diff --git a/Ps3DiscDumper/Ps3DiscDumper.csproj b/Ps3DiscDumper/Ps3DiscDumper.csproj index edbf75c..d3b628b 100644 --- a/Ps3DiscDumper/Ps3DiscDumper.csproj +++ b/Ps3DiscDumper/Ps3DiscDumper.csproj @@ -9,13 +9,7 @@ - - - - - - diff --git a/Ps3DiscDumper/Sfb/Sfb.cs b/Ps3DiscDumper/Sfb/Sfb.cs index 76291bd..2950265 100644 --- a/Ps3DiscDumper/Sfb/Sfb.cs +++ b/Ps3DiscDumper/Sfb/Sfb.cs @@ -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 KeyEntries = new(); + public readonly List KeyEntries = new(); } public class SfbKeyEntry diff --git a/Ps3DiscDumper/Sfb/SfbReader.cs b/Ps3DiscDumper/Sfb/SfbReader.cs index 26601cd..b592315 100644 --- a/Ps3DiscDumper/Sfb/SfbReader.cs +++ b/Ps3DiscDumper/Sfb/SfbReader.cs @@ -1,13 +1,13 @@ 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)); @@ -15,15 +15,15 @@ public static Ps3DiscDumper.Sfb.Sfb Parse(byte[] 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 { @@ -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); diff --git a/Tests/BitFiddlingTests.cs b/Tests/BitFiddlingTests.cs new file mode 100644 index 0000000..1215d5a --- /dev/null +++ b/Tests/BitFiddlingTests.cs @@ -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)); + } +} \ No newline at end of file diff --git a/Tests/IrdTests.cs b/Tests/IrdTests.cs index 26eef51..72e09c4 100644 --- a/Tests/IrdTests.cs +++ b/Tests/IrdTests.cs @@ -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); @@ -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")] @@ -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); @@ -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)) { @@ -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) diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 14c4821..2e918f8 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -7,12 +7,20 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/UI.WinForms.Msil/UI.WinForms.Msil.csproj b/UI.WinForms.Msil/UI.WinForms.Msil.csproj index e1b5bcd..c05faba 100644 --- a/UI.WinForms.Msil/UI.WinForms.Msil.csproj +++ b/UI.WinForms.Msil/UI.WinForms.Msil.csproj @@ -25,9 +25,6 @@ 1.16.0 - - 1.1.0 - 5.2.9 diff --git a/publish.ps1 b/publish.ps1 index ac22286..d9dc283 100644 --- a/publish.ps1 +++ b/publish.ps1 @@ -4,7 +4,7 @@ Clear-Host if ($PSVersionTable.PSVersion.Major -lt 6) { Write-Host 'Restarting using pwsh...' - &pwsh $PSCommandPath + pwsh $PSCommandPath return } @@ -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