|
| 1 | +#![no_std] |
| 2 | + |
| 3 | +use asr::{ |
| 4 | + future::{next_tick, retry}, |
| 5 | + signature::Signature, |
| 6 | + time::Duration, |
| 7 | + timer::{self}, |
| 8 | + watcher::Watcher, |
| 9 | + Process, |
| 10 | +}; |
| 11 | + |
| 12 | +const SAR_TIMER_SIGNATURE: Signature<42> = Signature::new("53 41 52 5F 54 49 4D 45 52 5F 53 54 41 52 54 00 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 53 41 52 5F 54 49 4D 45 52 5F 45 4E 44 00"); |
| 13 | +const SAR_TIMER_OFFSET: u64 = 16; |
| 14 | + |
| 15 | +const _TIMER_ACTION_NONE: i32 = 0; |
| 16 | +const TIMER_ACTION_START: i32 = 1; |
| 17 | +const TIMER_ACTION_RESTART: i32 = 2; |
| 18 | +const TIMER_ACTION_SPLIT: i32 = 3; |
| 19 | +const TIMER_ACTION_END: i32 = 4; |
| 20 | +const TIMER_ACTION_RESET: i32 = 5; |
| 21 | + |
| 22 | +asr::panic_handler!(); |
| 23 | + |
| 24 | +#[cfg(not(feature = "nightly"))] |
| 25 | +asr::async_main!(stable); |
| 26 | +#[cfg(feature = "nightly")] |
| 27 | +asr::async_main!(nightly); |
| 28 | + |
| 29 | +async fn main() { |
| 30 | + loop { |
| 31 | + asr::set_tick_rate(1.0); |
| 32 | + |
| 33 | + timer::pause_game_time(); |
| 34 | + |
| 35 | + let process = |
| 36 | + retry(|| Process::attach("portal2.exe").or_else(|| Process::attach("portal2_linux"))) |
| 37 | + .await; |
| 38 | + |
| 39 | + process |
| 40 | + .until_closes(async { |
| 41 | + let mut interface_address: Option<asr::Address>; |
| 42 | + |
| 43 | + loop { |
| 44 | + if process.get_module_address("sar.dll").is_ok() |
| 45 | + || process.get_module_address("sar.so").is_ok() |
| 46 | + { |
| 47 | + interface_address = process |
| 48 | + .memory_ranges() |
| 49 | + .filter(|m| { |
| 50 | + m.flags() |
| 51 | + .unwrap_or_default() |
| 52 | + .contains(asr::MemoryRangeFlags::WRITE) |
| 53 | + }) |
| 54 | + .find_map(|m| { |
| 55 | + m.range().map_or(None, |range| { |
| 56 | + SAR_TIMER_SIGNATURE.scan_process_range(&process, range) |
| 57 | + }) |
| 58 | + }) |
| 59 | + .and_then(|x| Some(x.add(SAR_TIMER_OFFSET))); |
| 60 | + |
| 61 | + if interface_address.is_some() { |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + next_tick().await; |
| 67 | + } |
| 68 | + |
| 69 | + let total_address = interface_address.unwrap(); |
| 70 | + let ipt_address = total_address.clone().add(4); |
| 71 | + let action_address = total_address.clone().add(8); |
| 72 | + |
| 73 | + asr::print_limited::<40>(&format_args!( |
| 74 | + "Found SAR + pubInterface at 0x{:X}", |
| 75 | + total_address.value() |
| 76 | + )); |
| 77 | + |
| 78 | + let mut total = Watcher::<i32>::new(); |
| 79 | + let mut ipt = Watcher::<f32>::new(); |
| 80 | + let mut action = Watcher::<i32>::new(); |
| 81 | + |
| 82 | + asr::set_tick_rate(120.0); |
| 83 | + |
| 84 | + loop { |
| 85 | + let Some(total) = total.update(process.read(total_address).ok()) else { |
| 86 | + next_tick().await; |
| 87 | + continue; |
| 88 | + }; |
| 89 | + let Some(ipt) = ipt.update(process.read(ipt_address).ok()) else { |
| 90 | + next_tick().await; |
| 91 | + continue; |
| 92 | + }; |
| 93 | + let Some(action) = action.update(process.read(action_address).ok()) else { |
| 94 | + next_tick().await; |
| 95 | + continue; |
| 96 | + }; |
| 97 | + |
| 98 | + timer::set_game_time(Duration::saturating_seconds_f32( |
| 99 | + total.current as f32 * ipt.current, |
| 100 | + )); |
| 101 | + |
| 102 | + if action.changed() { |
| 103 | + match action.current { |
| 104 | + TIMER_ACTION_START | TIMER_ACTION_RESTART => { |
| 105 | + timer::start(); |
| 106 | + } |
| 107 | + TIMER_ACTION_SPLIT | TIMER_ACTION_END => { |
| 108 | + timer::split(); |
| 109 | + } |
| 110 | + TIMER_ACTION_RESET => { |
| 111 | + timer::reset(); |
| 112 | + } |
| 113 | + _ => (), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + next_tick().await; |
| 118 | + } |
| 119 | + }) |
| 120 | + .await; |
| 121 | + } |
| 122 | +} |
0 commit comments