Tanoda
AssetLoaderZip.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using ICSharpCode.SharpZipLib.Zip;
4using TriLibCore.General;
6using TriLibCore.Utils;
7using UnityEngine;
8
9namespace TriLibCore
10{
12 //todo: unify async loading.
13 public static class AssetLoaderZip
14 {
18 private const int ZipBufferSize = 4096;
19
24 private static void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
25 {
26 var zipLoadCustomContextData = assetLoaderContext.CustomData as ZipLoadCustomContextData;
27 if (zipLoadCustomContextData != null)
28 {
29 zipLoadCustomContextData.Stream.Close();
30 zipLoadCustomContextData.OnMaterialsLoad(assetLoaderContext);
31 }
32 }
33
38 private static void OnError(IContextualizedError contextualizedError)
39 {
40 var assetLoaderContext = contextualizedError?.GetContext() as AssetLoaderContext;
41 var zipLoadCustomContextData = assetLoaderContext?.CustomData as ZipLoadCustomContextData;
42 zipLoadCustomContextData?.Stream.Close();
43 zipLoadCustomContextData?.OnError?.Invoke(contextualizedError);
44 }
45
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)
58 {
59 Stream stream = 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
62 {
63 ZipFile = zipFile,
64 Stream = stream,
65 CustomData = customContextData,
66 OnError = onError,
67 OnMaterialsLoad = onMaterialsLoad
68 });
69 }
70
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)
83 {
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
86 {
87 ZipFile = zipFile,
88 Stream = stream,
89 CustomData = customContextData,
90 OnError = onError,
91 OnMaterialsLoad = onMaterialsLoad
92 });
93 }
94
103 public static AssetLoaderContext LoadModelFromZipFileNoThread(string path, Action<IContextualizedError> onError = null, GameObject wrapperGameObject = null, AssetLoaderOptions assetLoaderOptions = null, object customContextData = null, string fileExtension = null)
104 {
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
108 {
109 ZipFile = zipFile,
110 CustomData = customContextData,
111 OnError = onError
112 });
113 stream.Close();
114 return assetLoaderContext;
115 }
116
125 public static AssetLoaderContext LoadModelFromZipStreamNoThread(Stream stream, Action<IContextualizedError> onError, GameObject wrapperGameObject = null, AssetLoaderOptions assetLoaderOptions = null, object customContextData = null, string fileExtension = null)
126 {
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
129 {
130 ZipFile = zipFile,
131 CustomData = customContextData,
132 OnError = onError
133 });
134 stream.Close();
135 return assetLoaderContext;
136 }
137
146 private static Stream SetupZipModelLoading(Action<IContextualizedError> onError, ref Stream stream, string path, ref AssetLoaderOptions assetLoaderOptions, ref string fileExtension, out ZipFile zipFile)
147 {
148 if (assetLoaderOptions == null)
149 {
150 assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
151 }
152 assetLoaderOptions.TextureMapper = ScriptableObject.CreateInstance<ZipFileTextureMapper>();
153 assetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance<ZipFileExternalDataMapper>();
154 assetLoaderOptions.FixedAllocations.Add(assetLoaderOptions.TextureMapper);
155 assetLoaderOptions.FixedAllocations.Add(assetLoaderOptions.ExternalDataMapper);
156 if (stream == null)
157 {
158 stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
159 }
160 var validExtensions = Readers.Extensions;
161 zipFile = new ZipFile(stream);
162 Stream memoryStream = null;
163 foreach (ZipEntry zipEntry in zipFile)
164 {
165 if (!zipEntry.IsFile)
166 {
167 continue;
168 }
169 var checkingFileExtension = FileUtils.GetFileExtension(zipEntry.Name, false);
170 if (fileExtension != null && checkingFileExtension == fileExtension)
171 {
172 memoryStream = ZipFileEntryToStream(out fileExtension, zipEntry, zipFile);
173 }
174 else if (validExtensions.Contains(checkingFileExtension))
175 {
176 memoryStream = ZipFileEntryToStream(out fileExtension, zipEntry, zipFile);
177 break;
178 }
179 }
180 if (memoryStream == null)
181 {
182 var exception = new Exception("Unable to find a suitable model on the Zip file. Please inform a valid model file extension.");
183 if (onError != null)
184 {
185 onError(new ContextualizedError<string>(exception, "Error"));
186 }
187 else
188 {
189 throw exception;
190 }
191 }
192 return memoryStream;
193 }
194
200 public static Stream ZipFileEntryToStream(out string fileExtension, ZipEntry zipEntry, ZipFile zipFile)
201 {
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);
209 return memoryStream;
210 }
211 }
212}
System.IO.FileMode FileMode
Definition: AssetLoader.cs:14
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Definition: TriLibLoader.cs:9
Represents a mapper class used to load external data from Zip files.
Represents a mapper class class used to load Textures from Zip files.