Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FileReference.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.IO;
  3. using Uniject;
  4. namespace UnityEngine.Purchasing
  5. {
  6. /// <summary>
  7. /// File Reference that can be created with a filename.
  8. /// The path to the file is constructed via `Application.persistentDataPath` and `Application.cloudProjectId`.
  9. /// Operations such as Save, Load, and Delete are available.
  10. /// One use case for this class is to create a file reference to a locally cached store catalog.
  11. /// </summary>
  12. ///
  13. internal class FileReference
  14. {
  15. private readonly string m_FilePath;
  16. private readonly ILogger m_Logger;
  17. /// <summary>
  18. /// Creates the instance of FileReference. Method allows dependency injection to ease testing
  19. /// by using Interfaces for the logger and util.
  20. /// </summary>
  21. /// <returns>The instance.</returns>
  22. /// <param name="filename">Filename.</param>
  23. /// <param name="logger">Logger.</param>
  24. /// <param name="util">Util.</param>
  25. internal static FileReference CreateInstance(string filename, ILogger logger, IUtil util)
  26. {
  27. try
  28. {
  29. var persistentDataPath = Path.Combine(util.persistentDataPath, "Unity");
  30. var uniquePathSuffix = Path.Combine(util.cloudProjectId, "IAP");
  31. var cachePath = Path.Combine(persistentDataPath, uniquePathSuffix);
  32. Directory.CreateDirectory(cachePath);
  33. var filePath = Path.Combine(cachePath, filename);
  34. return new FileReference(filePath, logger);
  35. }
  36. catch
  37. {
  38. // Not all platforms support writing to disk. E.g. tvOS throws exception: "System.UnauthorizedAccessException: Access to the path "/Unity" is denied."
  39. return null;
  40. }
  41. }
  42. /// <summary>
  43. /// Creates an instance of the Persist class
  44. /// Please use use the `CreateInstance` method unless the filepath
  45. /// cannot be created through UnityEngine.Application
  46. /// </summary>
  47. /// <param name="filePath">File path.</param>
  48. /// <param name="logger">Logger.</param>
  49. internal FileReference(string filePath, ILogger logger)
  50. {
  51. m_FilePath = filePath;
  52. m_Logger = logger;
  53. }
  54. /// <summary>
  55. /// Save the specified payload on file.
  56. /// </summary>
  57. /// <param name="payload">Payload.</param>
  58. internal void Save(string payload)
  59. {
  60. try
  61. {
  62. File.WriteAllText(m_FilePath, payload);
  63. }
  64. catch (Exception e)
  65. {
  66. m_Logger.LogError("Failed persisting content", e);
  67. }
  68. }
  69. /// <summary>
  70. /// Load the contents from the file as a string.
  71. /// </summary>
  72. /// <returns>String from file</returns>
  73. internal string Load()
  74. {
  75. try
  76. {
  77. return File.ReadAllText(m_FilePath);
  78. }
  79. catch
  80. {
  81. return null;
  82. }
  83. }
  84. /// <summary>
  85. /// Deletes the file
  86. /// </summary>
  87. internal void Delete()
  88. {
  89. try
  90. {
  91. File.Delete(m_FilePath);
  92. }
  93. catch (Exception e)
  94. {
  95. m_Logger.LogWarning("Failed deleting cached content", e);
  96. }
  97. }
  98. }
  99. }