K0lb3 / UnityPy

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

How to save env file as it's original file type (Compressed) #167

Open Azg-stdio opened 1 year ago

Azg-stdio commented 1 year ago

Code

for root, dirs, files in os.walk(source_folder):
    for file_name in files:
        file_path = os.path.join(root, file_name)
        env = UnityPy.load(file_path)

        # alternative way which keeps the original path
        for path,obj in env.container.items():
            if obj.type.name in ["Texture2D", "Sprite"]:                
                data = obj.read()
                # create dest based on original path
                dest = os.path.join(destination_folder, *path.split("/"))
                # make sure that the dir of that path exists
                os.makedirs(os.path.dirname(dest), exist_ok = True)
                # edit texture
                fp = os.path.join(replace_dir, data.name + '.png')
                pil_img = Image.open(fp)
                data.image = pil_img
                data.image.save(dest)

Error So this is working right now, it's loading a .bbq file which contains a .png file and other stuff, I'm just changing the .png file and saving, on the destination folder seems like the original path is working and everything, my question is ¿How do you convert it back to being a .bbq file?

To Reproduce This is the .bbq file I'm trying to edit: https://drive.google.com/file/d/1LA7sKNRrEXUeiHdzYjg8bfWyLSngBnG0/view?usp=sharing

I know it's not really a bug, but didn't found any info about it, thank you for the help and awesome work!

K0lb3 commented 1 year ago

It's described at UnityPy#environment.

So you have to add the following:

        with open(file_path, "wb") as f:
            f.write(env.file.save())

You probably don't want to replace files in place, though, as patching already once patched files can lead to problems.

Azg-stdio commented 1 year ago

I see, so my whole idea was wrong from the beggining.

I did try with the assets in the shared.assets bundle files but the library didn't show me anything there, maybe because it's for a game on Unity 2020.3? Does UnityPy work on that version?

Thank you for the help!