From 47c85b8caa97669a5ed6f8fde0610230f8f8e238 Mon Sep 17 00:00:00 2001 From: Sunset <26019675+lwd-temp@users.noreply.github.com> Date: Thu, 12 Jan 2023 20:22:00 +0800 Subject: [PATCH] Rewrite functions --- py/getModelsGlobal.py | 77 ++++++++++++++++++++++++------------------- py/getModelsJapan.py | 77 ++++++++++++++++++++++++------------------- requirements.txt | 1 - 3 files changed, 86 insertions(+), 69 deletions(-) diff --git a/py/getModelsGlobal.py b/py/getModelsGlobal.py index 29dda9a0..7cb92d83 100644 --- a/py/getModelsGlobal.py +++ b/py/getModelsGlobal.py @@ -1,9 +1,7 @@ import os -from io import BytesIO import requests -import unitypack -from PIL import ImageOps +import UnityPy # conf option = { @@ -119,45 +117,56 @@ def downloadFile(url, fname): def extractTextAsset(object, dest): + # parse the object data data = object.read() - if (type(data.script) == bytes): - with open(f"{dest}/{data.name}", "wb") as f: - f.write(data.script) - elif (type(data.script) == str): - with open(f"{dest}/{data.name}", "wb") as f: - f.write(bytes(str(data.script), 'utf-8')) - else: - raise Exception("Not handled") + + # create destination path + dest = os.path.join(dest, data.name) + + # touch folder + os.makedirs(os.path.dirname(dest), exist_ok=True) + + # just save + with open(dest, "wb") as f: + f.write(data.script) def extractTexture2D(object, dest): + # parse the object data data = object.read() - img = ImageOps.flip(data.image) - output = BytesIO() - img.save(output, format="png") - with open(f"{dest}/{data.name}.png", "wb") as f: - f.write(output.getvalue()) + + # create destination path + dest = os.path.join(dest, data.name) + + # touch folder + os.makedirs(os.path.dirname(dest), exist_ok=True) + + # make sure that the extension is correct + # you probably only want to do so with images/textures + dest, ext = os.path.splitext(dest) + dest = dest + ".png" + + img = data.image + img.save(dest) def extractCharacter(src, dest): - with open(src, "rb") as f: - bundle = unitypack.load(f) - for asset in bundle.assets: - # print("%s: %s:: %i objects" % (bundle, asset, len(asset.objects))) - for id, object in asset.objects.items(): - # print(id, object) - # extract skel & atlas - if object.type == "TextAsset": - data = object.read() - if ".atlas" in data.name or ".skel" in data.name: - print(data.name) - extractTextAsset(object, dest) - # extract texture - elif object.type == "Texture2D": - data = object.read() - - print(data.name + ".png") - extractTexture2D(object, dest) + # load the bundle + bundle = UnityPy.load(src) + + for obj in bundle.objects: + # extract skel & atlas + if obj.type.name == "TextAsset": + data = obj.read() + if ".atlas" in data.name or ".skel" in data.name: + print(data.name) + extractTextAsset(obj, dest) + # extract texture + elif obj.type.name == "Texture2D": + data = obj.read() + + print(data.name + ".png") + extractTexture2D(obj, dest) if __name__ == "__main__": diff --git a/py/getModelsJapan.py b/py/getModelsJapan.py index c80f5455..1a424ee2 100644 --- a/py/getModelsJapan.py +++ b/py/getModelsJapan.py @@ -1,9 +1,7 @@ import os -from io import BytesIO import requests -import unitypack -from PIL import ImageOps +import UnityPy # conf option = { @@ -59,45 +57,56 @@ def downloadFile(url, fname): def extractTextAsset(object, dest): + # parse the object data data = object.read() - if (type(data.script) == bytes): - with open(f"{dest}/{data.name}", "wb") as f: - f.write(data.script) - elif (type(data.script) == str): - with open(f"{dest}/{data.name}", "wb") as f: - f.write(bytes(str(data.script), 'utf-8')) - else: - raise Exception("Not handled") + + # create destination path + dest = os.path.join(dest, data.name) + + # touch folder + os.makedirs(os.path.dirname(dest), exist_ok=True) + + # just save + with open(dest, "wb") as f: + f.write(data.script) def extractTexture2D(object, dest): + # parse the object data data = object.read() - img = ImageOps.flip(data.image) - output = BytesIO() - img.save(output, format="png") - with open(f"{dest}/{data.name}.png", "wb") as f: - f.write(output.getvalue()) + + # create destination path + dest = os.path.join(dest, data.name) + + # touch folder + os.makedirs(os.path.dirname(dest), exist_ok=True) + + # make sure that the extension is correct + # you probably only want to do so with images/textures + dest, ext = os.path.splitext(dest) + dest = dest + ".png" + + img = data.image + img.save(dest) def extractCharacter(src, dest): - with open(src, "rb") as f: - bundle = unitypack.load(f) - for asset in bundle.assets: - # print("%s: %s:: %i objects" % (bundle, asset, len(asset.objects))) - for id, object in asset.objects.items(): - # print(id, object) - # extract skel & atlas - if object.type == "TextAsset": - data = object.read() - if ".atlas" in data.name or ".skel" in data.name: - print(data.name) - extractTextAsset(object, dest) - # extract texture - elif object.type == "Texture2D": - data = object.read() - - print(data.name + ".png") - extractTexture2D(object, dest) + # load the bundle + bundle = UnityPy.load(src) + + for obj in bundle.objects: + # extract skel & atlas + if obj.type.name == "TextAsset": + data = obj.read() + if ".atlas" in data.name or ".skel" in data.name: + print(data.name) + extractTextAsset(obj, dest) + # extract texture + elif obj.type.name == "Texture2D": + data = obj.read() + + print(data.name + ".png") + extractTexture2D(obj, dest) if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index 5ae2922a..66dd3ee4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -Pillow==9.4.0 requests==2.28.1 UnityPy==1.9.24 \ No newline at end of file