├── .gitignore ├── .gitattributes ├── rust-toolchain.toml ├── gm_microoptimisation_war_crime.dll.vdf ├── gm_microoptimisation_war_crime.so.vdf ├── .editorconfig ├── src ├── cxx │ ├── mod.rs │ ├── plugin.cpp │ ├── plugin.hpp │ └── include │ │ ├── iserverplugin.h │ │ └── iluainterface.h ├── localization.rs ├── lib.rs ├── strip.rs ├── realms.rs ├── enums.rs ├── hax.rs └── tests.rs ├── Cargo.toml ├── README.md └── Cargo.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.h linguist-vendored -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" -------------------------------------------------------------------------------- /gm_microoptimisation_war_crime.dll.vdf: -------------------------------------------------------------------------------- 1 | Plugin 2 | { 3 | file "lua/bin/gm_microoptimisation_war_crime.dll" 4 | } -------------------------------------------------------------------------------- /gm_microoptimisation_war_crime.so.vdf: -------------------------------------------------------------------------------- 1 | Plugin 2 | { 3 | file "lua/bin/gm_microoptimisation_war_crime.so" 4 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | indent_size = 4 6 | charset = utf-8 7 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /src/cxx/mod.rs: -------------------------------------------------------------------------------- 1 | #[cxx::bridge] pub mod bridge { 2 | extern "C++" { 3 | include!("plugin.hpp"); 4 | unsafe fn is_client(lua: usize) -> bool; 5 | unsafe fn is_server(lua: usize) -> bool; 6 | unsafe fn CreateInterface() -> usize; 7 | } 8 | } -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gm_microoptimisation_war_crime" 3 | version = "2.0.0" 4 | edition = "2021" 5 | 6 | [lib] 7 | crate-type = ["cdylib", "rlib"] 8 | 9 | [profile.release] 10 | panic = "abort" 11 | 12 | [dependencies] 13 | magic_static = "3.0.1" 14 | pcre2 = "0.2.3" 15 | cxx = "1.0.63" 16 | gmod = "14.0.2" 17 | gmserverplugin = "2.0.1" 18 | 19 | [build-dependencies] 20 | cxx-build = "1.0.63" 21 | -------------------------------------------------------------------------------- /src/cxx/plugin.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | WarcrimePlugin::WarcrimePlugin() {} 6 | 7 | static WarcrimePlugin* INSTANCE = new WarcrimePlugin(); 8 | uintptr_t CreateInterface() { 9 | return (uintptr_t) INSTANCE; 10 | } 11 | 12 | bool is_server(uintptr_t lua_ptr) { 13 | ILuaInterface* lua = (ILuaInterface*)lua_ptr; 14 | return lua->IsServer(); 15 | } 16 | 17 | bool is_client(uintptr_t lua_ptr) { 18 | ILuaInterface* lua = (ILuaInterface*)lua_ptr; 19 | return lua->IsClient(); 20 | } -------------------------------------------------------------------------------- /src/localization.rs: -------------------------------------------------------------------------------- 1 | magic_statics_mod! { 2 | static ref RE_LOCAL_WORD_BOUNDARY: crate::Regex = crate::Regex::new(r#"\bl"#).unwrap(); 3 | } 4 | 5 | pub fn is_localization(src: &[u8], start: usize) -> bool { 6 | if start == 0 { 7 | return false; 8 | } 9 | let mut cursor = start - 1; 10 | while cursor > 0 && src[cursor] == ' ' as u8 { 11 | cursor -= 1; 12 | } 13 | if cursor < b"local".len() - 1 { 14 | return false; 15 | } 16 | if &src[cursor - (b"local".len() - 1)..=cursor] != b"local" { 17 | return false; 18 | } 19 | if cursor <= 4 { 20 | return true; 21 | } 22 | RE_LOCAL_WORD_BOUNDARY.is_match(&src[cursor - (b"local".len() - 1) - 1..][..2]).unwrap() 23 | } 24 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(c_unwind)] 2 | 3 | #[macro_use] extern crate gmod; 4 | #[macro_use] extern crate magic_static; 5 | 6 | mod localization; 7 | mod realms; 8 | mod enums; 9 | mod strip; 10 | mod hax; 11 | mod cxx; 12 | 13 | pub(crate) type Regex = pcre2::bytes::Regex; 14 | 15 | #[magic_static::main( 16 | mod strip, 17 | mod realms, 18 | mod enums, 19 | mod localization 20 | )] 21 | pub fn init() {} 22 | 23 | #[cfg(any(debug_assertions, test))] 24 | mod tests; 25 | 26 | #[no_mangle] 27 | pub unsafe extern "C" fn CreateInterface() -> *mut std::ffi::c_void { 28 | extern "C" fn after_init(lua: *mut std::ffi::c_void, _: *mut std::ffi::c_void) { 29 | unsafe { 30 | enums::clean_up_global_table(gmod::lua::State(lua)); 31 | } 32 | } 33 | 34 | extern "C" fn before_init(_: *mut std::ffi::c_void, lua: *mut std::ffi::c_void) { 35 | unsafe { 36 | realms::detect(lua); 37 | } 38 | } 39 | 40 | extern "C" fn newstate(lua: *mut std::ffi::c_void, _: *mut std::ffi::c_void) { 41 | unsafe { 42 | gmod::set_lua_state(lua); 43 | init(); 44 | hax::init(); 45 | } 46 | } 47 | 48 | gmserverplugin::init(); 49 | gmserverplugin::newstate(newstate); 50 | gmserverplugin::before_init(before_init); 51 | gmserverplugin::after_init(after_init); 52 | 53 | cxx::bridge::CreateInterface() as *mut _ 54 | } -------------------------------------------------------------------------------- /src/cxx/plugin.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "rust/cxx.h" 4 | #include "gm_microoptimisation_war_crime/src/cxx/mod.rs.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class WarcrimePlugin : public IServerPluginCallbacks 12 | { 13 | public: 14 | WarcrimePlugin(); 15 | ~WarcrimePlugin(); 16 | 17 | virtual bool Load(CreateInterfaceFn, CreateInterfaceFn) { return true; }; 18 | virtual void Unload(void) {}; 19 | virtual void Pause(void) {}; 20 | virtual void UnPause(void) {}; 21 | virtual const char *GetPluginDescription(void) { return "warcrime"; }; 22 | virtual void LevelInit(char const *) {}; 23 | virtual void ServerActivate(void *, int, int) {}; 24 | virtual void GameFrame(bool) {}; 25 | virtual void LevelShutdown(void) {}; 26 | virtual void ClientActive(void *) {}; 27 | virtual void ClientDisconnect(void *) {}; 28 | virtual void ClientPutInServer(void *, char const *) {}; 29 | virtual void SetCommandClient(int ) {}; 30 | virtual void ClientSettingsChanged(void *) {}; 31 | virtual PLUGIN_RESULT ClientConnect(bool *, void *, const char *, const char *, char *, int ) { return PLUGIN_CONTINUE; }; 32 | virtual PLUGIN_RESULT ClientCommand(void *, const CCommand &) { return PLUGIN_CONTINUE; }; 33 | virtual PLUGIN_RESULT NetworkIDValidated(const char *, const char *) { return PLUGIN_CONTINUE; }; 34 | virtual void OnQueryCvarValueFinished(QueryCvarCookie_t , void *, EQueryCvarValueStatus , const char *, const char *) {}; 35 | virtual void OnEdictAllocated(void *) {}; 36 | virtual void OnEdictFreed(const void *) {}; 37 | }; 38 | 39 | uintptr_t CreateInterface(); 40 | 41 | bool is_server(uintptr_t lua_ptr); 42 | bool is_client(uintptr_t lua_ptr); -------------------------------------------------------------------------------- /src/strip.rs: -------------------------------------------------------------------------------- 1 | //! Generate a data structure containing ranges of comments and strings for ignoring during optimisation passes. 2 | 3 | use std::ops::Range; 4 | 5 | #[derive(Default)] 6 | pub(crate) struct StripTreeBuilder(Vec>); 7 | impl StripTreeBuilder { 8 | #[inline] 9 | pub(crate) fn insert(&mut self, range: Range) { 10 | self.0.push(range); 11 | } 12 | 13 | #[inline] 14 | pub(crate) fn build(self) -> StripTree { 15 | StripTree(self.0) 16 | } 17 | } 18 | 19 | pub(crate) struct StripTree(Vec>); 20 | impl StripTree { 21 | pub(crate) fn contains(&self, other: Range) -> bool { 22 | for range in &self.0 { 23 | if other.start >= range.start && other.end <= range.end { 24 | return true; 25 | } 26 | } 27 | false 28 | } 29 | } 30 | 31 | macro_rules! strips { 32 | { $($name:ident => $re:literal),* } => { 33 | #[doc(hidden)] 34 | mod regex { 35 | #![allow(non_upper_case_globals)] 36 | magic_statics_mod! { 37 | $(pub(super) static ref $name: crate::Regex = crate::Regex::new($re).unwrap();)* 38 | } 39 | } 40 | 41 | $( 42 | #[doc(hidden)] 43 | fn $name(tree: &mut StripTreeBuilder, src: &[u8]) { 44 | for m in self::regex::$name.find_iter(src).map(|m| m.unwrap()) { 45 | tree.insert((m.start()..m.end()).into()); 46 | } 47 | } 48 | )* 49 | 50 | pub(crate) fn generate(src: &[u8]) -> StripTree { 51 | let mut strip_tree = StripTreeBuilder::default(); 52 | $( 53 | $name(&mut strip_tree, src); 54 | )* 55 | strip_tree.build() 56 | } 57 | 58 | #[inline] 59 | pub fn magic_static() { 60 | regex::magic_static() 61 | } 62 | }; 63 | } 64 | 65 | strips! { 66 | comments => r#"--(?:\[(=*)\[[\S\s]+?\]\1\]|[\S\s]+?(?:\n|(?m:$)))"#, 67 | strings => r#"(?:("|')((?:\\\1|\\\\|.)*?)\1)|(?