From 518362b1e0b147e9efed095d4eb1c08521ec8ca6 Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Sat, 25 Mar 2023 15:44:05 +0100 Subject: [PATCH] faster, way faster loading times --- source/funkin/system/FunkinCache.hx | 163 ++++++++++++++++++++++++++++ source/funkin/system/Main.hx | 7 +- 2 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 source/funkin/system/FunkinCache.hx diff --git a/source/funkin/system/FunkinCache.hx b/source/funkin/system/FunkinCache.hx new file mode 100644 index 00000000..1265acd3 --- /dev/null +++ b/source/funkin/system/FunkinCache.hx @@ -0,0 +1,163 @@ +package funkin.system; + +import openfl.utils.AssetCache; +import openfl.display.BitmapData; +import openfl.media.Sound; +import openfl.text.Font; +#if lime +import lime.utils.Assets as LimeAssets; +#end + + +class FunkinCache extends AssetCache { + public static var instance:FunkinCache; + /** + Internal + **/ + @:noCompletion @:dox(hide) public var bitmapData2:Map; + + /** + Internal + **/ + @:noCompletion @:dox(hide) public var font2:Map; + + /** + Internal + **/ + @:noCompletion @:dox(hide) public var sound2:Map; + + public function new() { + super(); + moveToSecondLayer(); + instance = this; + } + + public static function init() { + openfl.utils.Assets.cache = new FunkinCache(); + + FlxG.signals.preStateSwitch.add(function() { + instance.moveToSecondLayer(); + }); + + FlxG.signals.postStateSwitch.add(function() { + instance.clearSecondLayer(); + }); + } + + public function moveToSecondLayer() { + bitmapData2 = bitmapData; + font2 = font; + sound2 = sound; + bitmapData = []; + font = []; + sound = []; + } + + public function clearSecondLayer() { + for(k=>b in bitmapData2) { + LimeAssets.cache.image.remove(k); + } + for(k=>f in font2) { + LimeAssets.cache.font.remove(k); + } + for(k=>s in sound2) { + LimeAssets.cache.audio.remove(k); + } + + bitmapData2 = []; + font2 = []; + sound2 = []; + } + + + + /** + Retrieves a cached BitmapData. + + @param id The ID of the cached BitmapData + @return The cached BitmapData instance + **/ + public override function getBitmapData(id:String):BitmapData + { + var s = bitmapData.get(id); + if (s != null) + return s; + var s2 = bitmapData2.get(id); + if (s2 != null) { + bitmapData2.remove(id); + bitmapData.set(id, s2); + } + return s2; + } + + /** + Retrieves a cached Font. + + @param id The ID of the cached Font + @return The cached Font instance + **/ + public override function getFont(id:String):Font + { + var s = font.get(id); + if (s != null) + return s; + var s2 = font2.get(id); + if (s2 != null) { + font2.remove(id); + font.set(id, s2); + } + return s2; + } + + /** + Retrieves a cached Sound. + + @param id The ID of the cached Sound + @return The cached Sound instance + **/ + public override function getSound(id:String):Sound + { + var s = sound.get(id); + if (s != null) + return s; + var s2 = sound2.get(id); + if (s2 != null) { + sound2.remove(id); + sound.set(id, s2); + } + return s2; + } + + /** + Checks whether a BitmapData asset is cached. + + @param id The ID of a BitmapData asset + @return Whether the object has been cached + **/ + public override function hasBitmapData(id:String):Bool + { + return bitmapData.exists(id) || bitmapData2.exists(id); + } + + /** + Checks whether a Font asset is cached. + + @param id The ID of a Font asset + @return Whether the object has been cached + **/ + public override function hasFont(id:String):Bool + { + return font.exists(id) || font2.exists(id); + } + + /** + Checks whether a Sound asset is cached. + + @param id The ID of a Sound asset + @return Whether the object has been cached + **/ + public override function hasSound(id:String):Bool + { + return sound.exists(id) || sound2.exists(id); + } +} \ No newline at end of file diff --git a/source/funkin/system/Main.hx b/source/funkin/system/Main.hx index 88f1a39d..e42c26ca 100644 --- a/source/funkin/system/Main.hx +++ b/source/funkin/system/Main.hx @@ -150,6 +150,7 @@ class Main extends Sprite for(i in 0...4) gameThreads.push(Thread.createWithEventLoop(function() {Thread.current().events.promise();})); #end + FunkinCache.init(); Paths.assetsTree = new AssetsLibraryList(); #if UPDATE_CHECKING @@ -253,12 +254,6 @@ class Main extends Sprite openfl.display3D.utils.UInt8Buff._pools.clear(); } - var cache = cast(Assets.cache, AssetCache); - for (key=>_ in cache.font) - cache.removeFont(key); - for (key=>_ in cache.sound) - cache.removeSound(key); - Paths.assetsTree.clearCache(); MemoryUtil.clearMajor();