unity-asset-cli 0.2.0

Command-line tools for Unity asset parsing and manipulation
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
//! Unity Asset Parser CLI - Async Version
//!
//! High-performance async command-line interface for parsing and manipulating Unity assets.

use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::{Path, PathBuf};
use std::time::Instant;
use unity_asset::{UnityDocument, YamlDocument};

#[cfg(feature = "async")]
use futures::stream::{self, StreamExt};
#[cfg(feature = "async")]
use indicatif::{ProgressBar, ProgressStyle};
#[cfg(feature = "async")]
use unity_asset::AsyncUnityDocument;

#[cfg(feature = "async")]
fn init_tracing() {
    let filter = tracing_subscriber::EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("error"));
    let _ = tracing_subscriber::fmt().with_env_filter(filter).try_init();
}

#[derive(Parser)]
#[command(name = "unity_asset_async")]
#[command(about = "A high-performance async Unity asset parser")]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Number of concurrent operations (default: 8)
    #[arg(long, global = true)]
    concurrency: Option<usize>,

    /// Show progress bars
    #[arg(long, global = true)]
    progress: bool,
}

#[derive(Subcommand)]
enum Commands {
    /// Parse Unity YAML files (supports batch processing)
    ParseYaml {
        /// Input YAML file or directory path
        #[arg(short, long)]
        input: PathBuf,

        /// Output format (summary, detailed, json)
        #[arg(short, long, default_value = "summary")]
        format: String,

        /// Preserve original types instead of converting to strings
        #[arg(long)]
        preserve_types: bool,

        /// Process files recursively
        #[arg(short, long)]
        recursive: bool,
    },

    /// Extract information from Unity files with concurrent processing
    Extract {
        /// Input file or directory path
        #[arg(short, long)]
        input: PathBuf,

        /// Output directory
        #[arg(short, long)]
        output: PathBuf,

        /// Unity class types to extract (GameObject, Transform, etc.)
        #[arg(long)]
        types: Vec<String>,

        /// Process files recursively
        #[arg(short, long)]
        recursive: bool,
    },
}

#[cfg(feature = "async")]
#[tokio::main]
async fn main() -> Result<()> {
    init_tracing();
    let cli = Cli::parse();
    let concurrency = cli.concurrency.unwrap_or(8); // Default to 8 concurrent operations

    println!(
        "🚀 Unity Asset Parser (Async) - Concurrency: {}",
        concurrency
    );

    match cli.command {
        Commands::ParseYaml {
            input,
            format,
            preserve_types,
            recursive,
        } => {
            parse_yaml_command_async(
                input,
                format,
                preserve_types,
                recursive,
                concurrency,
                cli.progress,
            )
            .await
        }
        Commands::Extract {
            input,
            output,
            types,
            recursive,
        } => {
            extract_command_async(input, output, types, recursive, concurrency, cli.progress).await
        }
    }
}

#[cfg(not(feature = "async"))]
fn main() -> Result<()> {
    eprintln!("❌ Async features not enabled. Please compile with --features async");
    std::process::exit(1);
}

#[cfg(feature = "async")]
async fn parse_yaml_command_async(
    input: PathBuf,
    format: String,
    preserve_types: bool,
    recursive: bool,
    concurrency: usize,
    show_progress: bool,
) -> Result<()> {
    let start_time = Instant::now();

    println!("📂 Scanning for YAML files...");
    let yaml_files = collect_yaml_files(&input, recursive).await?;

    if yaml_files.is_empty() {
        println!("⚠️  No YAML files found in {:?}", input);
        return Ok(());
    }

    println!("📄 Found {} YAML files", yaml_files.len());

    let progress = if show_progress {
        let pb = ProgressBar::new(yaml_files.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template(
                    "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})",
                )
                .unwrap()
                .progress_chars("#>-"),
        );
        Some(pb)
    } else {
        None
    };

    // Process files concurrently
    let file_count = yaml_files.len();
    let results = stream::iter(yaml_files)
        .map(|file_path| {
            let format = format.clone();
            let progress = progress.clone();
            async move {
                let result = process_single_yaml_file(&file_path, &format, preserve_types).await;
                if let Some(ref pb) = progress {
                    pb.inc(1);
                }
                (file_path, result)
            }
        })
        .buffer_unordered(concurrency)
        .collect::<Vec<_>>()
        .await;

    if let Some(pb) = progress {
        pb.finish_with_message("✅ Processing complete");
    }

    // Report results
    let mut success_count = 0;
    let mut error_count = 0;

    for (file_path, result) in results {
        match result {
            Ok(entry_count) => {
                success_count += 1;
                if !show_progress {
                    println!("{}: {} entries", file_path.display(), entry_count);
                }
            }
            Err(e) => {
                error_count += 1;
                eprintln!("{}: {}", file_path.display(), e);
            }
        }
    }

    let elapsed = start_time.elapsed();
    println!("\n📊 Summary:");
    println!("  ✅ Success: {}", success_count);
    println!("  ❌ Errors: {}", error_count);
    println!("  ⏱️  Time: {:.2}s", elapsed.as_secs_f64());
    if elapsed.as_secs_f64() > 0.0 {
        println!(
            "  🚀 Throughput: {:.1} files/sec",
            file_count as f64 / elapsed.as_secs_f64()
        );
    }

    Ok(())
}

