Merge branch 'a1phyr/improve_list_localization' into 'master'

Perf: avoid rescanning the localization directory on each frame

See merge request veloren/veloren!1676
This commit is contained in:
Imbris
2021-01-04 00:27:34 +00:00

View File

@ -201,7 +201,7 @@ impl assets::Compound for Localization {
asset_key: &str, asset_key: &str,
) -> Result<Self, assets::Error> { ) -> Result<Self, assets::Error> {
let raw = cache let raw = cache
.load::<RawLocalization>(&(asset_key.to_string() + "." + LANG_MANIFEST_FILE))? .load::<RawLocalization>(&[asset_key, ".", LANG_MANIFEST_FILE].concat())?
.cloned(); .cloned();
let mut localization = Localization::from(raw); let mut localization = Localization::from(raw);
@ -219,7 +219,7 @@ impl assets::Compound for Localization {
// Use the localization's subdirectory list to load fragments from there // Use the localization's subdirectory list to load fragments from there
for sub_directory in localization.sub_directories.iter() { for sub_directory in localization.sub_directories.iter() {
for localization_asset in cache for localization_asset in cache
.load_dir::<LocalizationFragment>(&(asset_key.to_string() + "." + &sub_directory))? .load_dir::<LocalizationFragment>(&[asset_key, ".", sub_directory].concat())?
.iter() .iter()
{ {
localization localization
@ -247,28 +247,42 @@ impl assets::Compound for Localization {
} }
} }
/// Load all the available languages located in the voxygen asset directory #[derive(Clone, Debug)]
pub fn list_localizations() -> Vec<LanguageMetadata> { struct LocalizationList(Vec<LanguageMetadata>);
let mut languages = vec![];
// List language directories impl assets::Compound for LocalizationList {
let i18n_root = assets::path_of("voxygen.i18n", ""); fn load<S: assets::source::Source>(
for i18n_directory in std::fs::read_dir(&i18n_root).unwrap() { cache: &assets::AssetCache<S>,
if let Ok(i18n_entry) = i18n_directory { specifier: &str,
if let Some(i18n_key) = i18n_entry.file_name().to_str() { ) -> Result<Self, assets::Error> {
// load the root file of all the subdirectories // List language directories
if let Ok(localization) = RawLocalization::load( let mut languages = vec![];
&("voxygen.i18n.".to_string() + i18n_key + "." + LANG_MANIFEST_FILE),
) { let i18n_root = assets::path_of(specifier, "");
languages.push(localization.read().metadata.clone()); for i18n_directory in std::fs::read_dir(&i18n_root)? {
if let Ok(i18n_entry) = i18n_directory {
if let Some(i18n_key) = i18n_entry.file_name().to_str() {
// load the root file of all the subdirectories
if let Ok(localization) = cache.load::<RawLocalization>(
&[specifier, ".", i18n_key, ".", LANG_MANIFEST_FILE].concat(),
) {
languages.push(localization.read().metadata.clone());
}
} }
} }
} }
Ok(LocalizationList(languages))
} }
languages }
/// Load all the available languages located in the voxygen asset directory
pub fn list_localizations() -> Vec<LanguageMetadata> {
LocalizationList::load_expect_cloned("voxygen.i18n").0
} }
/// Return the asset associated with the language_id /// Return the asset associated with the language_id
pub fn i18n_asset_key(language_id: &str) -> String { "voxygen.i18n.".to_string() + language_id } pub fn i18n_asset_key(language_id: &str) -> String { ["voxygen.i18n.", language_id].concat() }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {