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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::l2c_value::*;
impl L2CValue {
    pub const fn new_void() -> Self {
        Self {
            val_type: L2CValueType::Void,
            unk1: 0,
            inner: L2CValueInner { raw: 0 as u64 },
            unk2: 0
        }
    }

    pub const fn new_bool(val: bool) -> Self {
        Self {
            val_type: L2CValueType::Bool,
            unk1: 0,
            inner: L2CValueInner { raw: val as u64 },
            unk2: 0
        }
    }

    pub const fn new_int(val: u64) -> Self {
        Self {
            val_type: L2CValueType::Int,
            unk1: 0,
            inner: L2CValueInner { raw: val as u64 },
            unk2: 0
        }
    }

    pub const fn new_num(val: f32) -> Self {
        Self {
            val_type: L2CValueType::Num,
            unk1: 0,
            inner: L2CValueInner { raw_float: val },
            unk2: 0
        }
    }

    pub const fn new_hash(val: u64) -> Self {
        Self {
            val_type: L2CValueType::Hash,
            unk1: 0,
            inner: L2CValueInner { raw: val },
            unk2: 0,
        }
    }

    pub fn try_get_bool(&self) -> Option<bool> {
        if let L2CValueType::Bool = self.val_type {
            Some(unsafe { self.inner.raw } & 1 != 0)
        } else {
            None
        }
    }

    #[track_caller]
    pub fn get_bool(&self) -> bool {
        if let Some(val) = self.try_get_bool() {
            val
        } else {
            panic!("L2CValue: {:?} not a bool", self);
        }
    }

    pub fn try_get_int(&self) -> Option<u64> {
        if let L2CValueType::Int = self.val_type {
            Some(unsafe { self.inner.raw })
        } else {
            None
        }
    }

    #[track_caller]
    pub fn get_int(&self) -> u64 {
        if let Some(val) = self.try_get_int() {
            val
        } else {
            panic!("L2CValue: {:?} not an int", self);
        }
    }

    pub fn try_get_num(&self) -> Option<f32> {
        if let L2CValueType::Num = self.val_type {
            Some(unsafe { self.inner.raw_float })
        } else {
            None
        }
    }

    #[track_caller]
    pub fn get_num(&self) -> f32 {
        if let Some(val) = self.try_get_num() {
            val
        } else {
            panic!("L2CValue: {:?} not a float", self);
        }
    }

    pub fn try_get_ptr<T>(&self) -> Option<*mut T> {
        if let L2CValueType::Pointer = self.val_type {
            Some(unsafe { self.inner.raw as *mut T })
        } else {
            None
        }
    }

    #[track_caller]
    pub fn get_ptr<T>(&self) -> *mut T {
        if let Some(val) = self.try_get_ptr() {
            val
        } else {
            panic!("L2CValue: {:?} is not a pointer", self);
        }
    }

    pub fn assign(&mut self, other: &Self) {
        self.val_type = other.val_type;
        self.unk1 = other.unk1;
        self.inner = other.inner;
    }
}

macro_rules! impl_into_l2cvalue_int {
    (
        $(
            $ty:ty
        )*
    ) => {
        $(
            impl Into<L2CValue> for $ty {
                fn into(self) -> L2CValue {
                    L2CValue::new_int(self as u64)
                }
            }
        )*
    };
}

macro_rules! impl_into_l2cvalue_float {
    (
        $(
            $ty:ty
        )*
    ) => {
        $(
            impl Into<L2CValue> for $ty {
                fn into(self) -> L2CValue {
                    L2CValue::new_num(self as f32)
                }
            }
        )*
    };
}

impl Into<L2CValue> for bool {
    fn into(self) -> L2CValue {
        L2CValue::new_bool(self)
    }
}

impl_into_l2cvalue_int!(i8 u8 i16 u16 i32 u32 u64 i64);
impl_into_l2cvalue_float!(f32 f64);