@@ -27,7 +27,8 @@ public static ConfigNode FindConfigNodeIn(ConfigNode src, string nodeType,
2727 print ( "Searching node for " + nodeType + "[" + nodeName + "," + nodeTag + "]" ) ;
2828#endif
2929 int found = 0 ;
30- foreach ( ConfigNode n in src . GetNodes ( nodeType ) ) {
30+ foreach ( ConfigNode n in src . GetNodes ( nodeType ) )
31+ {
3132 if ( nodeName == null )
3233 {
3334 if ( index == found )
@@ -66,7 +67,7 @@ public static bool IsBraquetBalanced(String str)
6667 else if ( c == ']' )
6768 if ( stack . Count == 0 )
6869 return false ;
69- else if ( stack . Peek ( ) == '[' )
70+ else if ( stack . Peek ( ) == '[' )
7071 stack . Pop ( ) ;
7172 else
7273 return false ;
@@ -111,8 +112,8 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
111112 newNode . AddValue ( val . name , val . value ) ;
112113 else
113114 { // Parsing:
114- // Format is @key = value or @key *= value or @key += value or @key -= value
115- // or @key,index = value or @key,index *= value or @key,index += value or @key,index -= value
115+ // Format is @key = value or @key *= value or @key += value or @key -= value
116+ // or @key,index = value or @key,index *= value or @key,index += value or @key,index -= value
116117 string valName = val . name . Substring ( 1 ) ;
117118 int index = 0 ;
118119 if ( valName . Contains ( "," ) )
@@ -131,13 +132,13 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
131132 op = '+' ;
132133 else if ( val . name . Contains ( " -" ) ) // @key -= val
133134 op = '-' ;
134-
135+
135136 if ( op != ' ' )
136137 {
137138 valName = valName . Split ( ' ' ) [ 0 ] ;
138-
139+
139140 string ovalue = original . GetValue ( valName , index ) ;
140- if ( ovalue != null )
141+ if ( ovalue != null )
141142 {
142143 double s , os ;
143144 if ( double . TryParse ( value , out s ) && double . TryParse ( ovalue , out os ) )
@@ -164,7 +165,7 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
164165 newNode . AddValue ( valName , val . value ) ;
165166 }
166167 }
167-
168+
168169 }
169170 print ( vals ) ;
170171
@@ -223,10 +224,10 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
223224 {
224225 // format @NODETYPE {...} or ! instead of @
225226 nodeType = name . Substring ( 1 ) ;
226- nodeName = null ;
227+ nodeName = null ;
227228 }
228229
229-
230+
230231 if ( tag == "*" || cond . Length > 0 )
231232 { // get ALL nodes
232233 if ( cmd == '%' )
@@ -246,7 +247,7 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
246247 else
247248 { // just get one node
248249 ConfigNode n = FindConfigNodeIn ( newNode , nodeType , nodeName , index ) ;
249- if ( n != null ) subNodes . Add ( n ) ;
250+ if ( n != null ) subNodes . Add ( n ) ;
250251 }
251252
252253 if ( cmd == '@' || cmd == '!' || cmd == '$' )
@@ -313,7 +314,8 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
313314 public static List < UrlDir . UrlConfig > AllConfigsStartingWith ( string match )
314315 {
315316 List < UrlDir . UrlConfig > nodes = new List < UrlDir . UrlConfig > ( ) ;
316- foreach ( UrlDir . UrlConfig url in GameDatabase . Instance . root . AllConfigs ) {
317+ foreach ( UrlDir . UrlConfig url in GameDatabase . Instance . root . AllConfigs )
318+ {
317319 if ( url . type . StartsWith ( match ) )
318320 url . config . name = url . type ;
319321 nodes . Add ( url ) ;
@@ -345,7 +347,7 @@ public void OnGUI()
345347 }
346348 */
347349
348-
350+
349351
350352 if ( ! GameDatabase . Instance . IsReady ( ) && ( ( HighLogic . LoadedScene == GameScenes . MAINMENU ) || ( HighLogic . LoadedScene == GameScenes . SPACECENTER ) ) )
351353 {
@@ -377,26 +379,31 @@ public void OnGUI()
377379
378380 // Elect the newest loaded version of MM to process all patch files.
379381 // If there is a newer version loaded then don't do anything
380- if ( eligible . Any ( a =>
382+ if ( eligible . Any ( a =>
381383 a . assembly . GetName ( ) . Version . CompareTo ( currentAssembly . GetName ( ) . Version ) == 1
382- || a . assembly . Location . CompareTo ( currentAssembly . Location ) < 0 ) ) {
384+ || a . assembly . Location . CompareTo ( currentAssembly . Location ) < 0 ) )
385+ {
383386 loaded = true ;
384387 print ( "[ModuleManager] version " + currentAssembly . GetName ( ) . Version + " at " + currentAssembly . Location + " lost the election" ) ;
385388 return ;
386- } else {
389+ }
390+ else
391+ {
387392 string candidates = "" ;
388393 foreach ( AssemblyLoader . LoadedAssembly a in eligible )
389394 if ( currentAssembly . Location != a . path )
390395 candidates += "Version " + a . assembly . GetName ( ) . Version + " " + a . path + " " + "\n " ;
391396 if ( candidates . Length > 0 )
392397 print ( "[ModuleManager] version " + currentAssembly . GetName ( ) . Version + " at " + currentAssembly . Location + " won the election against\n " + candidates ) ;
393- }
398+ }
394399
395400 // Build a list of subdirectory that won't be processed
396401 List < String > excludePaths = new List < string > ( ) ;
397402
398- foreach ( UrlDir . UrlConfig mod in GameDatabase . Instance . root . AllConfigs ) {
399- if ( mod . name == "MODULEMANAGER[LOCAL]" ) {
403+ foreach ( UrlDir . UrlConfig mod in GameDatabase . Instance . root . AllConfigs )
404+ {
405+ if ( mod . name == "MODULEMANAGER[LOCAL]" )
406+ {
400407 string fullpath = mod . url . Substring ( 0 , mod . url . LastIndexOf ( '/' ) ) ;
401408 string excludepath = fullpath . Substring ( 0 , fullpath . LastIndexOf ( '/' ) ) ;
402409 excludePaths . Add ( excludepath ) ;
@@ -412,7 +419,7 @@ public void OnGUI()
412419
413420 mods = new List < AssemblyName > ( ) ;
414421
415- foreach ( AssemblyName a in modsWithDup )
422+ foreach ( AssemblyName a in modsWithDup )
416423 {
417424 if ( ! mods . Any ( m => m . Name == a . Name ) )
418425 mods . Add ( a ) ;
@@ -429,12 +436,12 @@ public void OnGUI()
429436 if ( cfgmod . type [ 0 ] == '@' || ( cfgmod . type [ 0 ] == '$' ) )
430437 {
431438 string name = RemoveWS ( cfgmod . name ) ;
432- if ( name . Contains ( ":FOR[" ) )
439+ if ( name . Contains ( ":FOR[" ) )
433440 { // check for FOR[] blocks that don't match loaded DLLs and add them to the pass list
434-
441+
435442 string dependency = name . Substring ( name . IndexOf ( ":FOR[" ) + 5 ) ;
436443 dependency = dependency . Substring ( 0 , dependency . IndexOf ( ']' ) ) ;
437- if ( mods . Find ( a => RemoveWS ( a . Name . ToUpper ( ) ) . Equals ( RemoveWS ( dependency . ToUpper ( ) ) ) ) == null )
444+ if ( mods . Find ( a => RemoveWS ( a . Name . ToUpper ( ) ) . Equals ( RemoveWS ( dependency . ToUpper ( ) ) ) ) == null )
438445 { // found one, now add it to the list.
439446 AssemblyName newMod = new AssemblyName ( dependency ) ;
440447 newMod . Name = dependency ;
@@ -445,36 +452,39 @@ public void OnGUI()
445452 }
446453 }
447454 log ( modlist ) ;
448-
455+
449456 // :First node (and any node without a :pass)
450457 ApplyPatch ( excludePaths , ":FIRST" ) ;
451458
452- foreach ( AssemblyName mod in mods )
459+ foreach ( AssemblyName mod in mods )
453460 {
454461 ApplyPatch ( excludePaths , ":BEFORE[" + mod . Name + "]" ) ;
455462 ApplyPatch ( excludePaths , ":FOR[" + mod . Name + "]" ) ;
456463 ApplyPatch ( excludePaths , ":AFTER[" + mod . Name + "]" ) ;
457464 }
458-
465+
459466 // :Final node
460467 ApplyPatch ( excludePaths , ":FINAL" ) ;
461468
462- print ( "[ModuleManager] Applied " + patchCount + " patches and found " + errorCount + " errors" ) ;
469+ print ( "[ModuleManager] Applied " + patchCount + " patches and found " + errorCount + " errors" ) ;
463470 loaded = true ;
464471
465472 }
466473 // Apply patch to all relevent nodes
467474 public void ApplyPatch ( List < String > excludePaths , string Stage )
468475 {
469476 print ( "[ModuleManager] " + Stage + ( Stage == ":FIRST" ? " (default) pass" : " pass" ) ) ;
470- foreach ( UrlDir . UrlConfig mod in GameDatabase . Instance . root . AllConfigs ) {
471- if ( mod . type [ 0 ] == '@' || ( mod . type [ 0 ] == '$' ) ) {
472- try {
477+ foreach ( UrlDir . UrlConfig mod in GameDatabase . Instance . root . AllConfigs )
478+ {
479+ if ( mod . type [ 0 ] == '@' || ( mod . type [ 0 ] == '$' ) )
480+ {
481+ try
482+ {
473483 string name = RemoveWS ( mod . name ) ;
474-
484+
475485
476486 string dependencies = "" ;
477- if ( name . Contains ( ":NEEDS[" ) )
487+ if ( name . Contains ( ":NEEDS[" ) )
478488 {
479489 dependencies = name . Substring ( name . IndexOf ( ":NEEDS[" ) + 7 ) . Replace ( "]" , "" ) ;
480490 name = name . Remove ( name . IndexOf ( ":NEEDS[" ) ) ;
@@ -485,9 +495,9 @@ public void ApplyPatch(List<String> excludePaths, string Stage)
485495 foreach ( string dependency in dependencies . Split ( ',' ) )
486496 {
487497 string check = RemoveWS ( dependency ) ;
488- if ( check [ 0 ] == '!' ^ ( mods . Find ( a => a . Name . ToUpper ( ) . Equals ( check . ToUpper ( ) ) ) == null ) )
498+ if ( check [ 0 ] == '!' ^ ( mods . Find ( a => a . Name . ToUpper ( ) . Equals ( check . ToUpper ( ) ) ) == null ) )
489499 {
490-
500+
491501 unresolvedDependencies = true ;
492502 if (
493503 ( Stage == ":FIRST"
@@ -509,14 +519,14 @@ public void ApplyPatch(List<String> excludePaths, string Stage)
509519
510520 }
511521 else if (
512- ( Stage == ":FIRST"
513- && ! name . ToUpper ( ) . Contains ( ":BEFORE[" )
514- && ! name . ToUpper ( ) . Contains ( ":FOR[" )
515- && ! name . ToUpper ( ) . Contains ( ":AFTER[" )
516- && ! name . ToUpper ( ) . Contains ( ":FINAL" )
517- ) ^ name . ToUpper ( ) . EndsWith ( Stage . ToUpper ( ) ) )
522+ ( Stage == ":FIRST"
523+ && ! name . ToUpper ( ) . Contains ( ":BEFORE[" )
524+ && ! name . ToUpper ( ) . Contains ( ":FOR[" )
525+ && ! name . ToUpper ( ) . Contains ( ":AFTER[" )
526+ && ! name . ToUpper ( ) . Contains ( ":FINAL" )
527+ ) ^ name . ToUpper ( ) . EndsWith ( Stage . ToUpper ( ) ) )
518528 {
519- char [ ] sep = new char [ ] { '[' , ']' } ;
529+ char [ ] sep = new char [ ] { '[' , ']' } ;
520530 string cond = "" ;
521531 if ( name . Contains ( Stage ) )
522532 name = name . Substring ( 0 , name . LastIndexOf ( Stage ) ) ;
@@ -529,7 +539,7 @@ public void ApplyPatch(List<String> excludePaths, string Stage)
529539 }
530540
531541 string [ ] splits = name . Split ( sep , 3 ) ;
532- string pattern = splits . Length > 1 ? splits [ 1 ] : null ;
542+ string pattern = splits . Length > 1 ? splits [ 1 ] : null ;
533543 string type = splits [ 0 ] . Substring ( 1 ) ;
534544
535545 if ( ! IsBraquetBalanced ( mod . name ) )
@@ -539,27 +549,33 @@ public void ApplyPatch(List<String> excludePaths, string Stage)
539549 continue ;
540550 }
541551
542- foreach ( UrlDir . UrlConfig url in GameDatabase . Instance . root . AllConfigs ) {
552+ foreach ( UrlDir . UrlConfig url in GameDatabase . Instance . root . AllConfigs )
553+ {
543554 if ( url . type == type
544555 && WildcardMatch ( url . name , pattern )
545556 && CheckCondition ( url . config , cond )
546557 && ! IsPathInList ( mod . url , excludePaths )
547- ) {
548- if ( mod . type [ 0 ] == '@' ) {
549- print ( "[ModuleManager] Applying node " + mod . url + " to " + url . url ) ;
550- patchCount ++ ;
551- url . config = ConfigManager . ModifyNode ( url . config , mod . config ) ;
552- }
553- else { // type = $
554- // Here we would duplicate an Node if we had the mean to do it
555- //ConfigNode newNode = ConfigManager.ModifyNode(url.config, mod.config);
556- //UrlDir.UrlConfig newurl = new UrlDir.UrlConfig(mod.parent, newNode);
557- //print("[ModuleManager] Copying Node " + newurl.url + " " + newurl.name);
558- }
558+ )
559+ {
560+ if ( mod . type [ 0 ] == '@' )
561+ {
562+ print ( "[ModuleManager] Applying node " + mod . url + " to " + url . url ) ;
563+ patchCount ++ ;
564+ url . config = ConfigManager . ModifyNode ( url . config , mod . config ) ;
565+ }
566+ else
567+ { // type = $
568+ // Here we would duplicate an Node if we had the mean to do it
569+ //ConfigNode newNode = ConfigManager.ModifyNode(url.config, mod.config);
570+ //UrlDir.UrlConfig newurl = new UrlDir.UrlConfig(mod.parent, newNode);
571+ //print("[ModuleManager] Copying Node " + newurl.url + " " + newurl.name);
572+ }
559573 }
560574 }
561575 }
562- } catch ( Exception e ) {
576+ }
577+ catch ( Exception e )
578+ {
563579 print ( "[ModuleManager] Exception while processing node : " + mod . url + "\n " + e . ToString ( ) ) ;
564580 }
565581 }
@@ -577,11 +593,14 @@ public static List<string> SplitCondition(string cond)
577593 List < string > conds = new List < string > ( ) ;
578594 int start = 0 ;
579595 int level = 0 ;
580- for ( int end = 0 ; end < cond . Length ; end ++ ) {
581- if ( cond [ end ] == ',' && level == 0 ) {
596+ for ( int end = 0 ; end < cond . Length ; end ++ )
597+ {
598+ if ( cond [ end ] == ',' && level == 0 )
599+ {
582600 conds . Add ( cond . Substring ( start , end - start ) ) ;
583601 start = end + 1 ;
584- } else if ( cond [ end ] == '[' )
602+ }
603+ else if ( cond [ end ] == '[' )
585604 level ++ ;
586605 else if ( cond [ end ] == ']' )
587606 level -- ;
@@ -597,13 +616,15 @@ public static bool CheckCondition(ConfigNode node, string conds)
597616
598617 List < string > condsList = SplitCondition ( conds ) ;
599618
600- if ( condsList . Count == 1 ) {
619+ if ( condsList . Count == 1 )
620+ {
601621 conds = condsList [ 0 ] ;
602622
603-
623+
604624
605625 string remainCond = "" ;
606- if ( conds . Contains ( "HAS[" ) ) {
626+ if ( conds . Contains ( "HAS[" ) )
627+ {
607628 int start = conds . IndexOf ( "HAS[" ) + 4 ;
608629 remainCond = conds . Substring ( start , condsList [ 0 ] . LastIndexOf ( ']' ) - start ) ;
609630 conds = conds . Substring ( 0 , start - 5 ) ;
@@ -614,27 +635,28 @@ public static bool CheckCondition(ConfigNode node, string conds)
614635 string type = splits [ 0 ] . Substring ( 1 ) ;
615636 string name = splits . Length > 1 ? splits [ 1 ] : null ;
616637
617- switch ( conds [ 0 ] ) {
618- case '@' :
619- case '!' :
620- // @MODULE[ModuleAlternator] or !MODULE[ModuleAlternator]
621- bool not = ( conds [ 0 ] == '!' ) ;
622- ConfigNode subNode = ConfigManager . FindConfigNodeIn ( node , type , name ) ;
623- if ( subNode != null )
624- return not ^ CheckCondition ( subNode , remainCond ) ;
625- return not ^ false ;
626- case '#' :
627- // #module[Winglet]
628- if ( node . HasValue ( type ) && node . GetValue ( type ) . Equals ( name ) )
629- return CheckCondition ( node , remainCond ) ;
630- return false ;
631- case '~' :
632- // ~breakingForce[] breakingForce is not present
633- if ( ! ( node . HasValue ( type ) ) )
634- return CheckCondition ( node , remainCond ) ;
635- return false ;
636- default :
637- return false ;
638+ switch ( conds [ 0 ] )
639+ {
640+ case '@' :
641+ case '!' :
642+ // @MODULE[ModuleAlternator] or !MODULE[ModuleAlternator]
643+ bool not = ( conds [ 0 ] == '!' ) ;
644+ ConfigNode subNode = ConfigManager . FindConfigNodeIn ( node , type , name ) ;
645+ if ( subNode != null )
646+ return not ^ CheckCondition ( subNode , remainCond ) ;
647+ return not ^ false ;
648+ case '#' :
649+ // #module[Winglet]
650+ if ( node . HasValue ( type ) && node . GetValue ( type ) . Equals ( name ) )
651+ return CheckCondition ( node , remainCond ) ;
652+ return false ;
653+ case '~' :
654+ // ~breakingForce[] breakingForce is not present
655+ if ( ! ( node . HasValue ( type ) ) )
656+ return CheckCondition ( node , remainCond ) ;
657+ return false ;
658+ default :
659+ return false ;
638660 }
639661 }
640662 return condsList . TrueForAll ( c => CheckCondition ( node , c ) ) ;
0 commit comments