├── .github └── workflows │ └── deploy.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── examples ├── buttons.rs ├── concommands.rs ├── convars.rs ├── interfaces.rs ├── offsets.rs └── schemas.rs ├── rustfmt.toml ├── src ├── analysis │ ├── client │ │ ├── buttons.rs │ │ ├── mod.rs │ │ └── offsets.rs │ ├── concommands.rs │ ├── convars.rs │ ├── engine2 │ │ ├── mod.rs │ │ └── offsets.rs │ ├── globals.rs │ ├── input_system │ │ ├── mod.rs │ │ └── offsets.rs │ ├── interfaces.rs │ ├── matchmaking │ │ ├── mod.rs │ │ └── offsets.rs │ ├── mod.rs │ ├── schemas.rs │ └── sound_system │ │ ├── mod.rs │ │ └── offsets.rs ├── error.rs ├── lib.rs └── source2 │ ├── mod.rs │ ├── schema_base_class_info_data.rs │ ├── schema_class_field_data.rs │ ├── schema_class_info_data.rs │ ├── schema_enum_info_data.rs │ ├── schema_enumerator_info_data.rs │ ├── schema_field_type.rs │ └── schema_metadata_entry_data.rs └── wasm ├── .gitignore ├── Cargo.toml ├── README.md ├── Trunk.toml ├── index.html ├── index.scss └── src ├── app.rs ├── components ├── alert.rs ├── clear_files_button.rs ├── file_accordion.rs ├── file_analysis.rs ├── file_upload_zone.rs ├── footer.rs ├── header.rs ├── loading_indicator.rs ├── mod.rs ├── tab.rs └── tab_pane.rs └── main.rs /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy to GitHub Pages 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | push: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | deploy: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | 17 | - name: Setup Rust 18 | uses: actions-rs/toolchain@v1 19 | with: 20 | toolchain: stable 21 | target: wasm32-unknown-unknown 22 | 23 | - name: Install Trunk 24 | uses: jetli/trunk-action@v0.5.0 25 | with: 26 | version: 'latest' 27 | 28 | - name: Build 29 | run: | 30 | cd wasm 31 | trunk build --release 32 | 33 | - name: Deploy 34 | uses: peaceiris/actions-gh-pages@v3 35 | with: 36 | github_token: ${{ secrets.GITHUB_TOKEN }} 37 | publish_branch: gh-pages 38 | publish_dir: ./wasm/dist -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /.vscode 3 | /target 4 | Cargo.lock 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cs2-analyzer" 3 | version = "0.1.0" 4 | authors = ["a2x", "ko1N "] 5 | edition = "2021" 6 | readme = "README.md" 7 | repository = "https://github.com/a2x/cs2-analyzer" 8 | license = "MIT" 9 | 10 | [workspace] 11 | members = [".", "./wasm"] 12 | 13 | [dependencies] 14 | dataview = "1.0" 15 | log = "0.4" 16 | num_enum = "0.7" 17 | num_enum_derive = "0.7" 18 | pelite = "0.10" 19 | phf = { version = "0.11", features = ["macros"] } 20 | rayon = "1.8" 21 | serde = { version = "1.0", features = ["derive"], optional = true } 22 | thiserror = "1.0" 23 | 24 | [dev-dependencies] 25 | walkdir = "2.4" 26 | winreg = "0.52" 27 | 28 | [features] 29 | default = [] 30 | serde_support = ["serde"] 31 | 32 | [[example]] 33 | name = "buttons" 34 | path = "examples/buttons.rs" 35 | 36 | [[example]] 37 | name = "concommands" 38 | path = "examples/concommands.rs" 39 | 40 | [[example]] 41 | name = "convars" 42 | path = "examples/convars.rs" 43 | 44 | [[example]] 45 | name = "interfaces" 46 | path = "examples/interfaces.rs" 47 | 48 | [[example]] 49 | name = "offsets" 50 | path = "examples/offsets.rs" 51 | 52 | [[example]] 53 | name = "schemas" 54 | path = "examples/schemas.rs" 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 a2x, ko1N 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cs2-analyzer 2 | 3 | A Rust crate for "analyzing" Counter-Strike: 2 binaries from disk and ones already loaded in memory. 4 | 5 | Web demo: https://a2x.github.io/cs2-analyzer 6 | 7 | ## Examples 8 | 9 | See the [examples](./examples) directory for examples. 10 | 11 | ## License 12 | 13 | Licensed under the MIT license ([LICENSE](./LICENSE)). 14 | -------------------------------------------------------------------------------- /examples/buttons.rs: -------------------------------------------------------------------------------- 1 | use cs2_analyzer::{Analyzer, AnalyzerOptions, Result}; 2 | 3 | fn main() -> Result<()> { 4 | let cs2_path = find_cs2_install_path()?; 5 | 6 | let mut analyzer = Analyzer::new_with_opts(AnalyzerOptions { 7 | buttons: true, 8 | concommands: false, 9 | convars: false, 10 | interfaces: false, 11 | offsets: false, 12 | schemas: false, 13 | }); 14 | 15 | analyzer.add_file(format!(r"{}\game\csgo\bin\win64\client.dll", cs2_path)); 16 | 17 | let result = analyzer.analyze_file("client.dll")?; 18 | 19 | for button in &result.buttons { 20 | println!( 21 | "found button: {} (client.dll + {:#X})", 22 | button.name, button.rva 23 | ); 24 | } 25 | 26 | Ok(()) 27 | } 28 | 29 | #[cfg(target_family = "windows")] 30 | fn find_cs2_install_path() -> Result { 31 | use winreg::enums::HKEY_LOCAL_MACHINE; 32 | use winreg::RegKey; 33 | 34 | let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); 35 | 36 | let cs2 = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Valve\cs2")?; 37 | 38 | let install_path: String = cs2.get_value("installpath")?; 39 | 40 | Ok(install_path) 41 | } 42 | 43 | #[cfg(not(target_family = "windows"))] 44 | fn find_cs2_install_path() -> Result { 45 | unimplemented!("auto-detecting cs2 install path is only supported on windows") 46 | } 47 | -------------------------------------------------------------------------------- /examples/concommands.rs: -------------------------------------------------------------------------------- 1 | use cs2_analyzer::{Analyzer, AnalyzerOptions, Result}; 2 | 3 | use walkdir::WalkDir; 4 | 5 | fn main() -> Result<()> { 6 | let cs2_path = find_cs2_install_path()?; 7 | 8 | let dll_paths: Vec<_> = WalkDir::new(&cs2_path) 9 | .into_iter() 10 | .filter_map(|e| e.ok()) 11 | .filter(|e| e.file_name().to_string_lossy().ends_with(".dll")) 12 | .map(|e| e.path().to_path_buf()) 13 | .collect(); 14 | 15 | let mut analyzer = Analyzer::new_with_opts(AnalyzerOptions { 16 | buttons: false, 17 | concommands: true, 18 | convars: false, 19 | interfaces: false, 20 | offsets: false, 21 | schemas: false, 22 | }); 23 | 24 | analyzer.add_files(&dll_paths); 25 | 26 | // Analyze all added files (This may take a while). 27 | let result = analyzer.analyze(); 28 | 29 | for (file_name, result) in &result { 30 | for concommand in &result.concommands { 31 | println!( 32 | "found concommand: {} in {} (flags: {:?}, description: {:?})", 33 | concommand.name, file_name, concommand.flags, concommand.description 34 | ); 35 | } 36 | 37 | println!( 38 | "found {} concommands in {}", 39 | result.concommands.len(), 40 | file_name 41 | ); 42 | } 43 | 44 | Ok(()) 45 | } 46 | 47 | #[cfg(target_family = "windows")] 48 | fn find_cs2_install_path() -> Result { 49 | use winreg::enums::HKEY_LOCAL_MACHINE; 50 | use winreg::RegKey; 51 | 52 | let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); 53 | 54 | let cs2 = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Valve\cs2")?; 55 | 56 | let install_path: String = cs2.get_value("installpath")?; 57 | 58 | Ok(install_path) 59 | } 60 | 61 | #[cfg(not(target_family = "windows"))] 62 | fn find_cs2_install_path() -> Result { 63 | unimplemented!("auto-detecting cs2 install path is only supported on windows") 64 | } 65 | -------------------------------------------------------------------------------- /examples/convars.rs: -------------------------------------------------------------------------------- 1 | use cs2_analyzer::{Analyzer, AnalyzerOptions, Result}; 2 | 3 | use walkdir::WalkDir; 4 | 5 | fn main() -> Result<()> { 6 | let cs2_path = find_cs2_install_path()?; 7 | 8 | let dll_paths: Vec<_> = WalkDir::new(&cs2_path) 9 | .into_iter() 10 | .filter_map(|e| e.ok()) 11 | .filter(|e| e.file_name().to_string_lossy().ends_with(".dll")) 12 | .map(|e| e.path().to_path_buf()) 13 | .collect(); 14 | 15 | let mut analyzer = Analyzer::new_with_opts(AnalyzerOptions { 16 | buttons: false, 17 | concommands: false, 18 | convars: true, 19 | interfaces: false, 20 | offsets: false, 21 | schemas: false, 22 | }); 23 | 24 | analyzer.add_files(&dll_paths); 25 | 26 | // Analyze all added files (This may take a while). 27 | let result = analyzer.analyze(); 28 | 29 | for (file_name, result) in &result { 30 | for convar in &result.convars { 31 | println!( 32 | "found convar: {} in {} (flags: {:?}, description: {:?})", 33 | convar.name, file_name, convar.flags, convar.description 34 | ); 35 | } 36 | 37 | println!("found {} convars in {}", result.convars.len(), file_name); 38 | } 39 | 40 | Ok(()) 41 | } 42 | 43 | #[cfg(target_family = "windows")] 44 | fn find_cs2_install_path() -> Result { 45 | use winreg::enums::HKEY_LOCAL_MACHINE; 46 | use winreg::RegKey; 47 | 48 | let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); 49 | 50 | let cs2 = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Valve\cs2")?; 51 | 52 | let install_path: String = cs2.get_value("installpath")?; 53 | 54 | Ok(install_path) 55 | } 56 | 57 | #[cfg(not(target_family = "windows"))] 58 | fn find_cs2_install_path() -> Result { 59 | unimplemented!("auto-detecting cs2 install path is only supported on windows") 60 | } 61 | -------------------------------------------------------------------------------- /examples/interfaces.rs: -------------------------------------------------------------------------------- 1 | use cs2_analyzer::{Analyzer, AnalyzerOptions, Result}; 2 | 3 | use walkdir::WalkDir; 4 | 5 | fn main() -> Result<()> { 6 | let cs2_path = find_cs2_install_path()?; 7 | 8 | let dll_paths: Vec<_> = WalkDir::new(&cs2_path) 9 | .into_iter() 10 | .filter_map(|e| e.ok()) 11 | .filter(|e| e.file_name().to_string_lossy().ends_with(".dll")) 12 | .map(|e| e.path().to_path_buf()) 13 | .collect(); 14 | 15 | let mut analyzer = Analyzer::new_with_opts(AnalyzerOptions { 16 | buttons: false, 17 | concommands: false, 18 | convars: false, 19 | interfaces: true, 20 | offsets: false, 21 | schemas: false, 22 | }); 23 | 24 | analyzer.add_files(&dll_paths); 25 | 26 | // Analyze all added files (This may take a while). 27 | let result = analyzer.analyze(); 28 | 29 | for (file_name, result) in &result { 30 | for iface in &result.interfaces { 31 | println!( 32 | "found interface: {} in {} ({} + {:#X})", 33 | iface.name, file_name, file_name, iface.rva 34 | ); 35 | } 36 | 37 | println!( 38 | "found {} interfaces in {}", 39 | result.interfaces.len(), 40 | file_name 41 | ); 42 | } 43 | 44 | Ok(()) 45 | } 46 | 47 | #[cfg(target_family = "windows")] 48 | fn find_cs2_install_path() -> Result { 49 | use winreg::enums::HKEY_LOCAL_MACHINE; 50 | use winreg::RegKey; 51 | 52 | let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); 53 | 54 | let cs2 = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Valve\cs2")?; 55 | 56 | let install_path: String = cs2.get_value("installpath")?; 57 | 58 | Ok(install_path) 59 | } 60 | 61 | #[cfg(not(target_family = "windows"))] 62 | fn find_cs2_install_path() -> Result { 63 | unimplemented!("auto-detecting cs2 install path is only supported on windows") 64 | } 65 | -------------------------------------------------------------------------------- /examples/offsets.rs: -------------------------------------------------------------------------------- 1 | use cs2_analyzer::{Analyzer, AnalyzerOptions, Result}; 2 | 3 | use walkdir::WalkDir; 4 | 5 | fn main() -> Result<()> { 6 | let cs2_path = find_cs2_install_path()?; 7 | 8 | let dll_paths: Vec<_> = WalkDir::new(&cs2_path) 9 | .into_iter() 10 | .filter_map(|e| e.ok()) 11 | .filter(|e| e.file_name().to_string_lossy().ends_with(".dll")) 12 | .map(|e| e.path().to_path_buf()) 13 | .collect(); 14 | 15 | let mut analyzer = Analyzer::new_with_opts(AnalyzerOptions { 16 | buttons: false, 17 | concommands: false, 18 | convars: false, 19 | interfaces: false, 20 | offsets: true, 21 | schemas: false, 22 | }); 23 | 24 | analyzer.add_files(&dll_paths); 25 | 26 | // Analyze all added files (This may take a while). 27 | let result = analyzer.analyze(); 28 | 29 | for (file_name, result) in &result { 30 | for (name, value) in &result.offsets { 31 | println!("found offset: {} ({} + {:#X})", name, file_name, value); 32 | } 33 | 34 | println!("found {} offsets in {}", result.offsets.len(), file_name); 35 | } 36 | 37 | Ok(()) 38 | } 39 | 40 | #[cfg(target_family = "windows")] 41 | fn find_cs2_install_path() -> Result { 42 | use winreg::enums::HKEY_LOCAL_MACHINE; 43 | use winreg::RegKey; 44 | 45 | let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); 46 | 47 | let cs2 = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Valve\cs2")?; 48 | 49 | let install_path: String = cs2.get_value("installpath")?; 50 | 51 | Ok(install_path) 52 | } 53 | 54 | #[cfg(not(target_family = "windows"))] 55 | fn find_cs2_install_path() -> Result { 56 | unimplemented!("auto-detecting cs2 install path is only supported on windows") 57 | } 58 | -------------------------------------------------------------------------------- /examples/schemas.rs: -------------------------------------------------------------------------------- 1 | use cs2_analyzer::{Analyzer, AnalyzerOptions, Result}; 2 | 3 | use walkdir::WalkDir; 4 | 5 | fn main() -> Result<()> { 6 | let cs2_path = find_cs2_install_path()?; 7 | 8 | let dll_paths: Vec<_> = WalkDir::new(&cs2_path) 9 | .into_iter() 10 | .filter_map(|e| e.ok()) 11 | .filter(|e| e.file_name().to_string_lossy().ends_with(".dll")) 12 | .map(|e| e.path().to_path_buf()) 13 | .collect(); 14 | 15 | let mut analyzer = Analyzer::new_with_opts(AnalyzerOptions { 16 | buttons: false, 17 | concommands: false, 18 | convars: false, 19 | interfaces: false, 20 | offsets: false, 21 | schemas: true, 22 | }); 23 | 24 | analyzer.add_files(&dll_paths); 25 | 26 | // Analyze all added files (This may take a while). 27 | let result = analyzer.analyze(); 28 | 29 | for (file_name, result) in &result { 30 | for class in &result.classes { 31 | println!( 32 | "found class: {} in {} (field count: {}, parent name: {:?})", 33 | class.name, 34 | file_name, 35 | class.fields.len(), 36 | class.parent.as_ref().map(|p| p.name) 37 | ); 38 | } 39 | 40 | for enum_ in &result.enums { 41 | println!( 42 | "found enum: {} in {} (member count: {}, alignment: {}, type name: {})", 43 | enum_.name, 44 | file_name, 45 | enum_.members.len(), 46 | enum_.alignment, 47 | enum_.type_name 48 | ); 49 | } 50 | 51 | println!( 52 | "found {} classes and {} enums in {}", 53 | result.classes.len(), 54 | result.enums.len(), 55 | file_name 56 | ); 57 | } 58 | 59 | Ok(()) 60 | } 61 | 62 | #[cfg(target_family = "windows")] 63 | fn find_cs2_install_path() -> Result { 64 | use winreg::enums::HKEY_LOCAL_MACHINE; 65 | use winreg::RegKey; 66 | 67 | let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); 68 | 69 | let cs2 = hklm.open_subkey(r"SOFTWARE\WOW6432Node\Valve\cs2")?; 70 | 71 | let install_path: String = cs2.get_value("installpath")?; 72 | 73 | Ok(install_path) 74 | } 75 | 76 | #[cfg(not(target_family = "windows"))] 77 | fn find_cs2_install_path() -> Result { 78 | unimplemented!("auto-detecting cs2 install path is only supported on windows") 79 | } 80 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | comment_width = 100 2 | format_code_in_doc_comments = true 3 | format_macro_bodies = true 4 | format_macro_matchers = true 5 | imports_granularity = "Module" 6 | normalize_comments = true 7 | normalize_doc_attributes = true 8 | reorder_impl_items = true 9 | reorder_imports = true 10 | tab_spaces = 4 11 | wrap_comments = true 12 | -------------------------------------------------------------------------------- /src/analysis/client/buttons.rs: -------------------------------------------------------------------------------- 1 | use log::info; 2 | 3 | use pelite::pattern; 4 | use pelite::pe64::{Pe, PeFile, Rva}; 5 | 6 | use crate::error::Result; 7 | 8 | #[derive(Clone, Copy, Debug, PartialEq)] 9 | pub struct Button<'a> { 10 | pub name: &'a str, 11 | pub rva: Rva, 12 | } 13 | 14 | pub fn buttons(file: PeFile<'_>) -> Vec> { 15 | let mut matches = file.scanner().matches_code(pattern!( 16 | "4883ec28 4533? (488d15${'} | 4c8d05${'}) (488d0d${'} | 48ba[8] 488d0d${'}) e8${(48895c2408 | 40534883ec20)}" 17 | )); 18 | 19 | let mut save = [0; 3]; 20 | 21 | let mut list = Vec::new(); 22 | 23 | while matches.next(&mut save) { 24 | _ = read(file, &save, &mut list); 25 | } 26 | 27 | list.dedup_by_key(|k| k.name); 28 | list.sort_unstable_by_key(|k| k.name); 29 | 30 | list 31 | } 32 | 33 | fn read<'a>(file: PeFile<'a>, save: &[Rva], list: &mut Vec>) -> Result<()> { 34 | let name = file.derva_c_str(save[1])?.to_str()?; 35 | let rva = save[2] + 0x30 - 0x8; 36 | 37 | info!("found button: {} at {:#X}", name, rva); 38 | 39 | list.push(Button { name, rva }); 40 | 41 | Ok(()) 42 | } 43 | -------------------------------------------------------------------------------- /src/analysis/client/mod.rs: -------------------------------------------------------------------------------- 1 | pub use buttons::{buttons, Button}; 2 | pub use offsets::offsets; 3 | 4 | pub mod buttons; 5 | pub mod offsets; 6 | -------------------------------------------------------------------------------- /src/analysis/client/offsets.rs: -------------------------------------------------------------------------------- 1 | use std::collections::BTreeMap; 2 | 3 | use pelite::pattern; 4 | use pelite::pattern::{save_len, Atom}; 5 | use pelite::pe64::{Pe, PeFile, Rva}; 6 | 7 | use phf::phf_map; 8 | 9 | static PATTERNS: phf::Map<&'static str, &'static [Atom]> = phf_map! { 10 | "dwCSGOInput" => pattern!("488905${'} 0f57c0 0f1105"), 11 | "dwEntityList" => pattern!("488935${'} 4885f6"), 12 | "dwGameEntitySystem" => pattern!("488b1d${'} 48891d"), 13 | "dwGameEntitySystem_highestEntityIndex" => pattern!("8b81u2?? 8902 488bc2 c3 cccccccc 48895c24? 48896c24"), 14 | "dwGameRules" => pattern!("48891d${'} ff15${} 84c0"), 15 | "dwGlobalVars" => pattern!("488915${'} 488942"), 16 | "dwGlowManager" => pattern!("488b05${'} c3 cccccccccccccccc 8b41"), 17 | "dwLocalPlayerController" => pattern!("488905${'} 8b9e"), 18 | "dwPlantedC4" => pattern!("488b15${'} 41ffc0"), 19 | "dwPrediction" => pattern!("488d05${'} c3 cccccccccccccccc 4883ec? 8b0d"), 20 | "dwSensitivity" => pattern!("488d0d${[8]'} 440f28c1 0f28f3 0f28fa e8"), 21 | "dwSensitivity_sensitivity" => pattern!("ff50u1 4c8bc6 488d55? 488bcf e8${} 84c0 0f85${} 4c8d45? 8bd3 488bcf e8${} e9${} f30f1006"), 22 | "dwViewMatrix" => pattern!("488d0d${'} 48c1e006"), 23 | "dwViewRender" => pattern!("488905${'} 488bc8 4885c0"), 24 | "dwWeaponC4" => pattern!("488b15${'} 488b5c24? ffc0 8905[4] 488bc7"), 25 | }; 26 | 27 | pub fn offsets(file: PeFile<'_>) -> BTreeMap<&'static str, Rva> { 28 | let mut map = BTreeMap::new(); 29 | 30 | for (name, pat) in &PATTERNS { 31 | let mut save = vec![0; save_len(&pat)]; 32 | 33 | if !file.scanner().finds_code(pat, &mut save) { 34 | continue; 35 | } 36 | 37 | let rva = save[1]; 38 | 39 | match *name { 40 | "dwCSGOInput" => { 41 | let mut save = [0; 2]; 42 | 43 | if file 44 | .scanner() 45 | .finds_code(pattern!("f2410f108430u4"), &mut save) 46 | { 47 | map.insert("dwViewAngles", rva + save[1]); 48 | } 49 | } 50 | "dwPrediction" => { 51 | map.insert("dwLocalPlayerPawn", rva + 0x180); 52 | } 53 | _ => {} 54 | } 55 | 56 | map.insert(*name, rva); 57 | } 58 | 59 | map 60 | } 61 | -------------------------------------------------------------------------------- /src/analysis/concommands.rs: -------------------------------------------------------------------------------- 1 | use log::{info, warn}; 2 | 3 | use pelite::pattern; 4 | use pelite::pe64::{Pe, PeFile, Rva}; 5 | 6 | #[cfg(feature = "serde_support")] 7 | use serde::{Deserialize, Serialize}; 8 | 9 | use super::convars::ConVarFlags; 10 | 11 | use crate::error::Result; 12 | 13 | #[derive(Clone, Copy, Debug, PartialEq)] 14 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 15 | pub struct ConCommand<'a> { 16 | pub name: &'a str, 17 | pub description: Option<&'a str>, 18 | pub flags: ConVarFlags, 19 | } 20 | 21 | impl<'a> ConCommand<'a> { 22 | #[inline] 23 | pub fn contains(&self, flag: ConVarFlags) -> bool { 24 | (self.flags as u32) & (flag as u32) != 0 25 | } 26 | 27 | #[inline] 28 | pub fn is_cheat(&self) -> bool { 29 | self.contains(ConVarFlags::Cheat) 30 | } 31 | 32 | #[inline] 33 | pub fn is_dev_only(&self) -> bool { 34 | self.contains(ConVarFlags::DevelopmentOnly) 35 | } 36 | 37 | #[inline] 38 | pub fn is_hidden(&self) -> bool { 39 | self.contains(ConVarFlags::Hidden) 40 | } 41 | 42 | #[inline] 43 | pub fn is_protected(&self) -> bool { 44 | self.contains(ConVarFlags::Protected) 45 | } 46 | 47 | #[inline] 48 | pub fn is_replicated(&self) -> bool { 49 | self.contains(ConVarFlags::Replicated) 50 | } 51 | } 52 | 53 | pub fn concommands(file: PeFile<'_>) -> Vec> { 54 | // XREF: "RegisterConCommand: Unknown error registering con command \"%s\"!\n" 55 | let mut matches = file.scanner().matches_code(pattern!( 56 | "4c8d0d${'} [5-40] 488d15${'} (488d0d${} | 88442438 488d0d${}) (48c7442420u4 | [5-60] 48c7442420u4) [5-40] e8${48895c2408}" 57 | )); 58 | 59 | let mut save = [0; 4]; 60 | 61 | let mut list = Vec::new(); 62 | 63 | while matches.next(&mut save) { 64 | _ = read(file, &save, &mut list); 65 | } 66 | 67 | if list.is_empty() { 68 | warn!("unable to find any concommands"); 69 | } 70 | 71 | list.dedup_by_key(|k| k.name); 72 | list.sort_unstable_by_key(|k| k.name); 73 | 74 | list 75 | } 76 | 77 | fn read<'a>(file: PeFile<'a>, save: &[Rva], list: &mut Vec>) -> Result<()> { 78 | let description = Some(file.derva_c_str(save[1])?.to_str()?).filter(|s| !s.is_empty()); 79 | let name = file.derva_c_str(save[2])?.to_str()?; 80 | 81 | let flags = ConVarFlags::try_from(save[3]).unwrap_or(ConVarFlags::None); 82 | 83 | info!("found concommand: {}", name); 84 | 85 | list.push(ConCommand { 86 | name, 87 | description, 88 | flags, 89 | }); 90 | 91 | Ok(()) 92 | } 93 | -------------------------------------------------------------------------------- /src/analysis/convars.rs: -------------------------------------------------------------------------------- 1 | use log::{info, warn}; 2 | 3 | use num_enum::TryFromPrimitive; 4 | 5 | use pelite::pattern; 6 | use pelite::pe64::{Pe, PeFile, Rva}; 7 | 8 | #[cfg(feature = "serde_support")] 9 | use serde::{Deserialize, Serialize}; 10 | 11 | use crate::error::Result; 12 | 13 | // TODO: Add other flags. 14 | #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, TryFromPrimitive)] 15 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 16 | #[repr(u32)] 17 | pub enum ConVarFlags { 18 | None = 0x0, 19 | Unregistered = 0x1, 20 | DevelopmentOnly = 0x2, 21 | GameDll = 0x4, 22 | ClientDll = 0x8, 23 | Hidden = 0x10, 24 | Protected = 0x20, 25 | SpOnly = 0x40, 26 | Archive = 0x80, 27 | Notify = 0x100, 28 | UserInfo = 0x200, 29 | Unlogged = 0x800, 30 | Replicated = 0x2000, 31 | Cheat = 0x4000, 32 | PerUser = 0x8000, 33 | Demo = 0x10000, 34 | DontRecord = 0x20000, 35 | NotConnected = 0x40000, 36 | VConsoleSetFocus = 0x8000000, 37 | } 38 | 39 | #[derive(Clone, Copy, Debug, PartialEq)] 40 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 41 | pub struct ConVar<'a> { 42 | pub name: &'a str, 43 | pub description: Option<&'a str>, 44 | pub flags: ConVarFlags, 45 | pub rva: Rva, 46 | } 47 | 48 | impl<'a> ConVar<'a> { 49 | #[inline] 50 | pub fn contains(&self, flag: ConVarFlags) -> bool { 51 | (self.flags as u32) & (flag as u32) != 0 52 | } 53 | 54 | #[inline] 55 | pub fn is_cheat(&self) -> bool { 56 | self.contains(ConVarFlags::Cheat) 57 | } 58 | 59 | #[inline] 60 | pub fn is_dev_only(&self) -> bool { 61 | self.contains(ConVarFlags::DevelopmentOnly) 62 | } 63 | 64 | #[inline] 65 | pub fn is_hidden(&self) -> bool { 66 | self.contains(ConVarFlags::Hidden) 67 | } 68 | 69 | #[inline] 70 | pub fn is_protected(&self) -> bool { 71 | self.contains(ConVarFlags::Protected) 72 | } 73 | 74 | #[inline] 75 | pub fn is_replicated(&self) -> bool { 76 | self.contains(ConVarFlags::Replicated) 77 | } 78 | } 79 | 80 | pub fn convars(file: PeFile<'_>) -> Vec> { 81 | // XREF: "RegisterConVar: Unknown error registering convar \"%s\"!\n" 82 | let mut matches = file.scanner().matches_code(pattern!( 83 | "e8${48895c2408} 488d442430 c6442434? ('4533c9 | 4c8d0d${'}) 4889442420 41b8u4 (c6442437? | c7442437${}) 488d15${'} 488d0d${'}" 84 | )); 85 | 86 | let mut save = [0; 5]; 87 | 88 | let mut list = Vec::new(); 89 | 90 | while matches.next(&mut save) { 91 | _ = read(file, &save, &mut list); 92 | } 93 | 94 | if list.is_empty() { 95 | warn!("unable to find any convars"); 96 | } 97 | 98 | list.dedup_by_key(|k| k.name); 99 | list.sort_unstable_by_key(|k| k.name); 100 | 101 | list 102 | } 103 | 104 | fn read<'a>(file: PeFile<'a>, save: &[Rva], list: &mut Vec>) -> Result<()> { 105 | let description = file.derva_c_str(save[1])?.to_str().ok(); 106 | let name = file.derva_c_str(save[3])?.to_str()?; 107 | 108 | let flags = ConVarFlags::try_from(save[2]).unwrap_or(ConVarFlags::None); 109 | 110 | info!("found convar: {}", name); 111 | 112 | list.push(ConVar { 113 | name, 114 | description, 115 | flags, 116 | rva: save[4] + 0x8, 117 | }); 118 | 119 | Ok(()) 120 | } 121 | -------------------------------------------------------------------------------- /src/analysis/engine2/mod.rs: -------------------------------------------------------------------------------- 1 | pub use offsets::offsets; 2 | 3 | pub mod offsets; 4 | -------------------------------------------------------------------------------- /src/analysis/engine2/offsets.rs: -------------------------------------------------------------------------------- 1 | use std::collections::BTreeMap; 2 | 3 | use pelite::pattern; 4 | use pelite::pattern::{save_len, Atom}; 5 | use pelite::pe64::{Pe, PeFile, Rva}; 6 | 7 | use phf::phf_map; 8 | 9 | static PATTERNS: phf::Map<&'static str, &'static [Atom]> = phf_map! { 10 | "dwBuildNumber" => pattern!("8905${'} 488d0d${} ff15${} 488b0d"), 11 | "dwNetworkGameClient" => pattern!("48893d${'} 488d15"), 12 | "dwNetworkGameClient_clientTickCount" => pattern!("8b81u4 c3 cccccccccccccccccc 8b81${} c3 cccccccccccccccccc 83b9"), 13 | "dwNetworkGameClient_deltaTick" => pattern!("89b3u4 8b45"), 14 | "dwNetworkGameClient_isBackgroundMap" => pattern!("0fb681u4 c3 cccccccccccccccc 0fb681${} c3 cccccccccccccccc 48895c24"), 15 | "dwNetworkGameClient_localPlayer" => pattern!("4883c0u1 488d0440 8b0cc1"), 16 | "dwNetworkGameClient_maxClients" => pattern!("8b81u4 c3cccccccccccccccccc 8b81${} ffc0"), 17 | "dwNetworkGameClient_serverTickCount" => pattern!("8b81u4 c3 cccccccccccccccccc 83b9"), 18 | "dwNetworkGameClient_signOnState" => pattern!("448b81u4 488d0d"), 19 | "dwWindowHeight" => pattern!("8b05${'} 8903"), 20 | "dwWindowWidth" => pattern!("8b05${'} 8907"), 21 | }; 22 | 23 | pub fn offsets(file: PeFile<'_>) -> BTreeMap<&'static str, Rva> { 24 | let mut map = BTreeMap::new(); 25 | 26 | for (name, pat) in &PATTERNS { 27 | let mut save = vec![0; save_len(&pat)]; 28 | 29 | if !file.scanner().finds_code(pat, &mut save) { 30 | continue; 31 | } 32 | 33 | let mut rva = save[1]; 34 | 35 | if *name == "dwNetworkGameClient_localPlayer" { 36 | // .text 48 83 C0 0A | add rax, 0Ah 37 | // .text 48 8D 04 40 | lea rax, [rax + rax * 2] 38 | // .text 8B 0C C1 | mov ecx, [rcx + rax * 8] 39 | rva = (rva + (rva * 2)) * 8; 40 | } 41 | 42 | map.insert(*name, rva); 43 | } 44 | 45 | map 46 | } 47 | -------------------------------------------------------------------------------- /src/analysis/globals.rs: -------------------------------------------------------------------------------- 1 | use pelite::pe64::{msvc, Pe, PeFile, PeObject, Ptr, Rva}; 2 | 3 | use rayon::prelude::*; 4 | 5 | use crate::error::Result; 6 | 7 | // Common instances that aren't very interesting. 8 | const BLACKLIST: [&'static str; 2] = [".?AVexception@std@@", ".?AVtype_info@@"]; 9 | 10 | #[derive(Clone, Copy, Debug)] 11 | pub struct Global<'a> { 12 | pub type_name: &'a str, 13 | pub instance: Rva, 14 | } 15 | 16 | pub fn globals(file: PeFile<'_>) -> Vec> { 17 | let image = file.image(); 18 | 19 | let mut list: Vec<_> = (0..image.len() / 8) 20 | .into_par_iter() 21 | .filter_map(|i| { 22 | file.file_offset_to_rva(i * 8) 23 | .ok() 24 | .and_then(|rva| global(file, rva).ok()) 25 | .filter(|instance| !BLACKLIST.contains(&instance.type_name)) 26 | }) 27 | .collect(); 28 | 29 | list.dedup_by_key(|k| k.type_name); 30 | 31 | list 32 | } 33 | 34 | fn global(file: PeFile<'_>, rva: Rva) -> Result> { 35 | let vtable_va = *file.derva::(rva)?; 36 | let vtable_rva = file.va_to_rva(vtable_va)?; 37 | 38 | let col_ptr = *file.deref::>((vtable_va - 0x8).into())?; 39 | let col = file.deref(col_ptr)?; 40 | 41 | let type_info = file.derva::(col.type_descriptor)?; 42 | 43 | if type_info.spare != Ptr::null() { 44 | return Err(pelite::Error::Null.into()); 45 | } 46 | 47 | let type_name = file.derva_c_str(col.type_descriptor + 0x10)?.to_str()?; 48 | 49 | Ok(Global { 50 | type_name, 51 | instance: vtable_rva, 52 | }) 53 | } 54 | -------------------------------------------------------------------------------- /src/analysis/input_system/mod.rs: -------------------------------------------------------------------------------- 1 | pub use offsets::offsets; 2 | 3 | pub mod offsets; 4 | -------------------------------------------------------------------------------- /src/analysis/input_system/offsets.rs: -------------------------------------------------------------------------------- 1 | use std::collections::BTreeMap; 2 | 3 | use pelite::pattern; 4 | use pelite::pattern::{save_len, Atom}; 5 | use pelite::pe64::{Pe, PeFile, Rva}; 6 | 7 | use phf::phf_map; 8 | 9 | static PATTERNS: phf::Map<&'static str, &'static [Atom]> = phf_map! { 10 | "dwInputSystem" => pattern!("488905${'} 488d05"), 11 | }; 12 | 13 | pub fn offsets(file: PeFile<'_>) -> BTreeMap<&'static str, Rva> { 14 | let mut map = BTreeMap::new(); 15 | 16 | for (name, pat) in &PATTERNS { 17 | let mut save = vec![0; save_len(&pat)]; 18 | 19 | if !file.scanner().finds_code(pat, &mut save) { 20 | continue; 21 | } 22 | 23 | map.insert(*name, save[1]); 24 | } 25 | 26 | map 27 | } 28 | -------------------------------------------------------------------------------- /src/analysis/interfaces.rs: -------------------------------------------------------------------------------- 1 | use log::{info, warn}; 2 | 3 | use pelite::pattern; 4 | use pelite::pe64::{Pe, PeFile, Rva}; 5 | 6 | #[cfg(feature = "serde_support")] 7 | use serde::{Deserialize, Serialize}; 8 | 9 | use crate::error::Result; 10 | 11 | #[derive(Clone, Copy, Debug, PartialEq)] 12 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 13 | pub struct Interface<'a> { 14 | pub name: &'a str, 15 | pub rva: Rva, 16 | } 17 | 18 | pub fn interfaces(file: PeFile<'_>) -> Vec> { 19 | if file 20 | .exports() 21 | .unwrap() 22 | .by() 23 | .unwrap() 24 | .name("CreateInterface") 25 | .is_err() 26 | { 27 | return Vec::new(); 28 | } 29 | 30 | let mut matches = file.scanner().matches_code(pattern!( 31 | "cc 4c8d05${'} 488d15${488d05${'}} 488d0d${} e9${4c894108} cc" 32 | )); 33 | 34 | let mut save = [0; 3]; 35 | 36 | let mut list = Vec::new(); 37 | 38 | while matches.next(&mut save) { 39 | _ = read(file, &save, &mut list); 40 | } 41 | 42 | if list.is_empty() { 43 | warn!("unable to find any interfaces"); 44 | } 45 | 46 | list.sort_unstable_by_key(|k| k.name); 47 | 48 | list 49 | } 50 | 51 | fn read<'a>(file: PeFile<'a>, save: &[Rva], list: &mut Vec>) -> Result<()> { 52 | let name = file.derva_c_str(save[1])?.to_str()?; 53 | let rva = save[2]; 54 | 55 | info!("found interface: {} at {:#X}", name, rva); 56 | 57 | list.push(Interface { name, rva }); 58 | 59 | Ok(()) 60 | } 61 | -------------------------------------------------------------------------------- /src/analysis/matchmaking/mod.rs: -------------------------------------------------------------------------------- 1 | pub use offsets::offsets; 2 | 3 | pub mod offsets; 4 | -------------------------------------------------------------------------------- /src/analysis/matchmaking/offsets.rs: -------------------------------------------------------------------------------- 1 | use std::collections::BTreeMap; 2 | 3 | use pelite::pattern; 4 | use pelite::pattern::{save_len, Atom}; 5 | use pelite::pe64::{Pe, PeFile, Rva}; 6 | 7 | use phf::phf_map; 8 | 9 | static PATTERNS: phf::Map<&'static str, &'static [Atom]> = phf_map! { 10 | "dwGameTypes" => pattern!("488d0d${'} 33d2"), 11 | "dwGameTypes_mapName" => pattern!("488b81u4 4885c074? 4883c0"), 12 | }; 13 | 14 | pub fn offsets(file: PeFile<'_>) -> BTreeMap<&'static str, Rva> { 15 | let mut map = BTreeMap::new(); 16 | 17 | for (name, pat) in &PATTERNS { 18 | let mut save = vec![0; save_len(&pat)]; 19 | 20 | if !file.scanner().finds_code(pat, &mut save) { 21 | continue; 22 | } 23 | 24 | map.insert(*name, save[1]); 25 | } 26 | 27 | map 28 | } 29 | -------------------------------------------------------------------------------- /src/analysis/mod.rs: -------------------------------------------------------------------------------- 1 | pub use client::Button; 2 | pub use concommands::ConCommand; 3 | pub use convars::{ConVar, ConVarFlags}; 4 | pub use globals::Global; 5 | pub use interfaces::Interface; 6 | pub use schemas::{Class, ClassField, Enum, EnumMember}; 7 | 8 | use std::collections::BTreeMap; 9 | 10 | use pelite::pattern; 11 | use pelite::pe64::{Pe, PeFile, Rva}; 12 | 13 | use crate::error::{Error, Result}; 14 | 15 | pub mod client; 16 | pub mod concommands; 17 | pub mod convars; 18 | pub mod engine2; 19 | pub mod globals; 20 | pub mod input_system; 21 | pub mod interfaces; 22 | pub mod matchmaking; 23 | pub mod schemas; 24 | pub mod sound_system; 25 | 26 | #[derive(Clone, Debug, Default, PartialEq)] 27 | pub struct AnalysisResult<'a> { 28 | pub buttons: Vec>, 29 | pub concommands: Vec>, 30 | pub convars: Vec>, 31 | pub interfaces: Vec>, 32 | pub offsets: BTreeMap<&'a str, Rva>, 33 | pub classes: Vec>, 34 | pub enums: Vec>, 35 | } 36 | 37 | #[derive(Clone, Copy, Debug, PartialEq)] 38 | pub struct AnalyzerOptions { 39 | /// Whether to parse key buttons. 40 | pub buttons: bool, 41 | 42 | /// Whether to parse concommands. 43 | pub concommands: bool, 44 | 45 | /// Whether to parse convars. 46 | pub convars: bool, 47 | 48 | /// Whether to parse interfaces. 49 | pub interfaces: bool, 50 | 51 | /// Whether to parse offsets. 52 | pub offsets: bool, 53 | 54 | /// Whether to parse schema classes/enums. 55 | pub schemas: bool, 56 | } 57 | 58 | impl Default for AnalyzerOptions { 59 | fn default() -> Self { 60 | Self { 61 | buttons: true, 62 | concommands: true, 63 | convars: true, 64 | interfaces: true, 65 | offsets: true, 66 | schemas: true, 67 | } 68 | } 69 | } 70 | 71 | pub fn analyze(file: PeFile<'_>) -> Result> { 72 | analyze_with_opts(file, &AnalyzerOptions::default()) 73 | } 74 | 75 | pub fn analyze_with_opts<'a>( 76 | file: PeFile<'a>, 77 | opts: &AnalyzerOptions, 78 | ) -> Result> { 79 | let module_name = read_module_name(file)?; 80 | 81 | let mut result = AnalysisResult::default(); 82 | 83 | if opts.buttons { 84 | result.buttons = match module_name { 85 | "client.dll" => client::buttons(file), 86 | _ => Vec::new(), 87 | }; 88 | } 89 | 90 | if opts.concommands { 91 | result.concommands = concommands::concommands(file); 92 | } 93 | 94 | if opts.convars { 95 | result.convars = convars::convars(file); 96 | } 97 | 98 | if opts.interfaces { 99 | result.interfaces = interfaces::interfaces(file); 100 | } 101 | 102 | if opts.offsets { 103 | result.offsets = match module_name { 104 | "client.dll" => client::offsets(file), 105 | "engine2.dll" => engine2::offsets(file), 106 | "inputsystem.dll" => input_system::offsets(file), 107 | "matchmaking.dll" => matchmaking::offsets(file), 108 | "soundsystem.dll" => sound_system::offsets(file), 109 | _ => BTreeMap::new(), 110 | }; 111 | } 112 | 113 | if opts.schemas { 114 | let (classes, enums) = schemas::schemas(file); 115 | 116 | result.classes = classes; 117 | result.enums = enums; 118 | } 119 | 120 | Ok(result) 121 | } 122 | 123 | fn read_module_name(file: PeFile<'_>) -> Result<&str> { 124 | let mut save = [0; 2]; 125 | 126 | if !file 127 | .scanner() 128 | .finds_code(pattern!("e8${488d05${'}} 488bd0498bcf"), &mut save) 129 | { 130 | return Err(Error::Other("unable to read module name")); 131 | } 132 | 133 | let name = file.derva_c_str(save[1])?.to_str()?; 134 | 135 | Ok(name) 136 | } 137 | -------------------------------------------------------------------------------- /src/analysis/schemas.rs: -------------------------------------------------------------------------------- 1 | use std::mem; 2 | use std::ops::Not; 3 | 4 | use log::{info, warn}; 5 | 6 | use pelite::pattern::{save_len, Atom}; 7 | use pelite::pe64::{Pe, PeFile, Ptr, Rva, Va}; 8 | use pelite::{pattern, Pod}; 9 | 10 | use rayon::prelude::*; 11 | 12 | #[cfg(feature = "serde_support")] 13 | use serde::{Deserialize, Serialize}; 14 | 15 | use super::globals; 16 | 17 | use crate::error::{Error, Result}; 18 | use crate::source2::*; 19 | 20 | #[derive(Clone, Debug, PartialEq)] 21 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 22 | pub struct Class<'a> { 23 | pub name: &'a str, 24 | pub parent: Option>>, 25 | pub fields: Vec>, 26 | pub metadata: Option>, 27 | } 28 | 29 | #[derive(Clone, Copy, Debug, PartialEq)] 30 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 31 | pub struct ClassField<'a> { 32 | pub name: &'a str, 33 | pub type_: Option, 34 | pub offset: i32, 35 | } 36 | 37 | #[derive(Clone, Copy, Debug, PartialEq)] 38 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 39 | pub struct ClassMetadata<'a> { 40 | pub name: &'a str, 41 | pub function: Rva, 42 | } 43 | 44 | #[derive(Clone, Debug, PartialEq)] 45 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 46 | pub struct Enum<'a> { 47 | pub name: &'a str, 48 | pub type_name: &'a str, 49 | pub alignment: u8, 50 | pub size: u16, 51 | pub members: Vec>, 52 | } 53 | 54 | impl<'a> Enum<'a> { 55 | #[inline] 56 | pub fn is_valid(&self) -> bool { 57 | self.size > 0 && self.alignment >= 1 && self.alignment <= 8 58 | } 59 | } 60 | 61 | #[derive(Clone, Copy, Debug, PartialEq)] 62 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 63 | pub struct EnumMember<'a> { 64 | pub name: &'a str, 65 | pub value: i64, 66 | } 67 | 68 | struct SchemaRegistration<'a> { 69 | #[allow(dead_code)] 70 | type_name: &'a str, 71 | constructor: Rva, 72 | } 73 | 74 | pub fn schemas(file: PeFile<'_>) -> (Vec>, Vec>) { 75 | if file 76 | .exports() 77 | .unwrap() 78 | .by() 79 | .unwrap() 80 | .name("InstallSchemaBindings") 81 | .is_err() 82 | { 83 | return (Vec::new(), Vec::new()); 84 | } 85 | 86 | let regs = schema_registrations(file); 87 | 88 | let mut classes: Vec<_> = regs 89 | .par_iter() 90 | .flat_map(|reg| process_entries::( 91 | file, 92 | reg, 93 | pattern!("(41??? | 83??) 7513 488b? (498bca 488d15${'} | 488d15${'}) (488b? ff90${} | ff90${})"), 94 | read_class, 95 | )) 96 | .collect(); 97 | 98 | let mut enums: Vec<_> = regs 99 | .par_iter() 100 | .flat_map(|reg| { 101 | process_entries::( 102 | file, 103 | reg, 104 | pattern!("488b? 488d?${'} 4889?2428 4c8d0d${}"), 105 | read_enum, 106 | ) 107 | }) 108 | .collect(); 109 | 110 | if classes.is_empty() { 111 | warn!("unable to find any classes"); 112 | } 113 | 114 | if enums.is_empty() { 115 | warn!("unable to find any enums"); 116 | } 117 | 118 | classes.sort_unstable_by_key(|k| k.name); 119 | enums.sort_unstable_by_key(|k| k.name); 120 | 121 | (classes, enums) 122 | } 123 | 124 | fn process_entries<'a, T, F, E>( 125 | file: PeFile<'a>, 126 | reg: &SchemaRegistration<'a>, 127 | pat: &[Atom], 128 | callback: F, 129 | ) -> Vec 130 | where 131 | T: Pod, 132 | F: Fn(PeFile<'a>, Ptr) -> Result, 133 | { 134 | let mut matches = file.scanner().matches_code(pat); 135 | 136 | let mut save = vec![0; save_len(pat)]; 137 | 138 | let start_addr = reg.constructor; 139 | let end_addr = start_addr + 0x1000; 140 | 141 | let mut list = Vec::new(); 142 | 143 | while matches.next(&mut save) { 144 | if start_addr < save[0] && save[0] < end_addr { 145 | if let Ok(entries) = table_entries::(file, save[1]) { 146 | for entry in &entries { 147 | if let Ok(result) = callback(file, *entry) { 148 | list.push(result); 149 | } 150 | } 151 | } 152 | } 153 | } 154 | 155 | list 156 | } 157 | 158 | fn read_class(file: PeFile<'_>, ptr: Ptr) -> Result> { 159 | let data = file.deref(ptr)?; 160 | let name = file.deref_c_str(data.name)?.to_str()?; 161 | 162 | let fields = read_class_fields(file, &data)?; 163 | 164 | let metadata = data 165 | .static_metadata 166 | .is_null() 167 | .not() 168 | .then(|| read_class_metadata(file, data.static_metadata)) 169 | .transpose()?; 170 | 171 | let parent = data 172 | .base_classes 173 | .is_null() 174 | .not() 175 | .then(|| { 176 | let base_class = file.deref(data.base_classes)?; 177 | 178 | read_class(file, base_class.prev).map(Box::new) 179 | }) 180 | .transpose()?; 181 | 182 | info!( 183 | "found class: {} (field count: {}, parent name: {:?})", 184 | name, 185 | fields.len(), 186 | parent.as_ref().map(|p| p.name) 187 | ); 188 | 189 | Ok(Class { 190 | name, 191 | parent, 192 | fields, 193 | metadata, 194 | }) 195 | } 196 | 197 | fn read_class_fields<'a>( 198 | file: PeFile<'a>, 199 | data: &SchemaClassInfoData, 200 | ) -> Result>> { 201 | (0..data.field_count) 202 | .into_par_iter() 203 | .map(|i| { 204 | let ptr = data.fields.at(i as _); 205 | 206 | let data = file.deref(ptr)?; 207 | let name = file.deref_c_str(data.name)?.to_str()?; 208 | 209 | Ok(ClassField { 210 | name, 211 | type_: data.schema_type(), 212 | offset: data.single_inheritance_offset, 213 | }) 214 | }) 215 | .collect() 216 | } 217 | 218 | fn read_class_metadata( 219 | file: PeFile<'_>, 220 | ptr: Ptr, 221 | ) -> Result> { 222 | let data = file.deref(ptr)?; 223 | let name = file.deref_c_str(data.name)?.to_str()?; 224 | let function_va = *file.deref(data.function)?; 225 | let function_rva = file.va_to_rva(function_va)?; 226 | 227 | Ok(ClassMetadata { 228 | name, 229 | function: function_rva, 230 | }) 231 | } 232 | 233 | fn read_enum(file: PeFile<'_>, ptr: Ptr) -> Result> { 234 | let data = file.deref(ptr)?; 235 | let name = file.deref_c_str(data.name)?.to_str()?; 236 | 237 | let members = read_enum_members(file, &data)?; 238 | 239 | let enum_ = Enum { 240 | name, 241 | type_name: data.type_name(), 242 | alignment: data.alignment, 243 | size: data.enumerator_count, 244 | members, 245 | }; 246 | 247 | if !enum_.is_valid() { 248 | return Err(Error::Other("invalid enum")); 249 | } 250 | 251 | info!( 252 | "found enum: {} (member count: {}, alignment: {}, type name: {})", 253 | enum_.name, 254 | enum_.members.len(), 255 | enum_.alignment, 256 | enum_.type_name 257 | ); 258 | 259 | Ok(enum_) 260 | } 261 | 262 | fn read_enum_members<'a>( 263 | file: PeFile<'a>, 264 | data: &SchemaEnumInfoData, 265 | ) -> Result>> { 266 | (0..data.enumerator_count) 267 | .into_par_iter() 268 | .map(|i| { 269 | let ptr = data.enumerators.at(i as _); 270 | 271 | let data = file.deref(ptr)?; 272 | let name = file.deref_c_str(data.name)?.to_str()?; 273 | 274 | Ok(EnumMember { 275 | name, 276 | value: unsafe { data.value.ulong } as i64, 277 | }) 278 | }) 279 | .collect() 280 | } 281 | 282 | fn schema_registrations(file: PeFile<'_>) -> Vec> { 283 | globals::globals(file) 284 | .par_iter() 285 | .filter(|instance| instance.type_name.contains("CSchemaRegistration_")) 286 | .filter_map(|global| { 287 | file.derva(global.instance) 288 | .and_then(|va| file.va_to_rva(*va)) 289 | .map(|constructor| SchemaRegistration { 290 | type_name: global.type_name, 291 | constructor, 292 | }) 293 | .inspect(|reg| { 294 | info!( 295 | "found schema registration: {} at {:#X} (constructor: {:#X})", 296 | global.type_name, global.instance, reg.constructor 297 | ); 298 | }) 299 | .ok() 300 | }) 301 | .collect() 302 | } 303 | 304 | fn table_entries(file: PeFile<'_>, table: Rva) -> Result>> { 305 | let mut cur_entry = file.rva_to_va(table)?; 306 | 307 | let mut entries = Vec::new(); 308 | 309 | while let Ok(entry) = file.deref_copy::>(cur_entry.into()) { 310 | if entry.is_null() { 311 | break; 312 | } 313 | 314 | entries.push(entry); 315 | 316 | cur_entry += mem::size_of::() as Va; 317 | } 318 | 319 | Ok(entries) 320 | } 321 | -------------------------------------------------------------------------------- /src/analysis/sound_system/mod.rs: -------------------------------------------------------------------------------- 1 | pub use offsets::offsets; 2 | 3 | pub mod offsets; 4 | -------------------------------------------------------------------------------- /src/analysis/sound_system/offsets.rs: -------------------------------------------------------------------------------- 1 | use std::collections::BTreeMap; 2 | 3 | use pelite::pattern; 4 | use pelite::pattern::{save_len, Atom}; 5 | use pelite::pe64::{Pe, PeFile, Rva}; 6 | 7 | use phf::phf_map; 8 | 9 | static PATTERNS: phf::Map<&'static str, &'static [Atom]> = phf_map! { 10 | "dwSoundSystem" => pattern!("488d05${'} c3 cccccccccccccccc 488915"), 11 | "dwSoundSystem_engineViewData" => pattern!("0f1147u1 0f104b"), 12 | }; 13 | 14 | pub fn offsets(file: PeFile<'_>) -> BTreeMap<&'static str, Rva> { 15 | let mut map = BTreeMap::new(); 16 | 17 | for (name, pat) in &PATTERNS { 18 | let mut save = vec![0; save_len(&pat)]; 19 | 20 | if !file.scanner().finds_code(pat, &mut save) { 21 | continue; 22 | } 23 | 24 | map.insert(*name, save[1]); 25 | } 26 | 27 | map 28 | } 29 | -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | use thiserror::Error; 2 | 3 | pub type Result = std::result::Result; 4 | 5 | #[derive(Debug, Error)] 6 | pub enum Error { 7 | #[error(transparent)] 8 | Io(#[from] std::io::Error), 9 | 10 | #[error(transparent)] 11 | Pelite(#[from] pelite::Error), 12 | 13 | #[error(transparent)] 14 | Utf8(#[from] std::str::Utf8Error), 15 | 16 | #[error("{0}")] 17 | Other(&'static str), 18 | } 19 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | pub use analysis::{AnalysisResult, AnalyzerOptions}; 2 | pub use error::{Error, Result}; 3 | 4 | use std::collections::HashMap; 5 | use std::path::Path; 6 | 7 | use pelite::pe64::PeFile; 8 | 9 | #[cfg(not(target_arch = "wasm32"))] 10 | use pelite::FileMap; 11 | 12 | pub mod analysis; 13 | pub mod error; 14 | 15 | mod source2; 16 | 17 | #[derive(Clone, Debug, PartialEq)] 18 | pub struct Analyzer { 19 | files: HashMap>, 20 | options: AnalyzerOptions, 21 | } 22 | 23 | impl Analyzer { 24 | /// Creates a new [`Analyzer`] instance with the default options. 25 | pub fn new() -> Self { 26 | Self { 27 | files: HashMap::new(), 28 | options: AnalyzerOptions::default(), 29 | } 30 | } 31 | 32 | /// Creates a new [`Analyzer`] instance with the specified options. 33 | pub fn new_with_opts(options: AnalyzerOptions) -> Self { 34 | Self { 35 | files: HashMap::new(), 36 | options, 37 | } 38 | } 39 | 40 | /// Adds a file to the analyzer. 41 | #[cfg(target_arch = "wasm32")] 42 | pub fn add_file>(&mut self, _path: P) -> Result<()> { 43 | Err(Error::Other( 44 | "Analyzer::add_file is not supported in the WebAssembly target", 45 | )) 46 | } 47 | 48 | /// Adds a file to the analyzer. 49 | #[cfg(not(target_arch = "wasm32"))] 50 | pub fn add_file>(&mut self, path: P) { 51 | let path = path.as_ref(); 52 | 53 | if let Some(file_name) = path.file_name().and_then(|f| f.to_str()) { 54 | if let Ok(map) = FileMap::open(path) { 55 | let data = map.as_ref().to_vec(); 56 | 57 | self.files.insert(file_name.to_string(), data); 58 | } 59 | } 60 | } 61 | 62 | /// Adds multiple files to the analyzer. 63 | #[cfg(target_arch = "wasm32")] 64 | pub fn add_files>(&mut self, _paths: &[P]) -> Result<()> { 65 | Err(Error::Other( 66 | "Analyzer::add_files is not supported in the WebAssembly target", 67 | )) 68 | } 69 | 70 | /// Adds multiple files to the analyzer. 71 | #[cfg(not(target_arch = "wasm32"))] 72 | pub fn add_files>(&mut self, paths: &[P]) { 73 | for path in paths { 74 | self.add_file(path); 75 | } 76 | } 77 | 78 | /// Analyzes all added files. 79 | #[cfg(target_arch = "wasm32")] 80 | pub fn analyze(&self) -> Result>> { 81 | Err(Error::Other( 82 | "Analyzer::analyze is not supported in the WebAssembly target", 83 | )) 84 | } 85 | 86 | /// Analyzes all added files. 87 | #[cfg(not(target_arch = "wasm32"))] 88 | pub fn analyze(&self) -> HashMap> { 89 | let mut results = HashMap::new(); 90 | 91 | for (file_name, data) in &self.files { 92 | if let Ok(result) = self.analyze_from_bytes(data) { 93 | results.insert(file_name.clone(), result); 94 | } 95 | } 96 | 97 | results 98 | } 99 | 100 | /// Analyzes a file by name. 101 | #[cfg(target_arch = "wasm32")] 102 | pub fn analyze_file(&self, _file_name: &str) -> Result> { 103 | Err(Error::Other( 104 | "Analyzer::analyze_file is not supported in the WebAssembly target", 105 | )) 106 | } 107 | 108 | /// Analyzes a file by name. 109 | #[cfg(not(target_arch = "wasm32"))] 110 | pub fn analyze_file(&self, file_name: &str) -> Result> { 111 | if let Some(data) = self.files.get(file_name) { 112 | self.analyze_from_bytes(data) 113 | } else { 114 | Err(Error::Other("file not found")) 115 | } 116 | } 117 | 118 | /// Analyzes a file from a byte slice. 119 | pub fn analyze_from_bytes<'a>(&self, bytes: &'a [u8]) -> Result> { 120 | let file = PeFile::from_bytes(bytes)?; 121 | 122 | analysis::analyze_with_opts(file, &self.options) 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/source2/mod.rs: -------------------------------------------------------------------------------- 1 | pub use schema_base_class_info_data::SchemaBaseClassInfoData; 2 | pub use schema_class_field_data::SchemaClassFieldData; 3 | pub use schema_class_info_data::SchemaClassInfoData; 4 | pub use schema_enum_info_data::SchemaEnumInfoData; 5 | pub use schema_enumerator_info_data::SchemaEnumeratorInfoData; 6 | pub use schema_field_type::SchemaFieldType; 7 | pub use schema_metadata_entry_data::SchemaMetadataEntryData; 8 | 9 | pub mod schema_base_class_info_data; 10 | pub mod schema_class_field_data; 11 | pub mod schema_class_info_data; 12 | pub mod schema_enum_info_data; 13 | pub mod schema_enumerator_info_data; 14 | pub mod schema_field_type; 15 | pub mod schema_metadata_entry_data; 16 | -------------------------------------------------------------------------------- /src/source2/schema_base_class_info_data.rs: -------------------------------------------------------------------------------- 1 | use pelite::pe64::Ptr; 2 | use pelite::Pod; 3 | 4 | use super::SchemaClassInfoData; 5 | 6 | #[derive(Pod)] 7 | #[repr(C)] 8 | pub struct SchemaBaseClassInfoData { 9 | pub offset: u32, // 0x0000 10 | pad_0004: [u8; 4], // 0x0004 11 | pub prev: Ptr, // 0x0008 12 | } 13 | -------------------------------------------------------------------------------- /src/source2/schema_class_field_data.rs: -------------------------------------------------------------------------------- 1 | use pelite::pe64::Ptr; 2 | use pelite::util::CStr; 3 | use pelite::Pod; 4 | 5 | use super::SchemaFieldType; 6 | 7 | #[derive(Pod)] 8 | #[repr(C)] 9 | pub struct SchemaClassFieldData { 10 | pub name: Ptr, // 0x0000 11 | pub schema_type: u8, // 0x0008 12 | pad_0009: [u8; 0x7], // 0x0009 13 | pub single_inheritance_offset: i32, // 0x0010 14 | pad_0014: [u8; 0xC], // 0x0014 15 | } 16 | 17 | impl SchemaClassFieldData { 18 | #[inline] 19 | pub fn schema_type(&self) -> Option { 20 | SchemaFieldType::try_from(self.schema_type).ok() 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/source2/schema_class_info_data.rs: -------------------------------------------------------------------------------- 1 | use pelite::pe64::{Ptr, Va}; 2 | use pelite::util::CStr; 3 | use pelite::Pod; 4 | 5 | use super::{SchemaBaseClassInfoData, SchemaClassFieldData, SchemaMetadataEntryData}; 6 | 7 | #[derive(Pod)] 8 | #[repr(C)] 9 | pub struct SchemaClassInfoData { 10 | pad_0000: [u8; 0x8], // 0x0000 11 | pub name: Ptr, // 0x0008 12 | pad_0010: [u8; 0x8], // 0x0010 13 | pub size: i32, // 0x0018 14 | pub field_count: u16, // 0x001C 15 | pad_001e: [u8; 0x4], // 0x001E 16 | pub alignment: u8, // 0x0022 17 | pad_0023: [u8; 0x5], // 0x0023 18 | pub fields: Ptr<[SchemaClassFieldData]>, // 0x0028 19 | pad_0030: [u8; 0x8], // 0x0030 20 | pub base_classes: Ptr, // 0x0038 21 | pad_0040: [u8; 0x8], // 0x0040 22 | pub static_metadata: Ptr, // 0x0048 23 | pad_0050: [u8; 0x18], // 0x0050 24 | pub function: Ptr, // 0x0068 25 | } 26 | -------------------------------------------------------------------------------- /src/source2/schema_enum_info_data.rs: -------------------------------------------------------------------------------- 1 | use pelite::pe64::Ptr; 2 | use pelite::util::CStr; 3 | use pelite::Pod; 4 | 5 | use super::SchemaEnumeratorInfoData; 6 | 7 | #[derive(Pod)] 8 | #[repr(C)] 9 | pub struct SchemaEnumInfoData { 10 | pad_0000: [u8; 0x8], // 0x0000 11 | pub name: Ptr, // 0x0008 12 | pad_0010: [u8; 0x8], // 0x0010 13 | pub alignment: u8, // 0x0018 14 | pad_0019: [u8; 0x3], // 0x0019 15 | pub enumerator_count: u16, // 0x001C 16 | pub static_metadata_count: u16, // 0x001E 17 | pub enumerators: Ptr<[SchemaEnumeratorInfoData]>, // 0x0020 18 | pad_0028: [u8; 0x20], // 0x0028 19 | } 20 | 21 | impl SchemaEnumInfoData { 22 | #[inline] 23 | pub fn type_name(&self) -> &str { 24 | match self.alignment { 25 | 1 => "uint8", 26 | 2 => "uint16", 27 | 4 => "uint32", 28 | 8 => "uint64", 29 | _ => "unknown", 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/source2/schema_enumerator_info_data.rs: -------------------------------------------------------------------------------- 1 | use pelite::pe64::Ptr; 2 | use pelite::util::CStr; 3 | use pelite::Pod; 4 | 5 | use super::SchemaMetadataEntryData; 6 | 7 | #[derive(Pod)] 8 | #[repr(C)] 9 | pub struct SchemaEnumeratorInfoData { 10 | pub name: Ptr, // 0x0000 11 | pub value: SchemaEnumeratorInfoDataUnion, // 0x0008 12 | pub static_metadata_count: i32, // 0x0010 13 | pad_0014: [u8; 0x4], // 0x0014 14 | pub static_metadata: Ptr, // 0x0018 15 | } 16 | 17 | #[repr(C)] 18 | pub union SchemaEnumeratorInfoDataUnion { 19 | pub uchar: u8, 20 | pub ushort: u16, 21 | pub uint: u32, 22 | pub ulong: u64, 23 | } 24 | 25 | unsafe impl Pod for SchemaEnumeratorInfoDataUnion {} 26 | -------------------------------------------------------------------------------- /src/source2/schema_field_type.rs: -------------------------------------------------------------------------------- 1 | use num_enum::TryFromPrimitive; 2 | 3 | #[cfg(feature = "serde_support")] 4 | use serde::{Deserialize, Serialize}; 5 | 6 | // TODO: Look into this properly. 7 | #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, TryFromPrimitive)] 8 | #[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))] 9 | #[repr(u8)] 10 | pub enum SchemaFieldType { 11 | Int8 = 6, 12 | UInt8 = 7, 13 | Int16 = 8, 14 | UInt16 = 9, 15 | Int32 = 10, 16 | UInt32 = 11, 17 | Int64 = 12, 18 | UInt64 = 13, 19 | Float32 = 14, 20 | Float64 = 15, 21 | Bool = 16, 22 | Vector = 18, 23 | VectorAligned = 19, 24 | Vector2D = 20, 25 | Vector4D = 21, 26 | QAngle = 22, 27 | Quaternion = 23, 28 | QuaternionStorage = 24, 29 | RadianEuler = 25, 30 | DegreeEuler = 26, 31 | Matrix3x4 = 28, 32 | Matrix3x4a = 29, 33 | CTransform = 30, 34 | Color = 32, 35 | CUtlBinaryBlock = 34, 36 | CUtlString = 35, 37 | CUtlSymbol = 36, 38 | CUtlStringToken = 38, 39 | } 40 | -------------------------------------------------------------------------------- /src/source2/schema_metadata_entry_data.rs: -------------------------------------------------------------------------------- 1 | use pelite::pe64::{Ptr, Va}; 2 | use pelite::util::CStr; 3 | use pelite::Pod; 4 | 5 | #[derive(Pod)] 6 | #[repr(C)] 7 | pub struct SchemaMetadataEntryData { 8 | pub name: Ptr, // 0x0000 9 | pub function: Ptr, // 0x0008 10 | } 11 | -------------------------------------------------------------------------------- /wasm/.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | -------------------------------------------------------------------------------- /wasm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cs2-analyzer-wasm" 3 | version = "0.1.0" 4 | authors = ["a2x"] 5 | edition = "2021" 6 | readme = "../README.md" 7 | repository = "https://github.com/a2x/cs2-analyzer" 8 | license = "MIT" 9 | 10 | [dependencies] 11 | cs2-analyzer = { path = ".." } 12 | gloo = "0.11" 13 | js-sys = "0.3" 14 | yew = { version = "0.21", features = ["csr"] } 15 | 16 | [dependencies.web-sys] 17 | version = "0.3" 18 | features = ["DataTransfer", "DragEvent", "File"] 19 | -------------------------------------------------------------------------------- /wasm/README.md: -------------------------------------------------------------------------------- 1 | # cs2-analyzer (WASM Demo) 2 | 3 | ## Running the Demo 4 | 5 | 1. `cargo install trunk --locked` 6 | 2. `trunk serve --release` -------------------------------------------------------------------------------- /wasm/Trunk.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "index.html" 3 | dist = "dist" 4 | release = false 5 | public_url = "/cs2-analyzer/" 6 | 7 | [watch] 8 | watch = ["./index.html", "src/"] 9 | 10 | [clean] 11 | dist = "dist" 12 | -------------------------------------------------------------------------------- /wasm/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | cs2-analyzer - WASM Demo 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /wasm/index.scss: -------------------------------------------------------------------------------- 1 | .file-upload-zone { 2 | border: 2px dashed #CCC; 3 | border-radius: 20px; 4 | cursor: pointer; 5 | margin-top: 2rem; 6 | padding: 20px; 7 | text-align: center; 8 | width: 100%; 9 | } 10 | 11 | .file-upload-zone:hover { 12 | background-color: rgba(255, 255, 255, 0.1); 13 | } -------------------------------------------------------------------------------- /wasm/src/app.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::rc::Rc; 3 | 4 | use cs2_analyzer::Analyzer; 5 | 6 | use gloo::file::callbacks::FileReader; 7 | use gloo::file::File; 8 | 9 | use yew::prelude::*; 10 | 11 | use crate::components::*; 12 | 13 | pub enum Msg { 14 | Loaded(String, Vec), 15 | Files(Vec), 16 | ClearFiles, 17 | } 18 | 19 | #[derive(Clone, PartialEq)] 20 | pub struct FileDetails { 21 | pub name: String, 22 | pub data: Vec, 23 | } 24 | 25 | pub struct App { 26 | analyzer: Analyzer, 27 | files: Vec, 28 | loading: bool, 29 | readers: HashMap, 30 | } 31 | 32 | impl Component for App { 33 | type Message = Msg; 34 | type Properties = (); 35 | 36 | fn create(_ctx: &Context) -> Self { 37 | Self { 38 | analyzer: Analyzer::new(), 39 | files: Vec::default(), 40 | loading: false, 41 | readers: HashMap::default(), 42 | } 43 | } 44 | 45 | fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { 46 | match msg { 47 | Msg::Loaded(file_name, data) => { 48 | if !self.readers.contains_key(&file_name) { 49 | return false; 50 | } 51 | 52 | self.files.push(FileDetails { 53 | data, 54 | name: file_name.clone(), 55 | }); 56 | 57 | self.readers.remove(&file_name); 58 | 59 | // Stop the loading animation if there are no more files to read. 60 | self.loading = !self.readers.is_empty(); 61 | 62 | true 63 | } 64 | Msg::Files(files) => { 65 | for file in &files { 66 | let file_name = file.name(); 67 | 68 | // Skip files that have already been processed. 69 | if self.files.iter().any(|f| f.name == file_name) { 70 | continue; 71 | } 72 | 73 | self.loading = true; 74 | 75 | let task = { 76 | let file_name = file_name.clone(); 77 | let link = ctx.link().clone(); 78 | 79 | gloo::file::callbacks::read_as_bytes(&file, move |res| { 80 | link.send_message(Msg::Loaded( 81 | file_name, 82 | res.expect("failed to read file"), 83 | )) 84 | }) 85 | }; 86 | 87 | self.readers.insert(file_name, task); 88 | } 89 | 90 | true 91 | } 92 | Msg::ClearFiles => { 93 | self.files.clear(); 94 | 95 | true 96 | } 97 | } 98 | } 99 | 100 | fn view(&self, ctx: &Context) -> Html { 101 | html! { 102 |
103 |
104 | 105 | 106 | 107 | {if self.loading { 108 | html! { 109 | 110 | } 111 | } else { 112 | html! { 113 |
114 | 118 | 119 | {for self.files.iter().map(|file| html! { 120 | 124 | })} 125 |
126 | } 127 | }} 128 | 129 |
130 |
131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /wasm/src/components/alert.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | #[derive(PartialEq)] 4 | pub enum AlertStyle { 5 | Success, 6 | Danger, 7 | Warning, 8 | Info, 9 | } 10 | 11 | impl AlertStyle { 12 | #[inline] 13 | pub fn class_name(&self) -> &str { 14 | match self { 15 | AlertStyle::Success => "alert-success", 16 | AlertStyle::Danger => "alert-danger", 17 | AlertStyle::Warning => "alert-warning", 18 | AlertStyle::Info => "alert-info", 19 | } 20 | } 21 | } 22 | 23 | #[derive(Properties, PartialEq)] 24 | pub struct AlertProps { 25 | pub style: AlertStyle, 26 | pub message: String, 27 | } 28 | 29 | #[function_component(Alert)] 30 | pub fn alert(props: &AlertProps) -> Html { 31 | html! { 32 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /wasm/src/components/clear_files_button.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | #[derive(Properties, PartialEq)] 4 | pub struct ClearFilesButtonProps { 5 | pub visible: bool, 6 | pub onclick: Callback<()>, 7 | } 8 | 9 | #[function_component(ClearFilesButton)] 10 | pub fn clear_files_button(props: &ClearFilesButtonProps) -> Html { 11 | if props.visible { 12 | html! { 13 |

14 | 19 | {"Clear uploaded files."} 20 | 21 |

22 | } 23 | } else { 24 | html! {} 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /wasm/src/components/file_accordion.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | use crate::app::FileDetails; 4 | 5 | #[derive(Properties, PartialEq)] 6 | pub struct FileAccordionProps { 7 | pub file: FileDetails, 8 | pub children: Children, 9 | } 10 | 11 | #[function_component(FileAccordion)] 12 | pub fn file_accordion(props: &FileAccordionProps) -> Html { 13 | let file_name = props.file.name.clone(); 14 | 15 | html! { 16 |
17 |
18 |

19 | 27 |

28 | 29 |
33 |
34 | { for props.children.iter() } 35 |
36 |
37 |
38 |
39 | } 40 | } 41 | -------------------------------------------------------------------------------- /wasm/src/components/file_analysis.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Debug; 2 | use std::rc::Rc; 3 | 4 | use cs2_analyzer::Analyzer; 5 | 6 | use yew::prelude::*; 7 | 8 | use crate::app::FileDetails; 9 | use crate::components::{Alert, AlertStyle, FileAccordion, Tab, TabPane}; 10 | 11 | #[derive(Properties, PartialEq)] 12 | pub struct FileAnalysisProps { 13 | pub file: FileDetails, 14 | pub analyzer: Rc, 15 | } 16 | 17 | #[function_component(FileAnalysis)] 18 | pub fn file_analysis(props: &FileAnalysisProps) -> Html { 19 | let file_name = props.file.name.clone(); 20 | 21 | match props.analyzer.analyze_from_bytes(&props.file.data) { 22 | Ok(result) => { 23 | let tabs: [(&str, &dyn Debug); 7] = [ 24 | ("Buttons", &result.buttons), 25 | ("ConCommands", &result.concommands), 26 | ("ConVars", &result.convars), 27 | ("Interfaces", &result.interfaces), 28 | ("Offsets", &result.offsets), 29 | ("Classes", &result.classes), 30 | ("Enums", &result.enums), 31 | ]; 32 | 33 | html! { 34 | 35 | 42 | 43 |
44 | {tabs.iter().enumerate().map(|(i, (name, items))| { 45 | html! { 46 | 51 |
52 |                                         {format!("{:#X?}", items)}
53 |                                     
54 |
55 | } 56 | }).collect::()} 57 |
58 |
59 | } 60 | } 61 | Err(_) => html! { 62 | 66 | }, 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /wasm/src/components/file_upload_zone.rs: -------------------------------------------------------------------------------- 1 | use gloo::file::File; 2 | 3 | use js_sys::wasm_bindgen::JsCast; 4 | 5 | use web_sys::{DragEvent, Event, FileList, HtmlInputElement}; 6 | 7 | use yew::prelude::*; 8 | 9 | #[derive(Properties, PartialEq)] 10 | pub struct FileUploadZoneProps { 11 | pub on_files_uploaded: Callback>, 12 | } 13 | 14 | #[function_component(FileUploadZone)] 15 | pub fn file_upload_zone(props: &FileUploadZoneProps) -> Html { 16 | let on_files_uploaded = props.on_files_uploaded.clone(); 17 | 18 | let upload_files = move |files: Option| { 19 | let mut result = Vec::new(); 20 | 21 | if let Some(files) = files { 22 | let files = js_sys::try_iter(&files) 23 | .unwrap() 24 | .unwrap() 25 | .map(|v| web_sys::File::from(v.unwrap())) 26 | .filter(|f| f.name().ends_with(".dll")) 27 | .map(File::from); 28 | 29 | result.extend(files); 30 | } 31 | 32 | on_files_uploaded.emit(result); 33 | }; 34 | 35 | html! { 36 |
37 |

