Skip to content

Commit

Permalink
rewrite registrymacro
Browse files Browse the repository at this point in the history
  • Loading branch information
lemz1 committed Nov 7, 2024
1 parent e80a686 commit cbff141
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 259 deletions.
26 changes: 16 additions & 10 deletions source/funkin/util/macro/EntryMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@ using StringTools;

class EntryMacro
{
public static macro function build(registryExpr:ExprOf<Class<Dynamic>>, ...additionalReferencedRegistries:ExprOf<Class<Dynamic>>):Array<Field>
public static macro function build(registry:ExprOf<Class<Dynamic>>, ...additionalReferencedRegistries:ExprOf<Class<Dynamic>>):Array<Field>
{
var fields = Context.getBuildFields();

var cls = Context.getLocalClass().get();

var entryData = getEntryData(cls);

var registryCls = MacroUtil.getClassTypeFromExpr(registryExpr);
makeFieldsCallable(additionalReferencedRegistries.append(registry));

fields = fields.concat(buildVariables(cls, entryData));
fields = fields.concat(buildMethods(cls, registryExpr));
fields = fields.concat(buildMethods(cls, registry));

return fields;
}

#if macro
static function makeFieldsCallable(registries:Array<ExprOf<Class<Dynamic>>>)
{
for (registry in registries)
{
MacroUtil.getClassTypeFromExpr(registry);
}
}

static function fieldAlreadyExists(name:String):Bool
{
for (field in Context.getBuildFields())
Expand Down Expand Up @@ -74,25 +82,23 @@ class EntryMacro

static function buildVariables(cls:ClassType, entryData:Dynamic):Array<Field>
{
var dataType:ComplexType = Context.getType('${entryData.module}.${entryData.name}').toComplexType();
var entryDataType:ComplexType = Context.getType('${entryData.module}.${entryData.name}').toComplexType();

return (macro class TempClass
{
public final id:String;

public final _data:Null<$dataType>;
}).fields.filter((field) -> {
return !fieldAlreadyExists(field.name);
});
public final _data:Null<$entryDataType>;
}).fields.filter((field) -> return !fieldAlreadyExists(field.name));
}

static function buildMethods(cls:ClassType, registryExpr:ExprOf<Class<Dynamic>>):Array<Field>
static function buildMethods(cls:ClassType, registry:ExprOf<Class<Dynamic>>):Array<Field>
{
return (macro class TempClass
{
public function _fetchData(id:String)
{
return ${registryExpr}.instance.parseEntryDataWithMigration(id, ${registryExpr}.instance.fetchEntryVersion(id));
return ${registry}.instance.parseEntryDataWithMigration(id, ${registry}.instance.fetchEntryVersion(id));
}

public function toString()
Expand Down
Loading

0 comments on commit cbff141

Please sign in to comment.