3using ICSharpCode.SharpZipLib.Zip;
13 public static class AssetLoaderZip
18 private const int ZipBufferSize = 4096;
24 private static void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
26 var zipLoadCustomContextData = assetLoaderContext.CustomData as ZipLoadCustomContextData;
27 if (zipLoadCustomContextData !=
null)
29 zipLoadCustomContextData.Stream.Close();
30 zipLoadCustomContextData.OnMaterialsLoad(assetLoaderContext);
38 private static void OnError(IContextualizedError contextualizedError)
40 var assetLoaderContext = contextualizedError?.GetContext() as AssetLoaderContext;
41 var zipLoadCustomContextData = assetLoaderContext?.CustomData as ZipLoadCustomContextData;
42 zipLoadCustomContextData?.Stream.Close();
43 zipLoadCustomContextData?.OnError?.Invoke(contextualizedError);
57 public static AssetLoaderContext LoadModelFromZipFile(
string path, Action<AssetLoaderContext> onLoad, Action<AssetLoaderContext> onMaterialsLoad, Action<AssetLoaderContext, float> onProgress, Action<IContextualizedError> onError =
null, GameObject wrapperGameObject =
null,
AssetLoaderOptions assetLoaderOptions =
null,
object customContextData =
null,
string fileExtension =
null)
60 var memoryStream = SetupZipModelLoading(onError, ref stream, path, ref assetLoaderOptions, ref fileExtension, out var zipFile);
61 return AssetLoader.LoadModelFromStream(memoryStream, path, fileExtension, onLoad, OnMaterialsLoad, onProgress, OnError, wrapperGameObject, assetLoaderOptions,
new ZipLoadCustomContextData
65 CustomData = customContextData,
67 OnMaterialsLoad = onMaterialsLoad
82 public static AssetLoaderContext LoadModelFromZipStream(Stream stream, Action<AssetLoaderContext> onLoad, Action<AssetLoaderContext> onMaterialsLoad, Action<AssetLoaderContext, float> onProgress, Action<IContextualizedError> onError =
null, GameObject wrapperGameObject =
null,
AssetLoaderOptions assetLoaderOptions =
null,
object customContextData =
null,
string fileExtension =
null)
84 var memoryStream = SetupZipModelLoading(onError, ref stream,
null, ref assetLoaderOptions, ref fileExtension, out var zipFile);
85 return AssetLoader.LoadModelFromStream(memoryStream,
null, fileExtension, onLoad, OnMaterialsLoad, onProgress, OnError, wrapperGameObject, assetLoaderOptions,
new ZipLoadCustomContextData
89 CustomData = customContextData,
91 OnMaterialsLoad = onMaterialsLoad
103 public static AssetLoaderContext LoadModelFromZipFileNoThread(
string path, Action<IContextualizedError> onError =
null, GameObject wrapperGameObject =
null,
AssetLoaderOptions assetLoaderOptions =
null,
object customContextData =
null,
string fileExtension =
null)
105 Stream stream =
null;
106 var memoryStream = SetupZipModelLoading(onError,ref stream, path, ref assetLoaderOptions, ref fileExtension, out var zipFile);
107 var assetLoaderContext = AssetLoader.LoadModelFromStreamNoThread(memoryStream, path, fileExtension, OnError, wrapperGameObject, assetLoaderOptions,
new ZipLoadCustomContextData
110 CustomData = customContextData,
114 return assetLoaderContext;
125 public static AssetLoaderContext LoadModelFromZipStreamNoThread(Stream stream, Action<IContextualizedError> onError, GameObject wrapperGameObject =
null,
AssetLoaderOptions assetLoaderOptions =
null,
object customContextData =
null,
string fileExtension =
null)
127 var memoryStream = SetupZipModelLoading(onError,ref stream,
null, ref assetLoaderOptions, ref fileExtension, out var zipFile);
128 var assetLoaderContext = AssetLoader.LoadModelFromStreamNoThread(memoryStream,
null, fileExtension, OnError, wrapperGameObject, assetLoaderOptions,
new ZipLoadCustomContextData
131 CustomData = customContextData,
135 return assetLoaderContext;
146 private static Stream SetupZipModelLoading(Action<IContextualizedError> onError, ref Stream stream,
string path, ref
AssetLoaderOptions assetLoaderOptions, ref
string fileExtension, out ZipFile zipFile)
148 if (assetLoaderOptions ==
null)
150 assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
154 assetLoaderOptions.FixedAllocations.Add(assetLoaderOptions.TextureMapper);
155 assetLoaderOptions.FixedAllocations.Add(assetLoaderOptions.ExternalDataMapper);
158 stream =
new FileStream(path,
FileMode.Open, FileAccess.Read, FileShare.Read);
160 var validExtensions = Readers.Extensions;
161 zipFile =
new ZipFile(stream);
162 Stream memoryStream =
null;
163 foreach (ZipEntry zipEntry
in zipFile)
165 if (!zipEntry.IsFile)
169 var checkingFileExtension = FileUtils.GetFileExtension(zipEntry.Name,
false);
170 if (fileExtension !=
null && checkingFileExtension == fileExtension)
172 memoryStream = ZipFileEntryToStream(out fileExtension, zipEntry, zipFile);
174 else if (validExtensions.Contains(checkingFileExtension))
176 memoryStream = ZipFileEntryToStream(out fileExtension, zipEntry, zipFile);
180 if (memoryStream ==
null)
182 var exception =
new Exception(
"Unable to find a suitable model on the Zip file. Please inform a valid model file extension.");
185 onError(
new ContextualizedError<string>(exception,
"Error"));
200 public static Stream ZipFileEntryToStream(out
string fileExtension, ZipEntry zipEntry, ZipFile zipFile)
202 var buffer =
new byte[ZipBufferSize];
203 var zipFileStream = zipFile.GetInputStream(zipEntry);
204 var memoryStream =
new MemoryStream(ZipBufferSize);
205 ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(zipFileStream, memoryStream, buffer);
206 memoryStream.Seek(0, SeekOrigin.Begin);
207 zipFileStream.Close();
208 fileExtension = FileUtils.GetFileExtension(zipEntry.Name,
false);
System.IO.FileMode FileMode
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Represents a mapper class used to load external data from Zip files.
Represents a mapper class class used to load Textures from Zip files.