Skip to content

Update FreeplayState.hx #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions source/funkin/backend/chart/Chart.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ class Chart {
var fromMods:Bool = fromMods;
for (path in [metaDiffPath, metaPath]) if (Assets.exists(path)) {
fromMods = Paths.assetsTree.existsSpecific(path, "TEXT", MODS);
try data = Json.parse(Assets.getText(path))
catch(e) Logs.trace('Failed to load song metadata for ${songName} ($path): ${Std.string(e)}', ERROR);
try {
var tempData = Json.parse(Assets.getText(path));
tempData.color = CoolUtil.getColorFromDynamic(tempData.color).getDefault(defaultColor);
data = tempData;
} catch(e) Logs.trace('Failed to load song metadata for ${songName} ($path): ${Std.string(e)}', ERROR);
if (data != null) break;
}

Expand All @@ -115,7 +118,6 @@ class Chart {
data.setFieldDefault("coopAllowed", false);
data.setFieldDefault("opponentModeAllowed", false);
data.setFieldDefault("displayName", data.name);
data.setFieldDefault("parsedColor", data.color.getColorFromDynamic().getDefault(defaultColor));

if (data.difficulties.length <= 0) {
data.difficulties = [for(f in Paths.getFolderContent('songs/${songNameLower}/charts/', false, !fromMods)) if (Path.extension(f = f.toUpperCase()) == "JSON") Path.withoutExtension(f)];
Expand Down Expand Up @@ -257,33 +259,33 @@ class Chart {

CoolUtil.safeSaveFile(chartPath, Json.stringify(filteredChart, null, saveSettings.prettyPrint == true ? "\t" : null));

// idk how null reacts to it so better be sure
if (saveSettings.overrideExistingMeta == true || !FileSystem.exists(metaPath))
CoolUtil.safeSaveFile(metaPath, Json.stringify(meta, null, saveSettings.prettyPrint == true ? "\t" : null));
CoolUtil.safeSaveFile(metaPath, makeMetaSaveable(meta));
#end
return filteredChart;
}

public static function filterChartForSaving(chart:ChartData, ?saveMetaInChart:Null<Bool>, ?saveEventsInChart:Null<Bool>):ChartData {
var data = Reflect.copy(chart); // make a copy of the chart to leave the OG intact
if (saveMetaInChart != true) {
data.meta = null;
} else {
data.meta = Reflect.copy(chart.meta); // also make a copy of the metadata to leave the OG intact.
if(data.meta != null && Reflect.hasField(data.meta, "parsedColor")) Reflect.deleteField(data.meta, "parsedColor");
}
data.meta = saveMetaInChart != true ? null : data.meta = Reflect.copy(chart.meta); // also make a copy of the metadata to leave the OG intact.

data.events = saveEventsInChart != true ? null : Reflect.copy(chart.events); // same here once again
data.fromMods = null;

var sortedData:Dynamic = {};
for(f in Reflect.fields(data)) {
for (f in Reflect.fields(data)) {
var v = Reflect.field(data, f);
if (v != null)
Reflect.setField(sortedData, f, v);
}
return sortedData;
}

public static inline function makeMetaSaveable(meta:ChartMetaData, prettyPrint:Bool = true):String {
var data:Dynamic = Reflect.copy(meta);
if (data.color != null) data.color = new FlxColor(data.color).toWebString(); // dont even ask me - Nex
return Json.stringify(data, null, prettyPrint ? "\t" : null);
}
}

typedef ChartSaveSettings = {
Expand Down
5 changes: 1 addition & 4 deletions source/funkin/backend/chart/ChartData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ typedef ChartMetaData = {
public var ?stepsPerBeat:Float;
public var ?needsVoices:Bool;
public var ?icon:String;
public var ?color:Dynamic;
public var ?color:FlxColor;
public var ?difficulties:Array<String>;
public var ?coopAllowed:Bool;
public var ?opponentModeAllowed:Bool;
public var ?customValues:Dynamic;

// NOT TO BE EXPORTED
public var ?parsedColor:FlxColor;
}

typedef ChartStrumLine = {
Expand Down
4 changes: 2 additions & 2 deletions source/funkin/editors/charter/Charter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1338,15 +1338,15 @@ class Charter extends UIState {
#if sys
CoolUtil.safeSaveFile(
'${Paths.getAssetsRoot()}/songs/${__song.toLowerCase()}/meta.json',
Json.stringify(PlayState.SONG.meta == null ? {} : PlayState.SONG.meta, null, "\t")
PlayState.SONG.meta == null ? null : Chart.makeMetaSaveable(PlayState.SONG.meta)
);
#else
_file_meta_saveas(_);
#end
}

function _file_meta_saveas(_) {
openSubState(new SaveSubstate(Json.stringify(PlayState.SONG.meta == null ? {} : PlayState.SONG.meta, null, "\t"), { // always pretty print meta
openSubState(new SaveSubstate(PlayState.SONG.meta == null ? null : Chart.makeMetaSaveable(PlayState.SONG.meta), { // always pretty print meta
defaultSaveFile: 'meta.json'
}));
}
Expand Down
5 changes: 2 additions & 3 deletions source/funkin/editors/charter/CharterMetaDataScreen.hx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CharterMetaDataScreen extends UISubstateWindow {
coopAllowedCheckbox = new UICheckbox(opponentModeCheckbox.x + 150 + 26, opponentModeCheckbox.y, "Co-op Mode", metadata.coopAllowed);
add(coopAllowedCheckbox);

colorWheel = new UIColorwheel(iconTextBox.x, coopAllowedCheckbox.y, metadata.parsedColor);
colorWheel = new UIColorwheel(iconTextBox.x, coopAllowedCheckbox.y, metadata.color);
add(colorWheel);
addLabelOn(colorWheel, "Color");

Expand Down Expand Up @@ -158,8 +158,7 @@ class CharterMetaDataScreen extends UISubstateWindow {
needsVoices: needsVoicesCheckbox.checked,
displayName: displayNameTextBox.label.text,
icon: iconTextBox.label.text,
color: colorWheel.curColorString,
parsedColor: colorWheel.curColor,
color: colorWheel.curColor,
opponentModeAllowed: opponentModeCheckbox.checked,
coopAllowed: coopAllowedCheckbox.checked,
difficulties: [for (diff in difficulitesTextBox.label.text.split(",")) diff.trim()],
Expand Down
19 changes: 10 additions & 9 deletions source/funkin/editors/charter/CharterSelection.hx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package funkin.editors.charter;

import funkin.backend.chart.ChartData;
import funkin.backend.chart.Chart;
import funkin.backend.chart.ChartData.ChartMetaData;
import haxe.Json;
import funkin.editors.charter.SongCreationScreen.SongCreationData;
import funkin.options.type.NewOption;
import funkin.backend.chart.ChartData;
import funkin.backend.system.framerate.Framerate;
import funkin.menus.FreeplayState.FreeplaySonglist;
import funkin.editors.EditorTreeMenu;
import funkin.editors.charter.SongCreationScreen.SongCreationData;
import funkin.menus.FreeplayState.FreeplaySonglist;
import funkin.options.*;
import funkin.options.type.*;
import funkin.options.type.NewOption;
import haxe.Json;

using StringTools;

Expand Down Expand Up @@ -38,7 +39,7 @@ class CharterSelection extends EditorTreeMenu {
FlxG.state.openSubState(new ChartCreationScreen(saveChart));
}));
optionsTree.add(new OptionsScreen(s.name, "Select a difficulty to continue.", list));
}, s.parsedColor.getDefault(0xFFFFFFFF))
}, s.color.getDefault(0xFFFFFFFF))
];

list.insert(0, new NewOption("New Song", "New Song", function() {
Expand Down Expand Up @@ -109,7 +110,7 @@ class CharterSelection extends EditorTreeMenu {
sys.FileSystem.createDirectory('$songFolder/charts');

// Save Files
CoolUtil.safeSaveFile('$songFolder/meta.json', Json.stringify(creation.meta, "\t"));
CoolUtil.safeSaveFile('$songFolder/meta.json', Chart.makeMetaSaveable(creation.meta));
if (creation.instBytes != null) sys.io.File.saveBytes('$songFolder/song/Inst.${Paths.SOUND_EXT}', creation.instBytes);
if (creation.voicesBytes != null) sys.io.File.saveBytes('$songFolder/song/Voices.${Paths.SOUND_EXT}', creation.voicesBytes);
#end
Expand All @@ -126,7 +127,7 @@ class CharterSelection extends EditorTreeMenu {
FlxG.state.openSubState(new ChartCreationScreen(saveChart));
}));
optionsTree.insert(1, new OptionsScreen(creation.meta.name, "Select a difficulty to continue.", list));
}, creation.meta.parsedColor.getDefault(0xFFFFFFFF));
}, creation.meta.color.getDefault(0xFFFFFFFF));

