unity-solution-generator 0.1.1

Regenerates Unity .csproj/.sln files from asmdef/asmref layout without launching the Unity editor.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Lockfile scanner: walks the Unity installation + project to materialise
//! every DLL reference, analyzer, and define needed for a `.csproj`.

use std::collections::BTreeSet;
use std::path::Path;

use ignore::{WalkBuilder, WalkState};
use walkdir::WalkDir;

use crate::defines::{DEFAULT_FEATURE_DEFINES, generate_version_defines, parse_scripting_defines};
use crate::error::{LockfileError, Result};
use crate::io::{file_exists, list_directory, read_file};
use crate::lockfile::{DllRef, Lockfile, RefCategory};
use crate::paths::{join_path, resolve_real_path};
use crate::project_scanner::parse_version_defines;

pub struct LockfileScanner;

/// Output of [`LockfileScanner::scan_with_artifacts`]: the lockfile plus the
/// concrete `.dll`/`.asmdef` paths that contributed to it. The caller (lock-cache)
/// uses the path list to build a fingerprint of contributing directories.
pub struct ScannedLockfile {
    pub lockfile: Lockfile,
    /// Paths relative to `project_root`, of every project-side `.dll` and `.asmdef`
    /// that the scan ingested.
    pub contributing_paths_relative: Vec<String>,
    /// Absolute paths outside `project_root` that the scan ingested (Unity
    /// `BuiltInPackages/`, the per-user tarball-extract cache). Watched by
    /// `lock-fingerprint` so post-Unity-install changes invalidate the lockfile.
    pub contributing_external_absolute: Vec<String>,
}

impl LockfileScanner {
    pub fn scan(project_root: &str) -> Result<Lockfile> {
        Self::scan_with_artifacts(project_root).map(|s| s.lockfile)
    }

