forked from andyburke/UnityHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiskCache.cs
128 lines (116 loc) · 3.55 KB
/
DiskCache.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using HTTP;
namespace HTTP
{
public class DiskCacheOperation
{
public bool isDone = false;
public bool fromCache = false;
public Request request = null;
}
#if UNITY_WEBPLAYER
public class DiskCache : MonoBehaviour
{
static DiskCache _instance = null;
public static DiskCache Instance {
get {
if (_instance == null) {
var g = new GameObject ("DiskCache", typeof(DiskCache));
g.hideFlags = HideFlags.HideAndDontSave;
_instance = g.GetComponent<DiskCache> ();
}
return _instance;
}
}
public DiskCacheOperation Fetch (Request request)
{
var handle = new DiskCacheOperation ();
handle.request = request;
StartCoroutine (Download (request, handle));
return handle;
}
IEnumerator Download(Request request, DiskCacheOperation handle)
{
request.Send ();
while (!request.isDone)
yield return new WaitForEndOfFrame ();
handle.isDone = true;
}
}
#else
public class DiskCache : MonoBehaviour
{
string cachePath = null;
static DiskCache _instance = null;
public static DiskCache Instance {
get {
if (_instance == null) {
var g = new GameObject ("DiskCache", typeof(DiskCache));
g.hideFlags = HideFlags.HideAndDontSave;
_instance = g.GetComponent<DiskCache> ();
}
return _instance;
}
}
void Awake ()
{
cachePath = System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "uwcache");
if (!Directory.Exists (cachePath))
Directory.CreateDirectory (cachePath);
}
public DiskCacheOperation Fetch (Request request)
{
var guid = "";
// MD5 is disposable
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx#3
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create ()) {
foreach (var b in md5.ComputeHash (System.Text.ASCIIEncoding.ASCII.GetBytes (request.uri.ToString ()))) {
guid = guid + b.ToString ("X2");
}
}
var filename = System.IO.Path.Combine (cachePath, guid);
if (File.Exists (filename) && File.Exists (filename + ".etag"))
request.SetHeader ("If-None-Match", File.ReadAllText (filename + ".etag"));
var handle = new DiskCacheOperation ();
handle.request = request;
StartCoroutine (DownloadAndSave (request, filename, handle));
return handle;
}
IEnumerator DownloadAndSave (Request request, string filename, DiskCacheOperation handle)
{
var useCachedVersion = File.Exists(filename);
Action< HTTP.Request > callback = request.completedCallback;
request.Send(); // will clear the completedCallback
while (!request.isDone)
yield return new WaitForEndOfFrame ();
if (request.exception == null && request.response != null) {
if (request.response.status == 200) {
var etag = request.response.GetHeader ("etag");
if (etag != "") {
File.WriteAllBytes (filename, request.response.bytes);
File.WriteAllText (filename + ".etag", etag);
}
useCachedVersion = false;
}
}
if(useCachedVersion) {
if(request.exception != null) {
Debug.LogWarning("Using cached version due to exception:" + request.exception);
request.exception = null;
}
request.response.status = 304;
request.response.bytes = File.ReadAllBytes (filename);
request.isDone = true;
}
handle.isDone = true;
if ( callback != null )
{
callback( request );
}
}
}
#endif
}