Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: starburst997/Unity.Trimmer
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: StromKuo/Unity.Trimmer
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Jul 25, 2023

  1. Copy the full SHA
    233e26a View commit details
  2. Copy the full SHA
    2b1895b View commit details
Showing with 44 additions and 41 deletions.
  1. +1 −1 .gitmodules
  2. +42 −39 CLI.Unity.Trimmer/Source/Commands/Trim.cs
  3. +1 −1 Dependencies/AssetsTools.NET
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "Dependencies/AssetsTools.NET"]
path = Dependencies/AssetsTools.NET
url = git@github.com:starburst997/AssetsTools.NET.git
url = https://github.com/nesrak1/AssetsTools.NET.git
81 changes: 42 additions & 39 deletions CLI.Unity.Trimmer/Source/Commands/Trim.cs
Original file line number Diff line number Diff line change
@@ -10,57 +10,60 @@ public static class Trim
{
public static void Execute(string input, string output, string classdata, string font)
{
var manager = new AssetsManager();
var manager = new AssetsManager();
var asset = manager.LoadAssetsFile(input, false);

if (!string.IsNullOrEmpty(classdata))
manager.LoadClassPackage(classdata); // TODO: Is this necessary?

manager.LoadClassDatabaseFromPackage(asset.file.typeTree.unityVersion);

Console.WriteLine($"Asset found: {asset.name} ({asset.file.typeTree.unityVersion})");

byte[] fontBytes = null;
if (!string.IsNullOrEmpty(font))
fontBytes = File.ReadAllBytes(font);

var replacers = new List<AssetsReplacer>();
var empty = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // 2x2 32bit

// Loop all assets to find all Texture2D and replace them with 2x2 empty one
// TODO: Still left with ~350kb of crap, seems like there is a compute shader worth 200kb, not sure if worth trying to slim the rest, when compressed with brotli we're left with less than 66kb which is a huge gain compared to before
foreach (var info in asset.table.assetFileInfo)
foreach (var info in asset.table.GetAssetsOfType((int)AssetClassID.Texture2D))
{
var baseField = manager.GetTypeInstance(asset, info).GetBaseField();
var name = baseField.Get("m_Name").GetValue().AsString();

Console.WriteLine($"Found asset: {name} / {(AssetClassID) info.curFileType}");

// We've got a `Texture2D` so replace it
if (info.curFileType == (int) AssetClassID.Texture2D)
{
var texture = TextureFile.ReadTextureFile(baseField);
if (texture.m_Width < 2 && texture.m_Height < 2) continue;

Console.WriteLine($"{texture.m_Width} / {texture.m_Height} / {(TextureFormat) texture.m_TextureFormat}");

// Create the new texture
texture.m_TextureFormat = (int) TextureFormat.RGBA32;
var data = TextureFile.Encode(empty, (TextureFormat) texture.m_TextureFormat, 2, 2);

texture.SetTextureDataRaw(data, 2, 2);
texture.WriteTo(baseField);

var bytes = baseField.WriteToByteArray();
var replacer = new AssetsReplacerFromMemory(0, info.index, (int) info.curFileType, 0xffff, bytes);
replacers.Add(replacer);

Console.WriteLine($"*** Texture replaced!");
}

// Also replace "Arial" font since we don't need it
if (fontBytes != null && info.curFileType == (int) AssetClassID.Font)
Console.WriteLine($"Found asset: {name} / {(AssetClassID)info.curFileType}");

var texture = TextureFile.ReadTextureFile(baseField);
if (texture.m_Width < 2 && texture.m_Height < 2) continue;

Console.WriteLine($"{texture.m_Width} / {texture.m_Height} / {(TextureFormat)texture.m_TextureFormat}");

// Create the new texture
texture.m_TextureFormat = (int)TextureFormat.RGBA32;
var data = TextureFile.Encode(empty, (TextureFormat)texture.m_TextureFormat, 2, 2);

texture.SetTextureDataRaw(data, 2, 2);
texture.WriteTo(baseField);

var bytes = baseField.WriteToByteArray();
var replacer = new AssetsReplacerFromMemory(0, info.index, (int)info.curFileType, 0xffff, bytes);
replacers.Add(replacer);

Console.WriteLine($"*** Texture replaced!");
}

if (fontBytes != null)
{
foreach (var info in asset.table.GetAssetsOfType((int)AssetClassID.Font))
{
var baseField = manager.GetTypeInstance(asset, info).GetBaseField();
var name = baseField.Get("m_Name").GetValue().AsString();

Console.WriteLine($"Found asset: {name} / {(AssetClassID)info.curFileType}");

Console.WriteLine($"Found Font!");
foreach (var child in baseField.children)
{
@@ -82,9 +85,9 @@ public static void Execute(string input, string output, string classdata, string
{
bytes[i] = (byte) child2[i].GetValue().AsInt();
}
File.WriteAllBytes($"{font}.copy", bytes);*/

// Overwrite data
child2.GetValue().Set(new AssetTypeArray(fontBytes.Length));

@@ -97,27 +100,27 @@ public static void Execute(string input, string output, string classdata, string
}

child2.SetChildrenList(children);

// Replace
var bytes = baseField.WriteToByteArray();
var replacer = new AssetsReplacerFromMemory(0, info.index, (int) info.curFileType, 0xffff, bytes);
var replacer = new AssetsReplacerFromMemory(0, info.index, (int)info.curFileType, 0xffff, bytes);
replacers.Add(replacer);
}
}
}
}
}
}

