Skip to content

Commit d2a1139

Browse files
committed
instSuffix, meta.metas for freeplay preview
1 parent d3ef27b commit d2a1139

File tree

6 files changed

+88
-41
lines changed

6 files changed

+88
-41
lines changed

source/funkin/backend/assets/Paths.hx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ class Paths
8888
inline static public function music(key:String, ?library:String, ?ext:String)
8989
return getPath('music/$key.${ext != null ? ext : Flags.SOUND_EXT}', library);
9090

91-
inline static public function voices(song:String, ?difficulty:String, ?prefix:String = "", ?ext:String) {
91+
inline static public function voices(song:String, ?difficulty:String, ?suffix:String = "", ?ext:String) {
9292
if (difficulty == null) difficulty = Flags.DEFAULT_DIFFICULTY;
9393
if (ext == null) ext = Flags.SOUND_EXT;
94-
var diff = getPath('songs/$song/song/Voices$prefix-${difficulty}.${ext}', null);
95-
return OpenFlAssets.exists(diff) ? diff : getPath('songs/$song/song/Voices$prefix.${ext}', null);
94+
var diff = getPath('songs/$song/song/Voices$suffix-${difficulty}.${ext}', null);
95+
return OpenFlAssets.exists(diff) ? diff : getPath('songs/$song/song/Voices$suffix.${ext}', null);
9696
}
9797

98-
inline static public function inst(song:String, ?difficulty:String, ?prefix:String = "", ?ext:String) {
98+
inline static public function inst(song:String, ?difficulty:String, ?suffix:String = "", ?ext:String) {
9999
if (difficulty == null) difficulty = Flags.DEFAULT_DIFFICULTY;
100100
if (ext == null) ext = Flags.SOUND_EXT;
101-
var diff = getPath('songs/$song/song/Inst$prefix-${difficulty}.${ext}', null);
102-
return OpenFlAssets.exists(diff) ? diff : getPath('songs/$song/song/Inst$prefix.${ext}', null);
101+
var diff = getPath('songs/$song/song/Inst$suffix-${difficulty}.${ext}', null);
102+
return OpenFlAssets.exists(diff) ? diff : getPath('songs/$song/song/Inst$suffix.${ext}', null);
103103
}
104104

105105
static public function image(key:String, ?library:String, checkForAtlas:Bool = false, ?ext:String) {

source/funkin/backend/chart/Chart.hx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ class Chart {
8383
return data;
8484
}
8585

86-
public static function loadChartMeta(songName:String, ?difficulty:String, fromMods:Bool = true):ChartMetaData {
86+
public static function loadChartMeta(songName:String, ?difficulty:String, fromMods:Bool = true, includeMetaDifficulties:Bool = false):ChartMetaData {
8787
if (difficulty == null) difficulty = Flags.DEFAULT_DIFFICULTY;
8888

8989
var metaPath = Paths.file('songs/${songName}/meta.json');
9090
var metaDiffPath = Paths.file('songs/${songName}/meta-${difficulty}.json');
9191

92-
var data:ChartMetaData = null;
92+
var data:ChartMetaData = null, fromDifficulty = false;
9393
var fromMods:Bool = fromMods;
9494
for (path in [metaDiffPath, metaPath]) if (Assets.exists(path)) {
9595
fromMods = Paths.assetsTree.existsSpecific(path, "TEXT", MODS);
@@ -98,23 +98,31 @@ class Chart {
9898
tempData.color = CoolUtil.getColorFromDynamic(tempData.color).getDefault(Flags.DEFAULT_COLOR);
9999
data = tempData;
100100
} catch(e) Logs.trace('Failed to load song metadata for ${songName} ($path): ${Std.string(e)}', ERROR);
101-
if (data != null) break;
101+
if (data != null) {
102+
fromDifficulty = path == metaDiffPath;
103+
break;
104+
}
102105
}
103106

104-
if (data == null) data = {
105-
name: songName,
106-
bpm: 100
107-
};
107+
if (data == null) {
108+
data = {
109+
name: songName,
110+
bpm: Flags.DEFAULT_BPM
111+
};
112+
}
113+
else {
114+
data.name = songName;
115+
data.setFieldDefault("bpm", Flags.DEFAULT_BPM);
116+
}
108117

109-
data.name = songName;
110-
data.setFieldDefault("bpm", Flags.DEFAULT_BPM);
118+
data.setFieldDefault("displayName", data.name);
111119
data.setFieldDefault("beatsPerMeasure", Flags.DEFAULT_BEATS_PER_MEASURE);
112120
data.setFieldDefault("stepsPerBeat", Flags.DEFAULT_STEPS_PER_BEAT);
113121
data.setFieldDefault("icon", Flags.DEFAULT_HEALTH_ICON);
114122
data.setFieldDefault("difficulties", []);
115123
data.setFieldDefault("coopAllowed", Flags.DEFAULT_COOP_ALLOWED);
116124
data.setFieldDefault("opponentModeAllowed", Flags.DEFAULT_OPPONENT_MODE_ALLOWED);
117-
data.setFieldDefault("displayName", data.name);
125+
data.setFieldDefault("instSuffix", "");
118126

119127
if (data.difficulties.length <= 0) {
120128
data.difficulties = [for(f in Paths.getFolderContent('songs/${songName}/charts/', false, fromMods ? MODS : SOURCE)) if (Path.extension(f.toUpperCase()) == "JSON") Path.withoutExtension(f)];
@@ -131,6 +139,12 @@ class Chart {
131139
}
132140
}
133141

142+
if (includeMetaDifficulties && !fromDifficulty) {
143+
data.metas = [];
144+
for (difficulty in data.difficulties) if (!data.metas.exists(difficulty) && Assets.exists(Paths.file('songs/${songName}/meta-${difficulty}.json')))
145+
data.metas.set(difficulty, loadChartMeta(songName, difficulty, fromMods, false));
146+
}
147+
134148
return data;
135149
}
136150

@@ -304,7 +318,8 @@ class Chart {
304318

305319
public static inline function makeMetaSaveable(meta:ChartMetaData, prettyPrint:Bool = true):String {
306320
var data:Dynamic = Reflect.copy(meta);
307-
if (data.color != null) data.color = new FlxColor(data.color).toWebString(); // dont even ask me - Nex
321+
if (data.color != null) data.color = FlxColor.fromInt(data.color).toWebString(); // dont even ask me - Nex
322+
Reflect.deleteField(data, 'metas');
308323
return Json.stringify(data, null, prettyPrint ? Flags.JSON_PRETTY_PRINT : null);
309324
}
310325
}

source/funkin/backend/chart/ChartData.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ typedef ChartMetaData = {
2828
public var ?coopAllowed:Bool;
2929
public var ?opponentModeAllowed:Bool;
3030
public var ?customValues:Dynamic;
31+
public var ?metas:Map<String, ChartMetaData>;
32+
public var ?instSuffix:String;
3133
}
3234

3335
typedef ChartStrumLine = {

source/funkin/editors/charter/Charter.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ class Charter extends UIState {
601601
Conductor.setupSong(PlayState.SONG);
602602
noteTypes = PlayState.SONG.noteTypes;
603603

604-
FlxG.sound.setMusic(FlxG.sound.load(Paths.inst(__song, __diff)));
604+
FlxG.sound.setMusic(FlxG.sound.load(Paths.inst(__song, __diff, PlayState.SONG.meta.instSuffix)));
605605
vocals = Assets.exists(Paths.voices(__song, __diff)) ? FlxG.sound.load(Paths.voices(__song, __diff)) : new FlxSound();
606606
vocals.group = FlxG.sound.defaultMusicGroup;
607607

@@ -680,7 +680,7 @@ class Charter extends UIState {
680680
if (FlxG.sound.music.loaded)
681681
wavesToGenerate.push({name: "Inst.ogg", sound: FlxG.sound.music});
682682

683-
if (PlayState.SONG.meta.needsVoices != false && vocals.loaded)
683+
if (vocals.loaded)
684684
wavesToGenerate.push({name: "Voices.ogg", sound: vocals});
685685

686686
for (strumLine in strumLines)

source/funkin/game/PlayState.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ class PlayState extends MusicBeatState
11131113
curSong = songData.meta.name.toLowerCase();
11141114
curSongID = curSong.replace(" ", "-");
11151115

1116-
FlxG.sound.setMusic(inst = FlxG.sound.load(Assets.getMusic(Paths.inst(SONG.meta.name, difficulty))));
1116+
FlxG.sound.setMusic(inst = FlxG.sound.load(Assets.getMusic(Paths.inst(SONG.meta.name, difficulty, SONG.meta.instSuffix))));
11171117

11181118
var vocalsPath = Paths.voices(SONG.meta.name, difficulty);
11191119
vocals = Assets.exists(vocalsPath) ? FlxG.sound.load(Options.streamedVocals ? Assets.getMusic(vocalsPath) : vocalsPath) : new FlxSound();

source/funkin/menus/FreeplayState.hx

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

470500
class 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

Comments
 (0)