Use macro to register ccmds

This commit is contained in:
Andreas Schaafsma 2025-03-11 09:11:45 +01:00
parent 9571e136b3
commit 34a691fa68
4 changed files with 92 additions and 5 deletions

View File

@ -15,6 +15,10 @@
<!-- classpath, haxe libs -->
<source path="src" />
<!-- Macro configuration -->
<haxeflag name="--macro" value="addGlobalMetadata('', '@:build(engine.macros.CCmdDecorator.build())')" />
<assets path="./res/textures" rename="textures" />
<assets path="./res/fonts" rename="fonts" />
<assets path="./res/data" rename="data"/>
@ -24,6 +28,6 @@
<!--<icon path="assets/openfl.svg" /> -->
<!-- <assets path="assets/img" rename="img" /> -->
<!-- optimize output
<haxeflag name="-dce full" /> -->
<!-- optimize output -->
<haxeflag name="-dce full" />
</project>

View 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");
}
}

View 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;
}
}

View File

@ -15,9 +15,10 @@ class Mode
public static function setVideoMode(width:Int, height:Int, fs:Int = null){
getWindow().resize(width,height);
}
public static var cvMatSetVideoMode:CCmd = ConVar.registerCCmd("mat_setvideomode",(args:Array<String>)->{
setVideoMode(Std.parseInt(args[0]), Std.parseInt(args[1]), Std.parseInt(args[2]));
});
@:command("mat_setvideomode")
public static function cCmdMatSetVideoMode(args:Array<String>){
Mode.setVideoMode(Std.parseInt(args[0]), Std.parseInt(args[1]), Std.parseInt(args[2]));
}
public static function switchFsMode(toState:Int = 0){
if(toState == 0){
if(Lib.current.stage.displayState != StageDisplayState.FULL_SCREEN_INTERACTIVE){