11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4+ #if NETCOREAPP
5+ using System . Text . Json ;
6+ #endif
47using BinaryObjectScanner ;
58using SabreTools . CommandLine ;
69using SabreTools . CommandLine . Inputs ;
@@ -255,18 +258,14 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
255258
256259 try
257260 {
258- var jsonSerializerOptions = new System . Text . Json . JsonSerializerOptions { WriteIndented = true } ;
261+ var jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true } ;
259262 string serializedData ;
260263
261264 if ( Nested )
262265 {
263266 // A nested dictionary is used to achieve proper serialization.
264- var nestedDictionary = new Dictionary < string , object > ( ) ;
265- var trimmedPath = path . TrimEnd ( [ '\\ ' , '/' ] ) ;
266-
267- // Move nested dictionary into final dictionary with the base path as a key.
268- //var finalDictionary = new Dictionary<string, Dictionary<string, object>>();
269- //finalDictionary.Add(trimmedPath, nestedDictionary);
267+ Dictionary < string , object > nestedDictionary = [ ] ;
268+ path = path . TrimEnd ( [ '\\ ' , '/' ] ) ;
270269
271270 // Sort the keys for consistent output
272271 string [ ] keys = [ .. protections . Keys ] ;
@@ -287,7 +286,7 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
287286 Array . Sort ( fileProtections ) ;
288287
289288 // Inserts key and protections into nested dictionary, with the key trimmed of the base path.
290- InsertNode ( nestedDictionary , key [ trimmedPath . Length .. ] , trimmedPath , fileProtections , modifyNodeList ) ;
289+ InsertNode ( nestedDictionary , key , path , fileProtections , modifyNodeList ) ;
291290 }
292291
293292 // Adds the non-leaf-node protections back in
@@ -301,14 +300,14 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
301300
302301 modifyNodeList [ i ] . Item1 [ modifyNodeList [ i ] . Item2 ] = modifyNode ;
303302 }
304-
303+
305304 // Create the output data
306- serializedData = System . Text . Json . JsonSerializer . Serialize ( nestedDictionary , jsonSerializerOptions ) ;
305+ serializedData = JsonSerializer . Serialize ( nestedDictionary , jsonSerializerOptions ) ;
307306 }
308307 else
309308 {
310309 // Create the output data
311- serializedData = System . Text . Json . JsonSerializer . Serialize ( protections , jsonSerializerOptions ) ;
310+ serializedData = JsonSerializer . Serialize ( protections , jsonSerializerOptions ) ;
312311 }
313312
314313 // Write the output data
@@ -327,31 +326,37 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
327326 /// <summary>
328327 /// Inserts file protection dictionary entries into a nested dictionary based on path
329328 /// </summary>
330- /// <param name="nestedDictionary">File or directory path</param>
331- /// <param name="path">The "key" for the given protection entry, already trimmed of its base path</param>
332- /// <param name="protections">The scanned protection(s) for a given file</param>
333- private static void InsertNode ( Dictionary < string , object > nestedDictionary ,
329+ /// <param name="dict">Existing output dictionary</param>
330+ /// <param name="key">The key for the given protection entry</param>
331+ /// <param name="path">Original base path used for scanning</param>
332+ /// <param name="protections">Set of protections found, if any</param>
333+ /// <param name="modifyNodeList">Set representing overlapping nodes to be processed after</param>
334+ private static void InsertNode ( Dictionary < string , object > dict ,
335+ string key ,
334336 string path ,
335- string fullPath ,
336337 string [ ] protections ,
337338 List < ( Dictionary < string , object > , string , string [ ] ) > modifyNodeList )
338339 {
339- var pathParts = path . Split ( Path . DirectorySeparatorChar , StringSplitOptions . RemoveEmptyEntries ) ;
340- if ( pathParts . Length <= 0 )
340+ // Remove the base path from the key for processing
341+ key = key [ path . Length ..] ;
342+
343+ // Split the input path, if possible
344+ var pathParts = key . Split ( Path . DirectorySeparatorChar , StringSplitOptions . RemoveEmptyEntries ) ;
345+ if ( pathParts . Length == 0 )
341346 {
342- modifyNodeList . Add ( ( nestedDictionary , fullPath , protections ) ) ;
347+ modifyNodeList . Add ( ( dict , path , protections ) ) ;
343348 return ;
344349 }
345-
346- if ( ! nestedDictionary . ContainsKey ( fullPath ) )
347- nestedDictionary [ fullPath ] = new Dictionary < string , object > ( ) ;
348350
349- var current = ( Dictionary < string , object > ) nestedDictionary [ fullPath ] ;
350-
351+ // Create the node if it doesn't already exist
352+ if ( ! dict . ContainsKey ( path ) )
353+ dict [ path ] = new Dictionary < string , object > ( ) ;
354+
355+ var current = ( Dictionary < string , object > ) dict [ path ] ;
356+
351357 // Traverses the nested dictionary until the "leaf" dictionary is reached.
352358 for ( int i = 0 ; i < pathParts . Length - 1 ; i ++ )
353359 {
354-
355360 var part = pathParts [ i ] ;
356361
357362 // Inserts new subdictionaries if one doesn't already exist
@@ -376,23 +381,10 @@ private static void InsertNode(Dictionary<string, object> nestedDictionary,
376381 }
377382
378383 // If the "leaf" dictionary has been reached, add the file and its protections.
379- if ( current . ContainsKey ( pathParts [ ^ 1 ] ) )
384+ if ( current . ContainsKey ( pathParts [ ^ 1 ] ) && current [ pathParts [ ^ 1 ] ] is string [ ] existing )
380385 {
381- var array1 = ( string [ ] ) current [ pathParts [ ^ 1 ] ] ;
382- var array2 = protections ;
383-
384- string [ ] result = new string [ array1 . Length + array2 . Length ] ;
385- for ( int i = 0 ; i < array1 . Length ; i ++ )
386- {
387- result [ i ] = array1 [ i ] ;
388- }
389-
390- for ( int i = 0 ; i < array2 . Length ; i ++ )
391- {
392- result [ array1 . Length + i ] = array2 [ i ] ;
393- }
394-
395- current [ pathParts [ ^ 1 ] ] = result ;
386+ string [ ] combined = [ .. existing , .. protections ] ;
387+ current [ pathParts [ ^ 1 ] ] = combined ;
396388 }
397389 else
398390 {
0 commit comments