From 9ce35bd2691ecfd8cbd848ce4ed54c8d4e099df0 Mon Sep 17 00:00:00 2001 From: kyubuns Date: Tue, 2 Jun 2020 00:14:03 +0900 Subject: [PATCH] fix: initialize bug on C# https://github.com/HeapsIO/hxbit/issues/47 --- haxelib.json | 4 +++- hxbitmini/Macros.hx | 31 ++++++++++++++++++++++++++++++- hxbitmini/Serializer.hx | 6 +++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/haxelib.json b/haxelib.json index efa09fb..cd56564 100644 --- a/haxelib.json +++ b/haxelib.json @@ -6,5 +6,7 @@ "version" : "1.5.0", "releasenote" : "", "contributors" : ["ncannasse"], - "dependencies" : {} + "dependencies" : { + "compiletime" : "2.8.0" + } } \ No newline at end of file diff --git a/hxbitmini/Macros.hx b/hxbitmini/Macros.hx index c255239..2d24a71 100644 --- a/hxbitmini/Macros.hx +++ b/hxbitmini/Macros.hx @@ -571,7 +571,14 @@ class Macros { pos : pos, access : [AStatic], meta : noCompletion, - kind : FVar(macro : Int, macro @:privateAccess hxbitmini.Serializer.registerClass($p{clName})), + kind : FVar(macro : Int), + }); + fields.push({ + name : "initCLID", + pos : pos, + access : access.concat([AStatic]), + meta : noCompletion, + kind : FFun({ args : [], ret : null , expr : macro __clid = @:privateAccess hxbitmini.Serializer.registerClass($p{clName}) }), }); fields.push({ name : "getCLID", @@ -903,6 +910,28 @@ class Macros { } } + public static function buildSerializer() { + var fields = Context.getBuildFields(); + var pos = Context.currentPos(); + var noCompletion = [{ name : ":noCompletion", pos : pos }]; + + fields.push({ + name : "initCLIDS", + pos : pos, + access : [AStatic, APrivate], + meta : noCompletion, + kind : FFun({ args : [], ret : null , expr : macro { + var serializables = CompileTime.getAllClasses(Serializable); + for( cl in serializables ) { + var fl : Dynamic = Reflect.field(cl, "initCLID"); + Reflect.callMethod(cl, fl, []); + } + } }), + }); + + return fields; + } + #end } diff --git a/hxbitmini/Serializer.hx b/hxbitmini/Serializer.hx index 636df2e..a4005ce 100644 --- a/hxbitmini/Serializer.hx +++ b/hxbitmini/Serializer.hx @@ -23,6 +23,7 @@ package hxbitmini; #if !macro +@:build(hxbitmini.Macros.buildSerializer()) class Serializer { static var SEQ = 0; @@ -105,7 +106,10 @@ class Serializer { var knownStructs : Array; public function new() { - if( CLIDS == null ) initClassIDS(); + if( CLIDS == null ) { + initCLIDS(); + initClassIDS(); + } } public function begin() {