Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to import/export monobehaviors from asset files? #289

Open
CloudSodaZero opened this issue Dec 29, 2024 · 0 comments
Open

How to import/export monobehaviors from asset files? #289

CloudSodaZero opened this issue Dec 29, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@CloudSodaZero
Copy link

Game
Muck
(Steam free game. Muck)

Files I used

Code
Code 1

import UnityPy

env = UnityPy.load("level0")
for obj in env.objects:
    if obj.type.name == "MonoBehaviour":
        if obj.serialized_type.node:
            print(obj.read_typetree())

Code 2

import json
import UnityPy

with open("typetree.json", "r") as f:
    typetree_data = json.load(f)

env = UnityPy.load("level0")
for obj in env.objects:
    if obj.type.name == "MonoBehaviour":
        monobehaviour = obj.read()
        script = monobehaviour.m_Script.read()
        if obj.serialized_type.nodes:
            tree = obj.read_typetree()

        else:
            if script.m_Namespace == "":
                typetree = typetree_data[script.m_AssemblyName][script.m_ClassName]
            else:
                typetree = typetree_data[script.m_AssemblyName][
                    f"{script.m_Namespace}.{script.m_ClassName}"
                ]
            tree = obj.read_typetree(typetree)
        print(tree)

Error
Code 1 is the one I used in the past to extract Monobehaviour from a .bundle file with a built-in typetree.
If you run the code above, it won't produce any output, probably because it doesn't have a typetree built in.

Code 2 results in an error as shown below.

Traceback (most recent call last):
  File "c:\Program Files (x86)\Steam\steamapps\common\Muck\Muck_Data\main.py", line 10, in <module>
    monobehaviour = obj.read()
                    ^^^^^^^^^^
  File "C:\Users\USER\AppData\Roaming\Python\Python312\site-packages\UnityPy\files\ObjectReader.py", line 174, in read
    obj = self.read_typetree(wrap=True, check_read=check_read)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Roaming\Python\Python312\site-packages\UnityPy\files\ObjectReader.py", line 210, in read_typetree
    ret = TypeTreeHelper.read_typetree(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Roaming\Python\Python312\site-packages\UnityPy\helpers\TypeTreeHelper.py", line 149, in read_typetree
    raise ValueError(
ValueError: Expected to read 52 bytes, but only read 32 bytes

if I edit obj.read() to obj.read(check_read=False), The error message will be different, as shown below.

Traceback (most recent call last):
  File "c:\Program Files (x86)\Steam\steamapps\common\Muck\Muck_Data\main.py", line 11, in <module>
    script = monobehaviour.m_Script.read()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Roaming\Python\Python312\site-packages\UnityPy\classes\PPtr.py", line 37, in read
    return self.deref_parse_as_object()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Roaming\Python\Python312\site-packages\UnityPy\classes\PPtr.py", line 92, in deref_parse_as_object
    return self.deref(assetsfile).parse_as_object()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Roaming\Python\Python312\site-packages\UnityPy\classes\PPtr.py", line 81, in deref
    cab = env.find_file(external_clean_path)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Roaming\Python\Python312\site-packages\UnityPy\environment.py", line 320, in find_file
    raise FileNotFoundError(f"File {name} not found in {self.path}")
FileNotFoundError: File unity_builtin_extra not found in C:\Program Files (x86)\Steam\steamapps\common\Muck\Muck_Data

If I move the unity_builtin_extra file to the Muck_Data folder, I get the same error message.

Bug
It shows ValueError: Expected to read 52 bytes, but only read 32 bytes
FileNotFoundError: File unity_builtin_extra not found in C:\Program Files (x86)\Steam\steamapps\common\Muck\Muck_Data
To Reproduce

My goal is to modify and import the TextMeshProUGUI asset from the assets in level0.
Can you tell how to extract the Typetree correctly, how to load it and how to extract the Monobehaviour from the .asset file using the Typetree json?
Thank you for your hard work.

@CloudSodaZero CloudSodaZero added the bug Something isn't working label Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant