2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-03-25 19:59:14 +03:00

117 lines
2.8 KiB
C
Raw Normal View History

2016-07-26 23:31:47 +07:00
#pragma once
2016-07-04 12:07:29 +06:00
// String describing platform/DLL-type, for matching lines in plugins.ini.
2016-07-26 07:22:47 +07:00
#ifdef _WIN32
2016-07-26 23:31:47 +07:00
#define UNUSED /**/
2016-07-26 23:31:47 +07:00
#define PLATFORM "mswin"
#define PLATFORM_SPC "win32"
#define PLATFORM_DLEXT ".dll"
2017-01-09 02:44:49 +03:00
#define likely(x) (x)
#define unlikely(x) (x)
2016-07-26 23:31:47 +07:00
#else
#define UNUSED __attribute__((unused))
2016-07-26 23:31:47 +07:00
#define PLATFORM "linux"
#define PLATFORM_SPC "lin32"
#define PLATFORM_DLEXT ".so"
2017-01-09 02:44:49 +03:00
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
2016-07-26 23:31:47 +07:00
#endif
2016-07-26 23:31:47 +07:00
#ifdef _WIN32
2017-01-07 01:24:40 +03:00
typedef HINSTANCE module_handle_t;
2016-07-26 23:31:47 +07:00
#else
2017-01-07 01:24:40 +03:00
typedef void* module_handle_t;
2016-07-26 23:31:47 +07:00
#endif
2017-01-07 01:24:40 +03:00
class CSysModule
{
public:
CSysModule();
module_handle_t load(const char* filename);
bool unload();
void* getsym(const char* name) const;
module_handle_t gethandle() const;
bool contain(void* addr) const;
static const char* getloaderror();
private:
module_handle_t m_handle;
uintptr_t m_base;
uintptr_t m_size;
};
2016-07-04 12:07:29 +06:00
// Windows doesn't have an strtok_r() routine, so we write our own.
#ifdef _WIN32
2017-01-07 01:24:40 +03:00
#define strtok_r(s, delim, ptrptr) mm_strtok_r(s, delim, ptrptr)
2016-07-26 23:31:47 +07:00
#endif // _WIN32
2016-07-04 12:07:29 +06:00
// Set filename and pathname maximum lengths. Note some windows compilers
// provide a <limits.h> which is incomplete and/or causes problems; see
// doc/windows_notes.txt for more information.
//
// Note that both OS's include room for null-termination:
// linux: "# chars in a path name including nul"
// win32: "note that the sizes include space for 0-terminator"
#if defined(__linux) || defined(__APPLE__)
2016-07-04 12:07:29 +06:00
#include <limits.h>
#elif defined(_WIN32)
#include <stdlib.h>
#define NAME_MAX _MAX_FNAME
#define PATH_MAX _MAX_PATH
2016-07-26 23:31:47 +07:00
#endif // _WIN32
2016-07-04 12:07:29 +06:00
// Various other windows routine differences.
#if defined(__linux) || defined(__APPLE__)
2016-07-04 12:07:29 +06:00
#include <unistd.h> // sleep
#ifndef O_BINARY
#define O_BINARY 0
2016-07-26 23:31:47 +07:00
#endif
2016-07-04 12:07:29 +06:00
#elif defined(_WIN32)
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define sleep(x) Sleep(x*1000)
#include <io.h>
#define open _open
#define read _read
#define write _write
#define close _close
2016-07-26 23:31:47 +07:00
#endif // _WIN32
#ifdef __GNUC__
#include <unistd.h> // _getcwd
#elif defined(_MSC_VER)
#include <direct.h> // _getcwd
#endif /* _MSC_VER */
2016-07-04 12:07:29 +06:00
#include <sys/stat.h>
#ifndef S_ISREG
// Linux gcc defines this; earlier mingw didn't, later mingw does;
// MSVC doesn't seem to.
#define S_ISREG(m) ((m) & S_IFREG)
#endif /* not S_ISREG */
#ifdef _WIN32
// The following two are defined in mingw but not in MSVC
#ifndef S_IRUSR
#define S_IRUSR _S_IREAD
#endif
#ifndef S_IWUSR
#define S_IWUSR _S_IWRITE
#endif
2016-07-26 23:31:47 +07:00
2016-07-04 12:07:29 +06:00
// The following two are defined neither in mingw nor in MSVC
#ifndef S_IRGRP
#define S_IRGRP S_IRUSR
#endif
#ifndef S_IWGRP
#define S_IWGRP S_IWUSR
#endif
2016-07-26 23:31:47 +07:00
2017-01-07 01:24:40 +03:00
const char *str_GetLastError();
2016-07-26 23:31:47 +07:00
#endif // _WIN32
2016-07-04 12:07:29 +06:00
2017-01-07 01:24:40 +03:00
const char* str_os_error();