150 lines
4.8 KiB
C#
150 lines
4.8 KiB
C#
using System;
|
||
using System.Diagnostics;
|
||
using System.Threading;
|
||
|
||
namespace ZNTechClone
|
||
{
|
||
public class HopSettings
|
||
{
|
||
public bool Enabled { get; set; }
|
||
public int TriggerKey { get; set; } = 0x20; // Space
|
||
public int ToggleKey { get; set; } = 0x78; // F11 临时切换
|
||
public int SideKey { get; set; } = 0x05; // 侧键
|
||
public bool UseSideKey { get; set; }
|
||
public bool UphillMode { get; set; }
|
||
public bool DoubleJump { get; set; } = true;
|
||
public int DoubleJumpDelayMs { get; set; } = 90;
|
||
public bool ReleaseCrouchOnDouble { get; set; }
|
||
public string CrouchMode { get; set; } = "松开";
|
||
public int IntervalMs { get; set; } = 35;
|
||
}
|
||
|
||
public class KnifeSettings
|
||
{
|
||
public bool Enabled { get; set; }
|
||
public int TriggerKey { get; set; } = 0x06; // 侧键 5
|
||
public int ToggleKey { get; set; } = 0x79; // F12
|
||
public int HoldMs { get; set; } = 120;
|
||
public bool UphillMode { get; set; }
|
||
}
|
||
|
||
public static class MacroEngine
|
||
{
|
||
private static Thread _thread;
|
||
private static volatile bool _running;
|
||
private static volatile bool _paused;
|
||
|
||
public static HopSettings Hop { get; } = new();
|
||
public static KnifeSettings Knife { get; } = new();
|
||
|
||
public static event Action<string> Log;
|
||
public static event Action<int, int> KeyPressed; // (vk, type)
|
||
|
||
public static bool IsRunning => _running;
|
||
public static bool IsPaused => _paused;
|
||
|
||
public static void TogglePause() => SetPaused(!_paused);
|
||
|
||
public static void Start()
|
||
{
|
||
if (_running) return;
|
||
_running = true;
|
||
_paused = false;
|
||
_thread = new Thread(Loop) { IsBackground = true, Name = "zntech-macro", Priority = ThreadPriority.Highest };
|
||
_thread.Start();
|
||
Log?.Invoke("宏引擎已启动");
|
||
}
|
||
|
||
public static void StopAll()
|
||
{
|
||
_running = false;
|
||
try { _thread?.Join(300); } catch { }
|
||
_thread = null;
|
||
Log?.Invoke("宏引擎已停止");
|
||
}
|
||
|
||
public static void SetPaused(bool paused)
|
||
{
|
||
_paused = paused;
|
||
Log?.Invoke(paused ? "已暂停(F8)" : "已恢复(F8)");
|
||
}
|
||
|
||
private static bool _wasHopDown;
|
||
private static bool _wasKnifeDown;
|
||
|
||
private static void Loop()
|
||
{
|
||
var sw = Stopwatch.StartNew();
|
||
while (_running)
|
||
{
|
||
try
|
||
{
|
||
// 全局急停 F8
|
||
if (Native.IsKeyDown(0x77))
|
||
{
|
||
SetPaused(!_paused);
|
||
Thread.Sleep(220);
|
||
continue;
|
||
}
|
||
|
||
if (_paused) { Thread.Sleep(10); continue; }
|
||
|
||
bool hopKey = Native.IsKeyDown(Hop.TriggerKey) || (Hop.UseSideKey && Native.IsKeyDown(Hop.SideKey));
|
||
bool knifeKey = Native.IsKeyDown(Knife.TriggerKey);
|
||
|
||
if (Hop.Enabled && hopKey && !_wasHopDown) DoHop();
|
||
if (Knife.Enabled && knifeKey && !_wasKnifeDown) DoKnife();
|
||
|
||
_wasHopDown = hopKey;
|
||
_wasKnifeDown = knifeKey;
|
||
|
||
Thread.Sleep(Math.Max(4, Math.Min(20, Hop.IntervalMs / 3)));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log?.Invoke("宏异常:" + ex.Message);
|
||
Thread.Sleep(200);
|
||
}
|
||
}
|
||
}
|
||
|
||
private static void DoHop()
|
||
{
|
||
int vk = Hop.TriggerKey;
|
||
Native.KeyDown(vk);
|
||
KeyPressed?.Invoke(vk, 1);
|
||
Thread.Sleep(18);
|
||
Native.KeyUp(vk);
|
||
KeyPressed?.Invoke(vk, 0);
|
||
|
||
if (Hop.DoubleJump)
|
||
{
|
||
Thread.Sleep(Math.Max(20, Hop.DoubleJumpDelayMs));
|
||
Native.KeyDown(vk);
|
||
Thread.Sleep(16);
|
||
Native.KeyUp(vk);
|
||
KeyPressed?.Invoke(vk, 1);
|
||
KeyPressed?.Invoke(vk, 0);
|
||
}
|
||
|
||
Log?.Invoke(Hop.UphillMode ? "连跳·上坡" : "连跳");
|
||
}
|
||
|
||
private static void DoKnife()
|
||
{
|
||
Native.MouseButton(true, true);
|
||
KeyPressed?.Invoke(0x01, 1);
|
||
Thread.Sleep(Math.Max(30, Knife.HoldMs));
|
||
Native.MouseButton(true, false);
|
||
KeyPressed?.Invoke(0x01, 0);
|
||
|
||
// 刀喷:挥刀后快速切枪(示意)
|
||
Thread.Sleep(40);
|
||
Native.KeyDown(0x51); Thread.Sleep(12); Native.KeyUp(0x51);
|
||
Native.KeyDown(0x51); Thread.Sleep(12); Native.KeyUp(0x51);
|
||
|
||
Log?.Invoke(Knife.UphillMode ? "刀喷·上坡" : "刀喷");
|
||
}
|
||
}
|
||
}
|