From 5bcc0f9b25c8dc4d8942fa87fb085430efc99575 Mon Sep 17 00:00:00 2001
From: Cameron Taylor <cameron.taylor.ninja@gmail.com>
Date: Wed, 5 Jun 2024 15:02:29 -0400
Subject: [PATCH] partial sound loading should be more resiliant

---
 source/funkin/audio/FunkinSound.hx         | 20 ++++++++++++++------
 source/funkin/ui/freeplay/FreeplayState.hx |  2 ++
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/source/funkin/audio/FunkinSound.hx b/source/funkin/audio/FunkinSound.hx
index 4cd0e7557..4f61e70c2 100644
--- a/source/funkin/audio/FunkinSound.hx
+++ b/source/funkin/audio/FunkinSound.hx
@@ -357,6 +357,11 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
 
     var shouldLoadPartial = params.partialParams?.loadPartial ?? false;
 
+    // even if we arent' trying to partial load a song, we want to error out any songs in progress,
+    // so we don't get overlapping music if someone were to load a new song while a partial one is loading!
+
+    emptyPartialQueue();
+
     if (shouldLoadPartial)
     {
       var music = FunkinSound.loadPartial(pathToUse, params.partialParams?.start ?? 0.0, params.partialParams?.end ?? 1.0, params?.startingVolume ?? 1.0,
@@ -364,12 +369,6 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
 
       if (music != null)
       {
-        while (partialQueue.length > 0)
-        {
-          @:nullSafety(Off)
-          partialQueue.pop().error("Cancel loading partial sound");
-        }
-
         partialQueue.push(music);
 
         @:nullSafety(Off)
@@ -406,6 +405,15 @@ class FunkinSound extends FlxSound implements ICloneable<FunkinSound>
     }
   }
 
+  public static function emptyPartialQueue():Void
+  {
+    while (partialQueue.length > 0)
+    {
+      @:nullSafety(Off)
+      partialQueue.pop().error("Cancel loading partial sound");
+    }
+  }
+
   static var partialQueue:Array<Promise<Null<FunkinSound>>> = [];
 
   /**
diff --git a/source/funkin/ui/freeplay/FreeplayState.hx b/source/funkin/ui/freeplay/FreeplayState.hx
index 4fc94c2d7..2ecbb5739 100644
--- a/source/funkin/ui/freeplay/FreeplayState.hx
+++ b/source/funkin/ui/freeplay/FreeplayState.hx
@@ -1769,6 +1769,8 @@ class FreeplayState extends MusicBeatSubState
     funnyScroll3.visible = false;
 
     new FlxTimer().start(1, function(tmr:FlxTimer) {
+      FunkinSound.emptyPartialQueue();
+
       Paths.setCurrentLevel(cap.songData.levelId);
       LoadingState.loadPlayState(
         {