Skip to content

Instantly share code, notes, and snippets.

@kkolyan
Created October 8, 2023 18:09
Show Gist options
  • Save kkolyan/e1e5a5688c7e065acc7574b5f9930058 to your computer and use it in GitHub Desktop.
Save kkolyan/e1e5a5688c7e065acc7574b5f9930058 to your computer and use it in GitHub Desktop.
use std::any::Any;
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use fyrox::asset::loader::{BoxedLoaderFuture, ResourceLoader};
use fyrox::asset::ResourceData;
use fyrox::asset::untyped::UntypedResource;
use fyrox::core::reflect::prelude::*;
use fyrox::core::{io, TypeUuidProvider};
use fyrox::core::uuid::{Uuid, uuid};
use fyrox::core::visitor::prelude::*;
use fyrox::asset::event::ResourceEventBroadcaster;
fn dsa () {
}
#[derive(Debug, Visit, Reflect)]
pub struct UnifiedCharacterConfig {
// You resource must store the path.
path: PathBuf,
some_data: String,
}
impl TypeUuidProvider for UnifiedCharacterConfig {
// Every resource must provide a unique identifier, that is used for dynamic type
// casting, serialization, etc.
fn type_uuid() -> Uuid {
uuid!("82795c5f-523b-4d39-8a1c-024386471380")
}
}
impl ResourceData for UnifiedCharacterConfig {
fn path(&self) -> Cow<Path> {
Cow::Borrowed(&self.path)
}
fn set_path(&mut self, path: PathBuf) {
self.path = path;
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn type_uuid(&self) -> Uuid {
<Self as TypeUuidProvider>::type_uuid()
}
}
pub(crate) struct CustomResourceLoader;
impl ResourceLoader for CustomResourceLoader {
fn extensions(&self) -> &[&str] {
// An array of extensitions, supported by this loader. There could be any number of extensions
// since sometimes multiple extensions map to a single resource (for instance, jpg, png, bmp, are
// all images).
&["my_resource"]
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn load(
&self,
resource: UntypedResource,
event_broadcaster: ResourceEventBroadcaster,
reload: bool,
) -> BoxedLoaderFuture {
Box::pin(async move {
let path = resource.path();
match io::load_file(&path).await {
Ok(content) => {
let my_resource = UnifiedCharacterConfig {
path,
some_data: String::from_utf8(content).unwrap(),
};
resource.commit_ok(my_resource);
// Notify potential subscribers that the resource was loader.
event_broadcaster.broadcast_loaded_or_reloaded(resource, reload);
}
Err(err) => {
resource.commit_error(path, err);
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment