-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
426 lines (371 loc) · 13.5 KB
/
Program.cs
File metadata and controls
426 lines (371 loc) · 13.5 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
namespace Instant_Cache_Cleaner
{
class Program
{
// This order is important as the client will lock some files and not others
static readonly string[] cacheFiles = new[]{
"HttpCache",
"PixmapCache",
"productInfoCache.db",
"_buddyState.pickle",
"productAuth.pickle",
"localstorage.pickle"
};
static void Main()
{
try
{
Logo();
Console.WriteLine("This will create a backup and then remove cache files from the client within seconds. ");
Console.WriteLine("Please exit the client and confirm the client is not running in the taskbar.");
Console.WriteLine("");
Prompt("Do you want to continue?", () =>
{
var appDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var imvuDir = Path.Combine(appDir, "IMVU");
var backupDir = Path.Combine(imvuDir, "ICC Backups");
Backup(imvuDir, backupDir);
Success();
Console.WriteLine("All finished! You may now re-open the client");
if(Directory.EnumerateFiles(backupDir).Any())
{
Prompt("Would you like to calculate the size of the ICC backups? ETA ~3 seconds to 5 minutes", () =>
{
Delete(backupDir);
});
}
});
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine("");
Console.WriteLine("FAILURE! Program was not able to clear the cache. Report the above text for help");
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
static void Logo()
{
Console.WriteLine(@" ");
Console.WriteLine(@" ");
Console.WriteLine(@" ___ _ _ ");
Console.WriteLine(@" |_ _|_ __ ___| |_ __ _ _ __ | |_ ");
Console.WriteLine(@" | || '_ \/ __| __/ _` | '_ \| __| ");
Console.WriteLine(@" | || | | \__ \ || (_| | | | | |_ ");
Console.WriteLine(@" |___|_| |_|___/\__\__,_|_| |_|\__| ");
Console.WriteLine(@" / ___|__ _ ___| |__ ___ ");
Console.WriteLine(@" | | / _` |/ __| '_ \ / _ \ ");
Console.WriteLine(@" | |__| (_| | (__| | | | __/ ");
Console.WriteLine(@" \____\__,_|\___|_| |_|\___| ");
Console.WriteLine(@" / ___| | ___ __ _ _ __ ___ _ __ ");
Console.WriteLine(@" | | | |/ _ \/ _` | '_ \ / _ \ '__| ");
Console.WriteLine(@" | |___| | __/ (_| | | | | __/ | ");
Console.WriteLine(@" \____|_|\___|\__,_|_| |_|\___|_| ");
Console.WriteLine(@" ");
Console.WriteLine(@" Version 1.0.0.0 ");
Console.WriteLine(@" https://find.vu ");
Console.WriteLine(@" ");
}
static void Success()
{
Console.WriteLine(@" ____ _ _ ____ ____ _____ ____ ____ _ ");
Console.WriteLine(@" / ___|| | | |/ ___/ ___| ____/ ___/ ___| | |");
Console.WriteLine(@" \___ \| | | | | | | | _| \___ \___ \ | |");
Console.WriteLine(@" ___) | |_| | |__| |___| |___ ___) |__) | |_|");
Console.WriteLine(@" |____/ \___/ \____\____|_____|____/____/ (_)");
Console.WriteLine(@" ");
}
static void Delete(string backupDir)
{
Try(() =>
{
Console.WriteLine("Calculating size of backups, please wait...");
var size = DirectoryHelper.GetDirectorySize(new DirectoryInfo(backupDir));
Console.WriteLine("The \"ICC Backups\" folder size is currently " + FormatBytes(size) + ".");
Prompt("Do you want to delete all backups?", () =>
{
Console.WriteLine("Calculating number of total files and time it will take...");
var files = DirectoryHelper.GetFilesFromDir(backupDir).ToList();
const int numTrial = 3000;
var startTime = DateTime.Now;
DirectoryHelper.FastDirectoryDelete(files.Take(numTrial).ToArray(), backupDir);
var filesPerSec = numTrial / (DateTime.Now - startTime).TotalSeconds;
var estimatedMinutes = Math.Round(files.Count() * (1d / filesPerSec) / 60d, MidpointRounding.ToPositiveInfinity);
Console.WriteLine($"Found {files.Count()} files, this should take about ~{estimatedMinutes} minute(s) or less...");
Console.WriteLine("");
Prompt("Do you wish to continue?", () =>
{
startTime = DateTime.Now;
DirectoryHelper.FastDirectoryDelete(files.Skip(numTrial).ToArray(), backupDir);
//Done just to be sure and to remove empty directories
DirectoryHelper.DeleteSubDirectories(backupDir);
Console.WriteLine("");
Console.WriteLine("Total elapsed time was " + (DateTime.Now - startTime).TotalSeconds + " seconds");
Console.WriteLine("All backups have been deleted.");
});
});
}, "There was an issue removing backups, Do you want to try again?");
}
private static string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
}
static void Backup(string imvuDir, string backupDir)
{
Try(() =>
{
if (!Directory.Exists(backupDir))
{
Directory.CreateDirectory(backupDir);
}
var tsStr = DateTimeOffset.Now.ToUnixTimeSeconds() + "_";
foreach (var cacheFile in cacheFiles)
{
var oldPath = $"{imvuDir}\\{cacheFile}";
var newPath = $"{backupDir}\\{tsStr}_{cacheFile}";
if (Directory.Exists(oldPath))
DirectoryHelper.RenameDirectory(oldPath, newPath);
else if (File.Exists(oldPath))
File.Move(oldPath, newPath);
else
newPath = "CACHE NOT FOUND - This is normal if you recently cleaned the cache.";
Console.WriteLine($"{oldPath}");
Console.WriteLine($" => {newPath}");
}
}, "There was an issue with doing the backup, Do you want to try again?");
}
static void Try(Action action, string msg)
{
try
{
action();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (ex is IOException)
{
Console.WriteLine("Exit out of the client if it is currently running !");
}
if (Prompt(msg, action) == ConsoleKey.N)
{
throw;
}
}
}
static ConsoleKey Prompt(string msg, Action action)
{
ConsoleKey response;
do
{
Console.WriteLine(msg + " [y/n]");
response = Console.ReadKey(false).Key;
if (response != ConsoleKey.Enter)
{
Console.WriteLine();
}
} while (response != ConsoleKey.Y && response != ConsoleKey.N);
if (response == ConsoleKey.Y)
{
action();
}
return response;
}
}
//https://stackoverflow.com/questions/24918768/progress-bar-in-console-application
class ConsoleUtility
{
const char _block = '■';
const string _back = "\r";
const string _twirl = "-\\|/";
public static void WriteProgressBar(int percent, bool update = false)
{
if (update)
Console.Write(_back);
Console.Write("[");
var p = (int)((percent / 10f) + .5f);
for (var i = 0; i < 10; ++i)
{
if (i >= p)
Console.Write(' ');
else
Console.Write(_block);
}
Console.Write("] {0,3:##0}%", percent);
}
public static void WriteProgress(int progress, bool update = false)
{
if (update)
Console.Write("\b");
Console.Write(_twirl[progress % _twirl.Length]);
}
}
public class DirectoryHelper
{
//https://stackoverflow.com/questions/44699238/how-to-fast-delete-many-files
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteFile(string lpFileName);
//https://stackoverflow.com/questions/468119/whats-the-best-way-to-calculate-the-size-of-a-directory-in-net
public static long GetDirectorySize(DirectoryInfo directoryInfo, bool recursive = true)
{
var startDirectorySize = default(long);
if (directoryInfo == null || !directoryInfo.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 directoryInfo.GetFiles())
System.Threading.Interlocked.Add(ref startDirectorySize, fileInfo.Length);
if (recursive) //Loop on Sub Direcotries in the Current Directory and Calculate it's files size.
System.Threading.Tasks.Parallel.ForEach(directoryInfo.GetDirectories(), (subDirectory) =>
System.Threading.Interlocked.Add(ref startDirectorySize, GetDirectorySize(subDirectory, recursive)));
return startDirectorySize; //Return full Size of this Directory.
}
//https://stackoverflow.com/questions/1817333/how-do-i-rename-a-folder-directory-in-c
public static bool RenameDirectory(string sourceDirectoryPath, string newDirectoryNameWithNoPath, bool suppressExceptions = false)
{
try
{
DirectoryInfo dirInfo;
try
{
//https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.-ctor?view=net-5.0
dirInfo = new DirectoryInfo(sourceDirectoryPath);
}
catch (ArgumentNullException e)
{
throw new Exception("Source directory path is null.", e);
}
catch (SecurityException e)
{
throw new Exception($"The caller does not have the required permission for Source Directory:{sourceDirectoryPath}.", e);
}
catch (ArgumentException e)
{
//Could reference: https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidpathchars?view=net-5.0
throw new Exception($"Source directory path contains invalid character(s): {sourceDirectoryPath}", e);
}
catch (PathTooLongException e)
{
throw new Exception($"Source directory path is too long. Length={sourceDirectoryPath.Length}", e);
}
string destinationDirectoryPath = dirInfo.Parent == null ? newDirectoryNameWithNoPath : Path.Combine(dirInfo.Parent.FullName, newDirectoryNameWithNoPath);
try
{
//https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.moveto?view=net-5.0
dirInfo.MoveTo(destinationDirectoryPath);
}
catch (ArgumentNullException e)
{
throw new Exception("Destination directory is null.", e);
}
catch (ArgumentException e)
{
throw new Exception("Destination directory must not be empty.", e);
}
catch (SecurityException e)
{
throw new Exception($"The caller does not have the required permission for Directory rename:{destinationDirectoryPath}.", e);
}
catch (PathTooLongException e)
{
throw new Exception($"Rename path is too long. Length={destinationDirectoryPath.Length}", e);
}
catch (IOException e)
{
if (Directory.Exists(destinationDirectoryPath))
{
throw new Exception($"Cannot rename source directory, destination directory already exists: {destinationDirectoryPath}", e);
}
if (string.Equals(sourceDirectoryPath, destinationDirectoryPath, StringComparison.InvariantCultureIgnoreCase))
{
throw new Exception($"Source directory cannot be the same as Destination directory.", e);
}
throw new Exception($"IOException: {e.Message}", e);
}
}
catch (Exception)
{
if (!suppressExceptions)
{
throw;
}
return false;
}
return true;
}
public static System.Collections.Generic.IEnumerable<string> GetFilesFromDir(string dir) =>
Directory.EnumerateFiles(dir).Concat(
Directory.EnumerateDirectories(dir)
.SelectMany(subdir => GetFilesFromDir(subdir)));
public static void FastDirectoryDelete(string[] files, string path)
{
ConsoleUtility.WriteProgress(0, true);
for (var i = 0; i < files.Count(); i++)
{
var progress = 100 * ((double)i / files.Count());
DeleteFile(files[i]);
ConsoleUtility.WriteProgressBar((int)progress, true);
}
ConsoleUtility.WriteProgressBar(100, true);
Console.WriteLine("");
}
public static void DeleteSubDirectories(string parentPath)
{
var dirs = Directory.GetDirectories(parentPath);
foreach (var dir in dirs)
{
DeleteDirectory(dir);
}
}
//http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
public static void DeleteDirectory(string directoryPath)
{
if (!Directory.Exists(directoryPath))
{
Console.WriteLine(
string.Format("Directory '{0}' is missing and can't be removed.",
directoryPath));
return;
}
var files = Directory.GetFiles(directoryPath);
var dirs = Directory.GetDirectories(directoryPath);
foreach (var file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (var dir in dirs)
{
DeleteDirectory(dir);
}
File.SetAttributes(directoryPath, FileAttributes.Normal);
try
{
Directory.Delete(directoryPath, false);
}
catch (IOException)
{
Console.WriteLine(string.Format("{0}The directory '{1}' could not be deleted!" +
"{0}Most of the time, this is due to an external process accessing the files in the temporary repositories created during the test runs, and keeping a handle on the directory, thus preventing the deletion of those files." +
"{0}Known and common causes include:" +
"{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" +
"{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus){0}",
Environment.NewLine, Path.GetFullPath(directoryPath)));
}
}
}
}