use system tick count instead of DateTime

This commit is contained in:
辉鸭蛋
2024-12-20 02:52:00 +08:00
parent ec8fa6e824
commit d5d65b7820
5 changed files with 31 additions and 25 deletions

View File

@@ -3,6 +3,7 @@ using SharpDX.DirectInput;
using System;
using System.Threading;
using System.Threading.Tasks;
using Vanara.PInvoke;
namespace BetterGenshinImpact.Core.Monitor;
@@ -11,6 +12,8 @@ public class DirectInputMonitor : IDisposable
private bool _isRunning = true;
private readonly Mouse _mouse;
public static int Interval = 5;
public DirectInputMonitor()
{
@@ -34,8 +37,8 @@ public class DirectInputMonitor : IDisposable
_mouse.Acquire();
MouseState state = _mouse.GetCurrentState();
// Debug.WriteLine($"{state.X} {state.Y} {state.Buttons[0]} {state.Buttons[1]}");
GlobalKeyMouseRecord.Instance.GlobalHookMouseMoveBy(state);
Thread.Sleep(5); // 10ms, equivalent to CLOCKS_PER_SEC/100
GlobalKeyMouseRecord.Instance.GlobalHookMouseMoveBy(state, Kernel32.GetTickCount());
Thread.Sleep(Interval); // 10ms, equivalent to CLOCKS_PER_SEC/100
}
});
}

View File

@@ -62,7 +62,7 @@ public class MouseKeyMonitor
private void GlobalHookKeyDown(object? sender, KeyEventArgs e)
{
// Debug.WriteLine("KeyDown: \t{0}", e.KeyCode);
GlobalKeyMouseRecord.Instance.GlobalHookKeyDown(e);
GlobalKeyMouseRecord.Instance.GlobalHookKeyDown(e, Kernel32.GetTickCount());
// 热键按下事件
HotKeyDown(sender, e);
@@ -100,7 +100,7 @@ public class MouseKeyMonitor
private void GlobalHookKeyUp(object? sender, KeyEventArgs e)
{
// Debug.WriteLine("KeyUp: \t{0}", e.KeyCode);
GlobalKeyMouseRecord.Instance.GlobalHookKeyUp(e);
GlobalKeyMouseRecord.Instance.GlobalHookKeyUp(e, Kernel32.GetTickCount());
// 热键松开事件
HotKeyUp(sender, e);

View File

@@ -115,7 +115,7 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
ra.Dispose();
}
public void GlobalHookKeyDown(KeyEventArgs e)
public void GlobalHookKeyDown(KeyEventArgs e, uint time)
{
// 排除热键
if (e.KeyCode.ToString() == TaskContext.Instance().Config.HotKeyConfig.KeyMouseMacroRecordHotkey)
@@ -139,10 +139,10 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
_keyDownState.Add(e.KeyCode, true);
}
// Debug.WriteLine($"KeyDown: {e.KeyCode}");
_recorder?.KeyDown(e);
_recorder?.KeyDown(e, time);
}
public void GlobalHookKeyUp(KeyEventArgs e)
public void GlobalHookKeyUp(KeyEventArgs e, uint time)
{
if (e.KeyCode.ToString() == TaskContext.Instance().Config.HotKeyConfig.Test1Hotkey)
{
@@ -153,7 +153,7 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
{
// Debug.WriteLine($"KeyUp: {e.KeyCode}");
_keyDownState[e.KeyCode] = false;
_recorder?.KeyUp(e);
_recorder?.KeyUp(e, time);
}
}
@@ -185,14 +185,14 @@ public class GlobalKeyMouseRecord : Singleton<GlobalKeyMouseRecord>
_recorder?.MouseWheel(e);
}
public void GlobalHookMouseMoveBy(MouseState state)
public void GlobalHookMouseMoveBy(MouseState state, uint time)
{
if (state is { X: 0, Y: 0 } || !_isInMainUi)
{
return;
}
// Debug.WriteLine($"MouseMoveBy: {state.X}, {state.Y}");
_recorder?.MouseMoveBy(state);
_recorder?.MouseMoveBy(state, time);
}
}

View File

