multiple xr toolkit package
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VRCCachedWWW.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using UnityEngine;
  5. public static class VRCCachedWWW {
  6. public const float DefaultCacheTimeHours = 24 * 7;
  7. public static void ClearOld(float cacheLimitHours = DefaultCacheTimeHours)
  8. {
  9. string cacheDir = CacheDir;
  10. if (System.IO.Directory.Exists(cacheDir))
  11. {
  12. foreach (string fileName in System.IO.Directory.GetFiles(cacheDir))
  13. {
  14. if (GetAge(fileName) > cacheLimitHours)
  15. System.IO.File.Delete(fileName);
  16. }
  17. }
  18. }
  19. private static string CacheDir
  20. {
  21. get
  22. {
  23. return Application.temporaryCachePath;
  24. }
  25. }
  26. public static IEnumerator Get(string url, System.Action<WWW> onDone, float cacheLimitHours = DefaultCacheTimeHours)
  27. {
  28. string cacheDir = CacheDir;
  29. if (!System.IO.Directory.Exists(cacheDir))
  30. System.IO.Directory.CreateDirectory(cacheDir);
  31. string hash = CreateHash(url);
  32. string cache = cacheDir + "/www_" + hash;
  33. string location = url;
  34. bool useCache = false;
  35. if (System.IO.File.Exists(cache))
  36. {
  37. if (GetAge(cache) > cacheLimitHours)
  38. System.IO.File.Delete(cache);
  39. else
  40. {
  41. location = "file://" + cache;
  42. useCache = true;
  43. }
  44. }
  45. while (true)
  46. {
  47. WWW target = new WWW(location);
  48. target.threadPriority = ThreadPriority.Low;
  49. while (!target.isDone)
  50. yield return null;
  51. if (!useCache)
  52. {
  53. if (System.IO.File.Exists(cache))
  54. System.IO.File.Delete(cache);
  55. if (string.IsNullOrEmpty(target.error))
  56. System.IO.File.WriteAllBytes(cache, target.bytes);
  57. onDone(target);
  58. break;
  59. }
  60. else
  61. {
  62. if (string.IsNullOrEmpty(target.error))
  63. {
  64. onDone(target);
  65. break;
  66. }
  67. else
  68. {
  69. if (System.IO.File.Exists(cache))
  70. System.IO.File.Delete(cache);
  71. location = url;
  72. useCache = false;
  73. }
  74. }
  75. }
  76. }
  77. private static string CreateHash(string _string)
  78. {
  79. SHA256 hash = SHA256.Create();
  80. byte[] computed_hash = hash.ComputeHash(System.Text.Encoding.Default.GetBytes(_string));
  81. return System.Uri.EscapeDataString(System.Convert.ToBase64String(computed_hash));
  82. }
  83. private static double GetAge(string file)
  84. {
  85. if (!System.IO.File.Exists(file))
  86. return 0;
  87. System.DateTime writeTime = System.IO.File.GetLastWriteTimeUtc(file);
  88. return System.DateTime.UtcNow.Subtract(writeTime).TotalHours;
  89. }
  90. }