Skip to content

Commit

Permalink
Add platform-specific module and user info retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
ggGhosTt committed Nov 30, 2024
1 parent 8e5f70f commit fe925a7
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/utils/xrForms/cl_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion src/xrCore/Platform/Linux/OSFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/xrCore/Platform/Linux/PlatformInit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
#include <stdint.h>
#include <utime.h>
#include <pthread.h>
#include <dlfcn.h>

#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>

#include <linux/limits.h>
#include <bits/local_lim.h>

#ifdef IXR_ARM64
# include <arm64_neon.h>
Expand Down
33 changes: 33 additions & 0 deletions src/xrCore/Platform/Windows/OSFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/xrCore/Platform/Windows/PlatformInit.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#pragma warning(push)
#pragma warning(disable:4005)
#include <windows.h>
#include <lmcons.h>
#include <dbghelp.h>

#ifdef IXR_ARM64
# include <arm64_neon.h>
Expand Down
10 changes: 5 additions & 5 deletions src/xrCore/StackTrace/StackTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
26 changes: 7 additions & 19 deletions src/xrCore/xrCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
2 changes: 1 addition & 1 deletion src/xrCore/xrDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ static BOOL bException = FALSE;
#define USE_OWN_ERROR_MESSAGE_WINDOW

#ifdef IXR_WINDOWS
#include <dbghelp.h> // MiniDump flags
#include <new.h> // for _set_new_mode
#include <signal.h> // for signals
#endif
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/xrEngine/EngineAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions src/xrGame/ui/UIMapList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 5 additions & 4 deletions src/xrGame/xrGameSpyServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fe925a7

Please sign in to comment.