Skip to content

Commit

Permalink
Added Focus check, Loaded Check and cleaned code
Browse files Browse the repository at this point in the history
- Checks for Game to be focused
- Checks for Save to be Loaded
- Cleaned the code a bit
  • Loading branch information
CapitaineToinon committed Apr 3, 2019
1 parent 16b1da2 commit b58fe79
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 31 deletions.
8 changes: 1 addition & 7 deletions WPF.Gadgetlemage/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows;

namespace WPF.Gadgetlemage
{
Expand Down
21 changes: 21 additions & 0 deletions WPF.Gadgetlemage/DarkSoulsHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ public class DarkSoulsHook : PHook
private const string RemasterItemAddrAOB = "48 89 5C 24 18 89 54 24 10 55 56 57 41 54 41 55 41 56 41 57 48 8D 6C 24 F9";
private const string RemasterInventoryDataAOB = "48 8B 05 ? ? ? ? 48 85 C0 ? ? F3 0F 58 80 AC 00 00 00";
private const string RemasterFlagsAOB = "48 8B 0D ? ? ? ? 99 33 C2 45 33 C0 2B C2 8D 50 F6";
public const string RemasterChrFollowCamAOB = "48 8B 0D ? ? ? ? E8 ? ? ? ? 48 8B 4E 68 48 8B 05 ? ? ? ? 48 89 48 60";
private PHPointer RemasterBasePtr;
private PHPointer RemasterItemAddr;
private PHPointer RemasterInventoryData;
private PHPointer RemasterFlags;
private PHPointer RemasterChrFollowCam;
#endregion

#region Public properties
Expand Down Expand Up @@ -89,6 +91,24 @@ public List<Weapon> Weapons
/// </summary>
public Weapon SelectedWeapon { get; set; }

/// <summary>
/// If the Process is in focus
/// </summary>
public bool Focused => Hooked && User32.GetForegroundProcessID() == Process.Id;

/// <summary>
/// If the save is loaded
/// </summary>
public bool Loaded
{
get
{
return (Version == Version.DarkSoulsRemastered)
? (RemasterChrFollowCam.Resolve() != IntPtr.Zero)
: true;
}
}

/// <summary>
/// Game version. Only remastered is 64 bits
/// </summary>
Expand Down Expand Up @@ -117,6 +137,7 @@ public DarkSoulsHook() : base(_refreshInterval, _minLifetime, processSelector)
RemasterBasePtr = RegisterRelativeAOB(RemasterBasePtrAOB, 3, 7);
RemasterItemAddr = RegisterAbsoluteAOB(RemasterItemAddrAOB);
RemasterFlags = RegisterRelativeAOB(RemasterFlagsAOB, 3, 7, 0, 0);
RemasterChrFollowCam = RegisterRelativeAOB(RemasterChrFollowCamAOB, 3, 7, 0, 0x60, 0x60);

RemasterInventoryData = RegisterRelativeAOB(RemasterInventoryDataAOB, 3, 7);
}
Expand Down
9 changes: 4 additions & 5 deletions WPF.Gadgetlemage/Flags.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace WPF.Gadgetlemage
{
/// <summary>
/// Flags magic
/// </summary>
public static class Flags
{
public static Dictionary<string, int> Groups = new Dictionary<string, int>()
Expand Down
24 changes: 12 additions & 12 deletions WPF.Gadgetlemage/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF.Gadgetlemage"
mc:Ignorable="d" Width="606.667" VerticalAlignment="Top" ResizeMode="NoResize" Height="415">
mc:Ignorable="d" Width="546" VerticalAlignment="Top" ResizeMode="NoResize" Height="415">
<Grid Margin="0,0,2,0" VerticalAlignment="Top">
<ComboBox x:Name="comboWeapons" Margin="10,10,90,0" VerticalAlignment="Top" FontFamily="Roboto" Height="22"/>
<Button x:Name="btnCreate" Content="Create" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top" Width="75" Height="22" FontFamily="Roboto"/>
Expand All @@ -29,22 +29,22 @@
<TextBlock Margin="10,34,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" FontFamily="Roboto" Foreground="#FF464646"><Run Text="Automatically creates the Black Knight weapon if it doesn't drop. "/><Run Text="The weapon can then be dropped manually if desired."/></TextBlock>
<TextBlock Margin="10,20,10,0" TextWrapping="Wrap" VerticalAlignment="Top" FontFamily="Roboto" FontWeight="Bold" Height="14"><Run Text="Automatically"/><Run Text=" Create Weapon"/></TextBlock>
<TextBlock Margin="10,70,10,0" TextWrapping="Wrap" VerticalAlignment="Top" FontFamily="Roboto" FontWeight="Bold" Height="14" Text="Enable Global Hotkey"/>
<TextBlock Margin="10,84,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" FontFamily="Roboto" Foreground="#FF464646" Text="Enables global hotkey. Hotkey is only active when Dark Souls or Dark Souls: Remastered has focus."/>
<TextBlock Margin="10,84,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" FontFamily="Roboto" Foreground="#FF464646" Text="Enables the global hotkey. The hotkey is only active when Dark Souls or Dark Souls: Remastered is in focus."/>
<TextBlock Margin="10,120,10,0" TextWrapping="Wrap" VerticalAlignment="Top" FontFamily="Roboto" FontWeight="Bold" Height="14"><Run Text="Consume "/><Run Text="Keypress"/></TextBlock>
<TextBlock Margin="10,134,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" FontFamily="Roboto" Foreground="#FF464646" Text="If enabled, keypress won't be forwarded to the game."/>
<TextBlock Margin="10,170,10,0" TextWrapping="Wrap" VerticalAlignment="Top" FontFamily="Roboto" FontWeight="Bold" Height="14" Text="Enable sound"/>
<TextBlock Margin="10,184,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" FontFamily="Roboto" Foreground="#FF464646"><Run Text="Emits "/><Run Text="a bip sound when a weapon is created, being through an automatic drop, hotkey or manual."/></TextBlock>
<CheckBox x:Name="cbxAuto" Margin="10,34,10,0" VerticalAlignment="Top" Height="16" Grid.Column="1"/>
<CheckBox x:Name="cbxHotkey" Content="" Margin="10,84,10,0" VerticalAlignment="Top" Height="16" Grid.Column="1"/>
<CheckBox x:Name="cbxConsume" Content="" VerticalAlignment="Top" Height="16" Grid.Column="1" Margin="10,133,10,0"/>
<CheckBox x:Name="cbxSound" Content="" VerticalAlignment="Top" Height="16" Grid.Column="1" Margin="10,184,10,0"/>
<TextBlock Margin="10,134,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" FontFamily="Roboto" Foreground="#FF464646" Text="If enabled, the keypress for the global hotkey won't be forwarded to the game."/>
<TextBlock Margin="10,170,10,0" TextWrapping="Wrap" VerticalAlignment="Top" FontFamily="Roboto" FontWeight="Bold" Height="14"><Run Text="Enable "/><Run Text="Sou"/><Run Text="nd"/></TextBlock>
<TextBlock Margin="10,184,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="31" FontFamily="Roboto" Foreground="#FF464646" Text="Emits a beep sound when a weapon is created, be it through an automatic drop, a hotkey or manually."/>
<CheckBox x:Name="cbxAuto" Margin="10,34,37,0" VerticalAlignment="Top" Height="16" Grid.Column="1"/>
<CheckBox x:Name="cbxHotkey" Content="" Margin="10,84,37,0" VerticalAlignment="Top" Height="16" Grid.Column="1"/>
<CheckBox x:Name="cbxConsume" Content="" VerticalAlignment="Top" Height="16" Grid.Column="1" Margin="10,133,37,0"/>
<CheckBox x:Name="cbxSound" Content="" VerticalAlignment="Top" Height="16" Grid.Column="1" Margin="10,184,37,0"/>

</Grid>
</GroupBox>
<TextBlock Margin="0,348,10,-21" HorizontalAlignment="Right" VerticalAlignment="Top">
<Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">
About
</Hyperlink>
<Hyperlink NavigateUri="https://github.com/CapitaineToinon/Gadgetlemage" RequestNavigate="Hyperlink_RequestNavigate">
About
</Hyperlink>
</TextBlock>
</Grid>
</Window>
9 changes: 6 additions & 3 deletions WPF.Gadgetlemage/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ private void AutoRefresh(CancellationToken ct)

Dispatcher.Invoke(new Action(() =>
{
btnCreate.IsEnabled = Hook.Loaded;

bool auto = cbxAuto.IsChecked ?? false;
if (Hook.Hooked && auto)
if (Hook.Hooked && Hook.Loaded && auto)
{
Hook.AutomaticallyGetItem();
}
Expand Down Expand Up @@ -208,7 +210,7 @@ private void KeyboardHook_KeyDownOrUp_ListenHotkey(object sender, GlobalKeyboard
{
bool hotkeyEnabled = cbxHotkey.IsChecked ?? false;
bool consume = cbxConsume.IsChecked ?? false;
if (!e.IsUp && hotkeyEnabled)
if (!e.IsUp && hotkeyEnabled && Hook.Focused)
{
if (createHotkey.Trigger(e.KeyCode) && consume)
{
Expand All @@ -224,6 +226,7 @@ private void KeyboardHook_KeyDownOrUp_ListenHotkey(object sender, GlobalKeyboard
/// <param name="e"></param>
private void KeyboardHook_KeyDownOrUp_SetupHotkey(object sender, GlobalKeyboardHookEventArgs e)
{
// Only on KeyDown, if process has focus and is loaded
if (!e.IsUp)
{
createHotkey.Key = (VirtualKey)e.KeyCode;
Expand Down Expand Up @@ -278,7 +281,7 @@ private void BtnHotkey_Click(object sender, RoutedEventArgs e)
/// </summary>
public void GetItem()
{
if (Hook.Hooked)
if (Hook.Hooked && Hook.Loaded)
{
Hook.GetItem();
}
Expand Down
21 changes: 21 additions & 0 deletions WPF.Gadgetlemage/User32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Runtime.InteropServices;

namespace WPF.Gadgetlemage
{
internal static class User32
{
[DllImport("user32.dll")]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);

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

public static uint GetForegroundProcessID()
{
IntPtr hWnd = GetForegroundWindow();
GetWindowThreadProcessId(hWnd, out uint pid);
return pid;
}
}
}
11 changes: 9 additions & 2 deletions WPF.Gadgetlemage/WPF.Gadgetlemage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -86,6 +88,7 @@
<Compile Include="Flags.cs" />
<Compile Include="Hotkey.cs" />
<Compile Include="InventoryItem.cs" />
<Compile Include="User32.cs" />
<Compile Include="Weapon.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -118,14 +121,18 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Resource Include="Ressources\PTDE.txt" />
Expand Down
2 changes: 0 additions & 2 deletions WPF.Gadgetlemage/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="LowLevelHooking" version="1.0.1" targetFramework="net461" />
<package id="MaterialDesignColors" version="1.1.2" targetFramework="net461" />
<package id="MaterialDesignThemes" version="2.5.1" targetFramework="net461" />
</packages>

0 comments on commit b58fe79

Please sign in to comment.