Skip to content

Commit

Permalink
Digital patching wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Jan 17, 2024
1 parent d3648bc commit c2c1de7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 28 deletions.
17 changes: 16 additions & 1 deletion Refresher/Accessors/ConsolePatchAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ public class ConsolePatchAccessor : PatchAccessor, IDisposable
private readonly FtpClient _client;
private const string BasePath = "/dev_hdd0/";

public string IdpsFile;

public ConsolePatchAccessor(string remoteIp)
{
this._client = new FtpClient(remoteIp, "anonymous", "");
this._client.Config.LogToConsole = true;
this._client.AutoConnect();

UriBuilder idpsPs3 = new("http", remoteIp, 80, "idps.ps3");
UriBuilder idpsHex = new("http", remoteIp, 80, "dev_hdd0/idps.hex");

HttpClient httpClient = new();

//Get the /idps.ps3 path, this creates the idps.hex file we can grab.
_ = httpClient.GetAsync(idpsPs3.Uri).Result;

//Get a new path for an IDPS file
this.IdpsFile = Path.GetTempFileName();

File.WriteAllBytes(this.IdpsFile, httpClient.GetAsync(idpsHex.Uri).Result.Content.ReadAsByteArrayAsync().Result);
}

private static string GetPath(string path)
{
if (Path.IsPathRooted(path)) return path;
Expand Down
14 changes: 7 additions & 7 deletions Refresher/Refresher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFramework Condition="!$([MSBuild]::IsOSPlatform('Windows'))">net7.0</TargetFramework>
<TargetFramework Condition="$([MSBuild]::IsOSPlatform('Windows'))">net7.0-windows</TargetFramework>
<TargetFramework Condition="!$([MSBuild]::IsOSPlatform('Windows'))">net8.0</TargetFramework>
<TargetFramework Condition="$([MSBuild]::IsOSPlatform('Windows'))">net8.0-windows</TargetFramework>
<ApplicationIcon>Resources\refresher.ico</ApplicationIcon>
<BuiltInComInteropSupport Condition="'$(TargetFramework)' == 'net7.0-windows'">true</BuiltInComInteropSupport>
</PropertyGroup>
Expand All @@ -17,13 +17,13 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="ELFSharp" Version="2.17.3" />
<PackageReference Include="Eto.Forms" Version="2.7.5" />
<PackageReference Condition="'$(TargetFramework)' != 'net7.0-windows'" Include="Eto.Platform.Gtk" Version="2.7.5" />
<PackageReference Condition="'$(TargetFramework)' == 'net7.0-windows'" Include="Eto.Platform.Wpf" Version="2.7.5" />
<PackageReference Include="Eto.Forms" Version="2.8.2" />
<PackageReference Condition="'$(TargetFramework)' != 'net7.0-windows'" Include="Eto.Platform.Gtk" Version="2.8.2" />
<PackageReference Condition="'$(TargetFramework)' == 'net7.0-windows'" Include="Eto.Platform.Wpf" Version="2.8.2" />
<EmbeddedResource Include="Resources\refresher.ico" LogicalName="refresher.ico" />
<PackageReference Include="FluentFTP" Version="48.0.3" />
<PackageReference Include="FluentFTP" Version="49.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SCEToolSharp" Version="1.0.11" />
<PackageReference Include="SCEToolSharp" Version="1.1.3" />
<EmbeddedResource Include="Resources\Allefresher.prx" />
</ItemGroup>

Expand Down
68 changes: 48 additions & 20 deletions Refresher/UI/IntegratedPatchForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ protected virtual void PathChanged(object? sender, EventArgs ev)
}
}

private readonly Dictionary<string, string> _cachedContentIds = new();

