Skip to content

Commit

Permalink
Ye
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Sep 4, 2024
1 parent de1ec09 commit a03435e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 6 deletions.
29 changes: 29 additions & 0 deletions JiayiLauncher/Features/Game/PackageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@ public async Task MinimizeFix(bool fix)
debugSettings.DisableDebugging(package.AppInfo.Package.Id.FullName);
}

public async Task MultiInstance(bool enable, string manifestPath)
{
const string f1 = "xmlns:build=\"http://schemas.microsoft.com/developer/appx/2015/build\""; // in AppxManifest.xml - Package
const string r1 = "xmlns:build=\"http://schemas.microsoft.com/developer/appx/2015/build\" xmlns:desktop4=\"http://schemas.microsoft.com/appx/manifest/desktop/windows10/4\"";
const string f2 = "EntryPoint=\"Minecraft_Win10.App\""; // in AppxManifest.xml - Application
const string r2 = "EntryPoint=\"Minecraft_Win10.App\" desktop4:SupportsMultipleInstances=\"true\"";

var manifest = await File.ReadAllTextAsync(manifestPath);

if (enable)
{
if (manifest.Contains(r1) && manifest.Contains(r2)) return;

manifest = manifest.Replace(f1, r1);
manifest = manifest.Replace(f2, r2);
}
else
{
if (!manifest.Contains(r1) && !manifest.Contains(r2)) return;

manifest = manifest.Replace(r1, f1);
manifest = manifest.Replace(r2, f2);
}

await File.WriteAllTextAsync(manifestPath, manifest);

// package should be re-registered at this point
}

