SHK Parser
A Rust library for parsing Stronghold Kingdoms attack formation files (.cas files).
Features
- ๐ Fast & Safe: Written in Rust with zero-copy parsing where possible
- ๐ฏ Type-Safe: Strong typing for all unit types and abilities
- ๐ Well-Documented: Comprehensive API documentation and examples
- ๐งช Well-Tested: Extensive test coverage with real game files
- ๐ง Easy to Use: Simple, clean API with helpful error messages
- ๐ Serialization: Optional JSON support via serde
- ๐ป CLI Tool: Command-line interface with multiple output formats
- โก Better Errors: Detailed error messages with thiserror
Optional Features
Enable additional functionality with cargo features:
[]
= { = "0.1.0", = [
"serde",
"cli"
] }
serde: Enables JSON serialization/deserializationcli: Enables the command-line interface
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
Parse a formation file:
use parse_formation_file;
Supported Units
The parser supports all Stronghold Kingdoms unit types:
Basic Units
- Archer: Basic ranged units
- Pikeman: Basic melee units
Advanced Units
- Catapult: Siege weapons with target coordinates
- Captain: Special command units with various abilities
Captain Abilities
- Delay: Wait for a specified time before acting
- Rallying Cry: Rally nearby troops to boost morale
- Arrow Volley: Coordinate arrow attack at target location
- Battle Cry: Boost unit morale and effectiveness
- Catapults Volley: Coordinate catapult bombardment at target
API Reference
Core Functions
// Parse from raw bytes
Types
Command Line Usage
With the cli feature enabled, you can use the binary tool:
# Parse all test files (default behavior)
# Parse specific files
# Verbose output with detailed parsing info
# JSON output (requires serde feature)
CLI Options
A parser for Stronghold Kingdoms formation files (.cas)
Usage: parse_formations [OPTIONS] [FILES]...
Arguments:
[FILES]... Formation file(s) to parse
Options:
-f, --format <FORMAT> Output format [default: human]
-v, --verbose Show detailed unit information
-h, --help Print help
File Format
.cas files use a simple binary format:
- Header (4 bytes): Number of units as little-endian u32
- Unit Records (variable length): One record per unit
Record Formats
| Unit Type | Size | Format |
|---|---|---|
| Archer/Pikeman | 3 bytes | x, y, unit_type |
| Catapult | 5 bytes | x, y, unit_type, target_x, target_y |
| Captain (simple) | 4 bytes | x, y, unit_type, wait_time |
| Captain (with target) | 6 bytes | x, y, unit_type, wait_time, target_x, target_y |
Examples
Parse and Display All Units
use parse_formation_file;
let units = parse_formation_file?;
for unit in &units
Filter by Unit Type
use ;
let units = parse_formation_file?;
// Find all captains
let captains: = units.iter
.filter
.collect;
println!;
Access Captain Abilities
use ;
let units = parse_formation_file?;
for unit in &units
Error Handling
The library provides detailed error information:
use ;
match parse_formation_file
Testing
Run the test suite:
The library includes tests with real .cas files to ensure compatibility.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.