Skip to content

Commit

Permalink
Mod sounds/music caching bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowMario committed Jan 15, 2024
1 parent d207c1e commit dcab743
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 120 deletions.
1 change: 0 additions & 1 deletion assets/shared/data/bopeebo/bopeebo-boobs.json

This file was deleted.

21 changes: 0 additions & 21 deletions source/backend/BaseStage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,6 @@ class BaseStage extends FlxBasic
PlayState.instance.endCallback = myfn;
}

//precache functions
public function precacheImage(key:String) precache(key, 'image');
public function precacheSound(key:String) precache(key, 'sound');
public function precacheMusic(key:String) precache(key, 'music');

public function precache(key:String, type:String)
{
if(onPlayState)
PlayState.instance.precacheList.set(key, type);

switch(type)
{
case 'image':
Paths.image(key);
case 'sound':
Paths.sound(key);
case 'music':
Paths.music(key);
}
}

// overrides
function startCountdown() if(onPlayState) return PlayState.instance.startCountdown(); else return false;
function endSong() if(onPlayState)return PlayState.instance.endSong(); else return false;
Expand Down
31 changes: 16 additions & 15 deletions source/backend/Paths.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ class Paths
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',
];
public static var dumpExclusions:Array<String> = ['assets/shared/music/freakyMenu.$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
Expand Down Expand Up @@ -66,24 +61,25 @@ class Paths

// define the locally tracked assets
public static var localTrackedAssets:Array<String> = [];
public static function clearStoredMemory(?cleanUnused:Bool = false) {
public static function clearStoredMemory() {
// 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)) {
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);
for (key => asset in currentTrackedSounds)
{
if (!localTrackedAssets.contains(key) && !dumpExclusions.contains(key) && asset != null)
{
Assets.cache.clear(key);
currentTrackedSounds.remove(key);
}
Expand Down Expand Up @@ -437,14 +433,16 @@ class Paths
#if MODS_ALLOWED
var modLibPath:String = '';
if (library != null) modLibPath = '$library/';
if (path != null) modLibPath += '$path/';
if (path != null) modLibPath += '$path';

var file:String = modsSounds(modLibPath, key);
if(FileSystem.exists(file)) {
if(!currentTrackedSounds.exists(file)) {
if(!currentTrackedSounds.exists(file))
{
currentTrackedSounds.set(file, Sound.fromFile(file));
//trace('precached mod sound: $file');
}
localTrackedAssets.push(key);
localTrackedAssets.push(file);
return currentTrackedSounds.get(file);
}
#end
Expand All @@ -460,7 +458,10 @@ class Paths
var retKey:String = (path != null) ? '$path/$key' : key;
retKey = ((path == 'songs') ? 'songs:' : '') + getPath('$retKey.$SOUND_EXT', SOUND, library);
if(OpenFlAssets.exists(retKey, SOUND))
{
currentTrackedSounds.set(gottenPath, OpenFlAssets.getSound(retKey));
//trace('precached vanilla sound: $retKey');
}
}
localTrackedAssets.push(gottenPath);
return currentTrackedSounds.get(gottenPath);
Expand Down
4 changes: 4 additions & 0 deletions source/cutscenes/DialogueBoxPsych.hx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class DialogueBoxPsych extends FlxSpriteGroup
{
super();

//precache sounds
Paths.sound('dialogue');
Paths.sound('dialogueClose');

if(song != null && song != '') {
FlxG.sound.playMusic(Paths.music(song), 0);
FlxG.sound.music.fadeIn(2, 0, 1);
Expand Down
54 changes: 29 additions & 25 deletions source/psychlua/HScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -198,33 +198,33 @@ class HScript extends SScript
set('keyJustPressed', function(name:String = '') {
name = name.toLowerCase();
switch(name) {
case 'left': return PlayState.instance.controls.NOTE_LEFT_P;
case 'down': return PlayState.instance.controls.NOTE_DOWN_P;
case 'up': return PlayState.instance.controls.NOTE_UP_P;
case 'right': return PlayState.instance.controls.NOTE_RIGHT_P;
default: return PlayState.instance.controls.justPressed(name);
case 'left': return Controls.instance.NOTE_LEFT_P;
case 'down': return Controls.instance.NOTE_DOWN_P;
case 'up': return Controls.instance.NOTE_UP_P;
case 'right': return Controls.instance.NOTE_RIGHT_P;
default: return Controls.instance.justPressed(name);
}
return false;
});
set('keyPressed', function(name:String = '') {
name = name.toLowerCase();
switch(name) {
case 'left': return PlayState.instance.controls.NOTE_LEFT;
case 'down': return PlayState.instance.controls.NOTE_DOWN;
case 'up': return PlayState.instance.controls.NOTE_UP;
case 'right': return PlayState.instance.controls.NOTE_RIGHT;
default: return PlayState.instance.controls.pressed(name);
case 'left': return Controls.instance.NOTE_LEFT;
case 'down': return Controls.instance.NOTE_DOWN;
case 'up': return Controls.instance.NOTE_UP;
case 'right': return Controls.instance.NOTE_RIGHT;
default: return Controls.instance.pressed(name);
}
return false;
});
set('keyReleased', function(name:String = '') {
name = name.toLowerCase();
switch(name) {
case 'left': return PlayState.instance.controls.NOTE_LEFT_R;
case 'down': return PlayState.instance.controls.NOTE_DOWN_R;
case 'up': return PlayState.instance.controls.NOTE_UP_R;
case 'right': return PlayState.instance.controls.NOTE_RIGHT_R;
default: return PlayState.instance.controls.justReleased(name);
case 'left': return Controls.instance.NOTE_LEFT_R;
case 'down': return Controls.instance.NOTE_DOWN_R;
case 'up': return Controls.instance.NOTE_UP_R;
case 'right': return Controls.instance.NOTE_RIGHT_R;
default: return Controls.instance.justReleased(name);
}
return false;
});
Expand Down Expand Up @@ -269,7 +269,8 @@ class HScript extends SScript
return;
}
#end
PlayState.instance.addTextToDebug('$origin - $msg', FlxColor.RED);
if(PlayState.instance != null) PlayState.instance.addTextToDebug('$origin - $msg', FlxColor.RED);
else trace('$origin - $msg');
}
});
#if LUA_ALLOWED
Expand All @@ -278,9 +279,7 @@ class HScript extends SScript
set('parentLua', null);
#end
set('this', this);
set('game', PlayState.instance);
if (PlayState.instance != null)
setSpecialObject(PlayState.instance, false, PlayState.instance.instancesExclude);
set('game', FlxG.state);

set('buildTarget', LuaUtils.getBuildTarget());
set('customSubstate', CustomSubstate.instance);
Expand All @@ -292,12 +291,17 @@ class HScript extends SScript
set('Function_StopHScript', LuaUtils.Function_StopHScript);
set('Function_StopAll', LuaUtils.Function_StopAll);

set('add', function(obj:FlxBasic) PlayState.instance.add(obj));
set('addBehindGF', function(obj:FlxBasic) PlayState.instance.addBehindGF(obj));
set('addBehindDad', function(obj:FlxBasic) PlayState.instance.addBehindDad(obj));
set('addBehindBF', function(obj:FlxBasic) PlayState.instance.addBehindBF(obj));
set('insert', function(pos:Int, obj:FlxBasic) PlayState.instance.insert(pos, obj));
set('remove', function(obj:FlxBasic, ?splice:Bool = false) PlayState.instance.remove(obj, splice));
set('add', FlxG.state.add);
set('insert', FlxG.state.insert);
set('remove', FlxG.state.remove);

if(PlayState.instance == FlxG.state)
{
set('addBehindGF', PlayState.instance.addBehindGF);
set('addBehindDad', PlayState.instance.addBehindDad);
set('addBehindBF', PlayState.instance.addBehindBF);
setSpecialObject(PlayState.instance, false, PlayState.instance.instancesExclude);
}

if(varsToBring != null) {
for (key in Reflect.fields(varsToBring)) {
Expand Down
53 changes: 16 additions & 37 deletions source/states/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ class PlayState extends MusicBeatState

// Less laggy controls
private var keysArray:Array<String>;

public var precacheList:Map<String, String> = new Map<String, String>();
public var songName:String;

// Callbacks for stages
Expand Down Expand Up @@ -617,42 +615,26 @@ class PlayState extends MusicBeatState
startCallback();
RecalculateRating();

//PRECACHING MISS SOUNDS BECAUSE I THINK THEY CAN LAG PEOPLE AND FUCK THEM UP IDK HOW HAXE WORKS
if(ClientPrefs.data.hitsoundVolume > 0) precacheList.set('hitsound', 'sound');
precacheList.set('missnote1', 'sound');
precacheList.set('missnote2', 'sound');
precacheList.set('missnote3', 'sound');
FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);

//PRECACHING THINGS THAT GET USED FREQUENTLY TO AVOID LAGSPIKES
if(ClientPrefs.data.hitsoundVolume > 0) Paths.sound('hitsound');
for (i in 1...4) Paths.sound('missnote$i');
Paths.image('alphabet');

if (PauseSubState.songName != null) {
precacheList.set(PauseSubState.songName, 'music');
} else if(ClientPrefs.data.pauseMusic != 'None') {
precacheList.set(Paths.formatToSongPath(ClientPrefs.data.pauseMusic), 'music');
}
if (PauseSubState.songName != null)
Paths.music(PauseSubState.songName);
else if(Paths.formatToSongPath(ClientPrefs.data.pauseMusic) != 'none')
Paths.music(Paths.formatToSongPath(ClientPrefs.data.pauseMusic));

precacheList.set('alphabet', 'image');
resetRPC();

FlxG.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
callOnScripts('onCreatePost');

cacheCountdown();
cachePopUpScore();

for (key => type in precacheList)
{
//trace('Key $key is type $type');
switch(type)
{
case 'image':
Paths.image(key);
case 'sound':
Paths.sound(key);
case 'music':
Paths.music(key);
}
}

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

Expand Down Expand Up @@ -906,8 +888,6 @@ class PlayState extends MusicBeatState

if(dialogueFile.dialogue.length > 0) {
inCutscene = true;
precacheList.set('dialogue', 'sound');
precacheList.set('dialogueClose', 'sound');
psychDialogue = new DialogueBoxPsych(dialogueFile, song);
psychDialogue.scrollFactor.set();
if(endingSong) {
Expand Down Expand Up @@ -1466,8 +1446,7 @@ class PlayState extends MusicBeatState
addCharacterToList(newCharacter, charType);

case 'Play Sound':
precacheList.set(event.value1, 'sound');
Paths.sound(event.value1);
Paths.sound(event.value1); //Precache sound
}
stagesFunc(function(stage:BaseStage) stage.eventPushedUnique(event));
}
Expand Down Expand Up @@ -2263,7 +2242,7 @@ class PlayState extends MusicBeatState
camFollow.x -= boyfriend.cameraPosition[0] - boyfriendCameraOffset[0];
camFollow.y += boyfriend.cameraPosition[1] + boyfriendCameraOffset[1];

if (Paths.formatToSongPath(SONG.song) == 'tutorial' && cameraTwn == null && FlxG.camera.zoom != 1)
if (songName == 'tutorial' && cameraTwn == null && FlxG.camera.zoom != 1)
{
cameraTwn = FlxTween.tween(FlxG.camera, {zoom: 1}, (Conductor.stepCrochet * 4 / 1000), {ease: FlxEase.elasticInOut, onComplete:
function (twn:FlxTween)
Expand All @@ -2276,7 +2255,7 @@ class PlayState extends MusicBeatState
}

public function tweenCamIn() {
if (Paths.formatToSongPath(SONG.song) == 'tutorial' && cameraTwn == null && FlxG.camera.zoom != 1.3) {
if (songName == 'tutorial' && cameraTwn == null && FlxG.camera.zoom != 1.3) {
cameraTwn = FlxTween.tween(FlxG.camera, {zoom: 1.3}, (Conductor.stepCrochet * 4 / 1000), {ease: FlxEase.elasticInOut, onComplete:
function (twn:FlxTween) {
cameraTwn = null;
Expand Down Expand Up @@ -2896,7 +2875,7 @@ class PlayState extends MusicBeatState
var result:Dynamic = callOnLuas('opponentNoteHitPre', [notes.members.indexOf(note), Math.abs(note.noteData), note.noteType, note.isSustainNote]);
if(result != LuaUtils.Function_Stop && result != LuaUtils.Function_StopHScript && result != LuaUtils.Function_StopAll) callOnHScript('opponentNoteHitPre', [note]);

if (Paths.formatToSongPath(SONG.song) != 'tutorial')
if (songName != 'tutorial')
camZooming = true;

if(note.noteType == 'Hey!' && dad.animOffsets.exists('hey')) {
Expand Down Expand Up @@ -3480,7 +3459,7 @@ class PlayState extends MusicBeatState
unlock = (!ClientPrefs.data.cacheOnGPU && !ClientPrefs.data.shaders && ClientPrefs.data.lowQuality && !ClientPrefs.data.antialiasing);

case 'debugger':
unlock = (Paths.formatToSongPath(SONG.song) == 'test' && !usedPractice);
unlock = (songName == 'test' && !usedPractice);
}
}
else // any FC achievements, name should be "weekFileName_nomiss", e.g: "week3_nomiss";
Expand Down
2 changes: 1 addition & 1 deletion source/states/stages/Limo.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Limo extends BaseStage
resetLimoKill();

//PRECACHE SOUND
precacheSound('dancerdeath');
Paths.sound('dancerdeath');
setDefaultGF('gf-car');
}

Expand Down
2 changes: 1 addition & 1 deletion source/states/stages/Mall.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Mall extends BaseStage

santa = new BGSprite('christmas/santa', -840, 150, 1, 1, ['santa idle in fear']);
add(santa);
precacheSound('Lights_Shut_off');
Paths.sound('Lights_Shut_off');
setDefaultGF('gf-christmas');

if(isStoryMode && !seenCutscene)
Expand Down
2 changes: 1 addition & 1 deletion source/states/stages/Philly.hx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Philly extends BaseStage
insert(members.indexOf(blammedLightsBlack) + 1, phillyGlowGradient);
if(!ClientPrefs.data.flashing) phillyGlowGradient.intendedAlpha = 0.7;

precacheImage('philly/particle'); //precache philly glow particle image
Paths.image('philly/particle'); //precache philly glow particle image
phillyGlowParticles = new FlxTypedGroup<PhillyGlowParticle>();
phillyGlowParticles.visible = false;
insert(members.indexOf(phillyGlowGradient) + 1, phillyGlowParticles);
Expand Down
4 changes: 2 additions & 2 deletions source/states/stages/Spooky.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Spooky extends BaseStage
add(halloweenBG);

//PRECACHE SOUNDS
precacheSound('thunder_1');
precacheSound('thunder_2');
Paths.sound('thunder_1');
Paths.sound('thunder_2');

//Monster cutscene
if (isStoryMode && !seenCutscene)
Expand Down
10 changes: 5 additions & 5 deletions source/states/stages/Tank.hx
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ class Tank extends BaseStage
prepareCutscene();
cutsceneHandler.endTime = 12;
cutsceneHandler.music = 'DISTORTO';
precacheSound('wellWellWell');
precacheSound('killYou');
precacheSound('bfBeep');
Paths.sound('wellWellWell');
Paths.sound('killYou');
Paths.sound('bfBeep');

var wellWellWell:FlxSound = new FlxSound().loadEmbedded(Paths.sound('wellWellWell'));
FlxG.sound.list.add(wellWellWell);
Expand Down Expand Up @@ -220,7 +220,7 @@ class Tank extends BaseStage
prepareCutscene();
cutsceneHandler.endTime = 11.5;
cutsceneHandler.music = 'DISTORTO';
precacheSound('tankSong2');
Paths.sound('tankSong2');

var tightBars:FlxSound = new FlxSound().loadEmbedded(Paths.sound('tankSong2'));
FlxG.sound.list.add(tightBars);
Expand Down Expand Up @@ -260,7 +260,7 @@ class Tank extends BaseStage
{
spr.y += 100;
});
precacheSound('stressCutscene');
Paths.sound('stressCutscene');

pico = new FlxAnimate(gf.x + 150, gf.y + 450);
pico.showPivot = false;
Expand Down
Loading

0 comments on commit dcab743

Please sign in to comment.