-
Notifications
You must be signed in to change notification settings - Fork 6
/
Vista.cs
282 lines (249 loc) · 7.48 KB
/
Vista.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
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Security.Principal;
using Microsoft.Win32;
namespace Noxico
{
public static class Vista
{
public static bool IsVista = (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6);
public static readonly Guid SavedGames = new Guid("4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4");
//Add more GUIDs here whenever interesting.
public static string GetInterestingPath(Guid target)
{
if (!IsVista)
return null;
string ret = null;
IntPtr pPath;
try
{
if (SafeNativeMethods.SHGetKnownFolderPath(target, 0, IntPtr.Zero, out pPath) == 0)
{
ret = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
}
return ret;
}
catch (DllNotFoundException)
{
return null;
}
}
public static bool GamepadEnabled { get; set; }
public static bool GamepadAvailable { get; private set; }
public static bool GamepadHasInput { get; private set; }
public static XInputGamePad GamepadState { get; private set; }
public static XInputButtons DPad { get; private set; }
public static XInputButtons Triggers { get; private set; }
private static XInputState padState;
private static int padPacket;
private static XInputButtons lastTrigs;
//private static readonly XInputButtons triggerMask = (XInputButtons)0xFFF0;
private static readonly XInputButtons directionMask = (XInputButtons)0x000F;
public static XInputErrorCodes UpdateGamepad()
{
if (!GamepadEnabled || !IsVista)
return XInputErrorCodes.AccessDenied;
try
{
var ret = SafeNativeMethods.XInputGetState(0, out padState);
GamepadAvailable = (ret == XInputErrorCodes.Success);
if (ret == XInputErrorCodes.Success)
{
GamepadState = padState.GamePad;
GamepadHasInput = (padPacket != padState.PacketNumber);
if (GamepadHasInput)
{
var rawButtons = padState.GamePad.Buttons;
DPad = rawButtons & directionMask;
var newTrigs = rawButtons; // & triggerMask;
if (lastTrigs != newTrigs)
Triggers = lastTrigs = newTrigs;
else if (newTrigs == lastTrigs)
Triggers = lastTrigs = 0;
}
padPacket = padState.PacketNumber;
}
return ret;
}
catch (DllNotFoundException)
{
//Thanks to Adamsk on #rgrd for bringing this to my attention.
GamepadEnabled = false;
return XInputErrorCodes.AccessDenied;
}
}
public static void ReleaseTriggers()
{
Triggers = 0;
}
public static bool GamepadFocused
{
set
{
if (GamepadEnabled && IsVista)
{
try
{
SafeNativeMethods.XInputEnable(value);
}
catch (DllNotFoundException)
{
GamepadEnabled = false;
}
}
}
}
}
public enum XInputErrorCodes : uint
{
Success,
Pending = 997u,
NotConnected = 1167u,
Empty = 4306u,
Busy = 170u,
AccessDenied = 5u,
AlreadyExists = 183u,
}
[Flags]
public enum XInputButtons : ushort
{
Up = 0x01,
Down = 0x02,
Left = 0x04,
Right = 0x08,
Start = 0x10,
Back = 0x20,
LeftThumb = 0x40,
RightThumb = 0x80,
LeftShoulder = 0x0100,
RightShoulder = 0x0200,
//0x0400 is unlisted?
BigButton = 0x0800,
A = 0x1000,
B = 0x2000,
X = 0x4000,
Y = 0x8000,
}
public struct XInputGamePad
{
public XInputButtons Buttons;
public byte LeftTrigger;
public byte RightTrigger;
public short ThumbLX;
public short ThumbLY;
public short ThumbRX;
public short ThumbRY;
}
public struct XInputState
{
public int PacketNumber;
public XInputGamePad GamePad;
}
internal static class SafeNativeMethods
{
[DllImport("shell32.dll")]
public static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
[DllImport("xinput1_3.dll")]
internal static extern XInputErrorCodes XInputGetState(int playerIndex, out XInputState pState);
[DllImport("xinput1_3.dll")]
internal static extern void XInputEnable([MarshalAs(UnmanagedType.Bool)]bool enable);
}
public static class UacHelper
{
private const string uacRegistryKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
private const string uacRegistryValue = "EnableLUA";
private static uint STANDARD_RIGHTS_READ = 0x00020000;
private static uint TOKEN_QUERY = 0x0008;
private static uint TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool GetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, uint TokenInformationLength, out uint ReturnLength);
public enum TOKEN_INFORMATION_CLASS
{
TokenUser = 1,
TokenGroups,
TokenPrivileges,
TokenOwner,
TokenPrimaryGroup,
TokenDefaultDacl,
TokenSource,
TokenType,
TokenImpersonationLevel,
TokenStatistics,
TokenRestrictedSids,
TokenSessionId,
TokenGroupsAndPrivileges,
TokenSessionReference,
TokenSandBoxInert,
TokenAuditPolicy,
TokenOrigin,
TokenElevationType,
TokenLinkedToken,
TokenElevation,
TokenHasRestrictions,
TokenAccessInformation,
TokenVirtualizationAllowed,
TokenVirtualizationEnabled,
TokenIntegrityLevel,
TokenUIAccess,
TokenMandatoryPolicy,
TokenLogonSid,
MaxTokenInfoClass
}
public enum TOKEN_ELEVATION_TYPE
{
TokenElevationTypeDefault = 1,
TokenElevationTypeFull,
TokenElevationTypeLimited
}
public static bool IsUacEnabled
{
get
{
RegistryKey uacKey = Registry.LocalMachine.OpenSubKey(uacRegistryKey, false);
bool result = uacKey.GetValue(uacRegistryValue).Equals(1);
return result;
}
}
public static bool IsProcessElevated
{
get
{
if (IsUacEnabled)
{
IntPtr tokenHandle;
if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_READ, out tokenHandle))
{
throw new ApplicationException("Could not get process token. Win32 Error Code: " + Marshal.GetLastWin32Error());
}
TOKEN_ELEVATION_TYPE elevationResult = TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault;
int elevationResultSize = Marshal.SizeOf((int)elevationResult);
uint returnedSize = 0;
IntPtr elevationTypePtr = Marshal.AllocHGlobal(elevationResultSize);
bool success = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, (uint)elevationResultSize, out returnedSize);
if (success)
{
elevationResult = (TOKEN_ELEVATION_TYPE)Marshal.ReadInt32(elevationTypePtr);
bool isProcessAdmin = elevationResult == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
return isProcessAdmin;
}
else
{
throw new ApplicationException("Unable to determine the current elevation.");
}
}
else
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool result = principal.IsInRole(WindowsBuiltInRole.Administrator);
return result;
}
}
}
}
}