Compare commits
No commits in common. "db77ca43881c9a6cceb6725295b7918638882c5f" and "e5599e1d275aab13c3a4279d43ec7bd470a48933" have entirely different histories.
db77ca4388
...
e5599e1d27
|
@ -590,19 +590,6 @@ dependencies = [
|
|||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"crossbeam-deque",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-queue",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.10"
|
||||
|
@ -635,15 +622,6 @@ dependencies = [
|
|||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.3.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.18"
|
||||
|
@ -836,27 +814,6 @@ dependencies = [
|
|||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "file-id"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6584280525fb2059cba3db2c04abf947a1a29a45ddae89f3870f8281704fafc9"
|
||||
dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall 0.4.1",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.28"
|
||||
|
@ -897,15 +854,6 @@ version = "2.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3aaba7ff514ee9d802b562927f80b1e94e93d8e74c31b134c9c3762dabf1a36b"
|
||||
|
||||
[[package]]
|
||||
name = "fsevent-sys"
|
||||
version = "4.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.30"
|
||||
|
@ -992,7 +940,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "0af1827b7dd2f36d740ae804c1b3ea0d64c12533fb61ff91883005143a0e8c5a"
|
||||
dependencies = [
|
||||
"core-foundation",
|
||||
"inotify 0.10.2",
|
||||
"inotify",
|
||||
"io-kit-sys",
|
||||
"js-sys",
|
||||
"libc",
|
||||
|
@ -1248,17 +1196,6 @@ version = "1.1.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a"
|
||||
|
||||
[[package]]
|
||||
name = "inotify"
|
||||
version = "0.9.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"inotify-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "inotify"
|
||||
version = "0.10.2"
|
||||
|
@ -1380,26 +1317,6 @@ dependencies = [
|
|||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kqueue"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7447f1ca1b7b563588a205fe93dea8df60fd981423a768bc1c0ded35ed147d0c"
|
||||
dependencies = [
|
||||
"kqueue-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kqueue-sys"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kv-log-macro"
|
||||
version = "1.0.7"
|
||||
|
@ -1588,15 +1505,12 @@ version = "0.0.1"
|
|||
dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.21.5",
|
||||
"crossbeam",
|
||||
"edict",
|
||||
"glam",
|
||||
"gltf",
|
||||
"image",
|
||||
"infer",
|
||||
"mime",
|
||||
"notify",
|
||||
"notify-debouncer-full",
|
||||
"percent-encoding",
|
||||
"thiserror",
|
||||
"tracing",
|
||||
|
@ -1822,39 +1736,6 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "notify"
|
||||
version = "6.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d"
|
||||
dependencies = [
|
||||
"bitflags 2.4.1",
|
||||
"crossbeam-channel",
|
||||
"filetime",
|
||||
"fsevent-sys",
|
||||
"inotify 0.9.6",
|
||||
"kqueue",
|
||||
"libc",
|
||||
"log",
|
||||
"mio",
|
||||
"walkdir",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "notify-debouncer-full"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49f5dab59c348b9b50cf7f261960a20e389feb2713636399cd9082cd4b536154"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"file-id",
|
||||
"log",
|
||||
"notify",
|
||||
"parking_lot",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
|
@ -2325,15 +2206,6 @@ version = "1.0.16"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scoped-tls"
|
||||
version = "1.0.1"
|
||||
|
@ -2808,16 +2680,6 @@ version = "1.1.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
|
|
|
@ -16,7 +16,7 @@ function on_update()
|
|||
--print("Lua's update function was called")
|
||||
|
||||
world:view(function (t)
|
||||
--print("Found entity at a really cool place: " .. tostring(t))
|
||||
print("Found entity at " .. tostring(t))
|
||||
t.translation = t.translation + Vec3.new(0, 0.0008, 0)
|
||||
|
||||
return t
|
||||
|
|
|
@ -306,7 +306,6 @@ async fn main() {
|
|||
let world = game.world();
|
||||
let mut res_man = world.get_resource_mut::<ResourceManager>();
|
||||
let script = res_man.request::<LuaScript>("scripts/test.lua").unwrap();
|
||||
res_man.watch("scripts/test.lua", false).unwrap();
|
||||
drop(res_man);
|
||||
|
||||
let script = Script::new("test.lua", script);
|
||||
|
|
|
@ -8,6 +8,8 @@ pub use spotlight::*;
|
|||
|
||||
use std::{collections::{VecDeque, HashMap}, marker::PhantomData};
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
use std::mem;
|
||||
|
||||
use crate::math::Transform;
|
||||
|
@ -172,7 +174,7 @@ impl LightUniformBuffers {
|
|||
if !self.point_lights.has_light(entity) || light_epoch == world_tick || transform_epoch == world_tick {
|
||||
let uniform = PointLightUniform::from_bundle(&point_light, &transform);
|
||||
self.point_lights.update_or_add(&mut self.lights_uniform.point_lights, entity, uniform);
|
||||
//debug!("Updated point light");
|
||||
debug!("Updated point light");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,11 +11,11 @@ pub struct MaterialSpecular {
|
|||
|
||||
impl MaterialSpecular {
|
||||
pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: Arc<wgpu::BindGroupLayout>, value: &lyra_resource::Specular) -> Self {
|
||||
let tex = value.texture.as_ref().map(|t| t.data_ref())
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), &i.image, None).unwrap());
|
||||
let tex = value.texture.as_ref().map(|t| &t.data.as_ref().unwrap().image)
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), i, None).unwrap());
|
||||
|
||||
let color_tex = value.color_texture.as_ref().map(|t| t.data_ref())
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout, &i.image, None).unwrap());
|
||||
let color_tex = value.color_texture.as_ref().map(|t| &t.data.as_ref().unwrap().image)
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout, i, None).unwrap());
|
||||
|
||||
Self {
|
||||
factor: value.factor,
|
||||
|
@ -39,8 +39,8 @@ pub struct Material {
|
|||
|
||||
impl Material {
|
||||
pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, bg_layout: Arc<wgpu::BindGroupLayout>, value: &lyra_resource::Material) -> Self {
|
||||
let diffuse_texture = value.base_color_texture.as_ref().map(|t| t.data_ref())
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), &i.image, None).unwrap());
|
||||
let diffuse_texture = value.base_color_texture.as_ref().map(|t| &t.data.as_ref().unwrap().image)
|
||||
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), i, None).unwrap());
|
||||
|
||||
let specular = value.specular.as_ref().map(|s| MaterialSpecular::from_resource(device, queue, bg_layout.clone(), s));
|
||||
|
||||
|
|
|
@ -430,7 +430,7 @@ impl Renderer for BasicRenderer {
|
|||
|
||||
let transform_val = cached.from_transform.lerp(cached.to_transform, alpha);
|
||||
|
||||
let model = model.data_ref();
|
||||
let model = model.data.as_ref().unwrap().as_ref();
|
||||
for mesh in model.meshes.iter() {
|
||||
if !self.process_mesh(entity, transform_val, mesh) && model_epoch == last_epoch {
|
||||
self.update_mesh_buffers(entity, mesh);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use image::GenericImageView;
|
||||
use lyra_resource::{ResHandle, Texture};
|
||||
use lyra_resource::{Resource, Texture};
|
||||
|
||||
use super::render_buffer::BindGroupPair;
|
||||
|
||||
|
@ -153,8 +153,8 @@ impl RenderTexture {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn update_texture(&mut self, _device: &wgpu::Device, queue: &wgpu::Queue, texture: &Arc<ResHandle<Texture>>) {
|
||||
let texture = &texture.data_ref().image;
|
||||
pub fn update_texture(&mut self, _device: &wgpu::Device, queue: &wgpu::Queue, texture: &Arc<Resource<Texture>>) {
|
||||
let texture = &texture.data.as_ref().unwrap().image;
|
||||
let rgba = texture.to_rgba8();
|
||||
let dimensions = texture.dimensions();
|
||||
let size = wgpu::Extent3d {
|
||||
|
|
|
@ -8,7 +8,6 @@ edition = "2021"
|
|||
[dependencies]
|
||||
anyhow = "1.0.75"
|
||||
base64 = "0.21.4"
|
||||
crossbeam = { version = "0.8.4", features = [ "crossbeam-channel" ] }
|
||||
edict = "0.5.0"
|
||||
glam = "0.24.1"
|
||||
gltf = { version = "1.3.0", features = ["KHR_materials_pbrSpecularGlossiness", "KHR_materials_specular"] }
|
||||
|
@ -16,9 +15,6 @@ image = "0.24.7"
|
|||
# not using custom matcher, or file type from file path
|
||||
infer = { version = "0.15.0", default-features = false }
|
||||
mime = "0.3.17"
|
||||
notify = "6.1.1"
|
||||
notify-debouncer-full = "0.3.1"
|
||||
#notify = { version = "6.1.1", default-features = false, features = [ "fsevent-sys", "macos_fsevent" ]} # disables crossbeam-channel
|
||||
percent-encoding = "2.3.0"
|
||||
thiserror = "1.0.48"
|
||||
tracing = "0.1.37"
|
||||
|
|
|
@ -16,7 +16,4 @@ pub use model::*;
|
|||
pub mod material;
|
||||
pub use material::*;
|
||||
|
||||
pub(crate) mod util;
|
||||
|
||||
pub use crossbeam::channel as channel;
|
||||
pub use notify;
|
||||
pub(crate) mod util;
|
|
@ -2,7 +2,7 @@ use std::{fs::File, sync::Arc, io::Read};
|
|||
|
||||
use image::ImageError;
|
||||
|
||||
use crate::{resource_manager::ResourceStorage, texture::Texture, resource::ResHandle, ResourceManager};
|
||||
use crate::{resource_manager::ResourceStorage, texture::Texture, resource::Resource, ResourceManager};
|
||||
|
||||
use super::{LoaderError, ResourceLoader};
|
||||
|
||||
|
@ -62,7 +62,7 @@ impl ResourceLoader for ImageLoader {
|
|||
let texture = Texture {
|
||||
image,
|
||||
};
|
||||
let res = ResHandle::with_data(path, texture);
|
||||
let res = Resource::with_data(path, texture);
|
||||
|
||||
Ok(Arc::new(res))
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ impl ResourceLoader for ImageLoader {
|
|||
let texture = Texture {
|
||||
image,
|
||||
};
|
||||
let res = ResHandle::with_data(&uuid::Uuid::new_v4().to_string(), texture);
|
||||
let res = Resource::with_data(&uuid::Uuid::new_v4().to_string(), texture);
|
||||
|
||||
Ok(Arc::new(res))
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::{sync::Arc, path::PathBuf};
|
|||
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{ResourceLoader, LoaderError, Mesh, Model, MeshVertexAttribute, VertexAttributeData, ResHandle, Material, MeshIndices, ResourceManager, util};
|
||||
use crate::{ResourceLoader, LoaderError, Mesh, Model, MeshVertexAttribute, VertexAttributeData, Resource, Material, MeshIndices, ResourceManager, util};
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
|
@ -189,7 +189,7 @@ impl ResourceLoader for ModelLoader {
|
|||
.collect();
|
||||
debug!("Loaded {} meshes, and {} materials from '{}'", meshes.len(), materials.len(), path);
|
||||
|
||||
Ok(Arc::new(ResHandle::with_data(path, Model::new(meshes))))
|
||||
Ok(Arc::new(Resource::with_data(path, Model::new(meshes))))
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
|
@ -216,8 +216,8 @@ mod tests {
|
|||
let mut manager = ResourceManager::new();
|
||||
let loader = ModelLoader::default();
|
||||
let model = loader.load(&mut manager, &path).unwrap();
|
||||
let model = Arc::downcast::<ResHandle<Model>>(model.as_arc_any()).unwrap();
|
||||
let model = model.data_ref();
|
||||
let model = Arc::downcast::<Resource<Model>>(model.as_arc_any()).unwrap();
|
||||
let model = model.data.as_ref().unwrap();
|
||||
assert_eq!(model.meshes.len(), 1); // There should only be 1 mesh
|
||||
let mesh = &model.meshes[0];
|
||||
assert!(mesh.position().unwrap().len() > 0);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::sync::{Arc, RwLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
|
@ -8,148 +8,25 @@ pub enum ResourceState {
|
|||
Ready,
|
||||
}
|
||||
|
||||
pub struct ResourceDataRef<'a, T> {
|
||||
guard: std::sync::RwLockReadGuard<'a, Resource<T>>,
|
||||
#[derive(Clone)]
|
||||
pub struct Resource<T> {
|
||||
pub path: String,
|
||||
pub data: Option<Arc<T>>,
|
||||
pub uuid: Uuid,
|
||||
pub state: ResourceState,
|
||||
}
|
||||
|
||||
impl<'a, T> std::ops::Deref for ResourceDataRef<'a, T> {
|
||||
type Target = T;
|
||||
/// A helper type to make it easier to use resources
|
||||
pub type ResHandle<T> = Arc<Resource<T>>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// safety: this struct must only be created if the resource is loaded
|
||||
self.guard.data.as_ref().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Resource<T> {
|
||||
path: String,
|
||||
pub(crate) data: Option<T>,
|
||||
pub(crate) version: usize,
|
||||
pub(crate) state: ResourceState,
|
||||
uuid: Uuid,
|
||||
pub(crate) is_watched: bool,
|
||||
}
|
||||
|
||||
/// A handle to a resource.
|
||||
///
|
||||
/// # Note
|
||||
/// This struct has an inner [`RwLock`] to the resource data, so most methods may be blocking.
|
||||
/// However, the only times it will be blocking is if another thread is reloading the resource
|
||||
/// and has a write lock on the data. This means that most of the time, it is not blocking.
|
||||
pub struct ResHandle<T> {
|
||||
pub(crate) data: Arc<RwLock<Resource<T>>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for ResHandle<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { data: self.data.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ResHandle<T> {
|
||||
impl<T> Resource<T> {
|
||||
/// Create the resource with data, its assumed the state is `Ready`
|
||||
pub fn with_data(path: &str, data: T) -> Self {
|
||||
let res_version = Resource {
|
||||
path: path.to_string(),
|
||||
data: Some(data),
|
||||
version: 0,
|
||||
state: ResourceState::Ready,
|
||||
uuid: Uuid::new_v4(),
|
||||
is_watched: false,
|
||||
};
|
||||
|
||||
Self {
|
||||
data: Arc::new(RwLock::new(res_version)),
|
||||
path: path.to_string(),
|
||||
data: Some(Arc::new(data)),
|
||||
uuid: Uuid::new_v4(),
|
||||
state: ResourceState::Ready,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a boolean indicating if this resource's path is being watched.
|
||||
pub fn is_watched(&self) -> bool {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
d.is_watched
|
||||
}
|
||||
|
||||
/// Returns a boolean indicating if this resource is loaded
|
||||
pub fn is_loaded(&self) -> bool {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
d.state == ResourceState::Ready
|
||||
}
|
||||
|
||||
/// Returns the current state of the resource.
|
||||
pub fn state(&self) -> ResourceState {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
d.state
|
||||
}
|
||||
|
||||
/// Returns the path that the resource was loaded from.
|
||||
pub fn path(&self) -> String {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
d.path.to_string()
|
||||
}
|
||||
|
||||
/// Returns the uuid of the resource.
|
||||
pub fn uuid(&self) -> Uuid {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
d.uuid
|
||||
}
|
||||
|
||||
/// Retrieves the current version of the resource. This gets incremented when the resource
|
||||
/// is reloaded.
|
||||
pub fn version(&self) -> usize {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
d.version
|
||||
}
|
||||
|
||||
/// Get a reference to the data in the resource
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the resource was not loaded yet.
|
||||
pub fn data_ref<'a>(&'a self) -> ResourceDataRef<'a, T> {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
ResourceDataRef {
|
||||
guard: d
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempt to get a borrow to the resource data. Returns `None` if the resource is not loaded.
|
||||
pub fn try_data_ref<'a>(&'a self) -> Option<ResourceDataRef<'a, T>> {
|
||||
if self.is_loaded() {
|
||||
let d = self.data.read().expect("Resource mutex was poisoned!");
|
||||
Some(ResourceDataRef {
|
||||
guard: d
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/* /// Get a reference to the data in the resource
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the resource was not loaded yet.
|
||||
pub fn data_ref(&self) -> &T {
|
||||
self.data.as_ref()
|
||||
.expect("Resource is not loaded yet (use try_data_ref, or wait until its loaded)!")
|
||||
}
|
||||
|
||||
/// If the resource is loaded, returns `Some` reference to the data in the resource,
|
||||
/// else it will return `None`
|
||||
pub fn try_data_ref(&self) -> Option<&T> {
|
||||
self.data.as_ref()
|
||||
}
|
||||
|
||||
/// Get a **mutable** reference to the data in the resource
|
||||
///
|
||||
/// # Panics
|
||||
/// Panics if the resource was not loaded yet.
|
||||
pub fn data_mut(&mut self) -> &mut T {
|
||||
self.data.as_mut()
|
||||
.expect("Resource is not loaded yet (use try_data_ref, or wait until its loaded)!")
|
||||
}
|
||||
|
||||
/// If the resource is loaded, returns `Some` **mutable** reference to the data in the resource,
|
||||
/// else it will return `None`
|
||||
pub fn try_data_mut(&mut self) -> Option<&mut T> {
|
||||
self.data.as_mut()
|
||||
} */
|
||||
}
|
||||
}
|
|
@ -1,22 +1,17 @@
|
|||
use std::{sync::{Arc, RwLock}, collections::HashMap, any::Any, path::Path, time::Duration};
|
||||
use std::{sync::Arc, collections::HashMap, any::Any};
|
||||
|
||||
use crossbeam::channel::Receiver;
|
||||
use notify::{Watcher, RecommendedWatcher};
|
||||
use notify_debouncer_full::{DebouncedEvent, FileIdMap};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{resource::ResHandle, loader::{ResourceLoader, LoaderError, image::ImageLoader, model::ModelLoader}};
|
||||
use crate::{resource::Resource, loader::{ResourceLoader, LoaderError, image::ImageLoader, model::ModelLoader}};
|
||||
|
||||
pub trait ResourceStorage: Send + Sync + Any + 'static {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
|
||||
fn as_box_any(self: Box<Self>) -> Box<dyn Any + Send + Sync>;
|
||||
fn set_watched(&self, watched: bool);
|
||||
}
|
||||
|
||||
/// Implements this trait for anything that fits the type bounds
|
||||
impl<T: Send + Sync + 'static> ResourceStorage for ResHandle<T> {
|
||||
impl<T: Send + Sync + 'static> ResourceStorage for T {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
@ -28,15 +23,6 @@ impl<T: Send + Sync + 'static> ResourceStorage for ResHandle<T> {
|
|||
fn as_arc_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
|
||||
self.clone()
|
||||
}
|
||||
|
||||
fn as_box_any(self: Box<Self>) -> Box<dyn Any + Send + Sync> {
|
||||
self
|
||||
}
|
||||
|
||||
fn set_watched(&self, watched: bool) {
|
||||
let mut w = self.data.write().unwrap();
|
||||
w.is_watched = watched;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
|
@ -60,16 +46,9 @@ impl From<LoaderError> for RequestError {
|
|||
/// A struct that stores all Manager data. This is requried for sending
|
||||
//struct ManagerStorage
|
||||
|
||||
/// A struct that
|
||||
pub struct ResourceWatcher {
|
||||
debouncer: Arc<RwLock<notify_debouncer_full::Debouncer<RecommendedWatcher, FileIdMap>>>,
|
||||
events_recv: Receiver<Result<Vec<DebouncedEvent>, Vec<notify::Error>>>,
|
||||
}
|
||||
|
||||
pub struct ResourceManager {
|
||||
resources: HashMap<String, Arc<dyn ResourceStorage>>,
|
||||
loaders: Vec<Arc<dyn ResourceLoader>>,
|
||||
watchers: HashMap<String, ResourceWatcher>,
|
||||
}
|
||||
|
||||
impl Default for ResourceManager {
|
||||
|
@ -83,7 +62,6 @@ impl ResourceManager {
|
|||
Self {
|
||||
resources: HashMap::new(),
|
||||
loaders: vec![ Arc::new(ImageLoader), Arc::new(ModelLoader) ],
|
||||
watchers: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,15 +73,11 @@ impl ResourceManager {
|
|||
self.loaders.push(Arc::new(L::default()));
|
||||
}
|
||||
|
||||
pub fn request<T>(&mut self, path: &str) -> Result<ResHandle<T>, RequestError>
|
||||
where
|
||||
T: Send + Sync + Any + 'static
|
||||
{
|
||||
pub fn request<T: Send + Sync + Any + 'static>(&mut self, path: &str) -> Result<Arc<Resource<T>>, RequestError> {
|
||||
match self.resources.get(&path.to_string()) {
|
||||
Some(res) => {
|
||||
let res = res.clone().as_arc_any();
|
||||
let res: Arc<ResHandle<T>> = res.downcast::<ResHandle<T>>().expect("Failure to downcast resource");
|
||||
let res = ResHandle::<T>::clone(&res);
|
||||
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
|
||||
|
||||
Ok(res)
|
||||
},
|
||||
|
@ -114,14 +88,12 @@ impl ResourceManager {
|
|||
// Load the resource and store it
|
||||
let loader = Arc::clone(loader); // stop borrowing from self
|
||||
let res = loader.load(self, path)?;
|
||||
let res: Arc<dyn ResourceStorage> = Arc::from(res);
|
||||
self.resources.insert(path.to_string(), res.clone());
|
||||
|
||||
// cast Arc<dyn ResourceStorage> to Arc<Resource<T>
|
||||
let res = res.as_arc_any();
|
||||
let res = res.downcast::<ResHandle<T>>()
|
||||
let res = res.downcast::<Resource<T>>()
|
||||
.expect("Failure to downcast resource! Does the loader return an `Arc<Resource<T>>`?");
|
||||
let res = ResHandle::<T>::clone(&res);
|
||||
|
||||
Ok(res)
|
||||
} else {
|
||||
|
@ -140,23 +112,18 @@ impl ResourceManager {
|
|||
/// * `bytes` - The bytes to store.
|
||||
///
|
||||
/// Returns: The `Arc` to the now stored resource
|
||||
pub fn load_bytes<T>(&mut self, ident: &str, mime_type: &str, bytes: Vec<u8>, offset: usize, length: usize) -> Result<ResHandle<T>, RequestError>
|
||||
where
|
||||
T: Send + Sync + Any + 'static
|
||||
{
|
||||
pub fn load_bytes<T: Send + Sync + Any + 'static>(&mut self, ident: &str, mime_type: &str, bytes: Vec<u8>, offset: usize, length: usize) -> Result<Arc<Resource<T>>, RequestError> {
|
||||
if let Some(loader) = self.loaders.iter()
|
||||
.find(|l| l.does_support_mime(mime_type)) {
|
||||
let loader = loader.clone();
|
||||
let res = loader.load_bytes(self, bytes, offset, length)?;
|
||||
let res: Arc<dyn ResourceStorage> = Arc::from(res);
|
||||
self.resources.insert(ident.to_string(), res.clone());
|
||||
// code here...
|
||||
|
||||
// cast Arc<dyn ResourceStorage> to Arc<Resource<T>
|
||||
let res = res.as_arc_any();
|
||||
let res = res.downcast::<ResHandle<T>>()
|
||||
let res = res.downcast::<Resource<T>>()
|
||||
.expect("Failure to downcast resource! Does the loader return an `Arc<Resource<T>>`?");
|
||||
let res = ResHandle::<T>::clone(&res);
|
||||
|
||||
Ok(res)
|
||||
} else {
|
||||
|
@ -165,14 +132,11 @@ impl ResourceManager {
|
|||
}
|
||||
|
||||
/// Requests bytes from the manager.
|
||||
pub fn request_loaded_bytes<T>(&mut self, ident: &str) -> Result<Arc<ResHandle<T>>, RequestError>
|
||||
where
|
||||
T: Send + Sync + Any + 'static
|
||||
{
|
||||
pub fn request_loaded_bytes<T: Send + Sync + Any + 'static>(&mut self, ident: &str) -> Result<Arc<Resource<T>>, RequestError> {
|
||||
match self.resources.get(&ident.to_string()) {
|
||||
Some(res) => {
|
||||
let res = res.clone().as_arc_any();
|
||||
let res = res.downcast::<ResHandle<T>>().expect("Failure to downcast resource");
|
||||
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
|
||||
|
||||
Ok(res)
|
||||
},
|
||||
|
@ -181,90 +145,6 @@ impl ResourceManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Start watching a path for changes. Returns a mspc channel that will send events
|
||||
pub fn watch(&mut self, path: &str, recursive: bool) -> notify::Result<Receiver<Result<Vec<DebouncedEvent>, Vec<notify::Error>>>> {
|
||||
let (send, recv) = crossbeam::channel::bounded(15);
|
||||
let mut watcher = notify_debouncer_full::new_debouncer(Duration::from_millis(1000), None, send)?;
|
||||
|
||||
let recurse_mode = match recursive {
|
||||
true => notify::RecursiveMode::Recursive,
|
||||
false => notify::RecursiveMode::NonRecursive,
|
||||
};
|
||||
watcher.watcher().watch(path.as_ref(), recurse_mode)?;
|
||||
|
||||
let watcher = Arc::new(RwLock::new(watcher));
|
||||
let watcher = ResourceWatcher {
|
||||
debouncer: watcher,
|
||||
events_recv: recv.clone(),
|
||||
};
|
||||
|
||||
self.watchers.insert(path.to_string(), watcher);
|
||||
|
||||
let res = self.resources.get(&path.to_string())
|
||||
.expect("The path that was watched has not been loaded as a resource yet");
|
||||
res.set_watched(true);
|
||||
|
||||
Ok(recv)
|
||||
}
|
||||
|
||||
/// Stops watching a path
|
||||
pub fn stop_watching(&mut self, path: &str) -> notify::Result<()> {
|
||||
if let Some(watcher) = self.watchers.get(path) {
|
||||
let mut watcher = watcher.debouncer.write().unwrap();
|
||||
watcher.watcher().unwatch(Path::new(path))?;
|
||||
|
||||
// unwrap is safe since only loaded resources can be watched
|
||||
let res = self.resources.get(&path.to_string()).unwrap();
|
||||
res.set_watched(false);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns a mspc receiver for watcher events of a specific path. The path must already
|
||||
/// be watched with [`ResourceManager::watch`] for this to return `Some`.
|
||||
pub fn watcher_event_recv(&self, path: &str) -> Option<Receiver<Result<Vec<DebouncedEvent>, Vec<notify::Error>>>> {
|
||||
self.watchers.get(&path.to_string())
|
||||
.map(|w| w.events_recv.clone())
|
||||
}
|
||||
|
||||
/// Reloads a resource. The data inside the resource will be updated, the state may
|
||||
pub fn reload<T>(&mut self, resource: ResHandle<T>) -> Result<(), RequestError>
|
||||
where
|
||||
T: Send + Sync + Any + 'static
|
||||
{
|
||||
let path = resource.path();
|
||||
if let Some(loader) = self.loaders.iter()
|
||||
.find(|l| l.does_support_file(&path)) {
|
||||
let loader = Arc::clone(loader); // stop borrowing from self
|
||||
let loaded = loader.load(self, &path)?;
|
||||
let loaded = loaded.as_arc_any();
|
||||
|
||||
let loaded = loaded.downcast::<ResHandle<T>>()
|
||||
.unwrap();
|
||||
let loaded = match Arc::try_unwrap(loaded) {
|
||||
Ok(v) => v,
|
||||
Err(_) => panic!("Seems impossible that this would happen, the resource was just loaded!"),
|
||||
};
|
||||
let loaded = loaded.data;
|
||||
let loaded = match Arc::try_unwrap(loaded) {
|
||||
Ok(v) => v,
|
||||
Err(_) => panic!("Seems impossible that this would happen, the resource was just loaded!"),
|
||||
};
|
||||
let loaded = loaded.into_inner().unwrap();
|
||||
|
||||
let res_lock = &resource.data;
|
||||
let mut res_lock = res_lock.write().unwrap();
|
||||
let version = res_lock.version;
|
||||
|
||||
res_lock.data = loaded.data;
|
||||
res_lock.state = loaded.state;
|
||||
res_lock.version = version + 1;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -285,20 +165,20 @@ mod tests {
|
|||
fn load_image() {
|
||||
let mut man = ResourceManager::new();
|
||||
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
||||
assert_eq!(res.state(), ResourceState::Ready);
|
||||
let img = res.try_data_ref();
|
||||
assert_eq!(res.state, ResourceState::Ready);
|
||||
let img = res.data.as_ref();
|
||||
img.unwrap();
|
||||
}
|
||||
|
||||
/// Ensures that only one copy of the same data was made
|
||||
/// Ensures that only one copy of the same thing made
|
||||
#[test]
|
||||
fn ensure_single() {
|
||||
let mut man = ResourceManager::new();
|
||||
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
||||
assert_eq!(Arc::strong_count(&res.data), 2);
|
||||
assert_eq!(Arc::strong_count(&res), 2);
|
||||
|
||||
let resagain = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
||||
assert_eq!(Arc::strong_count(&resagain.data), 3);
|
||||
assert_eq!(Arc::strong_count(&resagain), 3);
|
||||
}
|
||||
|
||||
/// Ensures that an error is returned when a file that doesn't exist is requested
|
||||
|
@ -316,42 +196,4 @@ mod tests {
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_image() {
|
||||
let mut man = ResourceManager::new();
|
||||
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
|
||||
assert_eq!(res.state(), ResourceState::Ready);
|
||||
let img = res.try_data_ref();
|
||||
img.unwrap();
|
||||
|
||||
man.reload(res.clone()).unwrap();
|
||||
assert_eq!(res.version(), 1);
|
||||
|
||||
man.reload(res.clone()).unwrap();
|
||||
assert_eq!(res.version(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn watch_image() {
|
||||
let image_path = get_image("squiggles.png");
|
||||
let image_bytes = std::fs::read(&image_path).unwrap();
|
||||
|
||||
let mut man = ResourceManager::new();
|
||||
let res = man.request::<Texture>(&image_path).unwrap();
|
||||
assert_eq!(res.state(), ResourceState::Ready);
|
||||
let img = res.try_data_ref();
|
||||
img.unwrap();
|
||||
|
||||
let recv = man.watch(&image_path, false).unwrap();
|
||||
|
||||
std::fs::remove_file(&image_path).unwrap();
|
||||
|
||||
let event = recv.recv().unwrap();
|
||||
let event = event.unwrap();
|
||||
|
||||
std::fs::write(image_path, image_bytes).unwrap();
|
||||
|
||||
assert!(event.iter().any(|ev| ev.kind.is_remove() || ev.kind.is_modify()));
|
||||
}
|
||||
}
|
|
@ -43,9 +43,7 @@ pub trait ScriptApiProvider {
|
|||
type ScriptContext;
|
||||
|
||||
/// Prepare the ECS world for this api. Things like registering types with the type registry happen here.
|
||||
fn prepare_world(&mut self, world: &mut World) {
|
||||
let _ = world; // remove compiler warning
|
||||
}
|
||||
fn prepare_world(&mut self, world: &mut World) {}
|
||||
|
||||
/// Exposes an API in the provided script context.
|
||||
fn expose_api(&mut self, ctx: &mut Self::ScriptContext) -> Result<(), ScriptError>;
|
||||
|
@ -122,12 +120,4 @@ impl<T> ScriptContexts<T> {
|
|||
pub fn has_context(&self, script_id: u64) -> bool {
|
||||
self.contexts.contains_key(&script_id)
|
||||
}
|
||||
|
||||
pub fn remove_context(&mut self, script_id: u64) -> Option<T> {
|
||||
self.contexts.remove(&script_id)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.contexts.len()
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use lyra_resource::{ResourceLoader, ResHandle};
|
||||
use lyra_resource::{ResourceLoader, Resource};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LuaScript {
|
||||
|
@ -23,7 +23,7 @@ impl ResourceLoader for LuaLoader {
|
|||
fn load(&self, _resource_manager: &mut lyra_resource::ResourceManager, path: &str) -> Result<std::sync::Arc<dyn lyra_resource::ResourceStorage>, lyra_resource::LoaderError> {
|
||||
let bytes = std::fs::read(path)?;
|
||||
|
||||
let s = ResHandle::with_data(path, LuaScript {
|
||||
let s = Resource::with_data(path, LuaScript {
|
||||
bytes
|
||||
});
|
||||
|
||||
|
@ -34,7 +34,7 @@ impl ResourceLoader for LuaLoader {
|
|||
let end = offset + length;
|
||||
let bytes = bytes[offset..end].to_vec();
|
||||
|
||||
let s = ResHandle::with_data("from bytes", LuaScript {
|
||||
let s = Resource::with_data("from bytes", LuaScript {
|
||||
bytes
|
||||
});
|
||||
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
pub mod dynamic_iter;
|
||||
use anyhow::anyhow;
|
||||
pub use dynamic_iter::*;
|
||||
|
||||
pub mod world;
|
||||
use lyra_game::{game::GameStages, plugin::Plugin};
|
||||
use lyra_game::{plugin::Plugin, game::GameStages};
|
||||
use lyra_resource::ResourceManager;
|
||||
use tracing::{debug, error, trace};
|
||||
pub use world::*;
|
||||
|
@ -20,49 +19,34 @@ pub mod wrappers;
|
|||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
use std::{any::TypeId, ptr::NonNull, sync::Mutex};
|
||||
use std::{ptr::NonNull, sync::Mutex, any::TypeId};
|
||||
|
||||
use lyra_ecs::{
|
||||
query::{Entities, ResMut, View},
|
||||
DynamicBundle, World,
|
||||
};
|
||||
use lyra_reflect::{FromType, Reflect, TypeRegistry};
|
||||
use lyra_ecs::{DynamicBundle, World, query::{ResMut, View, Entities}};
|
||||
use lyra_reflect::{Reflect, FromType, RegisteredType, TypeRegistry};
|
||||
|
||||
use mlua::{AnyUserDataExt, Lua};
|
||||
use mlua::{Lua, AnyUserDataExt};
|
||||
|
||||
pub type LuaContext = Mutex<mlua::Lua>;
|
||||
|
||||
pub const FN_NAME_INTERNAL_REFLECT_TYPE: &str = "__lyra_internal_reflect_type";
|
||||
pub const FN_NAME_INTERNAL_REFLECT: &str = "__lyra_internal_reflect";
|
||||
|
||||
use crate::{
|
||||
GameScriptExt, ScriptApiProviders, ScriptBorrow, ScriptContexts, ScriptData,
|
||||
ScriptDynamicBundle, ScriptError, ScriptHost, ScriptList, ScriptWorldPtr,
|
||||
};
|
||||
use crate::{ScriptBorrow, ScriptDynamicBundle, ScriptApiProviders, ScriptContexts, ScriptWorldPtr, ScriptList, ScriptData, ScriptHost, ScriptError, GameScriptExt};
|
||||
|
||||
use self::providers::{LyraEcsApiProvider, LyraMathApiProvider, UtilityApiProvider};
|
||||
use self::providers::{UtilityApiProvider, LyraMathApiProvider, LyraEcsApiProvider};
|
||||
|
||||
pub trait RegisterLuaType {
|
||||
/// Register a lua type that **is not wrapped**.
|
||||
fn register_lua_type<'a, T: Reflect + LuaProxy + Clone + mlua::FromLua<'a> + mlua::UserData>(
|
||||
&mut self,
|
||||
);
|
||||
fn register_lua_type<'a, T: Reflect + LuaProxy + Clone + mlua::FromLua<'a> + mlua::UserData>(&mut self);
|
||||
/// Registers a wrapped lua type.
|
||||
/// You provide the wrapper as `W`, and the type that the wrapper wraps, as `T`.
|
||||
fn register_lua_wrapper<
|
||||
'a,
|
||||
W: Reflect + LuaProxy + LuaWrapper + Clone + mlua::FromLua<'a> + mlua::UserData,
|
||||
>(
|
||||
&mut self,
|
||||
);
|
||||
fn register_lua_wrapper<'a, W: Reflect + LuaProxy + LuaWrapper + Clone + mlua::FromLua<'a> + mlua::UserData>(&mut self);
|
||||
}
|
||||
|
||||
impl RegisterLuaType for World {
|
||||
fn register_lua_type<'a, T: Reflect + LuaProxy + Clone + mlua::FromLua<'a> + mlua::UserData>(
|
||||
&mut self,
|
||||
) {
|
||||
fn register_lua_type<'a, T: Reflect + LuaProxy + Clone + mlua::FromLua<'a> + mlua::UserData>(&mut self) {
|
||||
let mut registry = self.get_resource_mut::<TypeRegistry>();
|
||||
|
||||
|
||||
let type_id = TypeId::of::<T>();
|
||||
|
||||
let reg_type = registry.get_type_or_default(type_id);
|
||||
|
@ -70,14 +54,9 @@ impl RegisterLuaType for World {
|
|||
//reg_type.add_data(<ReflectedComponent as FromType<T>>::from_type());
|
||||
}
|
||||
|
||||
fn register_lua_wrapper<
|
||||
'a,
|
||||
W: Reflect + LuaProxy + LuaWrapper + Clone + mlua::FromLua<'a> + mlua::UserData,
|
||||
>(
|
||||
&mut self,
|
||||
) {
|
||||
fn register_lua_wrapper<'a, W: Reflect + LuaProxy + LuaWrapper + Clone + mlua::FromLua<'a> + mlua::UserData>(&mut self) {
|
||||
let mut registry = self.get_resource_mut::<TypeRegistry>();
|
||||
|
||||
|
||||
let reg_type = registry.get_type_or_default(W::wrapped_type_id());
|
||||
reg_type.add_data(<ReflectLuaProxy as FromType<W>>::from_type());
|
||||
}
|
||||
|
@ -105,31 +84,17 @@ pub trait LuaWrapper {
|
|||
}
|
||||
|
||||
pub trait LuaProxy {
|
||||
fn as_lua_value<'lua>(
|
||||
lua: &'lua mlua::Lua,
|
||||
this: &dyn Reflect,
|
||||
) -> mlua::Result<mlua::AnyUserData<'lua>>;
|
||||
fn apply(
|
||||
lua: &mlua::Lua,
|
||||
this: &mut dyn Reflect,
|
||||
apply: &mlua::AnyUserData,
|
||||
) -> mlua::Result<()>;
|
||||
fn as_lua_value<'lua>(lua: &'lua mlua::Lua, this: &dyn Reflect) -> mlua::Result<mlua::AnyUserData<'lua>>;
|
||||
fn apply(lua: &mlua::Lua, this: &mut dyn Reflect, apply: &mlua::AnyUserData) -> mlua::Result<()>;
|
||||
}
|
||||
|
||||
impl<'a, T: Reflect + Clone + mlua::FromLua<'a> + mlua::UserData> LuaProxy for T {
|
||||
fn as_lua_value<'lua>(
|
||||
lua: &'lua mlua::Lua,
|
||||
this: &dyn Reflect,
|
||||
) -> mlua::Result<mlua::AnyUserData<'lua>> {
|
||||
fn as_lua_value<'lua>(lua: &'lua mlua::Lua, this: &dyn Reflect) -> mlua::Result<mlua::AnyUserData<'lua>> {
|
||||
let this = this.as_any().downcast_ref::<T>().unwrap();
|
||||
lua.create_userdata(this.clone())
|
||||
}
|
||||
|
||||
fn apply(
|
||||
_lua: &mlua::Lua,
|
||||
this: &mut dyn Reflect,
|
||||
apply: &mlua::AnyUserData,
|
||||
) -> mlua::Result<()> {
|
||||
fn apply(_lua: &mlua::Lua, this: &mut dyn Reflect, apply: &mlua::AnyUserData) -> mlua::Result<()> {
|
||||
let this = this.as_any_mut().downcast_mut::<T>().unwrap();
|
||||
let apply = apply.borrow::<T>()?;
|
||||
|
||||
|
@ -141,18 +106,11 @@ impl<'a, T: Reflect + Clone + mlua::FromLua<'a> + mlua::UserData> LuaProxy for T
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct ReflectLuaProxy {
|
||||
fn_as_uservalue:
|
||||
for<'a> fn(lua: &'a Lua, this_ptr: NonNull<u8>) -> mlua::Result<mlua::AnyUserData<'a>>,
|
||||
fn_apply: for<'a> fn(
|
||||
lua: &'a Lua,
|
||||
this_ptr: NonNull<u8>,
|
||||
apply: &'a mlua::AnyUserData<'a>,
|
||||
) -> mlua::Result<()>,
|
||||
fn_as_uservalue: for<'a> fn(lua: &'a Lua, this_ptr: NonNull<u8>) -> mlua::Result<mlua::AnyUserData<'a>>,
|
||||
fn_apply: for<'a> fn(lua: &'a Lua, this_ptr: NonNull<u8>, apply: &'a mlua::AnyUserData<'a>) -> mlua::Result<()>,
|
||||
}
|
||||
|
||||
impl<'a, T: Reflect + LuaProxy + Clone + mlua::FromLua<'a> + mlua::UserData> FromType<T>
|
||||
for ReflectLuaProxy
|
||||
{
|
||||
impl<'a, T: Reflect + LuaProxy + Clone + mlua::FromLua<'a> + mlua::UserData> FromType<T> for ReflectLuaProxy {
|
||||
fn from_type() -> Self {
|
||||
Self {
|
||||
fn_as_uservalue: |lua, this| -> mlua::Result<mlua::AnyUserData> {
|
||||
|
@ -162,7 +120,7 @@ impl<'a, T: Reflect + LuaProxy + Clone + mlua::FromLua<'a> + mlua::UserData> Fro
|
|||
fn_apply: |lua, ptr, apply| {
|
||||
let this = unsafe { ptr.cast::<T>().as_mut() };
|
||||
<T as LuaProxy>::apply(lua, this, apply)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -171,11 +129,7 @@ impl<'lua> mlua::FromLua<'lua> for ScriptDynamicBundle {
|
|||
fn from_lua(value: mlua::Value<'lua>, _lua: &'lua Lua) -> mlua::Result<Self> {
|
||||
match value {
|
||||
mlua::Value::UserData(ud) => Ok(ud.borrow::<Self>()?.clone()),
|
||||
mlua::Value::Nil => Err(mlua::Error::FromLuaConversionError {
|
||||
from: "Nil",
|
||||
to: "DynamicBundle",
|
||||
message: Some("Value was nil".to_string()),
|
||||
}),
|
||||
mlua::Value::Nil => Err(mlua::Error::FromLuaConversionError { from: "Nil", to: "DynamicBundle", message: Some("Value was nil".to_string()) }),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
@ -183,7 +137,9 @@ impl<'lua> mlua::FromLua<'lua> for ScriptDynamicBundle {
|
|||
|
||||
impl mlua::UserData for ScriptDynamicBundle {
|
||||
fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_function("new", |_, ()| Ok(ScriptDynamicBundle(DynamicBundle::new())));
|
||||
methods.add_function("new", |_, ()| {
|
||||
Ok(ScriptDynamicBundle(DynamicBundle::new()))
|
||||
});
|
||||
|
||||
methods.add_method_mut("push", |_, this, (comp,): (mlua::AnyUserData,)| {
|
||||
let script_brw = comp.call_method::<_, ScriptBorrow>(FN_NAME_INTERNAL_REFLECT, ())?;
|
||||
|
@ -198,84 +154,44 @@ impl mlua::UserData for ScriptDynamicBundle {
|
|||
}
|
||||
}
|
||||
|
||||
///
|
||||
/* fn lua_script_run_func(world: &mut World, function_name: &str) -> anyhow::Result<()> {
|
||||
|
||||
} */
|
||||
|
||||
/// A system that creates the script contexts in the world as new scripts are found
|
||||
pub fn lua_scripts_create_contexts(
|
||||
mut host: ResMut<LuaHost>,
|
||||
mut contexts: ResMut<ScriptContexts<LuaContext>>,
|
||||
mut providers: ResMut<ScriptApiProviders<LuaHost>>,
|
||||
view: View<(Entities, &ScriptList<LuaScript>)>,
|
||||
) -> anyhow::Result<()> {
|
||||
pub fn lua_scripts_create_contexts(mut host: ResMut<LuaHost>,
|
||||
mut contexts: ResMut<ScriptContexts<LuaContext>>,
|
||||
mut providers: ResMut<ScriptApiProviders<LuaHost>>,
|
||||
view: View<(Entities, &ScriptList<LuaScript>)>,
|
||||
) -> anyhow::Result<()> {
|
||||
|
||||
for (en, scripts) in view.into_iter() {
|
||||
for script in scripts.iter() {
|
||||
if !contexts.has_context(script.id()) {
|
||||
let script_data = ScriptData {
|
||||
name: script.name().to_string(),
|
||||
script_id: script.id(),
|
||||
entity: en,
|
||||
};
|
||||
let script_data = ScriptData {
|
||||
name: script.name().to_string(),
|
||||
script_id: script.id(),
|
||||
entity: en,
|
||||
};
|
||||
|
||||
if let Some(script_res) = &script.res_handle().try_data_ref() {
|
||||
if !contexts.has_context(script.id()) {
|
||||
if let Some(script_res) = &script.res_handle().data {
|
||||
debug!("Loading script '{}'...", script.name());
|
||||
let mut script_ctx =
|
||||
host.load_script(&script_res.bytes, &script_data, &mut providers)?;
|
||||
trace!("Finished loading script '{}'", script.name());
|
||||
let mut script_ctx = host.load_script(&script_res.bytes, &script_data, &mut providers).unwrap();
|
||||
debug!("Finished loading script '{}'", script.name());
|
||||
|
||||
debug!("Setting up script '{}'...", script.name());
|
||||
host.setup_script(&script_data, &mut script_ctx, &mut providers)?;
|
||||
trace!("Finished setting up script '{}'...", script.name());
|
||||
host.setup_script(&script_data, &mut script_ctx, &mut providers).unwrap();
|
||||
debug!("Finished setting up script '{}'...", script.name());
|
||||
|
||||
contexts.add_context(script.id(), script_ctx);
|
||||
} else {
|
||||
trace!("Script '{}' is not loaded yet, skipping for now", script.name());
|
||||
debug!("Script '{}' is not yet loaded, skipping", script.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A system that triggers a reload of watched script resources.
|
||||
///
|
||||
/// Note: This only works if the script is watched. See [`lyra_resource::ResourceManager::watch`].
|
||||
pub fn lua_scripts_reload_system(
|
||||
mut contexts: ResMut<ScriptContexts<LuaContext>>,
|
||||
mut resman: ResMut<ResourceManager>,
|
||||
view: View<&ScriptList<LuaScript>>,
|
||||
) -> anyhow::Result<()> {
|
||||
for scripts in view.into_iter() {
|
||||
for script in scripts.iter() {
|
||||
let handle = script.res_handle();
|
||||
if handle.is_watched() {
|
||||
let handle_path = handle.path();
|
||||
let watch_recv = resman.watcher_event_recv(&handle_path).unwrap();
|
||||
|
||||
match watch_recv.try_recv() {
|
||||
Ok(ev) => {
|
||||
let evs =
|
||||
ev.map_err(|e| anyhow!("Script watcher ran into errors: {:?}", e))?;
|
||||
|
||||
if evs.iter().any(|ev| ev.event.kind.is_modify()) {
|
||||
debug!(
|
||||
"Detected change of '{}' script, triggering reload",
|
||||
handle_path
|
||||
);
|
||||
|
||||
contexts.remove_context(script.id()).unwrap();
|
||||
resman.reload(handle)?;
|
||||
}
|
||||
}
|
||||
Err(e) => match e {
|
||||
lyra_resource::channel::TryRecvError::Empty => {}
|
||||
lyra_resource::channel::TryRecvError::Disconnected => {
|
||||
resman.stop_watching(&handle_path).unwrap();
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -294,31 +210,20 @@ fn lua_call_script_function(world: &mut World, stage_name: &str) -> anyhow::Resu
|
|||
};
|
||||
|
||||
if let Some(ctx) = contexts.get_context_mut(script.id()) {
|
||||
trace!(
|
||||
"Running '{}' function in script '{}'",
|
||||
stage_name,
|
||||
script.name()
|
||||
);
|
||||
|
||||
match host.call_script(
|
||||
world_ptr.clone(),
|
||||
&script_data,
|
||||
ctx,
|
||||
&mut providers,
|
||||
stage_name,
|
||||
) {
|
||||
Ok(()) => {}
|
||||
trace!("Running '{}' function in script '{}'", stage_name, script.name());
|
||||
match host.call_script(world_ptr.clone(), &script_data, ctx, &mut providers, stage_name) {
|
||||
Ok(()) => {},
|
||||
Err(e) => match e {
|
||||
ScriptError::MluaError(m) => {
|
||||
error!("Script '{}' ran into an error: {}", script.name(), m);
|
||||
}
|
||||
},
|
||||
ScriptError::Other(_) => return Err(e.into()),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -365,8 +270,7 @@ impl Plugin for LuaScriptingPlugin {
|
|||
world.add_resource_default::<ScriptApiProviders<LuaHost>>();
|
||||
world.add_resource_default::<ScriptContexts<LuaContext>>();
|
||||
|
||||
let mut loader = world
|
||||
.try_get_resource_mut::<ResourceManager>()
|
||||
let mut loader = world.try_get_resource_mut::<ResourceManager>()
|
||||
.expect("Add 'ResourceManager' to the world before trying to add this plugin");
|
||||
loader.register_loader::<LuaLoader>();
|
||||
drop(loader);
|
||||
|
@ -375,50 +279,15 @@ impl Plugin for LuaScriptingPlugin {
|
|||
game.add_script_api_provider::<LuaHost, _>(LyraEcsApiProvider);
|
||||
game.add_script_api_provider::<LuaHost, _>(LyraMathApiProvider);
|
||||
|
||||
game.add_system_to_stage(
|
||||
GameStages::First,
|
||||
"lua_create_contexts",
|
||||
lua_scripts_create_contexts,
|
||||
&[],
|
||||
)
|
||||
.add_system_to_stage(
|
||||
GameStages::First,
|
||||
"lua_reload_scripts",
|
||||
lua_scripts_reload_system,
|
||||
&["lua_create_contexts"],
|
||||
)
|
||||
.add_system_to_stage(
|
||||
GameStages::First,
|
||||
"lua_first_stage",
|
||||
lua_script_first_stage_system,
|
||||
&["lua_reload_scripts"],
|
||||
)
|
||||
// cannot depend on 'lua_create_contexts' since it will cause a panic.
|
||||
// the staged executor separates the executor of a single stage so this system
|
||||
// cannot depend on the other one.
|
||||
.add_system_to_stage(
|
||||
GameStages::PreUpdate,
|
||||
"lua_pre_update",
|
||||
lua_script_pre_update_stage_system,
|
||||
&[],
|
||||
)
|
||||
.add_system_to_stage(
|
||||
GameStages::Update,
|
||||
"lua_update",
|
||||
lua_script_update_stage_system,
|
||||
&[],
|
||||
)
|
||||
.add_system_to_stage(
|
||||
GameStages::PostUpdate,
|
||||
"lua_post_update",
|
||||
lua_script_post_update_stage_system,
|
||||
&[],
|
||||
)
|
||||
.add_system_to_stage(
|
||||
GameStages::Last,
|
||||
"lua_last_stage",
|
||||
lua_script_last_stage_system,
|
||||
&[],
|
||||
);
|
||||
game
|
||||
.add_system_to_stage(GameStages::First, "lua_create_contexts", lua_scripts_create_contexts, &[])
|
||||
.add_system_to_stage(GameStages::First, "lua_first_stage", lua_script_first_stage_system, &["lua_create_contexts"])
|
||||
// cannot depend on 'lua_create_contexts' since it will cause a panic.
|
||||
// the staged executor separates the executor of a single stage so this system
|
||||
// cannot depend on the other one.
|
||||
.add_system_to_stage(GameStages::PreUpdate, "lua_pre_update", lua_script_pre_update_stage_system, &[])
|
||||
.add_system_to_stage(GameStages::Update, "lua_update", lua_script_update_stage_system, &[])
|
||||
.add_system_to_stage(GameStages::PostUpdate, "lua_post_update", lua_script_post_update_stage_system, &[])
|
||||
.add_system_to_stage(GameStages::Last, "lua_last_stage", lua_script_last_stage_system, &[]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue