Skip to content

Commit 7a8f1f0

Browse files
Update FreeplayState.hx (#669)
* Update FreeplayState.hx * should be good * pretty print the color --------- Co-authored-by: ⍚~Nex <[email protected]>
1 parent bd79dd2 commit 7a8f1f0

File tree

7 files changed

+32
-34
lines changed

7 files changed

+32
-34
lines changed

source/funkin/backend/chart/Chart.hx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ class Chart {
9696
var fromMods:Bool = fromMods;
9797
for (path in [metaDiffPath, metaPath]) if (Assets.exists(path)) {
9898
fromMods = Paths.assetsTree.existsSpecific(path, "TEXT", MODS);
99-
try data = Json.parse(Assets.getText(path))
100-
catch(e) Logs.trace('Failed to load song metadata for ${songName} ($path): ${Std.string(e)}', ERROR);
99+
try {
100+
var tempData = Json.parse(Assets.getText(path));
101+
tempData.color = CoolUtil.getColorFromDynamic(tempData.color).getDefault(defaultColor);
102+
data = tempData;
103+
} catch(e) Logs.trace('Failed to load song metadata for ${songName} ($path): ${Std.string(e)}', ERROR);
101104
if (data != null) break;
102105
}
103106

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

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

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

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

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

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

279275
var sortedData:Dynamic = {};
280-
for(f in Reflect.fields(data)) {
276+
for (f in Reflect.fields(data)) {
281277
var v = Reflect.field(data, f);
282278
if (v != null)
283279
Reflect.setField(sortedData, f, v);
284280
}
285281
return sortedData;
286282
}
283+
284+
public static inline function makeMetaSaveable(meta:ChartMetaData, prettyPrint:Bool = true):String {
285+
var data:Dynamic = Reflect.copy(meta);
286+
if (data.color != null) data.color = new FlxColor(data.color).toWebString(); // dont even ask me - Nex
287+
return Json.stringify(data, null, prettyPrint ? "\t" : null);
288+
}
287289
}
288290

289291
typedef ChartSaveSettings = {

source/funkin/backend/chart/ChartData.hx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ typedef ChartMetaData = {
2222
public var ?stepsPerBeat:Float;
2323
public var ?needsVoices:Bool;
2424
public var ?icon:String;
25-
public var ?color:Dynamic;
25+
public var ?color:FlxColor;
2626
public var ?difficulties:Array<String>;
2727
public var ?coopAllowed:Bool;
2828
public var ?opponentModeAllowed:Bool;
2929
public var ?customValues:Dynamic;
30-
31-
// NOT TO BE EXPORTED
32-
public var ?parsedColor:FlxColor;
3330
}
3431

3532
typedef ChartStrumLine = {

source/funkin/editors/charter/Charter.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,15 +1338,15 @@ class Charter extends UIState {
13381338
#if sys
13391339
CoolUtil.safeSaveFile(
13401340
'${Paths.getAssetsRoot()}/songs/${__song.toLowerCase()}/meta.json',
1341-
Json.stringify(PlayState.SONG.meta == null ? {} : PlayState.SONG.meta, null, "\t")
1341+
PlayState.SONG.meta == null ? null : Chart.makeMetaSaveable(PlayState.SONG.meta)
13421342
);
13431343
#else
13441344
_file_meta_saveas(_);
13451345
#end
13461346
}
13471347

13481348
function _file_meta_saveas(_) {
1349-
openSubState(new SaveSubstate(Json.stringify(PlayState.SONG.meta == null ? {} : PlayState.SONG.meta, null, "\t"), { // always pretty print meta
1349+
openSubState(new SaveSubstate(PlayState.SONG.meta == null ? null : Chart.makeMetaSaveable(PlayState.SONG.meta), { // always pretty print meta
13501350
defaultSaveFile: 'meta.json'
13511351
}));
13521352
}

source/funkin/editors/charter/CharterMetaDataScreen.hx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CharterMetaDataScreen extends UISubstateWindow {
9191
coopAllowedCheckbox = new UICheckbox(opponentModeCheckbox.x + 150 + 26, opponentModeCheckbox.y, "Co-op Mode", metadata.coopAllowed);
9292
add(coopAllowedCheckbox);
9393

94-
colorWheel = new UIColorwheel(iconTextBox.x, coopAllowedCheckbox.y, metadata.parsedColor);
94+
colorWheel = new UIColorwheel(iconTextBox.x, coopAllowedCheckbox.y, metadata.color);
9595
add(colorWheel);
9696
addLabelOn(colorWheel, "Color");
9797

@@ -158,8 +158,7 @@ class CharterMetaDataScreen extends UISubstateWindow {
158158
needsVoices: needsVoicesCheckbox.checked,
159159
displayName: displayNameTextBox.label.text,
160160
icon: iconTextBox.label.text,
161-
color: colorWheel.curColorString,
162-
parsedColor: colorWheel.curColor,
161+
color: colorWheel.curColor,
163162
opponentModeAllowed: opponentModeCheckbox.checked,
164163
coopAllowed: coopAllowedCheckbox.checked,
165164
difficulties: [for (diff in difficulitesTextBox.label.text.split(",")) diff.trim()],

source/funkin/editors/charter/CharterSelection.hx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package funkin.editors.charter;
22

3-
import funkin.backend.chart.ChartData;
3+
import funkin.backend.chart.Chart;
44
import funkin.backend.chart.ChartData.ChartMetaData;
5-
import haxe.Json;
6-
import funkin.editors.charter.SongCreationScreen.SongCreationData;
7-
import funkin.options.type.NewOption;
5+
import funkin.backend.chart.ChartData;
86
import funkin.backend.system.framerate.Framerate;
9-
import funkin.menus.FreeplayState.FreeplaySonglist;
107
import funkin.editors.EditorTreeMenu;
8+
import funkin.editors.charter.SongCreationScreen.SongCreationData;
9+
import funkin.menus.FreeplayState.FreeplaySonglist;
1110
import funkin.options.*;
1211
import funkin.options.type.*;
12+
import funkin.options.type.NewOption;
13+
import haxe.Json;
1314

1415
using StringTools;
1516

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

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

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

131132
// Add to List
132133
freeplayList.songs.insert(0, creation.meta);
@@ -160,7 +161,7 @@ class CharterSelection extends EditorTreeMenu {
160161
var meta = Json.parse(sys.io.File.getContent('$songFolder/meta.json'));
161162
if (meta.difficulties != null && !meta.difficulties.contains(name)) {
162163
meta.difficulties.push(name);
163-
CoolUtil.safeSaveFile('$songFolder/meta.json', Json.stringify(meta));
164+
CoolUtil.safeSaveFile('$songFolder/meta.json', Chart.makeMetaSaveable(meta));
164165
}
165166
}
166167
}

source/funkin/editors/charter/SongCreationScreen.hx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ class SongCreationScreen extends UISubstateWindow {
254254
needsVoices: needsVoicesCheckbox.checked,
255255
displayName: displayNameTextBox.label.text,
256256
icon: iconTextBox.label.text,
257-
color: colorWheel.curColorString,
258-
parsedColor: colorWheel.curColor,
257+
color: colorWheel.curColor,
259258
opponentModeAllowed: opponentModeCheckbox.checked,
260259
coopAllowed: coopAllowedCheckbox.checked,
261260
difficulties: [for (diff in difficulitesTextBox.label.text.split(",")) diff.trim()],

source/funkin/menus/FreeplayState.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class FreeplayState extends MusicBeatState
234234
scoreText.x = coopText.x = scoreBG.x + 4;
235235
diffText.x = Std.int(scoreBG.x + ((scoreBG.width - diffText.width) / 2));
236236

237-
interpColor.fpsLerpTo(songs[curSelected].parsedColor, 0.0625);
237+
interpColor.fpsLerpTo(songs[curSelected].color, 0.0625);
238238
bg.color = interpColor.color;
239239

240240
#if PRELOAD_ALL
@@ -456,4 +456,4 @@ class FreeplaySonglist {
456456

457457
return songList;
458458
}
459-
}
459+
}

0 commit comments

Comments
 (0)