// Add to List
freeplayList.songs.insert(0, creation.meta);
Expand Down Expand Up @@ -160,7 +161,7 @@ class CharterSelection extends EditorTreeMenu {
var meta = Json.parse(sys.io.File.getContent('$songFolder/meta.json'));
if (meta.difficulties != null && !meta.difficulties.contains(name)) {
meta.difficulties.push(name);
CoolUtil.safeSaveFile('$songFolder/meta.json', Json.stringify(meta));
CoolUtil.safeSaveFile('$songFolder/meta.json', Chart.makeMetaSaveable(meta));
}
}
}
3 changes: 1 addition & 2 deletions source/funkin/editors/charter/SongCreationScreen.hx
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ class SongCreationScreen extends UISubstateWindow {
needsVoices: needsVoicesCheckbox.checked,
displayName: displayNameTextBox.label.text,
icon: iconTextBox.label.text,
color: colorWheel.curColorString,
parsedColor: colorWheel.curColor,
color: colorWheel.curColor,
opponentModeAllowed: opponentModeCheckbox.checked,
coopAllowed: coopAllowedCheckbox.checked,
difficulties: [for (diff in difficulitesTextBox.label.text.split(",")) diff.trim()],
Expand Down
4 changes: 2 additions & 2 deletions source/funkin/menus/FreeplayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class FreeplayState extends MusicBeatState
scoreText.x = coopText.x = scoreBG.x + 4;
diffText.x = Std.int(scoreBG.x + ((scoreBG.width - diffText.width) / 2));

interpColor.fpsLerpTo(songs[curSelected].parsedColor, 0.0625);
interpColor.fpsLerpTo(songs[curSelected].color, 0.0625);
bg.color = interpColor.color;

#if PRELOAD_ALL
Expand Down Expand Up @@ -456,4 +456,4 @@ class FreeplaySonglist {

return songList;
}
}
}