//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
/** @addtogroup Resources
* @{
*/
///
/// Handles dynamic loading of resources during runtime.
///
public static class Resources
{
///
/// Loads a resource at the specified path. If resource is already loaded an existing instance is returned.
///
///
/// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
/// running inside the editor this has similar functionality as if loading using the project library.
///
/// Type of the resource.
/// Path of the resource, relative to game directory. If running from editor this will be
/// relative to the project library. If a sub-resource within a file is needed, append the name
/// of the subresource to the path (for example mymesh.fbx/my_animation).
/// Flags used to control the load process.
/// Loaded resource, or null if resource cannot be found.
public static T Load(string path, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
{
return (T)Internal_Load(path, flags);
}
///
/// Loads a resource with the specified UUID. If resource is already loaded an existing instance is returned.
///
///
/// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
/// running inside the editor this has similar functionality as if loading using the project library.
///
/// Type of the resource.
/// Unique identifier of the resource to load.
/// Flags used to control the load process.
/// Loaded resource, or null if resource cannot be found.
public static T Load(UUID uuid, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
{
return (T)Internal_LoadFromUUID(ref uuid, flags);
}
///
/// Loads a resource at the specified path asynchonously (on a separate thread). If resource is already loaded
/// an existing instance is returned. Use to confirm the resource has been loaded
/// before using it. Use to track the loading progress of the resource.
///
///
/// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
/// running inside the editor this has similar functionality as if loading using the project library.
///
/// Type of the resource.
/// Path of the resource, relative to game directory. If running from editor this will be
/// relative to the project library. If a sub-resource within a file is needed, append the name
/// of the subresource to the path (for example mymesh.fbx/my_animation).
/// Flags used to control the load process.
/// Loaded resource, or null if resource cannot be found.
public static RRef LoadAsync(string path, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
{
return Internal_LoadAsync(path, flags).As();
}
///
/// Loads a resource with the specified UUID asynchonously (on a separate thread). If resource is already loaded
/// an existing instance is returned. Use to confirm the resource has been loaded
/// before using it. Use to track the loading progress of the resource.
///
///
/// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
/// running inside the editor this has similar functionality as if loading using the project library.
///
/// Type of the resource.
/// Unique identifier of the resource to load.
/// Flags used to control the load process.
/// Loaded resource, or null if resource cannot be found.
public static RRef LoadAsync(UUID uuid, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
{
return Internal_LoadAsyncFromUUID(ref uuid, flags).As();
}
///
/// Releases an internal reference to the resource held by the resources system. This allows the resource
/// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter.
///
/// Alternatively you can also skip manually calling and call
/// which will unload all resources that do not have any external references, but you lose the fine grained control
/// of what will be unloaded.
///
/// Resource to release
public static void Release(RRefBase resource)
{
if (resource == null)
return;
Internal_ReleaseRef(resource.GetCachedPtr());
}
///
/// Releases an internal reference to the resource held by the resources system. This allows the resource
/// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter.
///
/// Alternatively you can also skip manually calling and call
/// which will unload all resources that do not have any external references, but you lose the fine grained control
/// of what will be unloaded.
///
/// Resource to release
public static void Release(Resource resource)
{
if(resource == null)
return;
Internal_Release(resource.GetCachedPtr());
}
///
/// Unloads all resources that are no longer referenced. This only applies to resources loaded with "keepLoaded"
/// parameter, as all other resources will be unloaded when they go out of scope.
///
public static void UnloadUnused()
{
Internal_UnloadUnused();
}
///
/// Returns the loading progress of a resource that's being asynchronously loaded.
///
/// Resource whose load progress to check.
/// If false the progress will reflect the load progress only for this individual
/// resource. If true the progress will reflect load progress of this resource
/// and all of its dependencies.
/// Load progress in range [0, 1].
public static float GetLoadProgress(RRefBase resource, bool includeDependencies = true)
{
if(resource == null)
return 0.0f;
return Internal_GetLoadProgress(resource.GetCachedPtr(), includeDependencies);
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern Resource Internal_Load(string path, ResourceLoadFlag flags);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern Resource Internal_LoadFromUUID(ref UUID uuid, ResourceLoadFlag flags);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern RRefBase Internal_LoadAsync(string path, ResourceLoadFlag flags);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern RRefBase Internal_LoadAsyncFromUUID(ref UUID uuid, ResourceLoadFlag flags);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern float Internal_GetLoadProgress(IntPtr resource, bool loadDependencies);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_Release(IntPtr resource);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_ReleaseRef(IntPtr resource);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_UnloadUnused();
}
/** @} */
}