Skip to content

Commit fe925a7

Browse files
committed
Add platform-specific module and user info retrieval
1 parent 8e5f70f commit fe925a7

File tree

11 files changed

+93
-39
lines changed

11 files changed

+93
-39
lines changed

src/utils/xrForms/cl_log.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ void Phase(const char* phase_name) {
116116
csLog.Leave();
117117
}
118118

119+
// TODO: windows specific stuff, dunno about Linux
119120
HWND logWindow=0;
120121
void logThread(void* dummy) {
121122
SetProcessPriorityBoost(GetCurrentProcess(), TRUE);

src/xrCore/Platform/Linux/OSFile.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ namespace Platform
151151
return std::filesystem::path(std::string(result, count)).parent_path();
152152
}
153153

154-
IC std::string GetModuleName() {
154+
IC std::string GetModuleName()
155+
{
155156
char result[PATH_MAX];
156157
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
157158
if (count == -1)
@@ -160,6 +161,36 @@ namespace Platform
160161
}
161162
return std::string(result, count);
162163
}
164+
165+
IC std::string GetModuleNameForAddress(uintptr_t address)
166+
{
167+
Dl_info dl_info;
168+
if (dladdr((void*)address, &dl_info) && dl_info.dli_fname)
169+
{
170+
return std::string(dl_info.dli_fname);
171+
}
172+
return {};
173+
}
174+
175+
IC std::string GetUsrName()
176+
{
177+
char UserName[LOGIN_NAME_MAX];
178+
if (getlogin_r(UserName, sizeof(UserName)) == 0)
179+
{
180+
return std::string(UserName);
181+
}
182+
return {};
183+
}
184+
185+
IC std::string GetCompName()
186+
{
187+
char CompName[HOST_NAME_MAX];
188+
if (gethostname(CompName, sizeof(CompName)) == 0)
189+
{
190+
return std::string(CompName);
191+
}
192+
return {};
193+
}
163194

164195
IC bool OpenFileWnd(char* buffer, size_t sz_buf, FS_Path* P, int start_flt_ext, char flt[1024], LPCSTR offset, bool bMulti)
165196
{

src/xrCore/Platform/Linux/PlatformInit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <stdint.h>
77
#include <utime.h>
88
#include <pthread.h>
9+
#include <dlfcn.h>
910

1011
#include <sys/stat.h>
1112
#include <sys/mman.h>
1213
#include <sys/syscall.h>
1314
#include <sys/types.h>
1415

1516
#include <linux/limits.h>
17+
#include <bits/local_lim.h>
1618

1719
#ifdef IXR_ARM64
1820
# include <arm64_neon.h>

src/xrCore/Platform/Windows/OSFile.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,45 @@ namespace Platform
4040
}
4141
return std::string(ModuleName);
4242
}
43+
44+
IC std::string GetModuleNameForAddress(uintptr_t address)
45+
{
46+
char formatBuff[MAX_PATH] = { 0 };
47+
HINSTANCE hModule = (HINSTANCE)SymGetModuleBase(GetCurrentProcess(), address);
48+
if (hModule && GetModuleFileNameA(hModule, formatBuff, sizeof(formatBuff)))
49+
{
50+
return std::string(formatBuff);
51+
}
52+
return {};
53+
}
4354

4455
IC const xr_special_char* ValidPath(const xr_special_char* In)
4556
{
4657
return In;
4758
}
4859

60+
IC std::string GetUsrName()
61+
{
62+
char UserName[UNLEN + 1];
63+
DWORD size = sizeof(UserName);
64+
if (GetUserNameA(UserName, &size))
65+
{
66+
return std::string(UserName);
67+
}
68+
return {};
69+
}
70+
71+
IC std::string GetCompName()
72+
{
73+
char CompName[MAX_COMPUTERNAME_LENGTH + 1];
74+
DWORD size = sizeof(CompName);
75+
if (GetComputerNameA(CompName, &size))
76+
{
77+
return std::string(CompName);
78+
}
79+
return {};
80+
}
81+
4982
bool OpenFileWnd(char* buffer, size_t sz_buf, FS_Path* P, int start_flt_ext, char flt[1024], LPCSTR offset, bool bMulti);
5083
//bool SaveFileWnd(char* buffer, size_t sz_buf, FS_Path* P, int start_flt_ext, char flt[1024], LPCSTR offset);
5184

src/xrCore/Platform/Windows/PlatformInit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#pragma warning(push)
3535
#pragma warning(disable:4005)
3636
#include <windows.h>
37+
#include <lmcons.h>
38+
#include <dbghelp.h>
3739

3840
#ifdef IXR_ARM64
3941
# include <arm64_neon.h>

src/xrCore/StackTrace/StackTrace.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ namespace StackTrace
2727
}
2828

2929
frameStr.clear();
30-
string512 formatBuff;
3130

