2022-09-13 05:09:30 +00:00
|
|
|
package funkin.play.song;
|
|
|
|
|
|
|
|
import funkin.play.song.SongData.SongChartData;
|
|
|
|
import funkin.play.song.SongData.SongMetadata;
|
|
|
|
import funkin.play.song.SongData.SongPlayData;
|
|
|
|
import funkin.play.song.SongData.SongTimeChange;
|
|
|
|
import funkin.play.song.SongData.SongTimeFormat;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For SongMetadata and SongChartData objects,
|
|
|
|
* ensures mandatory fields are present and populates optional fields with default values.
|
|
|
|
*/
|
|
|
|
class SongValidator
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
public static final DEFAULT_SONGNAME:String = "Unknown";
|
|
|
|
public static final DEFAULT_ARTIST:String = "Unknown";
|
|
|
|
public static final DEFAULT_TIMEFORMAT:SongTimeFormat = SongTimeFormat.MILLISECONDS;
|
2023-08-10 19:34:40 +00:00
|
|
|
public static final DEFAULT_DIVISIONS:Null<Int> = null;
|
2023-05-25 22:33:39 +00:00
|
|
|
public static final DEFAULT_LOOPED:Bool = false;
|
2023-01-23 03:25:45 +00:00
|
|
|
public static final DEFAULT_STAGE:String = "mainStage";
|
|
|
|
public static final DEFAULT_SCROLLSPEED:Float = 1.0;
|
|
|
|
|
|
|
|
public static var DEFAULT_GENERATEDBY(get, null):String;
|
|
|
|
|
|
|
|
static function get_DEFAULT_GENERATEDBY():String
|
|
|
|
{
|
|
|
|
return '${Constants.TITLE} - ${Constants.VERSION}';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the fields of a SongMetadata object (excluding the version field).
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2023-01-23 03:25:45 +00:00
|
|
|
* @param input The SongMetadata object to validate.
|
|
|
|
* @param songId The ID of the song being validated. Only used for error messages.
|
|
|
|
* @return The validated SongMetadata object.
|
|
|
|
*/
|
|
|
|
public static function validateSongMetadata(input:SongMetadata, songId:String = 'unknown'):SongMetadata
|
|
|
|
{
|
|
|
|
if (input == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Could not parse metadata for song ${songId}');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input.songName == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Song ${songId} is missing a songName field. ');
|
|
|
|
input.songName = DEFAULT_SONGNAME;
|
|
|
|
}
|
|
|
|
if (input.artist == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Song ${songId} is missing an artist field. ');
|
|
|
|
input.artist = DEFAULT_ARTIST;
|
|
|
|
}
|
|
|
|
if (input.timeFormat == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Song ${songId} is missing a timeFormat field. ');
|
|
|
|
input.timeFormat = DEFAULT_TIMEFORMAT;
|
|
|
|
}
|
|
|
|
if (input.generatedBy == null)
|
|
|
|
{
|
|
|
|
input.generatedBy = DEFAULT_GENERATEDBY;
|
|
|
|
}
|
|
|
|
|
|
|
|
input.timeChanges = validateTimeChanges(input.timeChanges, songId);
|
2023-08-28 19:03:29 +00:00
|
|
|
if (input.timeChanges == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Song ${songId} is missing a timeChanges field. ');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
input.playData = validatePlayData(input.playData, songId);
|
|
|
|
|
2023-08-28 19:03:29 +00:00
|
|
|
if (input.variation == null) input.variation = '';
|
2023-01-23 03:25:45 +00:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the fields of a SongPlayData object.
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2023-01-23 03:25:45 +00:00
|
|
|
* @param input The SongPlayData object to validate.
|
|
|
|
* @param songId The ID of the song being validated. Only used for error messages.
|
|
|
|
* @return The validated SongPlayData object.
|
|
|
|
*/
|
|
|
|
public static function validatePlayData(input:SongPlayData, songId:String = 'unknown'):SongPlayData
|
|
|
|
{
|
2023-08-28 19:03:29 +00:00
|
|
|
if (input == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Could not parse metadata.playData for song ${songId}');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the fields of a TimeChange object.
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2023-01-23 03:25:45 +00:00
|
|
|
* @param input The TimeChange object to validate.
|
|
|
|
* @param songId The ID of the song being validated. Only used for error messages.
|
|
|
|
* @return The validated TimeChange object.
|
|
|
|
*/
|
|
|
|
public static function validateTimeChange(input:SongTimeChange, songId:String = 'unknown'):SongTimeChange
|
|
|
|
{
|
2023-08-28 19:03:29 +00:00
|
|
|
if (input == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Could not parse metadata.timeChange for song ${songId}');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates multiple TimeChange objects in an array.
|
|
|
|
*/
|
|
|
|
public static function validateTimeChanges(input:Array<SongTimeChange>, songId:String = 'unknown'):Array<SongTimeChange>
|
|
|
|
{
|
|
|
|
if (input == null)
|
|
|
|
{
|
2023-08-28 19:03:29 +00:00
|
|
|
trace('[SONGDATA] Could not parse metadata.timeChange for song ${songId}');
|
|
|
|
return null;
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
input = input.map((timeChange) -> validateTimeChange(timeChange, songId));
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the fields of a SongChartData object (excluding the version field).
|
2023-06-08 20:30:45 +00:00
|
|
|
*
|
2023-01-23 03:25:45 +00:00
|
|
|
* @param input The SongChartData object to validate.
|
|
|
|
* @param songId The ID of the song being validated. Only used for error messages.
|
|
|
|
* @return The validated SongChartData object.
|
|
|
|
*/
|
|
|
|
public static function validateSongChartData(input:SongChartData, songId:String = 'unknown'):SongChartData
|
|
|
|
{
|
|
|
|
if (input == null)
|
|
|
|
{
|
|
|
|
trace('[SONGDATA] Could not parse chart data for song ${songId}');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
2022-09-13 05:09:30 +00:00
|
|
|
}
|