Skip to main content

BuiltInResource

Struct BuiltInResource 

Source
pub struct BuiltInResource<T>{
    pub id: PathBuf,
    pub data_source: Option<DataSource>,
    pub resource: Resource<T>,
}
Expand description

Built-in resource is a resource embedded in the application executable file. It is a very useful mechanism when you need to bundle all game resources and put them in the executable file.

§Registration

Every built-in resource must be registered in a resource manager to be accessible via standard crate::manager::ResourceManager::request method. It could be done pretty easily:

use fyrox_resource::{
    builtin::BuiltInResource,
    core::{reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*, Uuid},
    manager::ResourceManager,
    Resource, ResourceData,
};
use std::{error::Error, path::Path};

#[derive(TypeUuidProvider, Default, Debug, Clone, Visit, Reflect)]
#[type_uuid(id = "00d036bb-fbed-47f7-94e3-b3fce93dee17")]
struct MyResource {
    some_data: String,
}

impl ResourceData for MyResource {
    fn type_uuid(&self) -> Uuid {
        <Self as TypeUuidProvider>::type_uuid()
    }

    fn save(&mut self, _path: &Path) -> Result<(), Box<dyn Error>> {
        Ok(())
    }

    fn can_be_saved(&self) -> bool {
        false
    }

    fn try_clone_box(&self) -> Option<Box<dyn ResourceData>> {
        Some(Box::new(self.clone()))
    }
}

fn register_built_in_resource(resource_manager: &ResourceManager) {
    let id = "MyResourceId";
    let some_data = "This string is a built-in resource with MyResourceId id.";

    let resource = Resource::new_embedded(MyResource {
        some_data: some_data.into(),
    });

    resource_manager.register_built_in_resource(BuiltInResource::new_no_source(id, resource));

    assert_eq!(
        resource_manager
            .request::<MyResource>(id)
            .data_ref()
            .some_data,
        some_data,
    )
}

Fields§

§id: PathBuf

Id of the built-in resource.

§data_source: Option<DataSource>

Initial data, from which the resource is created from.

§resource: Resource<T>

Ready-to-use (“loaded”) resource.

Implementations§

Source§

impl<T: TypedResourceData> BuiltInResource<T>

Source

pub fn new<F>(id: impl AsRef<Path>, data_source: DataSource, make: F) -> Self
where F: FnOnce(&[u8]) -> Resource<T>,

Creates a new built-in resource with an id, a data source and function that creates the resource from the given data source.

Source

pub fn new_no_source(id: impl AsRef<Path>, resource: Resource<T>) -> Self

Creates a new built-in resource from an id and arbitrary resource.

Source

pub fn resource(&self) -> Resource<T>

Returns the wrapped resource instance.

Trait Implementations§

Source§

impl<T: TypedResourceData> Clone for BuiltInResource<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: TypedResourceData> From<BuiltInResource<T>> for UntypedBuiltInResource

Source§

fn from(value: BuiltInResource<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for BuiltInResource<T>

§

impl<T> !RefUnwindSafe for BuiltInResource<T>

§

impl<T> Send for BuiltInResource<T>

§

impl<T> Sync for BuiltInResource<T>
where T: Sync,

§

impl<T> Unpin for BuiltInResource<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for BuiltInResource<T>

§

impl<T> !UnwindSafe for BuiltInResource<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsyncTaskResult for T
where T: Any + Send + 'static,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> ObjectOrVariant<T> for U

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V