-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
294 lines (240 loc) · 10.1 KB
/
Copy pathProgram.cs
File metadata and controls
294 lines (240 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using cs2_turnbinds;
public class Config
{
public List<int> Yaw { get; set; }
public double Sensitivity { get; set; }
public double M_Yaw { get; set; }
public Keys Left { get; set; }
public Keys Right { get; set; }
public Keys Toggle { get; set; }
public Keys Pause { get; set; }
public Keys Inc { get; set; }
public Keys Dec { get; set; }
public Keys Exit { get; set; }
}
[JsonSerializable(typeof(Config))]
[JsonSourceGenerationOptions(WriteIndented = true, PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower, Converters = new[] {typeof(KeysConverter)})]
public partial class ConfigJsonContext : JsonSerializerContext
{
}
public class KeysConverter : JsonConverter<Keys>
{
public override Keys Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string keyName = reader.GetString();
// try parse with _ prefix
if (Enum.TryParse(typeof(Keys), "_" + keyName, out var result2))
{
return (Keys) result2;
}
// try parse
if (Enum.TryParse(typeof(Keys), keyName, out var result))
{
return (Keys) result;
}
throw new JsonException($"Invalid key name: {keyName}");
}
public override void Write(Utf8JsonWriter writer, Keys value, JsonSerializerOptions options)
{
var str = value.ToString();
// replace _ prefix with nothing
if (str.StartsWith("_"))
{
writer.WriteStringValue(str.Substring(1));
}
else
{
writer.WriteStringValue(str);
}
}
}
class Program
{
[StructLayout(LayoutKind.Explicit)]
struct LARGE_INTEGER
{
[FieldOffset(0)] public long QuadPart;
}
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(int vKey);
[DllImport("ntdll.dll")]
private static extern uint NtDelayExecution([In] bool Alertable, [In] ref LARGE_INTEGER DelayInterval);
[DllImport("kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
// [DllImport("ntdll.dll")]
// private static extern int NtQueryTimerResolution(out uint MinimumResolution, out uint MaximumResolution, out uint CurrentResolution);
const int MOUSEEVENTF_MOVE = 0x0001;
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
const int SM_CXSCREEN = 0;
const int SM_CYSCREEN = 1;
static void MoveMouse(int deltaX, int deltaY)
{
mouse_event(MOUSEEVENTF_MOVE, deltaX, deltaY, 0, (int)IntPtr.Zero);
}
static void DelayExecutionBy(long hns)
{
// Convert the delay from 100-nanosecond intervals to a LARGE_INTEGER
LARGE_INTEGER interval;
interval.QuadPart = -1 * hns; // Negative value for relative delay
// Call NtDelayExecution
NtDelayExecution(false, ref interval);
}
public class MouseMoveCalculator
{
private long lastTime;
private double remainingMovement;
private long frequency;
public MouseMoveCalculator()
{
QueryPerformanceCounter(out lastTime);
QueryPerformanceFrequency(out frequency);
}
public int CalculateMovement(double yawspeed, double sensitivity, double m_yaw)
{
long currentTime;
QueryPerformanceCounter(out currentTime);
long elapsedTime = currentTime - lastTime;
// Calculate movement basedp
double movement = ((yawspeed / (sensitivity * m_yaw)) * elapsedTime) / frequency;
// Accumulate remaining movement fractions to ensure precision
remainingMovement += movement;
long movementAmount = (long)remainingMovement;
// Subtract the integer part used, keep the fractional part for next calculation
remainingMovement -= movementAmount;
// Update last time for next call
lastTime = currentTime;
return (int) movementAmount;
}
}
static bool IsKeyDown(int vk)
{
return (GetAsyncKeyState(vk) & 0x8000) != 0;
}
static void Main(string[] args)
{
var configFilePath = "config.json";
var configFileExists = File.Exists(configFilePath);
string configFile;
if (!configFileExists)
{
var defaultConfig = new Config
{
Yaw = new List<int> { 80, 160, 240 },
Sensitivity = 0.8,
M_Yaw = 0.022,
Left = Keys.LBUTTON,
Right = Keys.RBUTTON,
Toggle = Keys.XBUTTON1,
Pause = Keys.F1,
Inc = Keys.OEM_PLUS,
Dec = Keys.OEM_MINUS,
Exit = Keys.F2
};
configFile = JsonSerializer.Serialize(defaultConfig, typeof(Config), ConfigJsonContext.Default);
File.WriteAllText(configFilePath, configFile);
}
else
{
configFile = File.ReadAllText(configFilePath);
}
// use system.json
var config = JsonSerializer.Deserialize(configFile, typeof(Config), ConfigJsonContext.Default) as Config;
if (config == null)
{
throw new Exception("Config file is empty");
}
Console.WriteLine(@"
____ _____ ______ _____ _ _ _____ ______
/ __ \ / ____| ____| / ____| | | | __ \| ____|
| | | | | | |__ | (___ | | | | |__) | |__
| | | | | | __| \___ \| | | | _ /| __|
| |__| | |____| |____ _ ____) | |__| | | \ \| |
\____/ \_____|______(_)_____/ \____/|_| \_\_| ");
Console.WriteLine();
Console.WriteLine("Yaw speeds: " + string.Join(", ", config.Yaw));
Console.WriteLine("Sensitivity: " + config.Sensitivity);
Console.WriteLine("M_Yaw: " + config.M_Yaw);
Console.WriteLine("Left: " + config.Left);
Console.WriteLine("Right: " + config.Right);
Console.WriteLine("Toggle Yaw: " + config.Toggle);
Console.WriteLine("Pause: " + config.Pause);
Console.WriteLine("Increase Yaw: " + config.Inc);
Console.WriteLine("Decrease Yaw: " + config.Dec);
Console.WriteLine("Exit: " + config.Exit);
Console.WriteLine();
Console.WriteLine("To edit bindings, edit config.json and restart the program.");
Console.WriteLine("Use the keynames from here without the VK_ : https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes");
Console.WriteLine("IE: VK_LBUTTON is LBUTTON etc, A-Z are the same, 0-9 are the same, F1-F24 are the same, etc.");
Console.WriteLine();
Console.WriteLine("Current yaw speed: " + config.Yaw[0]);
Console.WriteLine("PAUSED, press " + config.Pause + " to unpause");
int currentYawIndex = 0;
bool paused = true;
var calculator = new MouseMoveCalculator();
while (true)
{
var movement = calculator.CalculateMovement(config.Yaw[currentYawIndex], config.Sensitivity, config.M_Yaw);
// Check if the left mouse button is pressed and not paused
if (IsKeyDown((int) config.Left) && !paused)
{
// Move the mouse left by the specified speed
MoveMouse(-movement, 0);
}
// Check if the right mouse button is pressed and not paused
if (IsKeyDown((int) config.Right) && !paused)
{
// Move the mouse right by the specified speed
MoveMouse(movement, 0);
}
// Check if the 'P' key is pressed to toggle pause state for left mouse button
if (IsKeyDown((int) config.Pause))
{
paused = !paused;
if (paused)
{
Console.WriteLine("PAUSED press " + config.Pause + " to unpause");
}
else
{
Console.WriteLine("ACTIVE press " + config.Pause + " to pause");
}
Thread.Sleep(200); // Delay to avoid multiple toggles with one key press
}
// Check if the '+' key is pressed to increase yaw speed
if (IsKeyDown((int) config.Inc) && !paused)
{
config.Yaw[currentYawIndex] += 10; // Increase yaw speed by 10
Thread.Sleep(200); // Delay to avoid multiple increments with one key press
Console.WriteLine($"Yaw speed increased to {config.Yaw[currentYawIndex]}");
}
// Check if the '-' key is pressed to decrease yaw speed
if (IsKeyDown((int) config.Dec) && !paused)
{
config.Yaw[currentYawIndex] -= 10; // Decrease yaw speed by 10
Thread.Sleep(200); // Delay to avoid multiple decrements with one key press
Console.WriteLine($"Yaw speed decreased to {config.Yaw[currentYawIndex]}");
}
if (IsKeyDown((int) config.Toggle) && !paused)
{
currentYawIndex = (currentYawIndex + 1) % config.Yaw.Count;
Console.WriteLine($"Yaw speed toggled to {config.Yaw[currentYawIndex]}");
Thread.Sleep(200); // Delay to avoid multiple toggles with one key press
}
// Check if the END key is pressed to exit the program
if (IsKeyDown((int) config.Exit))
{
Console.WriteLine("Exiting program...");
break; // Exit the loop
}
DelayExecutionBy(3500);
}
}
}