amxmodx/amxmodx/CLibrarySys.h
Arkshine 52c73126e1 VFS: Fix various things
- The "ALL" fake pathID is replaced by what does SM, having a public var NULL_STRING which will acts as NULL when needed.
  To make compiler accepting public array, this patch was needed: https://hg.alliedmods.net/sourcemod-central/rev/b12f329def09
- The offset thing in read_dir: considering that's something very specific to this native and that implementation in CDirectory doesn't make sense because of the offset compatibility for windows, all code is now in the native.
2015-03-25 13:50:07 +01:00

183 lines
4.1 KiB
C++

// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
#ifndef _INCLUDE_LIBRARY_SYS_H_
#define _INCLUDE_LIBRARY_SYS_H_
#include "amx.h" // cell
#include <interface.h> // Interface (HLSDK)
#include <am-utility.h> // AutoPtr
#if defined(WIN32)
# ifndef PLATFORM_WINDOWS
# define PLATFORM_WINDOWS 1
# endif
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# include <direct.h>
# include <io.h>
# define PLATFORM_LIB_EXT "dll"
# define PLATFORM_SEP_CHAR '\\'
# define PLATFORM_SEP_ALTCHAR '/'
# define PLATFORM_EXTERN_C extern "C" __declspec(dllexport)
#elif defined(__linux__) || defined(__APPLE__)
# if defined(__linux__)
# define PLATFORM_LINUX 1
# define PLATFORM_LIB_EXT "so"
# elif defined(__APPLE__)
# define PLATFORM_APPLE 1
# define PLATFORM_LIB_EXT "dylib"
# endif
# ifndef PLATFORM_POSIX
# define PLATFORM_POSIX 1
# endif
# include <stdio.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <errno.h>
# include <unistd.h>
# include <dirent.h>
# include <dlfcn.h>
# if defined(PLATFORM_APPLE)
# include <sys/syslimits.h>
# endif
# define PLATFORM_SEP_CHAR '/'
# define PLATFORM_SEP_ALTCHAR '\\'
# define PLATFORM_EXTERN_C extern "C" __attribute__((visibility("default")))
# define WINAPI
#endif
#define PLATFORM_MAX_PATH 260
#if defined PLATFORM_WINDOWS
typedef HMODULE LibraryHandle;
typedef HANDLE DirHandle;
#elif defined PLATFORM_POSIX
typedef void* LibraryHandle;
typedef DIR* DirHandle;
#endif
enum FileTimeType
{
FileTime_LastAccess = 0, /* Last access (not available on FAT) */
FileTime_Created = 1, /* Creation (not available on FAT) */
FileTime_LastChange = 2, /* Last modification */
};
class CDirectory
{
public:
CDirectory(const char* path);
~CDirectory();
public:
bool MoreFiles();
void NextEntry();
const char* GetEntryName();
bool IsEntryDirectory();
bool IsEntryFile();
bool IsEntryValid();
public:
bool IsValid();
DirHandle GetHandle();
private:
#if defined PLATFORM_WINDOWS
HANDLE m_dir;
WIN32_FIND_DATAA m_fd;
#elif defined PLATFORM_POSIX
DIR* m_dir;
struct dirent* m_ep;
char m_origpath[PLATFORM_MAX_PATH];
#endif
};
class CLibrary
{
public:
CLibrary(LibraryHandle me);
~CLibrary();
public:
void CloseLibrary();
void *GetSymbolAddress(const char* symname);
private:
LibraryHandle m_lib;
};
class LibrarySystem
{
public:
CLibrary* OpenLibrary(const char* path, char* error = nullptr, size_t maxlength = 0);
CDirectory* OpenDirectory(const char* path);
void CloseDirectory(CDirectory *dir);
bool PathExists(const char* path);
bool IsPathFile(const char* path);
bool IsPathDirectory(const char* path);
void GetPlatformError(char* error, size_t maxlength);
void GetPlatformErrorEx(int code, char* error, size_t maxlength);
size_t PathFormat(char* buffer, size_t len, const char* fmt, ...);
char* PathFormat(const char* fmt, ...);
const char* GetFileExtension(const char* filename);
bool CreateFolder(const char* path);
size_t GetFileFromPath(char* buffer, size_t maxlength, const char* path);
bool FileTime(const char* path, FileTimeType type, time_t* pTime);
void GetLoaderError(char* buffer, size_t maxlength);
};
extern LibrarySystem g_LibSys;
template <typename T>
bool GET_IFACE(const char* library, T*& var, const char* version)
{
const char* path = g_LibSys.PathFormat("%s.%s", library, PLATFORM_LIB_EXT);
ke::AutoPtr<CLibrary> lib(g_LibSys.OpenLibrary(path));
if (lib)
{
CreateInterfaceFn factory = reinterpret_cast<CreateInterfaceFn>(lib->GetSymbolAddress(CREATEINTERFACE_PROCNAME));
if (factory)
{
var = reinterpret_cast<T*>(factory(version, nullptr));
return true;
}
}
var = nullptr;
return false;
}
#endif // _INCLUDE_LIBRARY_SYS_H_