From 449902a50e9e27522df331db5bc6bb26211e85bd Mon Sep 17 00:00:00 2001 From: juliancoffee Date: Tue, 10 Aug 2021 00:25:36 +0300 Subject: [PATCH] Use asset::load_dir to get list of all items --- common/src/cmd.rs | 27 +++++++-------------------- common/src/comp/inventory/item/mod.rs | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/common/src/cmd.rs b/common/src/cmd.rs index 190444290e..4859170201 100644 --- a/common/src/cmd.rs +++ b/common/src/cmd.rs @@ -1,6 +1,6 @@ use crate::{ assets, - comp::{self, buff::BuffKind, AdminRole as Role, Skill}, + comp::{self, buff::BuffKind, inventory::item::try_all_item_defs, AdminRole as Role, Skill}, npc, terrain, }; use assets::AssetExt; @@ -9,7 +9,6 @@ use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use std::{ fmt::{self, Display}, - path::Path, str::FromStr, }; use strum::IntoEnumIterator; @@ -230,24 +229,12 @@ lazy_static! { static ref ROLES: Vec = ["admin", "moderator"].iter().copied().map(Into::into).collect(); /// List of item specifiers. Useful for tab completing - static ref ITEM_SPECS: Vec = { - let path = assets::ASSETS_PATH.join("common").join("items"); - let mut items = vec![]; - fn list_items (path: &Path, base: &Path, mut items: &mut Vec) -> std::io::Result<()>{ - for entry in std::fs::read_dir(path)? { - let path = entry?.path(); - if path.is_dir(){ - list_items(&path, base, &mut items)?; - } else if let Ok(path) = path.strip_prefix(base) { - let path = path.to_string_lossy().trim_end_matches(".ron").replace('/', ".").replace('\\', "."); - items.push(path); - } - } - Ok(()) - } - if list_items(&path, &assets::ASSETS_PATH, &mut items).is_err() { - warn!("There was a problem listing item assets"); - } + pub static ref ITEM_SPECS: Vec = { + let mut items = try_all_item_defs() + .unwrap_or_else(|e| { + warn!(?e, "Failed to load item specifiers"); + Vec::new() + }); items.sort(); items }; diff --git a/common/src/comp/inventory/item/mod.rs b/common/src/comp/inventory/item/mod.rs index 41863f52be..c425a8056b 100644 --- a/common/src/comp/inventory/item/mod.rs +++ b/common/src/comp/inventory/item/mod.rs @@ -887,15 +887,27 @@ impl<'a, T: ItemDesc + ?Sized> ItemDesc for &'a T { fn tags(&self) -> &[ItemTag] { (*self).tags() } } +/// Returns all item asset specifiers +/// +/// Panics in case of filesystem errors +pub fn all_item_defs_expect() -> Vec { + try_all_item_defs().expect("Failed to access items directory") +} + +/// Returns all item asset specifiers +pub fn try_all_item_defs() -> Result, Error> { + let defs = assets::load_dir::("common.items", true)?; + Ok(defs.ids().map(|id| id.to_owned()).collect()) +} + #[cfg(test)] mod tests { use super::*; #[test] fn test_assets_items() { - let defs = assets::load_dir::("common.items", true) - .expect("Failed to access items directory"); - for item in defs.ids().map(Item::new_from_asset_expect) { + let ids = all_item_defs_expect(); + for item in ids.iter().map(|id| Item::new_from_asset_expect(id)) { std::mem::drop(item) } }