-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUtils.cs
437 lines (369 loc) · 14.1 KB
/
Utils.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace ZenStates.Core
{
public static class Utils
{
public static bool Is64Bit => OpenHardwareMonitor.Hardware.OperatingSystem.Is64BitOperatingSystem;
public static uint SetBits(uint val, int offset, int n, uint newVal)
{
return val & ~(((1U << n) - 1) << offset) | (newVal << offset);
}
public static uint SetBit(uint val, int offset) => SetBits(val, offset, 1, 1);
public static uint ClearBit(uint val, int offset) => SetBits(val, offset, 1, 0);
public static uint GetBits(uint val, int offset, int n)
{
return (val >> offset) & ~(~0U << n);
}
public static uint GetBit(uint value, int bitOffset) => GetBits(value, bitOffset, 1);
public static uint BitSlice(uint val, int hi, int lo)
{
uint mask = (2U << hi - lo) - 1U;
return val >> lo & mask;
}
public static uint CountSetBits(uint v)
{
uint result = 0;
while (v > 0)
{
if ((v & 1) == 1)
result++;
v >>= 1;
}
return result;
}
// https://github.com/LibreHardwareMonitor/LibreHardwareMonitor/blob/master/LibreHardwareMonitorLib/Hardware/SMBios.cs#L918
public static bool IsBitSet(byte b, int pos)
{
return (b & (1 << pos)) != 0;
}
public static string GetStringPart(uint val)
{
return val != 0 ? Convert.ToChar(val).ToString() : "";
}
public static string IntToStr(uint val)
{
uint part1 = val & 0xff;
uint part2 = val >> 8 & 0xff;
uint part3 = val >> 16 & 0xff;
uint part4 = val >> 24 & 0xff;
return $"{GetStringPart(part1)}{GetStringPart(part2)}{GetStringPart(part3)}{GetStringPart(part4)}";
}
public static double VidToVoltage(uint vid)
{
return 1.55 - vid * 0.00625;
}
public static double VidToVoltageSVI3(uint vid)
{
return Math.Round(0.245 + vid * 0.005, 3);
}
public static uint VoltageToVidSVI3(double targetVoltage)
{
if (targetVoltage < 0.245)
return 0;
return (uint)Math.Round((targetVoltage - 0.245) / 0.005);
}
private static bool CheckAllZero<T>(ref T[] typedArray)
{
if (typedArray == null)
return true;
foreach (var value in typedArray)
{
if (Convert.ToUInt32(value) != 0)
return false;
}
return true;
}
public static bool AllZero(byte[] arr) => CheckAllZero(ref arr);
public static bool AllZero(int[] arr) => CheckAllZero(ref arr);
public static bool AllZero(uint[] arr) => CheckAllZero(ref arr);
public static bool AllZero(float[] arr) => CheckAllZero(ref arr);
public static uint[] MakeCmdArgs(uint[] args, uint maxArgs = Constants.DEFAULT_MAILBOX_ARGS)
{
uint[] cmdArgs = new uint[maxArgs];
uint length = (uint)Math.Min(maxArgs, args.Length);
for (int i = 0; i < length; i++)
cmdArgs[i] = args[i];
return cmdArgs;
}
public static uint[] MakeCmdArgs(uint arg = 0, uint maxArgs = Constants.DEFAULT_MAILBOX_ARGS)
{
return MakeCmdArgs(new uint[] { arg }, maxArgs);
}
// CO margin range seems to be from -30 to 30
// Margin arg seems to be 16 bits (lowest 16 bits of the command arg)
// Update 01 Nov 2022 - the range is different on Raphael, -40 is successfully set
public static uint MakePsmMarginArg(int margin)
{
// if (margin > 30)
// margin = 30;
// else if (margin < -30)
// margin = -30;
int offset = margin < 0 ? 0x100000 : 0;
return Convert.ToUInt32(offset + margin) & 0xffff;
}
public static T ByteArrayToStructure<T>(byte[] byteArray) where T : new()
{
if (byteArray == null)
return default;
T structure;
GCHandle handle = GCHandle.Alloc(byteArray, GCHandleType.Pinned);
try
{
structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
}
finally
{
handle.Free();
}
return structure;
}
public static uint ReverseBytes(uint value)
{
return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
(value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
}
public static string GetStringFromBytes(uint value)
{
byte[] bytes = BitConverter.GetBytes(value);
// Array.Reverse(bytes);
return System.Text.Encoding.ASCII.GetString(bytes).Replace("\0", " ");
}
public static string GetStringFromBytes(ulong value)
{
byte[] bytes = BitConverter.GetBytes(value);
// Array.Reverse(bytes);
return System.Text.Encoding.ASCII.GetString(bytes).Replace("\0", " ");
}
public static string GetStringFromBytes(byte[] value)
{
return System.Text.Encoding.ASCII.GetString(value).Replace("\0", " ");
}
/// <summary>Looks for the next occurrence of a sequence in a byte array</summary>
/// <param name="source">Array that will be scanned</param>
/// <param name="start">Index in the array at which scanning will begin</param>
/// <param name="pattern">Sequence the array will be scanned for</param>
/// <returns>
/// The index of the next occurrence of the sequence of -1 if not found
/// </returns>
public static int FindSequence(byte[] source, int start, byte[] pattern)
{
if (source == null || source.Length == 0 || pattern == null || pattern.Length == 0)
return -1;
if (pattern.Length > source.Length)
return -1;
if (start < 0 || start >= source.Length)
return -1;
int end = source.Length - pattern.Length;
byte firstByte = pattern[0];
while (start <= end)
{
if (source[start] == firstByte)
{
int offset;
for (offset = 1; offset < pattern.Length; offset++)
{
if (source[start + offset] != pattern[offset])
{
break;
}
}
if (offset == pattern.Length)
return start;
}
start++;
}
return -1;
}
public static bool ArrayMembersEqual(float[] array1, float[] array2, int numElements)
{
if (array1 == null || array2 == null)
return false;
if (array1.Length < numElements || array2.Length < numElements)
{
// throw new ArgumentException("Arrays are not long enough to compare the specified number of elements.");
Console.WriteLine("Arrays are not long enough to compare the specified number of elements.");
return false;
}
for (int i = 0; i < numElements; i++)
{
if (array1[i] != array2[i])
{
return false;
}
}
return true;
}
public static bool PartialStringMatch(string str, string[] arr)
{
bool match = false;
for (int i = 0; i < arr.Length; i++)
{
if (str.Contains(arr[i])) { match = true; break; }
}
return match;
}
public static float ToNanoseconds(uint value, float frequency)
{
if (frequency != 0)
{
float refiValue = Convert.ToSingle(value);
float trefins = refiValue * 2000f / frequency;
if (trefins > refiValue) trefins /= 2;
return trefins;
}
return 0;
}
public static void RemoveRegistryKey(string keyPath)
{
try
{
using (var key = Registry.LocalMachine.OpenSubKey(keyPath, true))
{
if (key != null)
{
Registry.LocalMachine.DeleteSubKeyTree(keyPath);
Console.WriteLine($"Deleted registry key: HKEY_LOCAL_MACHINE\\{keyPath}");
}
else
{
Console.WriteLine($"Registry key not found: HKEY_LOCAL_MACHINE\\{keyPath}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to delete registry key: {ex.Message}");
}
}
public class CommandExecutionResult
{
public bool Success { get; set; }
public string StandardOutput { get; set; }
public string StandardError { get; set; }
public int ExitCode { get; set; }
public string State { get; set; }
}
public static CommandExecutionResult ExecuteCommand(string command)
{
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var result = new CommandExecutionResult();
var standardOutput = new StringBuilder();
var standardError = new StringBuilder();
using (var process = Process.Start(processInfo))
{
if (process == null)
{
result.Success = false;
result.StandardError = "Failed to start process.";
return result;
}
process.OutputDataReceived += (sender, e) => { if (e.Data != null) standardOutput.AppendLine(e.Data); };
process.BeginOutputReadLine();
process.ErrorDataReceived += (sender, e) => { if (e.Data != null) standardError.AppendLine(e.Data); };
process.BeginErrorReadLine();
process.WaitForExit();
result.Success = process.ExitCode == 0;
result.StandardOutput = standardOutput.ToString();
result.StandardError = standardError.ToString();
result.ExitCode = process.ExitCode;
// Parse the state from the standard output
var stateMatch = Regex.Match(result.StandardOutput, @"STATE\s+:\s+\d+\s+(\w+)");
if (stateMatch.Success)
{
result.State = stateMatch.Groups[1].Value;
}
else
{
result.State = "Unknown";
}
}
return result;
}
public static bool DeleteFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
Console.WriteLine($"Deleted file: {filePath}");
}
else
{
Console.WriteLine($"File not found: {filePath}");
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public static bool HasDependentServices(string serviceName)
{
CommandExecutionResult result = ExecuteCommand($"sc.exe qc {serviceName}");
string output = result.StandardOutput;
bool hasDependents = false;
foreach (var line in output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
if (line.Trim().StartsWith("DEPENDENCIES"))
{
string dependencies = line.Split(new[] { ':' }, 2)[1].Trim();
if (!string.IsNullOrEmpty(dependencies))
{
hasDependents = true;
Console.WriteLine($"Dependent services: {dependencies}");
}
}
}
return hasDependents;
}
public static int GetServiceProcessId(string serviceName)
{
CommandExecutionResult result = ExecuteCommand($"sc.exe queryex {serviceName}");
string output = result.StandardOutput;
foreach (var line in output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
{
if (line.Trim().StartsWith("PID"))
{
if (int.TryParse(line.Split(':')[1].Trim(), out int pid))
{
return pid;
}
}
}
return -1;
}
public static void KillProcess(int processId)
{
try
{
Process process = Process.GetProcessById(processId);
process.Kill();
process.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine($"Failed to kill process {processId}: {ex.Message}");
}
}
public static bool ServiceExists(string serviceName)
{
CommandExecutionResult result = ExecuteCommand($"sc query {serviceName}");
string output = result.StandardOutput;
return !output.Contains("FAILED 1060");
}
}
}