122 lines
4.5 KiB
Rust
122 lines
4.5 KiB
Rust
//! Example demonstrating GUID resolution with .meta files
|
|
//!
|
|
//! This example shows how to:
|
|
//! - Load Unity files with their .meta files
|
|
//! - Access GUID to path mappings
|
|
//! - Resolve cross-file references using GUIDs
|
|
//!
|
|
//! Run with: cargo run --example guid_resolution
|
|
|
|
use cursebreaker_parser::{UnityProject, MetaFile};
|
|
use std::path::Path;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
println!("Unity GUID Resolution Example");
|
|
println!("==============================\n");
|
|
|
|
// Create a new Unity project with LRU cache
|
|
let mut project = UnityProject::new(1000);
|
|
|
|
// Example 1: Parse a .meta file directly
|
|
println!("Example 1: Parsing a .meta file");
|
|
println!("---------------------------------");
|
|
|
|
let meta_content = r#"
|
|
fileFormatVersion: 2
|
|
guid: 4ab6bfb0ff54cdf4c8dd38ca244d6f15
|
|
PrefabImporter:
|
|
externalObjects: {}
|
|
userData:
|
|
assetBundleName:
|
|
assetBundleVariant:
|
|
"#;
|
|
|
|
let meta = MetaFile::from_str(meta_content)?;
|
|
println!("Parsed GUID: {}", meta.guid());
|
|
println!("File format version: {:?}\n", meta.file_format_version());
|
|
|
|
// Example 2: Load Unity files with automatic .meta parsing
|
|
println!("Example 2: Loading Unity files with .meta files");
|
|
println!("-------------------------------------------------");
|
|
|
|
let test_dir = "data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Prefabs/Menu/Battle/Hand";
|
|
|
|
if Path::new(test_dir).exists() {
|
|
// Load all Unity files in the directory
|
|
let loaded_files = project.load_directory(test_dir)?;
|
|
|
|
println!("Loaded {} Unity files", loaded_files.len());
|
|
println!("Found {} GUID mappings\n", project.guid_mappings().len());
|
|
|
|
// Example 3: Inspect GUID mappings
|
|
println!("Example 3: GUID to Path Mappings");
|
|
println!("---------------------------------");
|
|
|
|
for (guid, path) in project.guid_mappings().iter().take(5) {
|
|
println!("GUID: {} -> {:?}", guid, path.file_name().unwrap());
|
|
}
|
|
println!();
|
|
|
|
// Example 4: Look up a file by GUID
|
|
println!("Example 4: Looking up files by GUID");
|
|
println!("------------------------------------");
|
|
|
|
if let Some((sample_guid, _)) = project.guid_mappings().iter().next() {
|
|
if let Some(path) = project.get_path_by_guid(sample_guid) {
|
|
println!("GUID {} resolves to:", sample_guid);
|
|
println!(" Path: {:?}", path);
|
|
|
|
// Get the file
|
|
if let Some(file) = project.get_file(path) {
|
|
println!(" Documents: {}", file.documents.len());
|
|
|
|
// Show the first GameObject
|
|
for doc in &file.documents {
|
|
if doc.is_game_object() {
|
|
if let Some(obj) = doc.get("GameObject").and_then(|v| v.as_object()) {
|
|
if let Some(name) = obj.get("m_Name").and_then(|v| v.as_str()) {
|
|
println!(" Contains GameObject: {}", name);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
println!();
|
|
|
|
// Example 5: Cross-file reference resolution (when available)
|
|
println!("Example 5: Cross-file Reference Resolution");
|
|
println!("-------------------------------------------");
|
|
|
|
// Find all external references in loaded files
|
|
let mut external_ref_count = 0;
|
|
|
|
for file in project.files().values() {
|
|
for doc in &file.documents {
|
|
// Scan properties for external references
|
|
for value in doc.properties.values() {
|
|
if let Some(ext_ref) = value.as_external_ref() {
|
|
external_ref_count += 1;
|
|
|
|
// Try to resolve this GUID
|
|
if let Some(target_path) = project.get_path_by_guid(&ext_ref.guid) {
|
|
println!("✓ External reference resolved:");
|
|
println!(" GUID: {}", ext_ref.guid);
|
|
println!(" Target: {:?}", target_path.file_name().unwrap());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
println!("\nFound {} external references in loaded files", external_ref_count);
|
|
} else {
|
|
println!("Test data directory not found: {}", test_dir);
|
|
println!("This example works best with Unity sample project files.");
|
|
}
|
|
|
|
Ok(())
|
|
}
|