Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game: WoWProcessInput uses source generated logging to avoid boxing #230

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions Game/Input/WowProcessInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Game
{
public class WowProcessInput : IMouseInput
public partial class WowProcessInput : IMouseInput
{
private const int MIN_DELAY = 25;
private const int MAX_DELAY = 55;
Expand All @@ -17,9 +17,7 @@ public class WowProcessInput : IMouseInput
private readonly IInput nativeInput;
private readonly IInput simulatorInput;

private readonly ConcurrentDictionary<ConsoleKey, bool> keyDict = new ConcurrentDictionary<ConsoleKey, bool>();

private bool debug = true;
private readonly ConcurrentDictionary<ConsoleKey, bool> keyDict = new();

public WowProcessInput(ILogger logger, WowProcess wowProcess)
{
Expand All @@ -38,14 +36,14 @@ private void KeyDown(ConsoleKey key, string description = "")
}
else
{
if(!keyDict.TryAdd(key, true))
if (!keyDict.TryAdd(key, true))
{
return;
}
}

if (!string.IsNullOrEmpty(description))
Log($"KeyDown {key} " + description);
LogKeyDown(logger, key, description);

nativeInput.KeyDown((int)key);
keyDict[key] = true;
Expand All @@ -62,21 +60,21 @@ private void KeyUp(ConsoleKey key, bool forceClick)
}
else
{
if(!keyDict.TryAdd(key, false))
if (!keyDict.TryAdd(key, false))
{
return;
}
}

Log($"KeyUp {key}");
LogKeyUp(logger, key);
nativeInput.KeyUp((int)key);

keyDict[key] = false;
}

public bool IsKeyDown(ConsoleKey key)
{
if(keyDict.TryGetValue(key, out var down))
if (keyDict.TryGetValue(key, out var down))
{
return down;
}
Expand All @@ -101,15 +99,15 @@ public void SetForegroundWindow()

public async ValueTask KeyPress(ConsoleKey key, int milliseconds, string description = "")
{
int totalDelayMs = await nativeInput.KeyPress((int)key, milliseconds);
int totalElapsedMs = await nativeInput.KeyPress((int)key, milliseconds);
if (!string.IsNullOrEmpty(description))
Log($"[{key}] {description} pressed for {totalDelayMs}ms");
LogKeyPress(logger, key, description, totalElapsedMs);
}

public async ValueTask KeyPressNoDelay(ConsoleKey key, int milliseconds, string description = "")
{
if (!string.IsNullOrEmpty(description))
Log($"[{key}] {description} pressing for {milliseconds}ms");
LogKeyPressNoDelay(logger, key, description, milliseconds);

await nativeInput.KeyPressNoDelay((int)key, milliseconds);
}
Expand All @@ -120,7 +118,7 @@ public void KeyPressSleep(ConsoleKey key, int milliseconds, string description =
return;

if (!string.IsNullOrEmpty(description))
Log($"[{key}] {description} pressing for {milliseconds}ms");
LogKeyPress(logger, key, description, milliseconds);

nativeInput.KeyPressSleep((int)key, milliseconds);
}
Expand Down Expand Up @@ -148,10 +146,28 @@ public async ValueTask LeftClickMouse(Point position)
await nativeInput.LeftClickMouse(position);
}

private void Log(string text)
{
if (debug)
logger.LogInformation($"Input: {text}");
}
[LoggerMessage(
EventId = 25,
Level = LogLevel.Debug,
Message = @"Input: KeyDown {key} {description}")]
static partial void LogKeyDown(ILogger logger, ConsoleKey key, string description);

[LoggerMessage(
EventId = 26,
Level = LogLevel.Debug,
Message = @"Input: KeyUp {key}")]
static partial void LogKeyUp(ILogger logger, ConsoleKey key);

[LoggerMessage(
EventId = 27,
Level = LogLevel.Debug,
Message = @"Input: [{key}] {description} pressed for {milliseconds}ms")]
static partial void LogKeyPress(ILogger logger, ConsoleKey key, string description, int milliseconds);

[LoggerMessage(
EventId = 28,
Level = LogLevel.Debug,
Message = @"Input: [{key}] {description} pressing for {milliseconds}ms")]
static partial void LogKeyPressNoDelay(ILogger logger, ConsoleKey key, string description, int milliseconds);
}
}