@@ -19,6 +19,16 @@ class FreeplayState extends MusicBeatState
1919 */
2020 public var songs : Array <ChartMetaData > = [];
2121
22+ /**
23+ * Current song metadata
24+ */
25+ public var curSong : Null <ChartMetaData >;
26+
27+ /**
28+ * Current song metadata difficulty
29+ */
30+ public var curSongDifficulty : Null <String >;
31+
2232 /**
2333 * Currently selected song
2434 */
@@ -107,7 +117,9 @@ class FreeplayState extends MusicBeatState
107117 curSelected = k ;
108118 }
109119 }
110- if (songs [curSelected ] != null ) {
120+ updateCurSongMeta ();
121+
122+ if (curSong != null ) {
111123 for (k => diff in songs [curSelected ].difficulties ) {
112124 if (diff == Options .freeplayLastDifficulty ) {
113125 curDifficulty = k ;
@@ -238,22 +250,22 @@ class FreeplayState extends MusicBeatState
238250 scoreText .x = coopText .x = scoreBG .x + 4 ;
239251 diffText .x = Std .int (scoreBG .x + ((scoreBG .width - diffText .width ) / 2 ));
240252
241- interpColor .fpsLerpTo (songs [ curSelected ] .color , 0.0625 );
253+ interpColor .fpsLerpTo (curSong .color , 0.0625 );
242254 bg .color = interpColor .color ;
243255
244256 #if PRELOAD_ALL
245257 var dontPlaySongThisFrame = false ;
246258 autoplayElapsed + = elapsed ;
247259 if (! disableAutoPlay && ! songInstPlaying && (autoplayElapsed > timeUntilAutoplay || FlxG .keys .justPressed .SPACE )) {
248- if (curPlayingInst != (curPlayingInst = Paths .inst (songs [ curSelected ] .name , songs [ curSelected ]. difficulties [ curDifficulty ] ))) {
260+ if (curPlayingInst != (curPlayingInst = Paths .inst (curSong .name , curSongDifficulty , curSong . instSuffix ))) {
249261 var streamed = false ;
250262 if (Options .streamedMusic ) {
251263 var sound = Assets .getMusic (curPlayingInst , true , false );
252264 streamed = sound != null ;
253265
254266 if (streamed && autoplayShouldPlay ) {
255267 FlxG .sound .playMusic (sound , 0 );
256- Conductor .changeBPM (songs [ curSelected ] .bpm , songs [ curSelected ] .beatsPerMeasure , songs [ curSelected ] .stepsPerBeat );
268+ Conductor .changeBPM (curSong .bpm , curSong .beatsPerMeasure , curSong .stepsPerBeat );
257269 }
258270 }
259271
@@ -268,7 +280,7 @@ class FreeplayState extends MusicBeatState
268280
269281 if (sound != null && autoplayShouldPlay ) {
270282 FlxG .sound .playMusic (sound , 0 );
271- Conductor .changeBPM (songs [ curSelected ] .bpm , songs [ curSelected ] .beatsPerMeasure , songs [ curSelected ] .stepsPerBeat );
283+ Conductor .changeBPM (curSong .bpm , curSong .beatsPerMeasure , curSong .stepsPerBeat );
272284 }
273285 }
274286 if (! disableAsyncLoading ) Main .execAsync (huh );
@@ -302,7 +314,6 @@ class FreeplayState extends MusicBeatState
302314 function updateCoopModes () {
303315 __opponentMode = false ;
304316 __coopMode = false ;
305- var curSong = songs [curSelected ];
306317 if (curSong .coopAllowed && curSong .opponentModeAllowed ) {
307318 __opponentMode = curCoopMode % 2 == 1 ;
308319 __coopMode = curCoopMode >= 2 ;
@@ -321,25 +332,25 @@ class FreeplayState extends MusicBeatState
321332
322333 if (songs [curSelected ].difficulties .length <= 0 ) return ;
323334
324- var event = event (" onSelect" , EventManager .get (FreeplaySongSelectEvent ).recycle (songs [ curSelected ] .name , songs [ curSelected ]. difficulties [ curDifficulty ] , __opponentMode , __coopMode ));
335+ var event = event (" onSelect" , EventManager .get (FreeplaySongSelectEvent ).recycle (curSong .name , curSongDifficulty , __opponentMode , __coopMode ));
325336
326337 if (event .cancelled ) return ;
327338
328339 #if PRELOAD_ALL
329340 autoplayShouldPlay = false ;
330341 #end
331342
332- Options .freeplayLastSong = songs [ curSelected ] .name ;
333- Options .freeplayLastDifficulty = songs [ curSelected ]. difficulties [ curDifficulty ] ;
343+ Options .freeplayLastSong = curSong .name ;
344+ Options .freeplayLastDifficulty = curSongDifficulty ;
334345
335346 PlayState .loadSong (event .song , event .difficulty , event .opponentMode , event .coopMode );
336347 FlxG .switchState (new PlayState ());
337348 }
338349
339350 public function convertChart () {
340- trace (' Converting ${songs [ curSelected ] .name } ( ${songs [ curSelected ]. difficulties [ curDifficulty ] }) to Codename format...' );
341- var chart = Chart .parse (songs [ curSelected ] .name , songs [ curSelected ]. difficulties [ curDifficulty ] );
342- Chart .save (' ${Main .pathBack }assets/songs/ ${songs [ curSelected ] .name }' , chart , songs [ curSelected ]. difficulties [ curDifficulty ] );
351+ trace (' Converting ${curSong .name } ( ${curSongDifficulty }) to Codename format...' );
352+ var chart = Chart .parse (curSong .name , curSongDifficulty );
353+ Chart .save (' ${Main .pathBack }assets/songs/ ${curSong .name }' , chart , curSongDifficulty );
343354 }
344355
345356 /**
@@ -355,10 +366,13 @@ class FreeplayState extends MusicBeatState
355366 var validDifficulties = curSong .difficulties .length > 0 ;
356367 var event = event (" onChangeDiff" , EventManager .get (MenuChangeEvent ).recycle (curDifficulty , validDifficulties ? FlxMath .wrap (curDifficulty + change , 0 , curSong .difficulties .length - 1 ) : 0 , change ));
357368
358- if (event .cancelled ) return ;
369+ if (event .cancelled ) {
370+ updateCurSongMeta ();
371+ return ;
372+ }
359373
360374 curDifficulty = event .value ;
361-
375+ updateCurSongMeta ();
362376 updateScore ();
363377
364378 if (curSong .difficulties .length > 1 )
@@ -376,7 +390,7 @@ class FreeplayState extends MusicBeatState
376390 var changes : Array <HighscoreChange > = [];
377391 if (__coopMode ) changes .push (CCoopMode );
378392 if (__opponentMode ) changes .push (COpponentMode );
379- var saveData = FunkinSave .getSongHighscore (songs [ curSelected ] .name , songs [ curSelected ]. difficulties [ curDifficulty ] , changes );
393+ var saveData = FunkinSave .getSongHighscore (curSong .name , curSongDifficulty , changes );
380394 intendedScore = saveData .score ;
381395 }
382396
@@ -397,9 +411,9 @@ class FreeplayState extends MusicBeatState
397411 */
398412 public function changeCoopMode (change : Int = 0 , force : Bool = false ) {
399413 if (change == 0 && ! force ) return ;
400- if (! songs [ curSelected ] .coopAllowed && ! songs [ curSelected ] .opponentModeAllowed ) return ;
414+ if (! curSong .coopAllowed && ! curSong .opponentModeAllowed ) return ;
401415
402- var bothEnabled = songs [ curSelected ] .coopAllowed && songs [ curSelected ] .opponentModeAllowed ;
416+ var bothEnabled = curSong .coopAllowed && curSong .opponentModeAllowed ;
403417 var event = event (" onChangeCoopMode" , EventManager .get (MenuChangeEvent ).recycle (curCoopMode , FlxMath .wrap (curCoopMode + change , 0 , bothEnabled ? 3 : 1 ), change ));
404418
405419 if (event .cancelled ) return ;
@@ -408,12 +422,19 @@ class FreeplayState extends MusicBeatState
408422
409423 updateScore ();
410424
425+ #if PRELOAD_ALL
426+ if (songs [curSelected ] != curSong ) {
427+ autoplayElapsed = 0 ;
428+ songInstPlaying = false ;
429+ }
430+ #end
431+
411432 var key = " [TAB] " ; // TODO: make this configurable
412433
413434 if (bothEnabled ) {
414435 coopText .text = key + coopLabels [curCoopMode ];
415436 } else {
416- coopText .text = key + coopLabels [curCoopMode * (songs [ curSelected ] .coopAllowed ? 2 : 1 )];
437+ coopText .text = key + coopLabels [curCoopMode * (curSong .coopAllowed ? 2 : 1 )];
417438 }
418439 }
419440
@@ -426,7 +447,6 @@ class FreeplayState extends MusicBeatState
426447 {
427448 if (change == 0 && ! force ) return ;
428449
429- var bothEnabled = songs [curSelected ].coopAllowed && songs [curSelected ].opponentModeAllowed ;
430450 var event = event (" onChangeSelection" , EventManager .get (MenuChangeEvent ).recycle (curSelected , FlxMath .wrap (curSelected + change , 0 , songs .length - 1 ), change ));
431451 if (event .cancelled ) return ;
432452
@@ -440,7 +460,7 @@ class FreeplayState extends MusicBeatState
440460 songInstPlaying = false ;
441461 #end
442462
443- coopText .visible = songs [ curSelected ] .coopAllowed || songs [ curSelected ] .opponentModeAllowed ;
463+ coopText .visible = curSong .coopAllowed || curSong .opponentModeAllowed ;
444464 }
445465
446466 function updateOptionsAlpha () {
@@ -465,6 +485,16 @@ class FreeplayState extends MusicBeatState
465485 item .alpha = selectedAlpha ;
466486 }
467487 }
488+
489+ function updateCurSongMeta () {
490+ var song = songs [curSelected ];
491+ if (song == null ) {
492+ curSong = null ;
493+ curSongDifficulty = null ;
494+ }
495+ else if ((curSong = song .metas .get (curSongDifficulty = curSong .difficulties [curDifficulty ])) == null )
496+ curSong = song ;
497+ }
468498}
469499
470500class FreeplaySonglist {
@@ -485,7 +515,7 @@ class FreeplaySonglist {
485515 }
486516 if (songsFound == null ) songsFound = Paths .getFolderDirectories (" songs" , false , source );
487517 if (songsFound .length > 0 ) {
488- for (s in songsFound ) songs .push (Chart .loadChartMeta (s , Flags . DEFAULT_DIFFICULTY , source == MODS ));
518+ for (s in songsFound ) songs .push (Chart .loadChartMeta (s , source == MODS ));
489519 return false ;
490520 }
491521 return true ;
0 commit comments