    pub fn scan_with_artifacts(project_root: &str) -> Result<ScannedLockfile> {
        let _span = tracing::info_span!("lockfile_scanner.scan").entered();
        let (version, unity_path) = resolve_unity_path(project_root)?;
        let app_contents = join_path(&unity_path, "Unity.app/Contents");
        let _unity_span = tracing::info_span!("lockfile_scanner.unity_install").entered();

        let managed_engine_dir = join_path(&app_contents, "Managed/UnityEngine");
        let mut engine_refs: Vec<DllRef> = Vec::new();
        let mut editor_refs: Vec<DllRef> = Vec::new();
        let mut managed_dlls: Vec<String> = list_directory(&managed_engine_dir)
            .into_iter()
            .filter(|n| n.ends_with(".dll"))
            .collect();
        managed_dlls.sort();
        for dll in &managed_dlls {
            let name = &dll[..dll.len() - 4];
            if !(name.starts_with("UnityEngine") || name.starts_with("UnityEditor")) {
                continue;
            }
            let path = format!(
                "$(UnityPath)/Unity.app/Contents/Managed/UnityEngine/{}",
                dll
            );
            if name.starts_with("UnityEditor") {
                editor_refs.push(DllRef::new(name, path));
            } else {
                engine_refs.push(DllRef::new(name, path));
            }
        }

        // Lives one level up from Managed/UnityEngine/.
        let graphs_dll = join_path(&app_contents, "Managed/UnityEditor.Graphs.dll");
        if file_exists(&graphs_dll) {
            editor_refs.push(DllRef::new(
                "UnityEditor.Graphs",
                "$(UnityPath)/Unity.app/Contents/Managed/UnityEditor.Graphs.dll",
            ));
        }

        let netstd_base = join_path(&app_contents, "NetStandard");
        let mut netstd_refs: Vec<DllRef> = Vec::new();
        walk_files(&netstd_base, &netstd_base, &[".dll"], false, |rel, name| {
            let n = &name[..name.len() - 4];
            netstd_refs.push(DllRef::new(
                n,
                format!("$(UnityPath)/Unity.app/Contents/NetStandard/{}", rel),
            ));
        });
        netstd_refs.sort_by(|a, b| a.name.cmp(&b.name));

        let playback_base = join_path(&unity_path, "PlaybackEngines");
        let ios_refs = scan_playback_dlls(
            &join_path(&playback_base, "iOSSupport"),
            "PlaybackEngines/iOSSupport",
        );
        let android_refs = scan_playback_dlls(
            &join_path(&playback_base, "AndroidPlayer"),
            "PlaybackEngines/AndroidPlayer",
        );
        let standalone_dir = join_path(&app_contents, "PlaybackEngines/MacStandaloneSupport");
        let standalone_refs = scan_playback_dlls(
            &standalone_dir,
            "Unity.app/Contents/PlaybackEngines/MacStandaloneSupport",
        );

        let source_gen_dir = join_path(&app_contents, "Tools/Unity.SourceGenerators");
        let mut analyzers: Vec<String> = Vec::new();
        let mut sg_dlls: Vec<String> = list_directory(&source_gen_dir)
            .into_iter()
            .filter(|n| n.ends_with(".dll"))
            .collect();
        sg_dlls.sort();
        for dll in sg_dlls {
            analyzers.push(format!(
                "$(UnityPath)/Unity.app/Contents/Tools/Unity.SourceGenerators/{}",
                dll
            ));
        }

        // Deduplicate by assembly name (first wins across Assets > Packages > PackageCache).
        // We walk each root in parallel (via `ignore`), collect the per-root hits into a
        // `Vec<(rel_path, file_name)>`, then iterate sequentially across roots in order
        // to preserve "first wins" semantics. The hot cost is reading `Library/PackageCache`,
        // which is heavily parallelisable.
        drop(_unity_span);
        let _proj_span = tracing::info_span!("lockfile_scanner.project_walk").entered();
        let mut project_refs: Vec<DllRef> = Vec::new();
        let mut seen_project_dlls: BTreeSet<String> = BTreeSet::new();
        let mut seen_analyzers: BTreeSet<String> = BTreeSet::new();
        let mut asmdef_paths: Vec<String> = Vec::new();
        let mut contributing: Vec<String> = Vec::new();
        let mut contributing_external: Vec<String> = Vec::new();
        for root in ["Assets", "Packages", "Library/PackageCache"] {
            let root_dir = join_path(project_root, root);
            let hits = parallel_walk_dlls_and_asmdefs(&root_dir, project_root);
            for (rel, file_name) in hits {
                contributing.push(rel.clone());
                if file_name.ends_with(".dll") {
                    let name = &file_name[..file_name.len() - 4];
                    let path = format!("$(ProjectRoot)/{}", rel);
                    if is_analyzer_dll(name) {
                        if seen_analyzers.insert(name.to_string()) {
                            analyzers.push(path);
                        }
                    } else if seen_project_dlls.insert(name.to_string()) {
                        project_refs.push(DllRef::new(name, path));
                    }
                } else {
                    asmdef_paths.push(join_path(project_root, &rel));
                }
            }
        }

        // Targeted fallback for packages that `Library/PackageCache/` doesn't
        // cover — typically a fresh worktree where Unity hasn't run yet, so
        // registry/builtin packages haven't been resolved into the per-project
        // cache. We read `packages-lock.json` to find what *should* be there;
        // for each gap we look up the package in `BuiltInPackages/` (already
        // extracted, lives in the Unity install) or extract from
        // `PackageManager/Editor/<name>-<version>.tgz` into a per-user cache.
        // Walking those sources in bulk would drown the cold-lock path
        // (~28 s on meow-tower); the missing-package set is usually empty in
        // practice, so the gated approach reverts to the original ~30 ms.
        let missing_packages = compute_missing_packages(project_root);
        if !missing_packages.is_empty() {
            tracing::info!(
                "lockfile_scanner: {} package(s) missing from PackageCache; falling back to BuiltInPackages + tgz extract",
                missing_packages.len()
            );
        }
        // Each fallback source roots its walk at the per-package directory and
        // emits ref paths using a single placeholder + prefix. Keeping `rel`
        // relative to the package dir (not the install/cache root) means the
        // emitted ref looks like `$(VAR)/<package-prefix>/<rel>` regardless of
        // source — no asymmetry between BuiltInPackages and the tgz cache.
        let mut ingest = |pkg_dir: &str, ref_prefix: &str| {
            contributing_external.push(pkg_dir.to_string());
            for (rel, file_name) in parallel_walk_dlls_and_asmdefs(pkg_dir, pkg_dir) {
                if file_name.ends_with(".dll") {
                    let name = &file_name[..file_name.len() - 4];
                    let path = format!("{}/{}", ref_prefix, rel);
                    if is_analyzer_dll(name) {
                        if seen_analyzers.insert(name.to_string()) {
                            analyzers.push(path);
                        }
                    } else if seen_project_dlls.insert(name.to_string()) {
                        project_refs.push(DllRef::new(name, path));
                    }
                } else {
                    asmdef_paths.push(format!("{}/{}", pkg_dir, rel));
                }
            }
        };
        for entry in &missing_packages {
            let builtin = format!(
                "{}/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/{}",
                unity_path, entry.name
            );
            if Path::new(&builtin).exists() {
                let prefix = format!(
                    "$(UnityPath)/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/{}",
                    entry.name
                );
                ingest(&builtin, &prefix);
                continue;
            }
            let Some(extract_root) = crate::package_cache::ensure_extracted_for_package(
                &unity_path,
                &version,
                &entry.name,
            ) else {
                tracing::warn!(
                    "lockfile_scanner: package '{}' missing from PackageCache, BuiltInPackages, and Editor/*.tgz",
                    entry.name
                );
                continue;
            };
            let prefix = format!("$(UsgCache)/{}", entry.name);
            ingest(&extract_root, &prefix);
        }

        analyzers.sort();
        project_refs.sort_by(|a, b| a.name.cmp(&b.name));

        drop(_proj_span);
        let _defines_span = tracing::info_span!("lockfile_scanner.defines").entered();
        let version_defines = generate_version_defines(&version);
        let asmdef_defines = collect_asmdef_version_defines(project_root, &asmdef_paths);
        let mut all_defines = version_defines;
        all_defines.extend(DEFAULT_FEATURE_DEFINES.iter().map(|s| s.to_string()));
        all_defines.extend(asmdef_defines);
        let scripting_defines = parse_scripting_defines(project_root);

        let mut refs = std::collections::BTreeMap::new();
        refs.insert(RefCategory::Engine, engine_refs);
        refs.insert(RefCategory::Editor, editor_refs);
        refs.insert(RefCategory::Netstandard, netstd_refs);
        refs.insert(RefCategory::PlaybackIos, ios_refs);
        refs.insert(RefCategory::PlaybackAndroid, android_refs);
        refs.insert(RefCategory::PlaybackStandalone, standalone_refs);
        refs.insert(RefCategory::Project, project_refs);

        let lockfile = Lockfile {
            unity_version: version,
            unity_path,
            lang_version: "9.0".to_string(),
            analyzers,
            refs,
            defines: all_defines,
            defines_scripting: scripting_defines,
        };
        Ok(ScannedLockfile {
            lockfile,
            contributing_paths_relative: contributing,
            contributing_external_absolute: contributing_external,
        })
    }
}

