K0lb3 / UnityPy

UnityPy is python module that makes it possible to extract/unpack and edit Unity assets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reading typetree error: Can't read 4 bytes at position 32 of 32

JunkBeat opened this issue · comments

Code:

import os
import UnityPy
import json
from UnityPy.classes import MonoBehaviour

file = 'sharedassets1.assets'
output_dir = "output"
with open("typetrees.json", 'r', encoding='utf-8') as f:
    trees = json.load(f)

print(file)
env = UnityPy.load(file)
env.files["globalgamemanagers.assets"] = env.load_file("globalgamemanagers.assets")
for obj in env.objects:
    if obj.type.name == "MonoBehaviour":
        data = obj.read()
        script = data.m_Script
        if not script:
            print("Script not found!")
            continue
            
        script = script.read()
        fp = os.path.join(output_dir, f"{script.name}-{file}-{obj.path_id}.json")

        if script.m_ClassName == "TextMeshProUGUI":
            if obj.serialized_type and obj.serialized_type.nodes:
                tree = obj.read_typetree()
            else:
                key = f"{script.m_Namespace}.{script.m_ClassName}"
                nodes = trees.get(key)
                if nodes is None:
                    print(f"Error: Class '{key}' not found in trees.")
                    continue
                
                try:
                    tree = obj.read_typetree(nodes)
                except ValueError as e:
                    print(f"{e} ({file}-PathID: {obj.path_id})")
                    continue

            with open(fp, "wt", encoding="utf8") as f:
                json.dump(tree, f, ensure_ascii=False, indent=4)
            print(f"Exported #{obj.path_id}")

Error:

Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets45.assets-PathID: 7404)
sharedassets1.assets
Exported #2844
Exported #2845
Exported #2846
Exported #2849
Exported #2851
Exported #2857
Exported #2860
Exported #2864
Exported #2865
Exported #2866
Exported #2872
Exported #2875
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2877)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2878)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2879)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2883)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2888)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2891)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2894)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2898)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2901)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2905)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2916)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2918)
Can't read 4 bytes at position 32 of 32
Error occured at UnityPyBoost/TypeTreeHelper.c:165:read_SInt32 (sharedassets1.assets-PathID: 2920)
Exported #2921
Exported #2922
...

I don't understand why this is happening. Reading typetree works fine for some env.objects, but causes errors for others. Most likely this is a bug.

Sample files: link

  • Python: 3.10.2
  • UnityPy: 1.10.2

My unity files were corrupted after being edited using another program. When I rolled back the files to the original ones, all the problems disappeared.