Skip to content

Commit

Permalink
fuck COM (minimize fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Jan 5, 2024
1 parent 348a433 commit b7866fc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
48 changes: 48 additions & 0 deletions JiayiLauncher/Features/Game/IHateCom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using JiayiLauncher.Utils;

namespace JiayiLauncher.Features.Game;

[GeneratedComInterface(StringMarshalling = StringMarshalling.Utf16)]
[Guid("f27c3930-8029-4ad1-94e3-3dba417810c1")]
public partial interface IPackageDebugSettings
{
void EnableDebugging(string packageFullName, string? debuggerCommandLine, string? environment);
void DisableDebugging(string packageFullName);
}

[GeneratedComClass]
public partial class PackageDebugSettings : IPackageDebugSettings
{
private readonly IPackageDebugSettings _interface;

public PackageDebugSettings()
{
ComWrappers wrappers = new StrategyBasedComWrappers();

var classId = new Guid("b1aec16f-2383-4852-b0e9-8f0b1dc66b4d");
var interfaceId = new Guid("f27c3930-8029-4ad1-94e3-3dba417810c1");
var hr = Imports.CoCreateInstance(
ref classId,
nint.Zero,
1, // CLSCTX_INPROC_SERVER
ref interfaceId,
out var obj);

if (hr != 0) Marshal.ThrowExceptionForHR(hr);

_interface = (IPackageDebugSettings)wrappers.GetOrCreateObjectForComInstance(obj, CreateObjectFlags.None);
}

public void EnableDebugging(string packageFullName, string? debuggerCommandLine, string? environment)
{
_interface.EnableDebugging(packageFullName, debuggerCommandLine, environment);
}

public void DisableDebugging(string packageFullName)
{
_interface.DisableDebugging(packageFullName);
}
}
20 changes: 18 additions & 2 deletions JiayiLauncher/Features/Game/PackageData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
Expand All @@ -11,12 +13,14 @@ namespace JiayiLauncher.Features.Game;

public static class PackageData
{
public const string FAMILY_NAME = "Microsoft.MinecraftUWP_8wekyb3d8bbwe";

public static PackageManager PackageManager { get; } = new();

public static async Task<AppDiagnosticInfo?> GetPackage()
{
var info =
await AppDiagnosticInfo.RequestInfoForPackageAsync("Microsoft.MinecraftUWP_8wekyb3d8bbwe");
await AppDiagnosticInfo.RequestInfoForPackageAsync(FAMILY_NAME);
if (info == null || info.Count == 0 || !Directory.Exists(info[0].AppInfo.Package.InstalledPath)) return null;
return info[0];
}
Expand Down Expand Up @@ -90,7 +94,19 @@ public static string GetGameDataPath()
{
// i thought i could just use the package for this but naw gotta hardcode it
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Packages", "Microsoft.MinecraftUWP_8wekyb3d8bbwe");
"Packages", FAMILY_NAME);
}

public static async Task MinimizeFix(bool fix)
{
var package = await GetPackage();
if (package == null) return;

var debugSettings = new PackageDebugSettings();
if (fix)
debugSettings.EnableDebugging(package.AppInfo.Package.Id.FullName, null, null);
else
debugSettings.DisableDebugging(package.AppInfo.Package.Id.FullName);
}

public static async Task BackupGameData(string to)
Expand Down
5 changes: 4 additions & 1 deletion JiayiLauncher/Features/Launch/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static async Task<LaunchResult> Launch(Mod mod)
LaunchProgressChanged?.Invoke(null, EventArgs.Empty);
Log.Write(nameof(Launcher), $"Launching {mod.Name}");

// minimize fix !
await PackageData.MinimizeFix(JiayiSettings.Instance.MinimizeFix);

var supported = await Minecraft.ModSupported(mod);
if (!supported)
{
Expand Down Expand Up @@ -85,7 +88,7 @@ public static async Task<LaunchResult> Launch(Mod mod)

// either wait for the game's modules to load
// or if the user has injection delay enabled, wait for the time they specified
if (JiayiSettings.Instance!.UseInjectionDelay)
if (JiayiSettings.Instance.UseInjectionDelay)
Task.Delay(JiayiSettings.Instance.InjectionDelay[2] * 1000).Wait();
else
await Minecraft.WaitForModules();
Expand Down
3 changes: 3 additions & 0 deletions JiayiLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Blazored.Toast;
using JiayiLauncher.Appearance;
using JiayiLauncher.Features.Discord;
using JiayiLauncher.Features.Game;
using JiayiLauncher.Features.Mods;
using JiayiLauncher.Features.Profiles;
using JiayiLauncher.Features.Stats;
Expand Down Expand Up @@ -101,6 +102,8 @@ public MainWindow()

RichPresence.Initialize();
JiayiStats.Save();

Task.Run(async () => await PackageData.MinimizeFix(true));
}

protected override void OnSourceInitialized(EventArgs e)
Expand Down
3 changes: 3 additions & 0 deletions JiayiLauncher/Settings/JiayiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ Only 1 formatting string can be used per field.
"Speed up loading times by terminating unnecessary processes. Beware of jank.",
tooltip: "May cause issues related to Microsoft Store licensing (determining whether you own the game or not).")]
public bool AccelerateGameLoading { get; set; } = false;

[Setting("Minimize fix", "Launch", "Prevents the game from suspending itself when minimized. Changes are applied upon launch.")]
public bool MinimizeFix { get; set; } = true;

// log settings
[Setting("Anonymize logs", "Logs",
Expand Down
6 changes: 5 additions & 1 deletion JiayiLauncher/Utils/Imports.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System;
using System.Runtime.InteropServices;

namespace JiayiLauncher.Utils;

Expand Down Expand Up @@ -59,4 +60,7 @@ public static partial nint CreateRemoteThread(nint hProcess, nint lpThreadAttrib

[LibraryImport("user32.dll")]
public static partial nint FindWindowW([MarshalAs(UnmanagedType.LPWStr)] string? lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName);

[LibraryImport("ole32.dll")]
public static partial int CoCreateInstance(ref Guid rclsid, nint pUnkOuter, uint dwClsContext, ref Guid riid, out nint ppv);
}

0 comments on commit b7866fc

Please sign in to comment.