Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions CollapseLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,14 @@ public static string MD5Hash(string path)
public static void ForceRestart()
{
// Workaround to artificially start new process and wait for the current one to be killed.
string collapsePath = AppExecutablePath;
Process.Start("cmd.exe", $"/c timeout /T 1 && start /I \"\" \"{collapsePath}\"");
var cmdProc = Process.Start(new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c timeout /T 1 && start \"\" \"{AppExecutablePath}\"",
UseShellExecute = true,
});

cmdProc?.WaitForExit();
Application.Current.Exit();
}
}
Expand Down
79 changes: 73 additions & 6 deletions CollapseLauncher/XAMLs/MainApp/TrayIcon.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CollapseLauncher.Extension;
using CollapseLauncher.Helper;
using CommunityToolkit.Mvvm.Input;
using H.NotifyIcon;
Expand All @@ -8,6 +9,7 @@
using Hi3Helper.Win32.Native.LibraryImport;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.Win32;
using System;
using System.Drawing;
using System.Threading.Tasks;
Expand Down Expand Up @@ -61,12 +63,32 @@ public TrayIcon()
InitializeComponent();

CollapseTaskbar.Logger = LoggerInstance;
CollapseTaskbar.SetValue(TaskbarIcon.LoggerProp, LoggerInstance);
var instanceCount = MainEntryPoint.InstanceCount;

ExceptionCatcher.ExceptionRaised +=
(_, e) =>
{
LogWriteLine($"[TrayIcon] Somehow GUID set is failed, restarting app with new GUID!\r\n{e.Exception.Message}",
LogType.Error, true);

var guid = LauncherConfig.SetGuid(instanceCount, null);

// Restart the app on failure because currently there is no
// legit way of reinitializing TrayIcon due to it being a
// dependency to many external functions
MainEntryPoint.ForceRestart();
};

Initializer(instanceCount);
Current = this;
}

private void Initializer(int instanceCount)
{
var instanceIndicator = "";
var instanceCount = MainEntryPoint.InstanceCount;
var guid = LauncherConfig.GetGuid(instanceCount);
if (Environment.OSVersion.Version >= new Version(10, 0, 22621))

var guid = LauncherConfig.GetGuid(instanceCount);
if (Environment.OSVersion.Version >= new Version(10, 0, 22621) && !isOldTaskbar_EP())
{
LogWriteLine("[TrayIcon] Initializing Tray with parameters:\r\n\t" +
$"GUID: {guid}\r\n\t" +
Expand All @@ -75,6 +97,13 @@ public TrayIcon()
}
else // Do not use static GUID on W10 and W11 21H2
{
if (isOldTaskbar_EP())
{
LogWriteLine("[TrayIcon] Detected ExplorerPatcher W10 Taskbar behavior!" +
"Reverting to old GUID behavior, your taskbar position might be inconsistent.",
LogType.Warning, true);
}

var guidGet = CollapseTaskbar.GetValue(TaskbarIcon.IdProperty).ToString();
LogWriteLine("[TrayIcon] Initializing Tray with parameters:\r\n\t" +
$"GUID: {guidGet}\r\n\t" +
Expand Down Expand Up @@ -111,14 +140,52 @@ public TrayIcon()
CollapseTaskbar.Visibility = Visibility.Visible;

CollapseTaskbar.TrayIcon.MessageWindow.BalloonToolTipChanged += BalloonChangedEvent;

Current = this;
}

public void Dispose()
{
CollapseTaskbar.Dispose();
}

/// <summary>
/// Detects presence of ExplorerPatcher setting for OldTaskbar
/// This is useful to check for Taskbar behavior whether its using W11 or W10 behaviors
/// </summary>
/// <returns>True when it detects "OldTaskbar" registry value >0</returns>
private bool isOldTaskbar_EP()
{
if (cached_isOldTaskbar_EP.HasValue)
return cached_isOldTaskbar_EP.Value;
#nullable enable
try
{
var regPath = "Software\\ExplorerPatcher";
using RegistryKey? regRoot = Registry.CurrentUser.OpenSubKey(regPath, false);

if (regRoot == null)
{
cached_isOldTaskbar_EP = false;
return false;
}

var val = regRoot.TryGetValue("OldTaskbar", false, null!);
bool ret = false;
if (val is int i)
ret = i != 0; // Return true on anything other than 0 (default W11 Taskbar)

cached_isOldTaskbar_EP = ret;
return ret;
}
catch (Exception)
{
// If any error in this method occurrs, just return false (EP not exist)
cached_isOldTaskbar_EP = false;
return false;
}
}

private bool? cached_isOldTaskbar_EP = null;
#nullable restore

private ILogger LoggerInstance => ILoggerHelper.GetILogger("TrayIcon");

Expand Down
2 changes: 1 addition & 1 deletion H.NotifyIcon
7 changes: 7 additions & 0 deletions Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}

// Check if user has permission
bool isUserHasPermission = ConverterTool.IsUserHasPermission(gameFolder);

Check warning on line 82 in Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64, net9.0-windows10.0.26100.0)

Possible null reference argument for parameter 'input' in 'bool ConverterTool.IsUserHasPermission(string input)'.

Check warning on line 82 in Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64, net9.0-windows10.0.26100.0)

Possible null reference argument for parameter 'input' in 'bool ConverterTool.IsUserHasPermission(string input)'.

Check warning on line 82 in Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64, net9.0-windows10.0.26100.0)

Possible null reference argument for parameter 'input' in 'bool ConverterTool.IsUserHasPermission(string input)'.

Check warning on line 82 in Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs

View workflow job for this annotation

GitHub Actions / build-nativeaot (Debug, x64, net9.0-windows10.0.26100.0)

Possible null reference argument for parameter 'input' in 'bool ConverterTool.IsUserHasPermission(string input)'.

// Assign boolean if IsConfigFileExist and IsUserHasPermission.
IsFirstInstall = !(isConfigFileExist && isUserHasPermission);
Expand Down Expand Up @@ -547,7 +547,14 @@
Guid g = Guid.NewGuid();
SetAndSaveConfigValue($"sessionGuid{sessionNum}", g);
return g;
}

public static Guid SetGuid(int sessionNum, Guid? guid)
{
guid ??= Guid.NewGuid();

SetAndSaveConfigValue($"sessionGuid{sessionNum}", guid.Value);
return guid.Value;
}

#endregion
Expand Down
Loading