tirbofish/dropbear · diff
fix: fix up some errors relating to scripting module
Signature present but could not be verified.
Unverified
@@ -30,7 +30,7 @@ impl<T> Handle<T> { /// Creates a null handle, for when there is no way to uniquely identify a hash (such as a viewport texture). /// /// # Safety - /// You will want to watch out, as adding this onto the asset registry with a type + /// You will want to watch out, as adding this onto the asset_old registry with a type /// where there already is a null handle item, it will be overwritten and data /// will not be saved. It is the reason why you will want to consider using the [Self::is_null] /// function to verify if the storage of the type has gone through correctly. @@ -79,14 +79,14 @@ impl AssetRegistry { hasher.finish() } - /// Checks if the asset registry contains a handle with the given hash. + /// Checks if the asset_old registry contains a handle with the given hash. /// /// It will check all different types, so it does not point out specifically where. pub fn contains_hash(&self, hash: u64) -> bool { self.textures.contains_key(&hash) || self.models.contains_key(&hash) } - /// Checks if the asset registry contains a handle with the given string label. + /// Checks if the asset_old registry contains a handle with the given string label. /// /// It will check all different types, so it does not point out specifically where. pub fn contains_label(&self, label: &str) -> bool { @@ -126,7 +126,7 @@ impl AssetRegistry { self.texture_labels.remove(label); } - /// Updates the asset server by inserting the texture provided at the location of the handle, + /// Updates the asset_old server by inserting the texture provided at the location of the handle, /// and removing the old texture (by returning it back to you). pub fn update_texture(&mut self, handle: Handle<Texture>, texture: Texture) -> Option<Texture> { self.textures.insert(handle.id, texture) @@ -181,7 +181,7 @@ impl ResourceReference { }) } - /// Returns the canonical euca URI for this reference if it points to a file asset. + /// Returns the canonical euca URI for this reference if it points to a file asset_old. pub fn as_uri(&self) -> Option<&str> { match &self.ref_type { ResourceReferenceType::File(reference) => Some(reference.as_str()), @@ -1,5 +1,3 @@ -pub mod texture; - pub mod shared { use dropbear_engine::asset::{AssetHandle, AssetRegistry}; use crate::scripting::result::DropbearNativeResult; @@ -1,39 +0,0 @@ -pub mod texture; -pub mod model; - -use dropbear_engine::asset::AssetKind; -use crate::pointer_convert; -use crate::ptr::AssetRegistryUnwrapped; -use crate::scripting::result::DropbearNativeResult; - -/** - * Fetches the asset information from the internal AssetRegistry (located in - * `dropbear_engine::asset::AssetRegistry`) from the provided label. - */ -#[uniffi::export] -pub fn dropbear_asset_get_asset( - asset_ptr: u64, - label: String, - kind: AssetKind, -) -> DropbearNativeResult<Option<u64>> { - let asset = pointer_convert!(asset_ptr => AssetRegistryUnwrapped); - let reader = asset.read(); - match kind { - AssetKind::Texture => { - let result = reader.get_texture_handle_from_label(&label); - if let Some(handle) = result { - Ok(Some(handle.id)) - } else { - Ok(None) - } - } - AssetKind::Model => { - let result = reader.get_model_handle_from_label(&label); - if let Some(handle) = result { - Ok(Some(handle.id)) - } else { - Ok(None) - } - } - } -} @@ -1,345 +0,0 @@ -use crate::pointer_convert; -use crate::ptr::AssetRegistryUnwrapped; -use crate::scripting::native::DropbearNativeError; -use crate::scripting::result::DropbearNativeResult; -use crate::types::{NQuaternion, NVector2, NVector3, NVector4}; -use dropbear_engine::asset::Handle; -use dropbear_engine::model::{Animation, AnimationChannel, AnimationInterpolation, ChannelValues, Material, Mesh, ModelVertex, Node, NodeTransform, Skin}; -use dropbear_engine::texture::Texture; - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NModelVertex { - pub position: NVector3, - pub normal: NVector3, - pub tangent: NVector4, - pub tex_coords0: NVector2, - pub tex_coords1: NVector2, - pub colour0: NVector4, - pub joints0: Vec<i32>, - pub weights0: NVector4, -} - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NMesh { - pub name: String, - pub num_elements: i32, - pub material_index: i32, - pub vertices: Vec<NModelVertex>, -} - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NMaterial { - pub name: String, - pub diffuse_texture: u64, - pub normal_texture: u64, - pub tint: NVector4, - pub emissive_factor: NVector3, - pub metallic_factor: f32, - pub roughness_factor: f32, - pub alpha_cutoff: Option<f32>, - pub double_sided: bool, - pub occlusion_strength: f32, - pub normal_scale: f32, - pub uv_tiling: NVector2, - pub emissive_texture: Option<u64>, - pub metallic_roughness_texture: Option<u64>, - pub occlusion_texture: Option<u64>, -} - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NNodeTransform { - pub translation: NVector3, - pub rotation: NQuaternion, - pub scale: NVector3, -} - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NNode { - pub name: String, - pub parent: Option<i32>, - pub children: Vec<i32>, - pub transform: NNodeTransform, -} - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NSkin { - pub name: String, - pub joints: Vec<i32>, - pub inverse_bind_matrices: Vec<Vec<f64>>, - pub skeleton_root: Option<i32>, -} - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NAnimation { - pub name: String, - pub channels: Vec<NAnimationChannel>, - pub duration: f32, -} - -#[derive(Clone, Debug, uniffi::Record)] -pub struct NAnimationChannel { - pub target_node: i32, - pub times: Vec<f64>, - pub values: NChannelValues, - pub interpolation: NAnimationInterpolation, -} - -#[derive(Clone, Debug, uniffi::Enum)] -pub enum NAnimationInterpolation { - Linear, - Step, - CubicSpline, -} - -#[derive(Clone, Debug, uniffi::Enum)] -pub enum NChannelValues { - Translations { values: Vec<NVector3> }, - Rotations { values: Vec<NQuaternion> }, - Scales { values: Vec<NVector3> }, -} - -fn texture_handle_id( - registry: &dropbear_engine::asset::AssetRegistry, - texture: &Texture, -) -> u64 { - texture - .hash() - .and_then(|hash| registry.texture_handle_by_hash(hash).map(|h| h.id)) - .unwrap_or(0) -} - -fn to_nvector3(v: glam::Vec3) -> NVector3 { - NVector3::from([v.x, v.y, v.z]) -} - -fn to_nvector2(v: [f32; 2]) -> NVector2 { - NVector2::from(v) -} - -fn to_nvector4(v: [f32; 4]) -> NVector4 { - NVector4::from(v) -} - -fn to_nquaternion(q: glam::Quat) -> NQuaternion { - NQuaternion { - x: q.x as f64, - y: q.y as f64, - z: q.z as f64, - w: q.w as f64, - } -} - -fn map_vertex(vertex: &ModelVertex) -> NModelVertex { - NModelVertex { - position: NVector3::from(vertex.position), - normal: NVector3::from(vertex.normal), - tangent: to_nvector4(vertex.tangent), - tex_coords0: to_nvector2(vertex.tex_coords0), - tex_coords1: to_nvector2(vertex.tex_coords1), - colour0: to_nvector4(vertex.colour0), - joints0: vertex.joints0.iter().map(|v| *v as i32).collect(), - weights0: to_nvector4(vertex.weights0), - } -} - -fn map_mesh(mesh: &Mesh) -> NMesh { - NMesh { - name: mesh.name.clone(), - num_elements: mesh.num_elements as i32, - material_index: mesh.material as i32, - vertices: mesh.vertices.iter().map(map_vertex).collect(), - } -} - -fn map_material( - registry: &dropbear_engine::asset::AssetRegistry, - material: &Material, -) -> NMaterial { - NMaterial { - name: material.name.clone(), - diffuse_texture: texture_handle_id(registry, &material.diffuse_texture), - normal_texture: texture_handle_id(registry, &material.normal_texture), - tint: to_nvector4(material.tint), - emissive_factor: NVector3::from(material.emissive_factor), - metallic_factor: material.metallic_factor, - roughness_factor: material.roughness_factor, - alpha_cutoff: material.alpha_cutoff, - double_sided: material.double_sided, - occlusion_strength: material.occlusion_strength, - normal_scale: material.normal_scale, - uv_tiling: to_nvector2(material.uv_tiling), - emissive_texture: material - .emissive_texture - .as_ref() - .map(|tex| texture_handle_id(registry, tex)) - .filter(|id| *id != 0), - metallic_roughness_texture: material - .metallic_roughness_texture - .as_ref() - .map(|tex| texture_handle_id(registry, tex)) - .filter(|id| *id != 0), - occlusion_texture: material - .occlusion_texture - .as_ref() - .map(|tex| texture_handle_id(registry, tex)) - .filter(|id| *id != 0), - } -} - -fn map_node_transform(transform: &NodeTransform) -> NNodeTransform { - NNodeTransform { - translation: to_nvector3(transform.translation), - rotation: to_nquaternion(transform.rotation), - scale: to_nvector3(transform.scale), - } -} - -fn map_node(node: &Node) -> NNode { - NNode { - name: node.name.clone(), - parent: node.parent.map(|v| v as i32), - children: node.children.iter().map(|v| *v as i32).collect(), - transform: map_node_transform(&node.transform), - } -} - -fn map_skin(skin: &Skin) -> NSkin { - let inverse_bind_matrices = skin - .inverse_bind_matrices - .iter() - .map(|matrix| matrix.to_cols_array().iter().map(|v| *v as f64).collect()) - .collect(); - - NSkin { - name: skin.name.clone(), - joints: skin.joints.iter().map(|v| *v as i32).collect(), - inverse_bind_matrices, - skeleton_root: skin.skeleton_root.map(|v| v as i32), - } -} - -fn map_interpolation(value: &AnimationInterpolation) -> NAnimationInterpolation { - match value { - AnimationInterpolation::Linear => NAnimationInterpolation::Linear, - AnimationInterpolation::Step => NAnimationInterpolation::Step, - AnimationInterpolation::CubicSpline => NAnimationInterpolation::CubicSpline, - } -} - -fn map_channel_values(values: &ChannelValues) -> NChannelValues { - match values { - ChannelValues::Translations(list) => NChannelValues::Translations { - values: list.iter().map(|v| to_nvector3(*v)).collect(), - }, - ChannelValues::Rotations(list) => NChannelValues::Rotations { - values: list.iter().map(|v| to_nquaternion(*v)).collect(), - }, - ChannelValues::Scales(list) => NChannelValues::Scales { - values: list.iter().map(|v| to_nvector3(*v)).collect(), - }, - } -} - -fn map_animation_channel(channel: &AnimationChannel) -> NAnimationChannel { - NAnimationChannel { - target_node: channel.target_node as i32, - times: channel.times.iter().map(|v| *v as f64).collect(), - values: map_channel_values(&channel.values), - interpolation: map_interpolation(&channel.interpolation), - } -} - -fn map_animation(animation: &Animation) -> NAnimation { - NAnimation { - name: animation.name.clone(), - channels: animation.channels.iter().map(map_animation_channel).collect(), - duration: animation.duration, - } -} - -#[uniffi::export] -pub fn dropbear_asset_model_get_label( - asset_registry: u64, - model_handle: u64, -) -> DropbearNativeResult<String> { - let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); - let label = asset - .read() - .get_label_from_model_handle(Handle::new(model_handle)) - .ok_or_else(|| DropbearNativeError::InvalidHandle)?; - Ok(label) -} - -#[uniffi::export] -pub fn dropbear_asset_model_get_meshes( - asset_registry: u64, - model_handle: u64, -) -> DropbearNativeResult<Vec<NMesh>> { - let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); - let reader = asset.read(); - let model = reader - .get_model(Handle::new(model_handle)) - .ok_or(DropbearNativeError::InvalidHandle)?; - - Ok(model.meshes.iter().map(map_mesh).collect()) -} - -#[uniffi::export] -pub fn dropbear_asset_model_get_materials( - asset_registry: u64, - model_handle: u64, -) -> DropbearNativeResult<Vec<NMaterial>> { - let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); - let reader = asset.read(); - let model = reader - .get_model(Handle::new(model_handle)) - .ok_or(DropbearNativeError::InvalidHandle)?; - - Ok(model - .materials - .iter() - .map(|mat| map_material(&reader, mat)) - .collect()) -} - -#[uniffi::export] -pub fn dropbear_asset_model_get_skins( - asset_registry: u64, - model_handle: u64, -) -> DropbearNativeResult<Vec<NSkin>> { - let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); - let reader = asset.read(); - let model = reader - .get_model(Handle::new(model_handle)) - .ok_or(DropbearNativeError::InvalidHandle)?; - - Ok(model.skins.iter().map(map_skin).collect()) -} - -#[uniffi::export] -pub fn dropbear_asset_model_get_animations( - asset_registry: u64, - model_handle: u64, -) -> DropbearNativeResult<Vec<NAnimation>> { - let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); - let reader = asset.read(); - let model = reader - .get_model(Handle::new(model_handle)) - .ok_or(DropbearNativeError::InvalidHandle)?; - - Ok(model.animations.iter().map(map_animation).collect()) -} - -#[uniffi::export] -pub fn dropbear_asset_model_get_nodes( - asset_registry: u64, - model_handle: u64, -) -> DropbearNativeResult<Vec<NNode>> { - let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); - let reader = asset.read(); - let model = reader - .get_model(Handle::new(model_handle)) - .ok_or(DropbearNativeError::InvalidHandle)?; - - Ok(model.nodes.iter().map(map_node).collect()) -} @@ -1,83 +0,0 @@ -pub mod shared { - use dropbear_engine::asset::{AssetHandle, AssetRegistry}; - use crate::scripting::native::DropbearNativeError; - use crate::scripting::result::DropbearNativeResult; - - pub fn get_texture_name( - asset_registry: &AssetRegistry, - handle: u64, - ) -> DropbearNativeResult<String> { - let texture = asset_registry - .get_material(AssetHandle::new(handle)) - .ok_or_else(|| DropbearNativeError::NoSuchHandle)?; - - Ok(texture.name.clone()) - } -} - -pub mod jni { - #![allow(non_snake_case)] - use jni::JNIEnv; - use jni::sys::{jlong, jstring}; - use dropbear_engine::asset::AssetRegistry; - use jni::objects::JClass; - use crate::asset::texture::shared::get_texture_name; - use crate::scripting::native::DropbearNativeError; - - #[unsafe(no_mangle)] - pub extern "system" fn Java_com_dropbear_asset_TextureHandleNative_getTextureName( - env: JNIEnv, - _class: JClass, - asset_registry_ptr: jlong, - handle: jlong, - ) -> jstring { - let asset_registry = crate::convert_ptr!(asset_registry_ptr => AssetRegistry); - let result = get_texture_name(asset_registry, handle as u64); - match result { - Ok(name) => { - let output = env.new_string(name).map_err(|_| DropbearNativeError::JNIFailedToCreateObject); - match output { - Ok(jstr) => jstr.into_raw(), - Err(e) => { - crate::ffi_error_return!("[ERROR] Failed to create Java string: {}", e) - } - } - } - Err(e) => { - crate::ffi_error_return!("[ERROR] {}", e) - } - } - } -} - -#[dropbear_macro::impl_c_api] -pub mod native { - use std::ffi::CString; - use crate::ptr::AssetRegistryPtr; - use crate::scripting::native::DropbearNativeError; - use crate::scripting::result::DropbearNativeResult; - use std::ffi::c_char; - use dropbear_engine::asset::AssetRegistry; - use crate::asset::texture::shared::get_texture_name; - - pub fn dropbear_get_texture_name( - asset_registry_ptr: AssetRegistryPtr, - handle: u64, - ) -> DropbearNativeResult<*mut c_char> { - let asset_registry = crate::convert_ptr!(asset_registry_ptr => AssetRegistry); - - let result = get_texture_name(asset_registry, handle) - .map(|name| { - match CString::new(name) { - Ok(c_str) => { - Ok(c_str.into_raw()) - }, - Err(_) => { - Err(DropbearNativeError::CStringError) - } - } - })?; - - result - } -} @@ -0,0 +1,39 @@ +pub mod texture; +pub mod model; + +use dropbear_engine::asset::AssetKind; +use crate::pointer_convert; +use crate::ptr::AssetRegistryUnwrapped; +use crate::scripting::result::DropbearNativeResult; + +/** + * Fetches the asset_old information from the internal AssetRegistry (located in + * `dropbear_engine::asset_old::AssetRegistry`) from the provided label. + */ +#[uniffi::export] +pub fn dropbear_asset_get_asset( + asset_ptr: u64, + label: String, + kind: AssetKind, +) -> DropbearNativeResult<Option<u64>> { + let asset = pointer_convert!(asset_ptr => AssetRegistryUnwrapped); + let reader = asset.read(); + match kind { + AssetKind::Texture => { + let result = reader.get_texture_handle_from_label(&label); + if let Some(handle) = result { + Ok(Some(handle.id)) + } else { + Ok(None) + } + } + AssetKind::Model => { + let result = reader.get_model_handle_from_label(&label); + if let Some(handle) = result { + Ok(Some(handle.id)) + } else { + Ok(None) + } + } + } +} @@ -0,0 +1,345 @@ +use crate::pointer_convert; +use crate::ptr::AssetRegistryUnwrapped; +use crate::scripting::native::DropbearNativeError; +use crate::scripting::result::DropbearNativeResult; +use crate::types::{NQuaternion, NVector2, NVector3, NVector4}; +use dropbear_engine::asset::Handle; +use dropbear_engine::model::{Animation, AnimationChannel, AnimationInterpolation, ChannelValues, Material, Mesh, ModelVertex, Node, NodeTransform, Skin}; +use dropbear_engine::texture::Texture; + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NModelVertex { + pub position: NVector3, + pub normal: NVector3, + pub tangent: NVector4, + pub tex_coords0: NVector2, + pub tex_coords1: NVector2, + pub colour0: NVector4, + pub joints0: Vec<i32>, + pub weights0: NVector4, +} + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NMesh { + pub name: String, + pub num_elements: i32, + pub material_index: i32, + pub vertices: Vec<NModelVertex>, +} + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NMaterial { + pub name: String, + pub diffuse_texture: u64, + pub normal_texture: u64, + pub tint: NVector4, + pub emissive_factor: NVector3, + pub metallic_factor: f32, + pub roughness_factor: f32, + pub alpha_cutoff: Option<f32>, + pub double_sided: bool, + pub occlusion_strength: f32, + pub normal_scale: f32, + pub uv_tiling: NVector2, + pub emissive_texture: Option<u64>, + pub metallic_roughness_texture: Option<u64>, + pub occlusion_texture: Option<u64>, +} + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NNodeTransform { + pub translation: NVector3, + pub rotation: NQuaternion, + pub scale: NVector3, +} + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NNode { + pub name: String, + pub parent: Option<i32>, + pub children: Vec<i32>, + pub transform: NNodeTransform, +} + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NSkin { + pub name: String, + pub joints: Vec<i32>, + pub inverse_bind_matrices: Vec<Vec<f64>>, + pub skeleton_root: Option<i32>, +} + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NAnimation { + pub name: String, + pub channels: Vec<NAnimationChannel>, + pub duration: f32, +} + +#[derive(Clone, Debug, uniffi::Record)] +pub struct NAnimationChannel { + pub target_node: i32, + pub times: Vec<f64>, + pub values: NChannelValues, + pub interpolation: NAnimationInterpolation, +} + +#[derive(Clone, Debug, uniffi::Enum)] +pub enum NAnimationInterpolation { + Linear, + Step, + CubicSpline, +} + +#[derive(Clone, Debug, uniffi::Enum)] +pub enum NChannelValues { + Translations { values: Vec<NVector3> }, + Rotations { values: Vec<NQuaternion> }, + Scales { values: Vec<NVector3> }, +} + +fn texture_handle_id( + registry: &dropbear_engine::asset::AssetRegistry, + texture: &Texture, +) -> u64 { + texture + .hash() + .and_then(|hash| registry.texture_handle_by_hash(hash).map(|h| h.id)) + .unwrap_or(0) +} + +fn to_nvector3(v: glam::Vec3) -> NVector3 { + NVector3::from([v.x, v.y, v.z]) +} + +fn to_nvector2(v: [f32; 2]) -> NVector2 { + NVector2::from(v) +} + +fn to_nvector4(v: [f32; 4]) -> NVector4 { + NVector4::from(v) +} + +fn to_nquaternion(q: glam::Quat) -> NQuaternion { + NQuaternion { + x: q.x as f64, + y: q.y as f64, + z: q.z as f64, + w: q.w as f64, + } +} + +fn map_vertex(vertex: &ModelVertex) -> NModelVertex { + NModelVertex { + position: NVector3::from(vertex.position), + normal: NVector3::from(vertex.normal), + tangent: to_nvector4(vertex.tangent), + tex_coords0: to_nvector2(vertex.tex_coords0), + tex_coords1: to_nvector2(vertex.tex_coords1), + colour0: to_nvector4(vertex.colour0), + joints0: vertex.joints0.iter().map(|v| *v as i32).collect(), + weights0: to_nvector4(vertex.weights0), + } +} + +fn map_mesh(mesh: &Mesh) -> NMesh { + NMesh { + name: mesh.name.clone(), + num_elements: mesh.num_elements as i32, + material_index: mesh.material as i32, + vertices: mesh.vertices.iter().map(map_vertex).collect(), + } +} + +fn map_material( + registry: &dropbear_engine::asset::AssetRegistry, + material: &Material, +) -> NMaterial { + NMaterial { + name: material.name.clone(), + diffuse_texture: texture_handle_id(registry, &material.diffuse_texture), + normal_texture: texture_handle_id(registry, &material.normal_texture), + tint: to_nvector4(material.tint), + emissive_factor: NVector3::from(material.emissive_factor), + metallic_factor: material.metallic_factor, + roughness_factor: material.roughness_factor, + alpha_cutoff: material.alpha_cutoff, + double_sided: material.double_sided, + occlusion_strength: material.occlusion_strength, + normal_scale: material.normal_scale, + uv_tiling: to_nvector2(material.uv_tiling), + emissive_texture: material + .emissive_texture + .as_ref() + .map(|tex| texture_handle_id(registry, tex)) + .filter(|id| *id != 0), + metallic_roughness_texture: material + .metallic_roughness_texture + .as_ref() + .map(|tex| texture_handle_id(registry, tex)) + .filter(|id| *id != 0), + occlusion_texture: material + .occlusion_texture + .as_ref() + .map(|tex| texture_handle_id(registry, tex)) + .filter(|id| *id != 0), + } +} + +fn map_node_transform(transform: &NodeTransform) -> NNodeTransform { + NNodeTransform { + translation: to_nvector3(transform.translation), + rotation: to_nquaternion(transform.rotation), + scale: to_nvector3(transform.scale), + } +} + +fn map_node(node: &Node) -> NNode { + NNode { + name: node.name.clone(), + parent: node.parent.map(|v| v as i32), + children: node.children.iter().map(|v| *v as i32).collect(), + transform: map_node_transform(&node.transform), + } +} + +fn map_skin(skin: &Skin) -> NSkin { + let inverse_bind_matrices = skin + .inverse_bind_matrices + .iter() + .map(|matrix| matrix.to_cols_array().iter().map(|v| *v as f64).collect()) + .collect(); + + NSkin { + name: skin.name.clone(), + joints: skin.joints.iter().map(|v| *v as i32).collect(), + inverse_bind_matrices, + skeleton_root: skin.skeleton_root.map(|v| v as i32), + } +} + +fn map_interpolation(value: &AnimationInterpolation) -> NAnimationInterpolation { + match value { + AnimationInterpolation::Linear => NAnimationInterpolation::Linear, + AnimationInterpolation::Step => NAnimationInterpolation::Step, + AnimationInterpolation::CubicSpline => NAnimationInterpolation::CubicSpline, + } +} + +fn map_channel_values(values: &ChannelValues) -> NChannelValues { + match values { + ChannelValues::Translations(list) => NChannelValues::Translations { + values: list.iter().map(|v| to_nvector3(*v)).collect(), + }, + ChannelValues::Rotations(list) => NChannelValues::Rotations { + values: list.iter().map(|v| to_nquaternion(*v)).collect(), + }, + ChannelValues::Scales(list) => NChannelValues::Scales { + values: list.iter().map(|v| to_nvector3(*v)).collect(), + }, + } +} + +fn map_animation_channel(channel: &AnimationChannel) -> NAnimationChannel { + NAnimationChannel { + target_node: channel.target_node as i32, + times: channel.times.iter().map(|v| *v as f64).collect(), + values: map_channel_values(&channel.values), + interpolation: map_interpolation(&channel.interpolation), + } +} + +fn map_animation(animation: &Animation) -> NAnimation { + NAnimation { + name: animation.name.clone(), + channels: animation.channels.iter().map(map_animation_channel).collect(), + duration: animation.duration, + } +} + +#[uniffi::export] +pub fn dropbear_asset_model_get_label( + asset_registry: u64, + model_handle: u64, +) -> DropbearNativeResult<String> { + let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); + let label = asset + .read() + .get_label_from_model_handle(Handle::new(model_handle)) + .ok_or_else(|| DropbearNativeError::InvalidHandle)?; + Ok(label) +} + +#[uniffi::export] +pub fn dropbear_asset_model_get_meshes( + asset_registry: u64, + model_handle: u64, +) -> DropbearNativeResult<Vec<NMesh>> { + let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); + let reader = asset.read(); + let model = reader + .get_model(Handle::new(model_handle)) + .ok_or(DropbearNativeError::InvalidHandle)?; + + Ok(model.meshes.iter().map(map_mesh).collect()) +} + +#[uniffi::export] +pub fn dropbear_asset_model_get_materials( + asset_registry: u64, + model_handle: u64, +) -> DropbearNativeResult<Vec<NMaterial>> { + let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); + let reader = asset.read(); + let model = reader + .get_model(Handle::new(model_handle)) + .ok_or(DropbearNativeError::InvalidHandle)?; + + Ok(model + .materials + .iter() + .map(|mat| map_material(&reader, mat)) + .collect()) +} + +#[uniffi::export] +pub fn dropbear_asset_model_get_skins( + asset_registry: u64, + model_handle: u64, +) -> DropbearNativeResult<Vec<NSkin>> { + let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); + let reader = asset.read(); + let model = reader + .get_model(Handle::new(model_handle)) + .ok_or(DropbearNativeError::InvalidHandle)?; + + Ok(model.skins.iter().map(map_skin).collect()) +} + +#[uniffi::export] +pub fn dropbear_asset_model_get_animations( + asset_registry: u64, + model_handle: u64, +) -> DropbearNativeResult<Vec<NAnimation>> { + let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); + let reader = asset.read(); + let model = reader + .get_model(Handle::new(model_handle)) + .ok_or(DropbearNativeError::InvalidHandle)?; + + Ok(model.animations.iter().map(map_animation).collect()) +} + +#[uniffi::export] +pub fn dropbear_asset_model_get_nodes( + asset_registry: u64, + model_handle: u64, +) -> DropbearNativeResult<Vec<NNode>> { + let asset = pointer_convert!(asset_registry => AssetRegistryUnwrapped); + let reader = asset.read(); + let model = reader + .get_model(Handle::new(model_handle)) + .ok_or(DropbearNativeError::InvalidHandle)?; + + Ok(model.nodes.iter().map(map_node).collect()) +} @@ -0,0 +1,83 @@ +pub mod shared { + use dropbear_engine::asset::{AssetHandle, AssetRegistry}; + use crate::scripting::native::DropbearNativeError; + use crate::scripting::result::DropbearNativeResult; + + pub fn get_texture_name( + asset_registry: &AssetRegistry, + handle: u64, + ) -> DropbearNativeResult<String> { + let texture = asset_registry + .get_material(AssetHandle::new(handle)) + .ok_or_else(|| DropbearNativeError::NoSuchHandle)?; + + Ok(texture.name.clone()) + } +} + +pub mod jni { + #![allow(non_snake_case)] + use jni::JNIEnv; + use jni::sys::{jlong, jstring}; + use dropbear_engine::asset::AssetRegistry; + use jni::objects::JClass; + use crate::asset::texture::shared::get_texture_name; + use crate::scripting::native::DropbearNativeError; + + #[unsafe(no_mangle)] + pub extern "system" fn Java_com_dropbear_asset_TextureHandleNative_getTextureName( + env: JNIEnv, + _class: JClass, + asset_registry_ptr: jlong, + handle: jlong, + ) -> jstring { + let asset_registry = crate::convert_ptr!(asset_registry_ptr => AssetRegistry); + let result = get_texture_name(asset_registry, handle as u64); + match result { + Ok(name) => { + let output = env.new_string(name).map_err(|_| DropbearNativeError::JNIFailedToCreateObject); + match output { + Ok(jstr) => jstr.into_raw(), + Err(e) => { + crate::ffi_error_return!("[ERROR] Failed to create Java string: {}", e) + } + } + } + Err(e) => { + crate::ffi_error_return!("[ERROR] {}", e) + } + } + } +} + +#[dropbear_macro::impl_c_api] +pub mod native { + use std::ffi::CString; + use crate::ptr::AssetRegistryPtr; + use crate::scripting::native::DropbearNativeError; + use crate::scripting::result::DropbearNativeResult; + use std::ffi::c_char; + use dropbear_engine::asset::AssetRegistry; + use crate::asset::texture::shared::get_texture_name; + + pub fn dropbear_get_texture_name( + asset_registry_ptr: AssetRegistryPtr, + handle: u64, + ) -> DropbearNativeResult<*mut c_char> { + let asset_registry = crate::convert_ptr!(asset_registry_ptr => AssetRegistry); + + let result = get_texture_name(asset_registry, handle) + .map(|name| { + match CString::new(name) { + Ok(c_str) => { + Ok(c_str.into_raw()) + }, + Err(_) => { + Err(DropbearNativeError::CStringError) + } + } + })?; + + result + } +} @@ -15,7 +15,6 @@ pub mod states; pub mod utils; pub mod command; pub mod physics; -pub mod asset; pub mod types; pub mod properties; pub mod mesh; @@ -23,6 +22,7 @@ pub mod entity; pub mod engine; pub mod transform; pub mod ui; +pub mod asset; pub use dropbear_macro as macros; pub use dropbear_traits as traits; @@ -212,7 +212,7 @@ pub mod jni { }; let Some(model_cache) = asset.get_pointer(Const("model_cache")) else { - let _ = env.throw_new("java/lang/IllegalArgumentException", "Unable to locate model_cache pointer within the asset registry"); + let _ = env.throw_new("java/lang/IllegalArgumentException", "Unable to locate model_cache pointer within the asset_old registry"); return; }; @@ -493,7 +493,7 @@ pub enum DropbearNativeError { /// When the argument is invalid #[error("Invalid argument")] InvalidArgument, - /// The handle provided does not exist. Could be for an asset, entity, or other handle type. + /// The handle provided does not exist. Could be for an asset_old, entity, or other handle type. #[error("No such handle")] NoSuchHandle, /// Failed to create a Java object via JNI. @@ -511,13 +511,13 @@ pub enum DropbearNativeError { /// Failed to unwrap a Java object via JNI. #[error("JNI unwrap failed")] JNIUnwrapFailed, - /// Generic asset error. There was an error thrown, however there is no context attached. - #[error("Generic asset error")] + /// Generic asset_old error. There was an error thrown, however there is no context attached. + #[error("Generic asset_old error")] GenericAssetError, /// The provided uri (either euca:// or https) was invalid and formatted wrong. #[error("Invalid URI")] InvalidURI, - /// The asset provided by the handle is wrong. + /// The asset_old provided by the handle is wrong. #[error("Asset not found")] AssetNotFound, /// When a handle has been inputted wrongly. @@ -321,7 +321,7 @@ fn resolve_resource_from_root(relative: &str, root: &Path) -> anyhow::Result<Pat } anyhow::bail!( - "Resource '{}' was resolved to '{}' but the file does not exist. Ensure the asset is packaged under '{}'", + "Resource '{}' was resolved to '{}' but the file does not exist. Ensure the asset_old is packaged under '{}'", relative, resolved.display(), root.display() @@ -470,7 +470,7 @@ impl<'a> EditorTabViewer<'a> { } fn draw_asset_icon(ui: &mut egui::Ui) { - let image = egui::Image::from_bytes("bytes://asset-viewer-icon", NO_TEXTURE) + let image = egui::Image::from_bytes("bytes://asset_old-viewer-icon", NO_TEXTURE) .max_size(egui::vec2(14.0, 14.0)); ui.add(image); } @@ -1653,7 +1653,7 @@ impl<'a> EditorTabViewer<'a> { } fn draw_asset_icon(ui: &mut egui::Ui) { - let image = egui::Image::from_bytes("bytes://asset-viewer-icon", NO_TEXTURE) + let image = egui::Image::from_bytes("bytes://asset_old-viewer-icon", NO_TEXTURE) .max_size(egui::vec2(14.0, 14.0)); ui.add(image); } @@ -274,7 +274,7 @@ async fn load_renderer_from_serialized( let mut mesh_renderer = match &renderer.handle.ref_type { ResourceReferenceType::None => anyhow::bail!( - "Renderer for '{}' does not specify an asset reference", + "Renderer for '{}' does not specify an asset_old reference", label ), ResourceReferenceType::Unassigned { id } => { @@ -64,7 +64,7 @@ impl AssetServer { self.texture_labels.insert(label.into(), handle.clone()); } - /// Updates the asset server by inserting the texture provided at the location of the handle, + /// Updates the asset_old server by inserting the texture provided at the location of the handle, /// and removing the old texture (by returning it back to you). pub fn update_texture(&mut self, handle: Handle<Texture>, texture: Texture) -> Option<Texture> { self.textures.insert(handle.id, texture) @@ -1,6 +1,7 @@ package com.dropbear.components import com.dropbear.EntityId +import com.dropbear.asset.ModelHandle import com.dropbear.asset.TextureHandle internal actual fun MeshRenderer.getModel(id: EntityId): ModelHandle? { @@ -1,9 +0,0 @@ -package com.dropbear.ui.widgets - -actual fun Button.getClicked(): Boolean { - TODO("Not yet implemented") -} - -actual fun Button.getHovering(): Boolean { - TODO("Not yet implemented") -} @@ -1,9 +0,0 @@ -package com.dropbear.ui.widgets - -actual fun Checkbox.getChecked(): Boolean { - TODO("Not yet implemented") -} - -actual fun Checkbox.hasCheckedState(): Boolean { - return false -}