fn resolve_unity_path(project_root: &str) -> Result<(String, String)> {
    let version_file = join_path(project_root, "ProjectSettings/ProjectVersion.txt");
    if !file_exists(&version_file) {
        return Err(LockfileError::NoProjectVersion(project_root.to_string()).into());
    }
    let content = read_file(&version_file)?;
    let Some(colon) = content.find(':') else {
        return Err(LockfileError::NoProjectVersion(project_root.to_string()).into());
    };
    let bytes = content.as_bytes();
    let mut i = colon + 1;
    while i < bytes.len() && bytes[i] == b' ' {
        i += 1;
    }
    let mut end = i;
    while end < bytes.len() && bytes[end] != b'\n' && bytes[end] != b'\r' {
        end += 1;
    }
    let version = content[i..end].to_string();
    if version.is_empty() {
        return Err(LockfileError::NoProjectVersion(project_root.to_string()).into());
    }

    let unity_path = format!("/Applications/Unity/Hub/Editor/{}", version);
    if !Path::new(&unity_path).exists() {
        return Err(LockfileError::UnityNotFound(unity_path).into());
    }
    Ok((version, resolve_real_path(&unity_path)))
}

/// Recursively walk `directory` (using walkdir), invoking `handler(relative_to_base, file_name)`
/// for each file with a matching extension. Skips dotfiles, tilde-suffixed entries, and
/// (optionally) native-plugin subdirs.
fn walk_files(
    directory: &str,
    base_path: &str,
    extensions: &[&str],
    skip_native_plugin_dirs: bool,
    mut handler: impl FnMut(&str, &str),
) {
    if !Path::new(directory).exists() {
        return;
    }
    let base = Path::new(base_path);
    let mut iter = WalkDir::new(directory)
        .follow_links(false)
        .into_iter()
        .filter_entry(|e| {
            let name = e.file_name().to_string_lossy();
            if name.starts_with('.') || name.ends_with('~') {
                return false;
            }
            if e.file_type().is_dir() && skip_native_plugin_dirs && is_native_plugin_dir(&name) {
                return false;
            }
            true
        });
    while let Some(entry) = iter.next() {
        let Ok(entry) = entry else {
            continue;
        };
        if !entry.file_type().is_file() {
            continue;
        }
        let name_owned = entry.file_name().to_string_lossy().into_owned();
        if !extensions.iter().any(|ext| name_owned.ends_with(ext)) {
            continue;
        }
        let Ok(rel_path) = entry.path().strip_prefix(base) else {
            continue;
        };
        let Some(rel) = rel_path.to_str() else {
            continue;
        };
        handler(rel, &name_owned);
    }
}

