From 0012c7779c471fc7fd2d1e3b669ae115bcfcb7a2 Mon Sep 17 00:00:00 2001 From: s1lent Date: Sat, 18 Nov 2017 22:53:18 +0700 Subject: [PATCH] Refactoring --- metamod/src/callback_jit.cpp | 2 +- metamod/src/dllapi.cpp | 6 +++--- metamod/src/game_support.cpp | 2 +- metamod/src/mdebug.cpp | 2 +- metamod/src/mplugin.cpp | 13 +++++-------- metamod/src/mutil.cpp | 4 ++-- metamod/src/osdep.h | 2 -- metamod/src/sdk_util.cpp | 9 ++++----- metamod/src/sys_module.cpp | 2 +- metamod/src/utils.cpp | 10 ++++++---- 10 files changed, 24 insertions(+), 28 deletions(-) diff --git a/metamod/src/callback_jit.cpp b/metamod/src/callback_jit.cpp index bd088dd..f8fa792 100644 --- a/metamod/src/callback_jit.cpp +++ b/metamod/src/callback_jit.cpp @@ -346,7 +346,7 @@ size_t CJit::compile_callback(jitdata_t* jitdata) auto codeSize = callback.GetCodeSize(); auto ptr = m_callback_allocator.allocate(codeSize); - return (size_t)memcpy(ptr, code, codeSize); + return (size_t)Q_memcpy(ptr, code, codeSize); } size_t CJit::compile_tramp(size_t ptr_to_func) diff --git a/metamod/src/dllapi.cpp b/metamod/src/dllapi.cpp index f937510..4c23523 100644 --- a/metamod/src/dllapi.cpp +++ b/metamod/src/dllapi.cpp @@ -32,7 +32,7 @@ void MM_PRE_HOOK EXT_FUNC mm_ClientDisconnect(edict_t *pEntity) // this forward can be disabled from metamod.cpp void MM_PRE_HOOK mm_ClientCommand(edict_t *pEntity) { - if (!strcmp(CMD_ARGV(0), "meta")) { + if (!Q_strcmp(CMD_ARGV(0), "meta")) { client_meta(pEntity); } } @@ -184,7 +184,7 @@ C_DLLEXPORT int GetEntityAPI2(DLL_FUNCTIONS *pFunctionTable, int *interfaceVersi return FALSE; } - memcpy(pFunctionTable, &sFunctionTable, sizeof(DLL_FUNCTIONS)); + Q_memcpy(pFunctionTable, &sFunctionTable, sizeof(DLL_FUNCTIONS)); return TRUE; } @@ -211,7 +211,7 @@ C_DLLEXPORT int GetNewDLLFunctions(NEW_DLL_FUNCTIONS *pNewFunctionTable, int *in } g_meta_extdll.load(); - memcpy(pNewFunctionTable, &sNewFunctionTable, sizeof(NEW_DLL_FUNCTIONS)); + Q_memcpy(pNewFunctionTable, &sNewFunctionTable, sizeof(NEW_DLL_FUNCTIONS)); return TRUE; } diff --git a/metamod/src/game_support.cpp b/metamod/src/game_support.cpp index 4fad48b..27f8d75 100644 --- a/metamod/src/game_support.cpp +++ b/metamod/src/game_support.cpp @@ -50,7 +50,7 @@ bool lookup_game_postfixes(gamedll_t *gamedll) Q_strlcpy(pathname, gamedll->pathname); // find extensions and skip - char *pos = strrchr(pathname, '.'); + char *pos = Q_strrchr(pathname, '.'); if (pos) { *pos = '\0'; } diff --git a/metamod/src/mdebug.cpp b/metamod/src/mdebug.cpp index 2aa6c26..83eb30d 100644 --- a/metamod/src/mdebug.cpp +++ b/metamod/src/mdebug.cpp @@ -6,7 +6,7 @@ void mdebug_to_file(const char* fmt, ...) va_list argptr; va_start(argptr, fmt); - vsnprintf(buf, sizeof buf, fmt, argptr); + Q_vsnprintf(buf, sizeof buf, fmt, argptr); va_end(argptr); FILE* fp = fopen("mdebug.log", "a"); diff --git a/metamod/src/mplugin.cpp b/metamod/src/mplugin.cpp index 09a1c18..38769b7 100644 --- a/metamod/src/mplugin.cpp +++ b/metamod/src/mplugin.cpp @@ -53,8 +53,7 @@ MPlugin::~MPlugin() bool MPlugin::ini_parseline(char *line) { char buf[1024]; - strncpy(buf, line, sizeof buf - 1); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, line); // grab platform ("win32" or "linux") char* ptr_token; @@ -282,8 +281,7 @@ char *MPlugin::resolve_dirs(const char *path, char *tempbuf, size_t maxlen) cons // try other file prefixes in gamedir char buf[MAX_PATH]; - strncpy(buf, tempbuf, sizeof buf - 1); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, tempbuf); char* found = resolve_suffix(buf, tempbuf, maxlen); if (found) @@ -294,8 +292,7 @@ char *MPlugin::resolve_dirs(const char *path, char *tempbuf, size_t maxlen) cons if (is_valid_path(tempbuf)) return tempbuf; - strncpy(buf, tempbuf, sizeof buf - 1); - buf[sizeof buf - 1] = '\0'; + Q_strlcpy(buf, tempbuf); // try other file prefixes for this path return resolve_suffix(buf, tempbuf, maxlen); @@ -311,7 +308,7 @@ char *MPlugin::resolve_dirs(const char *path, char *tempbuf, size_t maxlen) cons char* MPlugin::resolve_suffix(const char *path, char *tempbuf, size_t bufsize) const { if (Q_strstr(path, PLATFORM_DLEXT) && is_valid_path(path)) { - strncpy(tempbuf, path, bufsize - 1); + Q_strncpy(tempbuf, path, bufsize - 1); tempbuf[bufsize - 1] = '\0'; return tempbuf; } @@ -1115,7 +1112,7 @@ bool MPlugin::newer_file() const return false; } - time_t file_time = max(st.st_ctime, st.st_mtime); + time_t file_time = Q_max(st.st_ctime, st.st_mtime); META_DEBUG(5, "newer_file? file=%s; load=%d, file=%d; ctime=%d, mtime=%d", m_file, m_time_loaded, file_time, st.st_ctime, st.st_mtime); if (file_time > m_time_loaded) return true; diff --git a/metamod/src/mutil.cpp b/metamod/src/mutil.cpp index 873b71f..5e4d214 100644 --- a/metamod/src/mutil.cpp +++ b/metamod/src/mutil.cpp @@ -200,7 +200,7 @@ int EXT_FUNC mutil_GetUserMsgID(plid_t plid, const char* msgname, int* size) } for (int n = 1; n < arraysize(g_engine_msg_names); n++) { - if (!strcmp(msgname, g_engine_msg_names[n])) { + if (!Q_strcmp(msgname, g_engine_msg_names[n])) { if (size) *size = -1; return n; } @@ -361,7 +361,7 @@ const char* EXT_FUNC mutil_IsQueryingClientCvar(plid_t plid, const edict_t* pEdi int EXT_FUNC mutil_MakeRequestId(plid_t plid) { //the offset is to distinguish from gamedll requests, if any - return abs(0xbeef << 16) + (++g_requestid_counter); + return Q_abs(0xbeef << 16) + (++g_requestid_counter); } void EXT_FUNC mutil_GetHookTables(plid_t plid, enginefuncs_t** peng, DLL_FUNCTIONS** pdll, NEW_DLL_FUNCTIONS** pnewdll) diff --git a/metamod/src/osdep.h b/metamod/src/osdep.h index 39621ee..748a9f5 100644 --- a/metamod/src/osdep.h +++ b/metamod/src/osdep.h @@ -27,8 +27,6 @@ #define O_BINARY 0 #endif #elif defined(_WIN32) - #define snprintf _snprintf - #define vsnprintf _vsnprintf #define stat64 _stat64 #define sleep(x) Sleep(x*1000) #include diff --git a/metamod/src/sdk_util.cpp b/metamod/src/sdk_util.cpp index 6c473f8..51a4dbc 100644 --- a/metamod/src/sdk_util.cpp +++ b/metamod/src/sdk_util.cpp @@ -14,12 +14,12 @@ char* UTIL_VarArgs(const char* format, ...) short FixedSigned16(float value, float scale) { - return (short)clamp(int(value * scale), SHRT_MIN, SHRT_MAX); + return (short)Q_clamp(int(value * scale), SHRT_MIN, SHRT_MAX); } unsigned short FixedUnsigned16(float value, float scale) { - return (unsigned short)clamp(int(value * scale), 0, USHRT_MAX); + return (unsigned short)Q_clamp(int(value * scale), 0, USHRT_MAX); } void UTIL_HudMessage(edict_t* pEntity, const hudtextparms_t& textparms, const char* pMessage) @@ -52,13 +52,12 @@ void UTIL_HudMessage(edict_t* pEntity, const hudtextparms_t& textparms, const ch if (textparms.effect == 2) WRITE_SHORT(FixedUnsigned16(textparms.fxTime, 1 << 8)); - if (strlen(pMessage) < 512) { + if (Q_strlen(pMessage) < 512) { WRITE_STRING(pMessage); } else { char tmp[512]; - strncpy(tmp, pMessage, sizeof tmp - 1); - tmp[sizeof tmp - 1] = '\0'; + Q_strlcpy(tmp, pMessage); WRITE_STRING(tmp); } MESSAGE_END(); diff --git a/metamod/src/sys_module.cpp b/metamod/src/sys_module.cpp index 9b22242..0ec0284 100644 --- a/metamod/src/sys_module.cpp +++ b/metamod/src/sys_module.cpp @@ -159,7 +159,7 @@ module_handle_t CSysModule::load(const char *filepath) m_handle = dlopen(filepath, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND); char buf[1024], dummy[1024], path[260]; - sprintf(buf, "/proc/%i/maps", getpid()); + Q_sprintf(buf, "/proc/%i/maps", getpid()); FILE* fp = fopen(buf, "r"); diff --git a/metamod/src/utils.cpp b/metamod/src/utils.cpp index 816049b..cc7337e 100644 --- a/metamod/src/utils.cpp +++ b/metamod/src/utils.cpp @@ -36,9 +36,9 @@ char* trimbuf(char* str) for (ibuf = str; *ibuf && (byte)(*ibuf) < (byte)0x80 && isspace(*ibuf); ++ibuf) ; - int i = strlen(ibuf); + int i = Q_strlen(ibuf); if (str != ibuf) - memmove(str, ibuf, i); + Q_memmove(str, ibuf, i); while (--i >= 0) { if (!isspace(str[i])) @@ -177,7 +177,9 @@ char* full_gamedir_path(const char* path, char* fullpath) if (is_abs_path(path)) { Q_strlcpy(buf, path); } - else snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path); + else { + Q_snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path); + } // Remove relative path components, if possible. if (!realpath(buf, fullpath)) { @@ -197,7 +199,7 @@ void NORETURN Sys_Error(const char *error, ...) static char text[1024]; va_start(argptr, error); - vsnprintf(text, sizeof(text), error, argptr); + Q_vsnprintf(text, sizeof(text), error, argptr); va_end(argptr); META_CONS("FATAL ERROR (shutting down): %s\n", text);