1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use libil2cpp::{Parameters, Return, ThisParameter};

/// Trait implemented by all hooks to facilitate generic programming
pub trait Hook {
    /// Type of this for the hooked method
    type This: ThisParameter;
    /// Type of the parameters for the hooked method
    type Parameters: Parameters;
    /// Type of the return for the hooked method
    type Return: Return;

    /// Namespace of the hooked method's class
    const NAMESPACE: &'static str;
    /// Name of the hooked method's class
    const CLASS_NAME: &'static str;
    /// Name of the hooked method
    const METHOD_NAME: &'static str;

    /// Installs the hook
    fn install(&self) -> Result<(), HookInstallError>;

    /// Pointer to the hook function
    fn hook(&self) -> *const ();
    /// Pointer to the hooked method, if installed
    fn original(&self) -> Option<*const ()>;
}

/// Possible errors when installing a hook
#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HookInstallError {
    /// Hook already installed
    #[error("hook already installed")]
    AlreadyInstalled,

    /// Class not found
    #[error("class not found")]
    ClassNotFound,

    /// Method not found
    #[error("method not found")]
    MethodNotFound,

    /// Error installing hook
    #[error("error installing hook")]
    InstallError,
}