Last active
May 16, 2018 00:37
-
-
Save paulsinnett/aae1924c0ef21be0dcfee2c3a461f155 to your computer and use it in GitHub Desktop.
unitypackage unpacker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import tarfile | |
import os | |
import io | |
class UnityAsset: | |
pass | |
parser = argparse.ArgumentParser(description='Unpack a .unitypackage') | |
parser.add_argument('INPUT_PACKAGE', help='path to a .unitypackage file') | |
parser.add_argument('-o', '--output', help='output directory (defaults to current directory)') | |
args = parser.parse_args() | |
print('.unitypackage file = ' + args.INPUT_PACKAGE) | |
if (args.output): | |
output_directory = args.output | |
else: | |
output_directory = '.' | |
paths = {} | |
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive: | |
for entry in archive.getmembers(): | |
guid = os.path.dirname(entry.name) | |
if (guid not in paths): | |
paths[guid] = UnityAsset() | |
if (entry.isfile() and os.path.basename(entry.name) == 'pathname'): | |
paths[guid].pathname_file = entry | |
elif (os.path.basename(entry.name) == 'asset.meta'): | |
paths[guid].meta_file = entry | |
elif (os.path.basename(entry.name) == 'asset'): | |
paths[guid].asset_file = entry | |
for entry in paths: | |
if (hasattr(paths[entry], 'pathname_file')): | |
pathname = output_directory | |
pathname_file = paths[entry].pathname_file | |
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive: | |
with archive.extractfile(pathname_file) as pathfile: | |
with io.TextIOWrapper(io.BytesIO(pathfile.read())) as path: | |
pathname = os.path.join(output_directory, path.readline().strip()) | |
os.makedirs(os.path.dirname(pathname), exist_ok=True) | |
if (hasattr(paths[entry], 'meta_file')): | |
print(pathname + '.meta') | |
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive: | |
with archive.extractfile(paths[entry].meta_file) as meta_file: | |
with open(pathname + '.meta', 'wb') as output_file: | |
output_file.write(meta_file.read()) | |
if (hasattr(paths[entry], 'asset_file')): | |
print(pathname) | |
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive: | |
with archive.extractfile(paths[entry].asset_file) as asset_file: | |
with open(pathname, 'wb') as output_file: | |
output_file.write(asset_file.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
extract file names