implemented setting the working directory by finding a data folder in an ancestor directory of the exe files path

This commit is contained in:
PS 2021-02-28 15:18:39 -08:00
parent 4765301c22
commit c7eb56724f
3 changed files with 84 additions and 4 deletions

View File

@ -446,6 +446,83 @@ ReloadAndLinkDLL(win32_dll_refresh* DLL, context* Context, gs_work_queue* WorkQu
return Success; return Success;
} }
internal gs_const_string
GetExePath(HINSTANCE HInstance, gs_thread_context ThreadContext)
{
gs_const_string Result = {};
u32 Error = 0;
u32 PathSize = MAX_PATH;
char* Path = PushArray(ThreadContext.Transient, char, PathSize);
DWORD Length = GetModuleFileNameA(HInstance, Path, PathSize);
if (Length)
{
Error = GetLastError();
if (Error == ERROR_INSUFFICIENT_BUFFER) {
// PathSize wasn't long enough
// TODO(pjs): handle this case
InvalidCodePath;
}
Result.Str = Path;
Result.Length = (u64)Length;
}
else
{
Error = GetLastError();
InvalidCodePath;
}
return Result;
}
internal bool
SetWorkingDirectory(HINSTANCE HInstance, gs_thread_context ThreadContext)
{
bool Result = false;
gs_const_string ExePath = GetExePath(HInstance, ThreadContext);
gs_string ScratchPath = PushString(ThreadContext.Transient, ExePath.Length + 128);
gs_string WorkingDirectory = PushString(ThreadContext.Transient, ExePath.Length + 128);
while (WorkingDirectory.Length == 0)
{
s64 LastSlash = FindLastFromSet(ExePath, "\\/");
if (LastSlash < 0) break;
ExePath = Substring(ExePath, 0, LastSlash);
PrintF(&ScratchPath, "%S\\data", ExePath);
NullTerminate(&ScratchPath);
gs_file_info PathInfo = GetFileInfo(ThreadContext.FileHandler, ScratchPath.ConstString);
if (PathInfo.Path.Length > 0 &&
PathInfo.IsDirectory) {
PrintF(&WorkingDirectory, "%S", ExePath);
NullTerminate(&WorkingDirectory);
}
}
if (WorkingDirectory.Length > 0)
{
OutputDebugStringA("Setting Working Directory\n");
OutputDebugStringA(WorkingDirectory.Str);
Result = SetCurrentDirectory(WorkingDirectory.Str);
if (!Result)
{
u32 Error = GetLastError();
InvalidCodePath;
}
}
else
{
OutputDebugStringA("Error, no data folder found\n");
}
return Result;
}
int WINAPI int WINAPI
WinMain ( WinMain (
HINSTANCE HInstance, HINSTANCE HInstance,
@ -456,6 +533,8 @@ WinMain (
{ {
gs_thread_context ThreadContext = Win32CreateThreadContext(); gs_thread_context ThreadContext = Win32CreateThreadContext();
if (!SetWorkingDirectory(HInstance, ThreadContext)) return 1;
MainWindow = Win32CreateWindow (HInstance, "Foldhaus", 1440, 768, HandleWindowEvents); MainWindow = Win32CreateWindow (HInstance, "Foldhaus", 1440, 768, HandleWindowEvents);
Win32UpdateWindowDimension(&MainWindow); Win32UpdateWindowDimension(&MainWindow);

View File

@ -197,7 +197,8 @@ Win32EnumerateDirectoryIntoTempList(gs_file_handler FileHandler, temp_file_list*
{ {
u32 FilesCount = 0; u32 FilesCount = 0;
u32 IndexOfLastSlash = FindLastFromSet(Path, "\\/"); s64 IndexOfLastSlash = FindLastFromSet(Path, "\\/");
Assert(IndexOfLastSlash >= 0);
gs_const_string SearchPath = Substring(Path, 0, IndexOfLastSlash + 1); gs_const_string SearchPath = Substring(Path, 0, IndexOfLastSlash + 1);
WIN32_FIND_DATA FindFileData; WIN32_FIND_DATA FindFileData;

View File

@ -1573,12 +1573,12 @@ FindFirstFromSet(gs_const_string String, char* SetArray)
return Result; return Result;
} }
internal u64 internal s64
FindLastFromSet(gs_const_string String, char* SetArray) FindLastFromSet(gs_const_string String, char* SetArray)
{ {
gs_const_string Set = ConstString(SetArray); gs_const_string Set = ConstString(SetArray);
u64 Result = String.Length - 1; s64 Result = -1;
for(s64 At = Result; At >= 0; At--) for(s64 At = String.Length - 1; At >= 0; At--)
{ {
char CharAt = String.Str[At]; char CharAt = String.Str[At];
for (u64 SetAt = 0; SetAt < Set.Length; SetAt++) for (u64 SetAt = 0; SetAt < Set.Length; SetAt++)