Browse Source

added parseMetaData, and applyLang(ignoreMeta)

Nicolas Cannasse 6 years ago
parent
commit
b0cf64ed0a
1 changed files with 63 additions and 13 deletions
  1. 63 13
      hxd/res/DynamicText.hx

+ 63 - 13
hxd/res/DynamicText.hx

@@ -9,6 +9,9 @@ import haxe.xml.Fast in Access;
 import haxe.xml.Access;
 import haxe.xml.Access;
 #end
 #end
 
 
+typedef DynamicTextMeta = Map<String,DynamicTextMetaContent>;
+typedef DynamicTextMetaContent = { skip : Bool, sub : DynamicTextMeta };
+
 abstract NoArg({}) {
 abstract NoArg({}) {
 }
 }
 
 
@@ -31,7 +34,42 @@ class DynamicText {
 		return obj;
 		return obj;
 	}
 	}
 
 
-	public static function apply( obj : Dynamic, data : String, ?reference : String, ?onMissing ) {
+	public static function parseMetaData( data : String ) : DynamicTextMeta {
+		var x = new Access(Xml.parse(data).firstElement());
+		var m = new DynamicTextMeta();
+		for( e in x.elements ) {
+			var me = parseMetaDataRec(e);
+			if( me != null ) m.set(e.att.id, me);
+		}
+		return m;
+	}
+
+	static function parseMetaDataRec( x : Access ) : DynamicTextMetaContent {
+		var skip = x.has.skip;
+		var sub : DynamicTextMeta = null;
+		switch( x.name ) {
+		case "g":
+			var first = x.elements.hasNext() ? x.elements.next() : null;
+			var byId = first != null && first.has.id;
+			var index = -1;
+			for( e in x.elements ) {
+				index++;
+				var ms = parseMetaDataRec(e);
+				if( ms == null ) continue;
+				if( sub == null ) sub = new Map();
+				sub.set(byId ? e.att.id : "["+index+"]", ms);
+			}
+		case "t":
+			// nothing
+		default:
+			throw "Unknown tag " + x.name;
+		}
+		if( !skip && sub == null )
+			return null;
+		return { skip : skip, sub : sub };
+	}
+
+	static function apply( obj : Dynamic, data : String, ?reference : String, onMissing ) {
 		var x = new Access(Xml.parse(data).firstElement());
 		var x = new Access(Xml.parse(data).firstElement());
 		var ref = reference == null ? null : new Access(Xml.parse(reference).firstElement());
 		var ref = reference == null ? null : new Access(Xml.parse(reference).firstElement());
 		applyRec([], obj, x, ref, onMissing);
 		applyRec([], obj, x, ref, onMissing);
@@ -42,16 +80,16 @@ class DynamicText {
 	static function applyText( path : Array<String>, old : Dynamic, x : Access, ref : Access, onMissing ) {
 	static function applyText( path : Array<String>, old : Dynamic, x : Access, ref : Access, onMissing ) {
 		var str = x == null ? null : x.innerHTML;
 		var str = x == null ? null : x.innerHTML;
 		if( str == null ) {
 		if( str == null ) {
-			onMissing(path.join(".") + " is missing");
+			onMissing(path, "is missing");
 			return null;
 			return null;
 		}
 		}
 		if( Std.is(old,Array) ) {
 		if( Std.is(old,Array) ) {
-			onMissing(path.join(".")+" should be a group");
+			onMissing(path,"should be a group");
 			return null;
 			return null;
 		}
 		}
 		var strOld : String = if( Reflect.isFunction(old) ) old({}) else old;
 		var strOld : String = if( Reflect.isFunction(old) ) old({}) else old;
 		if( strOld == null ) {
 		if( strOld == null ) {
-			onMissing(path.join(".") + " is no longer used");
+			onMissing(path,"is no longer used");
 			return null;
 			return null;
 		}
 		}
 		var mparams = new Map();
 		var mparams = new Map();
@@ -60,7 +98,7 @@ class DynamicText {
 		r_attr.map(str, function(r) {
 		r_attr.map(str, function(r) {
 			var p = r.matched(1);
 			var p = r.matched(1);
 			if( !mparams.exists(p) ) {
 			if( !mparams.exists(p) ) {
-				onMissing(path.join(".") + " has extra param '" + p + "'");
+				onMissing(path,"has extra param '" + p + "'");
 				ok = false;
 				ok = false;
 			}
 			}
 			mparams.set(p, false);
 			mparams.set(p, false);
@@ -68,11 +106,11 @@ class DynamicText {
 		});
 		});
 		for( p in mparams.keys() )
 		for( p in mparams.keys() )
 			if( mparams.get(p) ) {
 			if( mparams.get(p) ) {
-				onMissing(path.join(".") + " is missing param '" + p + "'");
+				onMissing(path,"is missing param '" + p + "'");
 				ok = false;
 				ok = false;
 			}
 			}
 		if( ref != null && ref.innerHTML != strOld ) {
 		if( ref != null && ref.innerHTML != strOld ) {
-			onMissing(path.join(".") +" ignored since has changed");
+			onMissing(path,"ignored since has changed");
 			ok = false;
 			ok = false;
 		}
 		}
 		if( !ok )
 		if( !ok )
@@ -105,19 +143,19 @@ class DynamicText {
 				case "g":
 				case "g":
 					var id = x.att.id;
 					var id = x.att.id;
 					if( id == null ) {
 					if( id == null ) {
-						onMissing(path.join(".") +" group is missing id");
+						onMissing(path,"group is missing id");
 						continue;
 						continue;
 					}
 					}
 					path.push(id);
 					path.push(id);
 					fields.remove(id);
 					fields.remove(id);
 					var sub : Dynamic = Reflect.field(obj, id);
 					var sub : Dynamic = Reflect.field(obj, id);
 					if( sub == null ) {
 					if( sub == null ) {
-						onMissing(path.join(".") +" is no longer used");
+						onMissing(path,"is no longer used");
 						path.pop();
 						path.pop();
 						continue;
 						continue;
 					}
 					}
 					if( Std.is(sub,String) ) {
 					if( Std.is(sub,String) ) {
-						onMissing(path.join(".") +" should be a text and not a group");
+						onMissing(path,"should be a text and not a group");
 						path.pop();
 						path.pop();
 						continue;
 						continue;
 					}
 					}
@@ -151,7 +189,7 @@ class DynamicText {
 		}
 		}
 		for( f in fields.keys() ) {
 		for( f in fields.keys() ) {
 			path.push(f);
 			path.push(f);
-			onMissing(path.join(".") +" is missing");
+			onMissing(path,"is missing");
 			path.pop();
 			path.pop();
 		}
 		}
 	}
 	}
@@ -332,9 +370,21 @@ class DynamicText {
 			public static function load( data : String ) {
 			public static function load( data : String ) {
 				DATA = hxd.res.DynamicText.parse(data);
 				DATA = hxd.res.DynamicText.parse(data);
 			}
 			}
-			public static function applyLang( data : String, ?reference, ?onMissing ) {
+			public static function applyLang( data : String, ?reference, ?onMissing, ?ignoreMeta : hxd.res.DynamicText.DynamicTextMeta ) {
 				if( onMissing == null ) onMissing = function(msg) trace(msg);
 				if( onMissing == null ) onMissing = function(msg) trace(msg);
-				hxd.res.DynamicText.apply(DATA,data,reference,onMissing);
+				@:privateAccess hxd.res.DynamicText.apply(DATA,data,reference,function(path,msg) {
+					if( ignoreMeta != null ) {
+						var m = ignoreMeta;
+						for( p in path ) {
+							var me = m.get(p);
+							if( me == null ) break;
+							if( me.skip ) return;
+							m = me.sub;
+							if( m == null ) break;
+						}
+					}
+					onMissing(path.join(".")+" "+msg);
+				});
 			}
 			}
 		};
 		};
 		for( f in c.fields )
 		for( f in c.fields )