Skip to content

Commit

Permalink
Improve performance for Item Lurker
Browse files Browse the repository at this point in the history
  • Loading branch information
C1rdec committed Mar 18, 2020
1 parent fa51ee1 commit 86b9747
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 50 deletions.
12 changes: 12 additions & 0 deletions Lurker.Test/Events/TradeEventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ public void TryParse_NoLocation()
this.AssertLocation(new Location(), tradeEvent.Location);
}

[TestMethod]
public void TryParse_NoStashTabName()
{
var line = $"{BaseLogLine} {ItemName} {Price} {League} (stash tab ; position: left 10, top 2)";
var tradeEvent = TradeEvent.TryParse(line);

Assert.AreEqual(PlayerName, tradeEvent.PlayerName);
Assert.AreEqual(ItemName, tradeEvent.ItemName);
this.AssertPrice(this.ExpectedPrice, tradeEvent.Price);
this.AssertLocation(new Location() { Top = 2, Left = 10, StashTabName = string.Empty }, tradeEvent.Location);
}

private void AssertPrice(Price expectedPrice, Price actualPrice)
{
Assert.AreEqual(expectedPrice.NumberOfCurrencies, actualPrice.NumberOfCurrencies);
Expand Down
4 changes: 2 additions & 2 deletions src/Lurker.Console/Lurker.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<Reference Include="ConfOxide, Version=1.4.2.0, Culture=neutral, PublicKeyToken=934faed64f82030e, processorArchitecture=MSIL">
<HintPath>..\..\packages\ConfOxide.1.4.2.0\lib\net40\ConfOxide.dll</HintPath>
</Reference>
<Reference Include="Lurker.Patreon, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Lurker.Patreon.1.0.0\lib\net48\Lurker.Patreon.dll</HintPath>
<Reference Include="Lurker.Patreon, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Lurker.Patreon.1.0.1\lib\net48\Lurker.Patreon.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion src/Lurker.Console/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ConfOxide" version="1.4.2.0" targetFramework="net48" />
<package id="Lurker.Patreon" version="1.0.0" targetFramework="net48" />
<package id="Lurker.Patreon" version="1.0.1" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.7" targetFramework="net48" />
Expand Down
4 changes: 2 additions & 2 deletions src/Lurker.UI/Lurker.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
<Reference Include="Hardcodet.Wpf.TaskbarNotification, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Hardcodet.NotifyIcon.Wpf.1.0.8\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll</HintPath>
</Reference>
<Reference Include="Lurker.Patreon, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Lurker.Patreon.1.0.0\lib\net48\Lurker.Patreon.dll</HintPath>
<Reference Include="Lurker.Patreon, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Lurker.Patreon.1.0.1\lib\net48\Lurker.Patreon.dll</HintPath>
</Reference>
<Reference Include="MahApps.Metro, Version=1.6.5.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MahApps.Metro.1.6.5\lib\net47\MahApps.Metro.dll</HintPath>
Expand Down
19 changes: 15 additions & 4 deletions src/Lurker.UI/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,17 @@ public async void LoginToPatreon()
{
using (this._currentPatreonService = new Patreon.PatreonService())
{
if (!this._currentPatreonService.IsConnected)
{
await this._currentPatreonService.Login();
}

this.Pledging = await this._currentPatreonService.IsPledging();
if (this.Pledging)
{
this.SearchEnabled = true;
}

this.NotifyOfPropertyChange("NotConnected");
}
}
Expand Down Expand Up @@ -395,17 +405,18 @@ protected async override void OnActivate()
using (var service = new Patreon.PatreonService())
{
this.Pledging = await service.IsPledging();
if (!this.Pledging)
{
this._settingService.SearchEnabled = false;
}
}
}
else
{
this.Pledging = false;
}

if (!this.Pledging)
{
this.SearchEnabled = false;
}

