From 0774a5a571def662c746be2f4f6f8ff319278335 Mon Sep 17 00:00:00 2001 From: Shachar <110262023+shacharrr@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:38:17 +0300 Subject: [PATCH] Add files via upload --- Cargo.lock | 107 +++++-------------- Cargo.toml | 3 +- src/main.rs | 300 ++++++++++++++++++++++++++++++---------------------- 3 files changed, 206 insertions(+), 204 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d5d442d..66b3840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -500,7 +500,8 @@ dependencies = [ "eframe", "egui", "mki", - "rust-ini", + "serde", + "toml", ] [[package]] @@ -570,28 +571,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "const-random" -version = "0.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368a7a772ead6ce7e1de82bfb04c485f3db8ec744f72925af5735e29a22cc18e" -dependencies = [ - "const-random-macro", - "proc-macro-hack", -] - -[[package]] -name = "const-random-macro" -version = "0.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d7d6ab3c3a2282db210df5f02c4dab6e0a7057af0fb7ebd4070f30fe05c0ddb" -dependencies = [ - "getrandom", - "once_cell", - "proc-macro-hack", - "tiny-keccak", -] - [[package]] name = "core-foundation" version = "0.9.3" @@ -659,12 +638,6 @@ dependencies = [ "cfg-if 1.0.0", ] -[[package]] -name = "crunchy" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" - [[package]] name = "crypto-common" version = "0.1.6" @@ -717,15 +690,6 @@ dependencies = [ "libloading 0.8.0", ] -[[package]] -name = "dlv-list" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d529fd73d344663edfd598ccb3f344e46034db51ebd103518eae34338248ad73" -dependencies = [ - "const-random", -] - [[package]] name = "downcast-rs" version = "1.2.0" @@ -1147,12 +1111,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -[[package]] -name = "hashbrown" -version = "0.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" - [[package]] name = "hashbrown" version = "0.14.0" @@ -1726,16 +1684,6 @@ dependencies = [ "redox_syscall", ] -[[package]] -name = "ordered-multimap" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ed8acf08e98e744e5384c8bc63ceb0364e68a6854187221c18df61c4797690e" -dependencies = [ - "dlv-list", - "hashbrown 0.13.2", -] - [[package]] name = "ordered-stream" version = "0.2.0" @@ -1859,12 +1807,6 @@ dependencies = [ "toml_edit", ] -[[package]] -name = "proc-macro-hack" -version = "0.5.20+deprecated" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" - [[package]] name = "proc-macro2" version = "1.0.65" @@ -1957,16 +1899,6 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" -[[package]] -name = "rust-ini" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e2a3bcec1f113553ef1c88aae6c020a369d03d55b58de9869a0908930385091" -dependencies = [ - "cfg-if 1.0.0", - "ordered-multimap", -] - [[package]] name = "rustix" version = "0.37.23" @@ -2052,6 +1984,15 @@ dependencies = [ "syn 2.0.26", ] +[[package]] +name = "serde_spanned" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +dependencies = [ + "serde", +] + [[package]] name = "serde_yaml" version = "0.8.26" @@ -2237,15 +2178,6 @@ dependencies = [ "syn 2.0.26", ] -[[package]] -name = "tiny-keccak" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" -dependencies = [ - "crunchy", -] - [[package]] name = "tiny-skia" version = "0.8.4" @@ -2286,11 +2218,26 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "toml" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + [[package]] name = "toml_datetime" version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -2299,6 +2246,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" dependencies = [ "indexmap 2.0.0", + "serde", + "serde_spanned", "toml_datetime", "winnow", ] diff --git a/Cargo.toml b/Cargo.toml index e6e7a6d..3376de4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" eframe = "0.22.0" egui = "0.22.0" mki = "0.2.3" -rust-ini = "0.19.0" +serde = "1.0.171" +toml = "0.7.6" diff --git a/src/main.rs b/src/main.rs index c71cbda..2b5b88a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,84 +2,145 @@ use mki::Keyboard; -use std::thread; +use std::{thread, io::Write}; use std::time::Duration; +use serde::{Serialize, Deserialize}; +use std::fs; +use std::collections::HashMap; -use ini::Ini; use eframe::egui; use egui::{menu, Visuals}; -use std::collections::HashMap; -const CFG_PATH: &str = "./cfg/cjump.ini"; -static mut DARK: bool = true; +const CFG_PATH: &str = "./cfg/cjump.toml"; // Shity way should improve static mut FLOOP: bool = true; - -static mut JUMP_BIND: Keyboard = Keyboard::Space; -static mut PAUSE_BIND: Keyboard = Keyboard::Enter; static mut PAUSED: bool = false; -static mut DELAY: u64 = 850; -fn main() -> Result<(), eframe::Error> { - match std::fs::metadata(CFG_PATH) { - Ok(_) => { - let mut cfg = Ini::load_from_file(CFG_PATH).unwrap(); - let mut s = cfg.with_section(Some("style")); - let d = s.get("dark").unwrap(); - if d == "0"{ - unsafe { DARK = false }; +static mut TOML_CONVAR: TomlConVar = TomlConVar::new(); +static mut CONVAR: ConVar = ConVar::new(); + +#[derive(Deserialize, Serialize)] +struct TomlConVar { + dark_mode: String, + config: TomlConfig, +} + +impl TomlConVar { + const fn new() -> TomlConVar { + TomlConVar { + dark_mode: String::new(), + config: TomlConfig { + jump_bind: String::new(), + duck_bind: String::new(), + pause_bind: String::new(), + delay: String::new(), } + } + } - let jump_vk = create_jump_hash(); - let mut s = cfg.with_section(Some("binds")); - let d = s.get("jump").unwrap(); - unsafe { - JUMP_BIND = jump_vk[d]; - }; + fn convert_to_convar(&mut self) -> ConVar { + ConVar { + dark_mode: self.dark_mode.parse().unwrap(), + config: Config { + jump_bind: self.config.jump_bind.clone(), + duck_bind: self.config.duck_bind.clone(), + pause_bind: self.config.pause_bind.clone(), + delay: self.config.delay.parse().unwrap(), + } + } + } +} + +#[derive(Deserialize, Serialize)] +struct TomlConfig { + jump_bind: String, + duck_bind: String, + pause_bind: String, + delay: String, +} + +struct ConVar { + dark_mode: u8, + config: Config, +} + +impl ConVar { + const fn new() -> ConVar { + ConVar { + dark_mode: 1, + config: Config { + jump_bind: String::new(), + duck_bind: String::new(), + pause_bind: String::new(), + delay: 850, + } + } + } + + fn convert_to_toml(&mut self) -> TomlConVar { + TomlConVar { + dark_mode: self.dark_mode.to_string(), + config: TomlConfig { + jump_bind: self.config.jump_bind.clone(), + duck_bind: self.config.duck_bind.clone(), + pause_bind: self.config.pause_bind.clone(), + delay: self.config.delay.to_string(), + } + } + } +} + +struct Config { + jump_bind: String, + duck_bind: String, + pause_bind: String, + delay: u64, +} - let pause_vk = create_pause_hash(); - let mut s = cfg.with_section(Some("binds")); - let d = s.get("pause").unwrap(); - unsafe { - PAUSE_BIND = pause_vk[d]; - }; - let mut s = cfg.with_section(Some("binds")); - let d = s.get("delay").unwrap(); +fn main() -> Result<(), eframe::Error> { + match std::fs::metadata(CFG_PATH) { + Ok(_) => { + let contents = fs::read_to_string(CFG_PATH) + .expect("Should have been able to read the file"); + unsafe { - DELAY = d.parse().unwrap(); - }; + TOML_CONVAR = toml::from_str(contents.as_str()).unwrap(); + CONVAR = TOML_CONVAR.convert_to_convar(); + } }, Err(_) => { let path = std::path::Path::new(CFG_PATH); let prefix = path.parent().unwrap(); std::fs::create_dir_all(prefix).unwrap(); - std::fs::File::create(path).unwrap(); + let mut f = std::fs::File::create(path).unwrap(); - // Ini init - let mut cfg = Ini::new(); - cfg.with_section(Some("style")) - .set("dark", "1"); - cfg.with_section(Some("binds")) - .set("jump", "Space"); - cfg.with_section(Some("binds")) - .set("pause", "Enter"); - cfg.with_section(Some("binds")) - .set("delay", "850"); + let default_toml = r#"dark_mode = '1' - cfg.write_to_file(CFG_PATH).unwrap(); +[config] +jump_bind = 'Space' +duck_bind = 'Left Control' +pause_bind = 'Enter' +delay = '850' + "#; + let _ = f.write_all(default_toml.as_bytes()); + unsafe { - DARK = true; + TOML_CONVAR = toml::from_str(default_toml).unwrap(); + CONVAR = TOML_CONVAR.convert_to_convar(); }; }, } thread::spawn(|| { + let jump_vk = create_jump_hash(); + let duck_vk = create_duck_hash(); + let pause_vk = create_pause_hash(); loop { let mut spfloop = true; - while unsafe { PAUSE_BIND.is_pressed() } { + while unsafe { pause_vk[&CONVAR.config.pause_bind].is_pressed() } { if spfloop { unsafe { PAUSED = !PAUSED }; spfloop = false; @@ -87,12 +148,12 @@ fn main() -> Result<(), eframe::Error> { } let mut sjfloop = true; - while unsafe { JUMP_BIND.is_pressed() && !PAUSED} { + while unsafe { jump_vk[&CONVAR.config.jump_bind].is_pressed() && !PAUSED} { if sjfloop { sjfloop = false; - Keyboard::LeftControl.press(); - unsafe { thread::sleep(Duration::from_millis(DELAY)) } - Keyboard::LeftControl.release(); + unsafe { duck_vk[&CONVAR.config.duck_bind].press(); } + unsafe { thread::sleep(Duration::from_millis(CONVAR.config.delay)) } + unsafe { duck_vk[&CONVAR.config.duck_bind].release(); } } } } @@ -109,21 +170,12 @@ fn main() -> Result<(), eframe::Error> { ) } -fn find_key_for_value<'a>(map: &'a HashMap, value: Keyboard) -> Option<&'a String> { - map.iter() - .find_map(|(key, &val)| if val == value { Some(key) } else { None }) -} - fn create_jump_hash() -> HashMap { let mut jump_vk = HashMap::new(); jump_vk.insert( "Space".to_string(), Keyboard::Space, ); - jump_vk.insert( - "C".to_string(), - Keyboard::C, - ); jump_vk.insert( "V".to_string(), Keyboard::V, @@ -139,6 +191,19 @@ fn create_jump_hash() -> HashMap { jump_vk } +fn create_duck_hash() -> HashMap { + let mut jump_vk = HashMap::new(); + jump_vk.insert( + "Left Control".to_string(), + Keyboard::LeftControl, + ); + jump_vk.insert( + "C".to_string(), + Keyboard::C, + ); + jump_vk +} + fn create_pause_hash() -> HashMap { let mut pause_vk = HashMap::new(); pause_vk.insert( @@ -210,28 +275,13 @@ fn create_pause_hash() -> HashMap { struct MyApp { - dark: bool, - jump_vk: HashMap, - jump_select: String, - pause_vk: HashMap, - pause_select: String, - delay: u64 + dark: u8, } - impl Default for MyApp { fn default() -> Self { - let jump_vk = create_jump_hash(); - let pause_vk = create_pause_hash(); - let jc = jump_vk.clone(); - let cp = pause_vk.clone(); Self { - dark: unsafe { DARK }, - jump_vk: jump_vk, - jump_select: find_key_for_value(&jc, unsafe { JUMP_BIND }).unwrap().to_string(), - pause_vk: pause_vk, - pause_select: find_key_for_value(&cp, unsafe { PAUSE_BIND }).unwrap().to_string(), - delay: unsafe { DELAY }, + dark: unsafe { CONVAR.dark_mode }, } } } @@ -241,27 +291,29 @@ impl eframe::App for MyApp { egui::CentralPanel::default().show(ctx, |ui| { // Light/Dark Mode - if self.dark != unsafe{DARK} || unsafe{FLOOP} { - let mut cfg = Ini::load_from_file(CFG_PATH).unwrap(); - cfg.with_section(Some("style")) - .set("dark", (self.dark as u8).to_string().as_str()); - cfg.write_to_file(CFG_PATH).unwrap(); - + if self.dark != unsafe{ CONVAR.dark_mode } || unsafe{FLOOP} { unsafe { - DARK = self.dark; + CONVAR.dark_mode = self.dark; FLOOP = false; }; match self.dark { - false => ctx.set_visuals(Visuals::light()), - true => ctx.set_visuals(Visuals::dark()), + 1 => ctx.set_visuals(Visuals::light()), + 0 => ctx.set_visuals(Visuals::dark()), + _ => todo!(), } } // Menu bar menu::bar(ui, |ui| { ui.menu_button("Visuals", |ui| { - ui.checkbox(&mut self.dark, "Dark mode"); - }) + if ui.button("Dark mode").clicked() { + match self.dark { + 1 => self.dark = 0, + 0 => self.dark = 1, + _ => todo!() + } + } + }); }); ui.horizontal(|ui| { @@ -276,35 +328,42 @@ impl eframe::App for MyApp { ui.separator(); egui::ComboBox::from_label("Jump bind") - .selected_text(format!("{:?}", self.jump_select)) + .selected_text(format!("{:?}", unsafe { &CONVAR.config.jump_bind })) + .show_ui(ui, |ui| { + ui.selectable_value(unsafe{ &mut CONVAR.config.jump_bind }, String::from("Space"), "Space"); + ui.selectable_value(unsafe{ &mut CONVAR.config.jump_bind }, String::from("V"), "V"); + ui.selectable_value(unsafe{ &mut CONVAR.config.jump_bind }, String::from("B"), "B"); + ui.selectable_value(unsafe{ &mut CONVAR.config.jump_bind }, String::from("N"), "N"); + } + ); + + egui::ComboBox::from_label("Duck bind") + .selected_text(format!("{:?}", unsafe { &CONVAR.config.duck_bind })) .show_ui(ui, |ui| { - ui.selectable_value(&mut self.jump_select, String::from("Space"), "Space"); - ui.selectable_value(&mut self.jump_select, String::from("C"), "C"); - ui.selectable_value(&mut self.jump_select, String::from("V"), "V"); - ui.selectable_value(&mut self.jump_select, String::from("B"), "B"); - ui.selectable_value(&mut self.jump_select, String::from("N"), "N"); + ui.selectable_value(unsafe{ &mut CONVAR.config.duck_bind }, String::from("Left Control"), "L-CTRL"); + ui.selectable_value(unsafe{ &mut CONVAR.config.duck_bind }, String::from("C"), "C"); } ); egui::ComboBox::from_label("Pause bind") - .selected_text(format!("{:?}", self.pause_select)) + .selected_text(format!("{:?}", unsafe{ &CONVAR.config.pause_bind })) .show_ui(ui, |ui| { - ui.selectable_value(&mut self.pause_select, String::from("Enter"), "Enter"); - ui.selectable_value(&mut self.pause_select, String::from("Tab"), "Tab"); - ui.selectable_value(&mut self.pause_select, String::from("Backspace"), "Backspace"); - ui.selectable_value(&mut self.pause_select, String::from("Left Alt"), "Left Alt"); - ui.selectable_value(&mut self.pause_select, String::from("F1"), "F1"); - ui.selectable_value(&mut self.pause_select, String::from("F2"), "F2"); - ui.selectable_value(&mut self.pause_select, String::from("F3"), "F3"); - ui.selectable_value(&mut self.pause_select, String::from("F4"), "F4"); - ui.selectable_value(&mut self.pause_select, String::from("F5"), "F5"); - ui.selectable_value(&mut self.pause_select, String::from("F6"), "F6"); - ui.selectable_value(&mut self.pause_select, String::from("F7"), "F7"); - ui.selectable_value(&mut self.pause_select, String::from("F8"), "F8"); - ui.selectable_value(&mut self.pause_select, String::from("F9"), "F9"); - ui.selectable_value(&mut self.pause_select, String::from("F10"), "F10"); - ui.selectable_value(&mut self.pause_select, String::from("F11"), "F11"); - ui.selectable_value(&mut self.pause_select, String::from("F12"), "F12"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("Enter"), "Enter"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("Tab"), "Tab"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("Backspace"), "Backspace"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("Left Alt"), "L-ALT"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F1"), "F1"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F2"), "F2"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F3"), "F3"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F4"), "F4"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F5"), "F5"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F6"), "F6"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F7"), "F7"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F8"), "F8"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F9"), "F9"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F10"), "F10"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F11"), "F11"); + ui.selectable_value(unsafe{ &mut CONVAR.config.pause_bind }, String::from("F12"), "F12"); } ); @@ -312,24 +371,17 @@ impl eframe::App for MyApp { ui.horizontal(|ui| { ui.label("Delay (ms)"); - ui.add(egui::DragValue::new(&mut self.delay).speed(1)); + ui.add(egui::DragValue::new(unsafe { &mut CONVAR.config.delay }).speed(1)); }); if ui.button("Update settings").clicked() { - let mut cfg = Ini::load_from_file(CFG_PATH).unwrap(); - cfg.with_section(Some("binds")) - .set("jump", self.jump_select.as_str()); - cfg.with_section(Some("binds")) - .set("pause", self.pause_select.as_str()); - cfg.with_section(Some("binds")) - .set("delay", self.delay.to_string()); - - cfg.write_to_file(CFG_PATH).unwrap(); + let path = std::path::Path::new(CFG_PATH); + let mut f = std::fs::File::create(path).unwrap(); unsafe { - JUMP_BIND = self.jump_vk[self.jump_select.as_str()]; - PAUSE_BIND = self.pause_vk[self.pause_select.as_str()]; - DELAY = self.delay; + TOML_CONVAR = CONVAR.convert_to_toml(); + let content = toml::to_string(&TOML_CONVAR).unwrap(); + let _ = f.write_all(content.as_bytes()); }; } });