diff --git a/source/FreeplayState.hx b/source/FreeplayState.hx index e907f97..04c6e87 100644 --- a/source/FreeplayState.hx +++ b/source/FreeplayState.hx @@ -184,9 +184,9 @@ class FreeplayState extends MusicBeatState override function create() { #if not web - Paths.clearUnusedMemory(); - Paths.clearStoredMemory(); #end + // Paths.clearStoredMemory(); + // Paths.clearUnusedMemory(); #if desktop DiscordClient.changePresence("In the Freeplay Menu", null); #end diff --git a/source/Paths.hx b/source/Paths.hx index b3bf8ff..f265f9b 100644 --- a/source/Paths.hx +++ b/source/Paths.hx @@ -6,6 +6,13 @@ import openfl.utils.AssetType; import openfl.utils.Assets as OpenFlAssets; #if sys import sys.FileSystem; #end import flash.media.Sound; +import flixel.graphics.FlxGraphic; +import openfl.system.System; +import lime.utils.Assets; +import openfl.geom.Rectangle; +import openfl.display3D.textures.RectangleTexture; +import openfl.display.BitmapData; +import openfl.system.System; using StringTools; @@ -88,13 +95,81 @@ class Paths } } - inline static public function formatToSongPath(path:String) { + public static function excludeAsset(key:String) { + if (!dumpExclusions.contains(key)) + dumpExclusions.push(key); + } + + public static var dumpExclusions:Array = + [ + 'assets/shared/music/freakyMenu.$SOUND_EXT', + 'assets/shared/music/breakfast.$SOUND_EXT', + // 'assets/shared/music/tea-time.$SOUND_EXT', + ]; + + /// haya I love you for the base cache dump I took to the max + public static function clearUnusedMemory() { + // clear non local assets in the tracked assets list + for (key in currentTrackedAssets.keys()) { + // if it is not currently contained within the used local assets + if (!localTrackedAssets.contains(key) && !dumpExclusions.contains(key)) { + var obj = currentTrackedAssets.get(key); + @:privateAccess + if (obj != null) { + // remove the key from all cache maps + FlxG.bitmap._cache.remove(key); + openfl.Assets.cache.removeBitmapData(key); + currentTrackedAssets.remove(key); + + // and get rid of the object + obj.persist = false; // make sure the garbage collector actually clears it up + obj.destroyOnNoUse = true; + obj.destroy(); + } + } + } + + // run the garbage collector for good measure lmfao + System.gc(); + } + + public static var currentTrackedAssets:Map = []; + public static var localTrackedAssets:Array = []; + public static function clearStoredMemory(?cleanUnused:Bool = false) { + // clear anything not in the tracked assets list + @:privateAccess + for (key in FlxG.bitmap._cache.keys()) + { + var obj = FlxG.bitmap._cache.get(key); + if (obj != null && !currentTrackedAssets.exists(key)) { + openfl.Assets.cache.removeBitmapData(key); + FlxG.bitmap._cache.remove(key); + obj.destroy(); + } + } + + // clear all sounds that are cached + for (key in currentTrackedSounds.keys()) { + if (!localTrackedAssets.contains(key) + && !dumpExclusions.contains(key) && key != null) { + //trace('test: ' + dumpExclusions, key); + Assets.cache.clear(key); + currentTrackedSounds.remove(key); + } + } + // flags everything to be cleared out next unused memory clear + localTrackedAssets = []; + #if !html5 openfl.Assets.cache.clear("songs"); #end + } + + + /*inline static public function formatToSongPath(path:String) { //var invalidChars = ~/[~&\\;:<>#]/; // var hideChars = ~/[.,'"%?!]/; var path = path; return path; - } + } */ inline static public function txt(key:String, ?library:String) { @@ -168,7 +243,7 @@ class Paths inline static public function voices(song:String, addon:String = "") { - var songKey:String = '${formatToSongPath(song)}/Voices${(addon)}'; + var songKey:String = '${(song)}/Voices${(addon)}'; var voices = returnSound(null, songKey, 'songs'); return voices; //return 'songs:assets/songs/${song.toLowerCase()}/Voices${addon}.$SOUND_EXT'; @@ -182,7 +257,7 @@ class Paths inline static public function inst(song:String) { - var songKey:String = '${formatToSongPath(song)}/Inst'; + var songKey:String = '${(song)}/Inst'; var inst = returnSound(null, songKey, 'songs'); return inst; //return 'songs:assets/songs/${song.toLowerCase()}/Inst.$SOUND_EXT'; @@ -219,7 +294,7 @@ class Paths } } // Thanks psych engine for the sound code - public static var localTrackedAssets:Array = []; + public static var currentTrackedSounds:Map = []; public static function returnSound(path:String, key:String, ?library:String) { // #if MODS_ALLOWED diff --git a/source/PlaySelectionState.hx b/source/PlaySelectionState.hx index f519044..20e541d 100644 --- a/source/PlaySelectionState.hx +++ b/source/PlaySelectionState.hx @@ -40,6 +40,8 @@ class PlaySelectionState extends MusicBeatState override function create() { + //Paths.clearStoredMemory(); + #if desktop // Updating Discord Rich Presence DiscordClient.changePresence("In the Menus", null); @@ -150,6 +152,7 @@ class PlaySelectionState extends MusicBeatState { selectable = true; }); + // Paths.clearUnusedMemory(); } var selectedSomethin:Bool = false; diff --git a/source/PlayState.hx b/source/PlayState.hx index e000d1f..84870c3 100644 --- a/source/PlayState.hx +++ b/source/PlayState.hx @@ -526,6 +526,8 @@ class PlayState extends MusicBeatState override public function create() { + Paths.clearStoredMemory(); + var compactCombo:String = formatCompactNumber(combo); var compactMaxCombo:String = formatCompactNumber(maxCombo); var compactScore:String = formatCompactNumber(songScore); @@ -1961,6 +1963,7 @@ class PlayState extends MusicBeatState hidehphud(); super.create(); + Paths.clearUnusedMemory(); perlinCamera = new Perlin(Math.floor(Math.random() * 65535)); diff --git a/source/StoryMenuState.hx b/source/StoryMenuState.hx index fb12f08..ec613a9 100644 --- a/source/StoryMenuState.hx +++ b/source/StoryMenuState.hx @@ -28,6 +28,7 @@ using StringTools; class StoryMenuState extends MusicBeatState { + var scoreText:FlxText; public static var weekUnlocked:Array = [true, true, true, true, true, true]; @@ -61,6 +62,9 @@ class StoryMenuState extends MusicBeatState override function create() { + Paths.clearStoredMemory(); + Paths.clearUnusedMemory(); + awaitingExploitation = (FlxG.save.data.exploitationState == 'awaiting'); if (FlxG.save.data.masterWeekUnlocked) diff --git a/source/TitleState.hx b/source/TitleState.hx index a3fa057..99639a9 100644 --- a/source/TitleState.hx +++ b/source/TitleState.hx @@ -55,6 +55,8 @@ class TitleState extends MusicBeatState override public function create():Void { + Paths.clearStoredMemory(); + fun = FlxG.random.int(0, 999); if(fun == 1) { @@ -124,6 +126,7 @@ class TitleState extends MusicBeatState startIntro(); }); #end + Paths.clearUnusedMemory(); } var logoBl:FlxSprite;