Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Psych Engine memory stuff (omg memory like memory dave and bambi ost!… #1

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions source/FreeplayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
85 changes: 80 additions & 5 deletions source/Paths.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> =
[
'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<String, FlxGraphic> = [];
public static var localTrackedAssets:Array<String> = [];
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)
{
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -219,7 +294,7 @@ class Paths
}
}
// Thanks psych engine for the sound code
public static var localTrackedAssets:Array<String> = [];

public static var currentTrackedSounds:Map<String, Sound> = [];
public static function returnSound(path:String, key:String, ?library:String) {
// #if MODS_ALLOWED
Expand Down
3 changes: 3 additions & 0 deletions source/PlaySelectionState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -150,6 +152,7 @@ class PlaySelectionState extends MusicBeatState
{
selectable = true;
});
// Paths.clearUnusedMemory();
}

var selectedSomethin:Bool = false;
Expand Down
3 changes: 3 additions & 0 deletions source/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1961,6 +1963,7 @@ class PlayState extends MusicBeatState
hidehphud();

super.create();
Paths.clearUnusedMemory();

perlinCamera = new Perlin(Math.floor(Math.random() * 65535));

Expand Down
4 changes: 4 additions & 0 deletions source/StoryMenuState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using StringTools;

class StoryMenuState extends MusicBeatState
{

var scoreText:FlxText;

public static var weekUnlocked:Array<Bool> = [true, true, true, true, true, true];
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions source/TitleState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class TitleState extends MusicBeatState

override public function create():Void
{
Paths.clearStoredMemory();

fun = FlxG.random.int(0, 999);
if(fun == 1)
{
Expand Down Expand Up @@ -124,6 +126,7 @@ class TitleState extends MusicBeatState
startIntro();
});
#end
Paths.clearUnusedMemory();
}

var logoBl:FlxSprite;
Expand Down
Loading