#pragma warning disable 672
using System;
using System.IO;
using TriLibCore.Mappers;
namespace TriLibCore.Samples
{
///
/// A custom implementation that uses user-supplied callbacks
/// to handle external data loading. This is useful when the model references additional files
/// (e.g., textures, external geometry, etc.) and you want fine-grained control over how and
/// where those files are retrieved.
///
public class SimpleExternalDataMapper : ExternalDataMapper
{
///
/// The callback responsible for returning a to load the external file content.
///
private Func _streamReceivingCallback;
///
/// The callback responsible for providing a full or modified file path from the given original path.
///
private Func _finalPathReceivingCallback;
///
/// Configures the callbacks used for external data mapping.
///
/// A required callback that returns the used to read the requested file’s content.
/// An optional callback that modifies or resolves the final file path before loading.
/// Thrown if is null.
public void Setup(Func streamReceivingCallback, Func finalPathReceivingCallback)
{
if (streamReceivingCallback == null)
{
throw new Exception("Callback parameter is missing.");
}
_streamReceivingCallback = streamReceivingCallback;
_finalPathReceivingCallback = finalPathReceivingCallback;
}
///
/// Overrides the default mapping logic to use the user-supplied callbacks.
///
///
/// When TriLib needs to load external data (e.g., texture files), it calls this method,
/// allowing you to provide custom behavior such as streaming from memory or from an alternative storage location.
///
/// The current TriLib asset loading context containing model-related state.
/// The original filename (or path) referencing external data.
/// The resolved final path of the file. This may be modified by your custom callback.
/// An open containing the external file data.
public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
{
// Use custom logic to resolve final filename/path
finalPath = _finalPathReceivingCallback != null ? _finalPathReceivingCallback(originalFilename) : originalFilename;
// Retrieve a Stream for reading the external data
return _streamReceivingCallback(originalFilename);
}
}
}