Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lemz1 committed Oct 13, 2024
1 parent 3c9f36a commit 3b6061d
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 31 deletions.
33 changes: 16 additions & 17 deletions source/funkin/data/notestyle/NoteStyleRegistry.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import funkin.play.notes.notestyle.NoteStyle;
import funkin.play.notes.notestyle.ScriptedNoteStyle;
import funkin.data.notestyle.NoteStyleData;

@:build(funkin.util.macro.RegistryMacro.build())
class NoteStyleRegistry extends BaseRegistry<NoteStyle, NoteStyleData>
{
/**
Expand All @@ -15,14 +16,13 @@ class NoteStyleRegistry extends BaseRegistry<NoteStyle, NoteStyleData>

public static final NOTE_STYLE_DATA_VERSION_RULE:thx.semver.VersionRule = "1.1.x";

public static var instance(get, never):NoteStyleRegistry;
static var _instance:Null<NoteStyleRegistry> = null;

static function get_instance():NoteStyleRegistry
{
if (_instance == null) _instance = new NoteStyleRegistry();
return _instance;
}
// public static var instance(get, never):NoteStyleRegistry;
// static var _instance:Null<NoteStyleRegistry> = null;
// static function get_instance():NoteStyleRegistry
// {
// if (_instance == null) _instance = new NoteStyleRegistry();
// return _instance;
// }

public function new()
{
Expand Down Expand Up @@ -81,13 +81,12 @@ class NoteStyleRegistry extends BaseRegistry<NoteStyle, NoteStyleData>
return parser.value;
}

function createScriptedEntry(clsName:String):NoteStyle
{
return ScriptedNoteStyle.init(clsName, "unknown");
}

function getScriptedClassNames():Array<String>
{
return ScriptedNoteStyle.listScriptClasses();
}
// function createScriptedEntry(clsName:String):NoteStyle
// {
// return ScriptedNoteStyle.init(clsName, "unknown");
// }
// function getScriptedClassNames():Array<String>
// {
// return ScriptedNoteStyle.listScriptClasses();
// }
}
1 change: 0 additions & 1 deletion source/funkin/data/song/SongRegistry.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import funkin.util.VersionUtil;
using funkin.data.song.migrator.SongDataMigrator;

@:nullSafety
@:build(funkin.util.macro.RegistryMacro.build())
class SongRegistry extends BaseRegistry<Song, SongMetadata>
{
/**
Expand Down
201 changes: 188 additions & 13 deletions source/funkin/util/macro/RegistryMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,219 @@ class RegistryMacro
var fields = Context.getBuildFields();

var cls = Context.getLocalClass().get();
var clsName = cls.pack.join('.') + '.' + cls.name;

var typeParams = getTypeParams(cls);
var entryCls = typeParams.entryCls;
var jsonCls = typeParams.jsonCls;
var scriptedEntryCls = getScriptedEntryClass(entryCls);

var entryCls = typeParams[0];
var entryClsName = entryCls.pack.join('.') + '.' + entryCls.name;
// fields = fields.concat(buildInstanceField(cls));

var jsonCls = typeParams[1];
var jsonClsName = jsonCls.pack.join('.') + '.' + jsonCls.name;
// fields.push(buildGetScriptedClassNamesField(scriptedEntryClsName));

trace('ENTRY', entryClsName);
trace('JSON', jsonClsName);
// fields.push(buildCreateScriptedEntryField(entryCls, scriptedEntryClsName));

return fields;
}

#if macro
static function getTypeParams(cls:ClassType):Array<ClassType>
static function getTypeParams(cls:ClassType):RegistryTypeParams
{
switch (cls.superClass.t.get().kind)
{
case KGenericInstance(t, params):
var typeParams = [];
case KGenericInstance(_, params):
var typeParams:Array<Dynamic> = [];
for (param in params)
{
switch (param)
{
case TInst(t, _params):
case TInst(t, _):
typeParams.push(t.get());
case TType(t, _):
typeParams.push(t.get());
default:
throw 'Not a class';
}
}
return typeParams;
return {entryCls: typeParams[0], jsonCls: typeParams[1]};
default:
throw 'Not in the correct format';
}
return [];
}

static function getScriptedEntryClass(entryCls:ClassType):ClassType
{
var scriptedEntryClsName = entryCls.pack.join('.') + '.Scripted' + entryCls.name;
switch (Context.getType(scriptedEntryClsName))
{
case Type.TInst(t, _):
return t.get();
default:
throw 'Not A Class (${scriptedEntryClsName})';
};
}

static function buildInstanceField(cls:ClassType):Array<Field>
{
var fields = [];

fields.push(
{
name: '_instance',
access: [Access.APrivate, Access.AStatic],
kind: FieldType.FVar(ComplexType.TPath(
{
pack: [],
name: 'Null',
params: [
TypeParam.TPType(ComplexType.TPath(
{
pack: cls.pack,
name: cls.name,
params: []
}))
]
})),
pos: Context.currentPos()
});

fields.push(
{
name: 'instance',
access: [Access.APublic, Access.AStatic],
kind: FieldType.FProp("get", "never", ComplexType.TPath(
{
pack: cls.pack,
name: cls.name,
params: []
})),
pos: Context.currentPos()
});

var newStrExpr = 'new ${cls.pack.join('.')}.${cls.name}()';
var newExpr = Context.parse(newStrExpr, Context.currentPos());

fields.push(
{
name: 'get_instance',
access: [Access.APrivate, Access.AStatic],
kind: FFun(
{
args: [],
expr: macro
{
if (_instance == null)
{
_instance = ${newExpr};
}
return _instance;
},
params: [],
ret: ComplexType.TPath(
{
pack: cls.pack,
name: cls.name,
params: []
})
}),
pos: Context.currentPos()
});

return fields;
}

static function buildGetScriptedClassNamesField(scriptedEntryCls:ClassType):Field
{
var scriptedEntryExpr = Context.parse('${scriptedEntryCls.pack.join('.')}.${scriptedEntryCls.name}', Context.currentPos());

return {
name: 'getScriptedClassNames',
access: [Access.APrivate],
kind: FieldType.FFun(
{
args: [],
expr: macro
{
return ${scriptedEntryExpr}.listScriptClasses();
},
params: [],
ret: (macro :Array<String>)
}),
pos: Context.currentPos()
};
}

static function buildCreateScriptedEntryField(entryCls:ClassType, scriptedEntryCls:ClassType):Field
{
var scriptedStrExpr = '${scriptedEntryCls.pack.join('.')}.${scriptedEntryCls.name}.init(clsName, ${buildNullArgs(entryCls.constructor.get())})';
var scriptedInitExpr = Context.parse(scriptedStrExpr, Context.currentPos());

return {
name: 'createScriptedEntry',
access: [Access.APrivate],
kind: FieldType.FFun(
{
args: [
{
name: 'clsName',
type: (macro :String)
}
],
expr: macro
{
return ${scriptedInitExpr};
},
params: [],
ret: ComplexType.TPath(
{
pack: [],
name: 'Null',
params: [
TypeParam.TPType(ComplexType.TPath(
{
pack: entryCls.pack,
name: entryCls.name
}))
]
})
}),
pos: Context.currentPos()
};
}

static function buildNullArgs(fun:ClassField):String
{
var amount:Int = 0;
switch (fun.type)
{
case Type.TFun(args, _):
amount = args.length;
case Type.TLazy(f):
switch (f())
{
case Type.TFun(args, _):
amount = args.length;
default:
throw 'Not a function';
}
default:
throw 'Not a function';
}

var args = [];
for (i in 0...amount)
{
args.push('null');
}
return args.join(', ');
}
#end
}

#if macro
typedef RegistryTypeParams =
{
var entryCls:ClassType;
var jsonCls:Dynamic; // DefType or ClassType
}
#end

0 comments on commit 3b6061d

Please sign in to comment.