From 603ebb826cca4341db52119481fb2a8d7851a5fe Mon Sep 17 00:00:00 2001 From: yuvlian <51725554+yuvlian@users.noreply.github.com> Date: Sat, 21 Feb 2026 14:06:28 +0700 Subject: [PATCH] test messy res -> types::item --- .gitignore | 1 + resource_api/Cargo.toml | 1 + resource_api/src/main.rs | 6 +++- resource_api/src/res_parser/common.rs | 29 ++++++++++++++++ resource_api/src/res_parser/item.rs | 36 ++++++++++++++++++++ resource_api/src/res_parser/mod.rs | 6 +++- resource_api/src/res_parser/text_map.rs | 45 +++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 resource_api/src/res_parser/common.rs create mode 100644 resource_api/src/res_parser/item.rs create mode 100644 resource_api/src/res_parser/text_map.rs diff --git a/.gitignore b/.gitignore index b852a7e..effdcfa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ **/target **/*.lock +Resources-DUMP diff --git a/resource_api/Cargo.toml b/resource_api/Cargo.toml index 67e0c23..d12ddb5 100644 --- a/resource_api/Cargo.toml +++ b/resource_api/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] 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 b798090..4df96f0 100644 --- a/resource_api/src/main.rs +++ b/resource_api/src/main.rs @@ -3,5 +3,9 @@ mod singleton; mod types; fn main() { - println!("Hello, world!"); + let tm = res_parser::text_map::TextMaps::parse().unwrap(); + let item_search = res_parser::item::ItemConfigs::parse_to_api_type(&tm).unwrap(); + singleton::set_item_search(item_search).ok(); + let stellar_jade = singleton::get_item_mini_by_id(&1).unwrap(); + dbg!(stellar_jade); } diff --git a/resource_api/src/res_parser/common.rs b/resource_api/src/res_parser/common.rs new file mode 100644 index 0000000..c5a9f08 --- /dev/null +++ b/resource_api/src/res_parser/common.rs @@ -0,0 +1,29 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize)] +pub struct Hash { + pub Hash: i32, +} + +impl Hash { + pub const fn get_i32(s: &str) -> i32 { + let mut hash1: i32 = 5381; + let mut hash2: i32 = hash1; + + let bytes = s.as_bytes(); + let length = bytes.len(); + + 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; + } + + hash1.wrapping_add(hash2.wrapping_mul(1566083941)) + } +} diff --git a/resource_api/src/res_parser/item.rs b/resource_api/src/res_parser/item.rs new file mode 100644 index 0000000..cd8f289 --- /dev/null +++ b/resource_api/src/res_parser/item.rs @@ -0,0 +1,36 @@ +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); + +#[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}); + } + Ok(ItemSearch(item_search)) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ItemConfig { + pub ID: i32, + pub ItemName: Hash, +} + diff --git a/resource_api/src/res_parser/mod.rs b/resource_api/src/res_parser/mod.rs index 70b786d..9781bda 100644 --- a/resource_api/src/res_parser/mod.rs +++ b/resource_api/src/res_parser/mod.rs @@ -1 +1,5 @@ -// TODO +#![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 new file mode 100644 index 0000000..4ba2d53 --- /dev/null +++ b/resource_api/src/res_parser/text_map.rs @@ -0,0 +1,45 @@ +use serde::{Deserialize, Serialize}; +use super::common::Hash; + +// C:\Users\yuvlian\Downloads\git\others\srwk\Resources-DUMP\TextMap\TextMapEN.json + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct TextMaps(pub Vec); + +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 get_by_i32(&self, i: i32) -> Option { + self.get_by_hash(Hash { Hash: i }) + } + + pub fn get_by_str(&self, s: &str) -> Option { + let hash = Hash::get_i32(s); + self.get_by_hash(Hash { Hash: hash }) + } + + pub fn get_by_hash(&self, h: Hash) -> Option { + let mut m = None; + if h.Hash == 0 { + return m; + } + for t in self.0.iter() { + if t.ID.Hash == h.Hash { + m = Some(t.clone()); + break; + } + } + m + } +} + +#[derive(Default, Debug, Clone, Serialize, Deserialize)] +pub struct TextMap { + pub ID: Hash, + pub Text: String, + pub HasParam: bool, +}