|
1 | 1 | import os |
2 | | -from UnityPy import AssetsManager |
| 2 | +import UnityPy |
3 | 3 | from collections import Counter |
4 | 4 | import zipfile |
5 | | - |
6 | | -TYPES = ['Sprite', 'Texture2D', 'TextAsset'] |
| 5 | +import json |
| 6 | + |
| 7 | +TYPES = [ |
| 8 | + # Images |
| 9 | + 'Sprite', |
| 10 | + 'Texture2D', |
| 11 | + # Text (filish) |
| 12 | + 'TextAsset', |
| 13 | + 'Shader', |
| 14 | + 'MonoBehaviour', |
| 15 | + 'Mesh' |
| 16 | + # Font |
| 17 | + 'Font', |
| 18 | + # Audio |
| 19 | + 'AudioClip', |
| 20 | +] |
7 | 21 |
|
8 | 22 | ROOT = os.path.dirname(os.path.realpath(__file__)) |
9 | 23 |
|
|
23 | 37 |
|
24 | 38 |
|
25 | 39 | def main(): |
26 | | - for root, dirs, files in os.walk(ASSETS, topdown=False): |
27 | | - if '.git' in root: |
28 | | - continue |
29 | | - for f in files: |
30 | | - print(f) |
31 | | - extension = os.path.splitext(f)[1] |
32 | | - src = os.path.realpath(os.path.join(root, f)) |
33 | | - |
34 | | - if extension == ".zip": |
35 | | - archive = zipfile.ZipFile(src, 'r') |
36 | | - for zf in archive.namelist(): |
37 | | - extract_assets(archive.open(zf)) |
38 | | - else: |
39 | | - extract_assets(src) |
| 40 | + for root, dirs, files in os.walk(ASSETS, topdown=False): |
| 41 | + if '.git' in root: |
| 42 | + continue |
| 43 | + for f in files: |
| 44 | + print(f) |
| 45 | + extension = os.path.splitext(f)[1] |
| 46 | + src = os.path.realpath(os.path.join(root, f)) |
| 47 | + |
| 48 | + if extension == ".zip": |
| 49 | + archive = zipfile.ZipFile(src, 'r') |
| 50 | + for zf in archive.namelist(): |
| 51 | + extract_assets(archive.open(zf)) |
| 52 | + else: |
| 53 | + extract_assets(src) |
40 | 54 |
|
41 | 55 |
|
42 | 56 | def extract_assets(src): |
43 | | - # load source |
44 | | - am = AssetsManager(src) |
45 | | - |
46 | | - # iterate over assets |
47 | | - for asset in am.assets.values(): |
48 | | - # assets without container / internal path will be ignored for now |
49 | | - if not asset.container: |
50 | | - continue |
51 | | - |
52 | | - # check which mode we will have to use |
53 | | - num_cont = sum(1 for obj in asset.container.values() if obj.type in TYPES) |
54 | | - num_objs = sum(1 for obj in asset.objects.values() if obj.type in TYPES) |
55 | | - |
56 | | - # check if container contains all important assets, if yes, just ignore the container |
57 | | - if num_objs <= num_cont * 2: |
58 | | - for asset_path, obj in asset.container.items(): |
59 | | - fp = os.path.join(DST, *asset_path.split('/')[IGNOR_DIR_COUNT:]) |
60 | | - export_obj(obj, fp) |
61 | | - |
62 | | - # otherwise use the container to generate a path for the normal objects |
63 | | - else: |
64 | | - extracted = [] |
65 | | - # find the most common path |
66 | | - occurence_count = Counter(os.path.splitext(asset_path)[0] for asset_path in asset.container.keys()) |
67 | | - local_path = os.path.join(DST, *occurence_count.most_common(1)[0][0].split('/')[IGNOR_DIR_COUNT:]) |
68 | | - |
69 | | - for obj in asset.objects.values(): |
70 | | - if obj.path_id not in extracted: |
71 | | - extracted.extend(export_obj(obj, local_path, append_name=True)) |
| 57 | + # load source |
| 58 | + env = UnityPy.load(src) |
| 59 | + |
| 60 | + # iterate over assets |
| 61 | + for asset in env.assets: |
| 62 | + # assets without container / internal path will be ignored for now |
| 63 | + if not asset.container: |
| 64 | + continue |
| 65 | + |
| 66 | + # check which mode we will have to use |
| 67 | + num_cont = sum(1 for obj in asset.container.values() |
| 68 | + if obj.type in TYPES) |
| 69 | + num_objs = sum(1 for obj in asset.objects.values() |
| 70 | + if obj.type in TYPES) |
| 71 | + |
| 72 | + # check if container contains all important assets, if yes, just ignore the container |
| 73 | + if num_objs <= num_cont * 2: |
| 74 | + for asset_path, obj in asset.container.items(): |
| 75 | + fp = os.path.join(DST, *asset_path.split('/') |
| 76 | + [IGNOR_DIR_COUNT:]) |
| 77 | + export_obj(obj, fp) |
| 78 | + |
| 79 | + # otherwise use the container to generate a path for the normal objects |
| 80 | + else: |
| 81 | + extracted = [] |
| 82 | + # find the most common path |
| 83 | + occurence_count = Counter(os.path.splitext(asset_path)[ |
| 84 | + 0] for asset_path in asset.container.keys()) |
| 85 | + local_path = os.path.join( |
| 86 | + DST, *occurence_count.most_common(1)[0][0].split('/')[IGNOR_DIR_COUNT:]) |
| 87 | + |
| 88 | + for obj in asset.objects.values(): |
| 89 | + if obj.path_id not in extracted: |
| 90 | + extracted.extend(export_obj( |
| 91 | + obj, local_path, append_name=True)) |
72 | 92 |
|
73 | 93 |
|
74 | 94 | def export_obj(obj, fp: str, append_name: bool = False) -> list: |
75 | | - if obj.type not in TYPES: |
76 | | - return [] |
77 | | - data = obj.read() |
78 | | - if append_name: |
79 | | - fp = os.path.join(fp, data.name) |
80 | | - |
81 | | - fp, extension = os.path.splitext(fp) |
82 | | - os.makedirs(os.path.dirname(fp), exist_ok=True) |
83 | | - |
84 | | - if obj.type == 'TextAsset': |
85 | | - if not extension: |
86 | | - extension = '.txt' |
87 | | - with open(f"{fp}{extension}", 'wb') as f: |
88 | | - f.write(data.script) |
89 | | - |
90 | | - elif obj.type == "Sprite": |
91 | | - extension = ".png" |
92 | | - data.image.save(f"{fp}{extension}") |
93 | | - |
94 | | - return [obj.path_id, data.m_RD.texture.path_id, getattr(data.m_RD.alphaTexture, 'path_id', None)] |
95 | | - |
96 | | - elif obj.type == "Texture2D": |
97 | | - extension = ".png" |
98 | | - fp = f"{fp}{extension}" |
99 | | - if not os.path.exists(fp): |
100 | | - try: |
101 | | - data.image.save(fp) |
102 | | - except EOFError: |
103 | | - pass |
104 | | - |
105 | | - return [obj.path_id] |
| 95 | + if obj.type not in TYPES: |
| 96 | + return [] |
| 97 | + |
| 98 | + data = obj.read() |
| 99 | + if append_name: |
| 100 | + fp = os.path.join(fp, data.name) |
| 101 | + |
| 102 | + fp, extension = os.path.splitext(fp) |
| 103 | + os.makedirs(os.path.dirname(fp), exist_ok=True) |
| 104 | + |
| 105 | + # streamlineable types |
| 106 | + export = None |
| 107 | + if obj.type == 'TextAsset': |
| 108 | + if not extension: |
| 109 | + extension = '.txt' |
| 110 | + export = data.script |
| 111 | + |
| 112 | + elif obj.type == "Font": |
| 113 | + if data.m_FontData: |
| 114 | + extension = ".ttf" |
| 115 | + if data.m_FontData[0:4] == b"OTTO": |
| 116 | + extension = ".otf" |
| 117 | + export = data.m_FontData |
| 118 | + else: |
| 119 | + return [obj.path_id] |
| 120 | + |
| 121 | + elif obj.type == "Mesh": |
| 122 | + extension = ".obf" |
| 123 | + export = data.export().encode("utf8") |
| 124 | + |
| 125 | + elif obj.type == "Shader": |
| 126 | + extension = ".txt" |
| 127 | + export = data.export().encode("utf8") |
| 128 | + |
| 129 | + elif obj.type == "MonoBehaviour": |
| 130 | + # The data structure of MonoBehaviours is custom |
| 131 | + # and is stored as nodes |
| 132 | + # If this structure doesn't exist, |
| 133 | + # it might still help to at least save the binary data, |
| 134 | + # which can then be inspected in detail. |
| 135 | + if obj.serialized_type.nodes: |
| 136 | + extension = ".json" |
| 137 | + export = json.dumps( |
| 138 | + obj.read_typetree(), |
| 139 | + indent=4, |
| 140 | + ensure_ascii=False |
| 141 | + ).encode("utf8") |
| 142 | + else: |
| 143 | + extension = ".bin" |
| 144 | + export = data.raw_data |
| 145 | + |
| 146 | + if export: |
| 147 | + with open(f"{fp}{extension}", "wb") as f: |
| 148 | + f.write(export) |
| 149 | + |
| 150 | + # non-streamlineable types |
| 151 | + if obj.type == "Sprite" and data.Width: |
| 152 | + data.image.save(f"{fp}.png") |
| 153 | + |
| 154 | + return [obj.path_id, data.m_RD.texture.path_id, getattr(data.m_RD.alphaTexture, 'path_id', None)] |
| 155 | + |
| 156 | + elif obj.type == "Texture2D": |
| 157 | + if not os.path.exists(fp) and data.Width: |
| 158 | + # textures can have size 0..... |
| 159 | + data.image.save(f"{fp}.png") |
| 160 | + |
| 161 | + elif obj.type == "AudioClip": |
| 162 | + samples = data.samples |
| 163 | + if len(samples) == 0: |
| 164 | + pass |
| 165 | + elif len(samples) == 1: |
| 166 | + with open(f"{fp}.wav", "wb") as f: |
| 167 | + f.write(list(data.samples.values())[0]) |
| 168 | + else: |
| 169 | + os.makedirs(fp, exist_ok=True) |
| 170 | + for name, clip_data in samples.items(): |
| 171 | + with open(os.path.join(fp, f"{name}.wav"), "wb") as f: |
| 172 | + f.write(clip_data) |
| 173 | + return [obj.path_id] |
106 | 174 |
|
107 | 175 |
|
108 | 176 | if __name__ == '__main__': |
109 | | - main() |
| 177 | + main() |
0 commit comments