unity_scene_repacker 0.0.0__tar.gz → 0.1.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.
- unity_scene_repacker-0.1.0/PKG-INFO +46 -0
- {unity_scene_repacker-0.0.0 → unity_scene_repacker-0.1.0}/README.md +3 -17
- unity_scene_repacker-0.1.0/pyproject.toml +32 -0
- unity_scene_repacker-0.1.0/src/unity_scene_repacker/__init__.py +0 -0
- unity_scene_repacker-0.1.0/src/unity_scene_repacker/cli.py +99 -0
- unity_scene_repacker-0.1.0/src/unity_scene_repacker/data/empty_scene_bundle.unity3d +0 -0
- {unity_scene_repacker-0.0.0/src → unity_scene_repacker-0.1.0/src/unity_scene_repacker}/prune.py +25 -21
- {unity_scene_repacker-0.0.0/src → unity_scene_repacker-0.1.0/src/unity_scene_repacker}/repack.py +11 -7
- {unity_scene_repacker-0.0.0/src → unity_scene_repacker-0.1.0/src/unity_scene_repacker}/utils.py +45 -25
- unity_scene_repacker-0.0.0/PKG-INFO +0 -5
- unity_scene_repacker-0.0.0/pyproject.toml +0 -6
- unity_scene_repacker-0.0.0/setup.cfg +0 -4
- unity_scene_repacker-0.0.0/src/cli.py +0 -82
- unity_scene_repacker-0.0.0/src/unity_scene_repacker.egg-info/PKG-INFO +0 -5
- unity_scene_repacker-0.0.0/src/unity_scene_repacker.egg-info/SOURCES.txt +0 -11
- unity_scene_repacker-0.0.0/src/unity_scene_repacker.egg-info/dependency_links.txt +0 -1
- unity_scene_repacker-0.0.0/src/unity_scene_repacker.egg-info/top_level.txt +0 -5
- {unity_scene_repacker-0.0.0 → unity_scene_repacker-0.1.0}/LICENSE.md +0 -0
|
@@ -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
|
+
|
|
@@ -1,26 +1,13 @@
|
|
|
1
1
|
# unity-scene-repacker
|
|
2
2
|
|
|
3
3
|
When modding a unity game, you often want to `Instantiate` objects from a scene that isn't loaded.
|
|
4
|
-
One solution for this
|
|
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
5
|
This works, but is slow and memory intensive.
|
|
6
6
|
|
|
7
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
8
|
|
|
9
9
|
## Usage
|
|
10
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
11
|
```json
|
|
25
12
|
monsters.json
|
|
26
13
|
{
|
|
@@ -39,9 +26,8 @@ monsters.json
|
|
|
39
26
|
```
|
|
40
27
|
|
|
41
28
|
```sh
|
|
42
|
-
|
|
43
|
-
--
|
|
44
|
-
--preloads ninesols/monsters.json \
|
|
29
|
+
uv run src/unity_scene_repacker/cli.py \
|
|
30
|
+
--preloads ninesols/bosses.json \
|
|
45
31
|
--output out/preloads.bundle \
|
|
46
32
|
--game-dir '~/.local/share/Steam/steamapps/common/Nine Sols/NineSols_Data/'
|
|
47
33
|
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "unity-scene-repacker"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Add your description here"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Jakob Hellermann", email = "jakob.hellermann@protonmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = ["UnityPy==1.22.5"]
|
|
11
|
+
license = "MIT"
|
|
12
|
+
|
|
13
|
+
[project.urls]
|
|
14
|
+
homepage = "https://github.com/jakobhellermann/unity-scene-repacker"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
unity-asset-repacker = "unity_scene_repacker.cli:main"
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["flit_core >=3.12,<4"]
|
|
22
|
+
build-backend = "flit_core.buildapi"
|
|
23
|
+
|
|
24
|
+
[tool.flit.sdist]
|
|
25
|
+
include = ["src/unity_scene_repacker/data/*"]
|
|
26
|
+
|
|
27
|
+
[tool.ruff]
|
|
28
|
+
line-length = 120
|
|
29
|
+
|
|
30
|
+
[tool.pyright]
|
|
31
|
+
venvPath = "."
|
|
32
|
+
venv = ".venv"
|
|
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()
|
|
Binary file
|
{unity_scene_repacker-0.0.0/src → unity_scene_repacker-0.1.0/src/unity_scene_repacker}/prune.py
RENAMED
|
@@ -1,42 +1,46 @@
|
|
|
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
1
|
from collections import deque
|
|
6
2
|
from collections.abc import Iterator
|
|
7
|
-
from utils import lookup_path, get_root_objects
|
|
8
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
9
|
|
|
10
|
-
|
|
10
|
+
|
|
11
|
+
def prune(scene: SerializedFile, keep_paths: list[str], always_include: list[ClassIDType] = None) -> list[Transform]:
|
|
11
12
|
root_objs = list(get_root_objects(scene))
|
|
13
|
+
if len(root_objs) == 0:
|
|
14
|
+
print(scene)
|
|
12
15
|
keep_new_root = [lookup_path(keep, root_objs) for keep in keep_paths]
|
|
13
|
-
reachable = [keep.
|
|
14
|
-
prune_reachable(scene, reachable)
|
|
16
|
+
reachable = [keep.object_reader for keep in keep_new_root]
|
|
17
|
+
prune_reachable(scene, reachable, always_include)
|
|
15
18
|
|
|
16
19
|
for keep in keep_new_root:
|
|
17
|
-
keep = keep.
|
|
18
|
-
print(keep)
|
|
20
|
+
keep = keep.object_reader
|
|
19
21
|
tt = keep.read_typetree()
|
|
20
22
|
tt["m_Father"] = {"m_FileID": 0, "m_PathID": 0}
|
|
21
23
|
keep.save_typetree(tt)
|
|
22
24
|
|
|
23
|
-
|
|
24
25
|
return keep_new_root
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
|
|
28
|
+
def prune_reachable(scene: SerializedFile, reachable: list[ObjectReader[Transform]], always_include: list[ClassIDType] = None):
|
|
27
29
|
include: set[int] = set()
|
|
28
|
-
queue: deque[ObjectReader] = deque(reachable)
|
|
30
|
+
queue: deque[ObjectReader[Object] | PPtr[Object]] = deque(reachable)
|
|
29
31
|
|
|
30
32
|
while queue:
|
|
31
33
|
node = queue.popleft()
|
|
32
34
|
include.add(node.path_id)
|
|
33
35
|
|
|
34
|
-
for
|
|
35
|
-
if
|
|
36
|
-
queue.append(
|
|
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)
|
|
37
42
|
|
|
38
|
-
|
|
39
|
-
new_objects = {key: old_objects[key] for key in include}
|
|
43
|
+
new_objects = {key: scene.objects[key] for key in include}
|
|
40
44
|
|
|
41
45
|
scene.objects = dict(sorted(new_objects.items()))
|
|
42
46
|
|
|
@@ -53,9 +57,9 @@ def prune_reachable(scene: SerializedFile, reachable: list):
|
|
|
53
57
|
scene.types = new_types
|
|
54
58
|
|
|
55
59
|
|
|
56
|
-
def iterate_visible(obj: ObjectReader) -> Iterator[Object]:
|
|
57
|
-
if isinstance(obj, PPtr):
|
|
58
|
-
|
|
60
|
+
def iterate_visible(obj: ObjectReader) -> Iterator[PPtr[Object]]:
|
|
61
|
+
# if isinstance(obj, PPtr):
|
|
62
|
+
# obj = obj.get_obj()
|
|
59
63
|
|
|
60
64
|
if obj.type == ClassIDType.GameObject:
|
|
61
65
|
go: GameObject = obj.read()
|
{unity_scene_repacker-0.0.0/src → unity_scene_repacker-0.1.0/src/unity_scene_repacker}/repack.py
RENAMED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import UnityPy
|
|
2
2
|
from UnityPy.environment import SerializedFile
|
|
3
|
-
from UnityPy.files import BundleFile
|
|
3
|
+
from UnityPy.files import BundleFile, ObjectReader
|
|
4
4
|
from UnityPy.enums import ArchiveFlags
|
|
5
5
|
from UnityPy.classes import AssetBundle
|
|
6
6
|
|
|
7
|
-
from utils import Fake
|
|
7
|
+
from unity_scene_repacker.utils import Fake
|
|
8
|
+
from typing import cast
|
|
9
|
+
|
|
10
|
+
import importlib.resources
|
|
8
11
|
|
|
9
12
|
|
|
10
13
|
def repack_scene_bundle(scenes: dict[str, SerializedFile]) -> BundleFile:
|
|
11
|
-
|
|
14
|
+
with importlib.resources.open_binary("unity_scene_repacker.data", "empty_scene_bundle.unity3d") as f:
|
|
15
|
+
emptybundle_bin = f.read()
|
|
12
16
|
|
|
13
|
-
emptybundle: BundleFile = UnityPy.load(
|
|
17
|
+
emptybundle: BundleFile = UnityPy.load(emptybundle_bin).file
|
|
14
18
|
shared_assets: SerializedFile = emptybundle.files["BuildPlayer-EmptyScene.sharedAssets"]
|
|
15
19
|
|
|
16
|
-
assetbundle_meta: AssetBundle = shared_assets.objects[2]
|
|
20
|
+
assetbundle_meta: ObjectReader[AssetBundle] = shared_assets.objects[2]
|
|
17
21
|
assetbundle_meta.save_typetree(
|
|
18
22
|
{
|
|
19
23
|
"m_Name": "scenebundle",
|
|
@@ -58,7 +62,7 @@ def repack_scene_bundle(scenes: dict[str, SerializedFile]) -> BundleFile:
|
|
|
58
62
|
files[f"BuildPlayer-{name}.sharedAssets"] = scene_shared_assets
|
|
59
63
|
files[f"BuildPlayer-{name}"] = scene
|
|
60
64
|
|
|
61
|
-
return Fake(
|
|
65
|
+
return cast(BundleFile, Fake(
|
|
62
66
|
BundleFile,
|
|
63
67
|
signature="UnityFS",
|
|
64
68
|
version=8,
|
|
@@ -68,4 +72,4 @@ def repack_scene_bundle(scenes: dict[str, SerializedFile]) -> BundleFile:
|
|
|
68
72
|
_block_info_flags=64,
|
|
69
73
|
_uses_block_alignment=True,
|
|
70
74
|
files=files,
|
|
71
|
-
)
|
|
75
|
+
))
|
{unity_scene_repacker-0.0.0/src → unity_scene_repacker-0.1.0/src/unity_scene_repacker}/utils.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
2
|
from UnityPy.environment import Environment
|
|
3
3
|
from UnityPy.files import SerializedFile, ObjectReader
|
|
4
|
-
from UnityPy.classes import GameObject, Transform, Object
|
|
4
|
+
from UnityPy.classes import GameObject, Transform, Object, BuildSettings
|
|
5
5
|
from UnityPy.enums import ClassIDType
|
|
6
6
|
from collections.abc import Iterator
|
|
7
7
|
|
|
@@ -11,8 +11,7 @@ class Fake(object):
|
|
|
11
11
|
self.__class__ = _class
|
|
12
12
|
self.__dict__.update(kwargs)
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
def load_bundle(path):
|
|
14
|
+
def load_bundle(path: Path | str):
|
|
16
15
|
if isinstance(path, Path):
|
|
17
16
|
path = str(path)
|
|
18
17
|
return Environment(path).file
|
|
@@ -24,11 +23,27 @@ def load_scenes(project: Path, level_names: list[str], scene_map: dict[str, str]
|
|
|
24
23
|
env = Environment()
|
|
25
24
|
env.load_file(str(project.joinpath("globalgamemanagers.assets")))
|
|
26
25
|
for i, (path, name) in enumerate(zip(paths, level_names)):
|
|
27
|
-
print(f"Loading {i+1}/{len(paths)} [{name}] ", end="\r")
|
|
26
|
+
print(f"Loading {i + 1}/{len(paths)} [{name}] ", end="\r")
|
|
28
27
|
env.load_file(path)
|
|
29
28
|
print()
|
|
30
29
|
return [env.files[path] for path in paths]
|
|
31
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]
|
|
32
47
|
|
|
33
48
|
def get_root_objects(file: SerializedFile) -> Iterator[GameObject]:
|
|
34
49
|
for reader in get_root_object_readers(file):
|
|
@@ -37,32 +52,35 @@ def get_root_objects(file: SerializedFile) -> Iterator[GameObject]:
|
|
|
37
52
|
|
|
38
53
|
def get_root_object_readers(file: SerializedFile) -> Iterator[ObjectReader]:
|
|
39
54
|
for obj in file.objects.values():
|
|
40
|
-
if obj.class_id ==
|
|
55
|
+
if obj.class_id == ClassIDType.Transform:
|
|
41
56
|
transform: Transform = obj.read()
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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()
|
|
45
63
|
|
|
46
64
|
|
|
47
|
-
def components_in_children(
|
|
48
|
-
for c in
|
|
49
|
-
c: ObjectReader = c.
|
|
50
|
-
if
|
|
51
|
-
yield c
|
|
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()
|
|
52
70
|
|
|
53
|
-
for child in
|
|
54
|
-
yield from components_in_children(child.read().m_GameObject.read(),
|
|
71
|
+
for child in obj.m_Transform.read().m_Children:
|
|
72
|
+
yield from components_in_children(child.read().m_GameObject.read(), ty=ty)
|
|
55
73
|
|
|
56
74
|
|
|
57
|
-
def
|
|
75
|
+
def get_path(obj: GameObject) -> str:
|
|
58
76
|
path = []
|
|
59
77
|
|
|
60
|
-
current =
|
|
78
|
+
current = obj
|
|
61
79
|
|
|
62
80
|
while True:
|
|
63
|
-
path.append(current.
|
|
64
|
-
father = current.m_Transform.read().m_Father
|
|
65
|
-
if father is None:
|
|
81
|
+
path.append(current.m_Name)
|
|
82
|
+
father = current.m_Transform.read().m_Father
|
|
83
|
+
if father is None or father.path_id == 0:
|
|
66
84
|
break
|
|
67
85
|
current = father.read().m_GameObject.read()
|
|
68
86
|
|
|
@@ -73,8 +91,10 @@ def path(object: GameObject) -> str:
|
|
|
73
91
|
def lookup_path(path: str, root_objs: list[GameObject]) -> Transform:
|
|
74
92
|
root, *rest = path.split("/")
|
|
75
93
|
|
|
76
|
-
root_obj = next(filter(lambda obj: obj.
|
|
77
|
-
assert root_obj is not None
|
|
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
|
+
)
|
|
78
98
|
|
|
79
99
|
if not rest:
|
|
80
100
|
return root_obj.m_Transform.read()
|
|
@@ -86,10 +106,10 @@ def lookup_path_in(orig: str, path: list[str], current: Transform, report_errors
|
|
|
86
106
|
segment, *rest = path
|
|
87
107
|
|
|
88
108
|
candidates = []
|
|
89
|
-
for
|
|
90
|
-
child: Transform =
|
|
109
|
+
for c in current.m_Children:
|
|
110
|
+
child: Transform = c.read()
|
|
91
111
|
|
|
92
|
-
if child.m_GameObject.read().
|
|
112
|
+
if child.m_GameObject.read().m_Name == segment:
|
|
93
113
|
candidates.append(child)
|
|
94
114
|
|
|
95
115
|
if len(candidates) == 0:
|
|
@@ -1,82 +0,0 @@
|
|
|
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)
|
|
@@ -1,11 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
|
File without changes
|