From c7eb56724f4a0f33f621acccc58975cee44404f5 Mon Sep 17 00:00:00 2001 From: PS Date: Sun, 28 Feb 2021 15:18:39 -0800 Subject: [PATCH] implemented setting the working directory by finding a data folder in an ancestor directory of the exe files path --- src/app/platform_win32/win32_foldhaus.cpp | 79 +++++++++++++++++++ .../platform_win32/win32_foldhaus_fileio.h | 3 +- src/gs_libs/gs_types.cpp | 6 +- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/app/platform_win32/win32_foldhaus.cpp b/src/app/platform_win32/win32_foldhaus.cpp index c9208dc..df968c8 100644 --- a/src/app/platform_win32/win32_foldhaus.cpp +++ b/src/app/platform_win32/win32_foldhaus.cpp @@ -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); diff --git a/src/app/platform_win32/win32_foldhaus_fileio.h b/src/app/platform_win32/win32_foldhaus_fileio.h index bfb93ac..95163ea 100644 --- a/src/app/platform_win32/win32_foldhaus_fileio.h +++ b/src/app/platform_win32/win32_foldhaus_fileio.h @@ -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; diff --git a/src/gs_libs/gs_types.cpp b/src/gs_libs/gs_types.cpp index 611d868..197c270 100644 --- a/src/gs_libs/gs_types.cpp +++ b/src/gs_libs/gs_types.cpp @@ -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++)