227 lines
9.6 KiB
Rust
227 lines
9.6 KiB
Rust
//! Parse Cursebreaker Resources from 10_3.unity Scene
|
|
//!
|
|
//! This example demonstrates:
|
|
//! 1. Parsing the Cursebreaker Unity project
|
|
//! 2. Finding Interactable_Resource components
|
|
//! 3. Extracting typeId and transform positions
|
|
//! 4. Writing resource data to an output file
|
|
|
|
use cursebreaker_parser::{ItemDatabase, NpcDatabase, QuestDatabase, HarvestableDatabase, LootDatabase, MapDatabase, FastTravelDatabase, PlayerHouseDatabase, TraitDatabase, ShopDatabase, InteractableResource, MinimapDatabase};
|
|
use unity_parser::UnityProject;
|
|
use std::path::Path;
|
|
use unity_parser::log::DedupLogger;
|
|
use log::{info, error, warn, LevelFilter};
|
|
use diesel::prelude::*;
|
|
use diesel::sqlite::SqliteConnection;
|
|
use std::env;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let logger = DedupLogger::new();
|
|
log::set_boxed_logger(Box::new(logger))
|
|
.map(|()| log::set_max_level(LevelFilter::Trace))
|
|
.unwrap();
|
|
// log::set_max_level(LevelFilter::Warn);
|
|
|
|
info!("🎮 Cursebreaker - Resource Parser");
|
|
|
|
// Load items from XML
|
|
info!("📚 Loading game data from XML...");
|
|
|
|
let cb_assets_path = env::var("CB_ASSETS_PATH").unwrap_or_else(|_| "/home/connor/repos/CBAssets".to_string());
|
|
let items_path = format!("{}/Data/XMLs/Items/Items.xml", cb_assets_path);
|
|
let item_db = ItemDatabase::load_from_xml(items_path)?;
|
|
info!("✅ Loaded {} items", item_db.len());
|
|
|
|
let npcs_path = format!("{}/Data/XMLs/Npcs/NPCInfo.xml", cb_assets_path);
|
|
let npc_db = NpcDatabase::load_from_xml(npcs_path)?;
|
|
info!("✅ Loaded {} NPCs", npc_db.len());
|
|
|
|
let quests_path = format!("{}/Data/XMLs/Quests/Quests.xml", cb_assets_path);
|
|
let quest_db = QuestDatabase::load_from_xml(quests_path)?;
|
|
info!("✅ Loaded {} quests", quest_db.len());
|
|
|
|
let harvestables_path = format!("{}/Data/XMLs/Harvestables/HarvestableInfo.xml", cb_assets_path);
|
|
let harvestable_db = HarvestableDatabase::load_from_xml(harvestables_path)?;
|
|
info!("✅ Loaded {} harvestables", harvestable_db.len());
|
|
|
|
let loot_path = format!("{}/Data/XMLs/Loot/Loot.xml", cb_assets_path);
|
|
let loot_db = LootDatabase::load_from_xml(loot_path)?;
|
|
info!("✅ Loaded {} loot tables", loot_db.len());
|
|
|
|
let maps_path = format!("{}/Data/XMLs/Maps/Maps.xml", cb_assets_path);
|
|
let map_db = MapDatabase::load_from_xml(maps_path)?;
|
|
info!("✅ Loaded {} maps", map_db.len());
|
|
|
|
let fast_travel_dir = format!("{}/Data/XMLs", cb_assets_path);
|
|
let fast_travel_db = FastTravelDatabase::load_from_directory(fast_travel_dir)?;
|
|
info!("✅ Loaded {} fast travel locations", fast_travel_db.len());
|
|
|
|
let player_houses_path = format!("{}/Data/XMLs/PlayerHouses/PlayerHouses.xml", cb_assets_path);
|
|
let player_house_db = PlayerHouseDatabase::load_from_xml(player_houses_path)?;
|
|
info!("✅ Loaded {} player houses", player_house_db.len());
|
|
|
|
let traits_path = format!("{}/Data/XMLs/Traits/Traits.xml", cb_assets_path);
|
|
let trait_db = TraitDatabase::load_from_xml(traits_path)?;
|
|
info!("✅ Loaded {} traits", trait_db.len());
|
|
|
|
let shops_path = format!("{}/Data/XMLs/Shops/Shops.xml", cb_assets_path);
|
|
let shop_db = ShopDatabase::load_from_xml(shops_path)?;
|
|
info!("✅ Loaded {} shops", shop_db.len());
|
|
|
|
// Save to SQLite database
|
|
info!("\n💾 Saving game data to SQLite database...");
|
|
let mut conn = SqliteConnection::establish("cursebreaker.db")?;
|
|
|
|
match item_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} items to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save items: {}", e),
|
|
}
|
|
|
|
match npc_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} NPCs to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save NPCs: {}", e),
|
|
}
|
|
|
|
match quest_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} quests to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save quests: {}", e),
|
|
}
|
|
|
|
match harvestable_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} harvestables to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save harvestables: {}", e),
|
|
}
|
|
|
|
match loot_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} loot tables to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save loot tables: {}", e),
|
|
}
|
|
|
|
match map_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} maps to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save maps: {}", e),
|
|
}
|
|
|
|
match fast_travel_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} fast travel locations to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save fast travel locations: {}", e),
|
|
}
|
|
|
|
match player_house_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} player houses to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save player houses: {}", e),
|
|
}
|
|
|
|
match trait_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} traits to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save traits: {}", e),
|
|
}
|
|
|
|
match shop_db.save_to_db(&mut conn) {
|
|
Ok(count) => info!("✅ Saved {} shops to database", count),
|
|
Err(e) => warn!("⚠️ Failed to save shops: {}", e),
|
|
}
|
|
|
|
// Print statistics
|
|
info!("\n📊 Game Data Statistics:");
|
|
info!(" Items:");
|
|
info!(" • Weapons: {}", item_db.get_by_slot("weapon").len());
|
|
info!(" • Consumables: {}", item_db.get_by_slot("consumable").len());
|
|
info!(" NPCs:");
|
|
info!(" • Hostile: {}", npc_db.get_hostile().len());
|
|
info!(" • Interactable: {}", npc_db.get_interactable().len());
|
|
info!(" Quests:");
|
|
info!(" • Main quests: {}", quest_db.get_main_quests().len());
|
|
info!(" • Side quests: {}", quest_db.get_side_quests().len());
|
|
info!(" Harvestables:");
|
|
info!(" • Trees: {}", harvestable_db.get_trees().len());
|
|
info!(" • Woodcutting: {}", harvestable_db.get_by_skill("Woodcutting").len());
|
|
info!(" • Mining: {}", harvestable_db.get_by_skill("mining").len());
|
|
info!(" • Fishing: {}", harvestable_db.get_by_skill("Fishing").len());
|
|
info!(" • Alchemy: {}", harvestable_db.get_by_skill("Alchemy").len());
|
|
info!(" Loot:");
|
|
info!(" • Total tables: {}", loot_db.len());
|
|
info!(" • NPCs with loot: {}", loot_db.get_all_npcs_with_loot().len());
|
|
info!(" • Droppable items: {}", loot_db.get_all_droppable_items().len());
|
|
info!(" • Tables with conditional drops: {}", loot_db.get_conditional_tables().len());
|
|
|
|
// Initialize Unity project once - scans entire project for GUID mappings
|
|
let project_root = Path::new(&cb_assets_path);
|
|
info!("\n📦 Initializing Unity project from: {}", project_root.display());
|
|
|
|
let project = UnityProject::from_path(project_root)?;
|
|
|
|
// Now parse the scene using the pre-built GUID resolvers
|
|
let scene_path = "_GameAssets/Scenes/Tiles/10_3.unity";
|
|
info!("📁 Parsing scene: {}", scene_path);
|
|
|
|
log::logger().flush();
|
|
|
|
// Parse the scene using the project
|
|
match project.parse_scene(scene_path) {
|
|
Ok(mut scene) => {
|
|
info!("✅ Scene parsed successfully!");
|
|
info!(" Total entities: {}", scene.entity_map.len());
|
|
|
|
// Post-processing: Compute world transforms
|
|
info!("🔄 Computing world transforms...");
|
|
unity_parser::compute_world_transforms(&mut scene.world, &scene.entity_map);
|
|
info!(" ✓ World transforms computed");
|
|
|
|
// Get views for component types we need
|
|
// Find all entities that have Interactable_Resource
|
|
log::logger().flush();
|
|
|
|
scene.world
|
|
.query_all::<(&InteractableResource, &unity_parser::WorldTransform, &unity_parser::GameObject)>()
|
|
.for_each(|(resource, transform, object)| {
|
|
info!(" 📦 Resource: \"{}\"", object.name);
|
|
info!(" • typeId: {}", resource.type_id);
|
|
|
|
// Extract world position from WorldTransform
|
|
let world_pos = transform.position();
|
|
info!(" • Position: ({:.2}, {:.2}, {:.2})", world_pos.x, world_pos.y, world_pos.z);
|
|
log::logger().flush();
|
|
});
|
|
|
|
log::logger().flush();
|
|
}
|
|
Err(e) => {
|
|
error!("Parse error: {}", e);
|
|
return Err(Box::new(e));
|
|
}
|
|
}
|
|
|
|
log::logger().flush();
|
|
|
|
// Process minimap tiles
|
|
info!("\n🗺️ Processing minimap tiles...");
|
|
let minimap_db = MinimapDatabase::new("cursebreaker.db".to_string());
|
|
|
|
let minimap_path = format!("{}/Data/Textures/MinimapSquares", cb_assets_path);
|
|
match minimap_db.load_from_directory(&minimap_path, &cb_assets_path) {
|
|
Ok(count) => {
|
|
info!("✅ Processed {} minimap tiles", count);
|
|
|
|
if let Ok(stats) = minimap_db.get_storage_stats() {
|
|
info!(" Storage Statistics:");
|
|
info!(" • Original PNG total: {} MB", stats.total_original_size / 1_048_576);
|
|
info!(" • WebP total: {} MB", stats.total_webp_size() / 1_048_576);
|
|
info!(" • Compression ratio: {:.2}%", stats.compression_ratio());
|
|
}
|
|
|
|
if let Ok(bounds) = minimap_db.get_map_bounds() {
|
|
info!(" Map Bounds:");
|
|
info!(" • Min (x,y): {:?}", bounds.0);
|
|
info!(" • Max (x,y): {:?}", bounds.1);
|
|
}
|
|
}
|
|
Err(e) => {
|
|
error!("Failed to process minimap tiles: {}", e);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|