/// Walk `directory` in parallel using `ignore::WalkBuilder::build_parallel` and return
/// every file ending in `.dll` or `.asmdef`, as `(relative_to_strip_base, file_name)`.
/// Skips dotfiles, tilde-suffixed entries, and native-plugin directories.
fn parallel_walk_dlls_and_asmdefs(directory: &str, strip_base: &str) -> Vec<(String, String)> {
    if !Path::new(directory).exists() {
        return Vec::new();
    }
    // Component-aware prefix strip — see project_scanner.rs for rationale.
    let project_root_path = Path::new(strip_base);
    let mut builder = WalkBuilder::new(directory);
    builder
        .standard_filters(false)
        .hidden(false)
        .ignore(false)
        .git_ignore(false)
        .git_global(false)
        .git_exclude(false)
        .parents(false)
        .follow_links(false);

    let mut hits = crate::walk::parallel_walk(builder, |local: &mut Vec<(String, String)>, entry| {
        let name = entry.file_name().to_string_lossy();
        if name.starts_with('.') || name.ends_with('~') {
            return WalkState::Skip;
        }
        let Some(ft) = entry.file_type() else {
            return WalkState::Continue;
        };
        if ft.is_dir() {
            if is_native_plugin_dir(&name) {
                return WalkState::Skip;
            }
            return WalkState::Continue;
        }
        if !ft.is_file() {
            return WalkState::Continue;
        }
        let n: &str = name.as_ref();
        if !(n.ends_with(".dll") || n.ends_with(".asmdef")) {
            return WalkState::Continue;
        }
        let Ok(rel) = entry.path().strip_prefix(project_root_path) else {
            return WalkState::Continue;
        };
        let Some(rel_str) = rel.to_str() else {
            return WalkState::Continue;
        };
        local.push((rel_str.to_string(), n.to_string()));
        WalkState::Continue
    });
    // Stable order across the roots so the "first wins" dedupe pass is deterministic
    // even though the parallel walker fans out non-deterministically per thread.
    hits.sort();
    hits
}

/// Package entry that `Library/PackageCache/` is expected to cover but doesn't.
/// Driven by `Packages/packages-lock.json`: an entry is "expected" when its
/// `source` is something other than `embedded`/`local` (those live under
/// `Packages/` directly and are picked up by the project walk already).
#[derive(Debug)]
struct MissingPackage {
    name: String,
}

