ZipFileExternalDataMapper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.IO;
  3. using ICSharpCode.SharpZipLib.Zip;
  4. using TriLibCore.Utils;
  5. namespace TriLibCore.Mappers
  6. {
  7. /// <summary>Represents a mapper class used to load external data from Zip files.</summary>
  8. public class ZipFileExternalDataMapper : ExternalDataMapper
  9. {
  10. /// <inheritdoc />
  11. public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
  12. {
  13. if (!(assetLoaderContext.CustomData is ZipLoadCustomContextData zipLoadCustomContextData))
  14. {
  15. throw new Exception("Missing custom context data.");
  16. }
  17. var zipFile = zipLoadCustomContextData.ZipFile;
  18. if (zipFile == null)
  19. {
  20. throw new Exception("Zip file instance is null.");
  21. }
  22. var shortFileName = FileUtils.GetShortFilename(originalFilename).ToLowerInvariant();
  23. foreach (ZipEntry zipEntry in zipFile)
  24. {
  25. if (!zipEntry.IsFile)
  26. {
  27. continue;
  28. }
  29. var checkingFileShortName = FileUtils.GetShortFilename(zipEntry.Name).ToLowerInvariant();
  30. if (shortFileName == checkingFileShortName)
  31. {
  32. finalPath = zipFile.Name;
  33. string _;
  34. return AssetLoaderZip.ZipFileEntryToStream(out _, zipEntry, zipFile);
  35. }
  36. }
  37. finalPath = null;
  38. return null;
  39. }
  40. }
  41. }