fromsoftware_shared/
program.rs

1use std::sync::LazyLock;
2
3use pelite::pe64::{Pe, PeFile, PeObject, PeView};
4use windows::Win32::System::LibraryLoader::GetModuleHandleA;
5use windows::core::PCSTR;
6
7#[derive(Copy, Clone)]
8pub enum Program<'a> {
9    File(PeFile<'a>),
10    Mapping(PeView<'a>),
11}
12
13static CURRENT_BASE: LazyLock<Program> = LazyLock::new(|| {
14    let module = unsafe { GetModuleHandleA(PCSTR(std::ptr::null())).unwrap().0 } as *const u8;
15    Program::Mapping(unsafe { PeView::module(module) })
16});
17
18impl Program<'_> {
19    /// Returns the currently running programing.
20    pub fn current() -> Self {
21        *CURRENT_BASE
22    }
23}
24
25unsafe impl<'a> Pe<'a> for Program<'a> {}
26unsafe impl<'a> PeObject<'a> for Program<'a> {
27    fn image(&self) -> &'a [u8] {
28        match self {
29            Self::File(file) => file.image(),
30            Self::Mapping(mapping) => mapping.image(),
31        }
32    }
33
34    fn align(&self) -> pelite::Align {
35        match self {
36            Self::File(file) => file.align(),
37            Self::Mapping(mapping) => mapping.align(),
38        }
39    }
40}