ZipFileTextureMapper.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using ICSharpCode.SharpZipLib.Zip;
  3. using StbImageSharp;
  4. using TriLibCore.Interfaces;
  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 TextureLoadingContext Map(AssetLoaderContext assetLoaderContext, ITexture texture)
  13. {
  14. var zipLoadCustomContextData = assetLoaderContext.CustomData as ZipLoadCustomContextData;
  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. var shortFileName = FileUtils.GetShortFilename(texture.Filename).ToLowerInvariant();
  25. foreach (ZipEntry zipEntry in zipFile)
  26. {
  27. if (!zipEntry.IsFile)
  28. {
  29. continue;
  30. }
  31. var checkingFileShortName = FileUtils.GetShortFilename(zipEntry.Name).ToLowerInvariant();
  32. if (shortFileName == checkingFileShortName)
  33. {
  34. string _;
  35. var textureLoadingContext = new TextureLoadingContext
  36. {
  37. Context = assetLoaderContext,
  38. Stream = AssetLoaderZip.ZipFileEntryToStream(out _, zipEntry, zipFile),
  39. Texture = texture
  40. };
  41. return textureLoadingContext;
  42. }
  43. }
  44. return null;
  45. }
  46. }
  47. }