-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using UnityEngine; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Kongregate { | ||
public class CacheEntryQuery : CustomYieldInstruction { | ||
private readonly string Url; | ||
private int Status = 0; | ||
|
||
public CacheEntryQuery(string url) { | ||
Url = url; | ||
CachedXHRExtensions_SearchCache(url); | ||
} | ||
|
||
public override bool keepWaiting { | ||
get { | ||
Status = CachedXHRExtensions_CheckStatus(Url); | ||
return Status == 0; | ||
} | ||
} | ||
|
||
public bool IsCached { | ||
get { | ||
if (Status == 0) { | ||
Debug.Log("CacheEntryQuery: returning IsCached=false since query is pending"); | ||
return false; | ||
} | ||
|
||
return Status == 1; | ||
} | ||
} | ||
|
||
#if UNITY_WEBGL && !UNITY_EDITOR | ||
[DllImport("__Internal")] | ||
private static extern void CachedXHRExtensions_SearchCache(string url); | ||
|
||
[DllImport("__Internal")] | ||
private static extern int CachedXHRExtensions_CheckStatus(string url); | ||
#else | ||
private static void CachedXHRExtensions_SearchCache(string url) { } | ||
private static int CachedXHRExtensions_CheckStatus(string url) { return -1; } | ||
#endif | ||
} | ||
|
||
public class CachedXHRExtensions { | ||
public static void CleanCache() { | ||
CachedXHRExtensions_CleanCache(); | ||
} | ||
|
||
#if UNITY_WEBGL && !UNITY_EDITOR | ||
[DllImport("__Internal")] | ||
static extern int CachedXHRExtensions_CleanCache(); | ||
#else | ||
static void CachedXHRExtensions_CleanCache() {} | ||
#endif | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mergeInto(LibraryManager.library, { | ||
CachedXHRExtensions_SearchCache: function(url) { CachedXHRExtensions.searchCache(url); }, | ||
CachedXHRExtensions_CheckStatus: function(url) { return CachedXHRExtensions.checkStatus(url); }, | ||
CachedXHRExtensions_CleanCache: function() { | ||
try { | ||
var self = CachedXMLHttpRequest.cache; | ||
self.db.transaction([self.store], "readwrite").objectStore(self.store).clear().onerror = function(){ | ||
e.preventDefault(); | ||
}; | ||
} catch(e) {} | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
setTimeout(function(){ | ||
var enabled = typeof CachedXMLHttpRequest !== "undefined" && !!CachedXMLHttpRequest.cache; | ||
console.log("CachedXHRExtensions initialized, enabled=" + enabled); | ||
var CachedXHRExtensions = function() { | ||
this._cacheStates = {}; | ||
}; | ||
|
||
CachedXHRExtensions.prototype.searchCache = function(url) { | ||
if (!enabled) return; | ||
var self = this; | ||
url = typeof url === "string" ? url : Pointer_stringify(url); | ||
delete this._cacheStates[url]; | ||
|
||
CachedXMLHttpRequest.cache.get(url, function(err, result) { | ||
if (err || !result || !result.meta) { | ||
self._cacheStates[url] = false; | ||
} else { | ||
self._cacheStates[url] = true; | ||
} | ||
}); | ||
}; | ||
|
||
CachedXHRExtensions.prototype.checkStatus = function(url) { | ||
if (!enabled) return -1; | ||
|
||
url = typeof url === "string" ? url : Pointer_stringify(url); | ||
if (this._cacheStates[url] === undefined) return 0; | ||
return this._cacheStates[url] ? 1 : -1; | ||
}; | ||
|
||
window.CachedXHRExtensions = new CachedXHRExtensions(); | ||
}, 0); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
This plugin provides additional functionality on top of the CachedXMLHttpRequest | ||
addon. The classes referred to below are in the Kongregate namespace. | ||
|
||
* Clearing the cache by calling CachedXHRExtensions.CleanCache() | ||
* Asynchronously querying the cache to determine if an item exists: | ||
IEnumerator CheckIfAssetExists() { | ||
var query = new CacheEntryQuery("https://whatever.io/file.xml"); | ||
yield return query; | ||
if (query.IsCached) { | ||
Debug.Log("Asset exists in cache!"); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.