Files
benilla-octo/crates/benilla-formats/src/server_messages.rs
T
Sam SungandClaude Opus 5.5 8da634afa9 Comments in benilla-formats: one line of fact each, no history, no record numbers (#130)
Part of #41: the comments of `benilla-formats` (the library, its extract tools, examples and tests) are rewritten to the rule in `docs/METHOD.md`, which is what the code does and the reference fact behind it, in a line. 19,654 comment lines become 7,264.

Fixes #105: the WMO collision comments now name walking's box-query leaf `0x6bca50` and the camera and line-of-sight segment-query leaf `0x6bc700`, in `models/collision.rs`, `examples/wmo_bsp.rs` and `tests/chain.rs`.

- **The mechanical strip.** The citations that were only a private record number go first, and each edit is shown to be a pure removal.
- **Wrong facts corrected.** The rewrite corrects the comments that stated wrong facts. Among them:
  - A placed doodad's material alpha reads file slot 0 on its spawn clock, while the reference reads bone 0's armed sequence, re-rolled every play window. The old text said doodads are armed once at load.
  - The fog end is stored ×36 and scaled once at load (`0x6d6090`). It is not raw.
  - The MCSH bit picks a map doodad's sun scale, 1.0 lit or 0.5 shadowed. 2.5 belongs to an entity's light node.
  - `SpellRec` is the row as stored, column × 4, not a translated copy.
  - A global sequence's clock is anchored at the instance's attach time (`0x714352`).
  - Particle twinkle is a gated size multiplier (`0x7b2adc`).
  - The blob shadow sizes from the Stand box.
  - Data claims are checked against the install: the weapon-trail kits' parameters and alphas, the beam rows' noise, and the maps' `field_16`.
  - Dozens more turned up on the way: renamed or deleted items, docs sitting on the wrong item, stale counts and links.
- **Deviation labels.** "Deviation:" now marks only a choice with a reason. An unbuilt behaviour says so plainly.
- **Verification.** Every file strips to identical code, and `cargo fmt --check` is clean. Four second readers compared every file old against new, restored the facts the rewrite had dropped, and checked each new citation against the reference, the code and the shipped data.

String literals are untouched; they are #92.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 19:38:53 +07:00

138 lines
4.6 KiB
Rust

//! `ServerMessages.dbc`, the five sentences the server can say to everyone. `SMSG_SERVER_MESSAGE`
//! carries a row id and a fill: the client indexes this table by `messageType` (`0x49dfbb`, store
//! `[0xc0d990]`) and fills the row's `%s` with the packet's text (`snprintf(buf, 0x400, …)` at
//! `0x49dff0`), or copies the row when the text is empty (`0x49dfc8`, `0x64a5a0`). The ids are
//! vmangos's `ServerMessageType` (`World.h:62`).
use anyhow::{Context, Result};
use benilla_dbc::{FieldType, Schema, SchemaField};
use crate::chain::Chain;
use crate::dbc::{parse, str_at, u32_at};
const SERVER_MESSAGES: &str = "DBFilesClient\\ServerMessages.dbc";
/// The five rows, keyed by id.
#[derive(Clone, Debug, Default)]
pub struct ServerMessagesCatalog {
rows: Vec<(u32, String)>,
}
impl ServerMessagesCatalog {
/// A catalog of given rows, for tests without an install.
pub fn from_rows(rows: Vec<(u32, String)>) -> Self {
ServerMessagesCatalog { rows }
}
/// The format string for a `messageType`.
pub fn text(&self, message_type: u32) -> Option<&str> {
self.rows
.iter()
.find(|(id, _)| *id == message_type)
.map(|(_, t)| t.as_str())
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
pub fn len(&self) -> usize {
self.rows.len()
}
/// The line `SMSG_SERVER_MESSAGE` becomes (`0x49dfab`-`0x49e030`): a row and a fill format, a
/// row alone is copied, and no row falls back to the reference's `"[%d]: %s"` (`0x844864`).
///
/// Deviation: a row with two `%s` fills only the first, because the reference's one-argument
/// `snprintf` fills the second with stack garbage; no shipped row has two.
pub fn compose(&self, message_type: u32, fill: &str) -> String {
match self.text(message_type) {
Some(row) if !fill.is_empty() => row.replacen("%s", fill, 1),
Some(row) => row.to_string(),
None => format!("[{message_type}]: {fill}"),
}
}
}
fn schema() -> Schema {
let mut s = Schema::new("ServerMessages");
s.add_field(SchemaField::new("ID", FieldType::UInt32));
s.add_field(SchemaField::new_array("Text", FieldType::String, 8));
s.add_field(SchemaField::new("TextMask", FieldType::UInt32));
s.set_key_field("ID");
s
}
pub fn load_server_messages_catalog(chain: &mut Chain) -> Result<ServerMessagesCatalog> {
let bytes = chain
.read_file(SERVER_MESSAGES)
.context("reading ServerMessages.dbc")?;
let rs = parse(&bytes, schema(), "ServerMessages")?;
let mut rows = Vec::with_capacity(rs.records().len());
for r in rs.records() {
let (Some(id), Some(text)) = (u32_at(r, 0), str_at(&rs, r, 1)) else {
continue;
};
rows.push((id, text));
}
Ok(ServerMessagesCatalog { rows })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_real_table_is_the_five_documented_rows() {
let data = crate::wow_data_or_skip!();
let mut chain = crate::open_chain(&data).expect("open chain");
let cat = load_server_messages_catalog(&mut chain).expect("load ServerMessages");
let got: Vec<(u32, &str)> = cat.rows.iter().map(|(i, t)| (*i, t.as_str())).collect();
assert_eq!(
got,
vec![
(1, "[SERVER] Shutdown in %s"),
(2, "[SERVER] Restart in %s"),
(3, "%s"),
(4, "[SERVER] Shutdown cancelled"),
(5, "[SERVER] Restart cancelled"),
]
);
}
fn shipped() -> ServerMessagesCatalog {
ServerMessagesCatalog::from_rows(vec![
(1, "[SERVER] Shutdown in %s".into()),
(2, "[SERVER] Restart in %s".into()),
(3, "%s".into()),
(4, "[SERVER] Shutdown cancelled".into()),
(5, "[SERVER] Restart cancelled".into()),
])
}
#[test]
fn a_countdown_fills_its_row() {
assert_eq!(
shipped().compose(1, "15 Minutes"),
"[SERVER] Shutdown in 15 Minutes"
);
assert_eq!(shipped().compose(3, "back in five"), "back in five");
}
#[test]
fn a_cancellation_copies_its_row_whole() {
assert_eq!(shipped().compose(5, ""), "[SERVER] Restart cancelled");
}
#[test]
fn an_unknown_type_falls_back_to_the_bracketed_form() {
assert_eq!(shipped().compose(9, "something new"), "[9]: something new");
}
/// The reference tests the fill, not the row: a `%s` row with no fill is copied, token and all.
#[test]
fn an_empty_fill_never_formats() {
assert_eq!(shipped().compose(1, ""), "[SERVER] Shutdown in %s");
}
}