protected virtual void GameChanged(object? sender, EventArgs ev)
{
LibSceToolSharp.Init();
Expand All @@ -113,35 +115,50 @@ protected virtual void GameChanged(object? sender, EventArgs ev)

string downloadedFile = this.Accessor.DownloadFile(ebootPath);

this.LogMessage($"Downloaded EBOOT Path: {downloadedFile}");
if (!File.Exists(downloadedFile))
{
this.FailVerify("Could not find the EBOOT. Patching cannot continue.", clear: false);
return;
}

string contentId = LibSceToolSharp.GetContentId(downloadedFile).TrimEnd('\0');
this._cachedContentIds[game.TitleId] = contentId;

string licenseDir = Path.Join(Path.GetTempPath(), "refresher-" + Random.Shared.Next());
Directory.CreateDirectory(licenseDir);

// if this is a NP game then download RIFs/RAPs, disc copies don't need anything else
// if this is a NP game then download the RIF for the right content ID, disc copies don't need anything else
if (game.TitleId.StartsWith('N'))
{
// TODO: the first user might not have the licenses necessary, should download from all users
IEnumerable<string> licenseFiles = this.Accessor.GetFilesInDirectory(Path.Combine("home", "00000001", "exdata"));
foreach (string licenseFile in licenseFiles)
foreach (string user in this.Accessor.GetDirectoriesInDirectory(Path.Combine("home")))
{
// only download if it contains our game's title id
// TODO: determine content id directly so we skip dlc licenses
if(!licenseFile.Contains(game.TitleId)) continue;

string downloadedLicenseFile = this.Accessor.DownloadFile(licenseFile);
File.Move(downloadedLicenseFile, Path.Join(licenseDir, Path.GetFileName(licenseFile)));
foreach (string licenseFile in this.Accessor.GetFilesInDirectory(Path.Combine(user, "exdata")))
{
//If the license file does not contain the content ID in its path, skip it
if (!licenseFile.Contains(contentId))
continue;

//If it is a valid content id, lets download that user's exdata
string downloadedActDat = this.Accessor.DownloadFile(Path.Combine(user, "exdata", "act.dat"));
LibSceToolSharp.SetActDatFilePath(downloadedActDat);

//And the license file
string downloadedLicenseFile = this.Accessor.DownloadFile(licenseFile);
File.Move(downloadedLicenseFile, Path.Join(licenseDir, Path.GetFileName(licenseFile)));

Console.WriteLine($"Downloaded license file {licenseFile}.");
}
}
}

this.LogMessage($"Downloaded EBOOT Path: {downloadedFile}");
if (!File.Exists(downloadedFile))
{
this.FailVerify("Could not find the EBOOT. Patching cannot continue.", clear: false);
return;
}

this._tempFile = Path.GetTempFileName();

LibSceToolSharp.SetRapDirectory(licenseDir);

//If we are using the console patch accessor, fill out the IDPS patch file.
if (this.Accessor is ConsolePatchAccessor consolePatchAccessor)
LibSceToolSharp.SetIdpsFilePath(consolePatchAccessor.IdpsFile);

LibSceToolSharp.SetRifPath(licenseDir);
LibSceToolSharp.Decrypt(downloadedFile, this._tempFile);

this.LogMessage($"The EBOOT has been successfully decrypted. It's stored at {this._tempFile}.");
Expand All @@ -160,8 +177,19 @@ public override void CompletePatch(object? sender, EventArgs e) {
string fileToUpload;
if (this.NeedsResign)
{
GameItem? game = this.GameDropdown.SelectedValue as GameItem;
Debug.Assert(game != null);

string encryptedTempFile = Path.GetTempFileName();
LibSceToolSharp.SetDiscEncryptOptions();
if (game.TitleId.StartsWith('N'))
{
LibSceToolSharp.SetNpdrmEncryptOptions();
LibSceToolSharp.SetNpdrmContentId(this._cachedContentIds[game.TitleId]);
}
else
{
LibSceToolSharp.SetDiscEncryptOptions();
}
LibSceToolSharp.Encrypt(this._tempFile, encryptedTempFile);

fileToUpload = encryptedTempFile;
Expand Down

0 comments on commit c2c1de7

Please sign in to comment.