From d105686251c8471cf690d2ef57f31f18b82faf4c Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sat, 26 Oct 2024 20:20:32 +0200 Subject: [PATCH 01/23] abstracts --- project.hxp | 1 + source/funkin/modding/PolymodHandler.hx | 20 ++- source/funkin/util/macro/PolymodMacro.hx | 174 +++++++++++++++++++++++ 3 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 source/funkin/util/macro/PolymodMacro.hx diff --git a/project.hxp b/project.hxp index 2ec8ba4458..12588302f4 100644 --- a/project.hxp +++ b/project.hxp @@ -588,6 +588,7 @@ 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')"); + addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts()"); } function configureOutputDir() { diff --git a/source/funkin/modding/PolymodHandler.hx b/source/funkin/modding/PolymodHandler.hx index eb0e77fc55..de951640f3 100644 --- a/source/funkin/modding/PolymodHandler.hx +++ b/source/funkin/modding/PolymodHandler.hx @@ -248,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.getAbstractAliases()) + { + Polymod.addImportAlias(key, Type.resolveClass(value)); + } + // Add blacklisting for prohibited classes and packages. // `Sys` @@ -333,8 +338,19 @@ class PolymodHandler { return { assetLibraryPaths: [ - 'default' => 'preload', 'shared' => 'shared', 'songs' => 'songs', 'videos' => 'videos', 'tutorial' => 'tutorial', 'week1' => 'week1', - 'week2' => 'week2', 'week3' => 'week3', 'week4' => 'week4', 'week5' => 'week5', 'week6' => 'week6', 'week7' => 'week7', 'weekend1' => 'weekend1', + 'default' => 'preload', + 'shared' => 'shared', + 'songs' => 'songs', + 'videos' => 'videos', + 'tutorial' => 'tutorial', + 'week1' => 'week1', + 'week2' => 'week2', + 'week3' => 'week3', + 'week4' => 'week4', + 'week5' => 'week5', + 'week6' => 'week6', + 'week7' => 'week7', + 'weekend1' => 'weekend1', ], coreAssetRedirect: CORE_FOLDER, } diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx new file mode 100644 index 0000000000..955fdb3e5a --- /dev/null +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -0,0 +1,174 @@ +package funkin.util.macro; + +import haxe.macro.Context; +import haxe.macro.Expr; +import haxe.macro.Type; + +using StringTools; + +class PolymodMacro +{ + public static macro function buildPolymodAbstracts():Void + { + Context.onAfterInitMacros(() -> { + var type = Context.getType('flixel.util.FlxColor'); + var abst = switch (type) + { + case Type.TAbstract(t, _): + t.get(); + default: + throw 'BRUH'; + } + if (abst.impl == null) + { + return; + } + var cls = abst.impl.get(); + var fields:Array = []; + for (field in cls.statics.get()) + { + if (field.name == '_new') + { + continue; + } + + var polymodField = createField(field); + + if (polymodField == null) + { + continue; + } + + fields.push(polymodField); + }; + + Context.defineType( + { + pos: Context.currentPos(), + pack: ['polymod', 'abstract'].concat(abst.pack), + name: abst.name, + kind: TypeDefKind.TDClass(null, [], false, false, false), + fields: fields, + }, null); + }); + } + + public static macro function getAbstractAliases():ExprOf> + { + var abstractAliases:Map = new Map(); + var abstractTypes = [Context.getType('flixel.util.FlxColor')]; + for (abstractType in abstractTypes) + { + var type = switch (abstractType) + { + case Type.TAbstract(t, _): + t.get(); + default: + throw 'BRUH'; + } + + abstractAliases.set('${type.pack.join('.')}.${type.name}', 'polymod.abstract.${type.pack.join('.')}.${type.name}'); + } + return macro $v{abstractAliases}; + } + + #if macro + static function createField(field:ClassField):Field + { + switch (field.type) + { + case Type.TLazy(f): + return _createField(field, f()); + default: + return _createField(field, field.type); + } + } + + static function _createField(field:ClassField, type:Type):Field + { + var access = null; + var kind = null; + + switch (type) + { + case Type.TFun(args, ret): + var fieldArgs = []; + var exprArgs:Array = []; + for (arg in args) + { + if (arg.name == 'this') + { + return null; + } + exprArgs.push(arg.name); + fieldArgs.push( + { + name: arg.name, + type: Context.toComplexType(arg.t), + opt: arg.opt, + }); + } + var fieldParams = []; + for (param in field.params) + { + fieldParams.push( + { + name: param.name, + defaultType: Context.toComplexType(param.defaultType), + }); + } + + var strExpr = Context.parse('flixel.util.FlxColor.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); + + access = [Access.AStatic].concat(getFieldAccess(field)); + kind = FieldType.FFun( + { + args: fieldArgs, + ret: Context.toComplexType(ret), + expr: macro + { + return ${strExpr}; + }, + params: fieldParams + }); + case Type.TAbstract(t, params): + access = [Access.AStatic].concat(getFieldAccess(field)); + kind = FieldType.FVar(Context.toComplexType(t.get().type), null); + default: + return null; + }; + + if (access == null || kind == null) + { + return null; + } + + return { + name: field.name, + doc: field.doc, + access: access, + kind: kind, + pos: Context.currentPos() + }; + } + + static function getFieldAccess(field:ClassField):Array + { + var access = []; + access.push(field.isPublic ? Access.APublic : Access.APrivate); + if (field.isFinal) + { + access.push(Access.AFinal); + } + if (field.isAbstract) + { + access.push(Access.AAbstract); + } + if (field.isExtern) + { + access.push(Access.AExtern); + } + return access; + } + #end +} From ad23a1c6d0d6dc14bbba56cb960567983430cc89 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sat, 26 Oct 2024 22:29:47 +0200 Subject: [PATCH 02/23] static member variables retrieved using getters --- source/funkin/util/macro/PolymodMacro.hx | 216 ++++++++++++++++------- 1 file changed, 150 insertions(+), 66 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 955fdb3e5a..b8675f49f5 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -12,51 +12,32 @@ class PolymodMacro { Context.onAfterInitMacros(() -> { var type = Context.getType('flixel.util.FlxColor'); - var abst = switch (type) + switch (type) { case Type.TAbstract(t, _): - t.get(); + buildAbstract(t.get()); default: throw 'BRUH'; } - if (abst.impl == null) - { - return; - } - var cls = abst.impl.get(); - var fields:Array = []; - for (field in cls.statics.get()) - { - if (field.name == '_new') - { - continue; - } - - var polymodField = createField(field); - if (polymodField == null) - { - continue; - } - - fields.push(polymodField); - }; - - Context.defineType( - { - pos: Context.currentPos(), - pack: ['polymod', 'abstract'].concat(abst.pack), - name: abst.name, - kind: TypeDefKind.TDClass(null, [], false, false, false), - fields: fields, - }, null); + // var type = Context.getType('funkin.Paths.PathsFunction'); + // switch (type) + // { + // case Type.TAbstract(t, _): + // buildAbstract(t.get()); + // default: + // throw 'BRUH'; + // } }); } public static macro function getAbstractAliases():ExprOf> { var abstractAliases:Map = new Map(); - var abstractTypes = [Context.getType('flixel.util.FlxColor')]; + var abstractTypes = [ + Context.getType('flixel.util.FlxColor'), + // Context.getType('funkin.Paths.PathsFunction') + ]; for (abstractType in abstractTypes) { var type = switch (abstractType) @@ -67,27 +48,93 @@ class PolymodMacro throw 'BRUH'; } - abstractAliases.set('${type.pack.join('.')}.${type.name}', 'polymod.abstract.${type.pack.join('.')}.${type.name}'); + abstractAliases.set('${type.pack.join('.')}.${type.name}', 'polymod.abstracts.${type.pack.join('.')}.${type.name}'); } return macro $v{abstractAliases}; } #if macro - static function createField(field:ClassField):Field + static var skipFields:Array = []; + + static function buildAbstract(abstractCls:AbstractType):Void { + if (abstractCls.impl == null) + { + return; + } + + skipFields = []; + + var cls = abstractCls.impl.get(); + + // we use the functions to check whether we need to skip some fields + // that is why we sort the fields, so that functions are handled first + var sortedFields:Array = sortFields(cls.statics.get()); + + var fields:Array = []; + for (field in sortedFields) + { + if (field.name == '_new') + { + continue; + } + + fields = fields.concat(createFields(abstractCls, field)); + }; + + Context.defineType( + { + pos: Context.currentPos(), + pack: ['polymod', 'abstracts'].concat(abstractCls.pack), + name: abstractCls.name, + kind: TypeDefKind.TDClass(null, [], false, false, false), + fields: fields, + }, null); + } + + static function sortFields(fields:Array):Array + { + var sortedFields:Array = []; + for (field in fields) + { + switch (field.type) + { + case Type.TLazy(f): + switch (f()) + { + case Type.TFun(_, _): + sortedFields.insert(0, field); + default: + sortedFields.push(field); + } + case Type.TFun(_, _): + sortedFields.insert(0, field); + default: + sortedFields.push(field); + } + } + return sortedFields; + } + + static function createFields(cls:AbstractType, field:ClassField):Array + { + if (skipFields.contains(field.name)) + { + return []; + } + switch (field.type) { case Type.TLazy(f): - return _createField(field, f()); + return _createFields(cls, field, f()); default: - return _createField(field, field.type); + return _createFields(cls, field, field.type); } } - static function _createField(field:ClassField, type:Type):Field + static function _createFields(cls:AbstractType, field:ClassField, type:Type):Array { - var access = null; - var kind = null; + var fields = []; switch (type) { @@ -98,7 +145,12 @@ class PolymodMacro { if (arg.name == 'this') { - return null; + var memberVariable:String = field.name.replace('get_', '').replace('set_', ''); + if (memberVariable != field.name) + { + skipFields.push(memberVariable); + } + return []; } exprArgs.push(arg.name); fieldArgs.push( @@ -118,38 +170,70 @@ class PolymodMacro }); } - var strExpr = Context.parse('flixel.util.FlxColor.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); + var strExpr = Context.parse('${cls.pack.join('.')}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); - access = [Access.AStatic].concat(getFieldAccess(field)); - kind = FieldType.FFun( + fields.push( { - args: fieldArgs, - ret: Context.toComplexType(ret), - expr: macro - { - return ${strExpr}; - }, - params: fieldParams + name: field.name, + doc: field.doc, + access: [Access.AStatic].concat(getFieldAccess(field)), + kind: FieldType.FFun( + { + args: fieldArgs, + ret: Context.toComplexType(ret), + expr: macro + { + return ${strExpr}; + }, + params: fieldParams + }), + pos: Context.currentPos() }); case Type.TAbstract(t, params): - access = [Access.AStatic].concat(getFieldAccess(field)); - kind = FieldType.FVar(Context.toComplexType(t.get().type), null); - default: - return null; - }; + fields.push( + { + name: field.name, + doc: field.doc, + access: [Access.AStatic].concat(getFieldAccess(field)), + kind: FieldType.FProp('get', 'never', Context.toComplexType(t.get().type), null), + pos: Context.currentPos() + }); - if (access == null || kind == null) - { - return null; - } + var fieldParams = []; + for (param in field.params) + { + fieldParams.push( + { + name: param.name, + defaultType: Context.toComplexType(param.defaultType), + }); + } + + var strExpr = Context.parse('${cls.pack.join('.')}.${cls.name}.${field.name}', Context.currentPos()); + + fields.push( + { + name: 'get_${field.name}', + doc: field.doc, + access: [Access.AStatic, field.isPublic ? Access.APublic : Access.APrivate], + kind: FieldType.FFun( + { + args: [], + ret: Context.toComplexType(t.get().type), + expr: macro + { + return ${strExpr}; + }, + params: fieldParams + }), + pos: Context.currentPos() + }); - return { - name: field.name, - doc: field.doc, - access: access, - kind: kind, - pos: Context.currentPos() + default: + return []; }; + + return fields; } static function getFieldAccess(field:ClassField):Array From a01a5907e7e4267aee9df9d8fe763a583691fb99 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sat, 26 Oct 2024 22:39:52 +0200 Subject: [PATCH 03/23] use module --- source/funkin/util/macro/PolymodMacro.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index b8675f49f5..aac812c1b9 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -170,7 +170,7 @@ class PolymodMacro }); } - var strExpr = Context.parse('${cls.pack.join('.')}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); + var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); fields.push( { @@ -209,7 +209,7 @@ class PolymodMacro }); } - var strExpr = Context.parse('${cls.pack.join('.')}.${cls.name}.${field.name}', Context.currentPos()); + var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}', Context.currentPos()); fields.push( { From 2d68f60c9b126456e59f7f38673afa437d3e1beb Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sat, 26 Oct 2024 22:42:54 +0200 Subject: [PATCH 04/23] Update PolymodMacro.hx --- source/funkin/util/macro/PolymodMacro.hx | 1 + 1 file changed, 1 insertion(+) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index aac812c1b9..076145a1e0 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -48,6 +48,7 @@ class PolymodMacro throw 'BRUH'; } + // should this use `type.module` insead of `type.pack`? abstractAliases.set('${type.pack.join('.')}.${type.name}', 'polymod.abstracts.${type.pack.join('.')}.${type.name}'); } return macro $v{abstractAliases}; From c00e4e8316987daf59c1adb37c7efe8a505142ad Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sat, 26 Oct 2024 23:36:04 +0200 Subject: [PATCH 05/23] forgot that TType exists as well --- source/funkin/util/macro/PolymodMacro.hx | 58 +++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 076145a1e0..20b0670be9 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -66,7 +66,7 @@ class PolymodMacro skipFields = []; - var cls = abstractCls.impl.get(); + var cls:ClassType = abstractCls.impl.get(); // we use the functions to check whether we need to skip some fields // that is why we sort the fields, so that functions are handled first @@ -135,7 +135,7 @@ class PolymodMacro static function _createFields(cls:AbstractType, field:ClassField, type:Type):Array { - var fields = []; + var fields:Array = []; switch (type) { @@ -229,7 +229,61 @@ class PolymodMacro }), pos: Context.currentPos() }); + case TType(t, params): + var actualType = switch (Context.toComplexType(t.get().type)) + { + case ComplexType.TPath(ct): + ct.params = []; + for (param in params) + { + switch (param) + { + case Type.TInst(p, _): + ct.params.push(TypeParam.TPType(ComplexType.TPath( + { + pack: p.get().pack, + name: p.get().name + }))); + + case Type.TAbstract(p, _): + ct.params.push(TypeParam.TPType(Context.toComplexType(p.get().type))); + default: + throw 'unhandled type'; + } + } + ComplexType.TPath(ct); + default: + Context.toComplexType(t.get().type); + } + + fields.push( + { + name: field.name, + doc: field.doc, + access: [Access.AStatic].concat(getFieldAccess(field)), + kind: FieldType.FProp('get', 'never', actualType, null), + pos: Context.currentPos() + }); + + var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}', Context.currentPos()); + fields.push( + { + name: 'get_${field.name}', + doc: field.doc, + access: [Access.AStatic, field.isPublic ? Access.APublic : Access.APrivate], + kind: FieldType.FFun( + { + args: [], + ret: actualType, + expr: macro + { + return ${strExpr}; + }, + params: [] + }), + pos: Context.currentPos() + }); default: return []; }; From 0e36e9261dea72c118cd28ba4d37b2ccfa6ce510 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 00:31:52 +0200 Subject: [PATCH 06/23] abstract without to and from should work now --- source/funkin/util/macro/PolymodMacro.hx | 27 +++++++++++++----------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 20b0670be9..b515e5b135 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -20,14 +20,14 @@ class PolymodMacro throw 'BRUH'; } - // var type = Context.getType('funkin.Paths.PathsFunction'); - // switch (type) - // { - // case Type.TAbstract(t, _): - // buildAbstract(t.get()); - // default: - // throw 'BRUH'; - // } + var type = Context.getType('funkin.Paths.PathsFunction'); + switch (type) + { + case Type.TAbstract(t, _): + buildAbstract(t.get()); + default: + throw 'BRUH'; + } }); } @@ -36,7 +36,7 @@ class PolymodMacro var abstractAliases:Map = new Map(); var abstractTypes = [ Context.getType('flixel.util.FlxColor'), - // Context.getType('funkin.Paths.PathsFunction') + Context.getType('funkin.Paths.PathsFunction') ]; for (abstractType in abstractTypes) { @@ -86,7 +86,7 @@ class PolymodMacro Context.defineType( { pos: Context.currentPos(), - pack: ['polymod', 'abstracts'].concat(abstractCls.pack), + pack: ['polymod', 'abstracts'].concat(abstractCls.module.split('.')), name: abstractCls.name, kind: TypeDefKind.TDClass(null, [], false, false, false), fields: fields, @@ -191,12 +191,15 @@ class PolymodMacro pos: Context.currentPos() }); case Type.TAbstract(t, params): + var actualType:ComplexType = cls.to.length != 0 ? Context.toComplexType(t.get() + .type) : Context.toComplexType(Context.getType('${cls.module}.${cls.name}')); + fields.push( { name: field.name, doc: field.doc, access: [Access.AStatic].concat(getFieldAccess(field)), - kind: FieldType.FProp('get', 'never', Context.toComplexType(t.get().type), null), + kind: FieldType.FProp('get', 'never', actualType, null), pos: Context.currentPos() }); @@ -220,7 +223,7 @@ class PolymodMacro kind: FieldType.FFun( { args: [], - ret: Context.toComplexType(t.get().type), + ret: actualType, expr: macro { return ${strExpr}; From b26b1bf4e81d9dc92cb05a0f3803373bc0eaeb73 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 01:28:00 +0200 Subject: [PATCH 07/23] TODO: build specified abstracts --- source/funkin/modding/PolymodHandler.hx | 5 --- source/funkin/util/macro/PolymodMacro.hx | 48 +++++------------------- 2 files changed, 9 insertions(+), 44 deletions(-) diff --git a/source/funkin/modding/PolymodHandler.hx b/source/funkin/modding/PolymodHandler.hx index de951640f3..8852d06d8b 100644 --- a/source/funkin/modding/PolymodHandler.hx +++ b/source/funkin/modding/PolymodHandler.hx @@ -248,11 +248,6 @@ class PolymodHandler Polymod.addImportAlias('lime.utils.Assets', funkin.Assets); Polymod.addImportAlias('openfl.utils.Assets', funkin.Assets); - for (key => value in funkin.util.macro.PolymodMacro.getAbstractAliases()) - { - 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 b515e5b135..02085f5533 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -10,50 +10,20 @@ class PolymodMacro { public static macro function buildPolymodAbstracts():Void { - Context.onAfterInitMacros(() -> { - var type = Context.getType('flixel.util.FlxColor'); - switch (type) + Context.onAfterTyping((types) -> { + for (type in types) { - case Type.TAbstract(t, _): - buildAbstract(t.get()); - default: - throw 'BRUH'; - } - - var type = Context.getType('funkin.Paths.PathsFunction'); - switch (type) - { - case Type.TAbstract(t, _): - buildAbstract(t.get()); - default: - throw 'BRUH'; + switch (type) + { + case ModuleType.TAbstract(a): + var cls = a.get(); + default: + // do nothing + } } }); } - public static macro function getAbstractAliases():ExprOf> - { - var abstractAliases:Map = new Map(); - var abstractTypes = [ - Context.getType('flixel.util.FlxColor'), - Context.getType('funkin.Paths.PathsFunction') - ]; - for (abstractType in abstractTypes) - { - var type = switch (abstractType) - { - case Type.TAbstract(t, _): - t.get(); - default: - throw 'BRUH'; - } - - // should this use `type.module` insead of `type.pack`? - abstractAliases.set('${type.pack.join('.')}.${type.name}', 'polymod.abstracts.${type.pack.join('.')}.${type.name}'); - } - return macro $v{abstractAliases}; - } - #if macro static var skipFields:Array = []; From c75112b9989df7612164bd294640384116c517dc Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 01:56:50 +0200 Subject: [PATCH 08/23] build macro --- project.hxp | 1 - source/funkin/modding/PolymodHandler.hx | 1 + source/funkin/util/macro/PolymodMacro.hx | 7 ++++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/project.hxp b/project.hxp index 12588302f4..2ec8ba4458 100644 --- a/project.hxp +++ b/project.hxp @@ -588,7 +588,6 @@ 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')"); - addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts()"); } function configureOutputDir() { diff --git a/source/funkin/modding/PolymodHandler.hx b/source/funkin/modding/PolymodHandler.hx index 8852d06d8b..007df38bf9 100644 --- a/source/funkin/modding/PolymodHandler.hx +++ b/source/funkin/modding/PolymodHandler.hx @@ -24,6 +24,7 @@ import polymod.Polymod; /** * A class for interacting with Polymod, the atomic modding framework for Haxe. */ +@:build(funkin.util.macro.PolymodMacro.buildPolymodAbstracts()) class PolymodHandler { /** diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 02085f5533..ef60c4172b 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -8,8 +8,10 @@ using StringTools; class PolymodMacro { - public static macro function buildPolymodAbstracts():Void + public static macro function buildPolymodAbstracts():Array { + var fields:Array = Context.getBuildFields(); + Context.onAfterTyping((types) -> { for (type in types) { @@ -17,11 +19,14 @@ class PolymodMacro { case ModuleType.TAbstract(a): var cls = a.get(); + trace(cls.name); default: // do nothing } } }); + + return fields; } #if macro From 807373c2d2fddf9e2f8faa7c8bf91eb85e8b3ab6 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 02:30:02 +0200 Subject: [PATCH 09/23] lol --- source/funkin/modding/PolymodHandler.hx | 2 +- source/funkin/util/macro/PolymodMacro.hx | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/source/funkin/modding/PolymodHandler.hx b/source/funkin/modding/PolymodHandler.hx index 007df38bf9..bc0f6a9ef2 100644 --- a/source/funkin/modding/PolymodHandler.hx +++ b/source/funkin/modding/PolymodHandler.hx @@ -24,7 +24,7 @@ import polymod.Polymod; /** * A class for interacting with Polymod, the atomic modding framework for Haxe. */ -@:build(funkin.util.macro.PolymodMacro.buildPolymodAbstracts()) +@:build(funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['funkin.*', 'flixel.util.FlxColor'])) class PolymodHandler { /** diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index ef60c4172b..0e7abbc113 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -8,7 +8,7 @@ using StringTools; class PolymodMacro { - public static macro function buildPolymodAbstracts():Array + public static macro function buildPolymodAbstracts(abstractClasses:Array):Array { var fields:Array = Context.getBuildFields(); @@ -19,7 +19,18 @@ class PolymodMacro { case ModuleType.TAbstract(a): var cls = a.get(); - trace(cls.name); + for (abstractCls in abstractClasses) + { + if (!cls.module.startsWith(abstractCls.replace('.*', '')) + && cls.module + cls.name != abstractCls + && cls.pack.join('.') + '.' + cls.name != abstractCls) + { + continue; + } + + trace(cls.module + '.' + cls.name); + break; + } default: // do nothing } From 6d35d07245c5c9048e3becfdbb4d565227682b99 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 02:59:02 +0100 Subject: [PATCH 10/23] Update PolymodMacro.hx --- source/funkin/util/macro/PolymodMacro.hx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 0e7abbc113..bbe63e659f 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -27,8 +27,7 @@ class PolymodMacro { continue; } - - trace(cls.module + '.' + cls.name); + buildAbstract(cls); break; } default: @@ -153,7 +152,7 @@ class PolymodMacro fieldParams.push( { name: param.name, - defaultType: Context.toComplexType(param.defaultType), + defaultType: param.defaultType != null ? Context.toComplexType(param.defaultType) : null, }); } @@ -170,6 +169,7 @@ class PolymodMacro ret: Context.toComplexType(ret), expr: macro { + @:privateAccess return ${strExpr}; }, params: fieldParams @@ -195,7 +195,7 @@ class PolymodMacro fieldParams.push( { name: param.name, - defaultType: Context.toComplexType(param.defaultType), + defaultType: param.defaultType != null ? Context.toComplexType(param.defaultType) : null, }); } @@ -212,6 +212,7 @@ class PolymodMacro ret: actualType, expr: macro { + @:privateAccess return ${strExpr}; }, params: fieldParams @@ -267,6 +268,7 @@ class PolymodMacro ret: actualType, expr: macro { + @:privateAccess return ${strExpr}; }, params: [] From dcc58e9e33cc3ccb681d93131873bd6c210d9976 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 05:08:41 +0100 Subject: [PATCH 11/23] polymod aliases --- project.hxp | 3 ++ source/funkin/modding/PolymodHandler.hx | 6 ++- source/funkin/util/macro/PolymodMacro.hx | 49 ++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/project.hxp b/project.hxp index 2ec8ba4458..6504884219 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 bc0f6a9ef2..b5893160da 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 bbe63e659f..8eb8fb7c01 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 + public static var aliases(get, never):Map; + + static function get_aliases():Map { - var fields:Array = Context.getBuildFields(); + // 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 From 07749285f17690fbbfa1ad36307b08cb45f07d6b Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 15:45:31 +0100 Subject: [PATCH 12/23] flxsignal works now i just need to make it work for all abstracts --- project.hxp | 2 +- source/funkin/util/macro/PolymodMacro.hx | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/project.hxp b/project.hxp index 6504884219..bca1ecd3b7 100644 --- a/project.hxp +++ b/project.hxp @@ -589,7 +589,7 @@ class Project extends HXProject { // 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']; + var abstracts:Array = [/*'funkin.*', 'flixel.util.FlxColor', */'flixel.util.FlxTypedSignal']; addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])"); } diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 8eb8fb7c01..9e02035024 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -103,6 +103,7 @@ class PolymodMacro { if (field.name == '_new') { + fields.push(buildCreateField(abstractCls, field)); continue; } @@ -143,6 +144,26 @@ class PolymodMacro return sortedFields; } + static function buildCreateField(cls:AbstractType, field:ClassField):Field + { + var newExpr = Context.parse('new ${cls.module}.${cls.name}Void>()', Context.currentPos()); + + return { + name: 'create', + access: [Access.APublic, Access.AStatic], + kind: FieldType.FFun( + { + args: [], + ret: (macro :Dynamic), + expr: macro + { + return ${newExpr}; + }, + }), + pos: Context.currentPos() + }; + } + static function createFields(cls:AbstractType, field:ClassField):Array { if (skipFields.contains(field.name)) @@ -161,6 +182,11 @@ class PolymodMacro static function _createFields(cls:AbstractType, field:ClassField, type:Type):Array { + if (field.meta.has(':to')) + { + return []; + } + var fields:Array = []; switch (type) From 918292d5a8f561302e367fe52647dbb1f3272ba9 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 19:30:37 +0100 Subject: [PATCH 13/23] create abstracts for all specified modules --- project.hxp | 2 +- source/funkin/util/macro/PolymodMacro.hx | 69 ++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/project.hxp b/project.hxp index bca1ecd3b7..84ef4db097 100644 --- a/project.hxp +++ b/project.hxp @@ -589,7 +589,7 @@ class Project extends HXProject { // 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', */'flixel.util.FlxTypedSignal']; + var abstracts:Array = ['funkin.*', 'flixel.util.FlxColor', 'flixel.util.FlxTypedSignal']; addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])"); } diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 9e02035024..5f1c0230a4 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -4,6 +4,7 @@ import haxe.macro.Context; import haxe.macro.Expr; import haxe.macro.Type; +using haxe.macro.ExprTools; using StringTools; class PolymodMacro @@ -146,24 +147,86 @@ class PolymodMacro static function buildCreateField(cls:AbstractType, field:ClassField):Field { - var newExpr = Context.parse('new ${cls.module}.${cls.name}Void>()', Context.currentPos()); + var newExprStr = trialAndError(cls, field) ?? 'new ${cls.module}.${cls.name}'; + + var funcArgs = []; + var funcArgNames:Array = []; + switch (field.type) + { + case Type.TFun(args, _): + for (arg in args) + { + funcArgs.push( + { + name: arg.name, + type: (macro :Dynamic), + opt: arg.opt + }); + funcArgNames.push(arg.name); + } + default: + throw 'how is this not a function'; + } return { name: 'create', access: [Access.APublic, Access.AStatic], kind: FieldType.FFun( { - args: [], + args: funcArgs, ret: (macro :Dynamic), expr: macro { - return ${newExpr}; + return ${Context.parse(newExprStr + '(' + funcArgNames.join(', ') + ')', Context.currentPos())}; }, }), pos: Context.currentPos() }; } + static function trialAndError(cls:AbstractType, field:ClassField):Null + { + if (cls.params.length <= 0) + { + return null; + } + + function getCombinations(num:Int):Array> + { + if (num == 0) + { + return [[]]; + } + + var combinations:Array> = []; + for (combination in getCombinations(num - 1)) + { + // combinations.push(combination.concat(['Dynamic'])); + combinations.push(combination.concat(['Dynamic->Void'])); + } + + return combinations; + } + var typeArgss:Array> = getCombinations(cls.params.length); + + for (typeArgs in typeArgss) + { + // TODO: figure out a way to find out whether a typeparameter is valid or not + try + { + // var expr = Context.parse('new ${cls.module}.${cls.name}<' + typeArgs.join(', ') + '>()', Context.currentPos()); + // return expr; + return 'new ${cls.module}.${cls.name} <' + typeArgs.join(', ') + '>'; + } + catch (e) + { + trace(e); + } + } + + return null; + } + static function createFields(cls:AbstractType, field:ClassField):Array { if (skipFields.contains(field.name)) From f3b4cac92da3827b0b709aac81b41cb2d579a986 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 20:09:48 +0100 Subject: [PATCH 14/23] make things dynamic (this is because typeparameters cause problems) --- source/funkin/util/macro/PolymodMacro.hx | 35 ++++++------------------ 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 5f1c0230a4..7c61c1ada0 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -272,19 +272,10 @@ class PolymodMacro fieldArgs.push( { name: arg.name, - type: Context.toComplexType(arg.t), + type: (macro :Dynamic), opt: arg.opt, }); } - var fieldParams = []; - for (param in field.params) - { - fieldParams.push( - { - name: param.name, - defaultType: param.defaultType != null ? Context.toComplexType(param.defaultType) : null, - }); - } var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); @@ -296,13 +287,13 @@ class PolymodMacro kind: FieldType.FFun( { args: fieldArgs, - ret: Context.toComplexType(ret), + ret: (macro :Dynamic), expr: macro { @:privateAccess return ${strExpr}; }, - params: fieldParams + params: [] }), pos: Context.currentPos() }); @@ -315,20 +306,10 @@ class PolymodMacro name: field.name, doc: field.doc, access: [Access.AStatic].concat(getFieldAccess(field)), - kind: FieldType.FProp('get', 'never', actualType, null), + kind: FieldType.FProp('get', 'never', (macro :Dynamic), null), pos: Context.currentPos() }); - var fieldParams = []; - for (param in field.params) - { - fieldParams.push( - { - name: param.name, - defaultType: param.defaultType != null ? Context.toComplexType(param.defaultType) : null, - }); - } - var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}', Context.currentPos()); fields.push( @@ -339,13 +320,13 @@ class PolymodMacro kind: FieldType.FFun( { args: [], - ret: actualType, + ret: (macro :Dynamic), expr: macro { @:privateAccess return ${strExpr}; }, - params: fieldParams + params: [] }), pos: Context.currentPos() }); @@ -381,7 +362,7 @@ class PolymodMacro name: field.name, doc: field.doc, access: [Access.AStatic].concat(getFieldAccess(field)), - kind: FieldType.FProp('get', 'never', actualType, null), + kind: FieldType.FProp('get', 'never', (macro :Dynamic), null), pos: Context.currentPos() }); @@ -395,7 +376,7 @@ class PolymodMacro kind: FieldType.FFun( { args: [], - ret: actualType, + ret: (macro :Dynamic), expr: macro { @:privateAccess From 374f6837eba45e96ed0dc2d5ca2071798554ebce Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 20:20:55 +0100 Subject: [PATCH 15/23] remove this useless vars --- source/funkin/util/macro/PolymodMacro.hx | 29 ------------------------ 1 file changed, 29 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 7c61c1ada0..ec12f19a5e 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -298,9 +298,6 @@ class PolymodMacro pos: Context.currentPos() }); case Type.TAbstract(t, params): - var actualType:ComplexType = cls.to.length != 0 ? Context.toComplexType(t.get() - .type) : Context.toComplexType(Context.getType('${cls.module}.${cls.name}')); - fields.push( { name: field.name, @@ -331,32 +328,6 @@ class PolymodMacro pos: Context.currentPos() }); case TType(t, params): - var actualType = switch (Context.toComplexType(t.get().type)) - { - case ComplexType.TPath(ct): - ct.params = []; - for (param in params) - { - switch (param) - { - case Type.TInst(p, _): - ct.params.push(TypeParam.TPType(ComplexType.TPath( - { - pack: p.get().pack, - name: p.get().name - }))); - - case Type.TAbstract(p, _): - ct.params.push(TypeParam.TPType(Context.toComplexType(p.get().type))); - default: - throw 'unhandled type'; - } - } - ComplexType.TPath(ct); - default: - Context.toComplexType(t.get().type); - } - fields.push( { name: field.name, From 42d93c616a0993e93a80c520d247609f7cfd4514 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Sun, 27 Oct 2024 22:58:22 +0100 Subject: [PATCH 16/23] lets make flxsignal an edge-case and use dynamic normally --- source/funkin/util/macro/PolymodMacro.hx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index ec12f19a5e..fccb5aa932 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -186,6 +186,11 @@ class PolymodMacro static function trialAndError(cls:AbstractType, field:ClassField):Null { + if ('${cls.module}.${cls.name}' == 'flixel.util.FlxSignal.FlxTypedSignal') + { + return 'new flixel.util.FlxSignal.FlxTypedSignalVoid>'; + } + if (cls.params.length <= 0) { return null; @@ -201,8 +206,8 @@ class PolymodMacro var combinations:Array> = []; for (combination in getCombinations(num - 1)) { - // combinations.push(combination.concat(['Dynamic'])); - combinations.push(combination.concat(['Dynamic->Void'])); + combinations.push(combination.concat(['Dynamic'])); + // combinations.push(combination.concat(['Dynamic->Void'])); } return combinations; From ba72fee6092457ce555fa7595075ec89dc44f561 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Mon, 28 Oct 2024 00:31:43 +0100 Subject: [PATCH 17/23] basically the most important abstracts --- project.hxp | 8 +++++- source/funkin/util/macro/PolymodMacro.hx | 35 +++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/project.hxp b/project.hxp index 84ef4db097..a354bc39ac 100644 --- a/project.hxp +++ b/project.hxp @@ -589,7 +589,13 @@ class Project extends HXProject { // 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', 'flixel.util.FlxTypedSignal']; + var abstracts:Array = [ + 'funkin.*', + 'flixel.*', + 'haxe.ui.*', + '!flixel.graphics.frames.bmfont.BMFont', + '!flixel.addons.util.FlxSimplex' + ]; addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])"); } diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index fccb5aa932..da567b2aa6 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -25,6 +25,19 @@ class PolymodMacro return; } + var sortedAbstractClasses:Array = []; + for (abstractCls in abstractClasses) + { + if (abstractCls.startsWith('!')) + { + sortedAbstractClasses.insert(0, abstractCls); + } + else + { + sortedAbstractClasses.push(abstractCls); + } + } + var aliases:Map = new Map(); for (type in types) @@ -33,14 +46,27 @@ class PolymodMacro { case ModuleType.TAbstract(a): var cls = a.get(); - for (abstractCls in abstractClasses) + + for (abstractCls in sortedAbstractClasses) { - if (!cls.module.startsWith(abstractCls.replace('.*', '')) - && cls.module + cls.name != abstractCls - && cls.pack.join('.') + '.' + cls.name != abstractCls) + var negate:Bool = abstractCls.startsWith('!'); + var name:String = abstractCls.replace('!', '').replace('.*', ''); + if (!negate && !cls.module.startsWith(name) && cls.module + cls.name != name && cls.pack.join('.') + '.' + cls.name != name) { continue; } + else if (negate) + { + if (cls.module.startsWith(name) || cls.module + cls.name == name || cls.pack.join('.') + '.' + cls.name == name) + { + break; + } + else + { + continue; + } + } + aliases.set('${cls.pack.join('.')}.${cls.name}', 'polymod.abstracts.${cls.pack.join('.')}.${cls.name}'); buildAbstract(cls); break; @@ -177,6 +203,7 @@ class PolymodMacro ret: (macro :Dynamic), expr: macro { + @:privateAccess return ${Context.parse(newExprStr + '(' + funcArgNames.join(', ') + ')', Context.currentPos())}; }, }), From ee63e8d8d6e4fee32ca19ac6af29d84e7def97c1 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Mon, 28 Oct 2024 22:18:07 +0100 Subject: [PATCH 18/23] check for private abstracts --- project.hxp | 4 +--- source/funkin/util/macro/PolymodMacro.hx | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/project.hxp b/project.hxp index a354bc39ac..48fde12bb1 100644 --- a/project.hxp +++ b/project.hxp @@ -592,9 +592,7 @@ class Project extends HXProject { var abstracts:Array = [ 'funkin.*', 'flixel.*', - 'haxe.ui.*', - '!flixel.graphics.frames.bmfont.BMFont', - '!flixel.addons.util.FlxSimplex' + 'haxe.ui.*' ]; addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])"); } diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index da567b2aa6..c7a7a250b6 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -46,6 +46,10 @@ class PolymodMacro { case ModuleType.TAbstract(a): var cls = a.get(); + if (cls.isPrivate) + { + continue; + } for (abstractCls in sortedAbstractClasses) { From d27433318d16725645ba64b4a760fdc5f0173b57 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Tue, 29 Oct 2024 00:05:00 +0100 Subject: [PATCH 19/23] Update PolymodMacro.hx --- source/funkin/util/macro/PolymodMacro.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index c7a7a250b6..dd02c44b4b 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -144,7 +144,7 @@ class PolymodMacro Context.defineType( { pos: Context.currentPos(), - pack: ['polymod', 'abstracts'].concat(abstractCls.module.split('.')), + pack: ['polymod', 'abstracts'].concat(abstractCls.pack), name: abstractCls.name, kind: TypeDefKind.TDClass(null, [], false, false, false), fields: fields, From ba1dbb5f68eb461bf629d80873ca69146a7de2ad Mon Sep 17 00:00:00 2001 From: lemz1 Date: Tue, 29 Oct 2024 00:32:42 +0100 Subject: [PATCH 20/23] all haxe abstracts --- project.hxp | 2 +- source/funkin/util/macro/PolymodMacro.hx | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/project.hxp b/project.hxp index 48fde12bb1..c3982863a4 100644 --- a/project.hxp +++ b/project.hxp @@ -592,7 +592,7 @@ class Project extends HXProject { var abstracts:Array = [ 'funkin.*', 'flixel.*', - 'haxe.ui.*' + 'haxe.*' ]; addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])"); } diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index dd02c44b4b..dc004b9a29 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -313,7 +313,20 @@ class PolymodMacro }); } - var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); + var returnStr:String = 'return '; + var returnType:ComplexType = (macro :Dynamic); + switch (ret) + { + case Type.TAbstract(t, _): + if (t.get().name == 'Void') + { + returnStr = ''; + returnType = (macro :Void); + } + default: + } + + var strExpr = Context.parse('${returnStr}${cls.module}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); fields.push( { @@ -323,11 +336,11 @@ class PolymodMacro kind: FieldType.FFun( { args: fieldArgs, - ret: (macro :Dynamic), + ret: returnType, expr: macro { @:privateAccess - return ${strExpr}; + ${strExpr}; }, params: [] }), From 907f300267c69ec18b52de53f889cf632d1bc299 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Wed, 30 Oct 2024 00:04:42 +0100 Subject: [PATCH 21/23] a bit nicer syntax --- source/funkin/util/macro/PolymodMacro.hx | 102 +++++++---------------- 1 file changed, 32 insertions(+), 70 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index dc004b9a29..98c0f6ea18 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -55,13 +55,13 @@ class PolymodMacro { var negate:Bool = abstractCls.startsWith('!'); var name:String = abstractCls.replace('!', '').replace('.*', ''); - if (!negate && !cls.module.startsWith(name) && cls.module + cls.name != name && cls.pack.join('.') + '.' + cls.name != name) + if (!negate && !cls.module.startsWith(name) && moduleTypePath(cls) != name && packTypePath(cls) != name) { continue; } else if (negate) { - if (cls.module.startsWith(name) || cls.module + cls.name == name || cls.pack.join('.') + '.' + cls.name == name) + if (cls.module.startsWith(name) || moduleTypePath(cls) == name || packTypePath(cls) == name) { break; } @@ -71,7 +71,7 @@ class PolymodMacro } } - aliases.set('${cls.pack.join('.')}.${cls.name}', 'polymod.abstracts.${cls.pack.join('.')}.${cls.name}'); + aliases.set('${packTypePath(cls)}', 'polymod.abstracts.${packTypePath(cls)}'); buildAbstract(cls); break; } @@ -177,8 +177,6 @@ class PolymodMacro static function buildCreateField(cls:AbstractType, field:ClassField):Field { - var newExprStr = trialAndError(cls, field) ?? 'new ${cls.module}.${cls.name}'; - var funcArgs = []; var funcArgNames:Array = []; switch (field.type) @@ -198,6 +196,8 @@ class PolymodMacro throw 'how is this not a function'; } + var expr:String = '${newExpr(cls, field)}(${funcArgNames.join(', ')})'; + return { name: 'create', access: [Access.APublic, Access.AStatic], @@ -208,59 +208,26 @@ class PolymodMacro expr: macro { @:privateAccess - return ${Context.parse(newExprStr + '(' + funcArgNames.join(', ') + ')', Context.currentPos())}; + return ${Context.parse(expr, Context.currentPos())}; }, }), pos: Context.currentPos() }; } - static function trialAndError(cls:AbstractType, field:ClassField):Null + static function newExpr(cls:AbstractType, field:ClassField):Null { - if ('${cls.module}.${cls.name}' == 'flixel.util.FlxSignal.FlxTypedSignal') + if ('${moduleTypePath(cls)}' == 'flixel.util.FlxSignal.FlxTypedSignal') { return 'new flixel.util.FlxSignal.FlxTypedSignalVoid>'; } if (cls.params.length <= 0) { - return null; - } - - function getCombinations(num:Int):Array> - { - if (num == 0) - { - return [[]]; - } - - var combinations:Array> = []; - for (combination in getCombinations(num - 1)) - { - combinations.push(combination.concat(['Dynamic'])); - // combinations.push(combination.concat(['Dynamic->Void'])); - } - - return combinations; + return 'new ${moduleTypePath(cls)}'; } - var typeArgss:Array> = getCombinations(cls.params.length); - for (typeArgs in typeArgss) - { - // TODO: figure out a way to find out whether a typeparameter is valid or not - try - { - // var expr = Context.parse('new ${cls.module}.${cls.name}<' + typeArgs.join(', ') + '>()', Context.currentPos()); - // return expr; - return 'new ${cls.module}.${cls.name} <' + typeArgs.join(', ') + '>'; - } - catch (e) - { - trace(e); - } - } - - return null; + return 'new ${moduleTypePath(cls)}< ${[for (_ in 0...cls.params.length) 'Dynamic'].join(', ')} >'; } static function createFields(cls:AbstractType, field:ClassField):Array @@ -326,13 +293,13 @@ class PolymodMacro default: } - var strExpr = Context.parse('${returnStr}${cls.module}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos()); + var expr:String = '${returnStr}${moduleTypePath(cls)}.${field.name}(${exprArgs.join(', ')})'; fields.push( { name: field.name, doc: field.doc, - access: [Access.AStatic].concat(getFieldAccess(field)), + access: [Access.APublic, Access.AStatic], kind: FieldType.FFun( { args: fieldArgs, @@ -340,7 +307,7 @@ class PolymodMacro expr: macro { @:privateAccess - ${strExpr}; + ${Context.parse(expr, Context.currentPos())}; }, params: [] }), @@ -351,18 +318,18 @@ class PolymodMacro { name: field.name, doc: field.doc, - access: [Access.AStatic].concat(getFieldAccess(field)), + access: [Access.APublic, Access.AStatic], kind: FieldType.FProp('get', 'never', (macro :Dynamic), null), pos: Context.currentPos() }); - var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}', Context.currentPos()); + var expr:String = '${moduleTypePath(cls)}.${field.name}'; fields.push( { name: 'get_${field.name}', doc: field.doc, - access: [Access.AStatic, field.isPublic ? Access.APublic : Access.APrivate], + access: [Access.APublic, Access.AStatic], kind: FieldType.FFun( { args: [], @@ -370,7 +337,7 @@ class PolymodMacro expr: macro { @:privateAccess - return ${strExpr}; + return ${Context.parse(expr, Context.currentPos())}; }, params: [] }), @@ -381,18 +348,18 @@ class PolymodMacro { name: field.name, doc: field.doc, - access: [Access.AStatic].concat(getFieldAccess(field)), + access: [Access.APublic, Access.AStatic], kind: FieldType.FProp('get', 'never', (macro :Dynamic), null), pos: Context.currentPos() }); - var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}', Context.currentPos()); + var expr:String = '${moduleTypePath(cls)}.${field.name}'; fields.push( { name: 'get_${field.name}', doc: field.doc, - access: [Access.AStatic, field.isPublic ? Access.APublic : Access.APrivate], + access: [Access.APublic, Access.AStatic], kind: FieldType.FFun( { args: [], @@ -400,7 +367,7 @@ class PolymodMacro expr: macro { @:privateAccess - return ${strExpr}; + return ${Context.parse(expr, Context.currentPos())}; }, params: [] }), @@ -413,23 +380,18 @@ class PolymodMacro return fields; } - static function getFieldAccess(field:ClassField):Array + // Dynamic so any kind of type with `module` and `name` fields works + static function moduleTypePath(type:Dynamic):String { - var access = []; - access.push(field.isPublic ? Access.APublic : Access.APrivate); - if (field.isFinal) - { - access.push(Access.AFinal); - } - if (field.isAbstract) - { - access.push(Access.AAbstract); - } - if (field.isExtern) - { - access.push(Access.AExtern); - } - return access; + var dot:String = type.module.length != 0 ? '.' : ''; + return '${type.module}${dot}${type.name}'; + } + + // Dynamic so any kind of type with `pack` and `name` fields works + static function packTypePath(type:Dynamic):String + { + var dot:String = type.pack.length != 0 ? '.' : ''; + return '${type.pack.join('.')}${dot}${type.name}'; } #end } From 9ee4b0b94e4e797245db797a76dc508f169594a2 Mon Sep 17 00:00:00 2001 From: lemz1 Date: Wed, 30 Oct 2024 00:20:41 +0100 Subject: [PATCH 22/23] no more checkstyle warnings --- source/funkin/util/macro/PolymodMacro.hx | 27 ++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index 98c0f6ea18..acdaa6f859 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -7,8 +7,18 @@ import haxe.macro.Type; using haxe.macro.ExprTools; using StringTools; +/** + * This macro creates aliases for abstracts. + * That way we can use abstracts in hscript + */ +@SuppressWarnings(['checkstyle:CodeSimilarity', 'checkstyle:Dynamic']) class PolymodMacro { + /** + * The abstracts and their corresponding aliases + * `key` => original class path + * `value` => alias class path + */ public static var aliases(get, never):Map; static function get_aliases():Map @@ -17,6 +27,11 @@ class PolymodMacro return Reflect.callMethod(null, Reflect.field(Type.resolveClass('funkin.util.macro.AbstractAliases'), 'get'), []); } + /** + * Function that builds all the alias classes + * @param abstractClasses An array of packages and classes + * (`!` infront of the name will exclude classes from being built) + */ public static macro function buildPolymodAbstracts(abstractClasses:Array):Void { Context.onAfterTyping((types) -> { @@ -38,14 +53,14 @@ class PolymodMacro } } - var aliases:Map = new Map(); + var abstractAliases:Map = new Map(); for (type in types) { switch (type) { case ModuleType.TAbstract(a): - var cls = a.get(); + var cls:AbstractType = a.get(); if (cls.isPrivate) { continue; @@ -71,7 +86,7 @@ class PolymodMacro } } - aliases.set('${packTypePath(cls)}', 'polymod.abstracts.${packTypePath(cls)}'); + abstractAliases.set('${packTypePath(cls)}', 'polymod.abstracts.${packTypePath(cls)}'); buildAbstract(cls); break; } @@ -95,7 +110,7 @@ class PolymodMacro ret: (macro :Map), expr: macro { - return $v{aliases}; + return $v{abstractAliases}; } }), pos: Context.currentPos() @@ -177,7 +192,7 @@ class PolymodMacro static function buildCreateField(cls:AbstractType, field:ClassField):Field { - var funcArgs = []; + var funcArgs:Array = []; var funcArgNames:Array = []; switch (field.type) { @@ -258,7 +273,7 @@ class PolymodMacro switch (type) { case Type.TFun(args, ret): - var fieldArgs = []; + var fieldArgs:Array = []; var exprArgs:Array = []; for (arg in args) { From e9a1ee42b27d4032741a216423f031910a8b476f Mon Sep 17 00:00:00 2001 From: lemz1 Date: Wed, 30 Oct 2024 20:38:15 +0100 Subject: [PATCH 23/23] let the compiler figure out the type --- project.hxp | 8 +++--- source/funkin/util/macro/PolymodMacro.hx | 31 +++++++++++++----------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/project.hxp b/project.hxp index c3982863a4..d0559d08e5 100644 --- a/project.hxp +++ b/project.hxp @@ -590,9 +590,11 @@ class Project extends HXProject { addHaxeMacro("addMetadata('@:build(funkin.util.macro.FlxMacro.buildFlxBasic())', 'flixel.FlxBasic')"); var abstracts:Array = [ - 'funkin.*', - 'flixel.*', - 'haxe.*' + '.*', + '!thx.Set', + '!cpp.*', + '!lime.*', + '!openfl.*' ]; addHaxeMacro("funkin.util.macro.PolymodMacro.buildPolymodAbstracts(['" + abstracts.join("', '") + "'])"); } diff --git a/source/funkin/util/macro/PolymodMacro.hx b/source/funkin/util/macro/PolymodMacro.hx index acdaa6f859..c1c23253a6 100644 --- a/source/funkin/util/macro/PolymodMacro.hx +++ b/source/funkin/util/macro/PolymodMacro.hx @@ -86,7 +86,7 @@ class PolymodMacro } } - abstractAliases.set('${packTypePath(cls)}', 'polymod.abstracts.${packTypePath(cls)}'); + abstractAliases.set('${packTypePath(cls)}', 'polymod.abstracts.${packTypePath(cls)}${classSuffix}'); buildAbstract(cls); break; } @@ -126,6 +126,7 @@ class PolymodMacro } #if macro + static var classSuffix:String = '_'; static var alreadyCalled:Bool = false; static var skipFields:Array = []; @@ -160,7 +161,7 @@ class PolymodMacro { pos: Context.currentPos(), pack: ['polymod', 'abstracts'].concat(abstractCls.pack), - name: abstractCls.name, + name: abstractCls.name + classSuffix, kind: TypeDefKind.TDClass(null, [], false, false, false), fields: fields, }, null); @@ -202,7 +203,7 @@ class PolymodMacro funcArgs.push( { name: arg.name, - type: (macro :Dynamic), + type: null, opt: arg.opt }); funcArgNames.push(arg.name); @@ -219,7 +220,7 @@ class PolymodMacro kind: FieldType.FFun( { args: funcArgs, - ret: (macro :Dynamic), + ret: null, expr: macro { @:privateAccess @@ -290,23 +291,25 @@ class PolymodMacro fieldArgs.push( { name: arg.name, - type: (macro :Dynamic), + type: null, opt: arg.opt, }); } - var returnStr:String = 'return '; - var returnType:ComplexType = (macro :Dynamic); - switch (ret) + var returnStr:String = switch (ret) { case Type.TAbstract(t, _): if (t.get().name == 'Void') { - returnStr = ''; - returnType = (macro :Void); + ''; + } + else + { + 'return '; } default: - } + 'return '; + }; var expr:String = '${returnStr}${moduleTypePath(cls)}.${field.name}(${exprArgs.join(', ')})'; @@ -318,7 +321,7 @@ class PolymodMacro kind: FieldType.FFun( { args: fieldArgs, - ret: returnType, + ret: null, expr: macro { @:privateAccess @@ -348,7 +351,7 @@ class PolymodMacro kind: FieldType.FFun( { args: [], - ret: (macro :Dynamic), + ret: null, expr: macro { @:privateAccess @@ -378,7 +381,7 @@ class PolymodMacro kind: FieldType.FFun( { args: [], - ret: (macro :Dynamic), + ret: null, expr: macro { @:privateAccess