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

Python calls AssetsTools.NET uncompressible issue #137

Open
1103945342 opened this issue Nov 2, 2024 · 2 comments
Open

Python calls AssetsTools.NET uncompressible issue #137

1103945342 opened this issue Nov 2, 2024 · 2 comments

Comments

@1103945342
Copy link

When using python to call the pack compression method of the AssetsTools.NET, the file size of the final output is 0KB, here is my python code, looking forward to your reply

import clr
import os

clr.AddReference(r'C:\Users\Vasen\Desktop\PYTEXT\AssetsTools.NET.dll')

from AssetsTools.NET.Extra import AssetsManager
from AssetsTools.NET import AssetBundleCompressionType, AssetsFileWriter, AssetsFileReader

def compress_bundle(bundle_file_path):
if not os.path.exists(bundle_file_path):
print("指定的文件不存在.")
return

folder_path = os.path.dirname(bundle_file_path)
original_file_name = os.path.splitext(os.path.basename(bundle_file_path))[0]
file_extension = os.path.splitext(bundle_file_path)[1]

# 创建压缩文件的目标路径
compressed_file_path = os.path.join(folder_path, f'已压缩-{original_file_name}{file_extension}')

# 防止目标文件已存在,生成唯一文件名
if os.path.exists(compressed_file_path):
    counter = 1
    while os.path.exists(compressed_file_path):
        compressed_file_path = os.path.join(folder_path, f'已压缩-{original_file_name}({counter}){file_extension}')
        counter += 1

# 加载 AssetsManager 和 BundleFileInstance
am = AssetsManager()
bun = am.LoadBundleFile(bundle_file_path, False)

if bun is None or bun.file is None:
    print("加载包文件失败,未能找到文件内容.")
    return

try:
    # 使用 AssetsFileWriter 创建输出流
    writer = AssetsFileWriter(compressed_file_path)

    # 创建 AssetsFileReader 读取原始文件
    reader = AssetsFileReader(bundle_file_path)

    # 调试信息:检查 reader 的状态
    print("读取器的状态:", reader)

    # 检查输入文件的大小
    input_file_size = os.path.getsize(bundle_file_path)
    print("输入文件大小:", input_file_size, "字节")

    # 调用 Pack 方法进行压缩
    print("开始压缩资产包...")
    bun.file.Pack(reader, writer, AssetBundleCompressionType.LZMA)
    print("压缩完成,文件写入流...")

    # 检查输出流状态
    writer.Close()
    print("输出流已关闭。")

except Exception as e:
    print("压缩过程中发生错误:", str(e))

finally:
    # 关闭 writer 和 reader
    if 'writer' in locals():
        writer.Close()
    if 'reader' in locals():
        reader.Close()
    # 释放资源
    am.UnloadAll()  # 卸载所有资源
    print("所有资源已释放。")

compress_bundle(r'C:\Users\Vasen\Desktop\PYTEXT\sprite_character_fighter_equipment_avatar_skin.npk')

@nesrak1
Copy link
Owner

nesrak1 commented Nov 2, 2024

Pack will always write some bytes unless an exception is thrown. One of the common reasons pack throws is because the original bundle is compressed and pack only works on uncompressed bundles (although I'm guessing you are probably working off of a modified bundle, so I don't think this is it.)

@1103945342
Copy link
Author

I am certain that the asset package is in an uncompressed state, and UABEA can be compressed to a very small size. I can pack and compress it normally using the same C # syntax, but not using Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants