new super cool macro stuff whatev

This commit is contained in:
Yoshman29
2023-03-29 17:56:32 +02:00
parent b2a4baa11f
commit 484b989aed
5 changed files with 137 additions and 14 deletions
-1
View File
@@ -64,7 +64,6 @@ class EventMacro {
fields.push(funcField);
var p = new Printer();
switch(func.expr.expr) {
case EBlock(exprs):
// add a "set this" expr for each variable
+101
View File
@@ -0,0 +1,101 @@
package funkin.macros;
#if macro
/**
* Macro that automatically generates flush and load functions.
*/
class FunkinSaveMacro {
/**
* Generates flush and load functions.
* @param saveFieldName Name of the save field (`save`)
* @param saveFuncName Name of the save func (`flush`)
* @param loadFuncName Name of the load func (`load`)
* @return Array<Field>
*/
public static function build(saveFieldName:String = "save", saveFuncName:String = "flush", loadFuncName:String = "load"):Array<Field> {
var fields:Array<Field> = Context.getBuildFields();
var fieldNames:Array<String> = [];
for(field in fields) {
if (!field.access.contains(AStatic)) continue;
switch(field.kind) {
case FVar(type, expr):
if (field.name == saveFieldName) continue;
var valid:Bool = true;
if (field.meta != null)
for(m in field.meta)
if (m.name == ":doNotSave") {
valid = false;
break;
}
if (valid)
fieldNames.push(field.name);
default:
continue;
}
}
/**
* SAVE FUNCTION
*/
var saveFuncBlocks:Array<Expr> = [for(f in fieldNames)
macro ${{
pos: Context.currentPos(),
expr: EConst(CIdent(saveFieldName))
}}.data.$f = ${{
pos: Context.currentPos(),
expr: EConst(CIdent(f))
}}
];
saveFuncBlocks.push(macro ${{
pos: Context.currentPos(),
expr: EConst(CIdent(saveFieldName))
}}.flush());
fields.push({
pos: Context.currentPos(),
name: saveFuncName,
kind: FFun({
args: [],
expr: {
pos: Context.currentPos(),
expr: EBlock(saveFuncBlocks)
}
}),
access: [APublic, AStatic]
});
/**
* LOAD FUNCTION
*/
fields.push({
pos: Context.currentPos(),
name: loadFuncName,
kind: FFun({
args: [],
expr: {
pos: Context.currentPos(),
expr: EBlock([for(f in fieldNames)
macro if (${{
pos: Context.currentPos(),
expr: EConst(CIdent(saveFieldName))
}}.data.$f != null) ${{
pos: Context.currentPos(),
expr: EConst(CIdent(f))
}} = ${{
pos: Context.currentPos(),
expr: EConst(CIdent(saveFieldName))
}}.data.$f
])
}
}),
access: [APublic, AStatic]
});
return fields;
}
}
#end
+7 -12
View File
@@ -6,10 +6,13 @@ import flixel.util.FlxSave;
import flixel.input.keyboard.FlxKey;
@:build(funkin.macros.OptionsMacro.build())
@:build(funkin.macros.FunkinSaveMacro.build("__save", "__flush", "__load"))
class Options
{
@:dox(hide) public static var __save:FlxSave;
@:dox(hide) private static var __eventAdded = false;
@:dox(hide) @:doNotSave
public static var __save:FlxSave;
@:dox(hide) @:doNotSave
private static var __eventAdded = false;
/**
* SETTINGS
@@ -103,10 +106,7 @@ class Options
public static function load() {
if (__save == null) __save = new FlxSave();
__save.bind("options", "CodenameEngine");
for(field in Reflect.fields(__save.data)) {
var obj = Reflect.field(__save.data, field);
Reflect.setProperty(Options, field, obj);
}
__load();
if (!__eventAdded) {
Lib.application.onExit.add(function(i:Int) {
@@ -134,11 +134,6 @@ class Options
public static function save() {
volume = FlxG.sound.volume;
for(field in Type.getClassFields(Options)) {
var obj = Reflect.field(Options, field);
if (Reflect.isFunction(obj) || obj is FlxSave) continue;
Reflect.setField(__save.data, field, obj);
}
__save.flush();
__flush();
}
}
+28
View File
@@ -0,0 +1,28 @@
package funkin.saves;
import flixel.util.FlxSave;
/**
* Class used for saves WITHOUT going through the struggle of type checks
* Just add your save variables the way you would do in the Options.hx file.
* The macro will automatically generate the `flush` and `load` functions.
*/
@:build(funkin.macros.FunkinSaveMacro.build("save", "flush", "load"))
class FunkinSave {
// STUFF
#if REGION
public static var save(get, null):FlxSave;
private static inline function get_save()
return FlxG.save;
public static function init() {
FlxG.save.bind('save', 'CodenameEngine');
load();
}
#end
public static var totalPlayedGames:Int = 0;
public static var variableOne:String = "";
public static var coins:Int = 0;
public static var coolColor:String = "penis";
}
+1 -1
View File
@@ -187,7 +187,7 @@ class Main extends Sprite
Assets.registerLibrary('default', lib);
funkin.options.PlayerSettings.init();
FlxG.save.bind('save', 'CodenameEngine');
funkin.saves.FunkinSave.init();
Options.load();
Highscore.load();