mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Use asset::load_dir to get list of all items
This commit is contained in:
parent
2bc4f364bf
commit
449902a50e
@ -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<String> = ["admin", "moderator"].iter().copied().map(Into::into).collect();
|
||||
|
||||
/// List of item specifiers. Useful for tab completing
|
||||
static ref ITEM_SPECS: Vec<String> = {
|
||||
let path = assets::ASSETS_PATH.join("common").join("items");
|
||||
let mut items = vec![];
|
||||
fn list_items (path: &Path, base: &Path, mut items: &mut Vec<String>) -> 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<String> = {
|
||||
let mut items = try_all_item_defs()
|
||||
.unwrap_or_else(|e| {
|
||||
warn!(?e, "Failed to load item specifiers");
|
||||
Vec::new()
|
||||
});
|
||||
items.sort();
|
||||
items
|
||||
};
|
||||
|
@ -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<String> {
|
||||
try_all_item_defs().expect("Failed to access items directory")
|
||||
}
|
||||
|
||||
/// Returns all item asset specifiers
|
||||
pub fn try_all_item_defs() -> Result<Vec<String>, Error> {
|
||||
let defs = assets::load_dir::<RawItemDef>("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::<RawItemDef>("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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user