K0lb3 / UnityPy

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

Can't read bytes at position / Error while read type, read X bytes but expected Y bytes #195

Open kryakozebra opened 11 months ago

kryakozebra commented 11 months ago

Code

        env = UnityPy.load(bundle_file)
        for obj in env.objects:
            if obj.type.name != 'MonoBehaviour':
                continue
            if not obj.serialized_type.nodes:
                logging.warning('bundle: object does not have any nodes!')
                continue
            tree = obj.read_typetree()

Error

    tree = obj.read_typetree()
           ^^^^^^^^^^^^^^^^^^^
  File "site-packages\UnityPy\files\ObjectReader.py", line 219, in read_typetree
    res = TypeTreeHelper.read_typetree(nodes, self)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages\UnityPy\helpers\TypeTreeHelper.py", line 156, in read_typetree
    return read_typetree_c(
           ^^^^^^^^^^^^^^^^
ValueError: Can't read 1445199970 bytes at position 236 of 312
Error occured at UnityPyBoost/TypeTreeHelper.c:274:read_string

Almost exact error occurs if TypeTreeHelper.read_typetree_c = False is used:

    tree = obj.read_typetree()
           ^^^^^^^^^^^^^^^^^^^
  File "site-packages\UnityPy\files\ObjectReader.py", line 219, in read_typetree
    res = TypeTreeHelper.read_typetree(nodes, self)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages\UnityPy\helpers\TypeTreeHelper.py", line 167, in read_typetree
    raise TypeTreeError(
UnityPy.exceptions.TypeTreeError: Error while read type, read 260 bytes but expected 312 bytes

Bug I have 2 bundles with text assets; one is working fine, the other causes exceptions during obj.read_typetree() call. If I ignore these exceptions while reading - it seems to extract most of the bundle contents just fine. However if I then try to edit the contents and save the bundle, the resulting file causes the game to freeze on startup, and cannot be opened with AssetStudio for example.

To Reproduce