pub mod tables; use std::{collections::HashMap, sync::OnceLock}; use serde::{Deserialize, Serialize}; use thiserror::Error; #[derive(Serialize, Deserialize)] pub struct AssetsConfig { pub filecfg_path: String, pub usm_keys_path: String, } #[derive(Error, Debug)] pub enum DataLoadError { #[error("I/O error: {0}")] IoError(#[from] std::io::Error), #[error("from_json failed for type {0}, error: {1}")] FromJsonError(String, serde_json::Error), } static USM_KEY_MAP: OnceLock> = OnceLock::new(); pub fn init_data(config: &AssetsConfig) -> Result<(), DataLoadError> { tables::load_tables(&config.filecfg_path)?; if let Err(err) = load_usm_keys(&config.usm_keys_path) { tracing::warn!("failed to load USM keys, in-game cutscenes will not work! Reason: {err}"); USM_KEY_MAP.set(HashMap::new()).unwrap(); } Ok(()) } pub fn usm_key_map() -> &'static HashMap { USM_KEY_MAP.get().unwrap() } fn load_usm_keys(path: &str) -> Result<(), DataLoadError> { let data = std::fs::read_to_string(path).map_err(|err| DataLoadError::IoError(err))?; let map = serde_json::from_str::>(&data) .map_err(|err| DataLoadError::FromJsonError(String::from("USMKeyMap"), err))?; USM_KEY_MAP.set(map).unwrap(); Ok(()) }