Skip to content

Commit

Permalink
Merge branch 'main' into osx
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley authored May 7, 2024
2 parents 35196dd + 0cb2737 commit 1cc2d95
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 83 deletions.
20 changes: 17 additions & 3 deletions Refresher/Accessors/ConsolePatchAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Net;
using System.Net.Sockets;
using FluentFTP;
using Refresher.Exceptions;

Expand Down Expand Up @@ -48,8 +46,24 @@ public ConsolePatchAccessor(string remoteIp)

private HttpResponseMessage? IdpsRequestStep(ReadOnlySpan<char> stepName, HttpClient client, Uri uri)
{
HttpResponseMessage response;

Program.Log($" {stepName} ({uri.AbsolutePath})", "IDPS");
HttpResponseMessage response = client.GetAsync(uri).Result;
try
{
response = client.GetAsync(uri).Result;
}
catch (HttpRequestException e)
{
Program.Log($"Couldn't fetch the IDPS from the PS3 because we couldn't make the request: {e.Message}", "IDPS", BreadcrumbLevel.Error);
return null;
}
catch (Exception e)
{
Program.Log($"Couldn't fetch the IDPS from the PS3 because of an unknown error: {e}", "IDPS", BreadcrumbLevel.Error);
SentrySdk.CaptureException(e);
return null;
}
Program.Log($" {(int)response.StatusCode} {response.StatusCode} (success: {response.IsSuccessStatusCode})", "IDPS");

if (!response.IsSuccessStatusCode)
Expand Down
49 changes: 40 additions & 9 deletions Refresher/Patching/EbootPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
using System.Buffers.Binary;
using System.Collections.Frozen;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using ELFSharp.ELF;
using ELFSharp.ELF.Segments;
using Refresher.Verification;

namespace Refresher.Patching;

public partial class EbootPatcher : IPatcher
{
private readonly Lazy<List<PatchTargetInfo>> _targets;
private readonly Lazy<string?> _ppuHash = null;

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in Refresher/Patching/EbootPatcher.cs

View workflow job for this annotation

GitHub Actions / Build, Test, and Upload Builds (macos-latest)

Cannot convert null literal to non-nullable reference type.

public bool GenerateRpcs3Patch = false;
public string? Rpcs3PatchFolder = null;
public string? PpuHash = null;
public string? GameVersion = null;
public string? GameName;
public string? TitleId;
Expand All @@ -29,6 +32,7 @@ public EbootPatcher(Stream stream)
this.Stream.Position = 0;

this._targets = new Lazy<List<PatchTargetInfo>>(() => FindPatchableElements(stream));
this._ppuHash = new Lazy<string?>(() => GeneratePpuHash(stream));
}

public Stream Stream { get; }
Expand Down Expand Up @@ -308,6 +312,38 @@ private static void FindDigestAroundCookie(BinaryReader reader, List<long> found
}
}
}

public static string GeneratePpuHash(Stream stream)
{
stream.Position = 0;
ELF<ulong> elfFile = ELFReader.Load<ulong>(stream, false);

using SHA1 hash = SHA1.Create();
foreach (Segment<ulong>? segment in elfFile.Segments)
{
hash.TransformBlock(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness((uint)segment.Type)), 0, 4, null, 0);
hash.TransformBlock(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness((uint)segment.Flags)), 0, 4, null, 0);

if (segment.Type != SegmentType.Load || segment.Size == 0) continue;

hash.TransformBlock(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness(segment.Address)), 0, 8, null, 0);
hash.TransformBlock(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness(segment.Size)), 0, 8, null, 0);

byte[]? segmentData = segment.GetFileContents();

hash.TransformBlock(segmentData, 0, segmentData.Length, null, 0);
}

// trigger generation of hash after reading segments
hash.TransformFinalBlock([], 0, 0);

stream.Position = 0;

string ppuHash = BitConverter.ToString(hash.Hash!).Replace("-", "").ToLower();

Program.Log($"PPU hash: PPU-{ppuHash}", "PPU", BreadcrumbLevel.Debug);
return ppuHash;
}

/// <summary>
/// Checks the contents of the EBOOT to verify that it is patchable.
Expand Down Expand Up @@ -360,10 +396,10 @@ public List<Message> Verify(string url, bool patchDigest)

if (this.GenerateRpcs3Patch)
{
if (string.IsNullOrWhiteSpace(this.PpuHash))
if (string.IsNullOrWhiteSpace(this._ppuHash.Value))
{
messages.Add(new Message(MessageLevel.Error,
"Missing PPU hash! This is used by RPCS3 to know which game the patch is used for, please read the RPCS3 patching guide to learn how to get this value."));
"Couldn't determine the PPU hash. This is used by RPCS3 to know which game the patch is used for. Without it, we cannot continue patching."));
}
}

