diff --git a/src/utils/xrForms/cl_log.cpp b/src/utils/xrForms/cl_log.cpp index 9bb5ce1994..0c77f091df 100644 --- a/src/utils/xrForms/cl_log.cpp +++ b/src/utils/xrForms/cl_log.cpp @@ -116,6 +116,7 @@ void Phase(const char* phase_name) { csLog.Leave(); } +// TODO: windows specific stuff, dunno about Linux HWND logWindow=0; void logThread(void* dummy) { SetProcessPriorityBoost(GetCurrentProcess(), TRUE); diff --git a/src/xrCore/Platform/Linux/OSFile.h b/src/xrCore/Platform/Linux/OSFile.h index 7617873cf5..e21cd3bc42 100644 --- a/src/xrCore/Platform/Linux/OSFile.h +++ b/src/xrCore/Platform/Linux/OSFile.h @@ -151,7 +151,8 @@ namespace Platform return std::filesystem::path(std::string(result, count)).parent_path(); } - IC std::string GetModuleName() { + IC std::string GetModuleName() + { char result[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", result, PATH_MAX); if (count == -1) @@ -160,6 +161,36 @@ namespace Platform } return std::string(result, count); } + + IC std::string GetModuleNameForAddress(uintptr_t address) + { + Dl_info dl_info; + if (dladdr((void*)address, &dl_info) && dl_info.dli_fname) + { + return std::string(dl_info.dli_fname); + } + return {}; + } + + IC std::string GetUsrName() + { + char UserName[LOGIN_NAME_MAX]; + if (getlogin_r(UserName, sizeof(UserName)) == 0) + { + return std::string(UserName); + } + return {}; + } + + IC std::string GetCompName() + { + char CompName[HOST_NAME_MAX]; + if (gethostname(CompName, sizeof(CompName)) == 0) + { + return std::string(CompName); + } + return {}; + } IC bool OpenFileWnd(char* buffer, size_t sz_buf, FS_Path* P, int start_flt_ext, char flt[1024], LPCSTR offset, bool bMulti) { diff --git a/src/xrCore/Platform/Linux/PlatformInit.h b/src/xrCore/Platform/Linux/PlatformInit.h index f9155586a8..e5b31477d6 100644 --- a/src/xrCore/Platform/Linux/PlatformInit.h +++ b/src/xrCore/Platform/Linux/PlatformInit.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,6 +14,7 @@ #include #include +#include #ifdef IXR_ARM64 # include diff --git a/src/xrCore/Platform/Windows/OSFile.h b/src/xrCore/Platform/Windows/OSFile.h index a5619636bb..08bf732e55 100644 --- a/src/xrCore/Platform/Windows/OSFile.h +++ b/src/xrCore/Platform/Windows/OSFile.h @@ -40,12 +40,45 @@ namespace Platform } return std::string(ModuleName); } + + IC std::string GetModuleNameForAddress(uintptr_t address) + { + char formatBuff[MAX_PATH] = { 0 }; + HINSTANCE hModule = (HINSTANCE)SymGetModuleBase(GetCurrentProcess(), address); + if (hModule && GetModuleFileNameA(hModule, formatBuff, sizeof(formatBuff))) + { + return std::string(formatBuff); + } + return {}; + } IC const xr_special_char* ValidPath(const xr_special_char* In) { return In; } + IC std::string GetUsrName() + { + char UserName[UNLEN + 1]; + DWORD size = sizeof(UserName); + if (GetUserNameA(UserName, &size)) + { + return std::string(UserName); + } + return {}; + } + + IC std::string GetCompName() + { + char CompName[MAX_COMPUTERNAME_LENGTH + 1]; + DWORD size = sizeof(CompName); + if (GetComputerNameA(CompName, &size)) + { + return std::string(CompName); + } + return {}; + } + bool OpenFileWnd(char* buffer, size_t sz_buf, FS_Path* P, int start_flt_ext, char flt[1024], LPCSTR offset, bool bMulti); //bool SaveFileWnd(char* buffer, size_t sz_buf, FS_Path* P, int start_flt_ext, char flt[1024], LPCSTR offset); diff --git a/src/xrCore/Platform/Windows/PlatformInit.h b/src/xrCore/Platform/Windows/PlatformInit.h index 496043c5ce..966552a0fa 100644 --- a/src/xrCore/Platform/Windows/PlatformInit.h +++ b/src/xrCore/Platform/Windows/PlatformInit.h @@ -34,6 +34,8 @@ #pragma warning(push) #pragma warning(disable:4005) #include +#include +#include #ifdef IXR_ARM64 # include diff --git a/src/xrCore/StackTrace/StackTrace.h b/src/xrCore/StackTrace/StackTrace.h index af0233df54..63f39b4254 100644 --- a/src/xrCore/StackTrace/StackTrace.h +++ b/src/xrCore/StackTrace/StackTrace.h @@ -27,16 +27,16 @@ namespace StackTrace } frameStr.clear(); - string512 formatBuff; - //-' Module name: - HINSTANCE hModule = (HINSTANCE)SymGetModuleBase(GetCurrentProcess(), stackFrame->AddrPC.Offset); - if (hModule && GetModuleFileNameA(hModule, formatBuff, _countof(formatBuff))) + // Module name: + std::string moduleName = Platform::GetModuleNameForAddress(stackFrame->AddrPC.Offset); + if (!moduleName.empty()) { - frameStr.append(formatBuff); + frameStr.append(moduleName); } //-' Address: + string512 formatBuff; xr_sprintf(formatBuff, _countof(formatBuff), " at %p", stackFrame->AddrPC.Offset); frameStr.append(formatBuff); diff --git a/src/xrCore/xrCore.cpp b/src/xrCore/xrCore.cpp index 83eddc4981..34507d63e1 100644 --- a/src/xrCore/xrCore.cpp +++ b/src/xrCore/xrCore.cpp @@ -52,30 +52,18 @@ void xrCore::_initialize (LPCSTR _ApplicationName, xrLogger::LogCallback cb, BOO LoadParams(); #endif - string_path fn,dr,di; - // application path -#ifdef IXR_WINDOWS - GetModuleFileNameA(GetModuleHandleA(MODULE_NAME),fn,sizeof(fn)); - _splitpath (fn,dr,di,0,0); - xr_strconcat(ApplicationPath,dr,di); - - GetCurrentDirectoryA(sizeof(WorkingPath),WorkingPath); -#else - xr_strcpy(ApplicationPath, SDL_GetBasePath()); - xr_strcpy(WorkingPath, SDL_GetBasePath()); -#endif + std::string ApplicationPath = Platform::GetBinaryFolderPath().string(); + std::string WorkingPath = std::filesystem::current_path().string(); - xr_strcpy(g_application_path,sizeof(g_application_path),ApplicationPath); + xr_strcpy(g_application_path, sizeof(g_application_path), ApplicationPath.c_str()); // User/Comp Name -#ifdef IXR_WINDOWS - DWORD sz_user = sizeof(UserName); - GetUserNameA(UserName,&sz_user); + std::string user_name = Platform::GetUsrName(); + std::string comp_name = Platform::GetCompName(); - DWORD sz_comp = sizeof(CompName); - GetComputerNameA(CompName,&sz_comp); -#endif + xr_strcpy(UserName, sizeof(UserName), user_name.c_str()); + xr_strcpy(CompName, sizeof(CompName), comp_name.c_str()); // Mathematics & PSI detection CPU::Detect (); diff --git a/src/xrCore/xrDebug.cpp b/src/xrCore/xrDebug.cpp index f851309156..7c9adc7c29 100644 --- a/src/xrCore/xrDebug.cpp +++ b/src/xrCore/xrDebug.cpp @@ -25,7 +25,6 @@ static BOOL bException = FALSE; #define USE_OWN_ERROR_MESSAGE_WINDOW #ifdef IXR_WINDOWS -#include // MiniDump flags #include // for _set_new_mode #include // for signals #endif @@ -327,6 +326,7 @@ typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hF CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam ); +// TODO: windows specific stuff, Linux would require debugging tools and APIs like `libunwind`, `libbfd`, and `gdb`... void save_mini_dump (_EXCEPTION_POINTERS *pExceptionInfo) { // firstly see if dbghelp.dll is around and has the function we need diff --git a/src/xrEngine/EngineAPI.cpp b/src/xrEngine/EngineAPI.cpp index e639cb2c19..d83ace05e7 100644 --- a/src/xrEngine/EngineAPI.cpp +++ b/src/xrEngine/EngineAPI.cpp @@ -191,9 +191,7 @@ void CEngineAPI::CreateRendererList() } else { - char fullPath[MAX_PATH]{}; - GetModuleFileNameA(nullptr, fullPath, MAX_PATH); - auto dir = std::filesystem::weakly_canonical(fullPath).parent_path(); + auto dir = std::filesystem::weakly_canonical(Platform::GetBinaryFolderPath()); bSupports_r1 = std::filesystem::exists(dir / r1_name); bSupports_r2 = std::filesystem::exists(dir / r2_name); bSupports_r4 = std::filesystem::exists(dir / r4_name); diff --git a/src/xrGame/ui/UIMapList.cpp b/src/xrGame/ui/UIMapList.cpp index e90625e138..b0b13dd40f 100644 --- a/src/xrGame/ui/UIMapList.cpp +++ b/src/xrGame/ui/UIMapList.cpp @@ -67,14 +67,12 @@ CUIMapList::~CUIMapList() {} void CUIMapList::StartDedicatedServer(){ + std::string moduleFileName = Platform::GetModuleName(); - string_path ModuleFileName; - GetModuleFileNameA(nullptr, ModuleFileName, sizeof(ModuleFileName)); + std::filesystem::path moduleDir = std::filesystem::weakly_canonical(moduleFileName).parent_path(); + std::string moduleDirStr = moduleDir.string(); - char* ModuleName = nullptr; - GetFullPathNameA(ModuleFileName, sizeof(g_sLaunchWorkingFolder), g_sLaunchWorkingFolder, &ModuleName); - //removing module name from WorkingDirectory that contain full path... - ModuleName[0] = 0; + xr_strcpy(g_sLaunchWorkingFolder, moduleDirStr.c_str()); xr_strcpy (g_sLaunchOnExit_app, g_sLaunchWorkingFolder); xr_strcat (g_sLaunchOnExit_app, "dedicated\\xrEngine.exe"); diff --git a/src/xrGame/xrGameSpyServer.cpp b/src/xrGame/xrGameSpyServer.cpp index 2430f3c8e5..a5d9258a6b 100644 --- a/src/xrGame/xrGameSpyServer.cpp +++ b/src/xrGame/xrGameSpyServer.cpp @@ -60,10 +60,11 @@ xrGameSpyServer::EConnect xrGameSpyServer::Connect(shared_str &session_name, Gam if ( 0 == *(game->get_option_s (*session_name,"hname",nullptr))) { - string1024 CompName; - DWORD CompNameSize = 1024; - if (GetComputerNameA(CompName, &CompNameSize)) - HostName = CompName; + std::string CompName = Platform::GetCompName(); + if (!CompName.empty()) + { + HostName = CompName.c_str(); + } } else HostName = game->get_option_s (*session_name,"hname",nullptr);