Skip to content

Commit

Permalink
Merge branch 'dev' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
xxshady committed Aug 19, 2024
2 parents ae97450 + 4a94ec2 commit 62b37a0
Show file tree
Hide file tree
Showing 34 changed files with 384 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@
"LOG_LEVEL": "debug"
},
"rust-analyzer.check.command": "clippy",
"rust-analyzer.showUnlinkedFileNotification": false
"rust-analyzer.showUnlinkedFileNotification": false,
"rust-analyzer.cargo.features": "all"
}
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions altv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ core_resource = { path = "../core_resource", package = "altv_internal_core_resou

[package.metadata.docs.rs]
rustdoc-args = ["--generate-link-to-definition"]

[features]
# Enable support for clientside JS WASM API
clientside = ["core_resource/clientside"]
4 changes: 4 additions & 0 deletions altv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ pub use exports::{
ByteBuf,
// Bytes TODO: implement bytes deserialization
},
enumflags2,

MValueHashMap,
BaseObjectFilter,
ClosestEntitiesOrder,
};

pub use altv_sdk::{
Expand Down
2 changes: 1 addition & 1 deletion altv_sdk/cpp-sdk
2 changes: 1 addition & 1 deletion altv_sdk/cpp-sdk-version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define ALT_SDK_VERSION "22e1fdd"
#define ALT_SDK_VERSION "b1ced94"
1 change: 1 addition & 0 deletions altv_sdk/src/alt_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using AmmoSpecialType_t = uint32_t;
using VoiceConnectionState = uint8_t;
using CloudAuthResult_t = uint8_t;
using Benefit_t = uint8_t;
using ClosestEntitiesOrder_t = uint8_t;

// used for const std::string& return values in altv event classes
using StdStringClone = std::string;
Expand Down
4 changes: 2 additions & 2 deletions altv_sdk/src/alt_classes/ICore.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ BaseObjectVector GetEntitiesInRange(f32 position_x, f32 position_y, f32 position
}
return vec;
}
BaseObjectVector GetClosestEntities(f32 position_x, f32 position_y, f32 position_z, i32 range, i32 dimension, i32 limit, u64 allowedTypes) {
auto alt_vec = alt::ICore::Instance().GetClosestEntities({ position_x, position_y, position_z }, range, dimension, limit, allowedTypes);
BaseObjectVector GetClosestEntities(f32 position_x, f32 position_y, f32 position_z, i32 range, i32 dimension, i32 limit, u64 allowedTypes, ClosestEntitiesOrder_t sortOrder) {
auto alt_vec = alt::ICore::Instance().GetClosestEntities({ position_x, position_y, position_z }, range, dimension, limit, allowedTypes, static_cast<alt::common::Order>(sortOrder));
BaseObjectVector vec {};
vec.reserve(alt_vec.size());
for (const auto& e : alt_vec) {
Expand Down
5 changes: 5 additions & 0 deletions core_resource/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ paste = { workspace = true }
serde = { workspace = true }
serde_bytes = { workspace = true }
erased-serde = { workspace = true }
enumflags2 = "0.7.10"

[features]
# Enable support for clientside JS WASM API
clientside = []
25 changes: 16 additions & 9 deletions core_resource/src/base_objects/manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{sdk, VoidResult};
use std::{collections::HashMap, fmt::Debug, ptr::NonNull};

use crate::{sdk, VoidResult};
use super::{base_impl::BaseObjectContainer, BaseObjectId};

pub(crate) struct BaseObjectManager<T, InheritPtrs: Clone = ()> {
Expand All @@ -17,18 +16,26 @@ impl<T, InheritPtrs: Clone> BaseObjectManager<T, InheritPtrs> {
) {
self.objects.insert(ptr, base_object.clone());

let id = unsafe { sdk::IBaseObject::GetID(base_ptr.as_ptr()) };
let raw_ptr = base_ptr.as_ptr();

let id = unsafe { sdk::IBaseObject::GetID(raw_ptr) };
self.objects_by_id.insert(id, base_object);

#[cfg(feature = "clientside")]
crate::clientside::handle_base_object_creation(raw_ptr);
}

pub fn remove(&mut self, base_ptr: altv_sdk::BaseObjectMutPtr, ptr: NonNull<T>) -> VoidResult {
logger::debug!("remove ptr: {ptr:?}");
if self.objects.remove(&ptr).is_some() {
self.remove_id(base_ptr);
Ok(())
} else {
anyhow::bail!("unknown base object")
if self.objects.remove(&ptr).is_none() {
anyhow::bail!("unknown base object");
}
logger::debug!("remove ptr: {ptr:?}");
self.remove_id(base_ptr);

#[cfg(feature = "clientside")]
crate::clientside::handle_base_object_destruction();

Ok(())
}

pub fn remove_externally(
Expand Down
98 changes: 98 additions & 0 deletions core_resource/src/clientside.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::cell::Cell;
use altv_sdk::{helpers::get_base_object_type, BaseObjectRawMutPtr, BaseObjectType};
use mvalue::to_mvalue;
use crate::{sdk, helpers::base_ptr_to_raw};

// special key to avoid collisions with user's code
pub const GENERATION_ID_KEY: &str = "&^#altv-rust";

thread_local! {
pub static CURRENT_GENERATION_ID: Cell<u64> = const { Cell::new(1) };
}

pub fn handle_base_object_creation(raw_ptr: BaseObjectRawMutPtr) {
let btype = unsafe { get_base_object_type(raw_ptr) };
logger::debug!("initializing base object type: {btype:?} for clientside");

init_generation_of_created_base_object(raw_ptr, btype);
}

pub fn handle_base_object_destruction() {
let current = CURRENT_GENERATION_ID.get();
let next = current.checked_add(1).unwrap_or_else(|| {
logger::error!(
"Base object generation reached u64::MAX.\n\
Next base object will use non-unique generation.\n\
Consider opening issue in altv-rust repo: https://github.com/xxshady/altv-rust/issues."
);
1
});
CURRENT_GENERATION_ID.set(next);
}

fn init_generation_of_created_base_object(raw_ptr: BaseObjectRawMutPtr, btype: BaseObjectType) {
let generation_id = CURRENT_GENERATION_ID.get();

use BaseObjectType as B;
match btype {
B::Player | B::Vehicle | B::Ped | B::Object => {
let entity = base_ptr_to_raw!(raw_ptr, entity);
unsafe {
sdk::IEntity::SetStreamSyncedMetaData(
entity,
GENERATION_ID_KEY,
to_mvalue(&generation_id).unwrap().get(),
)
}
}
B::VirtualEntity => {
let entity = base_ptr_to_raw!(raw_ptr, virtual_entity);
unsafe {
sdk::IVirtualEntity::SetStreamSyncedMetaData(
entity,
GENERATION_ID_KEY,
to_mvalue(&generation_id).unwrap().get(),
)
}
}
B::Blip => unsafe {
sdk::IBaseObject::SetSyncedMetaData(
raw_ptr,
GENERATION_ID_KEY,
to_mvalue(&generation_id).unwrap().get(),
)
},
B::Checkpoint => {
let checkpoint = base_ptr_to_raw!(raw_ptr, checkpoint);
unsafe {
sdk::ICheckpoint::SetStreamSyncedMetaData(
checkpoint,
GENERATION_ID_KEY,
to_mvalue(&generation_id).unwrap().get(),
)
}
}
B::ConnectionInfo | B::Marker | B::VirtualEntityGroup | B::Colshape | B::VoiceChannel => {
logger::debug!("skipping {btype:?}");
}
B::Size
| B::Webview
| B::RmlElement
| B::RmlDocument
| B::LocalPlayer
| B::LocalObject
| B::TextLabel
| B::LocalPed
| B::LocalVehicle
| B::Font
| B::CustomTexture
| B::Audio
| B::AudioFilter
| B::AudioOutput
| B::AudioOutputAttached
| B::AudioOutputFrontend
| B::AudioOutputWorld
| B::HttpClient
| B::WebsocketClient => unreachable!(),
}
}
6 changes: 1 addition & 5 deletions core_resource/src/config_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,7 @@ fn get_string_list_required(node: ConfigNode) -> Vec<String> {
}

fn get_string_list(node: Option<ConfigNode>) -> Option<Vec<String>> {
let Some(list) = node else {
return None;
};

let list = list.as_list().unwrap();
let list = node?.as_list().unwrap();
Some(list.into_iter().map(|v| v.as_string().unwrap()).collect())
}

Expand Down
70 changes: 64 additions & 6 deletions core_resource/src/core_funcs.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
use crate::structs::{BaseObjectFilter, ClosestEntitiesOrder};
use enumflags2::BitFlags;

use crate::{
base_objects::AnyBaseObject,
helpers::{self, IntoHash, Hash},
sdk,
vector::Vector3,
};

/// # Examples
/// Get entity of any type
/// ```rust
/// # mod altv {
/// # pub use altv_internal_core_resource::exports::{*, core_funcs::*};
/// # }
/// # fn test() -> altv::VoidResult {
/// use altv::enumflags2::BitFlag;
///
/// let any_entities = altv::get_closest_entities(
/// 0,
/// 100,
/// 0,
/// 10,
/// altv::BaseObjectFilter::all(),
/// Default::default(),
/// );
/// # Ok(()) }
/// ```
///
/// Get only vehicles and peds
/// ```rust
/// # mod altv {
/// # pub use altv_internal_core_resource::exports::{*, core_funcs::*};
/// # }
/// # fn test() -> altv::VoidResult {
/// use altv::enumflags2::BitFlag;
///
/// let vehicles_and_peds = altv::get_closest_entities(
/// 0,
/// 100,
/// 0,
/// 10,
/// altv::BaseObjectFilter::Vehicle | altv::BaseObjectFilter::Ped,
/// Default::default(),
/// );
/// # Ok(()) }
/// ```
pub fn get_closest_entities(
pos: impl Into<Vector3>,
range: i32,
dimension: i32,
limit: i32,
allowed_types: u64,
allowed_types: impl Into<BitFlags<BaseObjectFilter>>,
order: ClosestEntitiesOrder,
) -> Vec<AnyBaseObject> {
let pos = pos.into();
let allowed_types = allowed_types.into();
let raw = unsafe {
sdk::ICore::GetClosestEntities(
pos.x(),
Expand All @@ -21,26 +64,41 @@ pub fn get_closest_entities(
range,
dimension,
limit,
allowed_types,
allowed_types.bits(),
order as u8,
)
};
helpers::read_cpp_base_object_vec(raw)
}

pub fn get_entities_in_dimension(dimension: i32, allowed_types: u64) -> Vec<AnyBaseObject> {
let raw = unsafe { sdk::ICore::GetEntitiesInDimension(dimension, allowed_types) };
/// For examples see [`get_closest_entities`]
pub fn get_entities_in_dimension(
dimension: i32,
allowed_types: impl Into<BitFlags<BaseObjectFilter>>,
) -> Vec<AnyBaseObject> {
let allowed_types = allowed_types.into();
let raw = unsafe { sdk::ICore::GetEntitiesInDimension(dimension, allowed_types.bits()) };
helpers::read_cpp_base_object_vec(raw)
}

/// For examples see [`get_closest_entities`]
pub fn get_entities_in_range(
pos: impl Into<Vector3>,
range: i32,
dimension: i32,
allowed_types: u64,
allowed_types: impl Into<BitFlags<BaseObjectFilter>>,
) -> Vec<AnyBaseObject> {
let pos = pos.into();
let allowed_types = allowed_types.into();
let raw = unsafe {
sdk::ICore::GetEntitiesInRange(pos.x(), pos.y(), pos.z(), range, dimension, allowed_types)
sdk::ICore::GetEntitiesInRange(
pos.x(),
pos.y(),
pos.z(),
range,
dimension,
allowed_types.bits(),
)
};
helpers::read_cpp_base_object_vec(raw)
}
Expand Down
4 changes: 1 addition & 3 deletions core_resource/src/events/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ pub fn get_base_object_from_event(
ptr: altv_sdk::BaseObjectRawMutPtr,
resource: &Resource,
) -> Option<AnyBaseObject> {
let Some(ptr) = NonNull::new(ptr) else {
return None;
};
let ptr = NonNull::new(ptr)?;
let base_object = resource.base_objects.borrow().get_by_ptr(ptr).unwrap();
Some(base_object)
}
Expand Down
4 changes: 3 additions & 1 deletion core_resource/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use anyhow;
pub use erased_serde;
pub use serde;
pub use serde_bytes;
pub use enumflags2;

pub use core_shared::*;

Expand Down Expand Up @@ -31,7 +32,8 @@ pub use crate::{
rgba::Rgba,
structs::{
AmmoType, AnimationFlags, AttachToEntityBoneIndex, AttachToEntityBoneName, PlayAnimation,
PlayerDateTime, PlayerHeadBlendData, AmmoFlags, Decoration,
PlayerDateTime, PlayerHeadBlendData, AmmoFlags, Decoration, BaseObjectFilter,
ClosestEntitiesOrder,
},
timers::{create_timer, remove_timer, Timer},
vector::{Vector2, Vector3},
Expand Down
Loading

0 comments on commit 62b37a0

Please sign in to comment.