Skip to content

Commit

Permalink
Replace RSAPatcher with a Harmony patch, reduces launch time
Browse files Browse the repository at this point in the history
  • Loading branch information
FailedShack committed Jun 17, 2019
1 parent b926dd4 commit d27817c
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 132 deletions.
3 changes: 3 additions & 0 deletions USBHelperInjector/Contracts/IInjectorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public interface IInjectorService
[OperationContract]
void SetDonationKey(string donationKey);

[OperationContract]
void SetPublicKey(string publicKey);

[OperationContract]
void SetDownloaderMaxRetries(int maxRetries);

Expand Down
5 changes: 5 additions & 0 deletions USBHelperInjector/InjectorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public void SetDonationKey(string donationKey)
Overrides.DonationKey = donationKey;
}

public void SetPublicKey(string publicKey)
{
Overrides.PublicKey = publicKey;
}

public void SetDownloaderMaxRetries(int maxRetries)
{
Overrides.MaxRetries = maxRetries;
Expand Down
2 changes: 2 additions & 0 deletions USBHelperInjector/Overrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Overrides

public static string DonationKey { get; set; }

public static string PublicKey { get; set; }

public static bool ForceKeySiteForm { get; set; }

public static WebProxy Proxy { get; set; }
Expand Down
28 changes: 28 additions & 0 deletions USBHelperInjector/Patches/PublicKeyPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Harmony;
using System.Linq;
using System.Reflection;

namespace USBHelperInjector.Patches
{
[HarmonyPatch]
class PublicKeyPatch
{
static MethodBase TargetMethod()
{
return (from type in ReflectionHelper.MainModule.GetTypes()
from prop in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Static)
where prop.Name == "keysPub"
select prop).FirstOrDefault().GetGetMethod(true);
}

static bool Prefix(ref string __result)
{
if (Overrides.PublicKey != null)
{
__result = Overrides.PublicKey;
return false;
}
return true;
}
}
}
1 change: 1 addition & 0 deletions USBHelperInjector/USBHelperInjector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Patches\KeySiteErrorMessagePatch.cs" />
<Compile Include="Patches\LanguagePatch.cs" />
<Compile Include="Patches\MessageBoxPatch.cs" />
<Compile Include="Patches\PublicKeyPatch.cs" />
<Compile Include="Patches\SearchPatch.cs" />
<Compile Include="Patches\ServicePointPatch.cs" />
<Compile Include="Patches\SettingsDonationKeyPatches.cs" />
Expand Down
3 changes: 2 additions & 1 deletion USBHelperLauncher/DebugMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public async Task<string> Build()
DateTime now = DateTime.UtcNow;
var hosts = Program.Hosts;
var av = new Dictionary<string, bool>();
sb.Append('-', 10).Append(" USBHelperLauncher Debug Information ").Append('-', 10).AppendLine();
sb.Append('-', 13).Append(" USBHelperLauncher Debug Information ").Append('-', 13).AppendLine();
sb.AppendLine("Debug Time: " + now + " (UTC)");
sb.AppendLine("Session Length: " + (now - Program.GetSessionStart()).ToString(@"hh\:mm\:ss"));
sb.AppendLine("Session GUID: " + Program.GetSessionGuid().ToString());
sb.AppendLine("Proxy Available: " + (exception == null ? "Yes" : "No (" + exception.Message + ")"));
sb.AppendLine("Public Key Override: " + (Program.OverridePublicKey ? "Yes" : "No"));
sb.AppendLine("Version: " + Program.GetVersion());
sb.AppendLine("Helper Version: " + Program.GetHelperVersion());
sb.AppendLine(".NET Framework Version: " + Get45or451FromRegistry());
Expand Down
3 changes: 2 additions & 1 deletion USBHelperLauncher/LauncherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public void SendInjectorSettings()
Program.GetLogger().WriteLine("Sending information to injector...");
var factory = new ChannelFactory<IInjectorService>(new NetNamedPipeBinding(), "net.pipe://localhost/InjectorService");
var channel = factory.CreateChannel();
if (Program.PatchPublicKey)
if (Program.OverridePublicKey)
{
channel.SetDonationKey(Program.GenerateDonationKey());
channel.SetPublicKey(Program.PublicKey);
}
if (Settings.TitleKeys.Count == 0)
{
Expand Down
37 changes: 10 additions & 27 deletions USBHelperLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Program
private static RSAParameters rsaParams;

public static Hosts Hosts { get; set; }
public static bool PatchPublicKey { get; private set; } = true;
public static string PublicKey { get; private set; }
public static bool OverridePublicKey { get; private set; } = true;

[STAThread]
static void Main(string[] args)
Expand All @@ -61,8 +62,8 @@ static void Main(string[] args)
{
switch (group.Value)
{
case "nopatch":
PatchPublicKey = false;
case "nokey":
OverridePublicKey = false;
break;
case "showconsole":
showConsole = true;
Expand Down Expand Up @@ -229,35 +230,17 @@ static void Main(string[] args)
executable = Path.Combine(GetLauncherPath(), "Patched.exe");
injector.Inject(executable);
logger.WriteLine("Injected module initializer.");
if (PatchPublicKey)
dialog.Invoke(new Action(() => dialog.Close()));

if (OverridePublicKey)
{
dialog.Invoke(new Action(() => dialog.SetHeader("Patching...")));
RSAPatcher patcher = new RSAPatcher(executable);
string xml;
// Generate an RSA key pair for our donation keys
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsaParams = rsa.ExportParameters(true);
xml = rsa.ToXmlString(false);
}
var builder = new StringBuilder();
var element = XElement.Parse(xml);
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true
};
using (var xmlWriter = XmlWriter.Create(builder, settings))
{
element.Save(xmlWriter);
PublicKey = rsa.ToXmlString(false);
}
patcher.SetPublicKey(builder.ToString());
logger.WriteLine("Patched public key.");
}
else
{
logger.WriteLine("Patching has been disabled.");
}
dialog.Invoke(new Action(() => dialog.Close()));

// Time to launch Wii U USB Helper
sessionStart = DateTime.UtcNow;
Expand All @@ -278,7 +261,7 @@ static void Main(string[] args)
MenuItem advanced = new MenuItem("Advanced");
advanced.MenuItems.Add("Toggle Console", OnVisibilityChange);
advanced.MenuItems.Add("Clear Install", OnClearInstall);
advanced.MenuItems.Add("Generate Donation Key", OnGenerateKey).Enabled = PatchPublicKey;
advanced.MenuItems.Add("Generate Donation Key", OnGenerateKey).Enabled = OverridePublicKey;
advanced.MenuItems.Add("Hosts Editor", OnOpenHostsEditor);
advanced.MenuItems.Add("Export Sessions", OnExportSessions);
trayMenu.MenuItems.Add("Exit", OnExit);
Expand Down
4 changes: 2 additions & 2 deletions USBHelperLauncher/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// Puede especificar todos los valores o utilizar los números de compilación y de revisión predeterminados
// mediante el carácter '*', como se muestra a continuación:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.15.0.0")]
[assembly: AssemblyFileVersion("0.15.0.0")]
[assembly: AssemblyVersion("0.15.0.1")]
[assembly: AssemblyFileVersion("0.15.0.1")]
100 changes: 0 additions & 100 deletions USBHelperLauncher/RSAPatcher.cs

This file was deleted.

1 change: 0 additions & 1 deletion USBHelperLauncher/USBHelperLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
<Compile Include="Net\Endpoint.cs" />
<Compile Include="Net\RegistrationEndpoint.cs" />
<Compile Include="Net\Request.cs" />
<Compile Include="RSAPatcher.cs" />
<Compile Include="Configuration\Settings.cs" />
<Compile Include="Utils\Buffer.cs" />
<Compile Include="Utils\CitraRepoUtil.cs" />
Expand Down

0 comments on commit d27817c

Please sign in to comment.