public async Task BackupGameData(string to)
{
Directory.CreateDirectory(to);
Expand Down
51 changes: 48 additions & 3 deletions JiayiLauncher/Features/Versions/VersionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
using System.Net.Http;
using System.Threading.Tasks;
using Windows.Management.Deployment;
using Blazored.Toast;
using Blazored.Toast.Services;
using JiayiLauncher.Features.Game;
using JiayiLauncher.Features.Shaders;
using JiayiLauncher.Localization;
using JiayiLauncher.Settings;
using JiayiLauncher.Shared.Components.Toasts;
using JiayiLauncher.Utils;
using Microsoft.AspNetCore.Components;

namespace JiayiLauncher.Features.Versions;

Expand Down Expand Up @@ -204,7 +209,7 @@ await Task.Run(() =>
}

// my favorite part of this class
public async Task<SwitchResult> Switch(string version)
public async Task<SwitchResult> Switch(string version, bool force = false)
{
_log.Write(nameof(VersionManager), $"Switching to version {version}");

Expand All @@ -229,8 +234,15 @@ public async Task<SwitchResult> Switch(string version)
{
if (package.InstalledPath.Contains(version))
{
_log.Write(nameof(VersionManager), "Version already installed");
return SwitchResult.Succeeded;
if (force)
{
_log.Write(nameof(VersionManager), "Version already installed, but force is enabled; switching anyway");
}
else
{
_log.Write(nameof(VersionManager), "Version already installed");
return SwitchResult.Succeeded;
}
}

if (package.IsDevelopmentMode)
Expand Down Expand Up @@ -261,6 +273,9 @@ await _packageData.PackageManager.RemovePackageAsync(package.Id.FullName,
_log.Write(nameof(VersionManager), "Registering package");

var manifest = Path.Combine(folder, "AppxManifest.xml");

// probably safe to apply multi instance setting now
await _packageData.MultiInstance(JiayiSettings.Instance.MultiInstance, manifest);

try
{
Expand Down Expand Up @@ -295,6 +310,36 @@ await _packageData.PackageManager.RemovePackageAsync(package.Id.FullName,

return SwitchResult.UnknownError;
}

public async Task<SwitchResult> Reregister()
{
_log.Write(nameof(VersionManager), "Reregistering package");

var blazor = Singletons.Get<BlazorBridge>();

var toastParams = new ToastParameters()
.Add(nameof(JiayiToast.Level), ToastLevel.Info)
.Add(nameof(JiayiToast.Title), "Reregister in progress...");

blazor.ShowToast(toastParams, toastSettings => toastSettings.DisableTimeout = true);

var version = await _packageData.GetVersion();
var result = await Switch(version, true);

toastParams = new ToastParameters()
.Add(nameof(JiayiToast.Level), result == SwitchResult.Succeeded
? ToastLevel.Success : ToastLevel.Error)
.Add(nameof(JiayiToast.Title), result == SwitchResult.Succeeded
? "Reregistered successfully." : "Failed to reregister. Check the logs for more information.")
.Add(nameof(JiayiToast.Buttons), new List<(string, EventCallback)>
{
(Strings.Okay, EventCallback.Empty)
});

blazor.ShowToast(toastParams, toastSettings => toastSettings.DisableTimeout = true);

return result;
}

public string? GetVersionPath(string ver)
{
Expand Down
6 changes: 3 additions & 3 deletions JiayiLauncher/Pages/Extra/FullVersionList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@
NavigationManager.NavigateTo("/Versions");
}

private int _switching = -1;
private List<int> _switching = new();

private async Task DownloadClicked(MinecraftVersion ver)
{
var i = _versionNames.IndexOf(ver.Version);
_switching = i;
_switching.Add(i);

_statuses[i] = Strings.Downloading;
_buttonStates[i] = true;
Expand All @@ -189,7 +189,7 @@

_statuses[i] = string.Empty;
_buttonStates[i] = false;
_switching = -1;
_switching.Remove(i);
StateHasChanged();
}

Expand Down
1 change: 1 addition & 0 deletions JiayiLauncher/Platforms/Windows/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public App()
"TimeoutException",
"Object name: 'System.Net.Sockets.NetworkStream'.",
"at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)",
"at Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager.SendMessage(String message)",
"System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request."
};

Expand Down
20 changes: 20 additions & 0 deletions JiayiLauncher/Settings/JiayiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Blazored.Modal.Services;
using Blazored.Toast;
using Blazored.Toast.Services;
using JiayiLauncher.Appearance;
using JiayiLauncher.Features.Game;
using JiayiLauncher.Features.Mods;
using JiayiLauncher.Features.Versions;
using JiayiLauncher.Localization;
using JiayiLauncher.Modals;
using JiayiLauncher.Settings.SpecialTypes;
using JiayiLauncher.Shared;
using JiayiLauncher.Shared.Components.Toasts;
Expand Down Expand Up @@ -426,6 +429,23 @@ Only 1 formatting string can be used per field.

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

[Setting("Multi instance", "Launch", "Allow multiple instances of the game to run at the same time. Changes are applied upon reregistering the game.")]
public bool MultiInstance { get; set; } = false;

[Setting("Reregister game", "Launch", "Reregister the game's package. This is required for some settings to take effect.")]
public (string, Action) ReregisterGame { get; set; } = ("Reregister", async () =>
{
var privileges = Singletons.Get<Privileges>();
var blazor = Singletons.Get<BlazorBridge>();
if (!privileges.IsAdmin())
{
var result = await blazor.ShowModal<Escalate>(Strings.PrivEscTitle);
if (result.Cancelled) return;
}
Task.Run(() => Singletons.Get<VersionManager>().Reregister());
});

// log settings
[Setting("Anonymize logs", "Logs",
Expand Down
24 changes: 24 additions & 0 deletions JiayiLauncher/Utils/BlazorBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ public void ShowToast(ToastParameters parameters, Action<ToastSettings> settings
if (!dispatched)
_log.Write(nameof(BlazorBridge), "Failed to dispatch toast.", Log.LogLevel.Error);
}

public async Task<ModalResult> ShowModal<T>(string title) where T : IComponent
{
var mainPage = (MainPage?)Application.Current!.MainPage;
if (mainPage == null)
{
_log.Write(nameof(BlazorBridge), "MainPage was null", Log.LogLevel.Error);
return ModalResult.Cancel();
}

ModalResult? result = null;

var dispatched = await mainPage.BlazorWebView.TryDispatchAsync(async sp =>
{
var modalService = sp.GetRequiredService<IModalService>();
var modal = modalService.Show<T>(title);
result = await modal.Result;
});

if (dispatched && result != null) return result;

_log.Write(nameof(BlazorBridge), "Failed to dispatch modal.", Log.LogLevel.Error);
return ModalResult.Cancel();
}

public async Task<ModalResult> ShowModal<T>(string title, ModalParameters parameters) where T : IComponent
{
Expand Down

0 comments on commit a03435e

Please sign in to comment.