ZipFileTextureMapper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma warning disable 672
  2. using System;
  3. using ICSharpCode.SharpZipLib.Zip;
  4. using TriLibCore.General;
  5. using TriLibCore.Utils;
  6. namespace TriLibCore.Mappers
  7. {
  8. /// <summary>Represents a mapper class class used to load Textures from Zip files.</summary>
  9. public class ZipFileTextureMapper : TextureMapper
  10. {
  11. /// <inheritdoc />
  12. public override void Map(TextureLoadingContext textureLoadingContext)
  13. {
  14. var zipLoadCustomContextData = CustomDataHelper.GetCustomData<ZipLoadCustomContextData>(textureLoadingContext.Context.CustomData);
  15. if (zipLoadCustomContextData == null)
  16. {
  17. throw new Exception("Missing custom context data.");
  18. }
  19. var zipFile = zipLoadCustomContextData.ZipFile;
  20. if (zipFile == null)
  21. {
  22. throw new Exception("Zip file instance is null.");
  23. }
  24. if (string.IsNullOrWhiteSpace(textureLoadingContext.Texture.Filename))
  25. {
  26. if (textureLoadingContext.Context.Options.ShowLoadingWarnings)
  27. {
  28. UnityEngine.Debug.LogWarning("Texture name is null.");
  29. }
  30. return;
  31. }
  32. var modelFilenameWithoutExtension = FileUtils.GetFilenameWithoutExtension(zipLoadCustomContextData.ZipEntry.Name).ToLowerInvariant();
  33. var textureShortName = FileUtils.GetShortFilename(textureLoadingContext.Texture.Filename).ToLowerInvariant();
  34. foreach (ZipEntry zipEntry in zipFile)
  35. {
  36. if (!zipEntry.IsFile)
  37. {
  38. continue;
  39. }
  40. var checkingFileShortName = FileUtils.GetShortFilename(zipEntry.Name).ToLowerInvariant();
  41. var checkingFilenameWithoutExtension = FileUtils.GetFilenameWithoutExtension(zipEntry.Name).ToLowerInvariant();
  42. if (
  43. TextureUtils.IsValidTextureFileType(checkingFileShortName) &&
  44. textureLoadingContext.TextureType == TextureType.Diffuse && modelFilenameWithoutExtension == checkingFilenameWithoutExtension
  45. || textureShortName == checkingFileShortName)
  46. {
  47. textureLoadingContext.Stream = AssetLoaderZip.ZipFileEntryToStream(out _, zipEntry, zipFile);
  48. }
  49. }
  50. }
  51. }
  52. }