-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.cs
More file actions
76 lines (64 loc) · 1.94 KB
/
Utilities.cs
File metadata and controls
76 lines (64 loc) · 1.94 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
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
namespace RoleBoi;
public static class Extensions
{
private static readonly DateTimeOffset UnixEpoch =
new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
public static long ToUnixTimeMicroseconds(this DateTimeOffset timestamp)
{
TimeSpan duration = timestamp - UnixEpoch;
// There are 10 ticks per microsecond.
return duration.Ticks / 10;
}
}
public static class Utilities
{
// CLOCK_MONOTONIC is the specific clock we want to read in the clock_gettime function and has the ID 1.
private const int CLOCK_MONOTONIC = 1;
[StructLayout(LayoutKind.Sequential)]
private struct Timespec
{
public long tv_sec;
public long tv_nsec;
}
[SupportedOSPlatform("linux")]
[DllImport("libc", EntryPoint = "clock_gettime", SetLastError = true)]
private static extern int clock_gettime(int clk_id, out Timespec tp);
public static long GetMonotonicUsec()
{
if (!OperatingSystem.IsLinux())
{
return -1;
}
try
{
if (clock_gettime(CLOCK_MONOTONIC, out Timespec ts) == 0)
{
checked
{
return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}
}
}
catch { /* ignored */ }
return Environment.TickCount64 * 1000;
}
public static string ReadManifestData(string embeddedFileName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string resourceName = assembly.GetManifestResourceNames().First(s => s.EndsWith(embeddedFileName,StringComparison.CurrentCultureIgnoreCase));
using Stream stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
throw new InvalidOperationException("Could not load manifest resource stream.");
}
using StreamReader reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
}