@@ -49,7 +49,7 @@ public class KeyMouseMacroPlayer
public static async Task PlayMacro(List<MacroEvent> macroEvents, CancellationToken ct)
{
WorkingArea = PrimaryScreen.WorkingArea;
var startTime = DateTime.UtcNow;
var startTime = Kernel32.GetTickCount();
foreach (var e in macroEvents)
{
if (ct.IsCancellationRequested)
@@ -57,14 +57,14 @@ public class KeyMouseMacroPlayer
return;
}
var timeToWait = (int)(e.Time - (DateTime.UtcNow - startTime).TotalMilliseconds);
var timeToWait = e.Time - (Kernel32.GetTickCount() - startTime);
if (timeToWait < 0)
{
TaskControl.Logger.LogWarning("无法原速重放事件{Event},落后{TimeToWait}ms", e.Type.ToString(), -timeToWait);
}
else
{
await Task.Delay(timeToWait, ct);
await Task.Delay((int)timeToWait, ct);
}
switch (e.Type)

View File

@@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows.Forms;
using Vanara.PInvoke;
namespace BetterGenshinImpact.Core.Recorder;
@@ -16,7 +17,7 @@ public class KeyMouseRecorder
{
public List<MacroEvent> MacroEvents { get; } = [];
public DateTime StartTime { get; set; } = DateTime.UtcNow;
public uint StartTime { get; set; } = Kernel32.GetTickCount();
public DateTime LastOrientationDetection { get; set; } = DateTime.UtcNow;
@@ -104,23 +105,23 @@ public class KeyMouseRecorder
return JsonSerializer.Serialize(keyMouseScript, JsonOptions);
}
public void KeyDown(KeyEventArgs e)
public void KeyDown(KeyEventArgs e, uint time)
{
MacroEvents.Add(new MacroEvent
{
Type = MacroEventType.KeyDown,
KeyCode = e.KeyValue,
Time = (DateTime.UtcNow - StartTime).TotalMilliseconds
Time = time - StartTime
});
}
public void KeyUp(KeyEventArgs e)
public void KeyUp(KeyEventArgs e, uint time)
{
MacroEvents.Add(new MacroEvent
{
Type = MacroEventType.KeyUp,
KeyCode = e.KeyValue,
Time = (DateTime.UtcNow - StartTime).TotalMilliseconds
Time = time - StartTime
});
}
@@ -132,7 +133,7 @@ public class KeyMouseRecorder
MouseX = e.X,
MouseY = e.Y,
MouseButton = e.Button.ToString(),
Time = (DateTime.UtcNow - StartTime).TotalMilliseconds
Time = e.Timestamp - StartTime
});
}
@@ -144,7 +145,7 @@ public class KeyMouseRecorder
MouseX = e.X,
MouseY = e.Y,
MouseButton = e.Button.ToString(),
Time = (DateTime.UtcNow - StartTime).TotalMilliseconds
Time = e.Timestamp - StartTime
});
}
@@ -155,7 +156,7 @@ public class KeyMouseRecorder
Type = MacroEventType.MouseMoveTo,
MouseX = e.X,
MouseY = e.Y,
Time = (DateTime.UtcNow - StartTime).TotalMilliseconds
Time = e.Timestamp - StartTime
});
}
@@ -165,28 +166,30 @@ public class KeyMouseRecorder
{
Type = MacroEventType.MouseWheel,
MouseY = e.Delta, // 120 的倍率
Time = (DateTime.UtcNow - StartTime).TotalMilliseconds
Time = e.Timestamp - StartTime
});
}
public void MouseMoveBy(MouseState state)
public void MouseMoveBy(MouseState state, uint time)
{
var now = DateTime.UtcNow;
int? cao = null;
if (TaskContext.Instance().Config.RecordConfig.IsRecordCameraOrientation)
{
var now = DateTime.UtcNow;
if ((now - LastOrientationDetection).TotalMilliseconds > 100.0)
{
LastOrientationDetection = now;
cao = (int)Math.Round(CameraOrientation.Compute(TaskControl.CaptureToRectArea().SrcMat));
}
}
MacroEvents.Add(new MacroEvent
{
Type = MacroEventType.MouseMoveBy,
MouseX = state.X,
MouseY = state.Y,
Time = (now - StartTime).TotalMilliseconds,
Time = time - 5,
CameraOrientation = cao,
});
}