Console.WriteLine($"Writing output to: \"{output}\"");

File.Delete(output);
using (var stream = File.OpenWrite(output))
using (var writer = new AssetsFileWriter(stream))
asset.file.Write(writer, 0, replacers, 0);

Console.WriteLine($"Done!");

manager.UnloadAllAssetsFiles();
}
}
2 changes: 1 addition & 1 deletion Dependencies/AssetsTools.NET
Submodule AssetsTools.NET updated 30 files
+118 −0 .github/workflows/dotnet.yml
+2 −2 AssetTools.NET/AssetsTools.NET.nuspec
+8 −3 AssetTools.NET/Extra/AssetsManager/AssetsFileInstance.cs
+49 −2 AssetTools.NET/Extra/AssetsManager/AssetsManager.cs
+2 −3 AssetTools.NET/Extra/AssetsManager/BundleFileInstance.cs
+1 −1 AssetTools.NET/Extra/CldbTypeTreeConverters/C2T5.cs
+2 −2 AssetTools.NET/Extra/Decompressors/LZ4/Safe/LZ4Codec.Safe32HC.Dirty.cs
+1 −1 AssetTools.NET/Extra/Decompressors/LZ4/Safe/LZ4Codec.Safe64HC.Dirty.cs
+144 −70 AssetTools.NET/Extra/MonoDeserializer/MonoDeserializer.cs
+75 −0 AssetTools.NET/Extra/MonoDeserializer/TypeDefWithSelfRef.cs
+2 −2 AssetTools.NET/Extra/TextureDecoders/BC7Decoder.cs
+61 −0 AssetTools.NET/Extra/UnityVersion.cs
+1 −1 AssetTools.NET/Extra/ValueBuilder.cs
+2 −5 AssetTools.NET/Standard/AssetTypeClass/AssetTypeInstance.cs
+3 −1 AssetTools.NET/Standard/AssetTypeClass/AssetTypeValueField.cs
+0 −14 AssetTools.NET/Standard/AssetsBundleFileFormat/AssetsBundleEntry.cs
+174 −216 AssetTools.NET/Standard/AssetsBundleFileFormat/AssetsBundleFile.cs
+5 −74 AssetTools.NET/Standard/AssetsBundleFileFormat/AssetsBundleHeader03.cs
+3 −1 AssetTools.NET/Standard/AssetsBundleFileFormat/AssetsBundleHeader06.cs
+0 −21 AssetTools.NET/Standard/AssetsBundleFileFormat/AssetsBundleOffsetPair.cs
+3 −26 AssetTools.NET/Standard/AssetsBundleFileFormat/AssetsList.cs
+2 −2 AssetTools.NET/Standard/AssetsFileFormat/AssetsFile.cs
+1 −1 AssetTools.NET/Standard/AssetsFileFormat/Type_0D.cs
+1 −1 AssetTools.NET/Standard/AssetsReplacer/AssetsReplacerFromStream.cs
+1 −1 AssetTools.NET/Standard/BundleReplacer/BundleReplacerFromStream.cs
+ AssetTools.NET/icon.png
+9 −2 AssetsView/Winforms/GameObjectViewer.cs
+1 −1 AssetsView/Winforms/StartScreen.cs
+6 −2 AssetsView/Winforms/TextureViewer.cs
+5 −9 README.md