finally! cache clearing!

This commit is contained in:
Yoshman29
2022-11-06 15:29:48 +01:00
parent 1cc101e2f0
commit 8b349e5ceb
5 changed files with 73 additions and 69 deletions
+1 -1
View File
@@ -1 +1 @@
1194
1199
+1 -1
View File
@@ -28,7 +28,7 @@ interface ModsAssetLibrary {
private function getAssetPath():String;
private function __isCacheValid(cache:Map<String, Dynamic>, asset:String):Bool;
private function __isCacheValid(cache:Map<String, Dynamic>, asset:String, isLocal:Bool = false):Bool;
private function __parseAsset(asset:String):Bool;
+45 -45
View File
@@ -80,7 +80,7 @@ class ModsFolder {
}
public static function unloadMod(mod:String) {
Assets.unloadLibrary('mods/$mod');
Assets.unloadLibrary('mods/$mod'.toLowerCase());
}
public static function prepareLibrary(libName:String, force:Bool = false) {
@@ -161,52 +161,52 @@ class ModsFolder {
}
private static function onStateSwitch(newState:FlxState) {
Assets.cache.clear();
lime.utils.Assets.cache.clear();
// Assets.cache.clear();
// lime.utils.Assets.cache.clear();
#if MOD_SUPPORT
if (currentModFolder == null) return;
var bmapsToRemove:Array<FlxGraphic> = [];
@:privateAccess
for(bmap in FlxG.bitmap._cache) {
if (bmap.assetsKey != null) {
var e = new LimeLibrarySymbol(bmap.assetsKey);
if (e.library is openfl.utils.AssetLibrary) {
@:privateAccess
e.library = cast(e.library, openfl.utils.AssetLibrary).__proxy;
}
if (e.library is ModsAssetLibrary) {
var lib = cast(e.library, ModsAssetLibrary);
if (!lib.__parseAsset(e.symbolName)) continue;
if (!lib.__isCacheValid(e.library.cachedImages, lib._parsedAsset)) {
e.library.cachedImages.remove(lib._parsedAsset);
bmapsToRemove.push(bmap);
}
}
}
}
// #if MOD_SUPPORT
// if (currentModFolder == null) return;
// var bmapsToRemove:Array<FlxGraphic> = [];
// @:privateAccess
// for(bmap in FlxG.bitmap._cache) {
// if (bmap.assetsKey != null) {
// var e = new LimeLibrarySymbol(bmap.assetsKey);
// if (e.library is openfl.utils.AssetLibrary) {
// @:privateAccess
// e.library = cast(e.library, openfl.utils.AssetLibrary).__proxy;
// }
// if (e.library is ModsAssetLibrary) {
// var lib = cast(e.library, ModsAssetLibrary);
// if (!lib.__parseAsset(e.symbolName)) continue;
// if (!lib.__isCacheValid(e.library.cachedImages, lib._parsedAsset)) {
// e.library.cachedImages.remove(lib._parsedAsset);
// bmapsToRemove.push(bmap);
// }
// }
// }
// }
// TODO: add setting for cache clearing
@:privateAccess
for(libName=>lib in lime.utils.Assets.libraries) {
var library = lib;
if (library is openfl.utils.AssetLibrary) {
var flLib = cast(library, openfl.utils.AssetLibrary);
@:privateAccess
if (flLib.__proxy != null) library = flLib.__proxy;
}
if (library is ModsAssetLibrary) {
var modLib = cast(library, ModsAssetLibrary);
@:privateAccess
library.cachedBytes = [];
@:privateAccess
for(sound in library.cachedAudioBuffers)
sound.dispose();
// // TODO: add setting for cache clearing
// @:privateAccess
// for(libName=>lib in lime.utils.Assets.libraries) {
// var library = lib;
// if (library is openfl.utils.AssetLibrary) {
// var flLib = cast(library, openfl.utils.AssetLibrary);
// @:privateAccess
// if (flLib.__proxy != null) library = flLib.__proxy;
// }
// if (library is ModsAssetLibrary) {
// var modLib = cast(library, ModsAssetLibrary);
// @:privateAccess
// for(sound in library.cachedAudioBuffers)
// sound.dispose();
// @:privateAccess
// library.cachedBytes = [];
}
}
for(e in bmapsToRemove)
FlxG.bitmap.remove(e);
#end
// }
// }
// for(e in bmapsToRemove)
// FlxG.bitmap.remove(e);
// #end
}
}
+16 -11
View File
@@ -2,6 +2,7 @@ package funkin.mods;
import lime.utils.Log;
import lime.utils.AssetLibrary;
import lime.utils.Assets as LimeAssets;
import lime.utils.AssetManifest;
import haxe.io.Path;
@@ -42,8 +43,11 @@ class ModsFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
}
public override function getAudioBuffer(id:String):AudioBuffer {
if (__isCacheValid(cachedAudioBuffers, id))
return cachedAudioBuffers.get(id);
trace(id);
if (__isCacheValid(LimeAssets.cache.audio, id)) {
trace("CACHE FOUND!!");
return LimeAssets.cache.audio.get('$libName:$id');
}
else {
if (!exists(id, "SOUND")) {
Log.error('ModsAssetLibrary: Audio Buffer at $id does not exist.');
@@ -52,13 +56,13 @@ class ModsFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
var path = getAssetPath();
editedTimes[id] = FileSystem.stat(path).mtime.getTime();
var e = AudioBuffer.fromFile(path);
cachedAudioBuffers.set(id, e);
// LimeAssets.cache.audio.set('$libName:$id', e);
return e;
}
}
public override function getBytes(id:String):Bytes {
if (__isCacheValid(cachedBytes, id))
if (__isCacheValid(cachedBytes, id, true))
return cachedBytes.get(id);
else {
if (!exists(id, "BINARY")) {
@@ -74,8 +78,8 @@ class ModsFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
}
public override function getFont(id:String):Font {
if (__isCacheValid(cachedFonts, id))
return cachedFonts.get(id);
if (__isCacheValid(LimeAssets.cache.font, id))
return LimeAssets.cache.font.get(id);
else {
if (!exists(id, "FONT")) {
Log.error('ModsAssetLibrary: Font at $id does not exist.');
@@ -84,14 +88,13 @@ class ModsFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
var path = getAssetPath();
editedTimes[id] = FileSystem.stat(path).mtime.getTime();
var e = Font.fromFile(path);
cachedFonts.set(id, e);
return e;
}
}
public override function getImage(id:String):Image {
if (useImageCache && __isCacheValid(cachedImages, id))
return cachedImages.get(id);
if (useImageCache && __isCacheValid(LimeAssets.cache.image, id))
return LimeAssets.cache.image.get('$libName:$id');
else {
if (!exists(id, "IMAGE")) {
Log.error('ModsAssetLibrary: Image at $id does not exist.');
@@ -101,7 +104,6 @@ class ModsFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
editedTimes[id] = FileSystem.stat(path).mtime.getTime();
var e = Image.fromFile(path);
if (useImageCache) cachedImages.set(id, e);
return e;
}
}
@@ -137,11 +139,14 @@ class ModsFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
return '$folderPath/$_parsedAsset';
}
private function __isCacheValid(cache:Map<String, Dynamic>, asset:String) {
private function __isCacheValid(cache:Map<String, Dynamic>, asset:String, isLocalCache:Bool = false) {
if (!editedTimes.exists(asset))
return false;
if (editedTimes[asset] == null) return false;
if (editedTimes[asset] < FileSystem.stat(getPath(asset)).mtime.getTime()) return false;
if (!isLocalCache) asset = '$libName:$asset';
return cache.exists(asset) && cache[asset] != null;
}
+10 -11
View File
@@ -14,6 +14,7 @@ import lime.net.HTTPRequest;
import lime.text.Font;
import lime.utils.AssetType;
import lime.utils.Bytes;
import lime.utils.Assets as LimeAssets;
#if MOD_SUPPORT
@@ -53,21 +54,20 @@ class ZipFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
public var _parsedAsset:String;
public override function getAudioBuffer(id:String):AudioBuffer {
if (__isCacheValid(cachedAudioBuffers, id))
return cachedAudioBuffers.get(id);
if (__isCacheValid(LimeAssets.cache.audio, id))
return LimeAssets.cache.audio.get('$libName:$id');
else {
if (!exists(id, "SOUND")) {
Log.error('ZipFolderLibrary: Audio Buffer at $id does not exist.');
return null;
}
var e = AudioBuffer.fromBytes(unzip(assets[_parsedAsset]));
cachedAudioBuffers.set(id, e);
return e;
}
}
public override function getBytes(id:String):Bytes {
if (__isCacheValid(cachedBytes, id))
if (__isCacheValid(cachedBytes, id, true))
return cachedBytes.get(id);
else {
if (!exists(id, "BINARY")) {
@@ -97,22 +97,21 @@ class ZipFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
}
public override function getFont(id:String):Font {
if (__isCacheValid(cachedFonts, id))
return cachedFonts.get(id);
if (__isCacheValid(LimeAssets.cache.font, id))
return LimeAssets.cache.font.get('$libName:$id');
else {
if (!exists(id, "FONT")) {
Log.error('ZipFolderLibrary: Font at $id does not exist.');
return null;
}
var e = Font.fromBytes(unzip(assets[_parsedAsset]));
cachedFonts.set(id, e);
return e;
}
}
public override function getImage(id:String):Image {
if (useImageCache && __isCacheValid(cachedImages, id))
return cachedImages.get(id);
if (useImageCache && __isCacheValid(LimeAssets.cache.image, id))
return LimeAssets.cache.image.get('$libName:$id');
else {
if (!exists(id, "IMAGE")) {
Log.error('ZipFolderLibrary: Image at $id does not exist.');
@@ -131,8 +130,8 @@ class ZipFolderLibrary extends AssetLibrary implements ModsAssetLibrary {
return true;
}
public function __isCacheValid(cache:Map<String, Dynamic>, asset:String) {
if (cache.exists(asset)) return true;
public function __isCacheValid(cache:Map<String, Dynamic>, asset:String, isLocal:Bool = false) {
if (cache.exists(isLocal ? '$libName:$asset': asset)) return true;
return false;
}