From 1ffd1a3bff2dbbe4b9ca56d8ab9195b4e6ff5e43 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 1 Apr 2025 16:43:21 -0400 Subject: [PATCH] Look for files in places with things (fixes #19) --- NDecrypt/Program.cs | 70 ++++++++++++++++++++++++++++++--------------- README.md | 7 +++-- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/NDecrypt/Program.cs b/NDecrypt/Program.cs index 23b04dd..db63d63 100644 --- a/NDecrypt/Program.cs +++ b/NDecrypt/Program.cs @@ -227,16 +227,8 @@ More than one path can be specified at a time."); return config; } - // Derive the keyfile path based on the runtime folder if not already set - // TODO: Search in other directories by default - using var processModule = System.Diagnostics.Process.GetCurrentProcess().MainModule; - string applicationDirectory = Path.GetDirectoryName(processModule?.FileName) ?? string.Empty; - - // Use the proper default name - config = Path.Combine(applicationDirectory, "config.json"); - - // Only return the path if the file exists - return File.Exists(config) ? config : null; + // Derive the keyfile path, if possible + return GetFileLocation("config.json"); } /// @@ -252,19 +244,8 @@ More than one path can be specified at a time."); return keyfile; } - // Derive the keyfile path based on the runtime folder if not already set - // TODO: Search in other directories by default - using var processModule = System.Diagnostics.Process.GetCurrentProcess().MainModule; - string applicationDirectory = Path.GetDirectoryName(processModule?.FileName) ?? string.Empty; - - // Use the proper default name for the type - if (useAesKeysTxt) - keyfile = Path.Combine(applicationDirectory, "aes_keys.txt"); - else - keyfile = Path.Combine(applicationDirectory, "keys.bin"); - - // Only return the path if the file exists - return File.Exists(keyfile) ? keyfile : null; + // Derive the keyfile path, if possible + return GetFileLocation(useAesKeysTxt ? "aes_keys.txt" : "keys.bin"); } /// @@ -328,6 +309,49 @@ More than one path can be specified at a time."); return FileType.NULL; } + /// + /// Search for a file in local and config directories + /// + /// Filename to check in local and config directories + /// The full path to the file if found, null otherwise + /// + /// This method looks in the following locations: + /// - %HOME%/.config/ndecrypt + /// - Assembly location directory + /// - Process runtime directory + /// + private static string? GetFileLocation(string filename) + { + // User home directory +#if NET20 || NET35 + string homeDir = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); + homeDir = Path.Combine(Path.Combine(homeDir, ".config"), "ndecrypt"); +#else + string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + homeDir = Path.Combine(homeDir, ".config", "ndecrypt"); +#endif + if (File.Exists(Path.Combine(homeDir, filename))) + return Path.Combine(homeDir, filename); + + // Local directory +#if NET20 || NET35 || NET40 || NET452 + string runtimeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); +#else + string runtimeDir = AppContext.BaseDirectory; +#endif + if (File.Exists(Path.Combine(runtimeDir, filename))) + return Path.Combine(runtimeDir, filename); + + // Process directory + using var processModule = System.Diagnostics.Process.GetCurrentProcess().MainModule; + string applicationDirectory = Path.GetDirectoryName(processModule?.FileName) ?? string.Empty; + if (File.Exists(Path.Combine(applicationDirectory, filename))) + return Path.Combine(applicationDirectory, filename); + + // No file was found + return null; + } + /// /// Write out the hashes of a file to a named file /// diff --git a/README.md b/README.md index 335d352..a5bf8b4 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,12 @@ For the latest WIP build here: [Rolling Release](https://github.com/SabreTools/N can be any file or folder that contains uncompressed items. More than one path can be specified at a time. -**Note:** This overwrites the input files, so make backups if you're working on your original, personal dumps. +### Notes on Running -**Note:** Mixed folders or inputs are also accepted, you can decrypt or encrypt multiple files, regardless of their type. This being said, you can only do encrypt OR decrypt at one time. +- This overwrites the input files, so make backups if you're working on your original, personal dumps. +- Mixed folders or inputs are also accepted, you can decrypt or encrypt multiple files, regardless of their type. This being said, you can only do encrypt OR decrypt at one time. +- Required files will automatically be searched for in the application runtime directory as well as `%HOME%/.config/ndecrypt`. +- If found, `config.json` will take priority over both `keys.bin` and `aes_keys.txt`, even if `-a` and/or `-k` are defined. You've been warned. ## I feel like something is missing...