ZipFileExternalDataMapper.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using ICSharpCode.SharpZipLib.Zip;
  5. using TriLibCore.Utils;
  6. namespace TriLibCore.Mappers
  7. {
  8. /// <summary>Represents a mapper class used to load external data from Zip files.</summary>
  9. public class ZipFileExternalDataMapper : ExternalDataMapper
  10. {
  11. /// <inheritdoc />
  12. public override Stream Map(AssetLoaderContext assetLoaderContext, string originalFilename, out string finalPath)
  13. {
  14. var zipLoadCustomContextData = CustomDataHelper.GetCustomData<ZipLoadCustomContextData>(assetLoaderContext.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. var shortFileName = FileUtils.GetShortFilename(originalFilename).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. finalPath = zipFile.Name;
  35. string _;
  36. return AssetLoaderZip.ZipFileEntryToStream(out _, zipEntry, zipFile);
  37. }
  38. }
  39. finalPath = null;
  40. return null;
  41. }
  42. }
  43. }