/// <summary>
/// Loads the Resource from the specified <see cref="Stream"/>. You shouldn't need this method in almost all cases.
/// Only use it when you know exactly what you're doing. Consider requesting the Resource from the <see cref="ContentProvider"/> instead.
/// </summary>
/// <typeparam name="T">
/// Desired Type of the returned reference. Does not affect the loaded Resource in any way - it is simply returned as T.
/// Results in returning null if the loaded Resource's Type isn't assignable to T.
/// </typeparam>
/// <param name="str">The stream to load the Resource from.</param>
/// <param name="resPath">The path that is assumed as the loaded Resource's origin.</param>
/// <param name="loadCallback">An optional callback that is invoked right after loading the Resource, but before initializing it.</param>
/// <param name="initResource">
/// Specifies whether or not the Resource is initialized by calling <see cref="Resource.OnLoaded"/>. Never attempt to use
/// uninitialized Resources or register them in the ContentProvider.
/// </param>
/// <returns>The Resource that has been loaded.</returns>
public static T Load <T>(Formatter formatter, string resPath = null, Action <T> loadCallback = null, bool initResource = true) where T : Resource
{
T newContent = null;
try
{
Resource res = formatter.ReadObject <Resource>();
if (res == null)
{
throw new ApplicationException("Loading Resource failed");
}
res.initState = InitState.Initializing;
res.path = resPath;
if (loadCallback != null)
{
loadCallback(res as T); // Callback before initializing.
}
if (initResource)
{
Init(res);
}
newContent = res as T;
}
catch (Exception e)
{
Log.Core.WriteError("Can't load {0} from '{1}', because an error occurred: {3}{2}",
Log.Type(typeof(T)),
resPath ?? formatter.ToString(),
Log.Exception(e),
Environment.NewLine);
}
return(newContent);
}