unity_scene_repacker 0.0.0__tar.gz

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.
@@ -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.
@@ -0,0 +1,5 @@
1
+ Metadata-Version: 2.4
2
+ Name: unity_scene_repacker
3
+ Version: 0.0.0
4
+ License-File: LICENSE.md
5
+ Dynamic: license-file
@@ -0,0 +1,47 @@
1
+ # unity-scene-repacker
2
+
3
+ When modding a unity game, you often want to `Instantiate` objects from a scene that isn't loaded.
4
+ One solution for this, is to load all the scenes you're interested in at startup, copy the gameobjects somewhere and unload the scene.
5
+ This works, but is slow and memory intensive.
6
+
7
+ 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.
8
+
9
+ ## Usage
10
+
11
+ ```json
12
+ scenes.json
13
+ {
14
+ "Logo": 0,
15
+ "TitleScreenMenu": 1,
16
+ "A0_S1_intro_morning": 2,
17
+ "A0_S2_intro_night": 3,
18
+ "A0_S3_altar": 4,
19
+ "A0_S4_tutorial": 5,
20
+ ...
21
+ }
22
+ ```
23
+
24
+ ```json
25
+ monsters.json
26
+ {
27
+ "A6_S3_Tutorial_And_SecretBoss_Remake": [
28
+ "A6_S3/Room/Prefab/Enemy/StealthGameMonster_RunningHog (3)",
29
+ "A6_S3/Room/Prefab/Enemy_2/\u5f29\u83c1\u82f1"
30
+ ],
31
+ "A0_S7_CaveReturned": [
32
+ "A0_S7/Room/StealthGameMonster_TutorialDummyNonAttack"
33
+ ],
34
+ "AG_SG1": [
35
+ "AG_SG1/Room/Shield Crawler Spawner \u4e1f\u91d1\u96fb\u87f2"
36
+ ],
37
+ ...
38
+ }
39
+ ```
40
+
41
+ ```sh
42
+ python src/cli.py \
43
+ --scene-defs ninesols/scenes.json \
44
+ --preloads ninesols/monsters.json \
45
+ --output out/preloads.bundle \
46
+ --game-dir '~/.local/share/Steam/steamapps/common/Nine Sols/NineSols_Data/'
47
+ ```
@@ -0,0 +1,6 @@
1
+ [tool.ruff]
2
+ line-length = 120
3
+
4
+ [tool.pyright]
5
+ venvPath = "."
6
+ venv = ".venv"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,82 @@
1
+ import UnityPy
2
+ from UnityPy.environment import Environment
3
+ from pathlib import Path
4
+ import json
5
+ import argparse
6
+
7
+ from repack import repack_scene_bundle
8
+ from prune import prune
9
+ from utils import get_root_objects, get_root_object_readers
10
+
11
+ parser = argparse.ArgumentParser()
12
+ parser.add_argument("--game-dir", help="game directory where the levels are, i.e. Game/Game_Data", required=True)
13
+ parser.add_argument(
14
+ "--scene-defs", help="path to json file, containing a map from scene name to build index", required=True
15
+ )
16
+ parser.add_argument(
17
+ "--preloads",
18
+ help="path to json file, containg map from scene name to list of gameobject paths to include in the assetbundle",
19
+ required=True,
20
+ )
21
+ parser.add_argument(
22
+ "-o",
23
+ "--output",
24
+ help="path to json file, containg map from scene name to list of gameobject paths to include in the assetbundle",
25
+ default="preloads.bundle",
26
+ )
27
+ parser.add_argument(
28
+ "--disable",
29
+ action=argparse.BooleanOptionalAction,
30
+ )
31
+ args = parser.parse_args()
32
+
33
+ scene_map = json.load(open(args.scene_defs, "r"))
34
+ monster_preloads = json.load(open(args.preloads, "r"))
35
+ # keys = ["A9_S1_Remake_4wei", "A0_S4_tutorial"]
36
+ # monster_preloads = {key: monster_preloads[key] for key in monster_preloads if key in keys}
37
+
38
+ out_path = Path(args.output)
39
+ project = Path(args.game_dir)
40
+
41
+ level_names = [name for name, _ in monster_preloads.items()]
42
+ paths = [str(project.joinpath(f"level{scene_map[name]}")) for name in level_names]
43
+
44
+ env = Environment()
45
+ for i, (path, name) in enumerate(zip(paths, level_names)):
46
+ print(f"Loading {i+1}/{len(paths)} [{name}] ", end="\r")
47
+ env.load_file(path)
48
+ print()
49
+ serialized_files = [env.files[path] for path in paths]
50
+
51
+
52
+ def rename(name: str) -> str:
53
+ name, *rest = name.split(" (")
54
+ print(name, rest)
55
+ return name
56
+
57
+
58
+ for level_name, file in zip(level_names, serialized_files):
59
+ print(f"Pruning {i+1}/{len(paths)} [{name}] ", end="\r")
60
+ level_monsters = monster_preloads[level_name]
61
+ prune(file, level_monsters)
62
+
63
+ for obj in get_root_object_readers(file):
64
+ tt = obj.read_typetree()
65
+ tt["m_Name"] = rename(tt["m_Name"])
66
+ if args.disable:
67
+ tt["m_IsActive"] = False
68
+ obj.save_typetree(tt)
69
+
70
+
71
+ new_bundle = repack_scene_bundle(dict(zip(level_names, serialized_files)))
72
+
73
+ out_path.parent.mkdir(parents=True, exist_ok=True)
74
+ with open(out_path, "wb") as f:
75
+ f.write(new_bundle.save())
76
+
77
+ for name, file in Environment(str(out_path)).file.files.items():
78
+ if name.endswith("sharedAssets"):
79
+ continue
80
+
81
+ for root in get_root_objects(file):
82
+ print(root.name)
@@ -0,0 +1,75 @@
1
+ from UnityPy.classes.SpriteAtlas import SpriteAtlas
2
+ from UnityPy.files import SerializedFile, ObjectReader
3
+ from UnityPy.classes import Object, GameObject, Transform, RectTransform, PPtr
4
+ from UnityPy.enums import ClassIDType
5
+ from collections import deque
6
+ from collections.abc import Iterator
7
+ from utils import lookup_path, get_root_objects
8
+
9
+
10
+ def prune(scene: SerializedFile, keep_paths: list[str]) -> list[Transform]:
11
+ root_objs = list(get_root_objects(scene))
12
+ keep_new_root = [lookup_path(keep, root_objs) for keep in keep_paths]
13
+ reachable = [keep.reader for keep in keep_new_root]
14
+ prune_reachable(scene, reachable)
15
+
16
+ for keep in keep_new_root:
17
+ keep = keep.reader
18
+ print(keep)
19
+ tt = keep.read_typetree()
20
+ tt["m_Father"] = {"m_FileID": 0, "m_PathID": 0}
21
+ keep.save_typetree(tt)
22
+
23
+
24
+ return keep_new_root
25
+
26
+ def prune_reachable(scene: SerializedFile, reachable: list):
27
+ include: set[int] = set()
28
+ queue: deque[ObjectReader] = deque(reachable)
29
+
30
+ while queue:
31
+ node = queue.popleft()
32
+ include.add(node.path_id)
33
+
34
+ for reachable in iterate_visible(node):
35
+ if reachable.path_id not in include:
36
+ queue.append(reachable)
37
+
38
+ old_objects = scene.objects
39
+ new_objects = {key: old_objects[key] for key in include}
40
+
41
+ scene.objects = dict(sorted(new_objects.items()))
42
+
43
+ # remove unused types
44
+ type_index = 0
45
+ type_mapping = {}
46
+ new_types = []
47
+ for used_type in set(obj.type_id for obj in scene.objects.values()):
48
+ type_mapping[used_type] = type_index
49
+ new_types.append(scene.types[used_type])
50
+ type_index += 1
51
+ for obj in scene.objects.values():
52
+ obj.type_id = type_mapping[obj.type_id]
53
+ scene.types = new_types
54
+
55
+
56
+ def iterate_visible(obj: ObjectReader) -> Iterator[Object]:
57
+ if isinstance(obj, PPtr):
58
+ obj = obj.get_obj()
59
+
60
+ if obj.type == ClassIDType.GameObject:
61
+ go: GameObject = obj.read()
62
+ yield from go.m_Components
63
+ elif obj.type == ClassIDType.Canvas:
64
+ pass
65
+ elif obj.type == ClassIDType.Transform:
66
+ transform: Transform = obj.read()
67
+ yield transform.m_GameObject
68
+ yield from transform.m_Children
69
+ elif obj.type == ClassIDType.RectTransform:
70
+ rect: RectTransform = obj.read()
71
+ yield rect.m_GameObject
72
+ yield from rect.m_Children
73
+ elif obj.type == ClassIDType.SpriteAtlas:
74
+ atlas: SpriteAtlas = obj.read()
75
+ yield from atlas.m_PackedSprites
@@ -0,0 +1,71 @@
1
+ import UnityPy
2
+ from UnityPy.environment import SerializedFile
3
+ from UnityPy.files import BundleFile
4
+ from UnityPy.enums import ArchiveFlags
5
+ from UnityPy.classes import AssetBundle
6
+
7
+ from utils import Fake
8
+
9
+
10
+ def repack_scene_bundle(scenes: dict[str, SerializedFile]) -> BundleFile:
11
+ emptybundle_path = "in/empty_scene_bundle"
12
+
13
+ emptybundle: BundleFile = UnityPy.load(emptybundle_path).file
14
+ shared_assets: SerializedFile = emptybundle.files["BuildPlayer-EmptyScene.sharedAssets"]
15
+
16
+ assetbundle_meta: AssetBundle = shared_assets.objects[2]
17
+ assetbundle_meta.save_typetree(
18
+ {
19
+ "m_Name": "scenebundle",
20
+ "m_PreloadTable": [],
21
+ "m_Container": [
22
+ (
23
+ f"Assets/SceneBundle/{name}.unity",
24
+ {
25
+ "preloadIndex": 0,
26
+ "preloadSize": 0,
27
+ "asset": {"m_FileID": 0, "m_PathID": 0},
28
+ },
29
+ )
30
+ for name in scenes
31
+ ],
32
+ "m_MainAsset": {
33
+ "preloadIndex": 0,
34
+ "preloadSize": 0,
35
+ "asset": {"m_FileID": 0, "m_PathID": 0},
36
+ },
37
+ "m_RuntimeCompatibility": 1,
38
+ "m_AssetBundleName": "",
39
+ "m_Dependencies": [],
40
+ "m_IsStreamedSceneAssetBundle": True,
41
+ "m_ExplicitDataLayout": 0,
42
+ "m_PathFlags": 7,
43
+ "m_SceneHashes": [],
44
+ }
45
+ )
46
+
47
+ files = {}
48
+ # first = True
49
+ for name, scene in scenes.items():
50
+ scene_shared_assets = shared_assets
51
+ # if not first:
52
+ # scene_shared_assets = copy.copy(shared_assets)
53
+ # scene_shared_assets.objects = copy.copy(scene_shared_assets.objects)
54
+ # assert scene_shared_assets.objects.pop(2).class_id == 142 # AssetBundle
55
+ # first = False
56
+
57
+ scene.flags = 4
58
+ files[f"BuildPlayer-{name}.sharedAssets"] = scene_shared_assets
59
+ files[f"BuildPlayer-{name}"] = scene
60
+
61
+ return Fake(
62
+ BundleFile,
63
+ signature="UnityFS",
64
+ version=8,
65
+ version_player="5.x.x",
66
+ version_engine="2022.3.18f1",
67
+ dataflags=ArchiveFlags.BlocksAndDirectoryInfoCombined | ArchiveFlags.BlockInfoNeedPaddingAtStart | 3,
68
+ _block_info_flags=64,
69
+ _uses_block_alignment=True,
70
+ files=files,
71
+ )
@@ -0,0 +1,5 @@
1
+ Metadata-Version: 2.4
2
+ Name: unity_scene_repacker
3
+ Version: 0.0.0
4
+ License-File: LICENSE.md
5
+ Dynamic: license-file
@@ -0,0 +1,11 @@
1
+ LICENSE.md
2
+ README.md
3
+ pyproject.toml
4
+ src/cli.py
5
+ src/prune.py
6
+ src/repack.py
7
+ src/utils.py
8
+ src/unity_scene_repacker.egg-info/PKG-INFO
9
+ src/unity_scene_repacker.egg-info/SOURCES.txt
10
+ src/unity_scene_repacker.egg-info/dependency_links.txt
11
+ src/unity_scene_repacker.egg-info/top_level.txt
@@ -0,0 +1,5 @@
1
+ cli
2
+ prune
3
+ repack
4
+ unity_scene_repacker
5
+ utils
@@ -0,0 +1,125 @@
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
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
+
15
+ def load_bundle(path):
16
+ if isinstance(path, Path):
17
+ path = str(path)
18
+ return Environment(path).file
19
+
20
+
21
+ def load_scenes(project: Path, level_names: list[str], scene_map: dict[str, str]) -> list[SerializedFile]:
22
+ paths = [str(project.joinpath(f"level{scene_map[name]}")) for name in level_names]
23
+
24
+ env = Environment()
25
+ env.load_file(str(project.joinpath("globalgamemanagers.assets")))
26
+ for i, (path, name) in enumerate(zip(paths, level_names)):
27
+ print(f"Loading {i+1}/{len(paths)} [{name}] ", end="\r")
28
+ env.load_file(path)
29
+ print()
30
+ return [env.files[path] for path in paths]
31
+
32
+
33
+ def get_root_objects(file: SerializedFile) -> Iterator[GameObject]:
34
+ for reader in get_root_object_readers(file):
35
+ yield reader.read()
36
+
37
+
38
+ def get_root_object_readers(file: SerializedFile) -> Iterator[ObjectReader]:
39
+ for obj in file.objects.values():
40
+ if obj.class_id == 4:
41
+ transform: Transform = obj.read()
42
+ parent = transform.m_Father.get_obj()
43
+ if parent is None:
44
+ yield transform.m_GameObject.get_obj()
45
+
46
+
47
+ def components_in_children(object: GameObject, *, type: ClassIDType = None) -> Iterator[Object]:
48
+ for c in object.m_Components:
49
+ c: ObjectReader = c.get_obj()
50
+ if type is None or c.type == type:
51
+ yield c
52
+
53
+ for child in object.m_Transform.read().m_Children:
54
+ yield from components_in_children(child.read().m_GameObject.read(), type=type)
55
+
56
+
57
+ def path(object: GameObject) -> str:
58
+ path = []
59
+
60
+ current = object
61
+
62
+ while True:
63
+ path.append(current.name)
64
+ father = current.m_Transform.read().m_Father.get_obj()
65
+ if father is None:
66
+ break
67
+ current = father.read().m_GameObject.read()
68
+
69
+ path.reverse()
70
+ return "/".join(path)
71
+
72
+
73
+ def lookup_path(path: str, root_objs: list[GameObject]) -> Transform:
74
+ root, *rest = path.split("/")
75
+
76
+ root_obj = next(filter(lambda obj: obj.name == root, root_objs), None)
77
+ assert root_obj is not None
78
+
79
+ if not rest:
80
+ return root_obj.m_Transform.read()
81
+
82
+ return lookup_path_in(path, rest, root_obj.m_Transform.read())
83
+
84
+
85
+ def lookup_path_in(orig: str, path: list[str], current: Transform, report_errors=True):
86
+ segment, *rest = path
87
+
88
+ candidates = []
89
+ for child in current.m_Children:
90
+ child: Transform = child.read()
91
+
92
+ if child.m_GameObject.read().name == segment:
93
+ candidates.append(child)
94
+
95
+ if len(candidates) == 0:
96
+ if not report_errors:
97
+ return None
98
+ raise Exception("todo")
99
+ elif len(candidates) > 1:
100
+ if rest:
101
+ candidates = list(flatten([lookup_path_in(orig, rest, candidate, False) for candidate in candidates]))
102
+
103
+ if len(candidates) == 1:
104
+ return candidates[0]
105
+ if report_errors:
106
+ print(f"found {len(candidates)} candidates for '{orig}', choosing first")
107
+ return candidates[0]
108
+ else:
109
+ return candidates
110
+ else:
111
+ candidate = candidates[0]
112
+
113
+ if not rest:
114
+ return candidate
115
+ return lookup_path_in(orig, rest, candidate, report_errors)
116
+
117
+
118
+ def flatten(nested):
119
+ for item in nested:
120
+ if item is None:
121
+ pass
122
+ elif isinstance(item, list):
123
+ yield from nested
124
+ else:
125
+ yield item