32-
//-' Module name:
33-
HINSTANCE hModule = (HINSTANCE)SymGetModuleBase(GetCurrentProcess(), stackFrame->AddrPC.Offset);
34-
if (hModule && GetModuleFileNameA(hModule, formatBuff, _countof(formatBuff)))
31+
// Module name:
32+
std::string moduleName = Platform::GetModuleNameForAddress(stackFrame->AddrPC.Offset);
33+
if (!moduleName.empty())
3534
{
36-
frameStr.append(formatBuff);
35+
frameStr.append(moduleName);
3736
}
3837

3938
//-' Address:
39+
string512 formatBuff;
4040
xr_sprintf(formatBuff, _countof(formatBuff), " at %p", stackFrame->AddrPC.Offset);
4141
frameStr.append(formatBuff);
4242

src/xrCore/xrCore.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,18 @@ void xrCore::_initialize (LPCSTR _ApplicationName, xrLogger::LogCallback cb, BOO
5252
LoadParams();
5353
#endif
5454

55-
string_path fn,dr,di;
56-
5755
// application path
58-
#ifdef IXR_WINDOWS
59-
GetModuleFileNameA(GetModuleHandleA(MODULE_NAME),fn,sizeof(fn));
60-
_splitpath (fn,dr,di,0,0);
61-
xr_strconcat(ApplicationPath,dr,di);
62-
63-
GetCurrentDirectoryA(sizeof(WorkingPath),WorkingPath);
64-
#else
65-
xr_strcpy(ApplicationPath, SDL_GetBasePath());
66-
xr_strcpy(WorkingPath, SDL_GetBasePath());
67-
#endif
56+
std::string ApplicationPath = Platform::GetBinaryFolderPath().string();
57+
std::string WorkingPath = std::filesystem::current_path().string();
6858

69-
xr_strcpy(g_application_path,sizeof(g_application_path),ApplicationPath);
59+
xr_strcpy(g_application_path, sizeof(g_application_path), ApplicationPath.c_str());
7060

7161
// User/Comp Name
72-
#ifdef IXR_WINDOWS
73-
DWORD sz_user = sizeof(UserName);
74-
GetUserNameA(UserName,&sz_user);
62+
std::string user_name = Platform::GetUsrName();
63+
std::string comp_name = Platform::GetCompName();
7564

76-
DWORD sz_comp = sizeof(CompName);
77-
GetComputerNameA(CompName,&sz_comp);
78-
#endif
65+
xr_strcpy(UserName, sizeof(UserName), user_name.c_str());
66+
xr_strcpy(CompName, sizeof(CompName), comp_name.c_str());
7967

8068
// Mathematics & PSI detection
8169
CPU::Detect ();

src/xrCore/xrDebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ static BOOL bException = FALSE;
2525
#define USE_OWN_ERROR_MESSAGE_WINDOW
2626

2727
#ifdef IXR_WINDOWS
28-
#include <dbghelp.h> // MiniDump flags
2928
#include <new.h> // for _set_new_mode
3029
#include <signal.h> // for signals
3130
#endif
@@ -327,6 +326,7 @@ typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hF
327326
CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam
328327
);
329328

329+
// TODO: windows specific stuff, Linux would require debugging tools and APIs like `libunwind`, `libbfd`, and `gdb`...
330330
void save_mini_dump (_EXCEPTION_POINTERS *pExceptionInfo)
331331
{
332332
// firstly see if dbghelp.dll is around and has the function we need

src/xrEngine/EngineAPI.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ void CEngineAPI::CreateRendererList()
191191
}
192192
else
193193
{
194-
char fullPath[MAX_PATH]{};
195-
GetModuleFileNameA(nullptr, fullPath, MAX_PATH);
196-
auto dir = std::filesystem::weakly_canonical(fullPath).parent_path();
194+
auto dir = std::filesystem::weakly_canonical(Platform::GetBinaryFolderPath());
197195
bSupports_r1 = std::filesystem::exists(dir / r1_name);
198196
bSupports_r2 = std::filesystem::exists(dir / r2_name);
199197
bSupports_r4 = std::filesystem::exists(dir / r4_name);

src/xrGame/ui/UIMapList.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@ CUIMapList::~CUIMapList()
6767
{}
6868

6969
void CUIMapList::StartDedicatedServer(){
70+
std::string moduleFileName = Platform::GetModuleName();
7071

71-
string_path ModuleFileName;
72-
GetModuleFileNameA(nullptr, ModuleFileName, sizeof(ModuleFileName));
72+
std::filesystem::path moduleDir = std::filesystem::weakly_canonical(moduleFileName).parent_path();
73+
std::string moduleDirStr = moduleDir.string();
7374

74-
char* ModuleName = nullptr;
75-
GetFullPathNameA(ModuleFileName, sizeof(g_sLaunchWorkingFolder), g_sLaunchWorkingFolder, &ModuleName);
76-
//removing module name from WorkingDirectory that contain full path...
77-
ModuleName[0] = 0;
75+
xr_strcpy(g_sLaunchWorkingFolder, moduleDirStr.c_str());
7876

7977
xr_strcpy (g_sLaunchOnExit_app, g_sLaunchWorkingFolder);
8078
xr_strcat (g_sLaunchOnExit_app, "dedicated\\xrEngine.exe");

0 commit comments

Comments
 (0)