-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (166 loc) · 7.34 KB
/
Program.cs
File metadata and controls
200 lines (166 loc) · 7.34 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
using SixLabors.ImageSharp;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace LibraryCacheReader
{
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Attempting to find Library file...");
Guid localLowId = new Guid("A520A1A4-1780-4FF6-BD18-167343C5AF16");
string path = GetKnownFolderPath(localLowId) + "\\Against Gravity\\Rec Room\\Library";
while(!File.Exists(path))
{
Console.WriteLine("Unable to find your Library file! Please drag the file onto this window and press Enter:");
string? newPath = Console.ReadLine();
if (string.IsNullOrEmpty(newPath))
{
Console.WriteLine("No path provided!");
continue;
}
if(newPath.StartsWith("\"") && newPath.EndsWith("\""))
{
newPath = newPath.Substring(1, newPath.Length - 2);
}
path = newPath;
}
Console.WriteLine($"Found Library file at: {path}\n\n");
string text = Encoding.ASCII.GetString(File.ReadAllBytes(path));
Regex urlRegex = new Regex(@"https?://[^\s""'?#]+\.(?:png|jpe?g)\b", RegexOptions.IgnoreCase);
MatchCollection urlMatches = urlRegex.Matches(text);
HashSet<string> seenData = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
List<string> links = new List<string>();
SemaphoreSlim downloadSlim = new SemaphoreSlim(2);
Random random = new Random();
List<Task> downloadTasks = new List<Task>();
string downloadDirectory = "DownloadedImages";
Directory.CreateDirectory(downloadDirectory);
string renderDirectory = Path.Combine(downloadDirectory, "Renders");
Directory.CreateDirectory(renderDirectory);
HttpClient client = new HttpClient();
for (int i = 0; i < urlMatches.Count; i++)
{
Match match = urlMatches[i];
string url = match.Value;
if (!seenData.Add(url))
continue;
links.Add(url);
}
// Save all links to a txt file
using(StreamWriter writer = new StreamWriter("output.txt"))
{
var groups = links
.GroupBy(x => Path.GetExtension(new Uri(x).AbsolutePath).ToLowerInvariant())
.OrderBy(y => y.Key);
foreach (var extension in groups)
{
writer.WriteLine($"Extension: {extension.Key}");
foreach(var url in extension)
{
writer.WriteLine($"{url}");
}
writer.WriteLine();
}
}
// Download all images
foreach(string url in links)
{
downloadTasks.Add(Task.Run(async () =>
{
await downloadSlim.WaitAsync();
try
{
string filename = Path.GetFileName(new Uri(url).AbsolutePath);
string downloadPath = Path.Combine(downloadDirectory, filename);
string? file = Directory.EnumerateFiles(downloadDirectory, filename, SearchOption.AllDirectories).FirstOrDefault();
if (file != null)
{
Console.WriteLine($"Image already downloaded! Skipping: {filename}");
return;
}
int attempts = 0;
bool success = false;
while(attempts < 3 && !success)
{
try
{
using HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
byte[] data = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync(downloadPath, data);
Console.WriteLine($"Downloaded: {url}");
success = true;
}
catch (Exception ex)
{
attempts++;
if (attempts < 3)
{
Console.WriteLine($"Retry {attempts} for: {url}");
await Task.Delay(1000);
}
else
{
Console.WriteLine($"Failed to download: {url}");
}
}
}
await Task.Delay(random.Next(500, 1000));
}
finally
{
downloadSlim.Release();
}
}));
}
await Task.WhenAll(downloadTasks);
Console.WriteLine("Downloaded all images! Moving...\n");
// Moves all 2k images to a separate folder as these are usually renders.
foreach (string filePath in Directory.GetFiles(downloadDirectory, "*.png"))
{
try
{
ImageInfo image = Image.Identify(filePath);
if (image == null)
continue;
if(image.Width == 2048 && image.Height == 2048)
{
string destinationPath = Path.Combine(renderDirectory, Path.GetFileName(filePath));
if (!File.Exists(destinationPath))
{
File.Move(filePath, destinationPath);
Console.WriteLine($"Moved {Path.GetFileName(filePath)} to Renders folder.");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing {filePath}: {ex.Message}");
}
}
Console.WriteLine("\nFinished downloading/moving everything! You can close this now.");
Console.ReadLine();
}
static string GetKnownFolderPath(Guid knownFolderId)
{
IntPtr pszPath = IntPtr.Zero;
try
{
int hr = SHGetKnownFolderPath(knownFolderId, 0, IntPtr.Zero, out pszPath);
if (hr >= 0)
return Marshal.PtrToStringAuto(pszPath);
throw Marshal.GetExceptionForHR(hr);
}
finally
{
if (pszPath != IntPtr.Zero)
Marshal.FreeCoTaskMem(pszPath);
}
}
[DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
}
}