From 4852515c4295a3b87bb9e435c0f8e38a67257323 Mon Sep 17 00:00:00 2001
From: EliteMasterEric <ericmyllyoja@gmail.com>
Date: Fri, 4 Aug 2023 11:18:00 -0400
Subject: [PATCH] Fix bug where pressing ENTER in UI would cause song to try to
 preview. Fix bug where trying to preview a newly created song would crash.

---
 source/funkin/play/song/Song.hx                     | 10 +++++++---
 source/funkin/ui/debug/charting/ChartEditorState.hx |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/source/funkin/play/song/Song.hx b/source/funkin/play/song/Song.hx
index 189adb840..ab22ad9e9 100644
--- a/source/funkin/play/song/Song.hx
+++ b/source/funkin/play/song/Song.hx
@@ -39,7 +39,11 @@ class Song implements IPlayStateScriptedClass
 
   var difficultyIds:Array<String>;
 
-  public function new(id:String)
+  /**
+   * @param id The ID of the song to load.
+   * @param ignoreErrors If false, an exception will be thrown if the song data could not be loaded.
+   */
+  public function new(id:String, ignoreErrors:Bool = false)
   {
     this.songId = id;
 
@@ -48,7 +52,7 @@ class Song implements IPlayStateScriptedClass
     difficulties = new Map<String, SongDifficulty>();
 
     _metadata = SongDataParser.loadSongMetadata(songId);
-    if (_metadata == null || _metadata.length == 0)
+    if (_metadata == null || _metadata.length == 0 && !ignoreErrors)
     {
       throw 'Could not find song data for songId: $songId';
     }
@@ -60,7 +64,7 @@ class Song implements IPlayStateScriptedClass
   public static function buildRaw(songId:String, metadata:Array<SongMetadata>, variations:Array<String>, charts:Map<String, SongChartData>,
       ?validScore:Bool = false):Song
   {
-    var result:Song = new Song(songId);
+    var result:Song = new Song(songId, true);
 
     result._metadata.clear();
     for (meta in metadata)
diff --git a/source/funkin/ui/debug/charting/ChartEditorState.hx b/source/funkin/ui/debug/charting/ChartEditorState.hx
index 8e5a65c80..4f487f70c 100644
--- a/source/funkin/ui/debug/charting/ChartEditorState.hx
+++ b/source/funkin/ui/debug/charting/ChartEditorState.hx
@@ -2761,7 +2761,7 @@ class ChartEditorState extends HaxeUIState
    */
   function handleTestKeybinds():Void
   {
-    if (FlxG.keys.justPressed.ENTER)
+    if (!isHaxeUIDialogOpen && FlxG.keys.justPressed.ENTER)
     {
       var minimal = FlxG.keys.pressed.SHIFT;
       testSongInPlayState(minimal);