4.8 KiB
4.8 KiB
XML Parsing in Cursebreaker Parser
This document describes the XML parsing functionality added to the cursebreaker-parser project.
Overview
The parser now supports loading game data from Cursebreaker's XML files and storing them in efficient data structures for runtime access and SQL database serialization.
Features
- ✅ Parse Items.xml with full attribute and nested element support
- ✅ In-memory database with fast lookups by ID, name, category, slot, and skill
- ✅ JSON serialization for SQL database storage
- ✅ Type-safe data structures with serde support
- ✅ Easy-to-use API
Quick Start
Loading Items
use cursebreaker_parser::ItemDatabase;
let item_db = ItemDatabase::load_from_xml("Data/XMLs/Items/Items.xml")?;
println!("Loaded {} items", item_db.len());
Querying Items
// Get by ID
if let Some(item) = item_db.get_by_id(150) {
println!("Found: {}", item.name);
}
// Get by category
let bows = item_db.get_by_category("bow");
// Get by slot
let weapons = item_db.get_by_slot("weapon");
// Get by skill requirement
let magic_items = item_db.get_by_skill("magic");
// Get all items
for item in item_db.all_items() {
println!("{}: {}", item.id, item.name);
}
SQL Serialization
// Prepare items for SQL insertion
let sql_data = item_db.prepare_for_sql();
for (id, name, json_data) in sql_data {
// INSERT INTO items (id, name, data) VALUES (?, ?, ?)
// Use your preferred SQL library to insert
}
Data Structures
Item
The main Item struct contains all item attributes from the XML:
pub struct Item {
// Required
pub id: i32,
pub name: String,
// Optional attributes
pub level: Option<i32>,
pub description: Option<String>,
pub price: Option<i32>,
pub slot: Option<String>,
pub category: Option<String>,
pub skill: Option<String>,
// ... many more fields
// Nested elements
pub stats: Vec<ItemStat>,
pub crafting_recipes: Vec<CraftingRecipe>,
pub animations: Option<AnimationSet>,
pub generate_rules: Vec<GenerateRule>,
}
ItemStat
Represents item statistics:
pub struct ItemStat {
// Damage
pub damagephysical: Option<i32>,
pub damagemagical: Option<i32>,
pub damageranged: Option<i32>,
// Accuracy
pub accuracyphysical: Option<i32>,
pub accuracymagical: Option<i32>,
pub accuracyranged: Option<i32>,
// Resistance
pub resistancephysical: Option<i32>,
pub resistancemagical: Option<i32>,
pub resistanceranged: Option<i32>,
// Core stats
pub health: Option<i32>,
pub mana: Option<i32>,
pub manaregen: Option<i32>,
pub healing: Option<i32>,
// Harvesting
pub harvestingspeedwoodcutting: Option<i32>,
}
Example Program
Run the demo to see all features in action:
cargo run --example item_database_demo
Statistics from Items.xml
When loaded from /home/connor/repos/CBAssets/Data/XMLs/Items/Items.xml:
- Total Items: 1,360
- Weapons: 166
- Armor: 148
- Consumables: 294
- Trinkets: 59
- Bows: 18
- Magic Items: 76
File Structure
cursebreaker-parser/
├── src/
│ ├── lib.rs # Library exports
│ ├── main.rs # Main binary (includes Unity + XML parsing)
│ ├── types/
│ │ ├── mod.rs
│ │ ├── item.rs # Item data structures
│ │ └── interactable_resource.rs
│ ├── xml_parser.rs # XML parsing logic
│ └── item_database.rs # ItemDatabase for runtime access
└── examples/
└── item_database_demo.rs # Full usage example
Dependencies Added
quick-xml = "0.37" # XML parsing
serde = { version = "1.0", features = ["derive"] } # Serialization
serde_json = "1.0" # JSON serialization
diesel = { version = "2.2", features = ["sqlite"], optional = true } # SQL (optional)
thiserror = "1.0" # Error handling
Future Enhancements
The same pattern can be extended to parse other XML files:
- NPCs (
/XMLs/Npcs/*.xml) - Quests (
/XMLs/Quests/*.xml) - Loot tables (
/XMLs/Loot/*.xml) - Maps (
/XMLs/Maps/*.xml) - Dialogue (
/XMLs/Dialogue/*.xml) - Events (
/XMLs/Events/*.xml)
Each would follow the same pattern:
- Define data structures in
src/types/ - Create parser in
src/xml_parser.rs - Create database wrapper for runtime access
- Add to
lib.rsexports
Integration with Unity Parser
The main binary (src/main.rs) demonstrates integration of both systems:
- Load game data from XML files (Items, etc.)
- Parse Unity scenes for game objects
- Cross-reference data (e.g., item IDs in loot spawners)
This creates a complete game data pipeline from source files to runtime.