|
| 1 | +use ctor::ctor; |
| 2 | +use simplelog::*; |
| 3 | +use std::{ |
| 4 | + collections::HashMap, |
| 5 | + ffi::{c_char, CString}, |
| 6 | + path::Path, |
| 7 | +}; |
| 8 | + |
| 9 | +#[repr(C)] |
| 10 | +#[derive(Copy, Clone, PartialEq)] |
| 11 | +pub enum ModelType { |
| 12 | + Object, |
| 13 | + Vehicle, |
| 14 | + Ped, |
| 15 | + Weapon, |
| 16 | + Hier, |
| 17 | +} |
| 18 | + |
| 19 | +#[repr(C)] |
| 20 | +pub struct Model { |
| 21 | + pub id: i32, |
| 22 | + pub r#type: ModelType, |
| 23 | +} |
| 24 | + |
| 25 | +pub struct ModelList { |
| 26 | + pub _by_id: HashMap<i32, CString>, |
| 27 | + pub _by_name: HashMap<String, Model>, |
| 28 | + pub model_names: Vec<String>, |
| 29 | +} |
| 30 | + |
| 31 | +impl ModelList { |
| 32 | + pub fn new() -> Self { |
| 33 | + Self { |
| 34 | + _by_id: HashMap::new(), |
| 35 | + _by_name: HashMap::new(), |
| 36 | + model_names: Vec::new(), |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + pub fn load_from_file(&mut self, file_name: &str) { |
| 41 | + let Ok(content) = std::fs::read_to_string(file_name) else { |
| 42 | + log::error!("Failed to read file: {}", file_name); |
| 43 | + return; |
| 44 | + }; |
| 45 | + let Ok(ide) = gta_ide_parser::parse(&content) else { |
| 46 | + log::error!("Failed to parse IDE: {}", file_name); |
| 47 | + return; |
| 48 | + }; |
| 49 | + for (section_name, lines) in ide { |
| 50 | + if ["txdp", "path", "2dfx"].contains(§ion_name.as_str()) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + for line in lines { |
| 54 | + let Ok(id) = line[0].parse::<i32>() else { |
| 55 | + log::error!("Failed to parse ID: {} in {}", line[0], file_name); |
| 56 | + continue; |
| 57 | + }; |
| 58 | + let name = line[1].to_string(); |
| 59 | + let r#type = match section_name.as_str() { |
| 60 | + "objs" | "tobj" | "anim" | "tanm" => ModelType::Object, |
| 61 | + "cars" => ModelType::Vehicle, |
| 62 | + "peds" => ModelType::Ped, |
| 63 | + "weap" => ModelType::Weapon, |
| 64 | + "hier" => ModelType::Hier, |
| 65 | + _ => { |
| 66 | + continue; |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + self.model_names.push(name.to_uppercase()); |
| 71 | + self._by_id |
| 72 | + .insert(id, CString::new(name.to_uppercase()).unwrap()); |
| 73 | + self._by_name |
| 74 | + .insert(name.to_ascii_lowercase(), Model { id, r#type }); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn find_by_id(&self, id: i32) -> Option<&CString> { |
| 80 | + self._by_id.get(&id) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn find_by_name(&self, name: &str) -> Option<&Model> { |
| 84 | + let needle = &name.to_ascii_lowercase(); |
| 85 | + match needle.as_str() { |
| 86 | + // old parser did not properly handle missing commas, resulting in broken names |
| 87 | + "emperoremperor" => { |
| 88 | + return Some(&Model { |
| 89 | + id: 585, |
| 90 | + r#type: ModelType::Vehicle, |
| 91 | + }) |
| 92 | + } |
| 93 | + "wayfarerwayfarer" => { |
| 94 | + return Some(&Model { |
| 95 | + id: 586, |
| 96 | + r#type: ModelType::Vehicle, |
| 97 | + }) |
| 98 | + } |
| 99 | + "dodododo" => { |
| 100 | + return Some(&Model { |
| 101 | + id: 593, |
| 102 | + r#type: ModelType::Vehicle, |
| 103 | + }) |
| 104 | + } |
| 105 | + _ => self._by_name.get(needle), |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + pub fn filter_by_name(&self, needle: &str) -> Vec<(CString, i32)> { |
| 110 | + let needle = needle.to_ascii_uppercase(); |
| 111 | + let mut results = Vec::new(); |
| 112 | + for name in self.model_names.iter() { |
| 113 | + if name.contains(&needle) { |
| 114 | + if let Some(model) = self.find_by_name(name) { |
| 115 | + if model.r#type == ModelType::Object { |
| 116 | + continue; // Skip objects |
| 117 | + } |
| 118 | + results.push((CString::new(name.clone()).unwrap(), model.id)); |
| 119 | + // if results.len() >= 200 { |
| 120 | + // break; // Limit results to 200 |
| 121 | + // } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + results |
| 126 | + } |
| 127 | +} |
0 commit comments