-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BranchHandler.cs
296 lines (248 loc) · 12.2 KB
/
BranchHandler.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
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Steam_Games_Branch_Manager
{
internal static class BranchHandler
{
internal static Tuple<DialogResult, string, string, string, string> TryCreateBranch(string gameName,
string gamePath, string acfPath, string branchName, BackgroundWorker worker, int depth = 0)
{
if (depth == 5)
{
MessageBox.Show(@"Too many failed attempts. Aborting.");
return new Tuple<DialogResult, string, string, string, string>(DialogResult.Abort, gameName, gamePath,
acfPath, branchName);
}
DialogResult result = DialogResult.OK;
try
{
if (!CreateGameBranch(gamePath, acfPath, branchName, worker))
throw new Exception("CreateBranch returned false");
}
catch (Exception exception)
{
result = MessageBox.Show($@"Failed to Create Backup of {gameName} from {gamePath}:
{exception}", @"ERROR",
MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
switch (result)
{
case DialogResult.Abort:
result = DialogResult.Abort;
break;
case DialogResult.Ignore:
result = DialogResult.OK;
break;
case DialogResult.Retry:
result = TryCreateBranch(gameName, gamePath, acfPath, branchName, worker, depth + 1).Item1;
break;
}
}
worker.ReportProgress(100, result == DialogResult.OK ? $"Creating {branchName} Complete" : "Failed");
return new Tuple<DialogResult, string, string, string, string>(result, gameName, gamePath, acfPath,
branchName);
}
[DllImport("kernel32.dll")]
private static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName,
SymbolicLink dwFlags);
private static bool CreateGameBranch(string gamePath, string acfPath, string branchName,
BackgroundWorker worker)
{
if (!Directory.Exists($"{gamePath}Branches"))
Directory.CreateDirectory($"{gamePath}Branches");
if (Directory.Exists($"{gamePath}Branches/{branchName}"))
throw new Exception(
$"{gamePath}Branches/{branchName}\nAlready Exists!\nCannot overwrite existing folders!");
if (branchName != "original" && !Directory.Exists($"{gamePath}Branches/original"))
throw new Exception($"Tried to create a branch but could not find \n{gamePath}Branches/original");
if (branchName != "original" && !File.Exists($"{gamePath}Branches/original/{Path.GetFileName(acfPath)}"))
throw new Exception(
$"original Acf file not found at {gamePath}Branches/original/{Path.GetFileName(acfPath)}.original");
if (branchName == "original")
{
Directory.Move(gamePath, $"{gamePath}Branches/{branchName}");
File.Move(acfPath, $"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}");
CreateSymbolicLink(gamePath, $"{gamePath}Branches/{branchName}", SymbolicLink.Directory);
CreateSymbolicLink(acfPath, $"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}",
SymbolicLink.File);
FileInfo pathInfo = new FileInfo(acfPath);
var success = pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint);
if (success)
return true;
if (File.Exists($"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}"))
File.Move($"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}", acfPath);
if (Directory.Exists($"{gamePath}Branches/{branchName}"))
Directory.Move($"{gamePath}Branches/{branchName}", gamePath);
return false;
}
CopyDirectory($"{gamePath}Branches/original", $"{gamePath}Branches/{branchName}", true, false, worker);
return Directory.Exists($"{gamePath}Branches/{branchName}") &&
File.Exists($"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}");
}
internal static void SetActiveBranch(string gamePath, string acfPath, string branchName)
{
FileInfo acfPathInfo = new FileInfo(acfPath);
if (File.Exists($"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}"))
{
try
{
acfPathInfo.Delete();
}
catch {
}
CreateSymbolicLink(acfPath, $"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}", SymbolicLink.File);
}
DirectoryInfo gamePathInfo = new DirectoryInfo(gamePath);
if (Directory.Exists($"{gamePath}Branches/{branchName}"))
{
try
{
gamePathInfo.Delete();
}
catch {
}
CreateSymbolicLink(gamePath, $"{gamePath}Branches/{branchName}", SymbolicLink.Directory);
}
}
private static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, bool deep,
BackgroundWorker worker)
{
// Get information about the source directory
DirectoryInfo dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
// Cache directories before we start copying
var dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
if (recursive && deep)
// If recursive and copying subdirectories, directory structure fist then files
foreach (DirectoryInfo subDir in dirs)
{
var newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true, true, worker);
}
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
var targetFilePath = Path.Combine(destinationDir, file.Name);
worker.ReportProgress(0, file.Name);
file.CopyTo(targetFilePath);
}
if (recursive && !deep)
// If recursive and copying subdirectories, all files first then each sub directory
foreach (DirectoryInfo subDir in dirs)
{
var newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true, false, worker);
}
}
public static Tuple<DialogResult, string, string, string, string> TryDeleteBranch(string gameName,
string gamePath, string acfPath, string branchName, BackgroundWorker worker, int depth = 0)
{
if (depth == 5)
{
MessageBox.Show(@"Too many failed attempts. Aborting.");
return new Tuple<DialogResult, string, string, string, string>(DialogResult.Abort, gameName, gamePath,
acfPath, branchName);
}
DialogResult result = DialogResult.OK;
try
{
if (!DeleteGameBranch(gamePath, acfPath, branchName, worker))
throw new Exception("CreateBranch returned false");
}
catch (Exception exception)
{
result = MessageBox.Show(
$@"Failed to delete branch {branchName} of {gameName} from {gamePath}Branches\{branchName}:
{exception}", @"ERROR",
MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
switch (result)
{
case DialogResult.Abort:
result = DialogResult.Abort;
break;
case DialogResult.Ignore:
result = DialogResult.OK;
break;
case DialogResult.Retry:
result = TryDeleteBranch(gameName, gamePath, acfPath, branchName, worker, depth + 1).Item1;
break;
}
}
worker.ReportProgress(100, result == DialogResult.OK ? $"Deleting {branchName} Complete" : "Failed");
return new Tuple<DialogResult, string, string, string, string>(result, gameName, gamePath, acfPath,
branchName);
}
public static Tuple<DialogResult, string, string, string, string[]> TryDeleteGame(string gameName,
string gamePath, string acfPath, string[] branches, BackgroundWorker worker, int depth = 0)
{
DialogResult result = DialogResult.OK;
foreach (var branch in branches)
{
result = TryDeleteBranch(gameName, gamePath, acfPath, branch, worker).Item1;
if (result != DialogResult.OK)
break;
}
DirectoryInfo branchFolderInfo = new DirectoryInfo($"{gamePath}Branches");
if (result == DialogResult.OK && branchFolderInfo.Exists && branchFolderInfo.GetDirectories().Length == 0)
branchFolderInfo.Delete();
worker.ReportProgress(100, result == DialogResult.OK ? $"Deleting {gameName} Complete" : "Failed");
return new Tuple<DialogResult, string, string, string, string[]>(result, gameName, gamePath, acfPath,
branches);
}
private static bool DeleteGameBranch(string gamePath, string acfPath, string branchName,
BackgroundWorker worker)
{
if (!Directory.Exists($"{gamePath}Branches"))
return true;
if (!Directory.Exists($"{gamePath}Branches/{branchName}"))
return true;
if (branchName == "original")
{
FileInfo acfPathInfo = new FileInfo(acfPath);
if (File.Exists($"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}") &&
(acfPathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint) || acfPathInfo.Exists))
{
acfPathInfo.Delete();
File.Move($"{gamePath}Branches/{branchName}/{Path.GetFileName(acfPath)}", acfPath);
}
DirectoryInfo gamePathInfo = new DirectoryInfo(gamePath);
if (Directory.Exists($"{gamePath}Branches/{branchName}") &&
gamePathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint))
{
gamePathInfo.Delete();
Directory.Move($"{gamePath}Branches/{branchName}", gamePath);
}
}
else
{
DeleteDirectory($"{gamePath}Branches/{branchName}", worker);
}
return !Directory.Exists($"{gamePath}Branches/{branchName}");
}
private static void DeleteDirectory(string sourceDir, BackgroundWorker worker)
{
DirectoryInfo dir = new DirectoryInfo(sourceDir);
if (!dir.Exists)
return;
var dirs = dir.GetDirectories();
foreach (DirectoryInfo subDir in dirs) DeleteDirectory(subDir.FullName, worker);
foreach (FileInfo file in dir.GetFiles())
{
worker.ReportProgress(0, file.Name);
file.Delete();
}
dir.Delete();
}
private enum SymbolicLink
{
File = 0,
Directory = 1
}
}
}