[*] Refactor AuProcess (paths)

This commit is contained in:
Reece Wilson 2023-12-13 18:11:35 +00:00
parent 98d79a5aa3
commit 1652231fd4
8 changed files with 117 additions and 81 deletions

View File

@ -10,11 +10,27 @@
namespace Aurora::Process
{
/**
Retrieves the applications current working directory
*/
* @brief Fetches the applications current working directory
* @param path
* @return
*/
AUKN_SYM bool GetWorkingDirectory(AuString &path);
/**
* @brief Fetches the applications binary or package directory
* @return
*/
AUKN_SYM const AuString *GetProcessDirectory();
AUKN_SYM bool GetProcName(AuString &executable);
AUKN_SYM bool GetProcDirectory(AuString &path);
AUKN_SYM bool GetProcFullPath(AuString &path);
/**
* @brief Fetches the full-path of the applications binary, if known
* @return
*/
AUKN_SYM const AuString *GetProcessFullPath();
/**
* @brief Fetches the applications primary module name
* @return
*/
AUKN_SYM const AuString *GetProcessName();
}

View File

@ -28,9 +28,9 @@ namespace Aurora::Console::ConsoleFIO
path += "/" + type + "/";
if (Process::GetProcName(procName))
if (auto pProcessName = Process::GetProcessName())
{
path += procName;
path += *pProcessName;
path.push_back('/');
}
else

View File

@ -867,9 +867,9 @@ namespace Aurora::Console::ConsoleTTY
void TTYConsole::HistorySetFile()
{
AuString path;
AuProcess::GetProcFullPath(path);
auto hash = AuFnv1a64Runtime(path.data(), path.size());
auto pProcPath = Process::GetProcessFullPath();
auto hash = AuFnv1a64Runtime(pProcPath ? pProcPath->data() : nullptr,
pProcPath ? pProcPath->size() : 0);
AuIOFS::NormalizePath(this->historyFileName, fmt::format("~/TTYHistory/{}.txt", hash));
}

View File

