Skip to content

Commit

Permalink
migration is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
lemz1 committed Nov 1, 2024
1 parent 5ab7332 commit e0ab779
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 134 deletions.
35 changes: 34 additions & 1 deletion source/funkin/data/character/CharacterData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ typedef CharacterData =
* The type of rendering system to use for the character.
* @default sparrow
*/
@:optional
@:default('sparrow')
var renderType:CharacterRenderType;

/**
Expand All @@ -66,32 +68,42 @@ typedef CharacterData =
* Pro tip: On pixel-art levels, save the sprites small and set this value to 6 or so to save memory.
* @default 1
*/
@:optional
@:default(1.0)
var scale:Null<Float>;

/**
* Optional data about the health icon for the character.
*/
@:optional
var healthIcon:Null<HealthIconData>;

@:optional
var death:Null<DeathData>;

/**
* The global offset to the character's position, in pixels.
* @default [0, 0]
*/
@:optional
@:default([0, 0])
var offsets:Null<Array<Float>>;

/**
* The amount to offset the camera by while focusing on this character.
* Default value focuses on the character directly.
* @default [0, 0]
*/
@:optional
@:default([0, 0])
var cameraOffsets:Array<Float>;

/**
* Setting this to true disables anti-aliasing for the character.
* @default false
*/
@:optional
@:default(false)
var isPixel:Null<Bool>;

/**
Expand All @@ -111,8 +123,10 @@ typedef CharacterData =
*
* Examples:
* - Daddy Dearest uses a value of `1.525`.
* @default 1.0
* @default 8.0
*/
@:optional
@:default(8.0)
var singTime:Null<Float>;

/**
Expand All @@ -124,6 +138,8 @@ typedef CharacterData =
* If animations are used, this is the name of the animation to play first.
* @default idle
*/
@:optional
@:default('idle')
var startingAnimation:Null<String>;

/**
Expand All @@ -132,6 +148,8 @@ typedef CharacterData =
*
* @default false
*/
@:optional
@:default(false)
var flipX:Null<Bool>;
};

Expand All @@ -144,29 +162,38 @@ typedef HealthIconData =
* The ID to use for the health icon.
* @default The character's ID
*/
@:optional
var id:Null<String>;

/**
* The scale of the health icon.
*/
@:optional
@:default(1.0)
var scale:Null<Float>;

/**
* Whether to flip the health icon horizontally.
* @default false
*/
@:optional
@:default(false)
var flipX:Null<Bool>;

/**
* Multiply scale by 6 and disable antialiasing
* @default false
*/
@:optional
@:default(false)
var isPixel:Null<Bool>;

/**
* The offset of the health icon, in pixels.
* @default [0, 25]
*/
@:optional
@:default([0, 25])
var offsets:Null<Array<Float>>;
}

Expand All @@ -177,18 +204,24 @@ typedef DeathData =
* Default value focuses on the character's graphic midpoint.
* @default [0, 0]
*/
@:optional
@:default([0, 0])
var ?cameraOffsets:Array<Float>;

/**
* The amount to zoom the camera by while focusing on this character as they die.
* Value is a multiplier of the default camera zoom for the stage.
* @default 1.0
*/
@:optional
@:default(1.0)
var ?cameraZoom:Float;

/**
* Impose a delay between when the character reaches `0` health and when the death animation plays.
* @default 0.0
*/
@:optional
@:default(0.0)
var ?preTransitionDelay:Float;
}
72 changes: 62 additions & 10 deletions source/funkin/data/character/CharacterRegistry.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package funkin.data.character;

import funkin.data.animation.AnimationData;
import funkin.data.character.CharacterData;
import funkin.data.character.migrator.CharacterData_v1_0_0;
import funkin.modding.events.ScriptEvent;
import funkin.modding.events.ScriptEventDispatcher;
import funkin.play.character.AnimateAtlasCharacter;
Expand All @@ -19,6 +20,8 @@ import funkin.util.VersionUtil;
import haxe.Json;
import flixel.graphics.frames.FlxFrame;

using funkin.data.character.migrator.CharacterDataMigrator;

/**
* NOTE: This doesn't act the same as the other registries.
* It doesn't implement `BaseRegistry` and fetching produces a new instance rather than reusing them.
Expand Down Expand Up @@ -388,24 +391,24 @@ class CharacterRegistry
{
var rawJson:JsonFile = loadCharacterFile(charId);

var charData:CharacterData = buildCharacterData(rawJson, charId);
var version = parseVersion(rawJson);

if (charData == null)
if (version == null)
{
// trace('[CHARACTER] Could not load character data for "$charId", check above for potential errors');
return null;
}
if (CHARACTER_DATA_VERSION_RULE == null || VersionUtil.validateVersionStr(charData.version, CHARACTER_DATA_VERSION_RULE))

if (CHARACTER_DATA_VERSION_RULE == null || VersionUtil.validateVersionStr(version, CHARACTER_DATA_VERSION_RULE))
{
return buildCharacterData(rawJson, charId);
}
else if (VersionUtil.validateVersion(version, "1.0.x"))
{
return charData;
return buildCharacterData_v1_0_0(rawJson, charId);
}
// else if (VersionUtil.validateVersion(charData.version, "1.0.x"))
// {
// return migrateCharacterData_v1_0_0(charData, charId);
// }
else
{
trace('[CHARACTER] Could not load character data for "$charId": bad version (got ${charData.version}, expected ${CHARACTER_DATA_VERSION_RULE})');
trace('[CHARACTER] Could not load character data for "$charId": bad version (got ${version}, expected ${CHARACTER_DATA_VERSION_RULE})');
return null;
}
}
Expand All @@ -426,6 +429,28 @@ class CharacterRegistry
};
}

static function parseVersion(rawJson:JsonFile):Null<String>
{
var parser = new json2object.JsonParser<JsonVersionGet>();
parser.ignoreUnknownVariables = true;

switch (rawJson)
{
case {fileName: fileName, contents: contents}:
parser.fromJson(contents, fileName);
default:
return null;
}

if (parser.errors.length > 0)
{
trace(parser.errors, "NO VERSION");
return null;
}

return parser.value.version;
}

static function buildCharacterData(rawJson:JsonFile, charId:String):Null<CharacterData>
{
var parser = new json2object.JsonParser<CharacterData>();
Expand All @@ -448,6 +473,28 @@ class CharacterRegistry
return parser.value;
}

static function buildCharacterData_v1_0_0(rawJson:JsonFile, charId:String):Null<CharacterData>
{
var parser = new json2object.JsonParser<CharacterData_v1_0_0>();
parser.ignoreUnknownVariables = false;

switch (rawJson)
{
case {fileName: fileName, contents: contents}:
parser.fromJson(contents, fileName);
default:
return null;
}

if (parser.errors.length > 0)
{
trace(parser.errors, charId);
return null;
}

return parser.value.migrate();
}

/**
* The default time the character should sing for, in steps.
* Values that are too low will cause the character to stop singing between notes.
Expand Down Expand Up @@ -640,3 +687,8 @@ class CharacterRegistry
return input;
}
}

typedef JsonVersionGet =
{
var version:String;
}
18 changes: 14 additions & 4 deletions source/funkin/data/character/migrator/CharacterDataMigrator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ package funkin.data.character.migrator;
import funkin.data.character.CharacterData;
import funkin.data.character.CharacterRegistry;
import funkin.data.animation.AnimationData;
import funkin.data.character.migrator.CharacterData_v1_0_0;

class CharacterDataMigrator
{
public static inline function migrate(input:CharacterData_v1_0_0.CharacterData_v1_0_0):CharacterData
public static overload extern inline function migrate(input:CharacterData_v1_0_0):CharacterData
{
return migrate_CharacterData_v1_0_0(input);
}

public static function migrate_CharacterData_v1_0_0(input:CharacterData_v1_0_0.CharacterData_v1_0_0):CharacterData
public static function migrate_CharacterData_v1_0_0(input:CharacterData_v1_0_0):CharacterData
{
var assetPaths:Array<String> = [input.assetPath];
for (animation in input.animations)
{
if (animation.assetPath != null)
{
assetPaths.pushUnique(animation.assetPath);
}
}

return {
version: CharacterRegistry.CHARACTER_DATA_VERSION,
name: input.name,
renderType: input.renderType,
assetPaths: [input.assetPath],
assetPaths: assetPaths,
scale: input.scale,
healthIcon: input.healthIcon,
death: input.death,
Expand All @@ -32,7 +42,7 @@ class CharacterDataMigrator
};
}

static function migrate_AnimationData_v1_0_0(input:Array<CharacterData_v1_0_0.AnimationData_v1_0_0>):Array<AnimationData>
static function migrate_AnimationData_v1_0_0(input:Array<AnimationData_v1_0_0>):Array<AnimationData>
{
var animations:Array<AnimationData> = [];
for (animation in input)
Expand Down
Loading

0 comments on commit e0ab779

Please sign in to comment.