implemented setting the working directory by finding a data folder in an ancestor directory of the exe files path
This commit is contained in:
parent
4765301c22
commit
c7eb56724f
|
@ -446,6 +446,83 @@ ReloadAndLinkDLL(win32_dll_refresh* DLL, context* Context, gs_work_queue* WorkQu
|
|||
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
|
||||
WinMain (
|
||||
HINSTANCE HInstance,
|
||||
|
@ -456,6 +533,8 @@ WinMain (
|
|||
{
|
||||
gs_thread_context ThreadContext = Win32CreateThreadContext();
|
||||
|
||||
if (!SetWorkingDirectory(HInstance, ThreadContext)) return 1;
|
||||
|
||||
MainWindow = Win32CreateWindow (HInstance, "Foldhaus", 1440, 768, HandleWindowEvents);
|
||||
Win32UpdateWindowDimension(&MainWindow);
|
||||
|
||||
|
|
|
@ -197,7 +197,8 @@ Win32EnumerateDirectoryIntoTempList(gs_file_handler FileHandler, temp_file_list*
|
|||
{
|
||||
u32 FilesCount = 0;
|
||||
|
||||
u32 IndexOfLastSlash = FindLastFromSet(Path, "\\/");
|
||||
s64 IndexOfLastSlash = FindLastFromSet(Path, "\\/");
|
||||
Assert(IndexOfLastSlash >= 0);
|
||||
gs_const_string SearchPath = Substring(Path, 0, IndexOfLastSlash + 1);
|
||||
|
||||
WIN32_FIND_DATA FindFileData;
|
||||
|
|
|
@ -1573,12 +1573,12 @@ FindFirstFromSet(gs_const_string String, char* SetArray)
|
|||
return Result;
|
||||
}
|
||||
|
||||
internal u64
|
||||
internal s64
|
||||
FindLastFromSet(gs_const_string String, char* SetArray)
|
||||
{
|
||||
gs_const_string Set = ConstString(SetArray);
|
||||
u64 Result = String.Length - 1;
|
||||
for(s64 At = Result; At >= 0; At--)
|
||||
s64 Result = -1;
|
||||
for(s64 At = String.Length - 1; At >= 0; At--)
|
||||
{
|
||||
char CharAt = String.Str[At];
|
||||
for (u64 SetAt = 0; SetAt < Set.Length; SetAt++)
|
||||
|
|
Loading…
Reference in New Issue