{"File upload"}

38 | 39 |
71 |

72 | {"Drag files here to upload, or click to select (e.g. client.dll, engine2.dll)."} 73 |

74 | 75 |

76 | 77 | {"Upload files"} 78 | 79 |

80 | 81 | 97 |
98 |
99 | } 100 | } 101 | -------------------------------------------------------------------------------- /wasm/src/components/footer.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | #[function_component(Footer)] 4 | pub fn footer() -> Html { 5 | html! { 6 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /wasm/src/components/header.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | #[function_component(Header)] 4 | pub fn header() -> Html { 5 | html! { 6 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /wasm/src/components/loading_indicator.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | #[function_component(LoadingIndicator)] 4 | pub fn loading_indicator() -> Html { 5 | html! { 6 |
7 |

{"File(s) being analyzed, please wait..."}

8 | 9 |
10 | {"Loading..."} 11 |
12 |
13 | } 14 | } 15 | -------------------------------------------------------------------------------- /wasm/src/components/mod.rs: -------------------------------------------------------------------------------- 1 | pub use alert::*; 2 | pub use clear_files_button::*; 3 | pub use file_accordion::*; 4 | pub use file_analysis::*; 5 | pub use file_upload_zone::*; 6 | pub use footer::*; 7 | pub use header::*; 8 | pub use loading_indicator::*; 9 | pub use tab::*; 10 | pub use tab_pane::*; 11 | 12 | pub mod alert; 13 | pub mod clear_files_button; 14 | pub mod file_accordion; 15 | pub mod file_analysis; 16 | pub mod file_upload_zone; 17 | pub mod footer; 18 | pub mod header; 19 | pub mod loading_indicator; 20 | pub mod tab; 21 | pub mod tab_pane; 22 | -------------------------------------------------------------------------------- /wasm/src/components/tab.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | #[derive(Properties, PartialEq)] 4 | pub struct TabProps { 5 | pub file_name: String, 6 | pub tab: String, 7 | pub is_active: bool, 8 | } 9 | 10 | #[function_component(Tab)] 11 | pub fn tab(props: &TabProps) -> Html { 12 | html! { 13 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /wasm/src/components/tab_pane.rs: -------------------------------------------------------------------------------- 1 | use yew::prelude::*; 2 | 3 | #[derive(Properties, PartialEq)] 4 | pub struct TabPaneProps { 5 | pub file_name: String, 6 | pub name: String, 7 | pub is_active: bool, 8 | pub children: Children, 9 | } 10 | 11 | #[function_component(TabPane)] 12 | pub fn tab_pane(props: &TabPaneProps) -> Html { 13 | html! { 14 |
19 |
20 |
21 | { for props.children.iter() } 22 |
23 |
24 |
25 | } 26 | } 27 | -------------------------------------------------------------------------------- /wasm/src/main.rs: -------------------------------------------------------------------------------- 1 | use app::App; 2 | 3 | mod app; 4 | mod components; 5 | 6 | fn main() { 7 | yew::Renderer::::new().render(); 8 | } 9 | --------------------------------------------------------------------------------