polymod aliases

This commit is contained in:
lemz1 2024-10-27 05:08:41 +01:00 committed by lemz
parent 6d35d07245
commit dcc58e9e33
3 changed files with 54 additions and 6 deletions

View file

@ -588,6 +588,9 @@ class Project extends HXProject {
function configureCustomMacros() { function configureCustomMacros() {
// This macro allows addition of new functionality to existing Flixel. --> // This macro allows addition of new functionality to existing Flixel. -->
addHaxeMacro("addMetadata('@:build(funkin.util.macro.FlxMacro.buildFlxBasic())', 'flixel.FlxBasic')"); addHaxeMacro("addMetadata('@:build(funkin.util.macro.FlxMacro.buildFlxBasic())', 'flixel.FlxBasic')");
var abstracts:Array<String> = ['funkin.*', 'flixel.util.FlxColor'];
addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])");
} }
function configureOutputDir() { function configureOutputDir() {

View file

@ -24,7 +24,6 @@ import polymod.Polymod;
/** /**
* A class for interacting with Polymod, the atomic modding framework for Haxe. * A class for interacting with Polymod, the atomic modding framework for Haxe.
*/ */
@:build(funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['funkin.*', 'flixel.util.FlxColor']))
class PolymodHandler class PolymodHandler
{ {
/** /**
@ -249,6 +248,11 @@ class PolymodHandler
Polymod.addImportAlias('lime.utils.Assets', funkin.Assets); Polymod.addImportAlias('lime.utils.Assets', funkin.Assets);
Polymod.addImportAlias('openfl.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. // Add blacklisting for prohibited classes and packages.
// `Sys` // `Sys`

View file

@ -8,11 +8,24 @@ using StringTools;
class PolymodMacro class PolymodMacro
{ {
public static macro function buildPolymodAbstracts(abstractClasses:Array<String>):Array<Field> public static var aliases(get, never):Map<String, String>;
{
var fields:Array<Field> = Context.getBuildFields();
static function get_aliases():Map<String, String>
{
// 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<String>):Void
{
Context.onAfterTyping((types) -> { Context.onAfterTyping((types) -> {
if (alreadyCalled)
{
return;
}
var aliases:Map<String, String> = new Map<String, String>();
for (type in types) for (type in types)
{ {
switch (type) switch (type)
@ -27,6 +40,7 @@ class PolymodMacro
{ {
continue; continue;
} }
aliases.set('${cls.pack.join('.')}.${cls.name}', 'polymod.abstracts.${cls.pack.join('.')}.${cls.name}');
buildAbstract(cls); buildAbstract(cls);
break; break;
} }
@ -34,12 +48,39 @@ class PolymodMacro
// do nothing // 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<String, String>),
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 #if macro
static var alreadyCalled:Bool = false;
static var skipFields:Array<String> = []; static var skipFields:Array<String> = [];
static function buildAbstract(abstractCls:AbstractType):Void static function buildAbstract(abstractCls:AbstractType):Void