-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgram.cs
331 lines (301 loc) · 13.6 KB
/
Program.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
/**
* @file Program.cs
*
* @copyright 2015-2018 Bill Zissimopoulos
*/
/*
* This file is part of WinFsp.
*
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License version 3 as published by the Free Software
* Foundation.
*
* Licensees holding a valid commercial license may use this software
* in accordance with the commercial license agreement provided in
* conjunction with the software. The terms and conditions of any such
* commercial license agreement shall govern, supersede, and render
* ineffective any application of the GPLv3 license to this software,
* notwithstanding of any reference thereto in the software or
* associated repository.
*/
using System;
using System.IO;
using Microsoft.Win32;
using Fsp;
using System.Security.AccessControl;
using System.Collections.Generic;
namespace XboxWinFsp
{
class XboxFsService : Service
{
private FileSystemHost _Host;
private List<FileSystemHost> _Hosts;
private class CommandLineUsageException : Exception
{
public CommandLineUsageException(String Message = null) : base(Message)
{
HasMessage = null != Message;
}
public bool HasMessage;
}
private const String PROGNAME = "xbox-winfsp";
public XboxFsService() : base("XboxFsService")
{
}
protected override void OnStart(String[] Args)
{
try
{
String DebugLogFile = null;
UInt32 DebugFlags = 0;
String VolumePrefix = null;
String ImagePath = null;
String MountPoint = null;
IntPtr DebugLogHandle = (IntPtr)(-1);
bool SetupFS = false;
bool RemoveFS = false;
FileSystemHost Host = null;
GdfxFileSystem Gdfx = null;
StfsFileSystem Stfs = null;
FatxFileSystem Fatx = null;
int I;
for (I = 1; Args.Length > I; I++)
{
String Arg = Args[I];
if ('-' != Arg[0])
break;
switch (Arg[1])
{
case '?':
throw new CommandLineUsageException();
case 'd':
argtol(Args, ref I, ref DebugFlags);
break;
case 'D':
argtos(Args, ref I, ref DebugLogFile);
break;
case 'm':
argtos(Args, ref I, ref MountPoint);
break;
case 'i':
argtos(Args, ref I, ref ImagePath);
break;
case 'u':
argtos(Args, ref I, ref VolumePrefix);
break;
case 's':
SetupFS = true;
break;
case 'r':
RemoveFS = true;
break;
default:
throw new CommandLineUsageException();
}
}
if (Args.Length > I)
throw new CommandLineUsageException();
if (SetupFS)
{
try
{
Console.WriteLine("\r\nSetting up Xbox filesystems...\r\n");
// Add to WinFsp services list, allows using "net use X: \\xbox-winfps\C$\game.iso"
Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\WinFsp\Services\xbox-winfsp", "CommandLine", "-u %1 -m %2", RegistryValueKind.String);
Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\WinFsp\Services\xbox-winfsp", "Executable", System.Reflection.Assembly.GetEntryAssembly().Location, RegistryValueKind.String);
Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\WinFsp\Services\xbox-winfsp", "Security", "D:P(A;;RPWPLC;;;WD)", RegistryValueKind.String);
Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\WinFsp\Services\xbox-winfsp", "JobControl", 1, RegistryValueKind.DWord);
// Context menu item for all files (since STFS has no extension...)
Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Classes\*\shell\Mount as Xbox STFS/GDF\command", null, $"\"{System.Reflection.Assembly.GetEntryAssembly().Location}\" -i \"%1\" -m *");
Console.WriteLine("Successfully setup filesystems, you may need to restart for changes to take effect.\r\n");
}
catch
{
Console.WriteLine("Error: Failed to setup filesystems, maybe try running as admin?\r\n");
}
}
if (RemoveFS)
{
try
{
bool error = false;
Console.WriteLine("\r\nRemoving any Xbox filesystems...\r\n");
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\WinFsp\Services", true);
if (key != null)
key.DeleteSubKeyTree("xbox-winfsp", true);
}
catch
{
Console.WriteLine("Error: Failed to remove xbox-winfsp key!\r\n");
error = true;
}
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Classes\*\shell", true);
if (key != null)
{
key.DeleteSubKeyTree("Mount as Xbox STFS/GDF");
}
}
catch
{
Console.WriteLine("Error: Failed to remove context-menu key!\r\n");
error = true;
}
if (error)
throw new Exception();
Console.WriteLine("Removed Xbox filesystems successfully.\r\n");
}
catch
{
Console.WriteLine("An error was encountered, maybe try running as admin?\r\n");
}
}
if (null == ImagePath && null != VolumePrefix)
{
I = VolumePrefix.IndexOf('\\');
if (-1 != I && VolumePrefix.Length > I && '\\' != VolumePrefix[I + 1])
{
I = VolumePrefix.IndexOf('\\', I + 1);
if (-1 != I &&
VolumePrefix.Length > I + 1 &&
(
('A' <= VolumePrefix[I + 1] && VolumePrefix[I + 1] <= 'Z') ||
('a' <= VolumePrefix[I + 1] && VolumePrefix[I + 1] <= 'z')
) &&
'$' == VolumePrefix[I + 2])
{
ImagePath = String.Format("{0}:{1}", VolumePrefix[I + 1], VolumePrefix.Substring(I + 3));
}
}
}
if (null != DebugLogFile)
if (0 > FileSystemHost.SetDebugLogFile(DebugLogFile))
throw new CommandLineUsageException("cannot open debug log file");
if (!string.IsNullOrEmpty(ImagePath) && !string.IsNullOrEmpty(MountPoint))
{
// For some reason WinFsp needs MountPoint to be null for wildcard to work without elevation...
bool openExplorer = false;
if (MountPoint == "*")
{
MountPoint = null;
openExplorer = true; // Open mounted drive in explorer if the mountPoint is wildcard - QoL :)
}
var fileStream = File.OpenRead(ImagePath);
Host = new FileSystemHost(Fatx = new FatxFileSystem(fileStream, ImagePath));
Host.Prefix = VolumePrefix;
if (Host.Mount(MountPoint, null, true, DebugFlags) < 0)
{
Fatx = null;
fileStream.Position = 0;
Host = new FileSystemHost(Stfs = new StfsFileSystem(fileStream, ImagePath));
Host.Prefix = VolumePrefix;
if (Host.Mount(MountPoint, null, true, DebugFlags) < 0)
{
Stfs = null;
fileStream.Position = 0;
Host = new FileSystemHost(Gdfx = new GdfxFileSystem(fileStream, ImagePath));
Host.Prefix = VolumePrefix;
if (Host.Mount(MountPoint, null, true, DebugFlags) < 0)
throw new IOException("cannot mount file system");
}
}
MountPoint = Host.MountPoint();
_Host = Host;
if (openExplorer)
System.Diagnostics.Process.Start("explorer.exe", MountPoint);
Log(EVENTLOG_INFORMATION_TYPE, String.Format("{0}{1}{2} -p {3} -m {4}",
PROGNAME,
null != VolumePrefix && 0 < VolumePrefix.Length ? " -u " : "",
null != VolumePrefix && 0 < VolumePrefix.Length ? VolumePrefix : "",
ImagePath,
MountPoint));
Console.Title = $"{MountPoint} - xbox-winfsp";
Console.WriteLine($"\r\n{ImagePath}:\r\n Mounted to {MountPoint}, hit CTRL+C in this window to unmount.\r\n");
}
else
{
_Hosts = new List<FileSystemHost>();
string connectedDrives = "";
if (Utility.IsAdministrator())
{
Log(EVENTLOG_INFORMATION_TYPE, "Loading Xbox partitions from physical drives...");
for (int i = 1; i < 11; i++)
{
try
{
var device = new FatxDevice(i);
if (!device.IsFatxDevice())
continue;
var partitions = device.LoadPartitions(DebugFlags);
if (partitions.Count > 0)
connectedDrives += $"{i} ";
_Hosts.AddRange(partitions);
}
catch
{ }
}
Log(EVENTLOG_INFORMATION_TYPE, $"Loaded {_Hosts.Count} Xbox partitions from drives.");
}
if (_Hosts.Count <= 0)
throw new CommandLineUsageException();
Console.Title = $"HDD {connectedDrives}- xbox-winfsp";
Console.WriteLine("\r\nHit CTRL+C in this window to unmount.");
}
}
catch (CommandLineUsageException ex)
{
Log(EVENTLOG_ERROR_TYPE, String.Format(
"{0}" +
"usage: {1} OPTIONS\n" +
"\n" +
"options:\n" +
" -d DebugFlags [-1: enable all debug logs]\n" +
" -D DebugLogFile [file path; use - for stderr]\n" +
" -i ImagePath [path to GDFX/STFS image to be mounted]\n" +
" -u \\Server\\ImagePath [UNC prefix (single backslash)]\n" +
" -m MountPoint [X:|*|directory]\n" +
" -s [installs xbox-winfsp filesystems, may need elevation!]\n" +
" -r [removes any xbox-winfsp filesystems, may need elevation!]\n",
ex.HasMessage ? ex.Message + "\n" : "",
PROGNAME)); ;
throw;
}
//}
//catch (Exception ex)
//{
// Log(EVENTLOG_ERROR_TYPE, String.Format("{0}", ex.Message));
// throw;
// }
}
protected override void OnStop()
{
_Host.Unmount();
_Host = null;
}
private static void argtos(String[] Args, ref int I, ref String V)
{
if (Args.Length > ++I)
V = Args[I];
else
throw new CommandLineUsageException();
}
private static void argtol(String[] Args, ref int I, ref UInt32 V)
{
Int32 R;
if (Args.Length > ++I)
V = Int32.TryParse(Args[I], out R) ? (UInt32)R : V;
else
throw new CommandLineUsageException();
}
}
class Program
{
static void Main(string[] args)
{
Environment.ExitCode = new XboxFsService().Run();
}
}
}