DiskCacheCommon.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Ryujinx.Common.Logging;
  2. using System.IO;
  3. namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
  4. {
  5. /// <summary>
  6. /// Common disk cache utility methods.
  7. /// </summary>
  8. static class DiskCacheCommon
  9. {
  10. /// <summary>
  11. /// Opens a file for read or write.
  12. /// </summary>
  13. /// <param name="basePath">Base path of the file (should not include the file name)</param>
  14. /// <param name="fileName">Name of the file</param>
  15. /// <param name="writable">Indicates if the file will be read or written</param>
  16. /// <returns>File stream</returns>
  17. public static FileStream OpenFile(string basePath, string fileName, bool writable)
  18. {
  19. string fullPath = Path.Combine(basePath, fileName);
  20. FileMode mode;
  21. FileAccess access;
  22. if (writable)
  23. {
  24. mode = FileMode.OpenOrCreate;
  25. access = FileAccess.ReadWrite;
  26. }
  27. else
  28. {
  29. mode = FileMode.Open;
  30. access = FileAccess.Read;
  31. }
  32. try
  33. {
  34. return new FileStream(fullPath, mode, access, FileShare.Read);
  35. }
  36. catch (IOException ioException)
  37. {
  38. Logger.Error?.Print(LogClass.Gpu, $"Could not access file \"{fullPath}\". {ioException.Message}");
  39. throw new DiskCacheLoadException(DiskCacheLoadResult.NoAccess);
  40. }
  41. }
  42. /// <summary>
  43. /// Gets the compression algorithm that should be used when writing the disk cache.
  44. /// </summary>
  45. /// <returns>Compression algorithm</returns>
  46. public static CompressionAlgorithm GetCompressionAlgorithm()
  47. {
  48. return CompressionAlgorithm.Deflate;
  49. }
  50. }
  51. }