Skip to content

Commit

Permalink
feat: restore from another instance
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Nov 3, 2024
1 parent 00447d3 commit a960f76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions BetterGenshinImpact/GameTask/AutoWood/AutoWoodTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private void Felling(WoodTaskParam taskParam, bool isLast = false)
private void PressZ(WoodTaskParam taskParam)
{
// IMPORTANT: MUST try focus before press Z
SystemControl.Focus(TaskContext.Instance().GameHandle);
SystemControl.FocusWindow(TaskContext.Instance().GameHandle);

if (_first)
{
Expand Down Expand Up @@ -457,7 +457,7 @@ private void PressZ(WoodTaskParam taskParam)

private void PressEsc(WoodTaskParam taskParam)
{
SystemControl.Focus(TaskContext.Instance().GameHandle);
SystemControl.FocusWindow(TaskContext.Instance().GameHandle);
Simulation.SendInput.Keyboard.KeyPress(VK.VK_ESCAPE);
// if (TaskContext.Instance().Config.AutoWoodConfig.PressTwoEscEnabled)
// {
Expand Down
21 changes: 20 additions & 1 deletion BetterGenshinImpact/GameTask/SystemControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,37 @@ public static void ActivateWindow()
ActivateWindow(TaskContext.Instance().GameHandle);
}

public static void Focus(nint hWnd)
public static void FocusWindow(nint hWnd)
{
if (User32.IsWindow(hWnd))
{
_ = User32.SendMessage(hWnd, User32.WindowMessage.WM_SYSCOMMAND, User32.SysCommand.SC_RESTORE, 0);
_ = User32.SetForegroundWindow(hWnd);

while (User32.IsIconic(hWnd))
{
continue;
}

_ = User32.BringWindowToTop(hWnd);
_ = User32.SetActiveWindow(hWnd);
}
}

public static void RestoreWindow(nint hWnd)
{
if (User32.IsWindow(hWnd))
{
_ = User32.SendMessage(hWnd, User32.WindowMessage.WM_SYSCOMMAND, User32.SysCommand.SC_RESTORE, 0);
_ = User32.SetForegroundWindow(hWnd);

if (User32.IsIconic(hWnd))
{
_ = User32.ShowWindow(hWnd, ShowWindowCommand.SW_RESTORE);
}

_ = User32.BringWindowToTop(hWnd);
_ = User32.SetActiveWindow(hWnd);
}
}

Expand Down
16 changes: 9 additions & 7 deletions BetterGenshinImpact/Helpers/RuntimeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.GameTask;
using Microsoft.Extensions.Hosting;
using System;
using System.ComponentModel;
Expand All @@ -9,6 +10,8 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace BetterGenshinImpact.Helpers;

Expand Down Expand Up @@ -116,19 +119,18 @@ public static void CheckSingleInstance(string instanceName, Action<bool> callbac
handle = new EventWaitHandle(false, EventResetMode.AutoReset, instanceName);
}

Task.Factory.StartNew(() =>
_ = Task.Factory.StartNew(() =>
{
while (handle.WaitOne())
{
#if false
UIDispatcherHelper.BeginInvoke(main =>
Application.Current.Dispatcher?.BeginInvoke(() =>
{
main?.Activate();
main?.Show();
Application.Current.MainWindow?.Show();
Application.Current.MainWindow?.Activate();
SystemControl.RestoreWindow(new WindowInteropHelper(Application.Current.MainWindow).Handle);
});
#endif
}
}, TaskCreationOptions.LongRunning);
}, TaskCreationOptions.LongRunning).ConfigureAwait(false);
}

public static void CheckIntegration()
Expand Down

0 comments on commit a960f76

Please sign in to comment.