K0lb3 / UnityPy

UnityPy is python module that makes it possible to extract/unpack and edit Unity assets
MIT License
820 stars 123 forks source link

Expected bytes error on monobehaviours with dumped typetree #105

Open lugia19 opened 2 years ago

lugia19 commented 2 years ago

This is an issue I previously reported on discord but I figured I might as well get a proper bug report in - it's still an issue as of 1.8.9. The typetree was generated from dummy DLLs, I've tried both Cpp2IL and Il2CppDumper, the typetree was identical.

Code

fp = r"level1_jp"
trees = json.load(open("typetrees.json", "r"))
env = UnityPy.load(fp)
for obj in env.objects:
    if obj.type.name == "MonoBehaviour":
        mb = obj.read()
        if not mb.m_Script:
            continue
        script = mb.m_Script.read()
        try:
            typetree = trees[script.m_AssemblyName][script.m_ClassName]
        except:
            continue
        try:
            res = obj.read_typetree(typetree)
        except Exception as e:
            print(e, obj.path_id, script.m_AssemblyName, script.m_ClassName)

Error

Error while read type, read 496 bytes but expected 500 bytes 590 Unity.TextMeshPro.dll TextMeshProUGUI
Error while read type, read 480 bytes but expected 484 bytes 591 Unity.TextMeshPro.dll TextMeshProUGUI
Error while read type, read 484 bytes but expected 488 bytes 592 Unity.TextMeshPro.dll TextMeshProUGUI

Bug The game crashes if I attempt to edit these assets.

To Reproduce I'm on python 3.9 using unitypy 1.8.9, and I've attached the typetree as well as the files causing the issue. holoEarth_ProblemData.zip

lugia19 commented 2 years ago

Update - I have now run into this issue with a separate game as well, its unity version is 2021.2.10f1

K0lb3 commented 2 years ago

I will look into it again. I guess the issue is either, that Unity uses some padding there (check if ends with 0es), or the typetree generator caused an issue somehow.

lugia19 commented 2 years ago

How should I check for the padding?