Compare commits

...

3 Commits

16 changed files with 688 additions and 122 deletions

140
Cargo.lock generated
View File

@ -590,6 +590,19 @@ 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"
@ -622,6 +635,15 @@ 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"
@ -814,6 +836,27 @@ 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"
@ -854,6 +897,15 @@ 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"
@ -940,7 +992,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0af1827b7dd2f36d740ae804c1b3ea0d64c12533fb61ff91883005143a0e8c5a"
dependencies = [
"core-foundation",
"inotify",
"inotify 0.10.2",
"io-kit-sys",
"js-sys",
"libc",
@ -1196,6 +1248,17 @@ 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"
@ -1317,6 +1380,26 @@ 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"
@ -1505,12 +1588,15 @@ 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",
@ -1736,6 +1822,39 @@ 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"
@ -2206,6 +2325,15 @@ 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"
@ -2680,6 +2808,16 @@ 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"

View File

@ -16,7 +16,7 @@ function on_update()
--print("Lua's update function was called")
world:view(function (t)
print("Found entity at " .. tostring(t))
--print("Found entity at a really cool place: " .. tostring(t))
t.translation = t.translation + Vec3.new(0, 0.0008, 0)
return t

View File

@ -306,6 +306,7 @@ 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);

View File

@ -8,8 +8,6 @@ pub use spotlight::*;
use std::{collections::{VecDeque, HashMap}, marker::PhantomData};
use tracing::debug;
use std::mem;
use crate::math::Transform;
@ -174,7 +172,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");
}
}

View File

@ -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.as_ref().unwrap().image)
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), i, None).unwrap());
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 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());
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());
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.as_ref().unwrap().image)
.map(|i| RenderTexture::from_image(device, queue, bg_layout.clone(), i, None).unwrap());
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 specular = value.specular.as_ref().map(|s| MaterialSpecular::from_resource(device, queue, bg_layout.clone(), s));

View File

@ -430,7 +430,7 @@ impl Renderer for BasicRenderer {
let transform_val = cached.from_transform.lerp(cached.to_transform, alpha);
let model = model.data.as_ref().unwrap().as_ref();
let model = model.data_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);

View File

