Use macro to register ccmds
This commit is contained in:
36
hGameTest/src/engine/ConVars_Engine.hx
Normal file
36
hGameTest/src/engine/ConVars_Engine.hx
Normal file
@@ -0,0 +1,36 @@
|
||||
package engine;
|
||||
|
||||
import game.video.Mode;
|
||||
import haxe.macro.Expr.Field;
|
||||
import engine.typedefs.console.CVar;
|
||||
import engine.typedefs.console.CCmd;
|
||||
import engine.enums.console.CVarType;
|
||||
import engine.enums.console.CVarFlag;
|
||||
import engine.macros.CCmdDecorator;
|
||||
import game.ui.console.Console;
|
||||
|
||||
@:keep
|
||||
// @:build(engine.macros.CCmdDecorator.build()) // No longer needed because we added the build macro to the project.xml
|
||||
class ConVars_Engine {
|
||||
|
||||
@:command("list")
|
||||
public static function listCommands(args:Array<String>) {
|
||||
var CVarMap:Map<String, CVar> = ConVar.getCVarMap();
|
||||
var keys:Array<String> = ConVar.getCVarNames();
|
||||
for(key in keys){
|
||||
if(CVarMap[key] != null){
|
||||
Console.devMsg(key+" "+ CVarMap[key].value);
|
||||
}
|
||||
else{
|
||||
Console.devMsg(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@:command("test")
|
||||
public static function testCommand() {
|
||||
// Command implementation
|
||||
trace("Test Output");
|
||||
}
|
||||
|
||||
}
|
||||
46
hGameTest/src/engine/macros/CCmdDecorator.hx
Normal file
46
hGameTest/src/engine/macros/CCmdDecorator.hx
Normal file
@@ -0,0 +1,46 @@
|
||||
package engine.macros;
|
||||
|
||||
import haxe.macro.Expr;
|
||||
import haxe.macro.Context;
|
||||
|
||||
class CCmdDecorator {
|
||||
public static macro function build():Array<Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
var pos = Context.currentPos();
|
||||
|
||||
for (field in fields) {
|
||||
var meta = field.meta != null ? Lambda.find(field.meta, function(m) return m.name == ":command") : null;
|
||||
if (meta != null) {
|
||||
trace(meta);
|
||||
switch field.kind {
|
||||
case FFun(f):
|
||||
var name = meta.params.length > 0
|
||||
? switch(meta.params[0].expr) {
|
||||
case EConst(CString(s)): s;
|
||||
case _: field.name;
|
||||
}
|
||||
: field.name;
|
||||
|
||||
// Create a static initializer field
|
||||
var initField = {
|
||||
name: "__init__" + name,
|
||||
access: [AStatic, APrivate],
|
||||
kind: FVar(null, macro {
|
||||
engine.ConVar.registerCCmd($v{name}, function(args:Array<String>) {
|
||||
${f.expr};
|
||||
});
|
||||
}),
|
||||
pos: pos,
|
||||
doc: null,
|
||||
meta: []
|
||||
};
|
||||
|
||||
// Add the initializer field
|
||||
fields.push(initField);
|
||||
case _:
|
||||
}
|
||||
}
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user