summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsLegacy/Assets/VRCSDK/Dependencies/VRChat/Editor/VRCCachedWebRequest.cs
blob: 2dab5f975fa380eb950dab6645e23c2fd82ed986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

public static class VRCCachedWebRequest
{
    private const float DefaultCacheTimeHours = 24 * 7;

    public static void ClearOld(float cacheLimitHours = DefaultCacheTimeHours)
    {
        string cacheDir = CacheDir;
        if(!Directory.Exists(cacheDir))
        {
            return;
        }

        foreach(string fileName in Directory.GetFiles(cacheDir))
        {
            if(!(GetAge(fileName) > cacheLimitHours))
            {
                continue;
            }

            Debug.Log($"Deleting {fileName}");
            File.Delete(fileName);
        }
    }

    private static string CacheDir => Application.temporaryCachePath;

    public static IEnumerator Get(string url, Action<Texture2D> onDone, float cacheLimitHours = DefaultCacheTimeHours)
    {
        string cacheDir = CacheDir;
        if(!Directory.Exists(cacheDir))
        {
            Directory.CreateDirectory(cacheDir);
        }

        string hash = CreateHash(url);
        string cache = cacheDir + "/www_" + hash;

        if(File.Exists(cache))
        {
            // Use cached file if it exists
            if(GetAge(cache) > cacheLimitHours)
            {
                File.Delete(cache);
            }
            else
            {
                Texture2D texture = new Texture2D(2, 2);
                if(!texture.LoadImage(File.ReadAllBytes(cache)))
                {
                    yield break;
                }

                // load texture from disk and exit if we successfully read it
                texture.Apply();
                onDone(texture);
            }
        }

        else
        {
            // No cached file, load it from url
            using(UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url))
            {
                // Wait until request and download are complete
                yield return uwr.SendWebRequest();
                while(!uwr.isDone || !uwr.downloadHandler.isDone)
                {
                    yield return null;
                }

                var texture = DownloadHandlerTexture.GetContent(uwr);

                if(string.IsNullOrEmpty(uwr.error))
                {
                    File.WriteAllBytes(cache, uwr.downloadHandler.data);
                }

                onDone(texture);
            }
        }
    }

    private static string CreateHash(string input)
    {
        SHA256 hash = SHA256.Create();
        byte[] computedHash = hash.ComputeHash(Encoding.Default.GetBytes(input));
        return Uri.EscapeDataString(Convert.ToBase64String(computedHash));
    }

    private static double GetAge(string file)
    {
        if(!File.Exists(file))
        {
            return 0;
        }

        DateTime writeTime = File.GetLastWriteTimeUtc(file);
        return DateTime.UtcNow.Subtract(writeTime).TotalHours;
    }
}