Skip to content

Commit

Permalink
Find process name instead of window name
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
sabihoshi committed Apr 4, 2021
1 parent 6b360cf commit 84ede76
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
40 changes: 1 addition & 39 deletions GenshinLyreMidiPlayer/Core/LyrePlayer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using WindowsInput;
using Melanchall.DryWetMidi.Core;

namespace GenshinLyreMidiPlayer.Core
{
public class LyrePlayer
{
public const string GenshinWindowName = "Genshin Impact";
private static readonly IInputSimulator Input = new InputSimulator();

private static readonly List<int> LyreNotes = new List<int>
Expand Down Expand Up @@ -39,20 +37,6 @@ public class LyrePlayer
83 // B5
};

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowTitle);

public static IntPtr FindWindow(string lpWindowName)
{
return FindWindow(null, lpWindowName);
}

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

/// <summary>
/// Play a MIDI note.
/// </summary>
Expand All @@ -63,7 +47,7 @@ public static IntPtr FindWindow(string lpWindowName)
public static bool PlayNote(NoteOnEvent note, bool transposeNotes, int keyOffset,
Keyboard.Layout selectedLayout)
{
if (!IsWindowFocused(GenshinWindowName))
if (!WindowHelper.IsGameFocused())
return false;

var noteId = note.NoteNumber - keyOffset;
Expand Down Expand Up @@ -104,27 +88,5 @@ public static void PlayNote(int noteId, Keyboard.Layout selectedLayout)
var key = Keyboard.GetLayout(selectedLayout)[keyIndex];
Input.Keyboard.KeyPress(key);
}

public static bool EnsureWindowOnTop()
{
var genshinWindow = FindWindow(GenshinWindowName);
SwitchToThisWindow(genshinWindow, true);

return !genshinWindow.Equals(IntPtr.Zero) &&
GetForegroundWindow().Equals(genshinWindow);
}

public static bool IsWindowFocused(IntPtr windowPtr)
{
var hWnd = GetForegroundWindow();
return hWnd.Equals(windowPtr)
&& !windowPtr.Equals(IntPtr.Zero);
}

public static bool IsWindowFocused(string windowName)
{
var windowPtr = FindWindow(windowName);
return IsWindowFocused(windowPtr);
}
}
}
50 changes: 50 additions & 0 deletions GenshinLyreMidiPlayer/Core/WindowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;

namespace GenshinLyreMidiPlayer.Core
{
public static class WindowHelper
{
public const string GenshinProcessName = "GenshinImpact";

private static IntPtr? FindWindowByProcessName(string processName)
{
var process = Process.GetProcessesByName(processName);
return process.FirstOrDefault()?.MainWindowHandle;
}

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

public static bool EnsureGameOnTop()
{
var genshinWindow = FindWindowByProcessName(GenshinProcessName);

if (genshinWindow is null)
return false;

SwitchToThisWindow((IntPtr) genshinWindow, true);

return !genshinWindow.Equals(IntPtr.Zero) &&
GetForegroundWindow().Equals(genshinWindow);
}

private static bool IsWindowFocused(IntPtr windowPtr)
{
var hWnd = GetForegroundWindow();
return hWnd.Equals(windowPtr);
}

public static bool IsGameFocused()
{
var genshinWindow = FindWindowByProcessName(GenshinProcessName);
return genshinWindow != null &&
IsWindowFocused((IntPtr) genshinWindow);
}
}
}
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer/GenshinLyreMidiPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>1.3.2</Version>
<Version>1.3.3</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions GenshinLyreMidiPlayer/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void PlayPause()
{
PlayPauseIcon = PauseIcon;

LyrePlayer.EnsureWindowOnTop();
WindowHelper.EnsureGameOnTop();
_playTimer = new Timer {Interval = 100};
_playTimer.Elapsed += PlayTimerElapsed;
_playTimer.Start();
Expand Down Expand Up @@ -339,7 +339,7 @@ private void PlayNote(NoteOnEvent note)

private void PlayTimerElapsed(object sender, ElapsedEventArgs e)
{
if (LyrePlayer.IsWindowFocused(LyrePlayer.GenshinWindowName))
if (WindowHelper.IsGameFocused())
{
_playback.Start();
_playTimer.Dispose();
Expand Down

0 comments on commit 84ede76

Please sign in to comment.