fn compute_missing_packages(project_root: &str) -> Vec<MissingPackage> {
    // Snapshot of currently-resolved packages by canonical name. Unity uses
    // `<name>@<hash>` for PackageCache directories; we strip the suffix.
    let pc_dir = join_path(project_root, "Library/PackageCache");
    let mut resolved: BTreeSet<String> = BTreeSet::new();
    for entry in list_directory(&pc_dir) {
        let name = match entry.find('@') {
            Some(i) => entry[..i].to_string(),
            None => entry,
        };
        resolved.insert(name);
    }

    let lock_path = join_path(project_root, "Packages/packages-lock.json");
    let Ok(content) = read_file(&lock_path) else {
        return Vec::new();
    };
    let v: serde_json::Value = match serde_json::from_str(&content) {
        Ok(v) => v,
        Err(e) => {
            tracing::warn!(
                "lockfile_scanner: malformed packages-lock.json ({}); skipping missing-package fallback",
                e
            );
            return Vec::new();
        }
    };
    let Some(deps) = v.get("dependencies").and_then(|x| x.as_object()) else {
        return Vec::new();
    };

    let mut missing = Vec::new();
    for (name, meta) in deps {
        let source = meta
            .get("source")
            .and_then(|s| s.as_str())
            .unwrap_or("");
        // `embedded` lives in `Packages/<name>/`; `local` is a `file:` path —
        // both are scanned by the project walk. Everything else (registry,
        // builtin, git) lands in `Library/PackageCache/` after Unity resolves.
        if matches!(source, "embedded" | "local") {
            continue;
        }
        if resolved.contains(name) {
            continue;
        }
        missing.push(MissingPackage {
            name: name.clone(),
        });
    }
    missing
}

fn is_native_plugin_dir(name: &str) -> bool {
    matches!(
        name,
        "x86" | "x86_64" | "arm64-v8a" | "armeabi-v7a" | "ARM64" | "x64"
    ) || name.ends_with(".framework")
        || name.ends_with(".bundle")
}

fn scan_playback_dlls(directory: &str, prefix: &str) -> Vec<DllRef> {
    let mut dlls: Vec<String> = list_directory(directory)
        .into_iter()
        .filter(|n| n.ends_with(".dll"))
        .collect();
    dlls.sort();
    dlls.into_iter()
        .filter_map(|dll| {
            let name = dll[..dll.len() - 4].to_string();
            if name.starts_with("UnityEditor.") || name.starts_with("Unity.Android.") {
                Some(DllRef::new(name, format!("$(UnityPath)/{}/{}", prefix, dll)))
            } else {
                None
            }
        })
        .collect()
}

fn is_analyzer_dll(name: &str) -> bool {
    let lower = name.to_ascii_lowercase();
    lower.contains("analyzer") || lower.contains("sourcegenerator")
}

fn collect_asmdef_version_defines(project_root: &str, asmdef_paths: &[String]) -> Vec<String> {
    let mut installed_packages: BTreeSet<String> = BTreeSet::new();
    installed_packages.insert("Unity".to_string());

    let manifest_path = join_path(project_root, "Packages/manifest.json");
    if let Ok(manifest) = read_file(&manifest_path) {
        if let Ok(v) = serde_json::from_str::<serde_json::Value>(&manifest) {
            if let Some(deps) = v.get("dependencies").and_then(|x| x.as_object()) {
                for pkg in deps.keys() {
                    installed_packages.insert(pkg.clone());
                }
            }
        }
    }
    for entry in list_directory(&join_path(project_root, "Packages")) {
        if entry.ends_with(".json") || entry.starts_with('.') {
            continue;
        }
        installed_packages.insert(entry);
    }

    let mut all: BTreeSet<String> = BTreeSet::new();
    for path in asmdef_paths {
        let Ok(content) = read_file(path) else {
            continue;
        };
        let Ok(v) = serde_json::from_str::<serde_json::Value>(&content) else {
            continue;
        };
        for vd in parse_version_defines(&v) {
            if installed_packages.contains(&vd.package_name) {
                all.insert(vd.define);
            }
        }
    }
    all.into_iter().collect()
}