hao-il2cpp-unity 0.0.8

Typed Unity wrappers for mods of Unity Il2Cpp games
Documentation
//! Unity structs and enums, laid out as the game passes them.

use hao_il2cpp_runtime::{element, Il2CppValue, TypeInfo};

macro_rules! value_type {
    ($(#[$doc:meta])* $name:ident = $namespace:literal $class:literal { $($(#[$field_doc:meta])* $field:ident: $t:ty),* }) => {
        $(#[$doc])*
        #[repr(C)]
        #[derive(Debug, Clone, Copy, PartialEq, Default)]
        pub struct $name {
            $($(#[$field_doc])* pub $field: $t,)*
        }

        unsafe impl Il2CppValue for $name {
            fn matches(ty: TypeInfo) -> bool {
                ty.is_value_type($namespace, $class)
            }
        }
    };
}

value_type!(
    /// `UnityEngine.Vector2`.
    Vector2 = "UnityEngine" "Vector2" {
        /// Horizontal; increases to the right.
        x: f32,
        /// Vertical; increases upwards.
        y: f32
    }
);
value_type!(
    /// `UnityEngine.Vector3`.
    Vector3 = "UnityEngine" "Vector3" {
        /// Right (positive) and left.
        x: f32,
        /// Up (positive) and down.
        y: f32,
        /// Forward (positive) and back; Unity is left-handed.
        z: f32
    }
);
value_type!(
    /// `UnityEngine.Color`, with components from 0 to 1.
    Color = "UnityEngine" "Color" {
        /// Red.
        r: f32,
        /// Green.
        g: f32,
        /// Blue.
        b: f32,
        /// Opacity: 0 is transparent, 1 opaque.
        a: f32
    }
);
value_type!(
    /// `UnityEngine.Quaternion`, a rotation. Use [`Quaternion::euler`] to build one.
    Quaternion = "UnityEngine" "Quaternion" {
        /// X of the vector part (the rotation axis scaled by `sin(angle / 2)`).
        x: f32,
        /// Y of the vector part.
        y: f32,
        /// Z of the vector part.
        z: f32,
        /// The scalar part, `cos(angle / 2)`; 1 with a zero vector part is no rotation.
        w: f32
    }
);

impl Vector2 {
    /// `(0, 0)`.
    pub const ZERO: Self = Self::new(0.0, 0.0);
    /// `(1, 1)`.
    pub const ONE: Self = Self::new(1.0, 1.0);

    /// A vector from its components (usable in `const`s).
    pub const fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    /// The vector's length.
    pub fn magnitude(self) -> f32 {
        (self.x * self.x + self.y * self.y).sqrt()
    }

    /// The same direction with length 1, or [`ZERO`](Self::ZERO) for a zero vector.
    pub fn normalized(self) -> Self {
        let m = self.magnitude();
        if m > 0.0 {
            Self::new(self.x / m, self.y / m)
        } else {
            Self::ZERO
        }
    }
}

impl std::ops::Add for Vector2 {
    type Output = Self;
    fn add(self, o: Self) -> Self {
        Self::new(self.x + o.x, self.y + o.y)
    }
}

impl std::ops::Sub for Vector2 {
    type Output = Self;
    fn sub(self, o: Self) -> Self {
        Self::new(self.x - o.x, self.y - o.y)
    }
}

impl std::ops::Mul<f32> for Vector2 {
    type Output = Self;
    fn mul(self, s: f32) -> Self {
        Self::new(self.x * s, self.y * s)
    }
}

impl Vector3 {
    /// `(0, 0, 0)`.
    pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
    /// `(1, 1, 1)`, e.g. the default scale.
    pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
    /// `(0, 1, 0)`, world up.
    pub const UP: Self = Self::new(0.0, 1.0, 0.0);
    /// `(0, 0, 1)`, world forward.
    pub const FORWARD: Self = Self::new(0.0, 0.0, 1.0);

    /// A vector from its components (usable in `const`s).
    pub const fn new(x: f32, y: f32, z: f32) -> Self {
        Self { x, y, z }
    }

    /// The dot product: the cosine of the angle between two unit vectors.
    pub fn dot(self, o: Self) -> f32 {
        self.x * o.x + self.y * o.y + self.z * o.z
    }

    /// The vector's length.
    pub fn magnitude(self) -> f32 {
        self.dot(self).sqrt()
    }

    /// The same direction with length 1, or [`ZERO`](Self::ZERO) for a zero vector.
    pub fn normalized(self) -> Self {
        let m = self.magnitude();
        if m > 0.0 {
            self * (1.0 / m)
        } else {
            Self::ZERO
        }
    }

    /// The distance between two points.
    pub fn distance(self, o: Self) -> f32 {
        (self - o).magnitude()
    }
}

impl std::ops::Add for Vector3 {
    type Output = Self;
    fn add(self, o: Self) -> Self {
        Self::new(self.x + o.x, self.y + o.y, self.z + o.z)
    }
}

impl std::ops::Sub for Vector3 {
    type Output = Self;
    fn sub(self, o: Self) -> Self {
        Self::new(self.x - o.x, self.y - o.y, self.z - o.z)
    }
}

impl std::ops::Mul<f32> for Vector3 {
    type Output = Self;
    fn mul(self, s: f32) -> Self {
        Self::new(self.x * s, self.y * s, self.z * s)
    }
}

impl Color {
    /// Opaque white.
    pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
    /// Opaque black.
    pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
    /// Opaque red.
    pub const RED: Self = Self::rgb(1.0, 0.0, 0.0);
    /// Opaque green.
    pub const GREEN: Self = Self::rgb(0.0, 1.0, 0.0);
    /// Opaque blue.
    pub const BLUE: Self = Self::rgb(0.0, 0.0, 1.0);
    /// Unity's `Color.yellow`, which is slightly orange: `(1, 0.92, 0.016)`.
    pub const YELLOW: Self = Self::rgb(1.0, 0.92, 0.016);
    /// Fully transparent, e.g. to clear an overlay.
    pub const CLEAR: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);

    /// An opaque colour, each component from 0 to 1.
    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
        Self::rgba(r, g, b, 1.0)
    }

    /// A colour with opacity `a` (0 transparent, 1 opaque).
    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
        Self { r, g, b, a }
    }

    /// The colour with its alpha replaced.
    pub const fn with_alpha(self, a: f32) -> Self {
        Self {
            r: self.r,
            g: self.g,
            b: self.b,
            a,
        }
    }
}

macro_rules! int_enum {
    ($(#[$doc:meta])* $name:ident = $namespace:literal $class:literal { $($(#[$variant_doc:meta])* $variant:ident = $value:literal),* }) => {
        $(#[$doc])*
        #[repr(transparent)]
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub struct $name(
            #[doc = concat!("The raw `", $class, "` value.")]
            pub i32,
        );

        impl $name {
            $(
                $(#[$variant_doc])*
                #[doc = ""]
                #[doc = concat!("`", $class, "` value `", stringify!($value), "`.")]
                pub const $variant: Self = Self($value);
            )*
        }

        unsafe impl Il2CppValue for $name {
            fn matches(ty: TypeInfo) -> bool {
                !ty.byref && ty.is_enum && ty.element == element::I4 && ty.namespace == $namespace && ty.name == $class
            }
        }
    };
}

int_enum!(
    /// `UnityEngine.SceneManagement.LoadSceneMode`.
    LoadSceneMode = "UnityEngine.SceneManagement" "LoadSceneMode" {
        /// The scene replaced every loaded scene.
        SINGLE = 0,
        /// The scene was loaded alongside the ones already loaded.
        ADDITIVE = 1
    }
);
int_enum!(
    /// `UnityEngine.RenderMode`, how a [`Canvas`](crate::Canvas) is drawn.
    RenderMode = "UnityEngine" "RenderMode" {
        /// Drawn over the whole screen, on top of every camera.
        SCREEN_SPACE_OVERLAY = 0,
        /// Drawn over the screen by a chosen camera, at a distance in front of it.
        SCREEN_SPACE_CAMERA = 1,
        /// A flat object placed in the scene like any other.
        WORLD_SPACE = 2
    }
);
int_enum!(
    /// `UnityEngine.TextureFormat`.
    TextureFormat = "UnityEngine" "TextureFormat" {
        /// One byte per pixel: alpha only.
        ALPHA8 = 1,
        /// Four bytes per pixel in R, G, B, A order; the usual choice for raw pixels.
        RGBA32 = 4,
        /// Four bytes per pixel in A, R, G, B order.
        ARGB32 = 5,
        /// Three bytes per pixel, no alpha.
        RGB24 = 3
    }
);
int_enum!(
    /// `UnityEngine.FilterMode`, how a texture is sampled. `POINT` samples the
    /// nearest texel exactly (no blending with neighbours).
    FilterMode = "UnityEngine" "FilterMode" {
        /// Nearest texel, for crisp pixel art and glyph atlases.
        POINT = 0,
        /// Blends the four nearest texels.
        BILINEAR = 1,
        /// Bilinear, also blended between mipmap levels.
        TRILINEAR = 2
    }
);
int_enum!(
    /// `UnityEngine.HideFlags`. `HIDE_AND_DONT_SAVE` keeps a runtime-created
    /// object (e.g. a `Material`) from being destroyed on scene load.
    HideFlags = "UnityEngine" "HideFlags" {
        /// Normal: visible, saved, and unloaded with its scene.
        NONE = 0,
        /// Not unloaded by `Resources.UnloadUnusedAssets`.
        DONT_UNLOAD_UNUSED_ASSET = 32,
        /// Hidden, never saved, and not unloaded on scene load; the owner must destroy it.
        HIDE_AND_DONT_SAVE = 61
    }
);
int_enum!(
    /// `UnityEngine.KeyCode`, for [`input`](crate::input). A few common keys are
    /// named; any other can be made with `KeyCode(value)`.
    KeyCode = "UnityEngine" "KeyCode" {
        BACKSPACE = 8, TAB = 9, RETURN = 13, ESCAPE = 27, SPACE = 32, DELETE = 127,
        LEFT = 276, RIGHT = 275, UP = 273, DOWN = 274,
        INSERT = 277, HOME = 278, END = 279, PAGE_UP = 280, PAGE_DOWN = 281,
        ALPHA0 = 48, ALPHA1 = 49, ALPHA2 = 50, ALPHA3 = 51,
        A = 97, B = 98, C = 99, D = 100, E = 101, F = 102, G = 103, H = 104,
        I = 105, J = 106, K = 107, L = 108, M = 109, N = 110, O = 111, P = 112,
        Q = 113, R = 114, S = 115, T = 116, U = 117, V = 118, W = 119, X = 120,
        Y = 121, Z = 122,
        F1 = 282, F2 = 283, F3 = 284, F4 = 285, F5 = 286, F6 = 287,
        RIGHT_SHIFT = 303, LEFT_SHIFT = 304, RIGHT_CONTROL = 305, LEFT_CONTROL = 306,
        RIGHT_ALT = 307, LEFT_ALT = 308
    }
);