-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
RtlFeatureManager.cs
203 lines (181 loc) · 8.89 KB
/
RtlFeatureManager.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
/*
ViVe - Vibranium feature configuration library
Copyright (C) 2019 @thebookisclosed
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace Albacore.ViVe
{
public static class RtlFeatureManager
{
public static List<FeatureConfiguration> QueryAllFeatureConfigurations()
{
uint dummy = 0;
return QueryAllFeatureConfigurations(FeatureConfigurationSection.Runtime, ref dummy);
}
public static List<FeatureConfiguration> QueryAllFeatureConfigurations(FeatureConfigurationSection section)
{
uint dummy = 0;
return QueryAllFeatureConfigurations(section, ref dummy);
}
public static List<FeatureConfiguration> QueryAllFeatureConfigurations(FeatureConfigurationSection section, ref uint changeStamp)
{
int featureCount = 0;
NativeMethods.RtlQueryAllFeatureConfigurations(section, ref changeStamp, IntPtr.Zero, ref featureCount);
if (featureCount == 0)
return null;
// One feature config is 12 bytes long
IntPtr rawBuf = Marshal.AllocHGlobal(featureCount * 12);
int hRes = NativeMethods.RtlQueryAllFeatureConfigurations(section, ref changeStamp, rawBuf, ref featureCount);
if (hRes != 0)
{
Marshal.FreeHGlobal(rawBuf);
return null;
}
byte[] buf = new byte[featureCount * 12];
Marshal.Copy(rawBuf, buf, 0, buf.Length);
Marshal.FreeHGlobal(rawBuf);
var allFeatureConfigs = new List<FeatureConfiguration>();
using (MemoryStream ms = new MemoryStream(buf, false))
{
using (BinaryReader br = new BinaryReader(ms))
{
for (int i = 0; i < featureCount; i++)
{
uint featureId = br.ReadUInt32();
int compactState = br.ReadInt32();
allFeatureConfigs.Add(new FeatureConfiguration() {
FeatureId = featureId,
Group = compactState & 0xF,
EnabledState = (FeatureEnabledState)((compactState & 0x30) >> 4),
EnabledStateOptions = (compactState & 0x40) >> 6,
Variant = (compactState & 0x3F00) >> 8,
VariantPayloadKind = (compactState & 0xC000) >> 14,
VariantPayload = br.ReadInt32() });
}
}
}
return allFeatureConfigs;
}
public static FeatureConfiguration QueryFeatureConfiguration(uint featureId)
{
uint dummy = 0;
return QueryFeatureConfiguration(featureId, FeatureConfigurationSection.Runtime, ref dummy);
}
public static FeatureConfiguration QueryFeatureConfiguration(uint featureId, FeatureConfigurationSection section)
{
uint dummy = 0;
return QueryFeatureConfiguration(featureId, section, ref dummy);
}
public static FeatureConfiguration QueryFeatureConfiguration(uint featureId, FeatureConfigurationSection section, ref uint changeStamp)
{
// One feature config is 12 bytes long
IntPtr rawBuf = Marshal.AllocHGlobal(12);
int hRes = NativeMethods.RtlQueryFeatureConfiguration(featureId, section, ref changeStamp, rawBuf);
if (hRes != 0)
{
Marshal.FreeHGlobal(rawBuf);
return null;
}
byte[] buf = new byte[12];
Marshal.Copy(rawBuf, buf, 0, buf.Length);
Marshal.FreeHGlobal(rawBuf);
int compactState = BitConverter.ToInt32(buf, 4);
return new FeatureConfiguration() {
FeatureId = BitConverter.ToUInt32(buf, 0),
Group = compactState & 0xF,
EnabledState = (FeatureEnabledState)((compactState & 0x30) >> 4),
EnabledStateOptions = (compactState & 0x40) >> 6,
Variant = (compactState & 0x3F00) >> 8,
VariantPayloadKind = (compactState & 0xC000) >> 14,
VariantPayload = BitConverter.ToInt32(buf, 8) };
}
public static uint QueryFeatureConfigurationChangeStamp()
{
return NativeMethods.RtlQueryFeatureConfigurationChangeStamp();
}
public static int SetFeatureConfigurations(List<FeatureConfiguration> configurations)
{
uint dummy = 0;
return SetFeatureConfigurations(configurations, FeatureConfigurationSection.Runtime, ref dummy);
}
public static int SetFeatureConfigurations(List<FeatureConfiguration> configurations, FeatureConfigurationSection section)
{
uint dummy = 0;
return SetFeatureConfigurations(configurations, section, ref dummy);
}
public static int SetFeatureConfigurations(List<FeatureConfiguration> configurations, FeatureConfigurationSection section, ref uint changeStamp)
{
return NativeMethods.RtlSetFeatureConfigurations(ref changeStamp, section, RtlDataHelpers.SerializeFeatureConfigurations(configurations), configurations.Count);
}
public static IntPtr RegisterFeatureConfigurationChangeNotification(FeatureConfigurationChangeCallback callback)
{
return RegisterFeatureConfigurationChangeNotification(callback, IntPtr.Zero);
}
public static IntPtr RegisterFeatureConfigurationChangeNotification(FeatureConfigurationChangeCallback callback, IntPtr context)
{
NativeMethods.RtlRegisterFeatureConfigurationChangeNotification(callback, context, IntPtr.Zero, out IntPtr sub);
return sub;
}
public static int UnregisterFeatureConfigurationChangeNotification(IntPtr notification)
{
return NativeMethods.RtlUnregisterFeatureConfigurationChangeNotification(notification);
}
public static List<FeatureUsageSubscription> QueryFeatureUsageSubscriptions()
{
int subCount = 0;
NativeMethods.RtlQueryFeatureUsageNotificationSubscriptions(IntPtr.Zero, ref subCount);
if (subCount == 0)
return null;
// One feature subscription is 16 bytes long
IntPtr rawBuf = Marshal.AllocHGlobal(subCount * 16);
int hRes = NativeMethods.RtlQueryFeatureUsageNotificationSubscriptions(rawBuf, ref subCount);
if (hRes != 0)
return null;
byte[] buf = new byte[subCount * 16];
Marshal.Copy(rawBuf, buf, 0, buf.Length);
Marshal.FreeHGlobal(rawBuf);
var allSubscriptions = new List<FeatureUsageSubscription>();
using (MemoryStream ms = new MemoryStream(buf, false))
{
using (BinaryReader br = new BinaryReader(ms))
{
for (int i = 0; i < subCount; i++)
{
allSubscriptions.Add(new FeatureUsageSubscription() {
FeatureId = br.ReadUInt32(),
ReportingKind = br.ReadUInt16(),
ReportingOptions = br.ReadUInt16(),
ReportingTarget = br.ReadUInt64()
});
}
}
}
return allSubscriptions;
}
public static int AddFeatureUsageSubscriptions(List<FeatureUsageSubscription> subscriptions)
{
return NativeMethods.RtlSubscribeForFeatureUsageNotification(RtlDataHelpers.SerializeFeatureUsageSubscriptions(subscriptions), subscriptions.Count);
}
public static int RemoveFeatureUsageSubscriptions(List<FeatureUsageSubscription> subscriptions)
{
return NativeMethods.RtlUnsubscribeFromFeatureUsageNotifications(RtlDataHelpers.SerializeFeatureUsageSubscriptions(subscriptions), subscriptions.Count);
}
public static int NotifyFeatureUsage(FeatureUsageReport report)
{
return NativeMethods.RtlNotifyFeatureUsage(RtlDataHelpers.SerializeFeatureUsageReport(report));
}
}
}