2022-10-07 00:37:21 -04:00
|
|
|
package funkin.play.song;
|
|
|
|
|
2023-09-08 17:46:44 -04:00
|
|
|
import funkin.data.song.SongData.SongChartData;
|
|
|
|
import funkin.data.song.SongData.SongMetadata;
|
2022-10-07 00:37:21 -04:00
|
|
|
import funkin.util.SerializerUtil;
|
2023-09-25 23:24:07 -04:00
|
|
|
import funkin.util.FileUtil;
|
2022-10-07 00:37:21 -04:00
|
|
|
import lime.utils.Bytes;
|
|
|
|
import openfl.events.Event;
|
|
|
|
import openfl.events.IOErrorEvent;
|
|
|
|
import openfl.net.FileReference;
|
|
|
|
|
|
|
|
/**
|
2023-09-25 23:24:07 -04:00
|
|
|
* TODO: Refactor and remove this.
|
2022-10-07 00:37:21 -04:00
|
|
|
*/
|
|
|
|
class SongSerializer
|
|
|
|
{
|
2023-01-22 22:25:45 -05:00
|
|
|
/**
|
|
|
|
* Access a SongChartData JSON file from a specific path, then load it.
|
|
|
|
* @param path The file path to read from.
|
|
|
|
*/
|
|
|
|
public static function importSongChartDataSync(path:String):SongChartData
|
|
|
|
{
|
2023-09-25 23:24:07 -04:00
|
|
|
var fileData = FileUtil.readStringFromPath(path);
|
2023-01-22 22:25:45 -05:00
|
|
|
|
|
|
|
if (fileData == null) return null;
|
|
|
|
|
|
|
|
var songChartData:SongChartData = fileData.parseJSON();
|
|
|
|
|
|
|
|
return songChartData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access a SongMetadata JSON file from a specific path, then load it.
|
|
|
|
* @param path The file path to read from.
|
|
|
|
*/
|
|
|
|
public static function importSongMetadataSync(path:String):SongMetadata
|
|
|
|
{
|
2023-09-25 23:24:07 -04:00
|
|
|
var fileData = FileUtil.readStringFromPath(path);
|
2023-01-22 22:25:45 -05:00
|
|
|
|
|
|
|
if (fileData == null) return null;
|
|
|
|
|
|
|
|
var songMetadata:SongMetadata = fileData.parseJSON();
|
|
|
|
|
|
|
|
return songMetadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt the user to browse for a SongChartData JSON file path, then load it.
|
|
|
|
* @param callback The function to call when the file is loaded.
|
|
|
|
*/
|
|
|
|
public static function importSongChartDataAsync(callback:SongChartData->Void):Void
|
|
|
|
{
|
2023-09-25 23:24:07 -04:00
|
|
|
FileUtil.browseFileReference(function(fileReference:FileReference) {
|
2023-01-22 22:25:45 -05:00
|
|
|
var data = fileReference.data.toString();
|
|
|
|
|
|
|
|
if (data == null) return;
|
|
|
|
|
|
|
|
var songChartData:SongChartData = data.parseJSON();
|
|
|
|
|
|
|
|
if (songChartData != null) callback(songChartData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt the user to browse for a SongMetadata JSON file path, then load it.
|
|
|
|
* @param callback The function to call when the file is loaded.
|
|
|
|
*/
|
|
|
|
public static function importSongMetadataAsync(callback:SongMetadata->Void):Void
|
|
|
|
{
|
2023-09-25 23:24:07 -04:00
|
|
|
FileUtil.browseFileReference(function(fileReference:FileReference) {
|
2023-01-22 22:25:45 -05:00
|
|
|
var data = fileReference.data.toString();
|
|
|
|
|
|
|
|
if (data == null) return;
|
|
|
|
|
|
|
|
var songMetadata:SongMetadata = data.parseJSON();
|
|
|
|
|
|
|
|
if (songMetadata != null) callback(songMetadata);
|
|
|
|
});
|
|
|
|
}
|
2022-10-07 00:37:21 -04:00
|
|
|
}
|