PerFilenameTextureMapper.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System.IO;
  2. using TriLibCore.General;
  3. using TriLibCore.Utils;
  4. namespace TriLibCore.Mappers
  5. {
  6. public class PerFilenameTextureMapper : TextureMapper
  7. {
  8. public override void Map(TextureLoadingContext textureLoadingContext)
  9. {
  10. if (textureLoadingContext.Context.Filename == null)
  11. {
  12. return;
  13. }
  14. var directory = FileUtils.GetFileDirectory(textureLoadingContext.Context.Filename);
  15. if (Directory.Exists(directory))
  16. {
  17. var modelShortFilename = FileUtils.GetShortFilename(textureLoadingContext.Context.Filename).ToLowerInvariant();
  18. var files = Directory.GetFiles(directory);
  19. foreach (var file in files)
  20. {
  21. if (!TextureUtils.IsValidTextureFileType(file))
  22. {
  23. continue;
  24. }
  25. var shortFilename = FileUtils.GetShortFilename(file).ToLowerInvariant();
  26. if (modelShortFilename == shortFilename)
  27. {
  28. textureLoadingContext.Stream = File.OpenRead(file);
  29. return;
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }