From 484b989aed48b92f95a1a11bf7b9179e7674b706 Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Wed, 29 Mar 2023 17:56:32 +0200 Subject: [PATCH] new super cool macro stuff whatev --- source/funkin/macros/EventMacro.hx | 1 - source/funkin/macros/FunkinSaveMacro.hx | 101 ++++++++++++++++++++++++ source/funkin/options/Options.hx | 19 ++--- source/funkin/saves/FunkinSave.hx | 28 +++++++ source/funkin/system/Main.hx | 2 +- 5 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 source/funkin/macros/FunkinSaveMacro.hx create mode 100644 source/funkin/saves/FunkinSave.hx diff --git a/source/funkin/macros/EventMacro.hx b/source/funkin/macros/EventMacro.hx index 8d35de18..16dbcb50 100644 --- a/source/funkin/macros/EventMacro.hx +++ b/source/funkin/macros/EventMacro.hx @@ -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 diff --git a/source/funkin/macros/FunkinSaveMacro.hx b/source/funkin/macros/FunkinSaveMacro.hx new file mode 100644 index 00000000..9c27e10b --- /dev/null +++ b/source/funkin/macros/FunkinSaveMacro.hx @@ -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 + */ + public static function build(saveFieldName:String = "save", saveFuncName:String = "flush", loadFuncName:String = "load"):Array { + + var fields:Array = Context.getBuildFields(); + + var fieldNames:Array = []; + 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 = [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 \ No newline at end of file diff --git a/source/funkin/options/Options.hx b/source/funkin/options/Options.hx index ec8d8446..4448448e 100644 --- a/source/funkin/options/Options.hx +++ b/source/funkin/options/Options.hx @@ -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(); } } diff --git a/source/funkin/saves/FunkinSave.hx b/source/funkin/saves/FunkinSave.hx new file mode 100644 index 00000000..25370360 --- /dev/null +++ b/source/funkin/saves/FunkinSave.hx @@ -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"; +} \ No newline at end of file diff --git a/source/funkin/system/Main.hx b/source/funkin/system/Main.hx index 77f88a48..b9ece676 100644 --- a/source/funkin/system/Main.hx +++ b/source/funkin/system/Main.hx @@ -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();