Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rare issues and add some QoL features #68

Merged
merged 14 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ServiceModel;

namespace USBHelperInjector.Contracts
namespace USBHelperInjector.IPC
{
[ServiceContract]
public interface IInjectorService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ServiceModel;
using System.Threading.Tasks;
using System;
using System.ServiceModel;

namespace USBHelperInjector.Contracts
namespace USBHelperInjector.IPC
{
[ServiceContract]
public interface ILauncherService
Expand All @@ -10,6 +10,6 @@ public interface ILauncherService
void SetKeySite(string site, string url);

[OperationContract]
void SendInjectorSettings();
void SendInjectorSettings(Uri uri);
}
}
12 changes: 12 additions & 0 deletions USBHelperInjector/IPC/IPCType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace USBHelperInjector.IPC
{
[JsonConverter(typeof(StringEnumConverter))]
public enum IPCType
{
NamedPipe,
TCP
}
}
78 changes: 78 additions & 0 deletions USBHelperInjector/IPC/IPCUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace USBHelperInjector.IPC
{
public class IPCUtils
shiftinv marked this conversation as resolved.
Show resolved Hide resolved
{
public static ServiceHost CreateService(IPCType ipcType, Type serviceType, Type contractType, out Uri uri)
{
switch (ipcType)
{
case IPCType.NamedPipe:
return CreateNamedPipeService(serviceType, contractType, out uri);
case IPCType.TCP:
return CreateTcpService(serviceType, contractType, out uri);
default:
throw new InvalidEnumArgumentException(nameof(ipcType), (int)ipcType, ipcType.GetType());
}
}

public static ServiceHost CreateNamedPipeService(Type serviceType, Type contractType, out Uri uri)
{
var guid = Guid.NewGuid().ToString("D");
var host = new ServiceHost(serviceType, new Uri($"net.pipe://localhost/{guid}"));
host.AddServiceEndpoint(contractType, GetNamedPipeBinding(), "");
host.Open();
uri = host.ChannelDispatchers.First().Listener.Uri;
return host;
}

public static ServiceHost CreateTcpService(Type serviceType, Type contractType, out Uri uri)
{
var host = new ServiceHost(serviceType, new Uri("net.tcp://127.0.0.1:0"));
var endpoint = host.AddServiceEndpoint(contractType, GetTcpBinding(), "", new Uri("net.tcp://127.0.0.1"));
shiftinv marked this conversation as resolved.
Show resolved Hide resolved
endpoint.ListenUriMode = ListenUriMode.Unique;
host.Open();
uri = host.ChannelDispatchers.First().Listener.Uri;
return host;
}


public static TContract CreateChannel<TContract>(IPCType ipcType, string address)
{
Binding binding;
switch (ipcType)
{
case IPCType.NamedPipe:
binding = GetNamedPipeBinding();
break;
case IPCType.TCP:
binding = GetTcpBinding();
break;
default:
throw new InvalidEnumArgumentException(nameof(ipcType), (int)ipcType, ipcType.GetType());
}

var factory = new ChannelFactory<TContract>(binding, address);
return factory.CreateChannel();
}


private static NetNamedPipeBinding GetNamedPipeBinding()
shiftinv marked this conversation as resolved.
Show resolved Hide resolved
{
return new NetNamedPipeBinding("");
}
private static NetTcpBinding GetTcpBinding()
{
return new NetTcpBinding("")
{
Security = { Mode = SecurityMode.None }
shiftinv marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
}
20 changes: 13 additions & 7 deletions USBHelperInjector/InjectorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using USBHelperInjector.Contracts;
using USBHelperInjector.IPC;
using USBHelperInjector.Patches.Attributes;

namespace USBHelperInjector
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class InjectorService : IInjectorService
{
public static Harmony Harmony { get; private set; }
Expand All @@ -27,14 +28,19 @@ public class InjectorService : IInjectorService

public static void Init()
{
var factory = new ChannelFactory<ILauncherService>(new NetNamedPipeBinding(""), "net.pipe://localhost/LauncherService");
LauncherService = factory.CreateChannel();
var args = Environment.GetCommandLineArgs();
var ipcType = (IPCType)Enum.Parse(typeof(IPCType), args[2]);
var launcherUri = args[3];

ServiceHost host = new ServiceHost(typeof(InjectorService), new Uri("net.pipe://localhost/InjectorService"));
host.AddServiceEndpoint(typeof(IInjectorService), new NetNamedPipeBinding(""), "");
host.Open();
IPCUtils.CreateService(
ipcType,
typeof(InjectorService),
typeof(IInjectorService),
out var serviceUri
);

LauncherService.SendInjectorSettings();
LauncherService = IPCUtils.CreateChannel<ILauncherService>(ipcType, launcherUri);
LauncherService.SendInjectorSettings(serviceUri);

Harmony = new Harmony("me.failedshack.usbhelperinjector");
var assembly = Assembly.GetExecutingAssembly();
Expand Down
15 changes: 15 additions & 0 deletions USBHelperInjector/ModuleInitInjectedAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace USBHelperInjector
{
[AttributeUsage(AttributeTargets.Assembly)]
public class ModuleInitInjectedAttribute : Attribute
{
public string Version { get; }

public ModuleInitInjectedAttribute(string version)
{
Version = version;
}
}
}
21 changes: 21 additions & 0 deletions USBHelperInjector/Patches/CommandLineArgsPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

using System.Linq;
using System.Reflection;
using HarmonyLib;

namespace USBHelperInjector.Patches
{
[HarmonyPatch]
class CommandLineArgsPatch
{
static MethodBase TargetMethod()
{
return ReflectionHelper.EntryPoint;
}

static void Prefix(ref string[] __0)
{
__0 = __0.Take(1).ToArray();
}
}
}
30 changes: 30 additions & 0 deletions USBHelperInjector/Patches/DownloadManagerOverlayPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using USBHelperInjector.Patches.Attributes;

namespace USBHelperInjector.Patches
{
[Optional]
[HarmonyPatch]
class DownloadManagerOverlayPatch
{
static MethodBase TargetMethod()
{
return AccessTools.DeclaredProperty(ReflectionHelper.Settings, "ShowDownloadManagerTip").GetGetMethod(true);
}

static bool Prefix(ref bool __result)
{
// Check if RivaTuner hooks are loaded into the process; RivaTuner can sometimes break d3d9
var modules = Process.GetCurrentProcess().Modules.Cast<ProcessModule>();
if (modules.Any(m => m.ModuleName.ToLowerInvariant().StartsWith("rtsshooks")))
{
__result = false;
return false;
}
return true;
}
}
}
40 changes: 40 additions & 0 deletions USBHelperInjector/Patches/InvalidConfigPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using HarmonyLib;
using USBHelperInjector.Patches.Attributes;

namespace USBHelperInjector.Patches
{
[Optional]
[HarmonyPatch]
class InvalidConfigPatch
{
static MethodBase TargetMethod()
{
return ReflectionHelper.Settings.GetConstructor(new Type[] { });
shiftinv marked this conversation as resolved.
Show resolved Hide resolved
}

static void Postfix()
{
try
{
// try to load user.config
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
}
catch (ConfigurationErrorsException e) when (e.GetBaseException() is XmlException)
{
// can't use `ConfigurationManager.OpenExeConfiguration` for getting the config path here,
// as it would preload the file and throw again
var configPathsType = Assembly.GetAssembly(typeof(Configuration)).GetType("System.Configuration.ClientConfigPaths");
var configPaths = AccessTools.Property(configPathsType, "Current").GetValue(null);
var fileName = (string)AccessTools.Property(configPathsType, "LocalConfigFilename").GetValue(configPaths);
File.Delete(fileName);
MessageBox.Show("A corrupted configuration file has been deleted, you'll need to go through the setup again.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
shiftinv marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
10 changes: 8 additions & 2 deletions USBHelperInjector/USBHelperInjector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Contracts\ILauncherService.cs" />
<Compile Include="IPC\ILauncherService.cs" />
<Compile Include="InjectorService.cs" />
<Compile Include="IPC\IPCType.cs" />
<Compile Include="IPC\IPCUtils.cs" />
<Compile Include="ModuleInitInjectedAttribute.cs" />
<Compile Include="Patches\Attributes\Optional.cs" />
<Compile Include="Overrides.cs" />
<Compile Include="Patches\CemuPathPatches.cs" />
<Compile Include="Patches\CommandLineArgsPatch.cs" />
<Compile Include="Patches\Disable3DSDownload.cs" />
<Compile Include="Patches\Attributes\VersionSpecific.cs" />
<Compile Include="Patches\DisableWebTabPatch.cs" />
Expand All @@ -65,7 +69,9 @@
<Compile Include="Patches\DowngradeHTTPSPatch.cs" />
<Compile Include="Patches\DownloaderPatch.cs" />
<Compile Include="Patches\DownloaderQueuePatch.cs" />
<Compile Include="Patches\DownloadManagerOverlayPatch.cs" />
<Compile Include="Patches\EasterEggFontPatch.cs" />
<Compile Include="Patches\InvalidConfigPatch.cs" />
<Compile Include="Patches\MoviePlaybackPatch.cs" />
<Compile Include="Patches\NusGrabberFormPatch.cs" />
<Compile Include="Patches\PortablePatches.cs" />
Expand All @@ -81,7 +87,7 @@
<Compile Include="Patches\SettingsDonationKeyPatches.cs" />
<Compile Include="Patches\SettingsProxyPatches.cs" />
<Compile Include="Patches\SpeedChartPatch.cs" />
<Compile Include="Contracts\IInjectorService.cs" />
<Compile Include="IPC\IInjectorService.cs" />
<Compile Include="Patches\StringLocalizationPatch.cs" />
<Compile Include="Patches\SystemUpdateWarningPatch.cs" />
<Compile Include="Patches\KeySiteFormEnterPatch.cs" />
Expand Down
3 changes: 3 additions & 0 deletions USBHelperLauncher/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<netNamedPipeBinding>
<binding receiveTimeout="infinite" />
</netNamedPipeBinding>
<netTcpBinding>
<binding receiveTimeout="infinite" />
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
4 changes: 4 additions & 0 deletions USBHelperLauncher/Configuration/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using USBHelperInjector.IPC;
using USBHelperLauncher.Net;

namespace USBHelperLauncher.Configuration
Expand Down Expand Up @@ -92,6 +93,9 @@ private static List<KeyValuePair<PropertyInfo, Setting>> Properties
[Setting("Injector", true)]
public static bool SplitUnpackDirectories { get; set; }

[Setting("Launcher", IPCType.TCP)]
public static IPCType IPCType { get; set; }

public static void Save()
{
DoNotModify = Program.GetVersion();
Expand Down
13 changes: 7 additions & 6 deletions USBHelperLauncher/LauncherService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Fiddler;
using System;
using Fiddler;
using System.ServiceModel;
using USBHelperInjector.Contracts;
using USBHelperInjector.IPC;
using USBHelperLauncher.Configuration;

namespace USBHelperLauncher
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
class LauncherService : ILauncherService
{
public void SetKeySite(string site, string url)
Expand All @@ -13,11 +15,10 @@ public void SetKeySite(string site, string url)
Settings.Save();
}

public void SendInjectorSettings()
public void SendInjectorSettings(Uri uri)
{
Program.Logger.WriteLine("Sending information to injector...");
var factory = new ChannelFactory<IInjectorService>(new NetNamedPipeBinding(""), "net.pipe://localhost/InjectorService");
var channel = factory.CreateChannel();
Program.Logger.WriteLine($"Sending information to injector ({uri})...");
var channel = IPCUtils.CreateChannel<IInjectorService>(Settings.IPCType, uri.ToString());
if (Program.OverridePublicKey)
{
channel.SetDonationKey(Program.GenerateDonationKey());
Expand Down
6 changes: 3 additions & 3 deletions USBHelperLauncher/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public Logger()

override public void Write(string output)
{
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
output = $"[{timestamp}] {output}";
// Capture the output and also send it to StdOut
Captured.Write(output);
stdOutWriter.Write(output);
}

override public void WriteLine(string output)
{
// Capture the output and also send it to StdOut
Captured.WriteLine(output);
stdOutWriter.WriteLine(output);
Write(output + Environment.NewLine);
}

public string GetLog()
Expand Down
Loading