Skip to content

Instantly share code, notes, and snippets.

@TellowKrinkle
Created November 28, 2020 04:26
Show Gist options
  • Save TellowKrinkle/9188559caf704f400d63db8740898a77 to your computer and use it in GitHub Desktop.
Save TellowKrinkle/9188559caf704f400d63db8740898a77 to your computer and use it in GitHub Desktop.
Extractor for unity3d files from Higurashi Mei
import unitypack
import unitypack.utils
import collections
import sys
import os
from unitypack.engine.texture import TextureFormat
from PIL import ImageOps
ASTC_MAP = {
TextureFormat.ASTC_RGB_4x4: (0x93B0, 0x1907),
TextureFormat.ASTC_RGB_5x5: (0x93B2, 0x1907),
TextureFormat.ASTC_RGB_6x6: (0x93B4, 0x1907),
TextureFormat.ASTC_RGB_8x8: (0x93B7, 0x1907),
TextureFormat.ASTC_RGB_10x10: (0x93BB, 0x1907),
TextureFormat.ASTC_RGB_12x12: (0x93BD, 0x1907),
TextureFormat.ASTC_RGBA_4x4: (0x93B0, 0x1908),
TextureFormat.ASTC_RGBA_5x5: (0x93B2, 0x1908),
TextureFormat.ASTC_RGBA_6x6: (0x93B4, 0x1908),
TextureFormat.ASTC_RGBA_8x8: (0x93B7, 0x1908),
TextureFormat.ASTC_RGBA_10x10: (0x93BB, 0x1908),
TextureFormat.ASTC_RGBA_12x12: (0x93BD, 0x1908),
TextureFormat.ETC2_RGBA8: (0x9278, 0x1908)
}
PVRTC_MAP = {
TextureFormat.PVRTC_RGB2: (0xC, 0x10000, 2),
TextureFormat.PVRTC_RGBA2: (0x18, 0x18000, 2),
TextureFormat.PVRTC_RGB4: (0xD, 0x10000, 4),
TextureFormat.PVRTC_RGBA4: (0x19, 0x18000, 4)
}
def processTex2D(info, outpath):
if info.format in ASTC_MAP:
with open(outpath + ".ktx", "wb") as outImage:
outImage.write(bytes([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) # identifier
outImage.write(0x04030201.to_bytes(4, byteorder="little")) # Endianness
outImage.write(b"\0" * 12) # glType, glTypeSize, glFormat
outImage.write(ASTC_MAP[info.format][0].to_bytes(4, byteorder="little")) # glInternalFormat
outImage.write(ASTC_MAP[info.format][1].to_bytes(4, byteorder="little")) # glBaseInternalFormat
outImage.write(info.width.to_bytes(4, byteorder="little")) # pixelWidth
outImage.write(info.height.to_bytes(4, byteorder="little")) # pixelHeight
outImage.write(b"\0" * 8) # pixelDepth, numberOfArrayElements
outImage.write(0x1.to_bytes(4, byteorder="little")) # numberOfFaces
outImage.write(0x1.to_bytes(4, byteorder="little")) # numberOfMipmapLevels
keyAndValue = b"KTXorientation\0S=r,T=u\0" # flip upside down
byteCount = len(keyAndValue) + (3 - ((len(keyAndValue) + 3) % 4))
outImage.write((4 + byteCount).to_bytes(4, byteorder="little")) # bytesOfKeyValueData
outImage.write(len(keyAndValue).to_bytes(4, byteorder="little")) # keyAndValueByteSize
outImage.write(keyAndValue + b"\0" * (byteCount - len(keyAndValue))) # keyAndValue, padding
outImage.write((info.width * info.height).to_bytes(4, byteorder="little")) # imageSize
outImage.write(info.image_data) # image data
return
if info.format in PVRTC_MAP:
with open(outpath + ".pvr", "wb") as outImage:
# outImage.write(0x03525650.to_bytes(4, byteorder="little")) # Version
# outImage.write(b"\0" * 4) # Flags
# outImage.write(PVRTC_MAP[info.format].to_bytes(8, byteorder="little")) # Pixel Format
# outImage.write(0x1.to_bytes(4, byteorder="little")) # Colour Space (sRGB)
# outImage.write(b"\0" * 4) # Channel Type (Unsigned Byte Normalized)
# outImage.write(info.height.to_bytes(4, byteorder="little")) # Height
# outImage.write(info.width.to_bytes(4, byteorder="little")) # Width
# outImage.write(0x1.to_bytes(4, byteorder="little")) # Depth
# outImage.write(0x1.to_bytes(4, byteorder="little")) # Num Surfaces
# outImage.write(0x1.to_bytes(4, byteorder="little")) # Num Faces
# outImage.write(0x1.to_bytes(4, byteorder="little")) # Mipmap Count
# outImage.write(b"\0" * 4) # Metadata Size
# outImage.write(info.image_data) # image data
outImage.write((52).to_bytes(4, byteorder="little")) # Header Size
outImage.write(info.height.to_bytes(4, byteorder="little")) # Height
outImage.write(info.width.to_bytes(4, byteorder="little")) # Width
outImage.write(0x1.to_bytes(4, byteorder="little")) # Mipmap Count
outImage.write(PVRTC_MAP[info.format][0].to_bytes(1, byteorder="little")) # Pixel Format
outImage.write(PVRTC_MAP[info.format][1].to_bytes(3, byteorder="big")) # Flags
outImage.write(len(info.image_data).to_bytes(4, byteorder="little")) # Surface Size
outImage.write(PVRTC_MAP[info.format][2].to_bytes(4, byteorder="little")) # Bits per Pixel
outImage.write(b"\xff\0\0\0") # Red Mask
outImage.write(b"\0\xff\0\0") # Green Mask
outImage.write(b"\0\0\xff\0") # Blue Mask
outImage.write(b"\0\0\0\xff") # Alpha Mask
outImage.write(b"PVR!") # PVR Identifier
outImage.write(0x1.to_bytes(4, byteorder="little")) # Number of Surfaces
outImage.write(info.image_data) # image data
return
try:
image = info.image
except NotImplementedError as error:
print("Failed to decode " + outpath + " with unsupported format " + str(info.format))
return
if not image:
return
img = ImageOps.flip(image)
with open(outpath + ".png", "wb") as outImage:
img.save(outImage, format="png")
def extract_asset(path, asset, print_ignored = True):
if asset.type in ("AssetBundle", "MonoScript", "AnimationClip", "GameObject", "Transform", "Mesh", "Sprite", "Avatar", "CharacterImasMotionAsset"):
return False
asset = asset.read()
outpath = os.path.join(sys.argv[1], path)
if not os.path.exists(os.path.dirname(outpath)):
os.makedirs(os.path.dirname(outpath))
if not os.path.splitext(outpath)[1]:
outpath += ".bin"
if isinstance(asset, unitypack.engine.Texture2D):
processTex2D(asset, os.path.splitext(outpath)[0])
elif isinstance(asset, unitypack.engine.TextAsset):
with open(outpath, "wb") as outfile:
if isinstance(asset.bytes, str):
outfile.write(asset.bytes.encode("utf-8"))
else:
outfile.write(asset.bytes)
elif isinstance(asset, unitypack.engine.AudioClip):
samples = unitypack.utils.extract_audioclip_samples(asset)
for name, sample in samples.items():
with open(os.path.join(os.path.dirname(outpath), name), "wb") as outfile:
outfile.write(sample)
else:
if print_ignored:
print(infile)
print(type(asset))
print(entry[1]["asset"].object.type)
return False
return True
for infile in sys.argv[2:]:
with open(infile, "rb") as file:
bundle = unitypack.load(file)
extracted = False
path = ""
for e in bundle.assets[0].objects.values():
if e.type != "AssetBundle": continue
for entry in e.read()["m_Container"]:
path = entry[0]
if path[-6:] == ".bytes":
path = path[:-6]
if entry[1]["asset"] is None:
continue
target = entry[1]["asset"]
if extract_asset(path, target.object):
extracted = True
if not extracted and path:
# We don't extract 3D models, but try to extract their textures
i = 0
for item in bundle.assets[0].objects.values():
if item.type == "Texture2D" and item.read().name:
name = item.read().name
if os.path.basename(os.path.splitext(path)[0]).lower() == name.lower():
name = os.path.join(os.path.dirname(path), name)
else:
name = os.path.splitext(path)[0] + "_" + name
extract_asset(name, item, print_ignored=False)
elif item.type == "TextAsset" and item.read().name:
name = os.path.splitext(path)[0] + "_" + item.read().name + ".txt"
extract_asset(name, item)
elif extract_asset(os.path.splitext(path)[0] + "_" + str(i), item, print_ignored=False):
i += 1
@StrayUwasa
Copy link

Also is there a way to change where the decompression goes? I want it in the D Drive however it seems like my computer is using the C drive to decrypt it. This is what I have been getting

Traceback (most recent call last):
File "D:\Art\Apps\Higurashi Mei\Higurashi Mei Extractor\9188559caf704f400d63db8740898a77-332733ee7d856609781fe86dedec6b886cca9802\HiguMeiExtractor.py", line 132, in
bundle = unitypack.load(file)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\unitypack_init_.py", line 12, in load
return env.load(file)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\unitypack\environment.py", line 27, in load
ret.load(file)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\unitypack\assetbundle.py", line 49, in load
self.load_unityfs(buf)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\unitypack\assetbundle.py", line 106, in load_unityfs
data = self.read_compressed_data(buf, compression)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\unitypack\assetbundle.py", line 92, in read_compressed_data
return lz4_decompress(data, self.uiblock_size)
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\unitypack\utils.py", line 11, in lz4_decompress
return decompress(data, size)
_block.LZ4BlockError: Decompression failed: corrupt input or insufficient space in destination buffer. Error code: 16

@TellowKrinkle
Copy link
Author

is input1.unity3d and input2.unity3d placeholders? Or do I just copy what you have here and put that into the command prompt and hit enter? And so I need to do anything to the apk of the game such as unzip it?

All of those are placeholders. output-directory is the place it will output, so replace that with D:\whateverblahblah

Looking back on the discord history from this, it looks like the unity3d files were a part of an encrypted archive. If you join the 07th-mod discord then this link should get you to the chat history from that.

Otherwise, the decryption tool is here: https://github.com/TellowKrinkle/SxamTools. If you currently have a list.bin, data.bin, and hash.bin, this is the tool to use. They may have changed the encryption key since then, since that was two years ago. If so, it should fail with a hash failure, and you'll have to get the new key by decompiling the game. If you don't want to set up a swift compiler, someone also made a C++ version: https://gist.github.com/viliml/e70ee7c9b14c7591ee3bd14809e2851c

If you currently have the game apk, that's not enough unfortunately. The game downloads its assets after completing the prologue, so the apk won't have them. I wasn't the one who got the original asset file, but I'd guess it involved using an Android file manager to grab the file out of the application's resources after running it. Might require a rooted phone, idk.

@Nakasu-ksm
Copy link

is input1.unity3d and input2.unity3d placeholders? Or do I just copy what you have here and put that into the command prompt and hit enter? And so I need to do anything to the apk of the game such as unzip it?

All of those are placeholders. output-directory is the place it will output, so replace that with D:\whateverblahblah

Looking back on the discord history from this, it looks like the unity3d files were a part of an encrypted archive. If you join the 07th-mod discord then this link should get you to the chat history from that.

Otherwise, the decryption tool is here: https://github.com/TellowKrinkle/SxamTools. If you currently have a list.bin, data.bin, and hash.bin, this is the tool to use. They may have changed the encryption key since then, since that was two years ago. If so, it should fail with a hash failure, and you'll have to get the new key by decompiling the game. If you don't want to set up a swift compiler, someone also made a C++ version: https://gist.github.com/viliml/e70ee7c9b14c7591ee3bd14809e2851c

If you currently have the game apk, that's not enough unfortunately. The game downloads its assets after completing the prologue, so the apk won't have them. I wasn't the one who got the original asset file, but I'd guess it involved using an Android file manager to grab the file out of the application's resources after running it. Might require a rooted phone, idk.

Do you know how to get the encryption key by decompiling , using which tools thank you!

@TellowKrinkle
Copy link
Author

I used ghidra with Il2CppDumper

It was in Sxam.Setting_TypeInfo->static_fields->DecodeKey, which was initialized in Sxam.Setting$$ctor and passed to Sxam.Crypto$$Decode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment