unity_scene_repacker 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,99 @@
1
+ import os.path
2
+
3
+ from UnityPy.enums import ClassIDType
4
+ from UnityPy.environment import Environment
5
+ from pathlib import Path
6
+ import json
7
+ import argparse
8
+
9
+ from unity_scene_repacker.repack import repack_scene_bundle
10
+ from unity_scene_repacker.prune import prune
11
+ from unity_scene_repacker.utils import get_root_objects, get_root_object_readers, get_scene_names
12
+
13
+ def rename(name: str) -> str:
14
+ name, *rest = name.split(" (")
15
+ return name
16
+
17
+
18
+ def main():
19
+ parser = argparse.ArgumentParser()
20
+ parser.add_argument("--game-dir", help="game directory where the levels are, i.e. Game/Game_Data", required=True)
21
+ parser.add_argument(
22
+ "--preloads",
23
+ help="path to json file, containg map from scene name to list of gameobject paths to include in the assetbundle",
24
+ required=True,
25
+ )
26
+ parser.add_argument(
27
+ "-o",
28
+ "--output",
29
+ help="path to json file, containg map from scene name to list of gameobject paths to include in the assetbundle",
30
+ default="preloads.bundle",
31
+ )
32
+ parser.add_argument(
33
+ "--disable",
34
+ action=argparse.BooleanOptionalAction,
35
+ default=True
36
+ )
37
+ args = parser.parse_args()
38
+
39
+ monster_preloads = json.load(open(args.preloads, "r"))
40
+
41
+ out_path = Path(args.output)
42
+ project = Path(args.game_dir)
43
+
44
+ env = Environment()
45
+ ggm = env.load_file(str(project.joinpath("globalgamemanagers")))
46
+
47
+ scene_map = { name: i for i, name in enumerate(get_scene_names(ggm)) }
48
+ level_names = monster_preloads.keys()
49
+ paths = [str(project.joinpath(f"level{scene_map[name]}")) for name in level_names]
50
+
51
+ for i, (path, name) in enumerate(zip(paths, level_names)):
52
+ print(f"Loading {i + 1}/{len(paths)} [{name}] ", end="\r")
53
+ env.load_file(path)
54
+ print()
55
+ serialized_files = [env.files[path] for path in paths]
56
+
57
+ object_count_before = sum(len(x.objects.values()) for x in serialized_files)
58
+
59
+ for i, (file, level_name) in enumerate(zip(serialized_files, level_names)):
60
+ print(f"Pruning {i + 1}/{len(paths)} [{level_name}] ", end="\r")
61
+ level_monsters = monster_preloads[level_name]
62
+
63
+ prune(file, level_monsters, [ClassIDType.RenderSettings])
64
+
65
+ for obj in get_root_object_readers(file):
66
+ tt = obj.read_typetree()
67
+ tt["m_Name"] = rename(tt["m_Name"])
68
+ if args.disable:
69
+ tt["m_IsActive"] = False
70
+ obj.save_typetree(tt)
71
+ print()
72
+
73
+ object_count_after = sum(len(x.objects.values()) for x in serialized_files)
74
+ print(f"Pruned {object_count_before} -> {object_count_after} objects")
75
+
76
+ prefix = "bundle"
77
+ new_bundle = repack_scene_bundle(dict(zip([f"{prefix}_{name}" for name in level_names], serialized_files)))
78
+
79
+ out_path.parent.mkdir(parents=True, exist_ok=True)
80
+ with open(out_path, "wb") as f:
81
+ f.write(new_bundle.save())
82
+ f.flush()
83
+
84
+ size_bytes = os.path.getsize(f.name)
85
+ size_mb = size_bytes / (1024 * 1024)
86
+
87
+
88
+ print()
89
+ print(f"{out_path} ({size_mb:.2f}MiB):")
90
+ for name, file in Environment(str(out_path)).file.files.items():
91
+ if name.endswith("sharedAssets"):
92
+ continue
93
+
94
+ for root in get_root_objects(file):
95
+ print(f"- '{root.m_Name}'")
96
+
97
+
98
+ if __name__ == "__main__":
99
+ main()
@@ -0,0 +1,79 @@
1
+ from collections import deque
2
+ from collections.abc import Iterator
3
+
4
+ from UnityPy.classes import Object, GameObject, Transform, RectTransform, PPtr, SpriteAtlas
5
+ from UnityPy.enums import ClassIDType
6
+ from UnityPy.files import SerializedFile, ObjectReader
7
+
8
+ from unity_scene_repacker.utils import lookup_path, get_root_objects
9
+
10
+
11
+ def prune(scene: SerializedFile, keep_paths: list[str], always_include: list[ClassIDType] = None) -> list[Transform]:
12
+ root_objs = list(get_root_objects(scene))
13
+ if len(root_objs) == 0:
14
+ print(scene)
15
+ keep_new_root = [lookup_path(keep, root_objs) for keep in keep_paths]
16
+ reachable = [keep.object_reader for keep in keep_new_root]
17
+ prune_reachable(scene, reachable, always_include)
18
+
19
+ for keep in keep_new_root:
20
+ keep = keep.object_reader
21
+ tt = keep.read_typetree()
22
+ tt["m_Father"] = {"m_FileID": 0, "m_PathID": 0}
23
+ keep.save_typetree(tt)
24
+
25
+ return keep_new_root
26
+
27
+
28
+ def prune_reachable(scene: SerializedFile, reachable: list[ObjectReader[Transform]], always_include: list[ClassIDType] = None):
29
+ include: set[int] = set()
30
+ queue: deque[ObjectReader[Object] | PPtr[Object]] = deque(reachable)
31
+
32
+ while queue:
33
+ node = queue.popleft()
34
+ include.add(node.path_id)
35
+
36
+ for r in iterate_visible(node):
37
+ if r.path_id not in include:
38
+ queue.append(r)
39
+
40
+ if always_include:
41
+ include.update(i for i in scene.objects if scene.objects[i].type in always_include)
42
+
43
+ new_objects = {key: scene.objects[key] for key in include}
44
+
45
+ scene.objects = dict(sorted(new_objects.items()))
46
+
47
+ # remove unused types
48
+ type_index = 0
49
+ type_mapping = {}
50
+ new_types = []
51
+ for used_type in set(obj.type_id for obj in scene.objects.values()):
52
+ type_mapping[used_type] = type_index
53
+ new_types.append(scene.types[used_type])
54
+ type_index += 1
55
+ for obj in scene.objects.values():
56
+ obj.type_id = type_mapping[obj.type_id]
57
+ scene.types = new_types
58
+
59
+
60
+ def iterate_visible(obj: ObjectReader) -> Iterator[PPtr[Object]]:
61
+ # if isinstance(obj, PPtr):
62
+ # obj = obj.get_obj()
63
+
64
+ if obj.type == ClassIDType.GameObject:
65
+ go: GameObject = obj.read()
66
+ yield from go.m_Components
67
+ elif obj.type == ClassIDType.Canvas:
68
+ pass
69
+ elif obj.type == ClassIDType.Transform:
70
+ transform: Transform = obj.read()
71
+ yield transform.m_GameObject
72
+ yield from transform.m_Children
73
+ elif obj.type == ClassIDType.RectTransform:
74
+ rect: RectTransform = obj.read()
75
+ yield rect.m_GameObject
76
+ yield from rect.m_Children
77
+ elif obj.type == ClassIDType.SpriteAtlas:
78
+ atlas: SpriteAtlas = obj.read()
79
+ yield from atlas.m_PackedSprites
@@ -0,0 +1,75 @@
1
+ import UnityPy
2
+ from UnityPy.environment import SerializedFile
3
+ from UnityPy.files import BundleFile, ObjectReader
4
+ from UnityPy.enums import ArchiveFlags
5
+ from UnityPy.classes import AssetBundle
6
+
7
+ from unity_scene_repacker.utils import Fake
8
+ from typing import cast
9
+
10
+ import importlib.resources
11
+
12
+
13
+ def repack_scene_bundle(scenes: dict[str, SerializedFile]) -> BundleFile:
14
+ with importlib.resources.open_binary("unity_scene_repacker.data", "empty_scene_bundle.unity3d") as f:
15
+ emptybundle_bin = f.read()
16
+
17
+ emptybundle: BundleFile = UnityPy.load(emptybundle_bin).file
18
+ shared_assets: SerializedFile = emptybundle.files["BuildPlayer-EmptyScene.sharedAssets"]
19
+
20
+ assetbundle_meta: ObjectReader[AssetBundle] = shared_assets.objects[2]
21
+ assetbundle_meta.save_typetree(
22
+ {
23
+ "m_Name": "scenebundle",
24
+ "m_PreloadTable": [],
25
+ "m_Container": [
26
+ (
27
+ f"Assets/SceneBundle/{name}.unity",
28
+ {
29
+ "preloadIndex": 0,
30
+ "preloadSize": 0,
31
+ "asset": {"m_FileID": 0, "m_PathID": 0},
32
+ },
33
+ )
34
+ for name in scenes
35
+ ],
36
+ "m_MainAsset": {
37
+ "preloadIndex": 0,
38
+ "preloadSize": 0,
39
+ "asset": {"m_FileID": 0, "m_PathID": 0},
40
+ },
41
+ "m_RuntimeCompatibility": 1,
42
+ "m_AssetBundleName": "",
43
+ "m_Dependencies": [],
44
+ "m_IsStreamedSceneAssetBundle": True,
45
+ "m_ExplicitDataLayout": 0,
46
+ "m_PathFlags": 7,
47
+ "m_SceneHashes": [],
48
+ }
49
+ )
50
+
51
+ files = {}
52
+ # first = True
53
+ for name, scene in scenes.items():
54
+ scene_shared_assets = shared_assets
55
+ # if not first:
56
+ # scene_shared_assets = copy.copy(shared_assets)
57
+ # scene_shared_assets.objects = copy.copy(scene_shared_assets.objects)
58
+ # assert scene_shared_assets.objects.pop(2).class_id == 142 # AssetBundle
59
+ # first = False
60
+
61
+ scene.flags = 4
62
+ files[f"BuildPlayer-{name}.sharedAssets"] = scene_shared_assets
63
+ files[f"BuildPlayer-{name}"] = scene
64
+
65
+ return cast(BundleFile, Fake(
66
+ BundleFile,
67
+ signature="UnityFS",
68
+ version=8,
69
+ version_player="5.x.x",
70
+ version_engine="2022.3.18f1",
71
+ dataflags=ArchiveFlags.BlocksAndDirectoryInfoCombined | ArchiveFlags.BlockInfoNeedPaddingAtStart | 3,
72
+ _block_info_flags=64,
73
+ _uses_block_alignment=True,
74
+ files=files,
75
+ ))
@@ -0,0 +1,145 @@
1
+ from pathlib import Path
2
+ from UnityPy.environment import Environment
3
+ from UnityPy.files import SerializedFile, ObjectReader
4
+ from UnityPy.classes import GameObject, Transform, Object, BuildSettings
5
+ from UnityPy.enums import ClassIDType
6
+ from collections.abc import Iterator
7
+
8
+
9
+ class Fake(object):
10
+ def __init__(self, _class, **kwargs):
11
+ self.__class__ = _class
12
+ self.__dict__.update(kwargs)
13
+
14
+ def load_bundle(path: Path | str):
15
+ if isinstance(path, Path):
16
+ path = str(path)
17
+ return Environment(path).file
18
+
19
+
20
+ def load_scenes(project: Path, level_names: list[str], scene_map: dict[str, str]) -> list[SerializedFile]:
21
+ paths = [str(project.joinpath(f"level{scene_map[name]}")) for name in level_names]
22
+
23
+ env = Environment()
24
+ env.load_file(str(project.joinpath("globalgamemanagers.assets")))
25
+ for i, (path, name) in enumerate(zip(paths, level_names)):
26
+ print(f"Loading {i + 1}/{len(paths)} [{name}] ", end="\r")
27
+ env.load_file(path)
28
+ print()
29
+ return [env.files[path] for path in paths]
30
+
31
+ def get_scene_names(globalgamemanagers: SerializedFile) -> list[str] | None:
32
+ for obj in globalgamemanagers.objects.values():
33
+ if obj.type == ClassIDType.BuildSettings:
34
+ settings: BuildSettings = obj.read()
35
+ return [Path(scene).stem for scene in settings.scenes]
36
+ return None
37
+
38
+
39
+ def get_root_object(file: SerializedFile, name: str) -> GameObject | None:
40
+ for root in get_root_objects(file):
41
+ if root.m_Name == name:
42
+ return root
43
+ return None
44
+
45
+ def get_child_names(obj: GameObject) -> list[str]:
46
+ return [child.read().m_GameObject.read().m_Name for child in obj.m_Transform.read().m_Children]
47
+
48
+ def get_root_objects(file: SerializedFile) -> Iterator[GameObject]:
49
+ for reader in get_root_object_readers(file):
50
+ yield reader.read()
51
+
52
+
53
+ def get_root_object_readers(file: SerializedFile) -> Iterator[ObjectReader]:
54
+ for obj in file.objects.values():
55
+ if obj.class_id == ClassIDType.Transform:
56
+ transform: Transform = obj.read()
57
+ if transform.m_Father.m_PathID != 0 and transform.m_Father.m_PathID not in transform.assets_file.objects:
58
+ # save_typetree only changes the underlying file, no what is accessible through python apparently
59
+ # so these are the newly unparented filtered objects
60
+ yield transform.m_GameObject.deref()
61
+ elif transform.m_Father.m_PathID == 0 or transform.m_Father.read() is None:
62
+ yield transform.m_GameObject.deref()
63
+
64
+
65
+ def components_in_children(obj: GameObject, *, ty: ClassIDType | None = None) -> Iterator[Object]:
66
+ for c in obj.m_Components:
67
+ c: ObjectReader = c.deref()
68
+ if ty is None or c.type == ty:
69
+ yield c.read()
70
+
71
+ for child in obj.m_Transform.read().m_Children:
72
+ yield from components_in_children(child.read().m_GameObject.read(), ty=ty)
73
+
74
+
75
+ def get_path(obj: GameObject) -> str:
76
+ path = []
77
+
78
+ current = obj
79
+
80
+ while True:
81
+ path.append(current.m_Name)
82
+ father = current.m_Transform.read().m_Father
83
+ if father is None or father.path_id == 0:
84
+ break
85
+ current = father.read().m_GameObject.read()
86
+
87
+ path.reverse()
88
+ return "/".join(path)
89
+
90
+
91
+ def lookup_path(path: str, root_objs: list[GameObject]) -> Transform:
92
+ root, *rest = path.split("/")
93
+
94
+ root_obj = next(filter(lambda obj: obj.m_Name == root, root_objs), None)
95
+ assert root_obj is not None, (
96
+ f"Root object not found: {root} of {path} in {list(map(lambda x: x.m_Name, root_objs))}"
97
+ )
98
+
99
+ if not rest:
100
+ return root_obj.m_Transform.read()
101
+
102
+ return lookup_path_in(path, rest, root_obj.m_Transform.read())
103
+
104
+
105
+ def lookup_path_in(orig: str, path: list[str], current: Transform, report_errors=True):
106
+ segment, *rest = path
107
+
108
+ candidates = []
109
+ for c in current.m_Children:
110
+ child: Transform = c.read()
111
+
112
+ if child.m_GameObject.read().m_Name == segment:
113
+ candidates.append(child)
114
+
115
+ if len(candidates) == 0:
116
+ if not report_errors:
117
+ return None
118
+ raise Exception("todo")
119
+ elif len(candidates) > 1:
120
+ if rest:
121
+ candidates = list(flatten([lookup_path_in(orig, rest, candidate, False) for candidate in candidates]))
122
+
123
+ if len(candidates) == 1:
124
+ return candidates[0]
125
+ if report_errors:
126
+ print(f"found {len(candidates)} candidates for '{orig}', choosing first")
127
+ return candidates[0]
128
+ else:
129
+ return candidates
130
+ else:
131
+ candidate = candidates[0]
132
+
133
+ if not rest:
134
+ return candidate
135
+ return lookup_path_in(orig, rest, candidate, report_errors)
136
+
137
+
138
+ def flatten(nested):
139
+ for item in nested:
140
+ if item is None:
141
+ pass
142
+ elif isinstance(item, list):
143
+ yield from nested
144
+ else:
145
+ yield item
@@ -0,0 +1,46 @@
1
+ Metadata-Version: 2.4
2
+ Name: unity-scene-repacker
3
+ Version: 0.1.0
4
+ Summary: Add your description here
5
+ Author-email: Jakob Hellermann <jakob.hellermann@protonmail.com>
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-Expression: MIT
9
+ License-File: LICENSE.md
10
+ Requires-Dist: UnityPy==1.22.5
11
+ Project-URL: homepage, https://github.com/jakobhellermann/unity-scene-repacker
12
+
13
+ # unity-scene-repacker
14
+
15
+ When modding a unity game, you often want to `Instantiate` objects from a scene that isn't loaded.
16
+ One solution for this is to load all the scenes you're interested in at startup, copy the gameobjects somewhere and unload the scene.
17
+ This works, but is slow and memory intensive.
18
+
19
+ This project lets you read scenes from the distributed game, take only what you need, and package those objects into an [AssetBundle](https://docs.unity3d.com/Manual/AssetBundlesIntro.html) that you can load at runtime.
20
+
21
+ ## Usage
22
+
23
+ ```json
24
+ monsters.json
25
+ {
26
+ "A6_S3_Tutorial_And_SecretBoss_Remake": [
27
+ "A6_S3/Room/Prefab/Enemy/StealthGameMonster_RunningHog (3)",
28
+ "A6_S3/Room/Prefab/Enemy_2/\u5f29\u83c1\u82f1"
29
+ ],
30
+ "A0_S7_CaveReturned": [
31
+ "A0_S7/Room/StealthGameMonster_TutorialDummyNonAttack"
32
+ ],
33
+ "AG_SG1": [
34
+ "AG_SG1/Room/Shield Crawler Spawner \u4e1f\u91d1\u96fb\u87f2"
35
+ ],
36
+ ...
37
+ }
38
+ ```
39
+
40
+ ```sh
41
+ uv run src/unity_scene_repacker/cli.py \
42
+ --preloads ninesols/bosses.json \
43
+ --output out/preloads.bundle \
44
+ --game-dir '~/.local/share/Steam/steamapps/common/Nine Sols/NineSols_Data/'
45
+ ```
46
+
@@ -0,0 +1,11 @@
1
+ unity_scene_repacker/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ unity_scene_repacker/cli.py,sha256=uOgwYkDi_nwJjWbcplCiVrgFKNBrLy176PRJAmVd4hs,3355
3
+ unity_scene_repacker/prune.py,sha256=BO7AhiRnao64XeWs9vcWYPFmfQvHDN-8idQPWaIEpvg,2741
4
+ unity_scene_repacker/repack.py,sha256=y_Qc423FmSNagqePxbIJxbtJ-JuyNvY-UL3MWjcbpC4,2594
5
+ unity_scene_repacker/utils.py,sha256=oazgNhPrrbSPuqZSNZb1rtOsewcWX-3wz-4eSKD7sYY,4922
6
+ unity_scene_repacker/data/empty_scene_bundle.unity3d,sha256=-XA1xCPGEFzkcI6pCRnP93FJgRigFLH48MP7_S499RE,18480
7
+ unity_scene_repacker-0.1.0.dist-info/entry_points.txt,sha256=H7jvGPA4Ik4UMoaeYGQpWM4cqvpomSEk-0h2RUv25eg,70
8
+ unity_scene_repacker-0.1.0.dist-info/licenses/LICENSE.md,sha256=8KJTqj6jREvqqXgntgh7OTLcJ9kflY4Zs0lGiCBS5N8,1073
9
+ unity_scene_repacker-0.1.0.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
10
+ unity_scene_repacker-0.1.0.dist-info/METADATA,sha256=CrpYiPFpqAVcV0SRS_E9AQeHSro83B2z4E5OJTvX6Ys,1535
11
+ unity_scene_repacker-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: flit 3.12.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ unity-asset-repacker=unity_scene_repacker.cli:main
3
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jakob Hellermann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.