From f4bfb67cbb436731b17c70eeba197e088e13c998 Mon Sep 17 00:00:00 2001 From: xshady Date: Mon, 23 Sep 2024 00:58:47 +0300 Subject: [PATCH] change indentation in some macros (where rustfmt did not) --- altv_module/src/helpers.rs | 56 +- .../src/base_objects/extra_pools/mod.rs | 142 ++-- core_resource/src/base_objects/objects.rs | 664 +++++++++--------- core_resource/src/config_node.rs | 68 +- core_resource/src/events/helpers.rs | 14 +- core_resource/src/events/mod.rs | 372 +++++----- core_resource/src/exports.rs | 24 +- core_resource/src/helpers.rs | 56 +- core_resource/src/lib.rs | 2 +- core_resource/src/meta/base_object/entry.rs | 154 ++-- core_resource/src/resource.rs | 44 +- mvalue/src/helpers.rs | 60 +- test/rust_resource/src/core_funcs.rs | 6 +- test/rust_resource/src/mvalue.rs | 180 ++--- 14 files changed, 921 insertions(+), 921 deletions(-) diff --git a/altv_module/src/helpers.rs b/altv_module/src/helpers.rs index 7adcc0fd..6ed47632 100644 --- a/altv_module/src/helpers.rs +++ b/altv_module/src/helpers.rs @@ -1,35 +1,35 @@ #[macro_export] macro_rules! on_base_object_event { - ($method_name:ident, $resource_name:expr, $base_object:expr) => { - paste::paste! { - RESOURCE_MANAGER_INSTANCE.with(|manager| { - let stringified_method_name = stringify!([$method_name]); + ($method_name:ident, $resource_name:expr, $base_object:expr) => { + paste::paste! { + RESOURCE_MANAGER_INSTANCE.with(|manager| { + let stringified_method_name = stringify!([$method_name]); - let manager = manager.borrow(); - if manager.is_pending(&$resource_name) { - logger::debug!( - "{} resource start is pending: {}", - stringified_method_name, - $resource_name - ); - return; - } + let manager = manager.borrow(); + if manager.is_pending(&$resource_name) { + logger::debug!( + "{} resource start is pending: {}", + stringified_method_name, + $resource_name + ); + return; + } - let base_object_type = unsafe { altv_sdk::helpers::get_base_object_type($base_object.as_ptr()) }; + let base_object_type = unsafe { altv_sdk::helpers::get_base_object_type($base_object.as_ptr()) }; - logger::debug!( - "{} type: {:?}", - stringified_method_name, - base_object_type - ); + logger::debug!( + "{} type: {:?}", + stringified_method_name, + base_object_type + ); - manager - .get_resource_for_module_by_name($resource_name) - .unwrap_or_else(|| { - panic!("{} resource: {:?} get_resource_for_module_by_path failed", stringified_method_name, $resource_name); - }) - .[<$method_name>]($base_object, base_object_type); - }); - } - }; + manager + .get_resource_for_module_by_name($resource_name) + .unwrap_or_else(|| { + panic!("{} resource: {:?} get_resource_for_module_by_path failed", stringified_method_name, $resource_name); + }) + .[<$method_name>]($base_object, base_object_type); + }); + } + }; } diff --git a/core_resource/src/base_objects/extra_pools/mod.rs b/core_resource/src/base_objects/extra_pools/mod.rs index ada1f3e7..e9886cdb 100644 --- a/core_resource/src/base_objects/extra_pools/mod.rs +++ b/core_resource/src/base_objects/extra_pools/mod.rs @@ -13,87 +13,87 @@ mod world_object; pub use world_object::{WorldObject, WorldObjectRawPtr}; macro_rules! extra_pool_enum { - (@internal - $any_name:ident, - $name:ident, - $raw_ptr_type:ty: [ $( - $variant:ident, - $container:ty, - $wrapper:ty; - )+ ] - ) => { - paste::paste! { - #[derive(Debug)] - pub enum $any_name { $( - $variant($container), - )+ } + (@internal + $any_name:ident, + $name:ident, + $raw_ptr_type:ty: [ $( + $variant:ident, + $container:ty, + $wrapper:ty; + )+ ] + ) => { + paste::paste! { + #[derive(Debug)] + pub enum $any_name { $( + $variant($container), + )+ } - impl $any_name { - #[allow(dead_code)] - pub(crate) fn raw_ptr(&self) -> SomeResult<$raw_ptr_type> { - use super::inherit_ptrs::traits::*; - match self { $( - $any_name::$variant(e) => Ok(e.inherit_ptrs()?.[<$name:snake>]()), - )+} - } - } - - impl TryFrom for $any_name { - type Error = anyhow::Error; - fn try_from(value: AnyBaseObject) -> Result { - Ok(match value { - $( - AnyBaseObject::$variant(e) => $any_name::$variant(e), - )+ - base_object => anyhow::bail!("cannot convert: {base_object:?} to {}", stringify!($any_name)), - }) - } - } + impl $any_name { + #[allow(dead_code)] + pub(crate) fn raw_ptr(&self) -> SomeResult<$raw_ptr_type> { + use super::inherit_ptrs::traits::*; + match self { $( + $any_name::$variant(e) => Ok(e.inherit_ptrs()?.[<$name:snake>]()), + )+} + } + } - $( - impl From<$container> for $any_name { - fn from(value: $container) -> Self { - $any_name::$variant(value) - } - } + impl TryFrom for $any_name { + type Error = anyhow::Error; + fn try_from(value: AnyBaseObject) -> Result { + Ok(match value { + $( + AnyBaseObject::$variant(e) => $any_name::$variant(e), + )+ + base_object => anyhow::bail!("cannot convert: {base_object:?} to {}", stringify!($any_name)), + }) + } + } - // TODO: refactor this shit, this only needed for meta - impl From> for $any_name { - fn from(value: Rc<$wrapper>) -> Self { - $any_name::$variant(BaseObjectContainer(value)) - } - } - )+ + $( + impl From<$container> for $any_name { + fn from(value: $container) -> Self { + $any_name::$variant(value) } - }; + } - ($name:ident, $raw_ptr_type:ty: [ $( $variant:ident, $base_object_mod:path; )+ ]) => { - paste::paste! { - extra_pool_enum!(@internal - [], - $name, - $raw_ptr_type: [ - $( - $variant, - $base_object_mod::[<$variant Container>], - $base_object_mod::$variant; - )+ - ] - ); + // TODO: refactor this shit, this only needed for meta + impl From> for $any_name { + fn from(value: Rc<$wrapper>) -> Self { + $any_name::$variant(BaseObjectContainer(value)) } - }; + } + )+ + } + }; + + ($name:ident, $raw_ptr_type:ty: [ $( $variant:ident, $base_object_mod:path; )+ ]) => { + paste::paste! { + extra_pool_enum!(@internal + [], + $name, + $raw_ptr_type: [ + $( + $variant, + $base_object_mod::[<$variant Container>], + $base_object_mod::$variant; + )+ + ] + ); + } + }; } extra_pool_enum!(Entity, EntityRawPtr: [ - Player, player; - Vehicle, vehicle; - Ped, ped; - Object, object; + Player, player; + Vehicle, vehicle; + Ped, ped; + Object, object; ]); extra_pool_enum!(WorldObject, WorldObjectRawPtr: [ - Player, player; - Vehicle, vehicle; - Ped, ped; - Object, object; + Player, player; + Vehicle, vehicle; + Ped, ped; + Object, object; ]); diff --git a/core_resource/src/base_objects/objects.rs b/core_resource/src/base_objects/objects.rs index 848ed65d..b9a473b2 100644 --- a/core_resource/src/base_objects/objects.rs +++ b/core_resource/src/base_objects/objects.rs @@ -9,357 +9,357 @@ use super::{ use crate::{col_shape::ColShapy, sdk}; macro_rules! base_objects { - (@internal $( - $manager_name_snake:ident - $name_struct:ident - $name_container:ident - $name_ptr:ident - $manager_name_rc:ident - $manager_name:ident: [ - $base_type:path, - $( - @inherit_classes: $inherit_ptrs_struct:ty, [ $( - $impl_trait:ty, - )+ ], - )? - ], - )+ ) => { - paste::paste! { - $( - pub mod $manager_name_snake { - use super::*; - - pub type $name_struct = sdk::alt::[]; - - #[doc = "[Implementation](struct.BaseObjectWrapper.html#" [<$manager_name:lower>] "-implementation)"] - pub type $manager_name = BaseObjectWrapper< - $name_struct - $(, $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct )? - >; - - // TODO: refactor this shit, this only needed for meta - #[allow(dead_code)] - pub(crate) type $manager_name_rc = Rc<$manager_name>; - - pub type $name_container = BaseObjectContainer< - $name_struct - $(, $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct )? - >; - - #[allow(dead_code)] - pub type $name_ptr = NonNull<$name_struct>; - - impl Debug for $name_container { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{} {{ ... }}", stringify!($manager_name)) - } - } - - impl BaseObjectPoolFuncs<$name_container> for $manager_name { - fn all() -> Vec<$name_container> { - $crate::resource::Resource::with_base_objects_ref(|v, _| v.[<$manager_name_snake>].all()) - } - - fn all_count() -> usize { - $crate::resource::Resource::with_base_objects_ref(|v, _| v.[<$manager_name_snake>].all_count()) - } - - fn get_by_id(id: BaseObjectId) -> Option<$name_container> { - $crate::resource::Resource::with_base_objects_ref(|v, _| { - v.[<$manager_name_snake>].get_by_id(id) - }) - } - } - $( $( - impl $impl_trait <$crate::base_objects::inherit_ptrs::$inherit_ptrs_struct> for $manager_name {} - )+ )? - - impl_deserialize_for!($name_container, $manager_name, [<$manager_name_snake _mvalue_deserialize_impl>]); - - #[macro_export] - macro_rules! [<__ $manager_name_snake _remove_from_pool>] { - ($base_object:expr) => { - $crate::resource::Resource::with_base_objects_mut(|mut v, _| -> $crate::VoidResult { - use $crate::base_objects::BasePtr; - let base_ptr = $base_object.base_ptr()?; - v.[<$manager_name_snake>].remove(base_ptr, $base_object.ptr()?)?; - Ok(()) - }) - }; - } - - #[allow(unused_imports)] - pub(crate) use [<__ $manager_name_snake _remove_from_pool>] as remove_from_pool; - - #[macro_export] - macro_rules! [<__ $manager_name_snake _add_to_pool>] { - ($ptr:expr) => { - $crate::resource::Resource::with_base_objects_mut(|mut v, _| { - let base_ptr = std::ptr::NonNull::new(unsafe { - $crate::sdk::$manager_name_snake::to_base_object($ptr.as_ptr()) - }).unwrap(); - - let inherit_ptrs = $crate::helpers::if_not!( - ($( - unsafe { $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct::new(base_ptr.as_ptr()) } - )?) {} - ); - - let container = Self::_new($ptr, base_ptr, inherit_ptrs); - - v.[<$manager_name_snake>].add(base_ptr, $ptr, container.clone()); - - container - }) - }; - } - - #[allow(unused_imports)] - pub(crate) use [<__ $manager_name_snake _add_to_pool>] as add_to_pool; + (@internal $( + $manager_name_snake:ident + $name_struct:ident + $name_container:ident + $name_ptr:ident + $manager_name_rc:ident + $manager_name:ident: [ + $base_type:path, + $( + @inherit_classes: $inherit_ptrs_struct:ty, [ $( + $impl_trait:ty, + )+ ], + )? + ], + )+ ) => { + paste::paste! { + $( + pub mod $manager_name_snake { + use super::*; + + pub type $name_struct = sdk::alt::[]; + + #[doc = "[Implementation](struct.BaseObjectWrapper.html#" [<$manager_name:lower>] "-implementation)"] + pub type $manager_name = BaseObjectWrapper< + $name_struct + $(, $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct )? + >; + + // TODO: refactor this shit, this only needed for meta + #[allow(dead_code)] + pub(crate) type $manager_name_rc = Rc<$manager_name>; + + pub type $name_container = BaseObjectContainer< + $name_struct + $(, $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct )? + >; + + #[allow(dead_code)] + pub type $name_ptr = NonNull<$name_struct>; + + impl Debug for $name_container { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} {{ ... }}", stringify!($manager_name)) + } + } + + impl BaseObjectPoolFuncs<$name_container> for $manager_name { + fn all() -> Vec<$name_container> { + $crate::resource::Resource::with_base_objects_ref(|v, _| v.[<$manager_name_snake>].all()) + } + + fn all_count() -> usize { + $crate::resource::Resource::with_base_objects_ref(|v, _| v.[<$manager_name_snake>].all_count()) + } + + fn get_by_id(id: BaseObjectId) -> Option<$name_container> { + $crate::resource::Resource::with_base_objects_ref(|v, _| { + v.[<$manager_name_snake>].get_by_id(id) + }) + } + } + $( $( + impl $impl_trait <$crate::base_objects::inherit_ptrs::$inherit_ptrs_struct> for $manager_name {} + )+ )? + + impl_deserialize_for!($name_container, $manager_name, [<$manager_name_snake _mvalue_deserialize_impl>]); + + #[macro_export] + macro_rules! [<__ $manager_name_snake _remove_from_pool>] { + ($base_object:expr) => { + $crate::resource::Resource::with_base_objects_mut(|mut v, _| -> $crate::VoidResult { + use $crate::base_objects::BasePtr; + let base_ptr = $base_object.base_ptr()?; + v.[<$manager_name_snake>].remove(base_ptr, $base_object.ptr()?)?; + Ok(()) + }) + }; + } + + #[allow(unused_imports)] + pub(crate) use [<__ $manager_name_snake _remove_from_pool>] as remove_from_pool; + + #[macro_export] + macro_rules! [<__ $manager_name_snake _add_to_pool>] { + ($ptr:expr) => { + $crate::resource::Resource::with_base_objects_mut(|mut v, _| { + let base_ptr = std::ptr::NonNull::new(unsafe { + $crate::sdk::$manager_name_snake::to_base_object($ptr.as_ptr()) + }).unwrap(); + + let inherit_ptrs = $crate::helpers::if_not!( + ($( + unsafe { $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct::new(base_ptr.as_ptr()) } + )?) {} + ); + + let container = Self::_new($ptr, base_ptr, inherit_ptrs); + + v.[<$manager_name_snake>].add(base_ptr, $ptr, container.clone()); + + container + }) + }; + } + + #[allow(unused_imports)] + pub(crate) use [<__ $manager_name_snake _add_to_pool>] as add_to_pool; + } + )+ + + #[derive(Debug)] + pub enum AnyBaseObject { $( + $manager_name($manager_name_snake::$name_container), + )+ } + + #[derive(Default)] + pub struct Store { + $( + pub(crate) $manager_name_snake: BaseObjectManager< + $manager_name_snake::$name_struct + $(, $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct )? + >, + )+ + } + + impl Store { + pub fn on_create( + &mut self, + base_ptr: NonNull, + base_object_type: altv_sdk::BaseObjectType, + ) { + match base_object_type { + $( + $base_type => { + let ptr = $crate::helpers::base_ptr_to!(base_ptr.as_ptr(), $manager_name_snake); + if self.[<$manager_name_snake>].has(ptr) { + logger::debug!("base object: {base_object_type:?} {ptr:?} already added"); + return; + } + + let inherit_ptrs = $crate::helpers::if_not!( + ($(unsafe { + $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct::new(base_ptr.as_ptr()) + })?) {} + ); + + let container = $manager_name_snake::$manager_name::_new( + ptr, + base_ptr, + inherit_ptrs + ); + + self.[<$manager_name_snake>].add( + base_ptr, + ptr, + container.clone(), + ); } - )+ + )+ + _ => { + logger::error!("unknown base object type: {base_object_type:?}") + } + } + } - #[derive(Debug)] - pub enum AnyBaseObject { $( - $manager_name($manager_name_snake::$name_container), - )+ } - - #[derive(Default)] - pub struct Store { - $( - pub(crate) $manager_name_snake: BaseObjectManager< - $manager_name_snake::$name_struct - $(, $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct )? - >, - )+ + pub fn on_remove( + &mut self, + base_ptr: NonNull, + base_object_type: altv_sdk::BaseObjectType, + ) { + match base_object_type { + $( + $base_type => { + let ptr = $crate::helpers::base_ptr_to!(base_ptr.as_ptr(), $manager_name_snake); + self.[<$manager_name_snake>].remove_externally(base_ptr, ptr).unwrap(); + } + )+ + _ => { + logger::error!("unknown base object type: {base_object_type:?}") } + } + } - impl Store { - pub fn on_create( - &mut self, - base_ptr: NonNull, - base_object_type: altv_sdk::BaseObjectType, - ) { - match base_object_type { - $( - $base_type => { - let ptr = $crate::helpers::base_ptr_to!(base_ptr.as_ptr(), $manager_name_snake); - if self.[<$manager_name_snake>].has(ptr) { - logger::debug!("base object: {base_object_type:?} {ptr:?} already added"); - return; - } - - let inherit_ptrs = $crate::helpers::if_not!( - ($(unsafe { - $crate::base_objects::inherit_ptrs::$inherit_ptrs_struct::new(base_ptr.as_ptr()) - })?) {} - ); - - let container = $manager_name_snake::$manager_name::_new( - ptr, - base_ptr, - inherit_ptrs - ); - - self.[<$manager_name_snake>].add( - base_ptr, - ptr, - container.clone(), - ); - } - )+ - _ => { - logger::error!("unknown base object type: {base_object_type:?}") - } - } - } - - pub fn on_remove( - &mut self, - base_ptr: NonNull, - base_object_type: altv_sdk::BaseObjectType, - ) { - match base_object_type { - $( - $base_type => { - let ptr = $crate::helpers::base_ptr_to!(base_ptr.as_ptr(), $manager_name_snake); - self.[<$manager_name_snake>].remove_externally(base_ptr, ptr).unwrap(); - } - )+ - _ => { - logger::error!("unknown base object type: {base_object_type:?}") - } - } - } - - pub fn get_by_ptr( - &self, - base_ptr: NonNull, - ) -> Option { - let base_object_type = unsafe { altv_sdk::helpers::get_base_object_type(base_ptr.as_ptr()) }; - - match base_object_type { - $( - $base_type => { - let ptr = $crate::helpers::base_ptr_to!(base_ptr.as_ptr(), $manager_name_snake); - let base_object = self.[<$manager_name_snake>].get_by_ptr(ptr); - if let Some(base_object) = base_object { - Some(AnyBaseObject::$manager_name(base_object)) - } else { - logger::debug!("base object type: {base_object_type:?} is not in pool"); - None - } - } - )+ - _ => { - logger::error!("unknown base object type: {base_object_type:?}"); - None - } - } - } - - pub fn get_by_id( - &self, - base_object_type: altv_sdk::BaseObjectType, - id: BaseObjectId, - ) -> Option { - match base_object_type { - $( - $base_type => { - let base_object = self.[<$manager_name_snake>].get_by_id(id); - if let Some(base_object) = base_object { - Some(AnyBaseObject::$manager_name(base_object)) - } else { - None - } - } - )+ - _ => { - logger::error!("unknown base object type: {base_object_type:?}"); - None - } - } - } - - pub fn all(&self) -> Vec { - let capacity = self.all_count(); - let mut objects = Vec::with_capacity(capacity); - $( - let objects_of_type = self.[<$manager_name_snake>].all(); - objects.extend(&mut objects_of_type.into_iter().map(AnyBaseObject::$manager_name)); - )+ - objects - } - - pub fn all_count(&self) -> usize { - let mut count = 0; - $( - count += self.[<$manager_name_snake>].all_count(); - )+ - count - } + pub fn get_by_ptr( + &self, + base_ptr: NonNull, + ) -> Option { + let base_object_type = unsafe { altv_sdk::helpers::get_base_object_type(base_ptr.as_ptr()) }; + + match base_object_type { + $( + $base_type => { + let ptr = $crate::helpers::base_ptr_to!(base_ptr.as_ptr(), $manager_name_snake); + let base_object = self.[<$manager_name_snake>].get_by_ptr(ptr); + if let Some(base_object) = base_object { + Some(AnyBaseObject::$manager_name(base_object)) + } else { + logger::debug!("base object type: {base_object_type:?} is not in pool"); + None + } } + )+ + _ => { + logger::error!("unknown base object type: {base_object_type:?}"); + None + } + } } - }; - - ( $( - $manager_name:ident: [ - $base_type:path, - $( - @inherit_classes: $inherit_ptrs_struct:ty, [ $( - $impl_trait:ty, - )+ ], - )? - ], - )+ ) => { - paste::paste! { - base_objects!(@internal $( - [<$manager_name:snake>] - [<$manager_name Struct>] - [<$manager_name Container>] - [<$manager_name MutPtr>] - [<$manager_name Rc>] - $manager_name: [ - $base_type, - $( @inherit_classes: $inherit_ptrs_struct, [ $( - $impl_trait, - )+ ], )? - ], - )+ ); + + pub fn get_by_id( + &self, + base_object_type: altv_sdk::BaseObjectType, + id: BaseObjectId, + ) -> Option { + match base_object_type { + $( + $base_type => { + let base_object = self.[<$manager_name_snake>].get_by_id(id); + if let Some(base_object) = base_object { + Some(AnyBaseObject::$manager_name(base_object)) + } else { + None + } + } + )+ + _ => { + logger::error!("unknown base object type: {base_object_type:?}"); + None + } + } } - }; -} -base_objects!( - ColShape: [ - altv_sdk::BaseObjectType::Colshape, - @inherit_classes: WorldColShape, [ - WorldObject, - ColShapy, - ], - ], - Vehicle: [ - altv_sdk::BaseObjectType::Vehicle, - @inherit_classes: WorldEntity, [ - WorldObject, - Entity, - ], - ], - Player: [ - altv_sdk::BaseObjectType::Player, - @inherit_classes: WorldEntity, [ - WorldObject, - Entity, - ], + pub fn all(&self) -> Vec { + let capacity = self.all_count(); + let mut objects = Vec::with_capacity(capacity); + $( + let objects_of_type = self.[<$manager_name_snake>].all(); + objects.extend(&mut objects_of_type.into_iter().map(AnyBaseObject::$manager_name)); + )+ + objects + } + + pub fn all_count(&self) -> usize { + let mut count = 0; + $( + count += self.[<$manager_name_snake>].all_count(); + )+ + count + } + } + } + }; + + ( $( + $manager_name:ident: [ + $base_type:path, + $( + @inherit_classes: $inherit_ptrs_struct:ty, [ $( + $impl_trait:ty, + )+ ], + )? ], - Ped: [ - altv_sdk::BaseObjectType::Ped, - @inherit_classes: WorldEntity, [ - WorldObject, - Entity, + )+ ) => { + paste::paste! { + base_objects!(@internal $( + [<$manager_name:snake>] + [<$manager_name Struct>] + [<$manager_name Container>] + [<$manager_name MutPtr>] + [<$manager_name Rc>] + $manager_name: [ + $base_type, + $( @inherit_classes: $inherit_ptrs_struct, [ $( + $impl_trait, + )+ ], )? ], + )+ ); + } + }; +} + +base_objects!( + ColShape: [ + altv_sdk::BaseObjectType::Colshape, + @inherit_classes: WorldColShape, [ + WorldObject, + ColShapy, ], - Object: [ - altv_sdk::BaseObjectType::Object, - @inherit_classes: WorldEntity, [ - WorldObject, - Entity, - ], + ], + Vehicle: [ + altv_sdk::BaseObjectType::Vehicle, + @inherit_classes: WorldEntity, [ + WorldObject, + Entity, ], - VirtualEntity: [ - altv_sdk::BaseObjectType::VirtualEntity, - @inherit_classes: WorldObject, [ - WorldObject, - ], + ], + Player: [ + altv_sdk::BaseObjectType::Player, + @inherit_classes: WorldEntity, [ + WorldObject, + Entity, ], - VirtualEntityGroup: [ - altv_sdk::BaseObjectType::VirtualEntityGroup, + ], + Ped: [ + altv_sdk::BaseObjectType::Ped, + @inherit_classes: WorldEntity, [ + WorldObject, + Entity, ], - Blip: [ - altv_sdk::BaseObjectType::Blip, - @inherit_classes: WorldObject, [ - WorldObject, - ], + ], + Object: [ + altv_sdk::BaseObjectType::Object, + @inherit_classes: WorldEntity, [ + WorldObject, + Entity, ], - VoiceChannel: [ - altv_sdk::BaseObjectType::VoiceChannel, + ], + VirtualEntity: [ + altv_sdk::BaseObjectType::VirtualEntity, + @inherit_classes: WorldObject, [ + WorldObject, ], - Marker: [ - altv_sdk::BaseObjectType::Marker, - @inherit_classes: WorldObject, [ - WorldObject, - ], + ], + VirtualEntityGroup: [ + altv_sdk::BaseObjectType::VirtualEntityGroup, + ], + Blip: [ + altv_sdk::BaseObjectType::Blip, + @inherit_classes: WorldObject, [ + WorldObject, ], - Checkpoint: [ - altv_sdk::BaseObjectType::Checkpoint, - @inherit_classes: WorldColShape, [ - WorldObject, - ColShapy, - ], + ], + VoiceChannel: [ + altv_sdk::BaseObjectType::VoiceChannel, + ], + Marker: [ + altv_sdk::BaseObjectType::Marker, + @inherit_classes: WorldObject, [ + WorldObject, ], - ConnectionInfo: [ - altv_sdk::BaseObjectType::ConnectionInfo, + ], + Checkpoint: [ + altv_sdk::BaseObjectType::Checkpoint, + @inherit_classes: WorldColShape, [ + WorldObject, + ColShapy, ], + ], + ConnectionInfo: [ + altv_sdk::BaseObjectType::ConnectionInfo, + ], ); impl Debug for Store { diff --git a/core_resource/src/config_node.rs b/core_resource/src/config_node.rs index b303e534..c315ca77 100644 --- a/core_resource/src/config_node.rs +++ b/core_resource/src/config_node.rs @@ -4,46 +4,46 @@ use std::collections::HashMap; use crate::sdk; macro_rules! config_nodes { - ( - $enum_name:ident [ $( - $type_name:ident $( : $rust_type:ty )?, - )+ ], - $type_mod_name:ident - ) => { - paste::paste! { - #[derive(Debug)] - pub enum $enum_name { $( - $type_name $( ($rust_type) )?, - )+ } + ( + $enum_name:ident [ $( + $type_name:ident $( : $rust_type:ty )?, + )+ ], + $type_mod_name:ident + ) => { + paste::paste! { + #[derive(Debug)] + pub enum $enum_name { $( + $type_name $( ($rust_type) )?, + )+ } - impl $enum_name { $( $( - pub fn [](self) -> Option<$rust_type> { - match self { - $enum_name::$type_name(v) => Some(v), - $enum_name::None => None, - _ => panic!("Expected {}", stringify!($type_name)) - } - } - )? )+ } - - mod $type_mod_name { - use super::*; - $( $( - #[allow(dead_code)] - pub(super) type $type_name = $rust_type; - )? )+ + impl $enum_name { $( $( + pub fn [](self) -> Option<$rust_type> { + match self { + $enum_name::$type_name(v) => Some(v), + $enum_name::None => None, + _ => panic!("Expected {}", stringify!($type_name)) } + } + )? )+ } + + mod $type_mod_name { + use super::*; + $( $( + #[allow(dead_code)] + pub(super) type $type_name = $rust_type; + )? )+ } - }; + } + }; } config_nodes!(ConfigNode [ - Bool: bool, - String: std::string::String, - F64: f64, - List: Vec, - Dict: HashMap, - None, + Bool: bool, + String: std::string::String, + F64: f64, + List: Vec, + Dict: HashMap, + None, ], config_type); type ConfigNodePtr = UniquePtr; diff --git a/core_resource/src/events/helpers.rs b/core_resource/src/events/helpers.rs index 26f882ea..d23e1907 100644 --- a/core_resource/src/events/helpers.rs +++ b/core_resource/src/events/helpers.rs @@ -8,13 +8,13 @@ use crate::{ #[macro_export] macro_rules! __base_event_to_specific { - ($base_event:expr, $specific:ident) => { - paste::paste! {{ - let raw = $crate::sdk::events::[]($base_event); - assert!(!raw.is_null(), "converting base event to: {} returned null", stringify!($specific)); - raw - }} - }; + ($base_event:expr, $specific:ident) => { + paste::paste! {{ + let raw = $crate::sdk::events::[]($base_event); + assert!(!raw.is_null(), "converting base event to: {} returned null", stringify!($specific)); + raw + }} + }; } pub use __base_event_to_specific as base_event_to_specific; diff --git a/core_resource/src/events/mod.rs b/core_resource/src/events/mod.rs index 80c375b4..7b3d57a6 100644 --- a/core_resource/src/events/mod.rs +++ b/core_resource/src/events/mod.rs @@ -22,205 +22,205 @@ macro_rules! log_user_handler_error { } macro_rules! supported_sdk_events { - ( $( $event_name:ident, )+ ) => { - #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] - pub enum SupportedEventType { - $( $event_name, )+ - } + ( $( $event_name:ident, )+ ) => { + #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] + pub enum SupportedEventType { + $( $event_name, )+ + } - impl TryFrom for SupportedEventType { - type Error = anyhow::Error; - fn try_from(value: SDKEventType) -> SomeResult { - match value { - $( - SDKEventType::$event_name => Ok(Self::$event_name), - )+ - event => { - anyhow::bail!("unsupported cpp sdk event type: {event:?}") - } - } - } + impl TryFrom for SupportedEventType { + type Error = anyhow::Error; + fn try_from(value: SDKEventType) -> SomeResult { + match value { + $( + SDKEventType::$event_name => Ok(Self::$event_name), + )+ + event => { + anyhow::bail!("unsupported cpp sdk event type: {event:?}") + } } + } + } - #[allow(clippy::from_over_into)] - impl Into for SupportedEventType { - fn into(self) -> SDKEventType { - match self { $( - Self::$event_name => SDKEventType::$event_name, - )+ } - } - } + #[allow(clippy::from_over_into)] + impl Into for SupportedEventType { + fn into(self) -> SDKEventType { + match self { $( + Self::$event_name => SDKEventType::$event_name, + )+ } + } + } - pub enum SDKContext { - $( $event_name(sdk_contexts::$event_name), )+ - } + pub enum SDKContext { + $( $event_name(sdk_contexts::$event_name), )+ + } - impl std::fmt::Debug for SDKContext { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let str = format!("{}", match self { - $( Self::$event_name(_) => stringify!(SDKContext::$event_name), )+ - }); + impl std::fmt::Debug for SDKContext { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = format!("{}", match self { + $( Self::$event_name(_) => stringify!(SDKContext::$event_name), )+ + }); - f.write_str(&str) - } - } + f.write_str(&str) + } + } - pub enum SDKHandler { $( - $event_name(Box VoidResult + 'static>), - )+ } + pub enum SDKHandler { $( + $event_name(Box VoidResult + 'static>), + )+ } - impl SDKHandler { - pub fn to_event_type(&self) -> SupportedEventType { - match self { $( - Self::$event_name(_) => SupportedEventType::$event_name, - )+ } - } - } + impl SDKHandler { + pub fn to_event_type(&self) -> SupportedEventType { + match self { $( + Self::$event_name(_) => SupportedEventType::$event_name, + )+ } + } + } - impl std::fmt::Debug for SDKHandler { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let str = format!("{}", match self { - $( Self::$event_name(_) => stringify!(SDKHandler::$event_name), )+ - }); + impl std::fmt::Debug for SDKHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = format!("{}", match self { + $( Self::$event_name(_) => stringify!(SDKHandler::$event_name), )+ + }); - f.write_str(&str) - } - } + f.write_str(&str) + } + } - pub(crate) fn sdk_context_from_supported_event_type( - event_type: SupportedEventType, - event_ptr: altv_sdk::CEventPtr, - resource: &Resource, - ) -> SDKContext { - match event_type { $( - SupportedEventType::$event_name => - SDKContext::$event_name(unsafe { sdk_contexts::$event_name::new(event_ptr, resource) }), - )+ } - } + pub(crate) fn sdk_context_from_supported_event_type( + event_type: SupportedEventType, + event_ptr: altv_sdk::CEventPtr, + resource: &Resource, + ) -> SDKContext { + match event_type { $( + SupportedEventType::$event_name => + SDKContext::$event_name(unsafe { sdk_contexts::$event_name::new(event_ptr, resource) }), + )+ } + } - pub fn call_user_sdk_handlers(context: &SDKContext, handlers: &mut [SDKHandler]) { - for h in handlers { - match h { $( - SDKHandler::$event_name(h) => { - let result = h( - if let SDKContext::$event_name(context) = context { - context - } else { - // this should never happen because SDKHandler gets converted to SupportedEventType - // automatically with `to_event_type()` - panic!("expected SDKContext: {}, received: {context:?}", stringify!($event_name)) - } - ); - log_user_handler_error!($event_name, result); - } - )+ } - } - } + pub fn call_user_sdk_handlers(context: &SDKContext, handlers: &mut [SDKHandler]) { + for h in handlers { + match h { $( + SDKHandler::$event_name(h) => { + let result = h( + if let SDKContext::$event_name(context) = context { + context + } else { + // this should never happen because SDKHandler gets converted to SupportedEventType + // automatically with `to_event_type()` + panic!("expected SDKContext: {}, received: {context:?}", stringify!($event_name)) + } + ); + log_user_handler_error!($event_name, result); + } + )+ } + } } + } } macro_rules! custom_events { - ( $( - $sdk_event_name:ident: [ $( $custom_event_name:ident, )+ ], - )+ ) => { - #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] - pub enum CustomEventType { $($( - $custom_event_name, + ( $( + $sdk_event_name:ident: [ $( $custom_event_name:ident, )+ ], + )+ ) => { + #[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)] + pub enum CustomEventType { $($( + $custom_event_name, + )+)+ } + + #[allow(clippy::from_over_into)] + impl Into for CustomEventType { + fn into(self) -> SupportedEventType { + match self { $($( + Self::$custom_event_name => SupportedEventType::$sdk_event_name, )+)+ } + } + } - #[allow(clippy::from_over_into)] - impl Into for CustomEventType { - fn into(self) -> SupportedEventType { - match self { $($( - Self::$custom_event_name => SupportedEventType::$sdk_event_name, - )+)+ } - } - } + pub enum CustomContext { $($( + $custom_event_name(custom_contexts::$custom_event_name), + )+)+ } - pub enum CustomContext { $($( - $custom_event_name(custom_contexts::$custom_event_name), - )+)+ } + impl std::fmt::Debug for CustomContext { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = format!("{}", match self { + $($( Self::$custom_event_name(_) => stringify!(CustomContext::$custom_event_name), )+)+ + }); - impl std::fmt::Debug for CustomContext { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let str = format!("{}", match self { - $($( Self::$custom_event_name(_) => stringify!(CustomContext::$custom_event_name), )+)+ - }); + f.write_str(&str) + } + } - f.write_str(&str) - } - } + pub enum CustomHandler { $($( + $custom_event_name(Box VoidResult + 'static>), + )+)+ } - pub enum CustomHandler { $($( - $custom_event_name(Box VoidResult + 'static>), + impl CustomHandler { + pub fn to_event_type(&self) -> CustomEventType { + match self { $($( + CustomHandler::$custom_event_name(_) => CustomEventType::$custom_event_name, )+)+ } + } + } - impl CustomHandler { - pub fn to_event_type(&self) -> CustomEventType { - match self { $($( - CustomHandler::$custom_event_name(_) => CustomEventType::$custom_event_name, - )+)+ } - } - } - - impl std::fmt::Debug for CustomHandler { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let str = format!("{}", match self { - $($( Self::$custom_event_name(_) => stringify!(CustomHandler::$custom_event_name), )+)+ - }); + impl std::fmt::Debug for CustomHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = format!("{}", match self { + $($( Self::$custom_event_name(_) => stringify!(CustomHandler::$custom_event_name), )+)+ + }); - f.write_str(&str) - } - } + f.write_str(&str) + } + } - pub fn custom_context_from_event_type( - event_type: CustomEventType, - context: &SDKContext, - resource: &Resource, - ) -> Option { - match (context, event_type) { - $($( - (SDKContext::$sdk_event_name(context), CustomEventType::$custom_event_name) => { - if let Some(c) = custom_contexts::$custom_event_name::new(context, resource) { - Some(CustomContext::$custom_event_name(c)) - } else { - None - } - } - )+)+ - _ => None, + pub fn custom_context_from_event_type( + event_type: CustomEventType, + context: &SDKContext, + resource: &Resource, + ) -> Option { + match (context, event_type) { + $($( + (SDKContext::$sdk_event_name(context), CustomEventType::$custom_event_name) => { + if let Some(c) = custom_contexts::$custom_event_name::new(context, resource) { + Some(CustomContext::$custom_event_name(c)) + } else { + None } - } + } + )+)+ + _ => None, + } + } - pub fn call_user_custom_handlers(context: &CustomContext, handlers: &mut [CustomHandler]) { - for h in handlers { - match h { $($( - CustomHandler::$custom_event_name(h) => { - let result = h( - if let CustomContext::$custom_event_name(context) = context { - context - } else { - // this shit should never happen - panic!("expected CustomContext: {}, received: {context:?}", stringify!($custom_event_name)) - } - ); - log_user_handler_error!($custom_event_name, result); - } - )+)+ } - } - } + pub fn call_user_custom_handlers(context: &CustomContext, handlers: &mut [CustomHandler]) { + for h in handlers { + match h { $($( + CustomHandler::$custom_event_name(h) => { + let result = h( + if let CustomContext::$custom_event_name(context) = context { + context + } else { + // this shit should never happen + panic!("expected CustomContext: {}, received: {context:?}", stringify!($custom_event_name)) + } + ); + log_user_handler_error!($custom_event_name, result); + } + )+)+ } + } + } - pub fn get_custom_event_types_from_sdk_type<'a>(sdk_event_type: SupportedEventType) -> Option<&'a[CustomEventType]> { - match sdk_event_type { - $( - SupportedEventType::$sdk_event_name => Some(&[$( - CustomEventType::$custom_event_name, - )+]), - )+ - _ => None, - } - } + pub fn get_custom_event_types_from_sdk_type<'a>(sdk_event_type: SupportedEventType) -> Option<&'a[CustomEventType]> { + match sdk_event_type { + $( + SupportedEventType::$sdk_event_name => Some(&[$( + CustomEventType::$custom_event_name, + )+]), + )+ + _ => None, + } } + } } supported_sdk_events!( @@ -278,23 +278,23 @@ supported_sdk_events!( ); custom_events!( - ColshapeEvent: [ - VehicleEnterColShape, - VehicleLeaveColShape, - PlayerEnterColShape, - PlayerLeaveColShape, - ], - ResourceStart: [ - ThisResourceStart, - ], - ResourceStop: [ - ThisResourceStop, - ], - VoiceConnectionEvent: [ - VoiceConnect, - VoiceDisconnect, - VoiceConnecting, - ], + ColshapeEvent: [ + VehicleEnterColShape, + VehicleLeaveColShape, + PlayerEnterColShape, + PlayerLeaveColShape, + ], + ResourceStart: [ + ThisResourceStart, + ], + ResourceStop: [ + ThisResourceStop, + ], + VoiceConnectionEvent: [ + VoiceConnect, + VoiceDisconnect, + VoiceConnecting, + ], ); #[derive(Default, Debug)] diff --git a/core_resource/src/exports.rs b/core_resource/src/exports.rs index e19ab442..449a803c 100644 --- a/core_resource/src/exports.rs +++ b/core_resource/src/exports.rs @@ -47,26 +47,26 @@ pub mod logging { #[macro_export] macro_rules! __log { - ($($arg:tt)*) => {{ - $crate::exports::logging::log(&format!($($arg)*)) - }} - } + ($($arg:tt)*) => {{ + $crate::exports::logging::log(&format!($($arg)*)) + }} + } pub use __log as log_macro; #[macro_export] macro_rules! __log_warn { - ($($arg:tt)*) => {{ - $crate::exports::logging::log_warn(&format!($($arg)*)) - }} - } + ($($arg:tt)*) => {{ + $crate::exports::logging::log_warn(&format!($($arg)*)) + }} + } pub use __log_warn as log_warn_macro; #[macro_export] macro_rules! __log_error { - ($($arg:tt)*) => {{ - $crate::exports::logging::log_error(&format!($($arg)*)) - }} - } + ($($arg:tt)*) => {{ + $crate::exports::logging::log_error(&format!($($arg)*)) + }} + } pub use __log_error as log_error_macro; } diff --git a/core_resource/src/helpers.rs b/core_resource/src/helpers.rs index fd3db615..418efdf9 100644 --- a/core_resource/src/helpers.rs +++ b/core_resource/src/helpers.rs @@ -111,13 +111,13 @@ impl IntoHash for structs::AmmoType { macro_rules! __get_any_option_base_object { ($get_ptr:expr, $base_obj_manager:ident) => { paste::paste! { { - let ptr = unsafe { $get_ptr }; - let Some(ptr) = std::ptr::NonNull::new(ptr) else { - return Ok(None); - }; - Ok($crate::resource::Resource::with_base_objects_ref(|v, _| { - v.[<$base_obj_manager>].get_by_ptr(ptr) - })) + let ptr = unsafe { $get_ptr }; + let Some(ptr) = std::ptr::NonNull::new(ptr) else { + return Ok(None); + }; + Ok($crate::resource::Resource::with_base_objects_ref(|v, _| { + v.[<$base_obj_manager>].get_by_ptr(ptr) + })) } } }; } @@ -220,25 +220,25 @@ pub fn get_ped(ptr: *mut sdk::alt::IPed, resource: &Resource) -> Option { - $code - }; - (($( $target:tt )+) $code:block) => { - $( $target )+ - }; + (() $code:block) => { + $code + }; + (($( $target:tt )+) $code:block) => { + $( $target )+ + }; } pub use __if_not as if_not; #[macro_export] macro_rules! __base_ptr_to { - ($base_ptr:expr, $target_type:ident) => { - paste::paste! { - // TODO: remove unsafe block from here, its not useful at all - unsafe { - std::ptr::NonNull::new($crate::sdk::base_object::[]($base_ptr)).unwrap() - } - } - }; + ($base_ptr:expr, $target_type:ident) => { + paste::paste! { + // TODO: remove unsafe block from here, its not useful at all + unsafe { + std::ptr::NonNull::new($crate::sdk::base_object::[]($base_ptr)).unwrap() + } + } + }; } pub use __base_ptr_to as base_ptr_to; @@ -268,15 +268,15 @@ pub fn read_cpp_base_object_vec( macro_rules! __create_base_object { ($namespace:path, $creation:expr, $else:expr) => {{ paste::paste! { - let ptr = $crate::resource::Resource::with_pending_base_object_destroy_or_creation_mut( - |_, _| unsafe { $creation }, - ); + let ptr = $crate::resource::Resource::with_pending_base_object_destroy_or_creation_mut( + |_, _| unsafe { $creation }, + ); - let Some(ptr) = std::ptr::NonNull::new(ptr) else { - $else - }; + let Some(ptr) = std::ptr::NonNull::new(ptr) else { + $else + }; - $namespace::add_to_pool!(ptr) + $namespace::add_to_pool!(ptr) } }}; } diff --git a/core_resource/src/lib.rs b/core_resource/src/lib.rs index 969cffdf..a36f4e4a 100644 --- a/core_resource/src/lib.rs +++ b/core_resource/src/lib.rs @@ -59,7 +59,7 @@ pub fn init( macro_rules! set_callback { ($name:ident, $closure:expr) => { paste::paste! { - resource_handlers.[<$name>].replace(Box::new($closure)); + resource_handlers.[<$name>].replace(Box::new($closure)); } }; } diff --git a/core_resource/src/meta/base_object/entry.rs b/core_resource/src/meta/base_object/entry.rs index 666b4e11..6a8deadc 100644 --- a/core_resource/src/meta/base_object/entry.rs +++ b/core_resource/src/meta/base_object/entry.rs @@ -58,90 +58,90 @@ pub trait BaseObjectMetaEntry { } macro_rules! impl_base_object_meta_entry { - ( - $meta_type:ident, - $entry_struct:path, - $sdk_namespace:path, - $raw_ptr:expr, - $( - @generics: [ $( - $generic_param:ident $(: $generic_param_trait:ty )?, - )+ ] - )? - ) => { - paste::paste! { - impl < - V: Serialize + DeserializeOwned, - $( $( $generic_param $( : $generic_param_trait )?, )+ )? - > BaseObjectMetaEntry for $entry_struct< - V, - $( $( $generic_param, )+ )? - > { - fn has(&self) -> SomeResult { - Ok(unsafe { $sdk_namespace::[]($raw_ptr(&self.base_object)?, &self.key) }) - } - - fn get(&self) -> SomeResult> { - let raw_ptr = $raw_ptr(&self.base_object)?; - - let mvalue = unsafe { - $sdk_namespace::[](raw_ptr, &self.key) - }.within_unique_ptr(); - let mvalue = mvalue::ConstMValue::new(mvalue); - - let deserialized: Option = mvalue::from_mvalue(&mvalue)?; - Ok(deserialized) - } - - fn get_or_set(&self, value: V) -> SomeResult { - let current_value: Option = self.get()?; - if let Some(v) = current_value { - Ok(v) - } else { - self.set(&value)?; - return Ok(value); - } - } - - fn set(&self, value: &V) -> VoidResult { - unsafe { - $sdk_namespace::[]( - $raw_ptr(&self.base_object)?, - &self.key, - mvalue::to_mvalue(value)?.get(), - ) - } - Ok(()) - } - - fn delete(&self) -> VoidResult { - unsafe { - $sdk_namespace::[]( - $raw_ptr(&self.base_object)?, - &self.key - ) - } - Ok(()) - } - } + ( + $meta_type:ident, + $entry_struct:path, + $sdk_namespace:path, + $raw_ptr:expr, + $( + @generics: [ $( + $generic_param:ident $(: $generic_param_trait:ty )?, + )+ ] + )? + ) => { + paste::paste! { + impl < + V: Serialize + DeserializeOwned, + $( $( $generic_param $( : $generic_param_trait )?, )+ )? + > BaseObjectMetaEntry for $entry_struct< + V, + $( $( $generic_param, )+ )? + > { + fn has(&self) -> SomeResult { + Ok(unsafe { $sdk_namespace::[]($raw_ptr(&self.base_object)?, &self.key) }) } - }; + + fn get(&self) -> SomeResult> { + let raw_ptr = $raw_ptr(&self.base_object)?; + + let mvalue = unsafe { + $sdk_namespace::[](raw_ptr, &self.key) + }.within_unique_ptr(); + let mvalue = mvalue::ConstMValue::new(mvalue); + + let deserialized: Option = mvalue::from_mvalue(&mvalue)?; + Ok(deserialized) + } + + fn get_or_set(&self, value: V) -> SomeResult { + let current_value: Option = self.get()?; + if let Some(v) = current_value { + Ok(v) + } else { + self.set(&value)?; + return Ok(value); + } + } + + fn set(&self, value: &V) -> VoidResult { + unsafe { + $sdk_namespace::[]( + $raw_ptr(&self.base_object)?, + &self.key, + mvalue::to_mvalue(value)?.get(), + ) + } + Ok(()) + } + + fn delete(&self) -> VoidResult { + unsafe { + $sdk_namespace::[]( + $raw_ptr(&self.base_object)?, + &self.key + ) + } + Ok(()) + } + } + } + }; } impl_base_object_meta_entry!( - Meta, - NormalBaseObjectMetaEntry, - sdk::IBaseObject, - BaseObjectWrapper::raw_base_ptr, - @generics: [T, InheritPtrs: Clone,] + Meta, + NormalBaseObjectMetaEntry, + sdk::IBaseObject, + BaseObjectWrapper::raw_base_ptr, + @generics: [T, InheritPtrs: Clone,] ); impl_base_object_meta_entry!( - SyncedMeta, - SyncedBaseObjectMetaEntry, - sdk::IBaseObject, - BaseObjectWrapper::raw_base_ptr, - @generics: [T, InheritPtrs: Clone,] + SyncedMeta, + SyncedBaseObjectMetaEntry, + sdk::IBaseObject, + BaseObjectWrapper::raw_base_ptr, + @generics: [T, InheritPtrs: Clone,] ); impl_base_object_meta_entry!( diff --git a/core_resource/src/resource.rs b/core_resource/src/resource.rs index e0552000..2173d21a 100644 --- a/core_resource/src/resource.rs +++ b/core_resource/src/resource.rs @@ -8,8 +8,8 @@ use core_shared::{ModuleHandlers, StringResourceName}; use crate::{alt_resource, base_objects, events, script_events, timers}; thread_local! { - pub static RESOURCE: Rc>> = - Rc::new(RefCell::new(None)); + pub static RESOURCE: Rc>> = + Rc::new(RefCell::new(None)); } #[derive(Debug, Default)] @@ -32,14 +32,14 @@ pub struct Resource { macro_rules! with_resource { ($func:expr, $property_name:ident, $borrow_func:ident) => { paste::paste! { - RESOURCE.with(|resource| { - let resource = resource.borrow(); - let resource = resource.as_ref().unwrap(); - let manager = resource.[<$property_name>].[<$borrow_func>]().unwrap_or_else(|_| { - panic!("Failed to {} `{}`", stringify!($borrow_func), stringify!($property_name)); - }); - $func(manager, resource) - }) + RESOURCE.with(|resource| { + let resource = resource.borrow(); + let resource = resource.as_ref().unwrap(); + let manager = resource.[<$property_name>].[<$borrow_func>]().unwrap_or_else(|_| { + panic!("Failed to {} `{}`", stringify!($borrow_func), stringify!($property_name)); + }); + $func(manager, resource) + }) } }; } @@ -47,12 +47,12 @@ macro_rules! with_resource { macro_rules! impl_borrow_fn { ($property_name:ident, $full_path:path) => { paste::paste! { - pub fn [](f: F) -> R - where - F: FnOnce(Ref<$full_path>, &Resource) -> R, - { - with_resource!(f, $property_name, try_borrow) - } + pub fn [](f: F) -> R + where + F: FnOnce(Ref<$full_path>, &Resource) -> R, + { + with_resource!(f, $property_name, try_borrow) + } } }; } @@ -60,12 +60,12 @@ macro_rules! impl_borrow_fn { macro_rules! impl_borrow_mut_fn { ($property_name:ident, $full_path:path) => { paste::paste! { - pub fn [](f: F) -> R - where - F: FnOnce(RefMut<$full_path>, &Resource) -> R, - { - with_resource!(f, $property_name, try_borrow_mut) - } + pub fn [](f: F) -> R + where + F: FnOnce(RefMut<$full_path>, &Resource) -> R, + { + with_resource!(f, $property_name, try_borrow_mut) + } } }; } diff --git a/mvalue/src/helpers.rs b/mvalue/src/helpers.rs index cfe094c3..332a433a 100644 --- a/mvalue/src/helpers.rs +++ b/mvalue/src/helpers.rs @@ -17,35 +17,35 @@ pub use __serialize_simple as serialize_simple; #[macro_export] macro_rules! __deserialize_simple { - ( - $self:ident, - $visitor:expr, - @sdk $sdk_type:ident: @rust $rust_type:ident - $(, $convert_method:ident )? - ) => {{ - paste::paste! { - $self.assert_mvalue_type($self.mvalue_type()?, altv_sdk::MValueType::$sdk_type)?; - let raw = unsafe { altv_sdk::ffi::[]($self.input.get()) }; - $visitor.[](raw $( . $convert_method () )?) - } - }}; + ( + $self:ident, + $visitor:expr, + @sdk $sdk_type:ident: @rust $rust_type:ident + $(, $convert_method:ident )? + ) => {{ + paste::paste! { + $self.assert_mvalue_type($self.mvalue_type()?, altv_sdk::MValueType::$sdk_type)?; + let raw = unsafe { altv_sdk::ffi::[]($self.input.get()) }; + $visitor.[](raw $( . $convert_method () )?) + } + }}; } pub use __deserialize_simple as deserialize_simple; #[macro_export] macro_rules! __deserialize_simple_unchecked { - ( - $self:ident, - $visitor:expr, - @sdk $sdk_type:ident: @rust $rust_type:ident - $(, $convert_method:ident )? - ) => {{ - paste::paste! { - let raw = unsafe { altv_sdk::ffi::[]($self.input.get()) }; - $visitor.[](raw $( . $convert_method () )?) - } - }}; + ( + $self:ident, + $visitor:expr, + @sdk $sdk_type:ident: @rust $rust_type:ident + $(, $convert_method:ident )? + ) => {{ + paste::paste! { + let raw = unsafe { altv_sdk::ffi::[]($self.input.get()) }; + $visitor.[](raw $( . $convert_method () )?) + } + }}; } pub use __deserialize_simple_unchecked as deserialize_simple_unchecked; @@ -53,13 +53,13 @@ pub use __deserialize_simple_unchecked as deserialize_simple_unchecked; #[macro_export] macro_rules! __generate_serde_via_bytes_for { ( - $value_type:ty, - $expecting_value:literal, - $serialization_key:path, - $module_name:ident, - $serialize_fields:expr, - $deserialize_byte_buf:expr - ) => { + $value_type:ty, + $expecting_value:literal, + $serialization_key:path, + $module_name:ident, + $serialize_fields:expr, + $deserialize_byte_buf:expr + ) => { mod $module_name { use super::*; diff --git a/test/rust_resource/src/core_funcs.rs b/test/rust_resource/src/core_funcs.rs index ed821c90..4458b212 100644 --- a/test/rust_resource/src/core_funcs.rs +++ b/test/rust_resource/src/core_funcs.rs @@ -3,9 +3,9 @@ use altv::{AmmoType, VoiceConnectionState}; macro_rules! test_property { ($name:path, $value:expr) => { paste::paste! { - dbg!(altv::[<$name>]()); - altv::[]($value); - assert_eq!(dbg!(altv::[<$name>]()), $value); + dbg!(altv::[<$name>]()); + altv::[]($value); + assert_eq!(dbg!(altv::[<$name>]()), $value); } }; } diff --git a/test/rust_resource/src/mvalue.rs b/test/rust_resource/src/mvalue.rs index 077c6c3e..e36a5a04 100644 --- a/test/rust_resource/src/mvalue.rs +++ b/test/rust_resource/src/mvalue.rs @@ -1,52 +1,52 @@ use altv::mvalue::{from_mvalue, to_mvalue}; macro_rules! to_and_from { - (@internal - $( - $deserialize_type:ty: $( $serialize:expr $( => $eq:expr )? ),+ ; - )+ - ) => { - $( $( - altv::log!( - "ser: {} expected: {}", - stringify!($serialize), - stringify!($deserialize_type) - ); - - let serialized_value = $serialize; - - let mvalue = to_mvalue(&serialized_value).unwrap(); - let deserialized: $deserialize_type = from_mvalue(&mvalue.into_const()).unwrap(); - altv::log!( - "de: {:?}, expected: {:?}", - deserialized, - serialized_value - ); - - #[allow(clippy::redundant_closure_call)] - $( $eq(deserialized, serialized_value); )? - )+ )+ - }; - - (@assert_eq $( - $deserialize_type:ty: $( $serialize:expr ),+ ; - )+) => { - to_and_from!(@internal - $( - $deserialize_type: $( $serialize => |a, b| { assert_eq!(a, b) } ),+ ; - )+ - ); - }; - - (@custom_eq $( - @eq $custom_eq:expr, $deserialize_type:ty: $( $serialize:expr ),+ ; - )+) => { - to_and_from!(@internal - $( - $deserialize_type: $( $serialize => $custom_eq ),+ ; - )+ - ); - }; + (@internal + $( + $deserialize_type:ty: $( $serialize:expr $( => $eq:expr )? ),+ ; + )+ + ) => { + $( $( + altv::log!( + "ser: {} expected: {}", + stringify!($serialize), + stringify!($deserialize_type) + ); + + let serialized_value = $serialize; + + let mvalue = to_mvalue(&serialized_value).unwrap(); + let deserialized: $deserialize_type = from_mvalue(&mvalue.into_const()).unwrap(); + altv::log!( + "de: {:?}, expected: {:?}", + deserialized, + serialized_value + ); + + #[allow(clippy::redundant_closure_call)] + $( $eq(deserialized, serialized_value); )? + )+ )+ + }; + + (@assert_eq $( + $deserialize_type:ty: $( $serialize:expr ),+ ; + )+) => { + to_and_from!(@internal + $( + $deserialize_type: $( $serialize => |a, b| { assert_eq!(a, b) } ),+ ; + )+ + ); + }; + + (@custom_eq $( + @eq $custom_eq:expr, $deserialize_type:ty: $( $serialize:expr ),+ ; + )+) => { + to_and_from!(@internal + $( + $deserialize_type: $( $serialize => $custom_eq ),+ ; + )+ + ); + }; } pub(crate) fn test_mvalue() { @@ -72,59 +72,59 @@ pub(crate) fn test_mvalue() { } to_and_from!(@assert_eq - bool: true, false; + bool: true, false; - i8: i8::MAX, i8::MIN; - i16: i16::MAX, i16::MIN; - i32: i32::MAX, i32::MIN; - i64: i64::MAX, i64::MIN; + i8: i8::MAX, i8::MIN; + i16: i16::MAX, i16::MIN; + i32: i32::MAX, i32::MIN; + i64: i64::MAX, i64::MIN; - u8: u8::MAX, u8::MIN; - u16: u16::MAX, u16::MIN; - u32: u32::MAX, u32::MIN; - u64: u64::MAX, u64::MIN; + u8: u8::MAX, u8::MIN; + u16: u16::MAX, u16::MIN; + u32: u32::MAX, u32::MIN; + u64: u64::MAX, u64::MIN; - f32: f32::MAX, f32::MIN; - f64: f64::MAX, f64::MIN; + f32: f32::MAX, f32::MIN; + f64: f64::MAX, f64::MIN; - Option: Some(true), Some(false), Option::::None; + Option: Some(true), Some(false), Option::::None; - String: "123456789AWjdkrOtignmzxcnb;l^)(%$*&@#^!$#)(%$?>0Да".to_string(), "123456789AWjdkrOtignmzxcnb;l^)(%$*&@#^!$#)(%$?>0Да"; + String: "123456789AWjdkrOtignmzxcnb;l^)(%$*&@#^!$#)(%$?>0Да".to_string(), "123456789AWjdkrOtignmzxcnb;l^)(%$*&@#^!$#)(%$?>0Да"; - Vec: vec![1, 2, 3]; - Vec: vec![true, false, true]; - Vec: vec!["123456789AWjdkrOtignmzxcnb;l^)(%$*&@#^!$#)(%$?>0Да".to_string()]; + Vec: vec![1, 2, 3]; + Vec: vec![true, false, true]; + Vec: vec!["123456789AWjdkrOtignmzxcnb;l^)(%$*&@#^!$#)(%$?>0Да".to_string()]; - altv::ByteBuf: altv::ByteBuf::from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, u8::MAX]); + altv::ByteBuf: altv::ByteBuf::from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, u8::MAX]); - altv::Rgba: altv::Rgba::new(u8::MAX, u8::MAX, u8::MAX, u8::MAX); + altv::Rgba: altv::Rgba::new(u8::MAX, u8::MAX, u8::MAX, u8::MAX); - altv::Vector3: altv::Vector3::new(f32::MAX, f32::MIN, 123); - altv::Vector2: altv::Vector2::new(f32::MAX, f32::MIN); - altv::Vector2: altv::Vector2::new(f32::MAX, 123); + altv::Vector3: altv::Vector3::new(f32::MAX, f32::MIN, 123); + altv::Vector2: altv::Vector2::new(f32::MAX, f32::MIN); + altv::Vector2: altv::Vector2::new(f32::MAX, 123); - altv::ColShapeContainer: altv::ColShape::new_circle(0, 10.0); + altv::ColShapeContainer: altv::ColShape::new_circle(0, 10.0); - TestEnum: TestEnum::Unit; - TestEnum: TestEnum::Newtype(123); - TestEnum: TestEnum::Tuple( - i32::MAX, - true, - String::from("wdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwdd"), - ); - TestEnum: TestEnum::Struct { a: 123, b: true }; - - TestEnumUntagged: TestEnumUntagged::Unit; - TestEnumUntagged: TestEnumUntagged::Newtype(123); - TestEnumUntagged: TestEnumUntagged::Tuple( - i32::MAX, - true, - String::from("wdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwdd"), - ); - TestEnumUntagged: TestEnumUntagged::Struct { a: 123, b: true }; + TestEnum: TestEnum::Unit; + TestEnum: TestEnum::Newtype(123); + TestEnum: TestEnum::Tuple( + i32::MAX, + true, + String::from("wdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwdd"), + ); + TestEnum: TestEnum::Struct { a: 123, b: true }; + + TestEnumUntagged: TestEnumUntagged::Unit; + TestEnumUntagged: TestEnumUntagged::Newtype(123); + TestEnumUntagged: TestEnumUntagged::Tuple( + i32::MAX, + true, + String::from("wdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwddwdwdwdd"), + ); + TestEnumUntagged: TestEnumUntagged::Struct { a: 123, b: true }; - // TODO: - // altv::AnyBaseObject: altv::VoiceChannel::new_spatial(0.0).unwrap(); + // TODO: + // altv::AnyBaseObject: altv::VoiceChannel::new_spatial(0.0).unwrap(); ); // unit variant @@ -198,8 +198,8 @@ pub(crate) fn test_mvalue() { } // to_and_from!(@custom_eq - // @eq |a: altv::Vector3, b: altv::Vector3| a.x() == b.x(), altv::Vector3: altv::Vector3::new(f32::MAX, f32::MIN, 123); - // @eq |a: altv::Vector2, b: altv::Vector2| a.x() == b.x(), altv::Vector2: altv::Vector2::new(f32::MAX, f32::MIN); - // @eq |a: altv::Vector2, b: altv::Vector2| a.x() == b.x(), altv::Vector2: altv::Vector2::new(f32::MAX, 123); + // @eq |a: altv::Vector3, b: altv::Vector3| a.x() == b.x(), altv::Vector3: altv::Vector3::new(f32::MAX, f32::MIN, 123); + // @eq |a: altv::Vector2, b: altv::Vector2| a.x() == b.x(), altv::Vector2: altv::Vector2::new(f32::MAX, f32::MIN); + // @eq |a: altv::Vector2, b: altv::Vector2| a.x() == b.x(), altv::Vector2: altv::Vector2::new(f32::MAX, 123); // ); }