@ -1,7 +1,7 @@
use std::sync::Arc;
use image::GenericImageView;
use lyra_resource::{Resource, Texture};
use lyra_resource::{ResHandle, 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<Resource<Texture>>) {
let texture = &texture.data.as_ref().unwrap().image;
pub fn update_texture(&mut self, _device: &wgpu::Device, queue: &wgpu::Queue, texture: &Arc<ResHandle<Texture>>) {
let texture = &texture.data_ref().image;
let rgba = texture.to_rgba8();
let dimensions = texture.dimensions();
let size = wgpu::Extent3d {

View File

@ -8,6 +8,7 @@ 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"] }
@ -15,6 +16,9 @@ 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"

View File

@ -17,3 +17,6 @@ pub mod material;
pub use material::*;
pub(crate) mod util;
pub use crossbeam::channel as channel;
pub use notify;

View File

@ -2,7 +2,7 @@ use std::{fs::File, sync::Arc, io::Read};
use image::ImageError;
use crate::{resource_manager::ResourceStorage, texture::Texture, resource::Resource, ResourceManager};
use crate::{resource_manager::ResourceStorage, texture::Texture, resource::ResHandle, ResourceManager};
use super::{LoaderError, ResourceLoader};
@ -62,7 +62,7 @@ impl ResourceLoader for ImageLoader {
let texture = Texture {
image,
};
let res = Resource::with_data(path, texture);
let res = ResHandle::with_data(path, texture);
Ok(Arc::new(res))
}
@ -76,7 +76,7 @@ impl ResourceLoader for ImageLoader {
let texture = Texture {
image,
};
let res = Resource::with_data(&uuid::Uuid::new_v4().to_string(), texture);
let res = ResHandle::with_data(&uuid::Uuid::new_v4().to_string(), texture);
Ok(Arc::new(res))
}

View File

@ -2,7 +2,7 @@ use std::{sync::Arc, path::PathBuf};
use thiserror::Error;
use crate::{ResourceLoader, LoaderError, Mesh, Model, MeshVertexAttribute, VertexAttributeData, Resource, Material, MeshIndices, ResourceManager, util};
use crate::{ResourceLoader, LoaderError, Mesh, Model, MeshVertexAttribute, VertexAttributeData, ResHandle, 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(Resource::with_data(path, Model::new(meshes))))
Ok(Arc::new(ResHandle::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::<Resource<Model>>(model.as_arc_any()).unwrap();
let model = model.data.as_ref().unwrap();
let model = Arc::downcast::<ResHandle<Model>>(model.as_arc_any()).unwrap();
let model = model.data_ref();
assert_eq!(model.meshes.len(), 1); // There should only be 1 mesh
let mesh = &model.meshes[0];
assert!(mesh.position().unwrap().len() > 0);

View File

@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, RwLock};
use uuid::Uuid;
@ -8,25 +8,148 @@ pub enum ResourceState {
Ready,
}
#[derive(Clone)]
pub struct Resource<T> {
pub path: String,
pub data: Option<Arc<T>>,
pub uuid: Uuid,
pub state: ResourceState,
pub struct ResourceDataRef<'a, T> {
guard: std::sync::RwLockReadGuard<'a, Resource<T>>,
}
/// A helper type to make it easier to use resources
pub type ResHandle<T> = Arc<Resource<T>>;
impl<'a, T> std::ops::Deref for ResourceDataRef<'a, T> {
type Target = 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 {
Self {
path: path.to_string(),
data: Some(Arc::new(data)),
uuid: Uuid::new_v4(),
state: ResourceState::Ready,
}
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> {
/// 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)),
}
}
/// 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()
} */
}

View File

@ -1,17 +1,22 @@
use std::{sync::Arc, collections::HashMap, any::Any};
use std::{sync::{Arc, RwLock}, collections::HashMap, any::Any, path::Path, time::Duration};
use crossbeam::channel::Receiver;
use notify::{Watcher, RecommendedWatcher};
use notify_debouncer_full::{DebouncedEvent, FileIdMap};
use thiserror::Error;
use crate::{resource::Resource, loader::{ResourceLoader, LoaderError, image::ImageLoader, model::ModelLoader}};
use crate::{resource::ResHandle, 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 T {
impl<T: Send + Sync + 'static> ResourceStorage for ResHandle<T> {
fn as_any(&self) -> &dyn Any {
self
}
@ -23,6 +28,15 @@ impl<T: Send + Sync + 'static> ResourceStorage for 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)]
@ -46,9 +60,16 @@ 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 {
@ -62,6 +83,7 @@ impl ResourceManager {
Self {
resources: HashMap::new(),
loaders: vec![ Arc::new(ImageLoader), Arc::new(ModelLoader) ],
watchers: HashMap::new(),
}
}
@ -73,11 +95,15 @@ impl ResourceManager {
self.loaders.push(Arc::new(L::default()));
}
pub fn request<T: Send + Sync + Any + 'static>(&mut self, path: &str) -> Result<Arc<Resource<T>>, RequestError> {
pub fn request<T>(&mut self, path: &str) -> Result<ResHandle<T>, RequestError>
where
T: Send + Sync + Any + 'static
{
match self.resources.get(&path.to_string()) {
Some(res) => {
let res = res.clone().as_arc_any();
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
let res: Arc<ResHandle<T>> = res.downcast::<ResHandle<T>>().expect("Failure to downcast resource");
let res = ResHandle::<T>::clone(&res);
Ok(res)
},
@ -88,12 +114,14 @@ 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::<Resource<T>>()
let res = res.downcast::<ResHandle<T>>()
.expect("Failure to downcast resource! Does the loader return an `Arc<Resource<T>>`?");
let res = ResHandle::<T>::clone(&res);
Ok(res)
} else {
@ -112,18 +140,23 @@ impl ResourceManager {
/// * `bytes` - The bytes to store.
///
/// Returns: The `Arc` to the now stored resource
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> {
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
{
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::<Resource<T>>()
let res = res.downcast::<ResHandle<T>>()
.expect("Failure to downcast resource! Does the loader return an `Arc<Resource<T>>`?");
let res = ResHandle::<T>::clone(&res);
Ok(res)
} else {
@ -132,11 +165,14 @@ impl ResourceManager {
}
/// Requests bytes from the manager.
pub fn request_loaded_bytes<T: Send + Sync + Any + 'static>(&mut self, ident: &str) -> Result<Arc<Resource<T>>, RequestError> {
pub fn request_loaded_bytes<T>(&mut self, ident: &str) -> Result<Arc<ResHandle<T>>, RequestError>
where
T: Send + Sync + Any + 'static
{
match self.resources.get(&ident.to_string()) {
Some(res) => {
let res = res.clone().as_arc_any();
let res = res.downcast::<Resource<T>>().expect("Failure to downcast resource");
let res = res.downcast::<ResHandle<T>>().expect("Failure to downcast resource");
Ok(res)
},
@ -145,6 +181,90 @@ 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)]
@ -165,20 +285,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.data.as_ref();
assert_eq!(res.state(), ResourceState::Ready);
let img = res.try_data_ref();
img.unwrap();
}
/// Ensures that only one copy of the same thing made
/// Ensures that only one copy of the same data was 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), 2);
assert_eq!(Arc::strong_count(&res.data), 2);
let resagain = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
assert_eq!(Arc::strong_count(&resagain), 3);
assert_eq!(Arc::strong_count(&resagain.data), 3);
}
/// Ensures that an error is returned when a file that doesn't exist is requested
@ -196,4 +316,42 @@ 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()));
}
}

View File

@ -43,7 +43,9 @@ 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) {}
fn prepare_world(&mut self, world: &mut World) {
let _ = world; // remove compiler warning
}
/// Exposes an API in the provided script context.
fn expose_api(&mut self, ctx: &mut Self::ScriptContext) -> Result<(), ScriptError>;
@ -120,4 +122,12 @@ 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()
}
}

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use lyra_resource::{ResourceLoader, Resource};
use lyra_resource::{ResourceLoader, ResHandle};
#[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 = Resource::with_data(path, LuaScript {
let s = ResHandle::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 = Resource::with_data("from bytes", LuaScript {
let s = ResHandle::with_data("from bytes", LuaScript {
bytes
});

View File

@ -1,8 +1,9 @@
pub mod dynamic_iter;
use anyhow::anyhow;
pub use dynamic_iter::*;
pub mod world;
use lyra_game::{plugin::Plugin, game::GameStages};
use lyra_game::{game::GameStages, plugin::Plugin};
use lyra_resource::ResourceManager;
use tracing::{debug, error, trace};
pub use world::*;
@ -19,32 +20,47 @@ pub mod wrappers;
#[cfg(test)]
mod test;
use std::{ptr::NonNull, sync::Mutex, any::TypeId};
use std::{any::TypeId, ptr::NonNull, sync::Mutex};
use lyra_ecs::{DynamicBundle, World, query::{ResMut, View, Entities}};
use lyra_reflect::{Reflect, FromType, RegisteredType, TypeRegistry};
use lyra_ecs::{
query::{Entities, ResMut, View},
DynamicBundle, World,
};
use lyra_reflect::{FromType, Reflect, TypeRegistry};
use mlua::{Lua, AnyUserDataExt};
use mlua::{AnyUserDataExt, Lua};
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::{ScriptBorrow, ScriptDynamicBundle, ScriptApiProviders, ScriptContexts, ScriptWorldPtr, ScriptList, ScriptData, ScriptHost, ScriptError, GameScriptExt};
use crate::{
GameScriptExt, ScriptApiProviders, ScriptBorrow, ScriptContexts, ScriptData,
ScriptDynamicBundle, ScriptError, ScriptHost, ScriptList, ScriptWorldPtr,
};
use self::providers::{UtilityApiProvider, LyraMathApiProvider, LyraEcsApiProvider};
use self::providers::{LyraEcsApiProvider, LyraMathApiProvider, UtilityApiProvider};
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>();
@ -54,7 +70,12 @@ 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());
@ -84,17 +105,31 @@ 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>()?;
@ -106,11 +141,18 @@ 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> {
@ -120,7 +162,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)
}
},
}
}
}
@ -129,7 +171,11 @@ 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!(),
}
}
@ -137,9 +183,7 @@ 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, ())?;
@ -154,39 +198,79 @@ 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() {
let script_data = ScriptData {
name: script.name().to_string(),
script_id: script.id(),
entity: en,
};
if !contexts.has_context(script.id()) {
if let Some(script_res) = &script.res_handle().data {
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() {
debug!("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());
let mut script_ctx =
host.load_script(&script_res.bytes, &script_data, &mut providers)?;
trace!("Finished loading script '{}'", script.name());
debug!("Setting up script '{}'...", script.name());
host.setup_script(&script_data, &mut script_ctx, &mut providers).unwrap();
debug!("Finished setting up script '{}'...", script.name());
host.setup_script(&script_data, &mut script_ctx, &mut providers)?;
trace!("Finished setting up script '{}'...", script.name());
contexts.add_context(script.id(), script_ctx);
} else {
debug!("Script '{}' is not yet loaded, skipping", script.name());
trace!("Script '{}' is not loaded yet, skipping for now", 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();
}
},
}
}
}
@ -210,13 +294,24 @@ 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()),
},
}
@ -270,7 +365,8 @@ 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);
@ -279,15 +375,50 @@ 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_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, &[]);
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,
&[],
);
}
}