this.AlertVolume = (int)(this._settingService.AlertVolume * 100);
this.CheckForUpdate();
base.OnActivate();
Expand Down
2 changes: 1 addition & 1 deletion src/Lurker.UI/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<package id="DeltaCompressionDotNet" version="1.1.0" targetFramework="net472" />
<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.8" targetFramework="net472" />
<package id="InputSimulator" version="1.0.4.0" targetFramework="net472" />
<package id="Lurker.Patreon" version="1.0.0" targetFramework="net48" />
<package id="Lurker.Patreon" version="1.0.1" targetFramework="net48" />
<package id="MahApps.Metro" version="1.6.5" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net48" />
Expand Down
109 changes: 75 additions & 34 deletions src/Lurker/ClipboardLurker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace Lurker
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WindowsInput;
using WK.Libraries.SharpClipboardNS;
using static Lurker.Native;

public class ClipboardLurker: IDisposable
{
Expand All @@ -31,7 +31,7 @@ public class ClipboardLurker: IDisposable
private SharpClipboard _clipboardMonitor;
private string _lastClipboardText = string.Empty;
private IKeyboardMouseEvents _keyboardEvent;
private PatreonService _patreonService;
private bool _isPledging;

#endregion

Expand All @@ -42,18 +42,25 @@ public class ClipboardLurker: IDisposable
/// </summary>
public ClipboardLurker(SettingsService settingsService)
{
Clipboard.Clear();
this._patreonService = new PatreonService();
this.ClearClipboard();
this._simulator = new InputSimulator();
this._clipboardMonitor = new SharpClipboard();
this._settingsService = settingsService;

this._settingsService.OnSave += this.SettingsService_OnSave;
this._keyboardEvent = Hook.GlobalEvents();
this._clipboardMonitor.ClipboardChanged += this.ClipboardMonitor_ClipboardChanged;

#if (!DEBUG)
this._keyboardEvent.MouseClick += this.KeyboardEvent_MouseClick;
#endif
#if (!DEBUG)
this._keyboardEvent.MouseClick += this.KeyboardEvent_MouseClick;
#endif

var service = new PatreonService();
service.IsPledging().ContinueWith(t =>
{
this._isPledging = t.Result;
service.Dispose();
});
}

#endregion
Expand All @@ -70,7 +77,7 @@ public ClipboardLurker(SettingsService settingsService)
/// </summary>
public event EventHandler<string> NewOffer;

#endregion
#endregion

#region Methods

Expand All @@ -90,9 +97,23 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this._patreonService.Dispose();
this._keyboardEvent.Dispose();
this._clipboardMonitor.ClipboardChanged -= ClipboardMonitor_ClipboardChanged;
this._settingsService.OnSave -= this.SettingsService_OnSave;
}
}


/// <summary>
/// Handles the OnSave event of the SettingsService control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private async void SettingsService_OnSave(object sender, EventArgs e)
{
using (var service = new PatreonService())
{
this._isPledging = await service.IsPledging();
}
}

Expand All @@ -103,11 +124,16 @@ protected virtual void Dispose(bool disposing)
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
private void KeyboardEvent_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
Task.Run(async () =>
Task.Run(() =>
{
if (!this._settingsService.SearchEnabled || !this._isPledging || e.Button != System.Windows.Forms.MouseButtons.Left)
{
return;
}
if (Native.IsKeyPressed(Native.VirtualKeyStates.VK_SHIFT) && Native.IsKeyPressed(Native.VirtualKeyStates.VK_CONTROL))
{
await this.ParseItem();
this.ParseItem();
}
});
}
Expand Down Expand Up @@ -135,7 +161,6 @@ private string GetClipboardText()
Thread.Sleep(200);
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Expand All @@ -144,6 +169,33 @@ private string GetClipboardText()
return clipboardText;
}

