From dcc58e9e33cc3ccb681d93131873bd6c210d9976 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 05:08:41 +0100 Subject: [PATCH] polymod aliases --- project.hxp | 3 ++ source/funkin/modding/PolymodHandler.hx | 6 ++- source/funkin/util/macro/PolymodMacro.hx | 51 +++++++++++++++++++++--- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/project.hxp b/project.hxp index 2ec8ba445..650488421 100644 --- a/project.hxp +++ b/project.hxp @@ -588,6 +588,9 @@ class Project extends HXProject { function configureCustomMacros() { // This macro allows addition of new functionality to existing Flixel. --> addHaxeMacro("addMetadata('@:build(funkin.util.macro.FlxMacro.buildFlxBasic())', 'flixel.FlxBasic')"); + + var abstracts:Array = ['funkin.*', 'flixel.util.FlxColor']; + addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])"); } function configureOutputDir() { diff --git a/source/funkin/modding/PolymodHandler.hx b/source/funkin/modding/PolymodHandler.hx index bc0f6a9ef..b5893160d 100644 --- a/source/funkin/modding/PolymodHandler.hx +++ b/source/funkin/modding/PolymodHandler.hx @@ -24,7 +24,6 @@ import polymod.Polymod; /** * A class for interacting with Polymod, the atomic modding framework for Haxe. */ -@:build(funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['funkin.*', 'flixel.util.FlxColor'])) class PolymodHandler { /** @@ -249,6 +248,11 @@ class PolymodHandler Polymod.addImportAlias('lime.utils.Assets', funkin.Assets); Polymod.addImportAlias('openfl.utils.Assets', funkin.Assets); + for (key => value in funkin.util.macro.PolymodMacro.aliases) + { + Polymod.addImportAlias(key, Type.resolveClass(value)); + } + // Add blacklisting for prohibited classes and packages. // `Sys` diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index bbe63e659..8eb8fb7c0 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -8,11 +8,24 @@ using StringTools; class PolymodMacro { - public static macro function buildPolymodAbstracts(abstractClasses:Array):Array - { - var fields:Array = Context.getBuildFields(); + public static var aliases(get, never):Map; + static function get_aliases():Map + { + // truly a sight to behold + return Reflect.callMethod(null, Reflect.field(Type.resolveClass('funkin.util.macro.AbstractAliases'), 'get'), []); + } + + public static macro function buildPolymodAbstracts(abstractClasses:Array):Void + { Context.onAfterTyping((types) -> { + if (alreadyCalled) + { + return; + } + + var aliases:Map = new Map(); + for (type in types) { switch (type) @@ -27,6 +40,7 @@ class PolymodMacro { continue; } + aliases.set('${cls.pack.join('.')}.${cls.name}', 'polymod.abstracts.${cls.pack.join('.')}.${cls.name}'); buildAbstract(cls); break; } @@ -34,12 +48,39 @@ class PolymodMacro // do nothing } } - }); - return fields; + Context.defineModule('funkin.util.macro.PolymodMacro', [ + { + pack: ['funkin', 'util', 'macro'], + name: 'AbstractAliases', + kind: TypeDefKind.TDClass(null, [], false, false, false), + fields: [ + { + name: 'get', + access: [Access.APublic, Access.AStatic], + kind: FieldType.FFun( + { + args: [], + ret: (macro :Map), + expr: macro + { + return $v{aliases}; + } + }), + pos: Context.currentPos() + } + ], + pos: Context.currentPos() + } + ]); + + // the callback is called twice, which this leads to issues + alreadyCalled = true; + }); } #if macro + static var alreadyCalled:Bool = false; static var skipFields:Array = []; static function buildAbstract(abstractCls:AbstractType):Void