Support .NET Framework using Newtonsoft

This commit is contained in:
Deterous
2024-03-05 13:02:25 +09:00
parent 4aa6aca25e
commit ea3f6d3f96
4 changed files with 22 additions and 31 deletions
+15 -15
View File
@@ -558,7 +558,7 @@ namespace LibIRD
if (line == null) if (line == null)
throw new InvalidDataException("Could not find Disc Key in .getkey.log"); throw new InvalidDataException("Could not find Disc Key in .getkey.log");
// Get Disc Key from log // Get Disc Key from log
string discKeyStr = line["disc_key = ".Length..]; string discKeyStr = line.Substring("disc_key = ".Length);
// Validate Disc Key from log // Validate Disc Key from log
if (discKeyStr.Length != 32) if (discKeyStr.Length != 32)
throw new InvalidDataException("Unexpected Disc Key in .getkey.log"); throw new InvalidDataException("Unexpected Disc Key in .getkey.log");
@@ -571,12 +571,12 @@ namespace LibIRD
if (line == null) if (line == null)
throw new InvalidDataException("Could not find Disc ID in .getkey.log"); throw new InvalidDataException("Could not find Disc ID in .getkey.log");
// Get Disc ID from log // Get Disc ID from log
string discIDStr = line["disc_id = ".Length..]; string discIDStr = line.Substring("disc_id = ".Length);
// Validate Disc ID from log // Validate Disc ID from log
if (discIDStr.Length != 32) if (discIDStr.Length != 32)
throw new InvalidDataException("Unexpected Disc ID in .getkey.log"); throw new InvalidDataException("Unexpected Disc ID in .getkey.log");
// Replace X's in Disc ID with 00000001 // Replace X's in Disc ID with 00000001
discIDStr = discIDStr[..24] + "00000001"; discIDStr = discIDStr.Substring(0, 24) + "00000001";
// Convert Disc ID to byte array // Convert Disc ID to byte array
discID = HexStringToByteArray(discIDStr); discID = HexStringToByteArray(discIDStr);
@@ -593,7 +593,7 @@ namespace LibIRD
if (discPICStr.Length != 256) if (discPICStr.Length != 256)
throw new InvalidDataException("Unexpected PIC in .getkey.log"); throw new InvalidDataException("Unexpected PIC in .getkey.log");
// Convert PIC to byte array // Convert PIC to byte array
discPIC = HexStringToByteArray(discPICStr[..230]); discPIC = HexStringToByteArray(discPICStr.Substring(0, 230));
// Double check for warnings in .getkey.log // Double check for warnings in .getkey.log
while ((line = sr.ReadLine()) != null && line.Trim().StartsWith("WARNING") == false && line.Trim().StartsWith("SUCCESS") == false) while ((line = sr.ReadLine()) != null && line.Trim().StartsWith("WARNING") == false && line.Trim().StartsWith("SUCCESS") == false)
@@ -645,7 +645,7 @@ namespace LibIRD
bool titleIDFound = ps3_DiscSFB.Field.TryGetValue("TITLE_ID", out string titleID); bool titleIDFound = ps3_DiscSFB.Field.TryGetValue("TITLE_ID", out string titleID);
// If a valid TITLE_ID field is present, remove the hyphen to fit into standard IRD file // If a valid TITLE_ID field is present, remove the hyphen to fit into standard IRD file
if (titleIDFound && titleID.Length == 10 && titleID[4] == '-') if (titleIDFound && titleID.Length == 10 && titleID[4] == '-')
TitleID = string.Concat(titleID.AsSpan(0, 4), titleID.AsSpan(5, 5)); TitleID = titleID.Substring(0, 4) + titleID.Substring(5, 5);
// If the version field is present, this is a multi-game disc // If the version field is present, this is a multi-game disc
// Redump-style IRDs use the VERSION field from PS3_DISC.SFB instead of VERSION from PARAM.SFO // Redump-style IRDs use the VERSION field from PS3_DISC.SFB instead of VERSION from PARAM.SFO
@@ -665,7 +665,7 @@ namespace LibIRD
{ {
bool titleIDFound = paramSFO.Field.TryGetValue("TITLE_ID", out string titleID); bool titleIDFound = paramSFO.Field.TryGetValue("TITLE_ID", out string titleID);
if (titleIDFound) if (titleIDFound)
TitleID = titleID.Length == 9 ? titleID : titleID.PadRight(9, '\0')[..9]; TitleID = titleID.Length == 9 ? titleID : titleID.PadRight(9, '\0').Substring(0, 9);
else else
TitleID = "\0\0\0\0\0\0\0\0\0"; TitleID = "\0\0\0\0\0\0\0\0\0";
} }
@@ -679,7 +679,7 @@ namespace LibIRD
{ {
bool discVersionFound = paramSFO.Field.TryGetValue("VERSION", out string discVersion); bool discVersionFound = paramSFO.Field.TryGetValue("VERSION", out string discVersion);
if (discVersionFound) if (discVersionFound)
DiscVersion = discVersion.Length == 5 ? discVersion : discVersion.PadRight(5, '\0')[..5]; DiscVersion = discVersion.Length == 5 ? discVersion : discVersion.PadRight(5, '\0').Substring(0, 5);
else else
DiscVersion = "\0\0\0\0\0"; DiscVersion = "\0\0\0\0\0";
} }
@@ -687,7 +687,7 @@ namespace LibIRD
// Try use App Version from PARAM.SFO // Try use App Version from PARAM.SFO
bool appVersionFound = paramSFO.Field.TryGetValue("APP_VER", out string appVersion); bool appVersionFound = paramSFO.Field.TryGetValue("APP_VER", out string appVersion);
if (appVersionFound) if (appVersionFound)
AppVersion = appVersion.Length == 5 ? appVersion : appVersion.PadRight(5, '\0')[..5]; AppVersion = appVersion.Length == 5 ? appVersion : appVersion.PadRight(5, '\0').Substring(0, 5);
else else
AppVersion = "\0\0\0\0\0"; AppVersion = "\0\0\0\0\0";
} }
@@ -758,7 +758,7 @@ namespace LibIRD
// PS3UPDAT.PUP file begins at first byte of dedicated cluster // PS3UPDAT.PUP file begins at first byte of dedicated cluster
UpdateOffset = SectorSize * updateClusters[0].Offset; UpdateOffset = SectorSize * updateClusters[0].Offset;
// Update file ends at the last byte of the last cluster // Update file ends at the last byte of the last cluster
UpdateEnd = SectorSize * updateClusters[^1].Offset + updateClusters[^1].Count; UpdateEnd = SectorSize * updateClusters[updateClusters.Length - 1].Offset + updateClusters[updateClusters.Length - 1].Count;
// Check PUP file Magic // Check PUP file Magic
fs.Seek(UpdateOffset, SeekOrigin.Begin); fs.Seek(UpdateOffset, SeekOrigin.Begin);
@@ -886,22 +886,22 @@ namespace LibIRD
{ {
// End sector of previous region is start of this region // End sector of previous region is start of this region
if (i % 2 == 1) if (i % 2 == 1)
RegionStart[i] = BitConverter.ToInt32(regionSector) + 1; RegionStart[i] = BitConverter.ToInt32(regionSector, 0) + 1;
else else
RegionStart[i] = BitConverter.ToInt32(regionSector); RegionStart[i] = BitConverter.ToInt32(regionSector, 0);
// Determine end sector offset of this region // Determine end sector offset of this region
fs.Read(regionSector, 0, 4); fs.Read(regionSector, 0, 4);
Array.Reverse(regionSector, 0, 4); Array.Reverse(regionSector, 0, 4);
if (i % 2 == 1) if (i % 2 == 1)
RegionEnd[i] = BitConverter.ToInt32(regionSector) - 1; RegionEnd[i] = BitConverter.ToInt32(regionSector, 0) - 1;
else else
RegionEnd[i] = BitConverter.ToInt32(regionSector); RegionEnd[i] = BitConverter.ToInt32(regionSector, 0);
} }
// Remove header from first region // Remove header from first region
RegionStart[0] = FirstDataSector; RegionStart[0] = FirstDataSector;
// Remove footer from last region // Remove footer from last region
RegionEnd[^1] = (UpdateEnd / SectorSize) - 1; RegionEnd[RegionEnd.Length - 1] = (UpdateEnd / SectorSize) - 1;
} }
/// <summary> /// <summary>
@@ -1205,7 +1205,7 @@ namespace LibIRD
} }
// Check if current file has ended in this buffer (assumes last extent contains last byte) // Check if current file has ended in this buffer (assumes last extent contains last byte)
long lastByte = SectorSize * FileExtents[i][^1].Offset + FileExtents[i][^1].Count; long lastByte = SectorSize * FileExtents[i][FileExtents[i].Length - 1].Offset + FileExtents[i][FileExtents[i].Length - 1].Count;
if (lastByte < SectorSize * (currentSector + bufSectors) if (lastByte < SectorSize * (currentSector + bufSectors)
&& lastByte > SectorSize * currentSector) && lastByte > SectorSize * currentSector)
{ {
+2 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<!-- Assembly Properties --> <!-- Assembly Properties -->
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks> <TargetFrameworks>net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers> <RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
@@ -27,6 +27,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" /> <PackageReference Include="System.IO.Hashing" Version="8.0.0" />
</ItemGroup> </ItemGroup>
+3 -8
View File
@@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.Json; using Newtonsoft.Json;
namespace LibIRD namespace LibIRD
{ {
@@ -68,7 +68,7 @@ namespace LibIRD
// Read SFB file version // Read SFB file version
byte[] buf = br.ReadBytes(2); byte[] buf = br.ReadBytes(2);
Array.Reverse(buf); Array.Reverse(buf);
Version = BitConverter.ToUInt16(buf); Version = BitConverter.ToUInt16(buf, 0);
// Process all field headers // Process all field headers
sfbStream.Seek(0x20, SeekOrigin.Begin); sfbStream.Seek(0x20, SeekOrigin.Begin);
@@ -131,11 +131,6 @@ namespace LibIRD
} }
} }
/// <summary>
/// Define JSON options once
/// </summary>
private readonly JsonSerializerOptions JsonOpts = new() { WriteIndented = true };
/// <summary> /// <summary>
/// Prints parameters extracted from PS3_DISC.SFB to a json object /// Prints parameters extracted from PS3_DISC.SFB to a json object
/// </summary> /// </summary>
@@ -143,7 +138,7 @@ namespace LibIRD
public void PrintJson(string jsonPath = null) public void PrintJson(string jsonPath = null)
{ {
// Serialise PS3_Disc.SFB data to a JSON object // Serialise PS3_Disc.SFB data to a JSON object
string json = JsonSerializer.Serialize(Field, JsonOpts); string json = JsonConvert.SerializeObject(Field, Formatting.Indented);
// If no path given, output to console // If no path given, output to console
if (jsonPath == null) if (jsonPath == null)
+2 -7
View File
@@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.Json; using Newtonsoft.Json;
namespace LibIRD namespace LibIRD
{ {
@@ -154,11 +154,6 @@ namespace LibIRD
} }
} }
/// <summary>
/// Define JSON options once
/// </summary>
private readonly JsonSerializerOptions JsonOpts = new() { WriteIndented = true };
/// <summary> /// <summary>
/// Prints parameters extracted from PARAM.SFO to a json object /// Prints parameters extracted from PARAM.SFO to a json object
/// </summary> /// </summary>
@@ -166,7 +161,7 @@ namespace LibIRD
public void PrintJson(string jsonPath = null) public void PrintJson(string jsonPath = null)
{ {
// Serialise PS3_Disc.SFB data to a JSON object // Serialise PS3_Disc.SFB data to a JSON object
string json = JsonSerializer.Serialize(Field, JsonOpts); string json = JsonConvert.SerializeObject(Field, Formatting.Indented);
// If no path given, output to console // If no path given, output to console
if (jsonPath == null) if (jsonPath == null)