Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions crates/openlogs-cli/src/game_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

use openlogs_gamedata::{
HttpFetch, download_boss_portraits, download_dungeons, download_map_art,
download_spell_classes, download_spell_tables,
HttpFetch, download_boss_portraits, download_dungeons, download_forever_tables,
download_map_art, download_spell_classes, download_spell_tables,
};

/// Downloads the game data from wago.tools into the files the app loads from `game-data/`.
Expand All @@ -16,6 +16,7 @@ pub fn export(out_dir: &Path) -> Result<(), Box<dyn Error>> {
let dungeons = download_dungeons(&fetch)?;
let portraits = download_boss_portraits(&fetch)?;
let map_art = download_map_art(&fetch)?;
let forever = download_forever_tables(&fetch)?;
let fetched_unix_ms = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis();

fs::create_dir_all(out_dir)?;
Expand All @@ -28,6 +29,20 @@ pub fn export(out_dir: &Path) -> Result<(), Box<dyn Error>> {
fs::write(out_dir.join("dungeons.json"), dungeons.to_json())?;
fs::write(out_dir.join("boss-portraits.json"), portraits.to_json())?;
fs::write(out_dir.join("map-art.json"), map_art.to_json())?;
let forever_dir = out_dir.join("forever");
fs::create_dir_all(&forever_dir)?;
fs::write(
forever_dir.join("spell-icons.json"),
forever.spells.icons.to_json(),
)?;
fs::write(
forever_dir.join("channel-spells.json"),
forever.spells.channels.to_json(),
)?;
fs::write(
forever_dir.join("spell-classes.json"),
forever.classes.to_json(),
)?;
// Written last: the app reads the tables only once the manifest names them.
let manifest = serde_json::json!({
"fetchedUnixMs": fetched_unix_ms,
Expand All @@ -37,6 +52,12 @@ pub fn export(out_dir: &Path) -> Result<(), Box<dyn Error>> {
"dungeons": dungeons.len(),
"bossPortraits": portraits.len(),
"mapArt": map_art.maps.len(),
"forever": {
"build": forever.build,
"spellIcons": forever.spells.icons.len(),
"channelSpells": forever.spells.channels.len(),
"spellClasses": forever.classes.class_spell_count(),
},
});
fs::write(out_dir.join("manifest.json"), manifest.to_string())?;
println!(
Expand All @@ -50,5 +71,13 @@ pub fn export(out_dir: &Path) -> Result<(), Box<dyn Error>> {
map_art.maps.len(),
out_dir.display()
);
println!(
"Forever {}: {} spells, {} channel spells and {} class spells written to {}",
forever.build,
forever.spells.icons.len(),
forever.spells.channels.len(),
forever.classes.class_spell_count(),
forever_dir.display()
);
Ok(())
}
60 changes: 54 additions & 6 deletions crates/openlogs-gamedata/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub const WAGO_UI_MAP_ASSIGNMENT_URL: &str = "https://wago.tools/db2/UiMapAssign
pub const WAGO_UI_MAP_GROUP_MEMBER_URL: &str = "https://wago.tools/db2/UiMapGroupMember/csv";
pub const WAGO_AREA_TABLE_URL: &str = "https://wago.tools/db2/AreaTable/csv";
pub const WAGO_MAP_URL: &str = "https://wago.tools/db2/Map/csv";
/// The newest build of each product, as `{ "<product>": { "version": "…" }, … }`.
pub const WAGO_LATEST_BUILDS_URL: &str = "https://wago.tools/api/builds/latest";
pub const FOREVER_PRODUCT: &str = "wowf";

#[derive(Debug, Error)]
pub enum DownloadError {
Expand All @@ -38,6 +41,10 @@ pub enum DownloadError {
BossPortraits(#[from] BossPortraitError),
#[error("downloaded map art data is invalid: {0}")]
MapArt(#[from] MapArtError),
#[error("downloaded build list is invalid: {0}")]
Builds(#[from] serde_json::Error),
#[error("wago.tools lists no build of {0}")]
NoBuild(&'static str),
}

/// The tables read from the spells' `SpellMisc` rows.
Expand All @@ -46,20 +53,61 @@ pub struct SpellTables {
pub channels: ChannelSpells,
}

/// The spell tables of the Forever build they were exported from.
pub struct ForeverTables {
pub build: String,
pub spells: SpellTables,
pub classes: SpellClasses,
}

/// Builds the spell icon and channel tables from the wago.tools exports.
pub fn download_spell_tables<F: Fetch>(fetch: &F) -> Result<SpellTables, DownloadError> {
let spell_misc = fetch.get(WAGO_SPELL_MISC_URL)?;
let manifest = fetch.get(WAGO_MANIFEST_URL)?;
spell_tables(fetch, None)
}

/// Builds the class and crowd-control spell table from the wago.tools exports.
pub fn download_spell_classes<F: Fetch>(fetch: &F) -> Result<SpellClasses, DownloadError> {
spell_classes(fetch, None)
}

/// Builds the spell tables of the newest Forever build.
pub fn download_forever_tables<F: Fetch>(fetch: &F) -> Result<ForeverTables, DownloadError> {
let build = latest_build(fetch, FOREVER_PRODUCT)?;
Ok(ForeverTables {
spells: spell_tables(fetch, Some(&build))?,
classes: spell_classes(fetch, Some(&build))?,
build,
})
}

fn latest_build<F: Fetch>(fetch: &F, product: &'static str) -> Result<String, DownloadError> {
let builds: serde_json::Value = serde_json::from_slice(&fetch.get(WAGO_LATEST_BUILDS_URL)?)?;
builds[product]["version"]
.as_str()
.map(str::to_string)
.ok_or(DownloadError::NoBuild(product))
}

/// A table of `build`, or of the newest retail build without one.
fn table_url(base: &str, build: Option<&str>) -> String {
match build {
Some(build) => format!("{base}?build={build}"),
None => base.to_string(),
}
}

fn spell_tables<F: Fetch>(fetch: &F, build: Option<&str>) -> Result<SpellTables, DownloadError> {
let spell_misc = fetch.get(&table_url(WAGO_SPELL_MISC_URL, build))?;
let manifest = fetch.get(&table_url(WAGO_MANIFEST_URL, build))?;
Ok(SpellTables {
icons: SpellIconTable::from_csv(&spell_misc, &manifest)?,
channels: ChannelSpells::from_csv(&spell_misc)?,
})
}

/// Builds the class and crowd-control spell table from the wago.tools exports.
pub fn download_spell_classes<F: Fetch>(fetch: &F) -> Result<SpellClasses, DownloadError> {
let spell_class_options = fetch.get(WAGO_SPELL_CLASS_OPTIONS_URL)?;
let spell_effect = fetch.get(WAGO_SPELL_EFFECT_URL)?;
fn spell_classes<F: Fetch>(fetch: &F, build: Option<&str>) -> Result<SpellClasses, DownloadError> {
let spell_class_options = fetch.get(&table_url(WAGO_SPELL_CLASS_OPTIONS_URL, build))?;
let spell_effect = fetch.get(&table_url(WAGO_SPELL_EFFECT_URL, build))?;
Ok(SpellClasses::from_csv(&spell_class_options, &spell_effect)?)
}

Expand Down
11 changes: 6 additions & 5 deletions crates/openlogs-gamedata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ pub use curated::{
RAID_FRAME_AURAS_FILE, RaidFrameAura, RaidFrameAuras,
};
pub use download::{
DownloadError, SpellTables, WAGO_AREA_TABLE_URL, WAGO_DUNGEON_ENCOUNTER_URL,
WAGO_JOURNAL_ENCOUNTER_CREATURE_URL, WAGO_JOURNAL_ENCOUNTER_URL, WAGO_MANIFEST_URL,
WAGO_MAP_CHALLENGE_MODE_URL, WAGO_MAP_URL, WAGO_SPELL_CLASS_OPTIONS_URL, WAGO_SPELL_EFFECT_URL,
WAGO_SPELL_MISC_URL, WAGO_UI_MAP_ASSIGNMENT_URL, WAGO_UI_MAP_GROUP_MEMBER_URL,
download_boss_portraits, download_dungeons, download_map_art, download_spell_classes,
DownloadError, FOREVER_PRODUCT, ForeverTables, SpellTables, WAGO_AREA_TABLE_URL,
WAGO_DUNGEON_ENCOUNTER_URL, WAGO_JOURNAL_ENCOUNTER_CREATURE_URL, WAGO_JOURNAL_ENCOUNTER_URL,
WAGO_LATEST_BUILDS_URL, WAGO_MANIFEST_URL, WAGO_MAP_CHALLENGE_MODE_URL, WAGO_MAP_URL,
WAGO_SPELL_CLASS_OPTIONS_URL, WAGO_SPELL_EFFECT_URL, WAGO_SPELL_MISC_URL,
WAGO_UI_MAP_ASSIGNMENT_URL, WAGO_UI_MAP_GROUP_MEMBER_URL, download_boss_portraits,
download_dungeons, download_forever_tables, download_map_art, download_spell_classes,
download_spell_tables,
};
pub use dungeons::{Dungeon, DungeonBoss, DungeonTable, DungeonTableError, KeyTimer};
Expand Down
78 changes: 78 additions & 0 deletions crates/openlogs-gamedata/tests/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::sync::Mutex;

use openlogs_gamedata::{
DownloadError, Fetch, FetchError, WAGO_LATEST_BUILDS_URL, download_forever_tables,
};

const SPELL_MISC: &str =
"ID,Attributes_1,DifficultyID,SpellIconFileDataID,SpellID\n1,0,0,132276,5487\n";
const MANIFEST: &str =
"ID,FilePath,FileName\n132276,Interface\\Icons\\,Ability_Racial_BearForm.blp\n";
const SPELL_CLASS_OPTIONS: &str = "ID,SpellID,SpellClassSet\n1,5487,7\n";
const SPELL_EFFECT: &str = "ID,EffectAura,SpellID\n1,0,5487\n";

/// Answers wago.tools requests from fixed tables and records every URL asked for.
struct FakeWago {
latest_builds: &'static str,
asked: Mutex<Vec<String>>,
}

impl FakeWago {
fn new(latest_builds: &'static str) -> Self {
Self {
latest_builds,
asked: Mutex::new(Vec::new()),
}
}
}

impl Fetch for FakeWago {
fn get(&self, url: &str) -> Result<Vec<u8>, FetchError> {
self.asked.lock().unwrap().push(url.to_string());
let table = url
.strip_prefix("https://wago.tools/db2/")
.and_then(|rest| rest.split('/').next());
let body = match table {
None if url == WAGO_LATEST_BUILDS_URL => self.latest_builds,
Some("SpellMisc") => SPELL_MISC,
Some("ManifestInterfaceData") => MANIFEST,
Some("SpellClassOptions") => SPELL_CLASS_OPTIONS,
Some("SpellEffect") => SPELL_EFFECT,
_ => return Err(FetchError::Status(404)),
};
Ok(body.as_bytes().to_vec())
}
}

#[test]
fn forever_tables_come_from_the_latest_forever_build() {
let wago =
FakeWago::new(r#"{"wow":{"version":"12.1.5.70961"},"wowf":{"version":"1.60.1.70959"}}"#);

let tables = download_forever_tables(&wago).unwrap();

assert_eq!(tables.build, "1.60.1.70959");
assert_eq!(
tables.spells.icons.icon(5487),
Some("ability_racial_bearform")
);
assert!(tables.classes.is_class_spell(5487));
let asked = wago.asked.lock().unwrap();
let tables_asked: Vec<&String> = asked
.iter()
.filter(|url| url.as_str() != WAGO_LATEST_BUILDS_URL)
.collect();
assert_eq!(tables_asked.len(), 4, "asked {asked:?}");
for url in tables_asked {
assert!(url.ends_with("/csv?build=1.60.1.70959"), "asked {url}");
}
}

#[test]
fn a_latest_builds_answer_without_forever_is_an_error() {
let wago = FakeWago::new(r#"{"wow":{"version":"12.1.5.70961"}}"#);

let err = download_forever_tables(&wago).err().unwrap();

assert!(matches!(err, DownloadError::NoBuild("wowf")), "got {err}");
}
20 changes: 18 additions & 2 deletions crates/openlogs-gamedata/tests/online.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Hits the real services. `cargo test -p openlogs-gamedata --test online -- --ignored`

use openlogs_gamedata::{
HttpFetch, download_boss_portraits, download_dungeons, download_map_art,
download_spell_classes, download_spell_tables,
HttpFetch, download_boss_portraits, download_dungeons, download_forever_tables,
download_map_art, download_spell_classes, download_spell_tables,
};

#[test]
Expand Down Expand Up @@ -63,3 +63,19 @@ fn wago_tools_provides_every_table() {
assert_eq!((sszorak_room.zone, sszorak_room.floor), (16915, Some(3)));
assert_eq!(map_art.pins[&3420].ui_map_id, 2609, "Sszorak has a pin");
}

#[test]
#[ignore = "needs network access to wago.tools"]
fn wago_tools_provides_the_forever_tables() {
let tables = download_forever_tables(&HttpFetch::for_bulk_data()).unwrap();
assert!(tables.build.starts_with("1."), "got build {}", tables.build);
assert!(
tables.spells.icons.icon(5487).is_some(),
"Bear Form has an icon"
);
assert!(
tables.classes.class_spell_count() > 1_000,
"got {} class spells",
tables.classes.class_spell_count()
);
}
Loading
Loading