mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-02-03 21:29:23 +00:00
Cleanup
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
#if NETCOREAPP
|
||||
using System.Text.Json;
|
||||
#endif
|
||||
using BinaryObjectScanner;
|
||||
using SabreTools.CommandLine;
|
||||
using SabreTools.CommandLine.Inputs;
|
||||
@@ -255,18 +258,14 @@ namespace ProtectionScan.Features
|
||||
|
||||
try
|
||||
{
|
||||
var jsonSerializerOptions = new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
|
||||
var jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
|
||||
string serializedData;
|
||||
|
||||
if (Nested)
|
||||
{
|
||||
// A nested dictionary is used to achieve proper serialization.
|
||||
var nestedDictionary = new Dictionary<string, object>();
|
||||
var trimmedPath = path.TrimEnd(['\\', '/']);
|
||||
|
||||
// Move nested dictionary into final dictionary with the base path as a key.
|
||||
//var finalDictionary = new Dictionary<string, Dictionary<string, object>>();
|
||||
//finalDictionary.Add(trimmedPath, nestedDictionary);
|
||||
Dictionary<string, object> nestedDictionary = [];
|
||||
path = path.TrimEnd(['\\', '/']);
|
||||
|
||||
// Sort the keys for consistent output
|
||||
string[] keys = [.. protections.Keys];
|
||||
@@ -287,7 +286,7 @@ namespace ProtectionScan.Features
|
||||
Array.Sort(fileProtections);
|
||||
|
||||
// Inserts key and protections into nested dictionary, with the key trimmed of the base path.
|
||||
InsertNode(nestedDictionary, key[trimmedPath.Length..], trimmedPath, fileProtections, modifyNodeList);
|
||||
InsertNode(nestedDictionary, key, path, fileProtections, modifyNodeList);
|
||||
}
|
||||
|
||||
// Adds the non-leaf-node protections back in
|
||||
@@ -301,14 +300,14 @@ namespace ProtectionScan.Features
|
||||
|
||||
modifyNodeList[i].Item1[modifyNodeList[i].Item2] = modifyNode;
|
||||
}
|
||||
|
||||
|
||||
// Create the output data
|
||||
serializedData = System.Text.Json.JsonSerializer.Serialize(nestedDictionary, jsonSerializerOptions);
|
||||
serializedData = JsonSerializer.Serialize(nestedDictionary, jsonSerializerOptions);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create the output data
|
||||
serializedData = System.Text.Json.JsonSerializer.Serialize(protections, jsonSerializerOptions);
|
||||
serializedData = JsonSerializer.Serialize(protections, jsonSerializerOptions);
|
||||
}
|
||||
|
||||
// Write the output data
|
||||
@@ -327,31 +326,37 @@ namespace ProtectionScan.Features
|
||||
/// <summary>
|
||||
/// Inserts file protection dictionary entries into a nested dictionary based on path
|
||||
/// </summary>
|
||||
/// <param name="nestedDictionary">File or directory path</param>
|
||||
/// <param name="path">The "key" for the given protection entry, already trimmed of its base path</param>
|
||||
/// <param name="protections">The scanned protection(s) for a given file</param>
|
||||
private static void InsertNode(Dictionary<string, object> nestedDictionary,
|
||||
/// <param name="dict">Existing output dictionary</param>
|
||||
/// <param name="key">The key for the given protection entry</param>
|
||||
/// <param name="path">Original base path used for scanning</param>
|
||||
/// <param name="protections">Set of protections found, if any</param>
|
||||
/// <param name="modifyNodeList">Set representing overlapping nodes to be processed after</param>
|
||||
private static void InsertNode(Dictionary<string, object> dict,
|
||||
string key,
|
||||
string path,
|
||||
string fullPath,
|
||||
string[] protections,
|
||||
List<(Dictionary<string, object>, string, string[])> modifyNodeList)
|
||||
{
|
||||
var pathParts = path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (pathParts.Length <= 0)
|
||||
// Remove the base path from the key for processing
|
||||
key = key[path.Length..];
|
||||
|
||||
// Split the input path, if possible
|
||||
var pathParts = key.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (pathParts.Length == 0)
|
||||
{
|
||||
modifyNodeList.Add((nestedDictionary, fullPath, protections));
|
||||
modifyNodeList.Add((dict, path, protections));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nestedDictionary.ContainsKey(fullPath))
|
||||
nestedDictionary[fullPath] = new Dictionary<string, object>();
|
||||
|
||||
var current = (Dictionary<string, object>)nestedDictionary[fullPath];
|
||||
|
||||
// Create the node if it doesn't already exist
|
||||
if (!dict.ContainsKey(path))
|
||||
dict[path] = new Dictionary<string, object>();
|
||||
|
||||
var current = (Dictionary<string, object>)dict[path];
|
||||
|
||||
// Traverses the nested dictionary until the "leaf" dictionary is reached.
|
||||
for (int i = 0; i < pathParts.Length - 1; i++)
|
||||
{
|
||||
|
||||
var part = pathParts[i];
|
||||
|
||||
// Inserts new subdictionaries if one doesn't already exist
|
||||
@@ -376,23 +381,10 @@ namespace ProtectionScan.Features
|
||||
}
|
||||
|
||||
// If the "leaf" dictionary has been reached, add the file and its protections.
|
||||
if (current.ContainsKey(pathParts[^1]))
|
||||
if (current.ContainsKey(pathParts[^1]) && current[pathParts[^1]] is string[] existing)
|
||||
{
|
||||
var array1 = (string[])current[pathParts[^1]];
|
||||
var array2 = protections;
|
||||
|
||||
string[] result = new string[array1.Length + array2.Length];
|
||||
for (int i = 0; i < array1.Length; i++)
|
||||
{
|
||||
result[i] = array1[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < array2.Length; i++)
|
||||
{
|
||||
result[array1.Length + i] = array2[i];
|
||||
}
|
||||
|
||||
current[pathParts[^1]] = result;
|
||||
string[] combined = [.. existing, .. protections];
|
||||
current[pathParts[^1]] = combined;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user