Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
190 lines (164 loc) · 6.43 KB
/
Copy pathProgram.cs
File metadata and controls
190 lines (164 loc) · 6.43 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
using AssetsTools.NET;
using AssetsTools.NET.Extra;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Unity3DCompressor
{
internal class Program
{
//Randomizing CAB-strings of game asset bundles can break their dependencies, only use this if you know what you're doing.
private static bool CABRandomization = false;
private static bool Verbose = false;
private static readonly RandomNumberGenerator rng = RandomNumberGenerator.Create();
private static void Main(string[] args)
{
var switches = args.Where(x => x.StartsWith("--")).ToArray();
args = args.Except(switches).ToArray();
if (args.Length == 0)
{
Console.WriteLine($"Drag and drop Unity asset bundles or folders containing asset bundles on to this .exe to compress them.");
Console.WriteLine($"Use '--randomizeCAB' switch to randomize CAB strings of compressed bundles. Do not use on game bundles or other bundles with dependencies.");
Console.WriteLine($"Press any key to exit.");
WaitForKey();
return;
}
foreach (var switchStr in switches)
{
switch (switchStr.ToLowerInvariant())
{
case "--randomizecab":
CABRandomization = true;
break;
case "--verbose":
Verbose = true;
break;
default:
Console.WriteLine("Ignoring unknown switch: " + switchStr);
break;
}
}
foreach (string path in args)
{
if (File.Exists(path))
{
CompressFile(path);
}
else if (Directory.Exists(path))
{
var allfiles = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
foreach (var x in allfiles)
{
CompressFile(x);
}
}
else
{
Console.WriteLine("Skipping invalid path: " + path);
}
}
Console.Write($"Finished compressing asset bundles. Press any key to exit.");
WaitForKey();
}
/// <summary>
/// Wait for a keypress when running in an interactive terminal.
/// Console.ReadKey throws if stdin is redirected (e.g. when launched from a script or file manager), so skip it.
/// </summary>
private static void WaitForKey()
{
if (!Console.IsInputRedirected)
Console.ReadKey();
}
public static bool CompressFile(string file)
{
if (!FileIsAssetBundle(file))
{
Console.WriteLine($"Skipping {file}, not an asset bundle.");
return false;
}
if (CABRandomization)
RandomizeCAB(file);
try
{
Console.WriteLine($"Compressing {file}");
var assetsManager = new AssetsManager();
var bundle = assetsManager.LoadBundleFile(file);
// LZMA bundles are unpacked on load, LZ4 blocks are decoded on the fly,
// so DataReader always exposes the plain serialized data here.
string temp = file + ".temp";
try
{
using (var stream = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.None))
using (var writer = new AssetsFileWriter(stream))
bundle.file.Pack(writer, AssetBundleCompressionType.LZ4);
assetsManager.UnloadAllBundleFiles();
File.Move(temp, file, true);
}
catch
{
if (File.Exists(temp))
File.Delete(temp);
throw;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error compressing, skipping ({ex.Message})");
if (Verbose)
Console.WriteLine(ex);
return false;
}
return true;
}
/// <summary>
/// Check if the file is an asset bundle
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static bool FileIsAssetBundle(string path)
{
if (Path.GetExtension(path) == ".unity3d")
return true;
byte[] buffer = new byte[7];
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
fs.Read(buffer, 0, buffer.Length);
fs.Close();
}
return Encoding.UTF8.GetString(buffer, 0, buffer.Length) == "UnityFS";
}
/// <summary>
/// Open the file, randomize the CAB-string, and save it
/// </summary>
/// <param name="file"></param>
internal static void RandomizeCAB(string file)
{
var assetBundleData = File.ReadAllBytes(file);
var ascii = Encoding.ASCII.GetString(assetBundleData, 0, Math.Min(1024, assetBundleData.Length - 4));
var origCabIndex = ascii.IndexOf("CAB-", StringComparison.Ordinal);
if (origCabIndex < 0)
return;
var origCabLength = ascii.Substring(origCabIndex).IndexOf('\0');
if (origCabLength < 0)
return;
var CAB = GenerateCAB().Substring(4);
var cabBytes = Encoding.ASCII.GetBytes(CAB);
if (origCabLength > 36)
return;
Buffer.BlockCopy(cabBytes, 36 - origCabLength, assetBundleData, origCabIndex + 4, origCabLength - 4);
File.WriteAllBytes(file, assetBundleData);
}
/// <summary>
/// Generate a new CAB-string
/// </summary>
/// <returns></returns>
internal static string GenerateCAB()
{
var rnbuf = new byte[16];
rng.GetBytes(rnbuf);
return "CAB-" + string.Concat(rnbuf.Select((x) => ((int)x).ToString("X2")).ToArray()).ToLower();
}
}
}