Skip to content

Commit

Permalink
Hotkeys Update
Browse files Browse the repository at this point in the history
Hotkeys now only trigger if the game is focused and the game isn't paused.
  • Loading branch information
ForgottenIce committed Mar 12, 2019
1 parent 999aa03 commit d1d90b2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Rayman2LevelSwitcher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<Button x:Name="btn_loadpos" Content="Load Pos" Margin="85,34,0,0" VerticalAlignment="Top" Click="btn_loadpos_Click" HorizontalAlignment="Left" Width="70"/>
<Button x:Name="btn_zerohp" Content="0hp" HorizontalAlignment="Right" Margin="0,34,132,0" VerticalAlignment="Top" Width="60" Click="btn_zerohp_Click"/>
<Button x:Name="btn_void" ToolTip="Press this while game is paused, then deathwarp for void (don't unpause)." Content="VOID" Margin="0,34,71,0" VerticalAlignment="Top" Click="btn_void_Click" HorizontalAlignment="Right" Width="56"/>
<CheckBox x:Name="chk_hotkeys" Content="Hotkeys" Margin="0,13,60,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="67"/>
<CheckBox x:Name="chk_hotkeys" Content="Hotkeys" Margin="0,13,60,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="67" Checked="chk_hotkeys_Checked" Unchecked="chk_hotkeys_Unchecked"/>
<Expander x:Name="exp_bookmarkmenu" Header="Bookmarks" ExpandDirection="Right" Margin="0,13,-244,8" HorizontalAlignment="Right" Width="310" Collapsed="exp_bookmarkmenu_Collapsed" Expanded="exp_bookmarkmenu_Expanded">
<Grid>
<TextBlock Text="Bookmarks:" Margin="10,6,157,0" VerticalAlignment="Top" Height="16"/>
Expand Down
58 changes: 54 additions & 4 deletions Rayman2LevelSwitcher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public partial class MainWindow : Window {

Thread bgThread;

bool hotkeysEnabled = false;

public MainWindow()
{
InitializeComponent();
Expand All @@ -70,15 +72,63 @@ public void BookmarkUpdater()
//Update bookmarks every second
Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(() => UpdateBookmarks() ));
//Dispatcher.Invoke(DispatcherPriority.Normal,
// new Action(() => txtblock_currentbookmarklevel.Text = Rayman2Handle()));
//Dispatcher.Invoke(DispatcherPriority.Normal,
// new Action(() => txtblock_currentbookmarklevel.Text += " " + Memory.GetForegroundWindow().ToString()));
Thread.Sleep(1000);
}
}

private bool Rayman2IsFocused()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = Memory.GetForegroundWindow();

if (Memory.GetWindowText(handle, Buff, nChars) > 0)
{
if (Buff.ToString() == "Rayman II")
{
return true;
}
}
return false;
}

private bool Rayman2IsPaused()
{
int processHandle = GetRayman2ProcessHandle(false);
if (processHandle < 0) { return false; }

int bytesReadOrWritten = 0;

byte[] pausePointerBuffer = new byte[4];

Memory.ReadProcessMemory((int)processHandle, off_voidpointer, pausePointerBuffer, pausePointerBuffer.Length, ref bytesReadOrWritten);

if (BitConverter.ToInt32(pausePointerBuffer, 0) == 1)
{
return true;
}
return false;
}

private void chk_hotkeys_Checked(object sender, RoutedEventArgs e)
{
hotkeysEnabled = true;
}

private void chk_hotkeys_Unchecked(object sender, RoutedEventArgs e)
{
hotkeysEnabled = false;
}

private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
{
//Debug.WriteLine(e.KeyboardData.VirtualCode);

if (!chk_hotkeys.IsChecked.Value) {
if (!hotkeysEnabled || Rayman2IsPaused() || !Rayman2IsFocused() && !Application.Current.MainWindow.IsActive) {
e.Handled = false;
return;
}
Expand Down Expand Up @@ -533,7 +583,7 @@ private void LoadBookmark()
private void RenameBookmark()
{
renameBookmarkName = "";
bool hotkeysIsChecked = chk_hotkeys.IsChecked ?? true;
bool hotkeysIsChecked = hotkeysEnabled;
int processHandle = GetRayman2ProcessHandle(false);
if (processHandle < 0 || !File.Exists(bookmarkFile) || listbox_bookmarklist.SelectedItem == null || listbox_bookmarklist.SelectedItems.Count > 1)
{
Expand All @@ -553,9 +603,9 @@ private void RenameBookmark()
rename.Left = Application.Current.MainWindow.Left + (Application.Current.MainWindow.Width / 2) - (rename.Width / 2);
rename.Top = Application.Current.MainWindow.Top + (Application.Current.MainWindow.Height / 2) - (rename.Height / 2);
rename.txtbox_name.Text = listbox_bookmarklist.SelectedItem.ToString();
chk_hotkeys.IsChecked = false;
hotkeysEnabled = false;
rename.ShowDialog();
chk_hotkeys.IsChecked = hotkeysIsChecked;
hotkeysEnabled = hotkeysIsChecked;
if (!String.IsNullOrEmpty(renameBookmarkName) && listbox_bookmarklist.SelectedItem.ToString() != renameBookmarkName)
{
var xml = XDocument.Load(bookmarkFile);
Expand Down
6 changes: 6 additions & 0 deletions Rayman2LevelSwitcher/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public static extern bool ReadProcessMemory(int hProcess,
public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress,
byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesWritten);

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

public static int GetPointerPath(int processHandle, int baseAddress, params int[] offsets)
{
int currentAddress = baseAddress;
Expand Down

0 comments on commit d1d90b2

Please sign in to comment.