/// <summary>
/// Clears the clipboard.
/// </summary>
private void ClearClipboard()
{
Thread thread = new Thread(() =>
{
var retryCount = 3;
while (retryCount != 0)
{
try
{
Clipboard.Clear();
break;
}
catch
{
retryCount--;
Thread.Sleep(200);
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}

/// <summary>
/// Handles the ClipboardChanged event of the ClipboardMonitor control.
/// </summary>
Expand Down Expand Up @@ -176,26 +228,21 @@ private void ClipboardMonitor_ClipboardChanged(object sender, SharpClipboard.Cli
/// <summary>
/// Parses the item.
/// </summary>
private async Task ParseItem()
private async void ParseItem()
{
if (!this._settingsService.SearchEnabled)
{
return;
}

if (!await this._patreonService.IsPledging())
{
return;
}

PoeItem item = default;

var retryCount = 3;
var retryCount = 2;
for (int i = 0; i < retryCount; i++)
{
item = await this.GetItemInClipboard();
if (item == null || !item.Identified)
if (item == null)
{
return;
}

if (!item.Identified)
{
await Task.Delay(50);
continue;
}

Expand All @@ -208,13 +255,7 @@ private async Task ParseItem()
}

this.Newitem?.Invoke(this, item);
try
{
Clipboard.Clear();
}
catch
{
}
this.ClearClipboard();
}

/// <summary>
Expand All @@ -224,7 +265,7 @@ private async Task ParseItem()
private async Task<PoeItem> GetItemInClipboard()
{
this._simulator.Keyboard.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.CONTROL, WindowsInput.Native.VirtualKeyCode.VK_C);
await Task.Delay(100);
await Task.Delay(20);
return this._itemParser.Parse(this.GetClipboardText());
}

Expand Down
10 changes: 7 additions & 3 deletions src/Lurker/Events/TradeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,16 @@ public Location ParseLocation(string locationValue)
}

// tab name
var stashTabName = string.Empty;
var tabValue = locationValue.GetLineBefore("\";");
var index = tabValue.IndexOf("\"");
var stashTabName = tabValue.Substring(index + 1);
if (!string.IsNullOrEmpty(tabValue))
{
var index = tabValue.IndexOf("\"");
stashTabName = tabValue.Substring(index + 1);
}

// Position
var positionValue = locationValue.GetLineAfter("\";");
var positionValue = locationValue.GetLineAfter(";");
var positionIndex = positionValue.IndexOf(PositionMarker);

if (positionIndex != -1)
Expand Down
4 changes: 2 additions & 2 deletions src/Lurker/Lurker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<Reference Include="Gma.System.MouseKeyHook, Version=5.6.130.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MouseKeyHook.5.6.0\lib\net40\Gma.System.MouseKeyHook.dll</HintPath>
</Reference>
<Reference Include="Lurker.Patreon, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Lurker.Patreon.1.0.0\lib\net48\Lurker.Patreon.dll</HintPath>
<Reference Include="Lurker.Patreon, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Lurker.Patreon.1.0.1\lib\net48\Lurker.Patreon.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll</HintPath>
Expand Down
26 changes: 26 additions & 0 deletions src/Lurker/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ public static bool IsKeyPressed(VirtualKeyStates nVirtKey)
}
}

public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

[DllImport("User32.dll")]
public static extern short GetKeyState(VirtualKeyStates nVirtKey);

[DllImport("User32.dll")]
public static extern int SetForegroundWindow(IntPtr point);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll")]
public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

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

Expand Down Expand Up @@ -77,6 +84,25 @@ public struct Rect
public int Bottom { get; set; }
}

public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}

public enum VirtualKeyStates : int
{
VK_LBUTTON = 0x01,
Expand Down
2 changes: 1 addition & 1 deletion src/Lurker/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="ConfOxide" version="1.4.2.0" targetFramework="net472" />
<package id="InputSimulator" version="1.0.4.0" targetFramework="net472" />
<package id="Lurker.Patreon" version="1.0.0" targetFramework="net48" />
<package id="Lurker.Patreon" version="1.0.1" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.7" targetFramework="net48" />
Expand Down

0 comments on commit 86b9747

Please sign in to comment.