Last active
January 21, 2016 07:58
-
-
Save triplefox/55428bbac42fda296b89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kha.Assets; | |
import kha.Image; | |
import kha.Sound; | |
import kha.Blob; | |
import kha.Video; | |
import kha.Font; | |
enum KhaAssetRequest { | |
KARBlob(k : String); | |
KARImage(k : String); | |
KARSound(k : String); | |
KARFont(k : String); | |
KARVideo(k : String); | |
} | |
enum KhaAsset { | |
KABlob(v : Blob); | |
KAImage(v : Image); | |
KASound(v : Sound); | |
KAFont(v : Font); | |
KAVideo(v : Video); | |
KAMissing(s : String); | |
} | |
class AssetCacheData { | |
public var blob : Map<String, Blob>; | |
public var image : Map<String, Image>; | |
public var sound : Map<String, Sound>; | |
public var font : Map<String, Font>; | |
public var video : Map<String, Video>; | |
public function new() { | |
blob = new Map(); | |
image = new Map(); | |
sound = new Map(); | |
font = new Map(); | |
video = new Map(); | |
} | |
} | |
class AssetCache { | |
public var static_cache : AssetCacheData; | |
public var dynamic_cache : AssetCacheData; | |
// the engine blocks execution while there are active load requests: | |
public var load_requests : Array<Array<KhaAsset>>; | |
public function new() { | |
load_requests = new Array(); | |
reset(); | |
} | |
public function get(ka : Array<KhaAssetRequest>, call : Array<KhaAsset>->Void) { | |
var result = new Array<KhaAsset>(); | |
load_requests.push(result); | |
// do we have a way to cancel load_requests? | |
var check = function() { | |
if (result.length == ka.length) { | |
call(result); | |
} | |
}; | |
for (n in ka) { | |
switch(n) { | |
case KARBlob(k): | |
if (dynamic_cache.blob.exists(k)) { | |
result.push(KABlob(dynamic_cache.blob.get(k))); | |
check(); | |
} else if (static_cache.blob.exists(k)) { | |
result.push(KABlob(static_cache.blob.get(k))); | |
check(); | |
} else { | |
var kk = StringTools.replace(k, ".", "_"); | |
var fload = (Reflect.field(Assets.blobs, kk + "Load")); | |
if (fload != null) { | |
fload(function() { | |
var fdata = (Reflect.field(Assets.blobs, kk)); | |
static_cache.blob.set(k, fdata); | |
result.push(KABlob(static_cache.blob.get(k))); | |
check(); | |
}); | |
} | |
else { | |
result.push(KAMissing(k)); | |
check(); | |
} | |
} | |
case KARImage(k): | |
if (dynamic_cache.image.exists(k)) { | |
result.push(KAImage(dynamic_cache.image.get(k))); | |
check(); | |
} else if (static_cache.image.exists(k)) { | |
result.push(KAImage(static_cache.image.get(k))); | |
check(); | |
} else { | |
var kk = StringTools.replace(k, ".", "_"); | |
var fload = (Reflect.field(Assets.images, kk + "Load")); | |
if (fload != null) { | |
fload(function() { | |
var fdata = (Reflect.field(Assets.images, kk)); | |
static_cache.image.set(k, fdata); | |
result.push(KAImage(static_cache.image.get(k))); | |
check(); | |
}); | |
} | |
else { | |
result.push(KAMissing(k)); | |
check(); | |
} | |
} | |
case KARSound(k): | |
if (dynamic_cache.sound.exists(k)) { | |
result.push(KASound(dynamic_cache.sound.get(k))); | |
check(); | |
} else if (static_cache.sound.exists(k)) { | |
result.push(KASound(static_cache.sound.get(k))); | |
check(); | |
} else { | |
var kk = StringTools.replace(k, ".", "_"); | |
var fload = (Reflect.field(Assets.sounds, kk + "Load")); | |
if (fload != null) { | |
fload(function() { | |
var fdata = (Reflect.field(Assets.sounds, kk)); | |
static_cache.font.set(k, fdata); | |
result.push(KASound(static_cache.sound.get(k))); | |
check(); | |
}); | |
} | |
else { | |
result.push(KAMissing(k)); | |
check(); | |
} | |
} | |
case KARFont(k): | |
if (dynamic_cache.font.exists(k)) { | |
result.push(KAFont(dynamic_cache.font.get(k))); | |
check(); | |
} else if (static_cache.font.exists(k)) { | |
result.push(KAFont(static_cache.font.get(k))); | |
check(); | |
} else { | |
var kk = StringTools.replace(k, ".", "_"); | |
var fload = (Reflect.field(Assets.fonts, kk + "Load")); | |
if (fload != null) { | |
fload(function() { | |
var fdata = (Reflect.field(Assets.fonts, kk)); | |
static_cache.font.set(k, fdata); | |
result.push(KAFont(static_cache.font.get(k))); | |
check(); | |
}); | |
} | |
else { | |
result.push(KAMissing(k)); | |
check(); | |
} | |
} | |
case KARVideo(k): | |
if (dynamic_cache.video.exists(k)) { | |
result.push(KAVideo(dynamic_cache.video.get(k))); | |
check(); | |
} else if (static_cache.video.exists(k)) { | |
result.push(KAVideo(static_cache.video.get(k))); | |
check(); | |
} else { | |
var kk = StringTools.replace(k, ".", "_"); | |
var fload = (Reflect.field(Assets.videos, kk + "Load")); | |
if (fload != null) { | |
fload(function() { | |
var fdata = (Reflect.field(Assets.videos, kk)); | |
static_cache.video.set(k, fdata); | |
result.push(KAVideo(static_cache.video.get(k))); | |
check(); | |
}); | |
} | |
else { | |
result.push(KAMissing(k)); | |
check(); | |
} | |
} | |
} | |
} | |
} | |
public function reset() { | |
static_cache = new AssetCacheData(); | |
dynamic_cache = new AssetCacheData(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment