Pārlūkot izejas kodu

init fields before unserializing (preserve behavior during convert)

ncannasse 9 gadi atpakaļ
vecāks
revīzija
0e7a453993
3 mainītis faili ar 22 papildinājumiem un 5 dzēšanām
  1. 19 4
      hxd/net/Macros.hx
  2. 1 0
      hxd/net/Serializable.hx
  3. 2 1
      hxd/net/Serializer.hx

+ 19 - 4
hxd/net/Macros.hx

@@ -514,6 +514,7 @@ class Macros {
 			ul.push(withPos(macro hxd.net.Macros.unserializeValue(__ctx, this.$fname),f.f.pos));
 		}
 
+		var noCompletion = [{ name : ":noCompletion", pos : pos }];
 		var access = [APublic];
 		if( isSubSer )
 			access.push(AOverride);
@@ -522,21 +523,21 @@ class Macros {
 				name : "__uid",
 				pos : pos,
 				access : [APublic],
-				meta : [{ name : ":noCompletion", pos : pos }],
+				meta : noCompletion,
 				kind : FVar(macro : Int, macro @:privateAccess hxd.net.Serializer.allocUID()),
 			});
 		fields.push({
 			name : "__clid",
 			pos : pos,
 			access : [AStatic],
-			meta : [{ name : ":noCompletion", pos : pos }],
+			meta : noCompletion,
 			kind : FVar(macro : Int, macro @:privateAccess hxd.net.Serializer.registerClass($i{cl.name})),
 		});
 		fields.push({
 			name : "getCLID",
 			pos : pos,
 			access : access,
-			meta : [{ name : ":noCompletion", pos : pos }],
+			meta : noCompletion,
 			kind : FFun({ args : [], ret : macro : Int, expr : macro return __clid }),
 		});
 
@@ -562,6 +563,7 @@ class Macros {
 				name : "getSerializeSchema",
 				pos : pos,
 				access : access,
+				meta : noCompletion,
 				kind : FFun({
 					args : [],
 					ret : null,
@@ -570,8 +572,21 @@ class Macros {
 			});
 		}
 
+		if( fieldsInits.length > 0 || !isSubSer )
+			fields.push({
+				name : "unserializeInit",
+				pos : pos,
+				meta : noCompletion,
+				access : access,
+				kind : FFun({
+					args : [],
+					ret : null,
+					expr : isSubSer ? macro { super.unserializeInit(); $b{fieldsInits}; } : { expr : EBlock(fieldsInits), pos : pos },
+				})
+			});
+
 		if( needUnserialize ) {
-			var unserExpr = macro @:privateAccess { $b{fieldsInits}; ${ if( isSubSer ) macro super.unserialize(__ctx) else macro { } }; $b{ul} };
+			var unserExpr = macro @:privateAccess { ${ if( isSubSer ) macro super.unserialize(__ctx) else macro { } }; $b{ul} };
 
 			for( f in fields )
 				if( f.name == "unserialize" ) {

+ 1 - 0
hxd/net/Serializable.hx

@@ -5,6 +5,7 @@ interface Serializable {
 	public var __uid : Int;
 	public function getCLID() : Int;
 	public function serialize( ctx : Serializer ) : Void;
+	public function unserializeInit() : Void;
 	public function unserialize( ctx : Serializer ) : Void;
 	public function getSerializeSchema() : Schema;
 }

+ 2 - 1
hxd/net/Serializer.hx

@@ -421,9 +421,9 @@ class Serializer {
 		var clidx = getCLID();
 		if( mapIndexes != null ) clidx = mapIndexes[clidx];
 		var i : Serializable = Type.createEmptyInstance(CLASSES[clidx]);
-
 		if( newObjects != null ) newObjects.push(i);
 		i.__uid = id;
+		i.unserializeInit();
 		refs[id] = i;
 		if( convert != null && convert[clidx] != null )
 			convertRef(i, convert[clidx]);
@@ -447,6 +447,7 @@ class Serializer {
 		var i : T = Type.createEmptyInstance(c);
 		if( newObjects != null ) newObjects.push(i);
 		i.__uid = id;
+		i.unserializeInit();
 		refs[id] = i;
 		if( convert != null && convert[clidx] != null )
 			convertRef(i, convert[clidx]);