unity_scene_repacker 3.0.0__tar.gz → 3.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.
Files changed (39) hide show
  1. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/.github/workflows/release.yml +8 -5
  2. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/Cargo.lock +3 -3
  3. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/Cargo.toml +1 -1
  4. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/PKG-INFO +1 -1
  5. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/README.md +24 -8
  6. unity_scene_repacker-3.1.0/cli/src/check_scripts.rs +194 -0
  7. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/src/lib.rs +47 -24
  8. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/docs/CHANGELOG.md +5 -1
  9. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/src/dependency_addressables.rs +33 -7
  10. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/src/lib.rs +1 -0
  11. unity_scene_repacker-3.1.0/src/script_check.rs +247 -0
  12. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/.clang-format +0 -0
  13. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/.gitignore +0 -0
  14. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/LICENSE.md +0 -0
  15. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/Cargo.toml +0 -0
  16. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/src/completion.rs +0 -0
  17. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/src/locate.rs +0 -0
  18. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/src/logger.rs +0 -0
  19. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/src/main.rs +0 -0
  20. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/src/py.rs +0 -0
  21. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/cli/src/utils.rs +0 -0
  22. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/docs/demo-cast.svg +0 -0
  23. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/docs/demo.cast +0 -0
  24. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/docs/release.md +0 -0
  25. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/examples/export_fsm.rs +0 -0
  26. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/examples/export_monobehaviour_typetrees.rs +0 -0
  27. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/examples/export_ns_monsters.rs +0 -0
  28. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/examples/hk.rs +0 -0
  29. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/examples/script_stats.rs +0 -0
  30. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/justfile +0 -0
  31. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/objects/hk-enemies.jsonc +0 -0
  32. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/objects/hk-in-silksong.jsonc +0 -0
  33. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/objects/hk-representative.jsonc +0 -0
  34. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/objects/ns-by-type.json +0 -0
  35. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/objects/ns-monsters.json +0 -0
  36. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/pyproject.toml +0 -0
  37. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/rustfmt.toml +0 -0
  38. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/src/merge_serialized.rs +0 -0
  39. {unity_scene_repacker-3.0.0 → unity_scene_repacker-3.1.0}/src/monobehaviour_typetree_export.rs +0 -0
@@ -104,6 +104,8 @@ jobs:
104
104
  runs-on: ubuntu-latest
105
105
  if: ${{ startsWith(github.ref, 'refs/tags/') }}
106
106
  needs: build
107
+ permissions:
108
+ id-token: write
107
109
 
108
110
  steps:
109
111
  - uses: actions/checkout@v7
@@ -122,10 +124,13 @@ jobs:
122
124
  - name: Pack NuGet package
123
125
  run: nuget pack ./bindings/nuget/unityscenerepacker.nuspec
124
126
 
127
+ - name: NuGet login
128
+ id: nuget-login
129
+ uses: NuGet/login@v1
130
+ with:
131
+ user: ${{ secrets.NUGET_USER }}
125
132
  - name: Push to NuGet
126
- env:
127
- NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
128
- run: nuget push unityscenerepacker.*.nupkg -SkipDuplicate -Source https://api.nuget.org/v3/index.json -ApiKey $NUGET_API_KEY
133
+ run: dotnet nuget push unityscenerepacker.*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ steps.nuget-login.outputs.NUGET_API_KEY }}
129
134
 
130
135
  release:
131
136
  name: Build & Publish PyPI wheels
@@ -150,8 +155,6 @@ jobs:
150
155
  - name: Publish to PyPI
151
156
  if: ${{ startsWith(github.ref, 'refs/tags/') }}
152
157
  uses: PyO3/maturin-action@v1
153
- env:
154
- MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
155
158
  with:
156
159
  command: upload
