@@ -27,6 +27,8 @@ public static ConfigNode FindConfigNodeIn(ConfigNode src, string nodeType,
2727 print ( "Searching node for " + nodeType + "[" + nodeName + "," + nodeTag + "]" ) ;
2828#endif
2929 foreach ( ConfigNode n in src . GetNodes ( nodeType ) ) {
30+ if ( nodeName == null && nodeTag == null )
31+ return n ;
3032 if ( n . HasValue ( "name" ) && WildcardMatch ( n . GetValue ( "name" ) , nodeName ) &&
3133 ( nodeTag == null ||
3234 ( n . HasValue ( "tag" ) && WildcardMatch ( n . GetValue ( "tag" ) , nodeTag ) ) ) ) {
@@ -54,6 +56,7 @@ public static string RemoveWS(string withWhite)
5456 { // Removes ALL whitespace of a string.
5557 return new string ( withWhite . ToCharArray ( ) . Where ( c => ! Char . IsWhiteSpace ( c ) ) . ToArray ( ) ) ;
5658 }
59+
5760 // ModifyNode applies the ConfigNode mod as a 'patch' to ConfigNode original, then returns the patched ConfigNode.
5861 // it uses FindConfigNodeIn(src, nodeType, nodeName, nodeTag) to recurse.
5962 public static ConfigNode ModifyNode ( ConfigNode original , ConfigNode mod )
@@ -93,8 +96,8 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
9396 newNode . AddNode ( subMod ) ;
9497 else {
9598 ConfigNode subNode ;
96- if ( subMod . name [ 0 ] == '@' && subMod . name [ 0 ] != '%' )
97- subNode = null ;
99+ // if (subMod.name[0] == '@' && subMod.name[0] != '%')
100+ // subNode = null;
98101
99102 if ( subMod . name . Contains ( "[" ) ) {
100103 // format @NODETYPE[Name] {...} or @NODETYPE[Name, Tag] {...} or ! instead of @
@@ -112,6 +115,9 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
112115 string nodeType = subMod . name . Substring ( 1 ) ;
113116
114117 // format @NODETYPE,N {...} or ! instead of @
118+ // The problem with ! is that the index is messed up
119+ // So the patch need to take that into account
120+ // and lower the index for the next search
115121 int index = 0 ;
116122 if ( nodeType . Contains ( "," ) ) {
117123 int . TryParse ( nodeType . Split ( ',' ) [ 1 ] , out index ) ;
@@ -134,14 +140,30 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
134140 if ( subMod . name [ 0 ] == '%' ) {
135141 // if the original node exist add it
136142 if ( subNode != null ) {
137- newNode . nodes . Add ( subNode ) ;
143+ ConfigNode newSubNode = ModifyNode ( subNode , subMod ) ;
144+ newNode . nodes . Add ( newSubNode ) ;
138145 }
139- else { // if not add the mod node without the % in its name
140- if ( subMod . name . Contains ( "[" ) )
141- subMod . name = subMod . name . Substring ( 1 ) . Split ( '[' ) [ 0 ] ;
142- else
143- subMod . name = subMod . name . Substring ( 1 ) ;
144- newNode . nodes . Add ( subMod ) ;
146+ else { // if not add the mod node without the % in its name
147+ // This part is messy. ialdabaoth is right, i need to rewrite.
148+ string type ;
149+ string name ;
150+ if ( subMod . name . Contains ( "[" ) ) {
151+ type = subMod . name . Substring ( 1 ) . Split ( '[' ) [ 0 ] ;
152+ name = subMod . name . Split ( '[' ) [ 1 ] . Replace ( "]" , "" ) ;
153+ }
154+ else
155+ {
156+ type = subMod . name . Substring ( 1 ) ;
157+ name = null ;
158+ }
159+
160+ ConfigNode copy = new ConfigNode ( type ) ;
161+
162+ if ( name != null )
163+ copy . AddValue ( "name" , name ) ;
164+
165+ ConfigNode newSubNode = ModifyNode ( copy , subMod ) ;
166+ newNode . nodes . Add ( newSubNode ) ;
145167 }
146168 }
147169 if ( subMod . name [ 0 ] == '$' )
@@ -178,7 +200,7 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
178200
179201 int patchCount = 0 ;
180202
181- public void Update ( )
203+ public void OnGUI ( )
182204 {
183205 /*
184206 * It should be a code to reload when the Reload Database debug button is used.
@@ -196,6 +218,8 @@ public void Update()
196218 }
197219 */
198220
221+
222+
199223 if ( ! GameDatabase . Instance . IsReady ( ) && ( ( HighLogic . LoadedScene == GameScenes . MAINMENU ) || ( HighLogic . LoadedScene == GameScenes . SPACECENTER ) ) )
200224 {
201225 return ;
@@ -293,17 +317,20 @@ public void ApplyPatch(List<String> excludePaths, bool final)
293317 if ( ! final ^ mod . name . EndsWith ( ":Final" ) ) {
294318 char [ ] sep = new char [ ] { '[' , ']' } ;
295319 string name = RemoveWS ( mod . name ) ;
320+ string cond = "" ;
296321 if ( final )
297322 name = name . Substring ( 0 , name . LastIndexOf ( ":Final" ) ) ;
298- string [ ] splits = name . Split ( sep , 3 ) ;
299- string pattern = splits [ 1 ] ;
300- string type = splits [ 0 ] . Substring ( 1 ) ;
301323
302- String cond = "" ;
303- if ( splits . Length > 2 && splits [ 2 ] . Length > 5 ) {
304- int start = splits [ 2 ] . IndexOf ( "HAS[" ) + 4 ;
305- cond = splits [ 2 ] . Substring ( start , splits [ 2 ] . LastIndexOf ( ']' ) - start ) ;
324+ if ( name . Contains ( ":HAS[" ) ) {
325+ int start = name . IndexOf ( ":HAS[" ) ;
326+ cond = name . Substring ( start + 5 , name . LastIndexOf ( ']' ) - start - 5 ) ;
327+ name = name . Substring ( 0 , start ) ;
306328 }
329+
330+ string [ ] splits = name . Split ( sep , 3 ) ;
331+ string pattern = splits . Length > 1 ? splits [ 1 ] : null ;
332+ string type = splits [ 0 ] . Substring ( 1 ) ;
333+
307334 foreach ( UrlDir . UrlConfig url in GameDatabase . Instance . root . AllConfigs ) {
308335 if ( url . type == type
309336 && WildcardMatch ( url . name , pattern )
@@ -364,15 +391,19 @@ public static bool CheckCondition(ConfigNode node, string conds)
364391 if ( condsList . Count == 1 ) {
365392 conds = condsList [ 0 ] ;
366393
394+
395+
367396 string remainCond = "" ;
368397 if ( conds . Contains ( "HAS[" ) ) {
369398 int start = conds . IndexOf ( "HAS[" ) + 4 ;
370399 remainCond = conds . Substring ( start , condsList [ 0 ] . LastIndexOf ( ']' ) - start ) ;
371400 conds = conds . Substring ( 0 , start - 5 ) ;
372401 }
373402
374- string type = conds . Substring ( 1 ) . Split ( '[' ) [ 0 ] ;
375- string name = conds . Split ( '[' ) [ 1 ] . Replace ( "]" , "" ) ;
403+ char [ ] sep = new char [ ] { '[' , ']' } ;
404+ string [ ] splits = conds . Split ( sep , 3 ) ;
405+ string type = splits [ 0 ] . Substring ( 1 ) ;
406+ string name = splits . Length > 1 ? splits [ 1 ] : null ;
376407
377408 switch ( conds [ 0 ] ) {
378409 case '@' :
@@ -402,6 +433,7 @@ public static bool CheckCondition(ConfigNode node, string conds)
402433
403434 public static bool WildcardMatch ( String s , String wildcard )
404435 {
436+ if ( wildcard == null ) return true ;
405437 String pattern = "^" + Regex . Escape ( wildcard ) . Replace ( @"\*" , ".*" ) . Replace ( @"\?" , "." ) + "$" ;
406438 Regex regex ;
407439 regex = new Regex ( pattern ) ;
@@ -436,13 +468,14 @@ public static ConfigNode SearchNodesSwitch(ConfigNode node, String rootName)
436468 {
437469 if ( cas . name == "DEFAULT" || ( cas . name == "CASE" && TestSwitchCase ( cas ) ) )
438470 {
439- foreach ( ConfigNode n in cas . nodes )
440- newNode . AddNode ( n ) ;
441-
442471 string list = "" ;
443472 foreach ( ConfigNode . Value value in cas . values )
444473 list += " " + value . name + "=" + value . value ;
445474 log ( "MM_SWITCH processed for " + rootName + " using " + cas . name + list ) ;
475+
476+ foreach ( ConfigNode n in cas . nodes )
477+ newNode . AddNode ( SearchNodesSwitch ( n , rootName ) ) ;
478+
446479 break ;
447480 }
448481 }
0 commit comments