@ -330,20 +330,13 @@ namespace Aurora::Debug
static AuString GetDumpName()
{
AuString exeName;
try
{
Process::GetProcName(exeName);
}
catch (...)
{
}
try
{
auto pProcessName = Process::GetProcessName();
auto tm = Time::ToCivilTime(Time::CurrentClockMS());
return AuLocale::TimeDateToFileNameISO8601(tm) + ".dmp";
return (pProcessName ? *pProcessName : AuString()) +
" " +
AuLocale::TimeDateToFileNameISO8601(tm) + ".dmp";
}
catch (...)
{
@ -524,8 +517,8 @@ namespace Aurora::Debug
// It's aggressive in its approach, giving the users a choice to forcefully terminate as soon as they spam click a busy app,
// or never at all. latterly, its not uncommon for the app to not come back up, bc win32.
// It's too easy to trigger the watchdog and impossible to stop it from deciding the windowed application must die
AuString procName;
if (!Process::GetProcName(procName))
auto pProcName = Process::GetProcessName();
if (!pProcName)
{
AuLogWarn("Couldn't disable Microsoft Glowware Reporting!");
return;
@ -534,17 +527,18 @@ namespace Aurora::Debug
// Yes, this is prone to module-name conflict issues because it's a dumb registry utility function
// This isn't what we want, but it's probably what the user wants
// MSFT is such a loving company, they'll do almost nothing to ensure they wont steal all your data
WerAddExcludedApplication(AuLocale::ConvertFromUTF8(procName).c_str(), false);
WerAddExcludedApplication(AuLocale::ConvertFromUTF8(pProcName->data()).c_str(), false);
#else
// This implementation doesn't require a single IAT entry to a pointless dll
AuString procName;
if (!Process::GetProcName(procName))
auto pProcName = Process::GetProcessName();
if (!pProcName)
{
AuLogWarn("Couldn't disable Microsoft Glowware Reporting!");
return;
}
auto procNameWide = AuLocale::ConvertFromUTF8(procName);
auto procNameWide = AuLocale::ConvertFromUTF8(pProcName->c_str());
HKEY hKey;
if (pRegOpenKeyExW &&

View File

@ -57,7 +57,11 @@ namespace Aurora::IO::FS
// Binary directory
else if (path[iOffset] == '^')
{
if (!Process::GetProcDirectory(buffer))
if (auto pProcPath = Process::GetProcessDirectory())
{
buffer = *pProcPath;
}
else
{
result.clear();
return;
@ -233,7 +237,10 @@ namespace Aurora::IO::FS
if (str[0] == '^')
{
AuProcess::GetProcDirectory(str);
if (auto pProcPath = Process::GetProcessDirectory())
{
str = *pProcPath;
}
return;
}

View File

@ -80,10 +80,9 @@ namespace Aurora::IO::FS
}
{
AuString tempPath;
if (Process::GetProcDirectory(tempPath))
if (auto pProcPath = Process::GetProcessDirectory())
{
tempPath += "/" + fileName;
auto tempPath = *pProcPath + "/" + fileName;
if (FileExists(tempPath))
{
path = tempPath;
@ -92,6 +91,7 @@ namespace Aurora::IO::FS
}
}
{
auto systemPath = gHomeDirectory + fileName;
if (FileExists(systemPath))
@ -451,7 +451,13 @@ namespace Aurora::IO::FS
AUKN_SYM bool GetPackagePath(AuString &path)
{
// TODO: iOS/mac OS -> CFBundleCopyResourcesDirectoryURL
return Process::GetProcDirectory(path);
if (auto pProcessDirectory = Process::GetProcessDirectory())
{
path = *pProcessDirectory;
return true;
}
return false;
}
AUKN_SYM bool GetWritableAppdata(AuString &path)

View File

@ -171,7 +171,7 @@ namespace Aurora::Process
#endif
}
static bool GetModulePath(AuString &module, AuString &partialPath, AuString &fullPath)
static bool GetModulePath(const AuString *&module, const AuString *&partialPath, const AuString *&fullPath)
{
static AuString cachedModule, cachedPartialPath, cachedFullPath;
static bool init = false;
@ -214,77 +214,87 @@ namespace Aurora::Process
returnCached:
module = cachedModule;
fullPath = cachedFullPath;
partialPath = cachedPartialPath;
module = &cachedModule;
fullPath = &cachedFullPath;
partialPath = &cachedPartialPath;
return true;
}
AUKN_SYM bool GetProcName(AuString &executable)
AUKN_SYM const AuString *GetProcessName()
{
AuString module, partial, full;
const AuString *pModule, *pPartial, *pFull;
try
{
if (!GetModulePath(module, partial, full))
if (GetModulePath(pModule, pPartial, pFull) &&
pModule &&
pModule->size())
{
return false;
}
executable = module;
}
catch (...)
{
return false;
}
return true;
}
AUKN_SYM bool GetProcDirectory(AuString &path)
{
AuString module, partial, full;
try
{
if (!GetModulePath(module, partial, full))
{
return false;
}
path = partial;
if (path.empty())
{
return GetWorkingDirectory(path);
return pModule;
}
}
catch (...)
{
return false;
SysPushErrorCatch();
}
return true;
return nullptr;
}
AUKN_SYM bool GetProcFullPath(AuString &path)
AUKN_SYM const AuString *GetProcessDirectory()
{
AuString module, partial, full;
const AuString *pModule, *pPartial, *pFull;
try
{
if (!GetModulePath(module, partial, full))
if (GetModulePath(pModule, pPartial, pFull) &&
pPartial &&
pPartial->size())
{
return false;
return pPartial;
}
path = full;
{
static AuString cwd;
static AuInitOnce gInitOnce;
gInitOnce.Call([]()
{
GetWorkingDirectory(cwd);
});
if (cwd.size())
{
return &cwd;
}
}
}
catch (...)
{
return false;
SysPushErrorCatch();
}
return true;
return nullptr;
}
AUKN_SYM const AuString *GetProcessFullPath()
{
const AuString *pModule, *pPartial, *pFull;
try
{
if (GetModulePath(pModule, pPartial, pFull) &&
pFull &&
pFull->size())
{
return pFull;
}
}
catch (...)
{
SysPushErrorCatch();
}
return nullptr;
}
}

View File

@ -483,12 +483,15 @@ namespace Aurora::Process
}
case EModulePath::eProcessDirectory:
{
if (!Process::GetProcDirectory(pathA))
if (auto pProcPath = Process::GetProcessDirectory())
{
pathA = *pProcPath;
break;
}
else
{
return {};
}
break;
}
case EModulePath::eModulePathSystemDir:
{