|
| 1 | +""" |
| 2 | +This script shows how to create a bundle from dumped assets from the memory. |
| 3 | +
|
| 4 | +The dumped assets consist of SerializedFiles and their resources(cabs). |
| 5 | +A sample file of the original game is required for this script. |
| 6 | +This example uses the globalgamemanager as this asset should exist in all Unity games. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import uuid |
| 11 | +import random |
| 12 | +from copy import copy |
| 13 | +import re |
| 14 | + |
| 15 | +import UnityPy |
| 16 | +from UnityPy.enums import ClassIDType |
| 17 | +from UnityPy.files import BundleFile |
| 18 | +from UnityPy.files.SerializedFile import FileIdentifier, ObjectReader, SerializedType |
| 19 | + |
| 20 | + |
| 21 | +SERIALIZED_PATH = "globalgamemanagers" |
| 22 | +DATA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + bf = Fake( |
| 27 | + signature="UnityFS", |
| 28 | + version=6, |
| 29 | + format=6, |
| 30 | + version_engine="2017.4.30f1", |
| 31 | + version_player="5.x.x", |
| 32 | + _class=BundleFile, |
| 33 | + files={}, |
| 34 | + ) |
| 35 | + # load default serialized file and prepare some variables for easier access to key objects |
| 36 | + env = UnityPy.load(SERIALIZED_PATH) |
| 37 | + sf = env.file # serialized file |
| 38 | + or_bp = list(sf.objects.values())[0].__dict__ # object data |
| 39 | + |
| 40 | + bf.files["serialized_file"] = sf |
| 41 | + sf._flag = 4 |
| 42 | + |
| 43 | + # remove all unnesessary stuff |
| 44 | + for key in list(sf.objects.keys()): |
| 45 | + del sf.objects[key] |
| 46 | + sf.externals = [] |
| 47 | + |
| 48 | + # add all files from DATA_PATH |
| 49 | + for root, dirs, files in os.walk(DATA_PATH): |
| 50 | + for f in files: |
| 51 | + fp = os.path.join(root, f) |
| 52 | + if f[:3] == "CAB": |
| 53 | + add_cab(bf, sf, root, f) |
| 54 | + else: |
| 55 | + add_object(sf, fp, or_bp) |
| 56 | + |
| 57 | + # save edited bundle |
| 58 | + open("bundle_edited.unity3d", "wb").write(bf.save()) |
| 59 | + |
| 60 | + |
| 61 | +def add_cab(bf, sf, root, f): |
| 62 | + fp = os.path.join(root, f) |
| 63 | + bf.files[f] = Fake(data=open(fp, "rb").read(), _flag=4) |
| 64 | + sf.externals.append( |
| 65 | + Fake( |
| 66 | + temp_empty="", |
| 67 | + guid=generate_16_byte_uid(), |
| 68 | + path=f"archive:/{os.path.basename(root)}/{f}", |
| 69 | + type=0, |
| 70 | + _class=FileIdentifier, |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def add_object(sf, fp, or_bp): |
| 76 | + # get correct type id |
| 77 | + path_id, class_name = os.path.splitext(os.path.basename(fp)) |
| 78 | + path_id = int(path_id) if re.match( |
| 79 | + r"^\d+$", path_id) else generate_path_id(sf.objects) |
| 80 | + class_id = getattr( |
| 81 | + ClassIDType, class_name[1:], ClassIDType.UnknownType).value |
| 82 | + type_id = -1 |
| 83 | + for i, styp in enumerate(sf.types): |
| 84 | + if styp.class_id == class_id: |
| 85 | + type_id = i |
| 86 | + if type_id == -1: # not found, add type |
| 87 | + type_id = len(sf.types) |
| 88 | + sf.types.append( |
| 89 | + Fake( |
| 90 | + class_id=class_id, |
| 91 | + is_stripped_type=False, |
| 92 | + node=[], |
| 93 | + script_type_index=-1, |
| 94 | + old_type_hash=generate_16_byte_uid(), |
| 95 | + _class=SerializedType, |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + # add new object |
| 100 | + odata = copy(or_bp) |
| 101 | + odata.update( |
| 102 | + { |
| 103 | + "data": open(fp, "rb").read(), |
| 104 | + "path_id": generate_path_id(sf.objects), |
| 105 | + "class_id": class_id, |
| 106 | + "type_id": type_id, |
| 107 | + } |
| 108 | + ) |
| 109 | + sf.objects[path_id] = Fake(**odata, _class=ObjectReader) |
| 110 | + |
| 111 | + |
| 112 | +def generate_path_id(objects): |
| 113 | + while True: |
| 114 | + uid = random.randint(-(2 ** 16), 2 ** 16 - 1) |
| 115 | + if uid not in objects: |
| 116 | + return uid |
| 117 | + |
| 118 | + |
| 119 | +def generate_16_byte_uid(): |
| 120 | + return uuid.uuid1().urn[-16:].encode("ascii") |
| 121 | + |
| 122 | + |
| 123 | +class Fake(object): |
| 124 | + """ |
| 125 | + fake class for easy class creation without init call |
| 126 | + """ |
| 127 | + |
| 128 | + def __init__(self, **kwargs): |
| 129 | + self.__dict__.update(kwargs) |
| 130 | + if "_class" in kwargs: |
| 131 | + self.__class__ = kwargs["_class"] |
| 132 | + |
| 133 | + def save(self): |
| 134 | + return self.data |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + main() |
0 commit comments