Created
November 28, 2020 04:26
-
-
Save TellowKrinkle/9188559caf704f400d63db8740898a77 to your computer and use it in GitHub Desktop.
Extractor for unity3d files from Higurashi Mei
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used ghidra with Il2CppDumper
It was in
Sxam.Setting_TypeInfo->static_fields->DecodeKey
, which was initialized inSxam.Setting$$ctor
and passed toSxam.Crypto$$Decode