-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheDeleter.cs
368 lines (317 loc) · 14 KB
/
CacheDeleter.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using Microsoft.Win32.SafeHandles;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;
namespace VRChat_Cache_Auto_Deleter
{
public partial class CacheDeleter : Form
{
public CacheDeleter()
{
InitializeComponent();
}
public long LastKnownDriveSpace = 120;
public long LastKnownCacheSize = 0;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
AutoDeleteTimer.Enabled = true;
AutoDeleteTimer.Start();
}
else
{
AutoDeleteTimer.Stop();
AutoDeleteTimer.Enabled = false;
}
}
public static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
/// <summary>
/// Clears VRChat's Cache
/// </summary>
public void ClearVRCCache()
{
if (Directory.Exists(VRChatCacheDir))
{
foreach (var folder in Directory.GetDirectories(
VRChatCacheDir + "\\vrchat\\"))
{
var DirInfo = new DirectoryInfo(folder);
switch (DirInfo.Name)
{
default:
MessageBox.Show(DirInfo.Name);
break;
case "Cookies":
try
{
Directory.Delete(folder, true);
}
catch
{
}
break;
case "Cache-WindowsPlayer":
try
{
Directory.Delete(folder, true);
}
catch
{
}
break;
case "HTTPCache-WindowsPlayer":
try
{
Directory.Delete(folder, true);
}
catch
{
}
break;
case "Unity":
try
{
Directory.Delete(folder, true);
}
catch
{
}
break;
case "VRCHTTPCache":
try
{
Directory.Delete(folder, true);
}
catch
{
}
break;
}
}
}
}
private void AutoDeleteTimer_Tick(object sender, EventArgs e)
{
var Drive = DriveInfo.GetDrives().First(o => o.Name == @"C:\");
if (Drive != null)
{
if (Drive.AvailableSpaceInGB() == 5 || VRChatCacheDir.GetDirectorySizeInGB() > 19) // Drive Space Is Less Than 5GB Or Cache Is Full
{
AutoDeleteTimer.Stop();
AutoDeleteTimer.Enabled = false;
ClearVRCCache();
AutoDeleteTimer.Enabled = true;
AutoDeleteTimer.Start();
}
}
label2.Text =
$"Last Known Free Drive Space: {Drive.AvailableSpaceInGB()}GB - Last Known Cache Size: {VRChatCacheDir.GetDirectorySizeInGB()}GB";
}
private void button1_Click(object sender, EventArgs e)
{
if (IsAdministrator() && comboBox2.Text.Contains(@":\"))
{
try
{
Enabled = false;
Directory.CreateDirectory(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToString().Replace(@"C:\", comboBox2.Text) + "\\LocalLow\\");
if (Directory.Exists(VRChatCacheDir))
{
CopyFolder(VRChatCacheDir, Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToString().Replace(@"C:\", comboBox2.Text) + "\\LocalLow\\VRChat\\");
}
if (Directory.Exists(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "\\LocalLow\\VRChat\\vrchat\\"))
{
Directory.Delete(VRChatCacheDir, true);
}
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c mklink /d " + VRChatCacheDir + " " + Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToString().Replace(@"C:\", comboBox2.Text) + "\\LocalLow\\VRChat\\";
process.StartInfo = startInfo;
process.Start();
Enabled = true;
}
catch (DirectoryNotFoundException)
{
Enabled = true;
MessageBox.Show("You Do Not Have A VRChat Cache Or You Have Already Moved It Before.");
}
}
else if (IsAdministrator() == true && !comboBox2.Text.Contains(@":\"))
{
MessageBox.Show("You Must Select A Drive To Proceed!");
}
else if (IsAdministrator() == false)
{
Enabled = true;
MessageBox.Show("This Tool MUST Be Ran As Administrator For This To Work!");
}
}
public string VRChatCacheDir =
(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) +
"\\LocalLow\\VRChat");
private void Form1_Load(object sender, EventArgs e)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
comboBox2.Items.Add(drive.Name);
}
try
{
VRChatCacheDir = GetRealPath(VRChatCacheDir);
}
catch
{
VRChatCacheDir =
(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) +
"\\LocalLow\\VRChat");
}
if (!IsAdministrator())
{
button1.Enabled = false;
button2.Enabled = false;
comboBox2.Enabled = false;
}
}
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetFinalPathNameByHandle([In] SafeFileHandle hFile, [Out] StringBuilder lpszFilePath, [In] int cchFilePath, [In] int dwFlags);
private const int CREATION_DISPOSITION_OPEN_EXISTING = 3;
private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
public static string GetRealPath(string path)
{
if (!Directory.Exists(path) && !File.Exists(path))
{
throw new IOException("Path not found");
}
SafeFileHandle directoryHandle = CreateFile(path, 0, 2, IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero); //Handle file / folder
if (directoryHandle.IsInvalid)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
StringBuilder result = new StringBuilder(512);
int mResult = GetFinalPathNameByHandle(directoryHandle, result, result.Capacity, 0);
if (mResult < 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (result.Length >= 4 && result[0] == '\\' && result[1] == '\\' && result[2] == '?' && result[3] == '\\')
{
return result.ToString().Substring(4); // "\\?\" remove
}
return result.ToString();
}
public static void CopyFolder(string sourceFolder, string destFolder)
{
if (!Directory.Exists(destFolder))
{
Directory.CreateDirectory(destFolder);
}
string[] files = Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string name = Path.GetFileName(file);
string dest = Path.Combine(destFolder, name);
File.Copy(file, dest);
}
string[] folders = Directory.GetDirectories(sourceFolder);
foreach (string folder in folders)
{
string name = Path.GetFileName(folder);
string dest = Path.Combine(destFolder, name);
CopyFolder(folder, dest);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (IsAdministrator() && Directory.Exists(VRChatCacheDir) && comboBox2.Text.Contains(@":\"))
{
Enabled = false;
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c rmdir " + VRChatCacheDir;
process.StartInfo = startInfo;
process.Start();
if (Directory.Exists(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToString().Replace(@"C:\", comboBox2.Text) + "\\LocalLow\\VRChat\\"))
{
CopyFolder(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToString().Replace(@"C:\", comboBox2.Text) + "\\LocalLow\\VRChat\\", VRChatCacheDir);
}
Enabled = true;
}
else if (IsAdministrator() == true && !comboBox2.Text.Contains(@":\"))
{
MessageBox.Show("You Must Select A Drive To Proceed!");
}
else if (IsAdministrator() == true)
{
Enabled = true;
MessageBox.Show("VRChat Cache Does Not Exist, Or Is Not A Redirect Link");
}
else
{
Enabled = true;
MessageBox.Show("This Tool MUST Be Ran As Administrator For This To Work!");
}
}
private void button3_Click(object sender, EventArgs e)
{
foreach (string file in Directory.GetFiles(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "\\LocalLow\\VRChat\\vrchat\\", "output_log_*.txt"))
{
File.Delete(file);
}
ClearVRCCache();
}
private void button4_Click(object sender, EventArgs e)
{
if (Directory.Exists(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "\\LocalLow\\VRChat\\vrchat\\HTTPCache-WindowsPlayer"))
{
Directory.Delete(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "\\LocalLow\\VRChat\\vrchat\\HTTPCache-WindowsPlayer", true);
}
}
}
public static class Extensions
{
public static long AvailableSpaceInMB(this DriveInfo drive)
{
return (drive.AvailableFreeSpace / 1048576);
}
public static long AvailableSpaceInGB(this DriveInfo drive)
{
return (drive.AvailableFreeSpace / 1073741824);
}
public static long GetDirectorySizeInGB(this string Dir)
{
return GetDirectorySize(Dir) / 1073741824;
}
public static long GetDirectorySize(this string Dir)
{
var Info = new DirectoryInfo(Dir);
var startDirectorySize = default(long);
if (Info == null || !Info.Exists)
{
return startDirectorySize; //Return 0 while Directory does not exist.
}
//Add size of files in the Current Directory to main size.
foreach (var fileInfo in Info.GetFiles("*.*", SearchOption.AllDirectories))
{
startDirectorySize += fileInfo.Length;
}
return startDirectorySize; //Return full Size of this Directory.
}
}
}