-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.cs
278 lines (243 loc) · 12.1 KB
/
Main.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
using AssetRipper.Primitives;
using System.Net;
namespace UDGB
{
public static class Program
{
#pragma warning disable SYSLIB0014 // Type or member is obsolete
internal static WebClient webClient = new WebClient();
#pragma warning restore SYSLIB0014 // Type or member is obsolete
private static string cache_path = null;
private static string temp_folder_path = null;
private static int cooldown_interval = 5; // In Seconds
internal enum OperationModes
{
Normal,
Android_Il2Cpp,
Android_Mono
}
private static OperationModes OperationMode = OperationModes.Normal;
public static int Main(string[] args)
{
webClient.Headers.Add("User-Agent", "Unity web player");
temp_folder_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp");
if (Directory.Exists(temp_folder_path))
Directory.Delete(temp_folder_path, true);
cache_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache.tmp");
if (File.Exists(cache_path))
File.Delete(cache_path);
if ((args.Length < 1) || (args.Length > 2) || string.IsNullOrEmpty(args[0]))
{
Logger.Error("Bad arguments for extractor process; expected arguments: <unityVersion>");
return -1;
}
string requested_version = args[0];
OperationMode = (requested_version.EndsWith(";android")
|| requested_version.EndsWith(";android_il2cpp"))
? OperationModes.Android_Il2Cpp
: (requested_version.EndsWith(";android_mono")
? OperationModes.Android_Mono
: OperationModes.Normal);
if (OperationMode != OperationModes.Normal)
requested_version = requested_version.Substring(0, requested_version.LastIndexOf(";"));
if ((args.Length == 2) && !string.IsNullOrEmpty(args[1]))
{
UnityVersion? version = GetUnityVersionFromString(requested_version);
if (version == null)
{
Logger.Error($"Failed to Find Unity Version [{requested_version}] in List!");
return -1;
}
cache_path = args[1];
try { return ExtractFilesFromArchive(cache_path, version.Value) ? 0 : -1; }
catch (Exception x) { Logger.Error(x.ToString()); return -1; }
}
return (ProcessSpecific(requested_version) ? 0 : -1);
}
private static UnityVersion? GetUnityVersionFromString(string requested_version)
=> UnityVersion.TryParse(requested_version, out UnityVersion returnVal, out _) ? returnVal : null;
private static bool ProcessSpecific(string requested_version)
{
UnityVersion? version = GetUnityVersionFromString(requested_version);
if (version == null)
{
Logger.Error($"Failed to Find Unity Version [{requested_version}] in List!");
return false;
}
return ProcessUnityVersion(version.Value);
}
private static bool VersionFilter(UnityVersion version, bool should_error = true)
{
if ((OperationMode == OperationModes.Android_Il2Cpp)
|| (OperationMode == OperationModes.Android_Mono))
{
if (((version.Major == 5) && (version.Minor <= 2))
|| (version.Major < 5))
{
if (should_error)
Logger.Error($"{version.ToStringWithoutType()} Has No Android Support Installer!");
else
Logger.Warning($"{version.ToStringWithoutType()} Has No Android Support Installer!");
return false;
}
}
return true;
}
private static bool ProcessAll()
{
/*
List<UnityVersion> sortedversiontbl = new List<UnityVersion>();
foreach (UnityVersion version in versions)
{
if (!VersionFilter(version, false))
continue;
string zip_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, (version.ToStringWithoutType() + ".zip"));
if (File.Exists(zip_path))
{
Logger.Warning($"{version.ToStringWithoutType()} Zip Already Exists! Skipping...");
continue;
}
Logger.Msg($"{version.ToStringWithoutType()} Zip Doesn't Exist! Adding to Download List...");
sortedversiontbl.Add(version);
}
int error_count = 0;
if (sortedversiontbl.Count >= 1)
{
int success_count = 0;
foreach (UnityVersion version in sortedversiontbl)
{
if (ProcessUnityVersion(version))
success_count += 1;
else
{
Logger.Warning("Failure Detected! Skipping to Next Version...");
error_count += 1;
}
Logger.Msg($"Cooldown Active for {cooldown_interval} seconds...");
Thread.Sleep(cooldown_interval * 1000);
}
if (error_count > 0)
Logger.Error($"{error_count} Failures");
if (success_count > 0)
Logger.Msg($"{success_count} Successful Zip Creations");
}
return (error_count <= 0);
*/
return false;
}
private static bool ProcessUnityVersion(UnityVersion version)
{
if (!VersionFilter(version))
return false;
string zip_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{version.ToStringWithoutType()}.zip");
if (File.Exists(zip_path))
File.Delete(zip_path);
string[]? downloadurls = UnityArchiveParser.GetDownloads(version, UnityArchiveParser.ePlatform.MACOS) ?? null;
if (downloadurls == null)
{
Logger.Error($"Failed to get Downloads from Unity Version {version.ToStringWithoutType()}");
return false;
}
string downloadUrl = downloadurls.First((x) => x.Contains("UnitySetup-Windows-Mono-Support-for-Editor"));
if ((OperationMode == OperationModes.Android_Il2Cpp)
|| (OperationMode == OperationModes.Android_Mono))
downloadUrl = downloadurls.First((x) => x.Contains("UnitySetup-Android-Support-for-Editor"));
Logger.Msg($"Downloading {downloadUrl}");
bool was_error = false;
try
{
webClient.DownloadFile(downloadUrl, cache_path);
was_error = !ExtractFilesFromArchive(downloadUrl, version);
Thread.Sleep(1000);
if (!was_error)
ArchiveHandler.CreateZip(temp_folder_path, zip_path);
}
catch (Exception x)
{
Logger.Error(x.ToString());
was_error = true;
}
#if !DEBUG
Logger.Msg("Cleaning up...");
if (Directory.Exists(temp_folder_path))
Directory.Delete(temp_folder_path, true);
if (File.Exists(cache_path))
File.Delete(cache_path);
string payload_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Payload~");
if (File.Exists(payload_path))
File.Delete(payload_path);
#endif
if (was_error)
return false;
Logger.Msg($"{version.ToStringWithoutType()} Zip Successfully Created!");
return true;
}
private static bool ExtractFilesFromArchive(string downloadUrl, UnityVersion version)
{
string internal_path = null;
string archive_path = cache_path;
switch (OperationMode)
{
// Unity Dependencies for Unstripping Only
case OperationModes.Normal:
default:
if (version.Major == 3)
internal_path = "Data/PlaybackEngines/windows64standaloneplayer/";
else if (version.Major == 4)
{
if (version.Minor >= 5)
internal_path = "Data/PlaybackEngines/windowsstandalonesupport/Variations/win64_nondevelopment/Data/";
else
internal_path = "Data/PlaybackEngines/windows64standaloneplayer/";
}
else if ((version.Major == 5) && (version.Minor <= 2))
internal_path = "./Unity/Unity.app/Contents/PlaybackEngines/WindowsStandaloneSupport/Variations/win64_nondevelopment_mono/Data/";
else if (version.Major > 2021 || (version.Major == 2021 && version.Minor >= 2))
internal_path = "./Variations/win64_player_nondevelopment_mono/Data/";
else
internal_path = "./Variations/win64_nondevelopment_mono/Data/";
if ((version.Major < 5)
|| ((version.Major == 5) && (version.Minor < 3))
|| downloadUrl.EndsWith(".pkg"))
{
Logger.Msg($"Extracting Payload...");
if (!ArchiveHandler.ExtractFiles(AppDomain.CurrentDomain.BaseDirectory, archive_path, "Payload~"))
return false;
archive_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Payload~");
}
Logger.Msg("Extracting DLLs from Archive...");
return ArchiveHandler.ExtractFiles(temp_folder_path, archive_path, internal_path + "Managed/*.dll");
// Full Android Libraries
case OperationModes.Android_Il2Cpp:
case OperationModes.Android_Mono:
string rootpath = "$INSTDIR$*";
string basefolder = $"{rootpath}/Variations/{((OperationMode == OperationModes.Android_Il2Cpp) ? "il2cpp" : "mono")}/";
string libfilename = "libunity.so";
Logger.Msg($"Extracting {libfilename} from Archive...");
if (!ArchiveHandler.ExtractFiles(temp_folder_path, archive_path, $"{basefolder}Release/Libs/*/{libfilename}", true))
return false;
Logger.Msg("Fixing Folder Structure...");
string libsfolderpath = Path.Combine(temp_folder_path, "Libs");
if (!Directory.Exists(libsfolderpath))
Directory.CreateDirectory(libsfolderpath);
foreach (string filepath in Directory.GetFiles(temp_folder_path, libfilename, SearchOption.AllDirectories))
{
Logger.Msg($"Moving {filepath}");
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(filepath));
string newpath = Path.Combine(libsfolderpath, dir.Name);
if (!Directory.Exists(newpath))
Directory.CreateDirectory(newpath);
File.Move(filepath, Path.Combine(newpath, Path.GetFileName(filepath)));
}
string rootfolder = Directory.GetDirectories(temp_folder_path, rootpath).First();
Logger.Msg($"Removing {rootfolder}");
Directory.Delete(rootfolder, true);
Logger.Msg($"Extracting Managed Folder...");
string newmanagedfolder = Path.Combine(temp_folder_path, "Managed");
if (!Directory.Exists(newmanagedfolder))
Directory.CreateDirectory(newmanagedfolder);
return ArchiveHandler.ExtractFiles(newmanagedfolder, archive_path, basefolder + "Managed/*.dll");
}
}
}
}