Expand All @@ -377,21 +413,16 @@ public void Patch(string url, bool patchDigest)
Debug.Assert(this.Rpcs3PatchFolder != null);
Debug.Assert(this.GameName != null);
Debug.Assert(this.TitleId != null);
Debug.Assert(!string.IsNullOrEmpty(this.PpuHash));
Debug.Assert(!string.IsNullOrEmpty(this.GameVersion));

string patchesFile = Path.Combine(this.Rpcs3PatchFolder, "imported_patch.yml");

if (!File.Exists(patchesFile))
//Write the header to the patches file
File.WriteAllText(patchesFile, "Version: 1.2\n\n");

//Trim the PPU- prefix, if its there
if (this.PpuHash.StartsWith("PPU-"))
this.PpuHash = this.PpuHash[4..];

string template = $"""
PPU-{this.PpuHash}:
PPU-{this._ppuHash}:
"Refresher Patch ({url})":
Games:
"{this.GameName}":
Expand Down
13 changes: 7 additions & 6 deletions Refresher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CommandLine;
using Eto.Forms;
using Refresher.CLI;
using Refresher.Patching;
using Refresher.UI;
using Sentry;

Expand Down Expand Up @@ -70,10 +71,10 @@ public static void Main(string[] args)
SentrySdk.CaptureException((Exception)eventArgs.ExceptionObject);
SentrySdk.Flush();
MessageBox.Show($"""
There was an unhandled error in Refresher!
*Please* screenshot this message box and send it to us over GitHub or Discord with details on what you were doing. This is likely a bug in Refresher.
There was an unhandled error in Refresher.
This has been automatically reported to us. The exception details has been displayed for further debugging:
Exception details: {eventArgs.ExceptionObject}
{eventArgs.ExceptionObject}
""",
"Critical Error");

Expand All @@ -88,10 +89,10 @@ There was an unhandled error in Refresher!
SentrySdk.CaptureException(ex);
SentrySdk.Flush();
MessageBox.Show($"""
There was an unhandled error in Refresher!
*Please* screenshot this message box and send it to us over GitHub or Discord with details on what you were doing. This is likely a bug in Refresher.
There was an unhandled error in Refresher.
This has been automatically reported to us. The exception details has been displayed for further debugging:
Exception details: {ex}
{ex}
""", "Critical Error");
}
App.Dispose();
Expand Down
19 changes: 2 additions & 17 deletions Refresher/UI/EmulatorPatchForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Refresher.UI;
public class EmulatorPatchForm : IntegratedPatchForm
{
private FilePicker _folderField = null!;
private TextBox _ppuHash = null!;
private Button _filePatchButton = null!;

public EmulatorPatchForm() : base("RPCS3 Patch")
Expand All @@ -33,17 +32,6 @@ public EmulatorPatchForm() : base("RPCS3 Patch")
this.LogMessage("RPCS3's path has been detected automatically! You do not need to change the path.");
}
}

this._ppuHash.TextChanged += this.UpdateTextFields;
}

private void UpdateTextFields(object? sender, EventArgs args)
{
if (this.Patcher != null)
{
this.Patcher.PpuHash = this._ppuHash.Text;
this.Reverify(null, EventArgs.Empty);
}
}

protected override void BeforePatch(object? sender, EventArgs e)
Expand All @@ -57,11 +45,10 @@ protected override void BeforePatch(object? sender, EventArgs e)
if (this.Patcher != null)
{
this.Patcher.GenerateRpcs3Patch = true;
this.Patcher.PpuHash = this._ppuHash.Text;
this.Patcher.GameVersion = game.Version;
this.Patcher.Rpcs3PatchFolder = Path.Combine(this._folderField.FilePath, "../patches");
this.Patcher.GameName = ((GameItem)this.GameDropdown.SelectedValue).Text;
this.Patcher.TitleId = ((GameItem)this.GameDropdown.SelectedValue).TitleId;
this.Patcher.GameName = game.Text;
this.Patcher.TitleId = game.TitleId;

try
{
Expand Down Expand Up @@ -95,7 +82,6 @@ protected override IEnumerable<TableRow> AddFields()
{
return new[]
{
AddField("Game PPU hash", out this._ppuHash),
AddField("RPCS3 dev_hdd0 folder", out this._folderField),
AddField("", out this._filePatchButton),
};
Expand All @@ -112,7 +98,6 @@ protected override void GameChanged(object? sender, EventArgs ev)
}

this.Patcher.GenerateRpcs3Patch = true;
this.UpdateTextFields(null, EventArgs.Empty);
this.Reverify(null, EventArgs.Empty);
}

Expand Down
Loading

0 comments on commit 1cc2d95

Please sign in to comment.