mirror of
https://github.com/FunkinCrew/Funkin.git
synced 2024-11-27 10:05:41 -05:00
170 lines
5.3 KiB
Haxe
170 lines
5.3 KiB
Haxe
package funkin.data;
|
|
|
|
import funkin.data.song.importer.FNFLegacyData.LegacyNote;
|
|
import hxjsonast.Json;
|
|
import hxjsonast.Tools;
|
|
import hxjsonast.Json.JObjectField;
|
|
import haxe.ds.Either;
|
|
import funkin.data.song.importer.FNFLegacyData.LegacyNoteSection;
|
|
import funkin.data.song.importer.FNFLegacyData.LegacyNoteData;
|
|
import funkin.data.song.importer.FNFLegacyData.LegacyScrollSpeeds;
|
|
|
|
/**
|
|
* `json2object` has an annotation `@:jcustomparse` which allows for mutation of parsed values.
|
|
*
|
|
* It also allows for validation, since throwing an error in this function will cause the issue to be properly caught.
|
|
* Parsing will fail and `parser.errors` will contain the thrown exception.
|
|
*
|
|
* Functions must be of the signature `(hxjsonast.Json, String) -> T`, where the String is the property name and `T` is the type of the property.
|
|
*/
|
|
class DataParse
|
|
{
|
|
/**
|
|
* `@:jcustomparse(funkin.data.DataParse.stringNotEmpty)`
|
|
* @param json Contains the `pos` and `value` of the property.
|
|
* @param name The name of the property.
|
|
* @throws If the property is not a string or is empty.
|
|
*/
|
|
public static function stringNotEmpty(json:Json, name:String):String
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JString(s):
|
|
if (s == "") throw 'Expected property $name to be non-empty.';
|
|
return s;
|
|
default:
|
|
throw 'Expected property $name to be a string, but it was ${json.value}.';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parser which outputs a Dynamic value, either a object or something else.
|
|
* @param json
|
|
* @param name
|
|
* @return The value of the property.
|
|
*/
|
|
public static function dynamicValue(json:Json, name:String):Dynamic
|
|
{
|
|
return Tools.getValue(json);
|
|
}
|
|
|
|
/**
|
|
* Parser which outputs a `Either<Array<LegacyNoteSection>, LegacyNoteData>`.
|
|
* Used by the FNF legacy JSON importer.
|
|
*/
|
|
public static function eitherLegacyNoteData(json:Json, name:String):Either<Array<LegacyNoteSection>, LegacyNoteData>
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JArray(values):
|
|
return Either.Left(legacyNoteSectionArray(json, name));
|
|
case JObject(fields):
|
|
return Either.Right(cast Tools.getValue(json));
|
|
default:
|
|
throw 'Expected property $name to be note data, but it was ${json.value}.';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parser which outputs a `Either<Float, LegacyScrollSpeeds>`.
|
|
* Used by the FNF legacy JSON importer.
|
|
*/
|
|
public static function eitherLegacyScrollSpeeds(json:Json, name:String):Either<Float, LegacyScrollSpeeds>
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JNumber(f):
|
|
return Either.Left(Std.parseFloat(f));
|
|
case JObject(fields):
|
|
return Either.Right(cast Tools.getValue(json));
|
|
default:
|
|
throw 'Expected property $name to be scroll speeds, but it was ${json.value}.';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Array of JSON fields `[{key, value}, {key, value}]` to a Dynamic object `{key:value, key:value}`.
|
|
* @param fields
|
|
* @return Dynamic
|
|
*/
|
|
static function jsonFieldsToDynamicObject(fields:Array<JObjectField>):Dynamic
|
|
{
|
|
var result:Dynamic = {};
|
|
for (field in fields)
|
|
{
|
|
Reflect.setField(result, field.name, Tools.getValue(field.value));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Array of JSON elements `[Json, Json, Json]` to a Dynamic array `[String, Object, Int, Array]`
|
|
* @param jsons
|
|
* @return Array<Dynamic>
|
|
*/
|
|
static function jsonArrayToDynamicArray(jsons:Array<Json>):Array<Null<Dynamic>>
|
|
{
|
|
return [for (json in jsons) Tools.getValue(json)];
|
|
}
|
|
|
|
static function legacyNoteSectionArray(json:Json, name:String):Array<LegacyNoteSection>
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JArray(values):
|
|
return [for (value in values) legacyNoteSection(value, name)];
|
|
default:
|
|
throw 'Expected property to be an array, but it was ${json.value}.';
|
|
}
|
|
}
|
|
|
|
static function legacyNoteSection(json:Json, name:String):LegacyNoteSection
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JObject(fields):
|
|
return cast Tools.getValue(json);
|
|
default:
|
|
throw 'Expected property $name to be an object, but it was ${json.value}.';
|
|
}
|
|
}
|
|
|
|
public static function legacyNoteData(json:Json, name:String):LegacyNoteData
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JObject(fields):
|
|
return cast Tools.getValue(json);
|
|
default:
|
|
throw 'Expected property $name to be an object, but it was ${json.value}.';
|
|
}
|
|
}
|
|
|
|
public static function legacyNotes(json:Json, name:String):Array<LegacyNote>
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JArray(values):
|
|
return [for (value in values) legacyNote(value, name)];
|
|
default:
|
|
throw 'Expected property $name to be an array of notes, but it was ${json.value}.';
|
|
}
|
|
}
|
|
|
|
public static function legacyNote(json:Json, name:String):LegacyNote
|
|
{
|
|
switch (json.value)
|
|
{
|
|
case JArray(values):
|
|
// var time:Null<Float> = values[0] == null ? null : Tools.getValue(values[0]);
|
|
// var data:Null<Int> = values[1] == null ? null : Tools.getValue(values[1]);
|
|
// var length:Null<Float> = values[2] == null ? null : Tools.getValue(values[2]);
|
|
// var alt:Null<Bool> = values[3] == null ? null : Tools.getValue(values[3]);
|
|
|
|
// return new LegacyNote(time, data, length, alt);
|
|
return null;
|
|
default:
|
|
throw 'Expected property $name to be a note, but it was ${json.value}.';
|
|
}
|
|
}
|
|
}
|