Skip to content

Commit

Permalink
json utils fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Jun 27, 2024
1 parent 906ce19 commit 4f48f62
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 36 deletions.
3 changes: 2 additions & 1 deletion source/funkin/objects/NotesGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class NotesGroup extends Group
if (initNoteData < 0) continue; // Negative notes arent supported

var noteData:Int = initNoteData % Conductor.NOTE_DATA_LENGTH;
var noteType:String = NoteUtil.getTypeName(songNote.type);
var noteType:String = NoteUtil.resolveType(songNote.type);
var mustPress:Bool = mustHit ? initNoteData < Conductor.NOTE_DATA_LENGTH : initNoteData >= Conductor.NOTE_DATA_LENGTH;
var targetStrum:NoteStrum = mustPress ? playerStrums.members[noteData] : opponentStrums.members[noteData];
var skin:String = NoteUtil.getTypeJson(noteType)?.skin ?? SkinUtil.curSkin;
Expand All @@ -271,6 +271,7 @@ class NotesGroup extends Group
unspawnNotes.push(note);

if (susLength > 0) {
// TODO: notetype skin sustains arent loading for some reason (?) fix dat
var sustain:Sustain = new Sustain(noteData, strumTime, susLength, skin, note);
sustain.noteSpeed = songSpeed;

Expand Down
6 changes: 3 additions & 3 deletions source/funkin/states/editors/chart/ChartPreview.hx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class ChartPreview extends FlxSpriteExt {
function getNoteColor(note:Array<Dynamic>):FlxColor {
final key = note[3] + "-" + note[1] % 4;
if (colorMap.exists(key)) return colorMap.get(key);
final _skin = NoteUtil.getTypeJson(NoteUtil.getTypeName(note[3])).skin;
final _colors = SkinUtil.getSkinData(_skin).noteData.noteColorArray;
final color = FlxColorFix.fromString(_colors[cast note[1]%4]);
final skin = NoteUtil.getTypeJson(NoteUtil.resolveType(note[3])).skin;
final colors = SkinUtil.getSkinData(skin).noteData.noteColorArray;
final color = FlxColorFix.fromString(colors[cast note[1] % 4]);
colorMap.set(key, color);
return color;
}
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/states/editors/chart/grid/ChartNoteGrid.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ChartNoteGrid extends ChartGridBase<ChartNote>
final noteData:Int = data[1];
final susLength:Float = data[2];

final noteType:String = NoteUtil.getTypeName(data[3]);
final noteType:String = NoteUtil.resolveType(data[3]);
final typeData:NoteTypeJson = NoteUtil.getTypeJson(noteType);

var pos:FlxPoint = FlxPoint.get(
Expand Down
31 changes: 22 additions & 9 deletions source/funkin/states/menus/StoryMenuState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class StoryMenuState extends MusicBeatState {
inline function getCurData() return storyWeeks[curWeek].data;
inline function getWeekChars() return getCurData().storyCharacters;

override function destroy():Void {
super.destroy();
MenuCharacter.cachedChars.clear();
}

override function create():Void
{
if (FlxG.sound.music == null)
Expand Down Expand Up @@ -58,17 +63,18 @@ class StoryMenuState extends MusicBeatState {
storyWeeks.push(week);

ModdingUtil.runFunctionMod(week.modFolder, () -> {
var item = new MenuItem(i, week.data.weekImage, !Highscore.getWeekUnlock(week.name));
var locked = !Highscore.getWeekUnlock(week.name);
var item = new MenuItem(i, week.data.weekImage, locked);
grpWeekText.add(item);
});
}
});

MenuCharacter.cachedChars.clear();
for (i in 0...3) {
var weekChar:MenuCharacter = new MenuCharacter((FlxG.width * 0.25) * (1 + i) - 150, 70, getWeekChars()[i]);
grpWeekCharacters.add(weekChar);
}

grpWeekCharacters.members[1].screenCenter(X);

difficultySelectors = new FlxGroup();
Expand Down Expand Up @@ -178,17 +184,24 @@ class StoryMenuState extends MusicBeatState {
var cachedChars:Array<String> = [];

function cacheChars() {
final startMod = ModdingUtil.curModFolder;
for (week in storyWeeks) {
if (WeekSetup.vanillaWeekList.contains(week)) continue;
var startMod = ModdingUtil.curModFolder;
var dummyChar = new MenuCharacter(0,0,"");
for (week in storyWeeks)
{
if (WeekSetup.vanillaWeekList.contains(week))
continue;

ModdingUtil.curModFolder = week.modFolder;
for (i in week.data.storyCharacters) {
if (cachedChars.contains(i)) continue; // Avoid duplicates
for (i in week.data.storyCharacters)
{
if (i.length <= 0 || cachedChars.contains(i))// Avoid duplicates
continue;

cachedChars.push(i);
var char = new MenuCharacter(0,0,i);
char = null;
dummyChar.setupChar(i);
}
}
dummyChar.destroy();
ModdingUtil.curModFolder = startMod;
}

Expand Down
9 changes: 5 additions & 4 deletions source/funkin/states/menus/items/MenuCharacter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ class MenuCharacter extends FlxSpriteExt

public static var cachedChars:Map<String, MenuCharJson> = [];

static function cacheChar(char:String):MenuCharJson
public static function cacheChar(char:String):MenuCharJson
{
if (cachedChars.exists(char))
return cachedChars.get(char);

var data:MenuCharJson = JsonUtil.checkJson(DEFAULT_MENU_CHAR, JsonUtil.getJson(char, charsFolder, "images"));
cachedChars.set(char, data);
return data;
Expand All @@ -51,9 +54,7 @@ class MenuCharacter extends FlxSpriteExt
{
lastChar = char;

var charJson:MenuCharJson = cachedChars.get(char);
if (charJson == null)
charJson = cacheChar(char);
var charJson:MenuCharJson = cacheChar(char);

lerpColor = charJson.lerpColor;
if (!lerpColor)
Expand Down
41 changes: 27 additions & 14 deletions source/funkin/util/JsonUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,49 @@ typedef JsonSpritesheet = {

class JsonUtil
{
public static function getSubFolderJsonList(folder:String= 'data/scripts/global', ?subFolders:Array<String>)
public static function getSubFolderJsonList(folder:String, ?subFolders:Array<String>)
{
var subFolderList:Array<String> = [];

if (subFolders != null) {
subFolders.fastForEach((subFolder, i) -> {
subFolderList = subFolderList.concat(getJsonList('$folder/$subFolder'));
subFolderList.merge(getJsonList('$folder/$subFolder'));
});
}

return getJsonList(folder).concat(subFolderList);
}

inline public static function getJsonList(folder:String = 'scripts/global',
assets:Bool = true, globalMod:Bool = true, curMod:Bool = true, allMods:Bool = false,
fullPath:Bool = false, mainFolder:String = 'data'):Array<String>
public static function getJsonList(?folder:String, ?assets:Bool, ?global:Bool, ?mod:Bool, ?all:Bool, ?fullPath:Bool, ?mainFolder:String):Array<String>
{
var assetsList:Array<String> = assets ? Paths.getFileList(TEXT, fullPath, 'json', '/$mainFolder/$folder') : [];
var modList:Array<String> = #if desktop Paths.getModFileList('$mainFolder/$folder', 'json', fullPath, globalMod, curMod, allMods); #else []; #end
return assetsList.concat(modList);
folder ??= 'scripts/global';
assets ??= true;
global ??= true; mod ??= true; all ??= false;
fullPath ??= false;
mainFolder ??= 'data';

if (!folder.startsWith('$mainFolder/'))
folder = '$mainFolder/$folder';

var list:Array<String> = [];

if (assets) {
list.merge(Paths.getFileList(TEXT, fullPath, 'json', '/$folder'));
}

#if MODS_ALLOWED
if (global || mod || all) {
list.merge(Paths.getModFileList(folder, 'json', fullPath, global, mod, all));
}
#end

return list;
}

public static function getJson(path:String, folder:String = '', library:String = 'data'):Dynamic
{
if (!getJsonList(folder, true, true, true, false, false, library).contains(path))
return null;

var getJson = CoolUtil.getFileContent(Paths.file('$library/$folder/$path.json', TEXT));
var returnJson:Dynamic = Json.parse(getJson);
return returnJson;
var rawJson = CoolUtil.getFileContent(Paths.file('$library/$folder/$path.json', TEXT));
return rawJson.length > 0 ? Json.parse(rawJson) : null;
}

inline public static function getAsepritePacker(graphic:FlxGraphic, sourceJson:String):FlxAtlasFrames {
Expand Down
7 changes: 3 additions & 4 deletions source/funkin/util/NoteUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class NoteUtil
public static var noteTypesMap:Map<String, NoteTypeJson> = [];
public static var noteTypesArray:Array<String> = [];

inline public static function getTypeName(type:Dynamic):Dynamic {
return (Std.isOfType(type, String)) ? type : noteTypesArray[type];
public static inline function resolveType(type:Dynamic):String {
return (type is String) ? type : (noteTypesArray[type] ?? "default");
}

static function getList():Array<String>
Expand All @@ -69,8 +69,7 @@ class NoteUtil
if (noteTypesMap.exists(type))
return noteTypesMap.get(type);

var typeJson:NoteTypeJson = JsonUtil.getJson(type, 'notetypes');
typeJson = JsonUtil.checkJson(DEFAULT_NOTE_TYPE, typeJson);
var typeJson:NoteTypeJson = JsonUtil.checkJson(DEFAULT_NOTE_TYPE, JsonUtil.getJson(type, 'notetypes'));
noteTypesMap.set(type, typeJson);
return typeJson;
}
Expand Down
10 changes: 10 additions & 0 deletions source/macros/FastArray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class FastArray<T>
#end
}

inline public static function merge<T>(array:Array<T>, input:Array<T>):Void {
var l:Int = input.length;
var i:Int = 0;

while (i < l) {
array.push(unsafeGet(input, i));
i++;
}
}

inline public static function clear<T>(array:Array<T>):Void {
#if web
array.splice(0, array.length); // Splice is faster on html5 for whatever reason
Expand Down

0 comments on commit 4f48f62

Please sign in to comment.