Ability to load masterdata from a folder

This commit is contained in:
Ethan O'Brien
2026-05-10 11:40:04 -05:00
parent 95c31f9e54
commit e9c84be5fa
5 changed files with 83 additions and 11 deletions

View File

@@ -13,9 +13,9 @@ pub fn routes(cfg: &mut web::ServiceConfig) {
async fn get(_req: HttpRequest) -> impl Responder {
let mut response = object!{};
response["Bundle"] = include_file!("src/router/asset_lists/Bundle.json").into();
response["Movie"] = include_file!("src/router/asset_lists/Movie.json").into();
response["Sound"] = include_file!("src/router/asset_lists/Sound.json").into();
response["Bundle"] = load_list("Bundle").into();
response["Movie"] = load_list("Movie").into();
response["Sound"] = load_list("Sound").into();
let body = jzon::stringify(response);
HttpResponse::Ok()
@@ -24,6 +24,21 @@ async fn get(_req: HttpRequest) -> impl Responder {
.body(body)
}
fn load_list(name: &str) -> String {
let rel = format!("asset_lists/{}.json", name);
if let Some(bytes) = crate::runtime::read_masterdata_file(&rel) {
if let Ok(s) = String::from_utf8(bytes) {
return s;
}
}
match name {
"Bundle" => include_file!("src/router/asset_lists/Bundle.json"),
"Movie" => include_file!("src/router/asset_lists/Movie.json"),
"Sound" => include_file!("src/router/asset_lists/Sound.json"),
_ => unreachable!(),
}
}
async fn supported() -> impl Responder {
"SUPPORTED"
}

View File

@@ -33,6 +33,13 @@ fn dir_for(region: Region) -> &'static Dir<'static> {
}
}
fn region_subdir(region: Region) -> &'static str {
match region {
Region::Jp => "csv",
Region::En => "csv-en",
}
}
pub fn get_all(region: Region) -> JsonValue {
let mut rv = object!{};
@@ -42,18 +49,26 @@ pub fn get_all(region: Region) -> JsonValue {
.and_then(|s| s.to_str())
.unwrap_or_default();
if !table_name.is_empty() {
rv[table_name] = file.contents_utf8().unwrap_or_default().into();
if table_name.is_empty() {
continue;
}
if let Some(bytes) = csv_bytes(region, table_name) {
rv[table_name] = String::from_utf8(bytes).unwrap_or_default().into();
}
}
rv
}
pub fn csv_bytes(region: Region, name: &str) -> Option<&'static [u8]> {
pub fn csv_bytes(region: Region, name: &str) -> Option<Vec<u8>> {
let rel = format!("{}/{}.csv", region_subdir(region), name);
if let Some(bytes) = crate::runtime::read_masterdata_file(&rel) {
return Some(bytes);
}
dir_for(region)
.get_file(format!("{name}.csv"))
.map(|f| f.contents())
.map(|f| f.contents().to_vec())
}
pub fn table(region: Region, name: &str) -> JsonValue {
@@ -65,7 +80,7 @@ pub fn table(region: Region, name: &str) -> JsonValue {
let bytes = csv_bytes(region, name).unwrap_or_else(|| {
panic!("masterdata CSV not bundled: {name}.csv ({region:?})")
});
let parsed = parse_csv(name, bytes);
let parsed = parse_csv(name, &bytes);
TABLE_CACHE.lock().unwrap().insert(key, parsed.clone());
parsed