#pragma warning disable 672
using System;
using System.IO;
using TriLibCore.Interfaces;
using TriLibCore.Mappers;
using TriLibCore.Utils;
using UnityEngine;
namespace TriLibCore.Samples
{
///
/// Custom external data mapper which works with callbacks.
///
public class SimpleExternalDataMapper : ExternalDataMapper
{
private Func _streamReceivingCallback;
private Func _finalPathReceivingCallback;
public void Setup(Func streamReceivingCallback, Func finalPathReceivingCallback)
{
if (streamReceivingCallback == null)
{
throw new Exception("Callback parameter is missing.");
}
_streamReceivingCallback = streamReceivingCallback;
_finalPathReceivingCallback = finalPathReceivingCallback;
}
public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
{
finalPath = _finalPathReceivingCallback != null ? _finalPathReceivingCallback(originalFilename) : originalFilename;
return _streamReceivingCallback(originalFilename);
}
}
///
/// Custom texture mapper which works with callbacks.
///
public class SimpleTextureMapper : TextureMapper
{
private Func _streamReceivingCallback;
public void Setup(Func streamReceivingCallback)
{
if (streamReceivingCallback == null)
{
throw new Exception("Callback parameter is missing.");
}
_streamReceivingCallback = streamReceivingCallback;
}
public override void Map(TextureLoadingContext textureLoadingContext)
{
var stream = _streamReceivingCallback(textureLoadingContext.Texture);
textureLoadingContext.Stream = stream;
}
}
///
/// Represents a class used to load models from Byte Arrays using callbacks to map External Data and Textures.
///
public class SimpleCustomAssetLoader
{
///
/// Loads a model from the given Byte Array data using the given callbacks to handle events/external data.
///
/// The model data Byte Array.
/// The model file extension.
/// The error event callback (optional).
/// The loading progress event callback.
/// The model loading event callback.
/// The event that returns a Stream to read the external data passed to it.
/// The event that returns a file-system complete filename from the filename passed to it (optional).
/// The event that returns a Stream to read Texture data from the filename passed to it.
/// The model filename (optional).
/// The GameObject to wrap the loaded model (optional).
/// The AssetLoaderOptions to use when loading the model (optional).
/// Any custom data to pass to the loading method, which can be retrieved later (optional).
/// The AssetLoaderContext containing all common data regarding the model loading.
public static AssetLoaderContext LoadModelFromByteData(
byte[] data,
string modelExtension,
Action onError,
Action onProgress,
Action onModelFullyLoad,
Func customDataReceivingCallback,
Func customFilenameReceivingCallback,
Func customTextureReceivingCallback,
string modelFilename = null,
GameObject wrapperGameObject = null,
AssetLoaderOptions assetLoaderOptions = null,
object customData = null)
{
if (data == null || data.Length == 0)
{
throw new Exception("Missing model file byte data.");
}
return LoadModelFromStream(new MemoryStream(data), modelExtension, onError, onProgress, onModelFullyLoad, customDataReceivingCallback, customFilenameReceivingCallback, customTextureReceivingCallback, modelFilename, wrapperGameObject, assetLoaderOptions, customData);
}
///
/// Loads a model from the given Byte Array data using the given callbacks to handle events/external data.
///
/// The model data Stream.
/// The model file extension.
/// The error event callback (optional).
/// The loading progress event callback.
/// The model loading event callback.
/// The event that returns a Stream to read the external data passed to it.
/// The event that returns a file-system complete filename from the filename passed to it (optional).
/// The event that returns a Stream to read Texture data from the filename passed to it.
/// The model filename (optional).
/// The GameObject to wrap the loaded model (optional).
/// The AssetLoaderOptions to use when loading the model (optional).
/// Any custom data to pass to the loading method, which can be retrieved later (optional).
/// The AssetLoaderContext containing all common data regarding the model loading.
public static AssetLoaderContext LoadModelFromStream(
Stream stream,
string modelExtension,
Action onError,
Action onProgress,
Action onModelFullyLoad,
Func customDataReceivingCallback,
Func customFilenameReceivingCallback,
Func customTextureReceivingCallback,
string modelFilename = null,
GameObject wrapperGameObject = null,
AssetLoaderOptions assetLoaderOptions = null,
object customData = null)
{
if (stream == null)
{
throw new Exception("Missing model file byte data.");
}
if (string.IsNullOrWhiteSpace(modelExtension) && !string.IsNullOrWhiteSpace(modelFilename))
{
modelExtension = FileUtils.GetFileExtension(modelFilename);
}
if (string.IsNullOrWhiteSpace(modelExtension))
{
throw new Exception("Missing model extension parameter");
}
var simpleExternalDataMapper = ScriptableObject.CreateInstance();
simpleExternalDataMapper.Setup(customDataReceivingCallback, customFilenameReceivingCallback);
var simpleTextureMapper = ScriptableObject.CreateInstance();
simpleTextureMapper.Setup(customTextureReceivingCallback);
if (assetLoaderOptions == null)
{
assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
}
assetLoaderOptions.ExternalDataMapper = simpleExternalDataMapper;
assetLoaderOptions.TextureMappers = new TextureMapper[] { simpleTextureMapper};
return AssetLoader.LoadModelFromStream(stream, modelFilename, modelExtension, null, onModelFullyLoad, onProgress, onError, wrapperGameObject, assetLoaderOptions, customData);
}
}
}