@@ -52,6 +52,28 @@ public static ConfigNode FindConfigNodeIn(ConfigNode src, string nodeType,
5252 }
5353 return null ;
5454 }
55+
56+ public static bool IsBraquetBalanced ( String str )
57+ {
58+ Stack < char > stack = new Stack < char > ( ) ;
59+
60+ char c ;
61+ for ( int i = 0 ; i < str . Length ; i ++ )
62+ {
63+ c = str [ i ] ;
64+ if ( c == '[' )
65+ stack . Push ( c ) ;
66+ else if ( c == ']' )
67+ if ( stack . Count == 0 )
68+ return false ;
69+ else if ( stack . Peek ( ) == '[' )
70+ stack . Pop ( ) ;
71+ else
72+ return false ;
73+ }
74+ return stack . Count == 0 ;
75+ }
76+
5577 // Added that to prevent a crash in KSP 'CopyTo' when a subnode has an empty name like
5678 // Like a pair of curly bracket without a name before them
5779 public static bool IsSane ( ConfigNode node )
@@ -149,6 +171,14 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
149171 foreach ( ConfigNode subMod in mod . nodes )
150172 {
151173 subMod . name = RemoveWS ( subMod . name ) ;
174+
175+ if ( ! IsBraquetBalanced ( subMod . name ) )
176+ {
177+ print ( "[ModuleManager] Skipping a patch subnode with unbalanced square brackets or a space (replace them with a '?') in " + mod . name + " : \n " + subMod . name + "\n " ) ;
178+ errorCount ++ ;
179+ continue ;
180+ }
181+
152182 char cmd = subMod . name [ 0 ] ;
153183 if ( cmd != '@' && cmd != '!' && cmd != '%' && cmd != '$' )
154184 newNode . AddNode ( subMod ) ;
@@ -293,7 +323,8 @@ public static ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
293323
294324 bool loaded = false ;
295325
296- int patchCount = 0 ;
326+ static int patchCount = 0 ;
327+ static int errorCount = 0 ;
297328 List < AssemblyName > mods ;
298329
299330 public void OnGUI ( )
@@ -314,7 +345,7 @@ public void OnGUI()
314345 }
315346 */
316347
317-
348+
318349
319350 if ( ! GameDatabase . Instance . IsReady ( ) && ( ( HighLogic . LoadedScene == GameScenes . MAINMENU ) || ( HighLogic . LoadedScene == GameScenes . SPACECENTER ) ) )
320351 {
@@ -324,6 +355,9 @@ public void OnGUI()
324355 if ( loaded )
325356 return ;
326357
358+ patchCount = 0 ;
359+ errorCount = 0 ;
360+
327361 // Check for old version and MMSarbianExt
328362 var oldMM = AssemblyLoader . loadedAssemblies . Where ( a => a . assembly . GetName ( ) . Name == Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ) . Where ( a => a . assembly . GetName ( ) . Version . CompareTo ( new System . Version ( 1 , 6 ) ) == - 1 ) ;
329363 var oldAssemblies = oldMM . Concat ( AssemblyLoader . loadedAssemblies . Where ( a => a . assembly . GetName ( ) . Name == "MMSarbianExt" ) ) ;
@@ -425,7 +459,7 @@ public void OnGUI()
425459 // :Final node
426460 ApplyPatch ( excludePaths , ":FINAL" ) ;
427461
428- print ( "[ModuleManager] Applied " + patchCount + " patches" ) ;
462+ print ( "[ModuleManager] Applied " + patchCount + " patches and found " + errorCount + " errors" ) ;
429463 loaded = true ;
430464
431465 }
@@ -496,7 +530,14 @@ public void ApplyPatch(List<String> excludePaths, string Stage)
496530
497531 string [ ] splits = name . Split ( sep , 3 ) ;
498532 string pattern = splits . Length > 1 ? splits [ 1 ] : null ;
499- string type = splits [ 0 ] . Substring ( 1 ) ;
533+ string type = splits [ 0 ] . Substring ( 1 ) ;
534+
535+ if ( ! IsBraquetBalanced ( mod . name ) )
536+ {
537+ print ( "[ModuleManager] Skipping a patch with unbalanced square brackets or a space (replace them with a '?') :\n " + mod . name + "\n " ) ;
538+ errorCount ++ ;
539+ continue ;
540+ }
500541
501542 foreach ( UrlDir . UrlConfig url in GameDatabase . Instance . root . AllConfigs ) {
502543 if ( url . type == type
0 commit comments