Skip to content

Error get_item_for_version(exactVersion: UnityVersion, items: List[Tuple[UnityVersion, Any]]) #339

@Serrallonga25

Description

@Serrallonga25

Code

def unpack_all_assets(source_folder: str, destination_folder: str):
    # iterate over all files in source folder
    for root, dirs, files in os.walk(source_folder):
        for file_name in files:
            # generate file_path
            file_path = os.path.join(root, file_name)
            # load that file via UnityPy.load
            env = UnityPy.load(file_path)

            # iterate over internal objects
            for obj in env.objects:
                # process specific object types
                if obj.type.name in ["Texture2D", "Sprite"]:
                    # parse the object data
                    data = obj.read()

Error
error('unpack requires a buffer of 1 bytes')

Bug
Does not detect the Unity version structure correctly UnityPy.helpers.Tpk.get_item_for_version(exactVersion: UnityVersion, items: List[Tuple[UnityVersion, Any]]) -> Any:

I pass it the version (2018, 1, 0, 2) and it returns the <UnityPy.helpers.Tpk.TpkUnityClass> of the version (2018, 1, 0, 1)

Original Function:

def get_item_for_version(exactVersion: UnityVersion, items: List[Tuple[UnityVersion, Any]]) -> Any:
    ret = None
    for version, item in items:
        if exactVersion >= version:
            ret = item
        else:
            break
    if ret:
        return ret
    raise ValueError("Could not find exact version")
class_id = 213 #<ClassIDType.Sprite: 213>
version = (2018, 1, 0, 2)

class_info = TPKTYPETREE.ClassInformation[class_id].getVersionedClass(UnityVersion.fromList(*version))

UnityVersion 2018.1.0.2 UnityVersion 2017.3.0.1 568016507297071106 567735040910295297
* UnityVersion 2018.1.0.2 UnityVersion 2018.1.0.2  568016507297071106 568016507297071362

for UnityVersion(2018.1.0.2) it returns UnityVersion(2017.3.0.1) instead of UnityVersion(2018.1.0.2)

and

class_id = 25
version = (2018, 1, 0, 2)
class_info = TPKTYPETREE.ClassInformation[class_id].getVersionedClass(UnityVersion.fromList(*version))

UnityVersion 2018.1.0.2 UnityVersion 2017.3.0.1 568016507297071106 567735040910295297

it's right

Solution:
A possible solution, (it seems to work for me)

def get_item_for_version(exactVersion: UnityVersion, items: List[Tuple[UnityVersion, Any]]) -> Any:
    ret = None
    for version, item in items:
        version = UnityVersion.fromString(str(version).replace('UnityVersion', "").strip())
        if exactVersion >= version:
            ret = item
        else:
            break
    
    if ret:
        return ret
    raise ValueError("Could not find exact version")

I have to do more tests

To Reproduce

  • a copy of the file that causes the problem
  • following data:
    • Python version 3.9
    • UnityPy version 1.23.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions