Funkin/source/funkin/ui/credits/CreditsDataHandler.hx

136 lines
3.3 KiB
Haxe
Raw Normal View History

2024-03-26 12:33:54 -04:00
package funkin.ui.credits;
import funkin.data.JsonFile;
using StringTools;
@:nullSafety
class CreditsDataHandler
{
public static final BACKER_PUBLIC_URL:String = 'https://funkin.me/backers';
#if HARDCODED_CREDITS
static final CREDITS_DATA_PATH:String = "assets/exclude/data/credits.json";
#else
static final CREDITS_DATA_PATH:String = "assets/data/credits.json";
#end
public static function debugPrint(data:Null<CreditsData>):Void
{
if (data == null)
{
trace('CreditsData(NULL)');
return;
}
2024-03-26 12:33:54 -04:00
if (data.entries == null || data.entries.length == 0)
{
trace('CreditsData(EMPTY)');
2024-03-26 12:33:54 -04:00
return;
}
2024-03-28 02:57:22 -04:00
var entryCount = data.entries.length;
var lineCount = 0;
for (entry in data.entries)
2024-03-26 12:33:54 -04:00
{
2024-03-28 02:57:22 -04:00
lineCount += entry?.body?.length ?? 0;
2024-03-26 12:33:54 -04:00
}
2024-03-28 02:57:22 -04:00
trace('CreditsData($entryCount entries containing $lineCount lines)');
2024-03-26 12:33:54 -04:00
}
/**
* If for some reason the full credits won't load,
* use this hardcoded data for the original Funkin' Crew.
*
* @return `CreditsData`
*/
public static inline function getFallback():CreditsData
{
return {
2024-03-28 02:57:22 -04:00
entries: [
2024-03-26 12:33:54 -04:00
{
2024-03-28 02:57:22 -04:00
header: 'Founders',
body: [
{line: 'ninjamuffin99'},
{line: 'PhantomArcade'},
{line: 'KawaiSprite'},
{line: 'evilsk8r'},
2024-03-26 12:33:54 -04:00
]
}
]
};
}
public static function fetchBackerEntries():Array<String>
{
2024-04-12 14:44:01 -04:00
// TODO: Implement a web request.
2024-03-26 12:33:54 -04:00
// We can't just grab the current Kickstarter data and include it in builds,
// because we don't want to deadname people who haven't logged into the portal yet.
// It can be async and paginated for performance!
2024-04-12 14:44:01 -04:00
return [];
2024-03-26 12:33:54 -04:00
}
#if HARDCODED_CREDITS
/**
* The data for the credits.
* Hardcoded into game via a macro at compile time.
*/
public static final CREDITS_DATA:Null<CreditsData> = #if macro null #else CreditsDataMacro.loadCreditsData() #end;
#else
/**
* The data for the credits.
* Loaded dynamically from the game folder when needed.
* Nullable because data may fail to parse.
*/
public static var CREDITS_DATA(get, default):Null<CreditsData> = null;
static function get_CREDITS_DATA():Null<CreditsData>
{
if (CREDITS_DATA == null) CREDITS_DATA = parseCreditsData(fetchCreditsData());
return CREDITS_DATA;
}
static function fetchCreditsData():funkin.data.JsonFile
{
var rawJson:String = openfl.Assets.getText(CREDITS_DATA_PATH).trim();
return {
fileName: CREDITS_DATA_PATH,
contents: rawJson
};
}
static function parseCreditsData(file:JsonFile):Null<CreditsData>
{
#if !macro
if (file.contents == null) return null;
var parser = new json2object.JsonParser<CreditsData>();
parser.ignoreUnknownVariables = false;
trace('[CREDITS] Parsing credits data from ${CREDITS_DATA_PATH}');
parser.fromJson(file.contents, file.fileName);
if (parser.errors.length > 0)
{
printErrors(parser.errors, file.fileName);
return null;
}
return parser.value;
#else
return null;
#end
}
static function printErrors(errors:Array<json2object.Error>, id:String = ''):Void
{
trace('[CREDITS] Failed to parse credits data: ${id}');
for (error in errors)
funkin.data.DataError.printError(error);
}
#end
}