#[cfg(feature = "async")]
async fn process_single_yaml_file(
    file_path: &PathBuf,
    format: &str,
    preserve_types: bool,
) -> Result<usize> {
    let doc = YamlDocument::load_yaml_async(file_path, preserve_types).await?;
    let entry_count = UnityDocument::entries(&doc).len();

    match format {
        "summary" => {
            // Just return count for summary
        }
        "detailed" => {
            println!("\n📄 File: {}", file_path.display());
            for (i, entry) in UnityDocument::entries(&doc).iter().enumerate().take(3) {
                println!(
                    "  [{}]: {} (ID: {}, Anchor: {})",
                    i, entry.class_name, entry.class_id, entry.anchor
                );
            }
            if entry_count > 3 {
                println!("  ... and {} more entries", entry_count - 3);
            }
        }
        _ => {
            // Default behavior
        }
    }

    Ok(entry_count)
}

#[cfg(feature = "async")]
async fn collect_yaml_files(input: &PathBuf, recursive: bool) -> Result<Vec<PathBuf>> {
    let mut files = Vec::new();

    if input.is_file() {
        if is_yaml_file(input) {
            files.push(input.clone());
        }
    } else if input.is_dir() {
        collect_yaml_files_from_dir(input, recursive, &mut files).await?;
    }

    Ok(files)
}

#[cfg(feature = "async")]
async fn collect_yaml_files_from_dir(
    dir: &PathBuf,
    recursive: bool,
    files: &mut Vec<PathBuf>,
) -> Result<()> {
    let mut entries = tokio::fs::read_dir(dir).await?;

    while let Some(entry) = entries.next_entry().await? {
        let path = entry.path();

        if path.is_file() && is_yaml_file(&path) {
            files.push(path);
        } else if path.is_dir() && recursive {
            Box::pin(collect_yaml_files_from_dir(&path, recursive, files)).await?;
        }
    }

    Ok(())
}

fn is_yaml_file(path: &Path) -> bool {
    if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
        matches!(ext, "asset" | "prefab" | "unity" | "meta" | "yaml" | "yml")
    } else {
        false
    }
}

#[cfg(feature = "async")]
async fn extract_command_async(
    input: PathBuf,
    output: PathBuf,
    types: Vec<String>,
    recursive: bool,
    concurrency: usize,
    show_progress: bool,
) -> Result<()> {
    let start_time = Instant::now();

    println!("📂 Scanning for Unity files...");
    let unity_files = collect_yaml_files(&input, recursive).await?;

    if unity_files.is_empty() {
        println!("⚠️  No Unity files found in {:?}", input);
        return Ok(());
    }

    println!("📄 Found {} Unity files", unity_files.len());

    // Create output directory
    tokio::fs::create_dir_all(&output).await?;

    let progress = if show_progress {
        let pb = ProgressBar::new(unity_files.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template(
                    "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})",
                )
                .unwrap()
                .progress_chars("#>-"),
        );
        Some(pb)
    } else {
        None
    };

    // Process files concurrently
    let results = stream::iter(unity_files)
        .map(|file_path| {
            let output = output.clone();
            let types = types.clone();
            let progress = progress.clone();
            async move {
                let result = extract_single_file(&file_path, &output, &types).await;
                if let Some(ref pb) = progress {
                    pb.inc(1);
                }
                (file_path, result)
            }
        })
        .buffer_unordered(concurrency)
        .collect::<Vec<_>>()
        .await;

    if let Some(pb) = progress {
        pb.finish_with_message("✅ Extraction complete");
    }

    // Report results
    let mut success_count = 0;
    let mut error_count = 0;
    let mut total_extracted = 0;

    for (file_path, result) in results {
        match result {
            Ok(extracted_count) => {
                success_count += 1;
                total_extracted += extracted_count;
                if !show_progress {
                    println!(
                        "{}: {} entries extracted",
                        file_path.display(),
                        extracted_count
                    );
                }
            }
            Err(e) => {
                error_count += 1;
                eprintln!("{}: {}", file_path.display(), e);
            }
        }
    }

    let elapsed = start_time.elapsed();
    println!("\n📊 Extraction Summary:");
    println!("  ✅ Files processed: {}", success_count);
    println!("  📦 Total entries extracted: {}", total_extracted);
    println!("  ❌ Errors: {}", error_count);
    println!("  ⏱️  Time: {:.2}s", elapsed.as_secs_f64());
    println!("  📁 Output directory: {}", output.display());

    Ok(())
}

#[cfg(feature = "async")]
async fn extract_single_file(
    file_path: &PathBuf,
    output_dir: &Path,
    types: &[String],
) -> Result<usize> {
    let doc = YamlDocument::load_yaml_async(file_path, false).await?;

    // Filter by types if specified
    let entries_to_extract: Vec<_> = if types.is_empty() {
        UnityDocument::entries(&doc).iter().collect()
    } else {
        doc.filter(
            Some(&types.iter().map(|s| s.as_str()).collect::<Vec<_>>()),
            None,
        )
    };

    let mut extracted_count = 0;

    // Extract each entry
    for (i, entry) in entries_to_extract.iter().enumerate() {
        let filename = format!("{}_{:03}_{}.yaml", entry.class_name, i, entry.anchor);
        let entry_path = output_dir.join(filename);

        // Create a single-entry document
        let mut single_doc = YamlDocument::new();
        single_doc.add_entry((*entry).clone());

        // Save the entry asynchronously
        single_doc.save_to_path_async(&entry_path).await?;
        extracted_count += 1;
    }

    Ok(extracted_count)
}