157
160
  args: --non-interactive --skip-existing wheels-*/*
@@ -81,7 +81,7 @@ checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5"
81
81
 
82
82
  [[package]]
83
83
  name = "bindings"
84
- version = "3.0.0"
84
+ version = "3.1.0"
85
85
  dependencies = [
86
86
  "anyhow",
87
87
  "indexmap",
@@ -1130,7 +1130,7 @@ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
1130
1130
 
1131
1131
  [[package]]
1132
1132
  name = "unity-scene-repacker"
1133
- version = "3.0.0"
1133
+ version = "3.1.0"
1134
1134
  dependencies = [
1135
1135
  "anyhow",
1136
1136
  "byteorder",
@@ -1150,7 +1150,7 @@ dependencies = [
1150
1150
 
1151
1151
  [[package]]
1152
1152
  name = "unity-scene-repacker-bin"
1153
- version = "3.0.0"
1153
+ version = "3.1.0"
1154
1154
  dependencies = [
1155
1155
  "anyhow",
1156
1156
  "clap",
@@ -25,7 +25,7 @@ serde_json = { version = "1.0", features = ["preserve_order"] }
25
25
  [workspace]
26
26
  members = [".", "cli"]
27
27
 
28
- package.version = "3.0.0"
28
+ package.version = "3.1.0"
29
29
  package.edition = "2024"
30
30
  package.license = "MIT"
31
31
  package.repository = "https://github.com/jakobhellermann/unity-scene-repacker"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: unity-scene-repacker
3
- Version: 3.0.0
3
+ Version: 3.1.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Topic :: Games/Entertainment
6
6
  License-File: LICENSE.md
@@ -243,6 +243,11 @@ public sealed class RepackedCatalog : IDisposable {
243
243
 
244
244
  ```
245
245
  Usage: unity-scene-repacker [OPTIONS] <--game-dir <GAME_DIR>|--steam-game <STEAM_GAME>>
246
+ unity-scene-repacker <COMMAND>
247
+
248
+ Commands:
249
+ check-scripts Check whether the scripts used in a repacked bundle exist with the same fields in another game
250
+ help Print this message or the help of the given subcommand(s)
246
251
 
247
252
  Options:
248
253
  -h, --help
@@ -261,7 +266,7 @@ Game options:
261
266
  Repack options:
262
267
  --scene-objects <SCENE_OBJECTS>
263
268
  Path to JSON file, containing a map of scene name to a list of gameobject paths to include
264
-
269
+
265
270
  {
266
271
  "Fungus1_12": [
267
272
  "simple_grass",
@@ -271,24 +276,24 @@ Repack options:
271
276
  "WhiteBench",
272
277
  ]
273
278
  }
274
-
279
+
275
280
 
276
281
  --extra-objects <EXTRA_OBJECTS>
277
282
  Path to JSON file, containing a map of C# type to monobehaviour names. Useful for scriptable objects etc., which do not exist in the transform hierarchy.
278
-
283
+
279
284
  {
280
285
  "FXDealerMaterialTag": ["[FXDealer] 0_YeeAttack _PostureDecrease"]
281
286
  }
282
-
287
+
283
288
 
284
289
  Output options:
285
290
  --mode <MODE>
286
- [default: scene]
287
-
288
291
  Possible values:
289
292
  - scene: A scene asset bundle, containing the original scenes filtered. Load using LoadScene
290
293
  - asset: An asset bundle containing individual assets you can load using LoadAsset. The objects are copied from the original level files
291
294
  - asset-shallow: An asset bundle containing individual assets you can load using LoadAsset. The bundle is completely empty, and only references the original game level files
295
+
296
+ [default: scene]
292
297
 
293
298
  --disable
294
299
  When true, all gameobjects in the scene will start out disabled
@@ -296,16 +301,27 @@ Output options:
296
301
  --compression <COMPRESSION>
297
302
  Compression level to apply
298
303
 
299
- [default: lzma]
300
-
301
304
  Possible values:
302
305
  - none
303
306
  - lzma
307
+ - lz4: Fast compression, larger output than lz4hc
304
308
  - lz4hc: Best compression at the cost of speed
309
+
310
+ [default: lz4]
305
311
 
306
312
  -o, --output <OUTPUT>
307
313
  [default: out.unity3d]
308
314
 
309
315
  --bundle-name <BUNDLE_NAME>
310
316
  Name to give the assetbundle. Should be unique for your game
317
+
318
+ --write-typetrees
319
+ When set, write the resulting bundle with typetree information embedded.
320
+
321
+ This lets unity load asset bundles for different unity versions, as long as they are similar enough. Larger version differences might not work due to format changes to e.g. shaders, materials and animations.
322
+
323
+ --emit-dependency-addressables
324
+ When enabled, the game files the bundle depends on get copied to deps/ next to the output, and an Addressables catalog <bundle name>.catalog.bin is written referencing the repacked objects and their dependencies.
325
+
326
+ The catalog loads bundles from the Addressables runtime property {UnitySceneRepacker.<bundle name>}. Implies --write-typetrees.
311
327
  ```
@@ -0,0 +1,194 @@
1
+ use std::io::IsTerminal as _;
2
+ use std::path::{Path, PathBuf};
3
+
4
+ use anyhow::{Context, Result};
5
+ use clap::Args;
6
+ use clap_complete::ArgValueCompleter;
7
+ use paris::{success, warn};
8
+ use rabex::tpk::TpkTypeTreeBlob;
9
+ use rabex::typetree::typetree_cache::sync::TypeTreeCache;
10
+ use rabex_env::Environment;
11
+ use rabex_env::resolver::GameFiles;
12
+ use unity_scene_repacker::script_check::{
13
+ FieldDiff, ScriptIssue, ScriptReport, check_bundle_scripts,
14
+ };
15
+
16
+ use crate::{GameArgs, completion, resolve_game_dir};
17
+
18
+ #[derive(Args, Debug)]
19
+ pub struct CheckScriptsArgs {
20
+ /// The repacked bundle
21
+ bundle: PathBuf,
22
+ /// The game the bundle was repacked from
23
+ #[clap(flatten)]
24
+ game: GameArgs,
25
+ #[clap(flatten)]
26
+ target: TargetGameArgs,
27
+ }
28
+
29
+ #[derive(Args, Debug)]
30
+ #[group(required = true, multiple = false)]
31
+ #[clap(next_help_heading = "Target game options")]
32
+ pub struct TargetGameArgs {
33
+ /// Data directory of the game the bundle will be loaded in
34
+ #[arg(long)]
35
+ target_game_dir: Option<PathBuf>,
36
+ /// App ID or search term for the steam game the bundle will be loaded in
37
+ #[arg(long, add = ArgValueCompleter::new(completion::complete_steam_game))]
38
+ target_steam_game: Option<String>,
39
+ }
40
+
41
+ pub fn run(args: CheckScriptsArgs) -> Result<()> {
42
+ let source_dir = resolve_game_dir(args.game.game_dir, args.game.steam_game)?;
43
+ let target_dir = resolve_game_dir(args.target.target_game_dir, args.target.target_steam_game)?;
44
+
45
+ let open = |dir: &Path| -> Result<Environment> {
46
+ Ok(Environment::new(
47
+ GameFiles::probe(dir)?,
48
+ TypeTreeCache::new(TpkTypeTreeBlob::embedded()),
49
+ ))
50
+ };
51
+ let source = open(&source_dir)?;
52
+ let target = open(&target_dir)?;
53
+
54
+ let bundle = std::fs::read(&args.bundle)
55
+ .with_context(|| format!("couldn't read bundle '{}'", args.bundle.display()))?;
56
+ let reports = check_bundle_scripts(&source, &target, &bundle)?;
57
+
58
+ let mut missing = Vec::new();
59
+ let mut differ = Vec::new();
60
+ let mut ok = 0;
61
+ for report in &reports {
62
+ match &report.issue {
63
+ None => ok += 1,
64
+ Some(ScriptIssue::FieldsDiffer(diffs)) => differ.push((report, diffs)),
65
+ Some(issue) => missing.push((report, issue)),
66
+ }
67
+ }
68
+
69
+ let script_name = |report: &ScriptReport| match &report.script {
70
+ Some(script) => format!(
71
+ "{} ({})",
72
+ script.full_name,
73
+ script.assembly.trim_end_matches(".dll")
74
+ ),
75
+ None => "<no script>".to_owned(),
76
+ };
77
+ let width = reports
78
+ .iter()
79
+ .map(|r| r.behaviours.to_string().len())
80
+ .max()
81
+ .unwrap_or(1);
82
+
83
+ let color = std::io::stdout().is_terminal();
84
+ let paint = |code: &str, text: &str| match color {
85
+ true => format!("\x1b[{code}m{text}\x1b[0m"),
86
+ false => text.to_owned(),
87
+ };
88
+
89
+ if !differ.is_empty() {
90
+ warn!("<b>{} load with different fields</>", scripts(differ.len()));
91
+ for (report, diffs) in &differ {
92
+ println!(" {:>width$}x {}", report.behaviours, script_name(report));
93
+ let mut lines = diffs
94
+ .iter()
95
+ .map(|diff| match diff {
96
+ FieldDiff::Removed { path, ty } => {
97
+ (0, "-", "31", field_path(path), short_type(ty))
98
+ }
99
+ FieldDiff::TypeChanged { path, from, to } => (
100
+ 1,
101
+ "~",
102
+ "33",
103
+ field_path(path),
104
+ format!("{} -> {}", short_type(from), short_type(to)),
105
+ ),
106
+ FieldDiff::Added { path, ty } => {
107
+ (2, "+", "32", field_path(path), short_type(ty))
108
+ }
109
+ })
110
+ .collect::<Vec<_>>();
111
+ lines.sort();
112
+ let path_width = lines
113
+ .iter()
114
+ .map(|(_, _, _, path, _)| path.len())
115
+ .max()
116
+ .unwrap_or(0);
117
+ for (_, sign, code, path, ty) in lines {
118
+ println!(
119
+ " {:width$} {} {}",
120
+ "",
121
+ paint(code, &format!("{sign} {path:path_width$}")),
122
+ paint("2", &ty),
123
+ );
124
+ }
125
+ }
126
+ println!();
127
+ }
128
+
129
+ if !missing.is_empty() {
130
+ report_error(&format!(
131
+ "<b>{} missing in target</>",
132
+ scripts(missing.len())
133
+ ));
134
+ for (report, issue) in &missing {
135
+ let reason = match issue {
136
+ ScriptIssue::NoScript => "",
137
+ ScriptIssue::NotInSource => "not in the source game's assemblies",
138
+ ScriptIssue::MissingAssembly => "assembly missing",
139
+ ScriptIssue::MissingClass => "class missing",
140
+ ScriptIssue::FieldsDiffer(_) => unreachable!(),
141
+ };
142
+ println!(
143
+ " {:>width$}x {} {}",
144
+ report.behaviours,
145
+ script_name(report),
146
+ paint("2", reason)
147
+ );
148
+ }
149
+ println!();
150
+ }
151
+
152
+ if !missing.is_empty() {
153
+ report_error(&format!(
154
+ "{} of {} won't load in the target game, {} load with different fields",
155
+ missing.len(),
156
+ scripts(reports.len()),
157
+ differ.len()
158
+ ));
159
+ std::process::exit(1);
160
+ }
161
+ if !differ.is_empty() {
162
+ warn!(
163
+ "All scripts exist in the target game, {} of {} with different fields",
164
+ differ.len(),
165
+ scripts(reports.len())
166
+ );
167
+ return Ok(());
168
+ }
169
+ success!(
170
+ "All {} exist in the target game with the same fields",
171
+ scripts(ok)
172
+ );
173
+ Ok(())
174
+ }
175
+
176
+ fn field_path(path: &str) -> String {
177
+ path.replace(".Array.data", "[]")
178
+ }
179
+
180
+ fn short_type(ty: &str) -> String {
181
+ ty.replace('$', "")
182
+ }
183
+
184
+ fn scripts(count: usize) -> String {
185
+ match count {
186
+ 1 => "1 script".to_owned(),
187
+ n => format!("{n} scripts"),
188
+ }
189
+ }
190
+
191
+ /// Like `paris::error!`, but on stdout with the rest of the report
192
+ fn report_error(message: &str) {
193
+ paris::output::format_stdout(format!("<red><cross></> {message}"), "\n");
194
+ }
@@ -1,3 +1,4 @@
1
+ mod check_scripts;
1
2
  mod completion;
2
3
  mod locate;
3
4
  mod logger;
@@ -27,8 +28,14 @@ use unity_scene_repacker::{RepackSettings, Stats};
27
28
  use crate::utils::friendly_size;
28
29
 
29
30
  #[derive(Parser, Debug)]
30
- #[command(version)]
31
+ #[command(
32
+ version,
33
+ args_conflicts_with_subcommands = true,
34
+ subcommand_negates_reqs = true
35
+ )]
31
36
  struct Arguments {
37
+ #[command(subcommand)]
38
+ command: Option<Command>,
32
39
  #[clap(flatten)]
33
40
  game: GameArgs,
34
41
  #[clap(flatten)]
@@ -40,13 +47,19 @@ struct Arguments {
40
47
  #[derive(Args, Debug)]
41
48
  #[group(required = true, multiple = false)]
42
49
  #[clap(next_help_heading = "Game options")]
43
- struct GameArgs {
50
+ pub(crate) struct GameArgs {
44
51
  /// Directory where the levels files are, e.g. steam/Hollow_Knight/hollow_knight_Data
45
52
  #[arg(long)]
46
- game_dir: Option<PathBuf>,
53
+ pub game_dir: Option<PathBuf>,
47
54
  #[arg(long, add = ArgValueCompleter::new(completion::complete_steam_game))]
48
55
  /// App ID or search term for the steam game to detect
49
- steam_game: Option<String>,
56
+ pub steam_game: Option<String>,
57
+ }
58
+
59
+ #[derive(clap::Subcommand, Debug)]
60
+ enum Command {
61
+ /// Check whether the scripts used in a repacked bundle exist with the same fields in another game.
62
+ CheckScripts(check_scripts::CheckScriptsArgs),
50
63
  }
51
64
 
52
65
  #[derive(Args, Debug)]
@@ -90,7 +103,7 @@ struct OutputArgs {
90
103
  disable: bool,
91
104
 
92
105
  /// Compression level to apply
93
- #[arg(long, default_value = "lzma")]
106
+ #[arg(long, default_value = "lz4")]
94
107
  compression: Compression,
95
108
 
96
109
  #[arg(long, short = 'o', default_value = "out.unity3d")]
@@ -133,7 +146,8 @@ pub enum Mode {
133
146
  pub enum Compression {
134
147
  None = 0,
135
148
  Lzma = 1,
136
- // Lz4 = 2,
149
+ /// Fast compression, larger output than lz4hc
150
+ Lz4 = 2,
137
151
  /// Best compression at the cost of speed
138
152
  Lz4hc = 3,
139
153
  // Lzham = 4,
@@ -162,23 +176,11 @@ pub fn main(args: Vec<OsString>) {
162
176
  fn run(args: Vec<OsString>) -> Result<()> {
163
177
  let args = Arguments::parse_from(args);
164
178
 
165
- let game_dir = match args.game.game_dir {
166
- Some(game_dir) => {
167
- ensure!(
168
- game_dir.exists(),
169
- "Game directory '{}' does not exist",
170
- game_dir.display()
171
- );
172
- match locate::find_unity_data_dir(&game_dir) {
173
- Ok(Some(data_dir)) => data_dir,
174
- _ => game_dir,
175
- }
176
- }
177
- None => {
178
- let game = args.game.steam_game.unwrap();
179
- locate::locate_steam_game(&game)?
180
- }
181
- };
179
+ if let Some(Command::CheckScripts(args)) = args.command {
180
+ return check_scripts::run(args);
181
+ }
182
+
183
+ let game_dir = resolve_game_dir(args.game.game_dir, args.game.steam_game)?;
182
184
 
183
185
  #[cfg(feature = "dhat-heap")]
184
186
  let _profiler = dhat::Profiler::new_heap();
@@ -268,7 +270,7 @@ fn run(args: Vec<OsString>) -> Result<()> {
268
270
  let compression = match args.output.compression {
269
271
  Compression::None => CompressionType::None,
270
272
  Compression::Lzma => CompressionType::Lzma,
271
- // Compression::Lz4 => CompressionType::Lz4,
273
+ Compression::Lz4 => CompressionType::Lz4,
272
274
  Compression::Lz4hc => CompressionType::Lz4hc,
273
275
  // Compression::Lzham => CompressionType::Lzham,
274
276
  };
@@ -443,3 +445,24 @@ fn write_catalog(
443
445
 
444
446
  Ok(())
445
447
  }
448
+
449
+ pub(crate) fn resolve_game_dir(
450
+ game_dir: Option<PathBuf>,
451
+ steam_game: Option<String>,
452
+ ) -> Result<PathBuf> {
453
+ match (game_dir, steam_game) {
454
+ (Some(game_dir), _) => {
455
+ ensure!(
456
+ game_dir.exists(),
457
+ "Game directory '{}' does not exist",
458
+ game_dir.display()
459
+ );
460
+ Ok(match locate::find_unity_data_dir(&game_dir) {
461
+ Ok(Some(data_dir)) => data_dir,
462
+ _ => game_dir,
463
+ })
464
+ }
465
+ (None, Some(game)) => locate::locate_steam_game(&game),
466
+ (None, None) => bail!("no game specified"),
467
+ }
468
+ }
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
 
3
- ## Unreleased
3
+ ## Version 3.1.0
4
+ - default to `lz4` compression level by default, which compresses faster and load a lot faster
5
+ - add `rabex check-scripts out/hk-in-silksong.bundle --steam-game 'Hollow Knight' --target-steam-game Silksong` subcommand for checking script compatibility
6
+
7
+ ## Version 3.0.0
4
8
  - add `--emit-dependency-addressables` to support loading repacked objects as addressables in another game
5
9
  - add `--write-typetrees` for asset bundles, to support loading from multiple different unity versions
6
10
  - when monobehaviours reference pruned objects, set them to null and log a warning
@@ -22,6 +22,7 @@ use rabex_env::resolver::EnvResolver as _;
22
22
  use rabex_env::streams::MapStreamLocations as _;
23
23
  use rabex_env::unity::types::{AssetBundle, AssetInfo, MonoScript};
24
24
  use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
25
+ use rustc_hash::FxHashMap;
25
26
 
26
27
  use crate::{RepackScene, get_asset_bundle_object_asset_name};
27
28
 
@@ -41,11 +42,7 @@ pub struct Dependencies {
41
42
 
42
43
  impl Dependencies {
43
44
  pub fn new(env: &Environment, scenes: &[RepackScene]) -> Result<Self> {
44
- let prefix = env
45
- .app_info()?
46
- .name
47
- .to_lowercase()
48
- .replace(|c: char| !c.is_ascii_alphanumeric(), "_");
45
+ let prefix = game_prefix(env)?;
49
46
 
50
47
  let scene_lookup = env.build_settings()?.scene_name_lookup();
51
48
  let scene_files = scenes
@@ -78,9 +75,8 @@ impl Dependencies {
78
75
  Ok(Dependencies { prefix, containers })
79
76
  }
80
77
 
81
- /// The name of the bundle entry holding `file`
82
78
  fn cab_name(&self, file: &str) -> String {
83
- format!("CAB-{}-{}", self.prefix, file.replace('.', "_"))
79
+ cab_name(&self.prefix, file)
84
80
  }
85
81
 
86
82
  /// The path other files reference `file` by once bundled.
@@ -278,6 +274,36 @@ fn bundle_key(file: &str) -> String {
278
274
  format!("deps/{file}.bundle")
279
275
  }
280
276
 
277
+ /// The prefix of the bundle entries of `env`'s game files
278
+ fn game_prefix(env: &Environment) -> Result<String> {
279
+ Ok(env
280
+ .app_info()?
281
+ .name
282
+ .to_lowercase()
283
+ .replace(|c: char| !c.is_ascii_alphanumeric(), "_"))
284
+ }
285
+
286
+ /// The name of the bundle entry holding `file`
287
+ fn cab_name(prefix: &str, file: &str) -> String {
288
+ format!("CAB-{prefix}-{}", file.replace('.', "_"))
289
+ }
290
+
291
+ /// Maps the bundle names of `env`'s serialized files back to the files.
292
+ pub fn bundled_game_files(env: &Environment) -> Result<FxHashMap<String, ExternalPath>> {
293
+ let prefix = game_prefix(env)?;
294
+ env.game_files
295
+ .serialized_files()?
296
+ .into_iter()
297
+ .map(|path| {
298
+ let path = path
299
+ .into_os_string()
300
+ .into_string()
301
+ .map_err(|path| anyhow::anyhow!("non-utf8 game file {path:?}"))?;
302
+ Ok((cab_name(&prefix, &path), path))
303
+ })
304
+ .collect()
305
+ }
306
+
281
307
  /// Unity expects the `AssetBundle` object at path id 1, which might be already used
282
308
  fn needs_split(file: &SerializedFile) -> bool {
283
309
  file.get_object_info(1)
@@ -1,6 +1,7 @@
1
1
  pub mod dependency_addressables;
2
2
  mod merge_serialized;
3
3
  pub mod monobehaviour_typetree_export;
4
+ pub mod script_check;
4
5
 
5
6
  pub use rabex;
6
7
  use rabex_env::Environment;
@@ -0,0 +1,247 @@
1
+ //! Check whether the scripts of a repacked bundle exist with the same fields in another game.
2
+ use std::collections::BTreeMap;
3
+ use std::io::{Cursor, ErrorKind};
4
+ use std::path::Path;
5
+
6
+ use anyhow::{Context, Result};
7
+ use rabex::files::SerializedFile;
8
+ use rabex::files::bundlefile::{BundleFileReader, ExtractionConfig};
9
+ use rabex::objects::{ClassId, PPtr};
10
+ use rabex::typetree::{TypeTreeNode, TypeTreeProvider as _};
11
+ use rabex_env::Environment;
12
+ use rabex_env::addressables::ArchivePath;
13
+ use rabex_env::resolver::EnvResolver as _;
14
+ use rabex_env::unity::types::MonoScript;
15
+ use rustc_hash::FxHashMap;
16
+
17
+ use crate::dependency_addressables::{self, ExternalPath};
18
+
19
+ #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20
+ pub struct ScriptName {
21
+ pub assembly: String,
22
+ pub full_name: String,
23
+ }
24
+
25
+ #[derive(Debug)]
26
+ pub enum ScriptIssue {
27
+ /// The MonoBehaviour has no script.
28
+ NoScript,
29
+ /// The source game can't produce a type tree for the script.
30
+ NotInSource,
31
+ MissingAssembly,
32
+ MissingClass,
33
+ FieldsDiffer(Vec<FieldDiff>),
34
+ }
35
+
36
+ #[derive(Debug)]
37
+ pub enum FieldDiff {
38
+ Removed {
39
+ path: String,
40
+ ty: String,
41
+ },
42
+ Added {
43
+ path: String,
44
+ ty: String,
45
+ },
46
+ TypeChanged {
47
+ path: String,
48
+ from: String,
49
+ to: String,
50
+ },
51
+ }
52
+
53
+ #[derive(Debug)]
54
+ pub struct ScriptReport {
55
+ pub script: Option<ScriptName>,
56
+ pub behaviours: usize,
57
+ pub issue: Option<ScriptIssue>,
58
+ }
59
+
60
+ /// Checks every script referenced by the MonoBehaviours in `bundle`, which was repacked from `source`, against `target`.
61
+ pub fn check_bundle_scripts(
62
+ source: &Environment,
63
+ target: &Environment,
64
+ bundle: &[u8],
65
+ ) -> Result<Vec<ScriptReport>> {
66
+ let usages = collect_script_usages(source, bundle)?;
67
+
68
+ let source_base = monobehaviour_base_fields(source)?;
69
+ let target_base = monobehaviour_base_fields(target)?;
70
+
71
+ let mut reports = usages
72
+ .into_iter()
73
+ .map(|(script, behaviours)| -> Result<_> {
74
+ let issue = match &script {
75
+ None => Some(ScriptIssue::NoScript),
76
+ Some(script) => check_script(source, target, script, source_base, target_base)
77
+ .with_context(|| {
78
+ format!("checking {} ({})", script.full_name, script.assembly)
79
+ })?,
80
+ };
81
+ Ok(ScriptReport {
82
+ script,
83
+ behaviours,
84
+ issue,
85
+ })
86
+ })
87
+ .collect::<Result<Vec<_>>>()?;
88
+
89
+ reports.sort_by(|a, b| {
90
+ b.behaviours
91
+ .cmp(&a.behaviours)
92
+ .then(a.script.cmp(&b.script))
93
+ });
94
+
95
+ Ok(reports)
96
+ }
97
+
98
+ fn collect_script_usages(
99
+ source: &Environment,
100
+ bundle: &[u8],
101
+ ) -> Result<BTreeMap<Option<ScriptName>, usize>> {
102
+ let bundle = BundleFileReader::from_reader(Cursor::new(bundle), &ExtractionConfig::default())?;
103
+
104
+ let mut bundled_files = None;
105
+ let mut usages = BTreeMap::new();
106
+ for entry in bundle.serialized_files() {
107
+ let data = bundle.read_at_entry(entry)?;
108
+ let file = SerializedFile::from_reader(&mut Cursor::new(&data))
109
+ .with_context(|| format!("reading {} in bundle", entry.path))?;
110
+
111
+ let mut scripts = FxHashMap::<PPtr, Option<ScriptName>>::default();
112
+ for info in file.objects() {
113
+ if info.m_ClassID != ClassId::MonoBehaviour {
114
+ continue;
115
+ }
116
+ let script = match file.script_type(info).and_then(PPtr::optional) {
117
+ None => None,
118
+ Some(pptr) => match scripts.get(&pptr) {
119
+ Some(script) => script.clone(),
120
+ None => {
121
+ let script = read_script(source, &file, pptr, &mut bundled_files)?;
122
+ scripts.insert(pptr, Some(script.clone()));
123
+ Some(script)
124
+ }
125
+ },
126
+ };
127
+ *usages.entry(script).or_insert(0) += 1;
128
+ }
129
+ }
130
+ Ok(usages)
131
+ }
132
+
133
+ fn read_script(
134
+ source: &Environment,
135
+ file: &SerializedFile,
136
+ pptr: PPtr,
137
+ bundled_files: &mut Option<FxHashMap<String, ExternalPath>>,
138
+ ) -> Result<ScriptName> {
139
+ let index = pptr
140
+ .m_FileID
141
+ .get_externals_index()
142
+ .with_context(|| format!("script {pptr:?} is not in an external file"))?;
143
+ let external = &file
144
+ .m_Externals
145
+ .get(index)
146
+ .with_context(|| format!("script {pptr:?} points to a missing external"))?
147
+ .pathName;
148
+
149
+ let path = match ArchivePath::try_parse(Path::new(external))? {
150
+ Some(archive) => {
151
+ let bundled_files = match bundled_files {
152
+ Some(files) => files,
153
+ None => bundled_files.insert(dependency_addressables::bundled_game_files(source)?),
154
+ };
155
+ bundled_files
156
+ .get(archive.bundle)
157
+ .with_context(|| format!("{external} is not a bundled file of the source game"))?
158
+ }
159
+ None => external,
160
+ };
161
+
162
+ let script = source
163
+ .load_serialized(path)?
164
+ .object_at::<MonoScript>(pptr.m_PathID)?
165
+ .read()
166
+ .with_context(|| format!("reading script {} in {path}", pptr.m_PathID))?;
167
+ Ok(ScriptName {
168
+ assembly: script.assembly_name().into_owned(),
169
+ full_name: script.full_name().into_owned(),
170
+ })
171
+ }
172
+
173
+ fn check_script(
174
+ source: &Environment,
175
+ target: &Environment,
176
+ script: &ScriptName,
177
+ source_base: usize,
178
+ target_base: usize,
179
+ ) -> Result<Option<ScriptIssue>> {
180
+ let Some(source_tt) = source.generate_typetree(&script.assembly, &script.full_name)? else {
181
+ return Ok(Some(ScriptIssue::NotInSource));
182
+ };
183
+
184
+ match target
185
+ .game_files
186
+ .read_path(&Path::new("Managed").join(&script.assembly))
187
+ {
188
+ Ok(_) => {}
189
+ Err(e) if e.kind() == ErrorKind::NotFound => return Ok(Some(ScriptIssue::MissingAssembly)),
190
+ Err(e) => return Err(e.into()),
191
+ }
192
+ let Some(target_tt) = target.generate_typetree(&script.assembly, &script.full_name)? else {
193
+ return Ok(Some(ScriptIssue::MissingClass));
194
+ };
195
+
196
+ let source_fields = source_tt
197
+ .children
198
+ .get(source_base..)
199
+ .context("source type tree is missing the MonoBehaviour fields")?;
200
+ let target_fields = target_tt
201
+ .children
202
+ .get(target_base..)
203
+ .context("target type tree is missing the MonoBehaviour fields")?;
204
+ let mut diffs = Vec::new();
205
+ diff_fields(source_fields, target_fields, "", &mut diffs);
206
+ Ok((!diffs.is_empty()).then_some(ScriptIssue::FieldsDiffer(diffs)))
207
+ }
208
+
209
+ /// The number of leading fields generated type trees share with every MonoBehaviour
210
+ fn monobehaviour_base_fields(env: &Environment) -> Result<usize> {
211
+ let node = env
212
+ .tpk
213
+ .get_typetree_node(ClassId::MonoBehaviour, env.unity_version()?)
214
+ .context("no MonoBehaviour type tree for this unity version")?;
215
+ Ok(node.children.len())
216
+ }
217
+
218
+ fn diff_fields(
219
+ source: &[TypeTreeNode],
220
+ target: &[TypeTreeNode],
221
+ prefix: &str,
222
+ diffs: &mut Vec<FieldDiff>,
223
+ ) {
224
+ for field in source {
225
+ let path = format!("{prefix}{}", field.m_Name);
226
+ match target.iter().find(|t| t.m_Name == field.m_Name) {
227
+ None => diffs.push(FieldDiff::Removed {
228
+ path,
229
+ ty: field.m_Type.clone(),
230
+ }),
231
+ Some(t) if t.m_Type != field.m_Type => diffs.push(FieldDiff::TypeChanged {
232
+ path,
233
+ from: field.m_Type.clone(),
234
+ to: t.m_Type.clone(),
235
+ }),
236
+ Some(t) => diff_fields(&field.children, &t.children, &format!("{path}."), diffs),
237
+ }
238
+ }
239
+ for field in target {
240
+ if !source.iter().any(|s| s.m_Name == field.m_Name) {
241
+ diffs.push(FieldDiff::Added {
242
+ path: format!("{prefix}{}", field.m_Name),
243
+ ty: field.m_Type.clone(),
244
+ });
245
+ }
246
+ }
247
+ }