Part of #41: the comments of `benilla-ui` (the Lua UI engine, its FrameXML loader and its widgets) are rewritten to the rule in `docs/METHOD.md`, which is what the code does and the reference fact behind it, in a line. 43,140 comment lines become 15,161. - **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: - `0x64a480` is the case-sensitive compare (a byte `repz cmpsb`), and `0x64a4c0` the case-insensitive one. - A frame's id sits at `+0xb0`, not `+0xb4`. `EnableKeyboard`/`IsKeyboardEnabled` are `0x776ec0`/`0x776f90`. - A region's unpinned edge has no fallback to its owner's rect: the owner supplies only the scale. - `$parent` expands to the nearest named ancestor, or "Top" when none is named (`0x76c5b0`), not simply to the parent. - A StatusBar fills from its internal normalised fraction, not from `GetValue()`. - The addresses of `StartDuel` and `StartDuelUnit` were swapped. - Many "untraced" notes now give the traced reference fact: the drag threshold, the per-grip sizing edges, the empty-`autoFocus` skip, and `SetTexCoordModifiesRect`'s default. - Stock FrameXML line citations are checked against the stock 1.12 files and corrected. `ref-` file names become the stock file names. - **Deviation labels.** "Deviation:" now marks only a choice with a reason. A surface that exists only for the retired Era addon target says so plainly: it is not 1.12. - **Verification.** Every file strips to identical code, and `cargo fmt --check` is clean. Nine second readers compared every file old against new, restored the facts the rewrite had dropped, and checked each new citation against the code, the stock files, vmangos and the reference. Behaviour differences the rewrite surfaced are filed as their own issues (#131–#142), and more are being validated. The comments here state what the code does. 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>
223 lines
7.7 KiB
Rust
223 lines
7.7 KiB
Rust
//! The stance bar's four bindings, which `BonusActionBarFrame.lua`'s `ShapeshiftBar_*` calls, over
|
|
//! a form list the app resolves and pushes; flags answer 1/nil and the cooldown is the reference's
|
|
//! `(start, duration, enable)`.
|
|
|
|
use mlua::{Lua, MultiValue, Value};
|
|
|
|
use super::Model;
|
|
|
|
/// One stance-bar button, resolved by the app.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct ShapeshiftFormView {
|
|
/// The form spell a click queues.
|
|
pub spell_id: u32,
|
|
/// The icon texture path; `None` shows the slot's fallback.
|
|
pub texture: Option<String>,
|
|
/// The spell name, `GetShapeshiftFormInfo`'s second return.
|
|
pub name: String,
|
|
/// The player's current form (the checked ring).
|
|
pub active: bool,
|
|
/// Castable now; the reference greys the icon to 0.4 when not.
|
|
pub castable: bool,
|
|
/// `(start_ms on the GetTime clock, duration_ms, enabled)`, as an action's cooldown.
|
|
pub cooldown: Option<(i64, u32, bool)>,
|
|
}
|
|
|
|
/// A pushed form with its cooldown in `GetTime` seconds.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub(crate) struct StoredShapeshiftForm {
|
|
pub(crate) view: ShapeshiftFormView,
|
|
/// `(start_s, duration_s, enabled)`.
|
|
pub(crate) cooldown: Option<(f64, f64, bool)>,
|
|
}
|
|
|
|
impl super::UiScript {
|
|
/// Replace the form list, in bar order; the app fires `UPDATE_SHAPESHIFT_FORMS` itself.
|
|
pub fn set_shapeshift_forms(&mut self, forms: Vec<ShapeshiftFormView>) {
|
|
let mut model = self.model_mut();
|
|
model.shapeshift_forms = forms
|
|
.into_iter()
|
|
.map(|view| {
|
|
let cooldown = view.cooldown.map(|(start_ms, duration_ms, enabled)| {
|
|
(
|
|
start_ms as f64 / 1000.0,
|
|
f64::from(duration_ms) / 1000.0,
|
|
enabled,
|
|
)
|
|
});
|
|
StoredShapeshiftForm { view, cooldown }
|
|
})
|
|
.collect();
|
|
}
|
|
|
|
/// Drain the form spells `CastShapeshiftForm` queued; the app casts each, or cancels it when it
|
|
/// is the active form.
|
|
pub fn take_shapeshift_casts(&mut self) -> Vec<u32> {
|
|
std::mem::take(&mut self.model_mut().shapeshift_casts)
|
|
}
|
|
}
|
|
|
|
/// A 1-based button index, as the reference indexes.
|
|
fn form_at(model: &Model, i: u32) -> Option<&StoredShapeshiftForm> {
|
|
usize::try_from(i.checked_sub(1)?)
|
|
.ok()
|
|
.and_then(|n| model.shapeshift_forms.get(n))
|
|
}
|
|
|
|
pub(super) fn install(lua: &Lua) -> mlua::Result<()> {
|
|
let g = lua.globals();
|
|
|
|
g.set(
|
|
"GetNumShapeshiftForms",
|
|
lua.create_function(|lua, ()| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(model.shapeshift_forms.len() as i64)
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"GetShapeshiftFormInfo",
|
|
lua.create_function(|lua, i: u32| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
let Some(form) = form_at(&model, i) else {
|
|
return Ok(MultiValue::from_vec(vec![Value::Nil]));
|
|
};
|
|
let texture = match &form.view.texture {
|
|
Some(t) => Value::String(lua.create_string(t)?),
|
|
None => Value::Nil,
|
|
};
|
|
let flag = |b: bool| if b { Value::Integer(1) } else { Value::Nil };
|
|
Ok(MultiValue::from_vec(vec![
|
|
texture,
|
|
Value::String(lua.create_string(&form.view.name)?),
|
|
flag(form.view.active),
|
|
flag(form.view.castable),
|
|
]))
|
|
})?,
|
|
)?;
|
|
|
|
// As `GetActionCooldown`: an elapsed or absent cooldown answers `(0, 0, 1)`, so a re-feed never
|
|
// replays the sweep.
|
|
g.set(
|
|
"GetShapeshiftFormCooldown",
|
|
lua.create_function(|lua, i: u32| {
|
|
let now: f64 = lua.globals().get("__benilla_now").unwrap_or(0.0);
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(match form_at(&model, i).and_then(|f| f.cooldown) {
|
|
Some((start, duration, enabled)) if start + duration > now || !enabled => {
|
|
(start, duration, i32::from(enabled))
|
|
}
|
|
_ => (0.0, 0.0, 1),
|
|
})
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"CastShapeshiftForm",
|
|
lua.create_function(|lua, i: u32| {
|
|
let mut model = lua.app_data_mut::<Model>().expect("model app_data");
|
|
if let Some(spell_id) = form_at(&model, i).map(|f| f.view.spell_id) {
|
|
model.shapeshift_casts.push(spell_id);
|
|
}
|
|
Ok(())
|
|
})?,
|
|
)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::ShapeshiftFormView;
|
|
use crate::script::UiScript;
|
|
|
|
fn forms() -> Vec<ShapeshiftFormView> {
|
|
vec![
|
|
ShapeshiftFormView {
|
|
spell_id: 2457,
|
|
texture: Some("Interface\\Icons\\Ability_Warrior_OffensiveStance".into()),
|
|
name: "Battle Stance".into(),
|
|
active: true,
|
|
castable: true,
|
|
cooldown: None,
|
|
},
|
|
ShapeshiftFormView {
|
|
spell_id: 71,
|
|
texture: Some("Interface\\Icons\\Ability_Warrior_DefensiveStance".into()),
|
|
name: "Defensive Stance".into(),
|
|
active: false,
|
|
castable: true,
|
|
cooldown: Some((9400, 1500, true)),
|
|
},
|
|
]
|
|
}
|
|
|
|
#[test]
|
|
fn form_info_reads_and_out_of_range_nil() {
|
|
let mut s = UiScript::new().unwrap();
|
|
assert_eq!(s.eval::<i64>("return GetNumShapeshiftForms()").unwrap(), 0);
|
|
assert!(s
|
|
.eval::<bool>("return GetShapeshiftFormInfo(1) == nil")
|
|
.unwrap());
|
|
|
|
s.set_shapeshift_forms(forms());
|
|
assert_eq!(s.eval::<i64>("return GetNumShapeshiftForms()").unwrap(), 2);
|
|
|
|
let (texture, name, active, castable) = s
|
|
.eval::<(String, String, Option<i64>, Option<i64>)>("return GetShapeshiftFormInfo(1)")
|
|
.unwrap();
|
|
assert_eq!(
|
|
(texture.as_str(), name.as_str(), active, castable),
|
|
(
|
|
"Interface\\Icons\\Ability_Warrior_OffensiveStance",
|
|
"Battle Stance",
|
|
Some(1),
|
|
Some(1)
|
|
)
|
|
);
|
|
assert!(s
|
|
.eval::<bool>("local _, _, a, c = GetShapeshiftFormInfo(2) return a == nil and c == 1")
|
|
.unwrap());
|
|
assert!(s
|
|
.eval::<bool>("return GetShapeshiftFormInfo(3) == nil")
|
|
.unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn cooldown_triple_stamps_to_the_vm_clock_and_goes_cold() {
|
|
let mut s = UiScript::new().unwrap();
|
|
s.tick(10.0); // GetTime == 10
|
|
s.set_shapeshift_forms(forms());
|
|
|
|
assert_eq!(
|
|
s.eval::<(f64, f64, i32)>("return GetShapeshiftFormCooldown(1)")
|
|
.unwrap(),
|
|
(0.0, 0.0, 1)
|
|
);
|
|
let (start, duration, enable) = s
|
|
.eval::<(f64, f64, i32)>("return GetShapeshiftFormCooldown(2)")
|
|
.unwrap();
|
|
assert!((start - 9.4).abs() < 1e-9, "start {start}");
|
|
assert!((duration - 1.5).abs() < 1e-9);
|
|
assert_eq!(enable, 1);
|
|
|
|
s.tick(2.0); // now == 12 > 9.4 + 1.5
|
|
assert_eq!(
|
|
s.eval::<(f64, f64, i32)>("return GetShapeshiftFormCooldown(2)")
|
|
.unwrap(),
|
|
(0.0, 0.0, 1)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn cast_queues_the_form_spell_and_ignores_out_of_range() {
|
|
let mut s = UiScript::new().unwrap();
|
|
s.set_shapeshift_forms(forms());
|
|
|
|
s.run("CastShapeshiftForm(2)").unwrap();
|
|
s.run("CastShapeshiftForm(9)").unwrap(); // out of range: no-op
|
|
assert_eq!(s.take_shapeshift_casts(), vec![71]);
|
|
assert!(s.take_shapeshift_casts().is_empty(), "drain empties");
|
|
}
|
|
}
|