From 633228af42104447627d7d7e382ea748ce137b9a Mon Sep 17 00:00:00 2001 From: yuvlian <51725554+yuvlian@users.noreply.github.com> Date: Sat, 21 Feb 2026 14:25:20 +0700 Subject: [PATCH] anyhow, cargo fmt --- resource_api/Cargo.toml | 1 + resource_api/src/main.rs | 1 + resource_api/src/res_parser/common.rs | 10 ++--- resource_api/src/res_parser/item.rs | 56 +++++++++++++++---------- resource_api/src/res_parser/mod.rs | 2 - resource_api/src/res_parser/text_map.rs | 55 ++++++++++++------------ resource_api/src/types/character.rs | 2 +- resource_api/src/types/lightcone.rs | 2 +- resource_api/src/types/relic.rs | 2 +- 9 files changed, 73 insertions(+), 58 deletions(-) diff --git a/resource_api/Cargo.toml b/resource_api/Cargo.toml index d12ddb5..427c375 100644 --- a/resource_api/Cargo.toml +++ b/resource_api/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] +anyhow = "1.0.102" paste = "1.0.15" serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.149" diff --git a/resource_api/src/main.rs b/resource_api/src/main.rs index 4df96f0..7458f87 100644 --- a/resource_api/src/main.rs +++ b/resource_api/src/main.rs @@ -8,4 +8,5 @@ fn main() { singleton::set_item_search(item_search).ok(); let stellar_jade = singleton::get_item_mini_by_id(&1).unwrap(); dbg!(stellar_jade); + println!("{}", res_parser::common::Hash::from_str("HelLONi1")); } diff --git a/resource_api/src/res_parser/common.rs b/resource_api/src/res_parser/common.rs index c5a9f08..7d9d2b3 100644 --- a/resource_api/src/res_parser/common.rs +++ b/resource_api/src/res_parser/common.rs @@ -2,13 +2,13 @@ use serde::{Deserialize, Serialize}; #[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)] pub struct Hash { - pub Hash: i32, + #[serde(rename = "Hash")] + pub hash: i32, } impl Hash { - pub const fn get_i32(s: &str) -> i32 { - let mut hash1: i32 = 5381; - let mut hash2: i32 = hash1; + pub const fn from_str(s: &str) -> i32 { + let mut hash1 @ mut hash2 = 5381i32; let bytes = s.as_bytes(); let length = bytes.len(); @@ -16,11 +16,9 @@ impl Hash { let mut i = 0; while i < length { hash1 = ((hash1 << 5).wrapping_add(hash1)) ^ (bytes[i] as i32); - if i + 1 < length { hash2 = ((hash2 << 5).wrapping_add(hash2)) ^ (bytes[i + 1] as i32); } - i += 2; } diff --git a/resource_api/src/res_parser/item.rs b/resource_api/src/res_parser/item.rs index cd8f289..948dc82 100644 --- a/resource_api/src/res_parser/item.rs +++ b/resource_api/src/res_parser/item.rs @@ -1,36 +1,50 @@ -use serde::{Deserialize, Serialize}; use super::common::Hash; use super::text_map::TextMaps; -use std::collections::BTreeMap; use crate::types::common::Image; -use crate::types::item::{ItemSearch, ItemMini}; - -// C:\Users\yuvlian\Downloads\git\others\srwk\Resources-DUMP\ExcelOutput\ItemConfig.json -// pub struct ItemSearch(pub BTreeMap); +use crate::types::item::{ItemMini, ItemSearch}; +use anyhow::{Context, Result}; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ItemConfigs(pub Vec); impl ItemConfigs { - pub fn parse_to_api_type(tm: &TextMaps) -> Result { - let file = std::fs::read_to_string("../Resources-DUMP/ExcelOutput/ItemConfig.json").unwrap(); - let cfg: Vec = serde_json::from_str(&file).unwrap(); - let cfg = ItemConfigs(cfg); - let mut item_search = BTreeMap::new(); - for item in cfg.0.iter() { - let id = item.ID; - // println!("{}", id); - let name = tm.get_by_hash(item.ItemName).unwrap_or_default(); - let img = Image { path: String::new(), filename: String::new(), author_info: None }; - item_search.insert(id, ItemMini { name: name.Text.clone(), image: img}); - } + pub fn parse_to_api_type(tm: &TextMaps) -> Result { + let path = "../Resources-DUMP/ExcelOutput/ItemConfig.json"; + let file = std::fs::File::open(path).with_context(|| format!("Failed to open {}", path))?; + let reader = std::io::BufReader::new(file); + let cfg: Vec = + serde_json::from_reader(reader).with_context(|| format!("Failed to parse {}", path))?; + + let item_search = cfg + .into_iter() + .map(|item| { + let id = item.id; + let name = tm.get_by_hash(item.item_name).unwrap_or_default(); + let img = Image { + path: String::new(), + filename: String::new(), + author_info: None, + }; + ( + id, + ItemMini { + name: name.text.clone(), + image: img, + }, + ) + }) + .collect(); + Ok(ItemSearch(item_search)) } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ItemConfig { - pub ID: i32, - pub ItemName: Hash, + #[serde(rename = "ID")] + pub id: i32, + #[serde(rename = "ItemName")] + pub item_name: Hash, } - diff --git a/resource_api/src/res_parser/mod.rs b/resource_api/src/res_parser/mod.rs index 9781bda..2b54091 100644 --- a/resource_api/src/res_parser/mod.rs +++ b/resource_api/src/res_parser/mod.rs @@ -1,5 +1,3 @@ -#![allow(non_snake_case)] - pub mod common; pub mod item; pub mod text_map; diff --git a/resource_api/src/res_parser/text_map.rs b/resource_api/src/res_parser/text_map.rs index 4ba2d53..ff6cac2 100644 --- a/resource_api/src/res_parser/text_map.rs +++ b/resource_api/src/res_parser/text_map.rs @@ -1,45 +1,48 @@ -use serde::{Deserialize, Serialize}; use super::common::Hash; - -// C:\Users\yuvlian\Downloads\git\others\srwk\Resources-DUMP\TextMap\TextMapEN.json +use anyhow::{Context, Result}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::fs::File; +use std::io::BufReader; #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct TextMaps(pub Vec); +pub struct TextMaps(HashMap); impl TextMaps { - pub fn parse() -> Result { - let file = std::fs::read_to_string("../Resources-DUMP/TextMap/TextMapEN.json").unwrap(); - let v: Vec = serde_json::from_str(&file).unwrap(); - Ok(TextMaps(v)) - } + pub fn parse() -> Result { + let path = "../Resources-DUMP/TextMap/TextMapEN.json"; + let file = File::open(path).with_context(|| format!("Failed to open {}", path))?; + let reader = BufReader::new(file); + let v: Vec = + serde_json::from_reader(reader).with_context(|| format!("Failed to parse {}", path))?; - pub fn get_by_i32(&self, i: i32) -> Option { - self.get_by_hash(Hash { Hash: i }) + let map = v.into_iter().map(|t| (t.id.hash, t)).collect(); + Ok(TextMaps(map)) } pub fn get_by_str(&self, s: &str) -> Option { - let hash = Hash::get_i32(s); - self.get_by_hash(Hash { Hash: hash }) + let hash = Hash::from_str(s); + self.get_by_i32(hash) + } + + pub fn get_by_i32(&self, i: i32) -> Option { + self.get_by_hash(Hash { hash: i }) } pub fn get_by_hash(&self, h: Hash) -> Option { - let mut m = None; - if h.Hash == 0 { - return m; + if h.hash == 0 { + return None; } - for t in self.0.iter() { - if t.ID.Hash == h.Hash { - m = Some(t.clone()); - break; - } - } - m + self.0.get(&h.hash).cloned() } } #[derive(Default, Debug, Clone, Serialize, Deserialize)] pub struct TextMap { - pub ID: Hash, - pub Text: String, - pub HasParam: bool, + #[serde(rename = "ID")] + pub id: Hash, + #[serde(rename = "Text")] + pub text: String, + #[serde(rename = "HasParam")] + pub has_param: bool, } diff --git a/resource_api/src/types/character.rs b/resource_api/src/types/character.rs index c002397..6d59b14 100644 --- a/resource_api/src/types/character.rs +++ b/resource_api/src/types/character.rs @@ -1,5 +1,5 @@ use super::common::{ - Ascension, Count, Desc, Element, Id, Level, Path, Rarity, StatType, StatValue, Image, + Ascension, Count, Desc, Element, Id, Image, Level, Path, Rarity, StatType, StatValue, }; use super::item::ItemMini; use serde::{Deserialize, Serialize}; diff --git a/resource_api/src/types/lightcone.rs b/resource_api/src/types/lightcone.rs index 5dab1a0..69d7931 100644 --- a/resource_api/src/types/lightcone.rs +++ b/resource_api/src/types/lightcone.rs @@ -1,4 +1,4 @@ -use super::common::{Count, Desc, Id, Level, Path, Rarity, StatValue, Image}; +use super::common::{Count, Desc, Id, Image, Level, Path, Rarity, StatValue}; use super::item::ItemMini; use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, HashMap}; diff --git a/resource_api/src/types/relic.rs b/resource_api/src/types/relic.rs index b6abb92..2bb0f04 100644 --- a/resource_api/src/types/relic.rs +++ b/resource_api/src/types/relic.rs @@ -1,4 +1,4 @@ -use super::common::{Desc, Id, RelicType, Image}; +use super::common::{Desc, Id, Image, RelicType}; use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, HashMap};