Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Totally untested sscanf utilising code... #240

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ add_samp_plugin(mysql
mysql.hpp
sdk.hpp
types.hpp
sscanf.cpp
sscanf.hpp
plugin.def
)

Expand Down
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "version.hpp"

#include "mysql.hpp"
#include "sscanf.hpp"



Expand All @@ -34,6 +35,7 @@ PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
}

logprintf(" >> plugin.mysql: " MYSQL_VERSION " successfully loaded.");
FindSscanf();
return true;
}

Expand Down Expand Up @@ -63,6 +65,7 @@ PLUGIN_EXPORT void PLUGIN_CALL ProcessTick()

extern "C" const AMX_NATIVE_INFO native_list[] =
{
AMX_DEFINE_NATIVE(cache_get_sscanf) // MUST come first.
AMX_DEFINE_NATIVE(orm_create)
AMX_DEFINE_NATIVE(orm_destroy)

Expand Down Expand Up @@ -148,7 +151,8 @@ PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
{
samplog::Api::Get()->RegisterAmx(amx);
CCallbackManager::Get()->AddAmx(amx);
return amx_Register(amx, native_list, -1);
// Skip the first native table entry if `sscanf` isn't loaded.
return amx_Register(amx, native_list + (PawnSScanf ? 0 : 1), -1);
}

PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
Expand Down
2 changes: 2 additions & 0 deletions src/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ using std::string;
namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;

typedef cell (PAWN_NATIVE_API *sscanf_t)(AMX * amx, char * string, char * format, cell * params, int paramCount, char * file, int line);
extern sscanf_t PawnSScanf;

template<typename T>
bool ConvertStrToData(const string &src, T &dest)
Expand Down
60 changes: 60 additions & 0 deletions src/natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "COrm.hpp"
#include "CLog.hpp"
#include "misc.hpp"
#include "sscanf.hpp"

#include <fstream>
#include <future>
Expand Down Expand Up @@ -1648,6 +1649,65 @@ AMX_DECLARE_NATIVE(Native::cache_is_value_index_null)
return 1;
}

// native cache_get_sscanf(row_idx, const format[], {Float, _}:...);
AMX_DECLARE_NATIVE(Native::cache_get_sscanf)
{
CScopedDebugInfo dbg_info(amx, "cache_get_sscanf", params, "ds");
auto resultset = CResultSetManager::Get()->GetActiveResultSet();
if (resultset == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR, "no active cache");
return 0;
}

if (!PawnSScanf)
{
CLog::Get()->LogNative(LogLevel::ERROR, "sscanf native not loaded");
return 0;
}

const string format = amx_GetCppString(amx, params[2]);
if (format.empty())
{
CLog::Get()->LogNative(LogLevel::ERROR, "empty sscanf format");
return 0;
}

Result_t result = resultset->GetActiveResult();
if (result == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR, "active cache has no results");
return 0;
}

const cell &row_idx = params[1];
if (row_idx >= result->GetRowCount())
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid row index '{}' (number of rows: '{}')",
row_idx, result->GetRowCount());
return 0;
}

const char *data = nullptr;
if (result->GetRowData(row_idx, 0, &data) == false)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid row ('{}') index",
params[1]);
return 0;
}

if (data == nullptr) //NULL value
data = "NULL";

CLog::Get()->LogNative(LogLevel::DEBUG, "assigned value: '{}'", data);

int ret = PawnSScanf(amx, const_cast<char *>(data), const_cast<char *>(format.c_str()), params + 3, params[0] / 4 - 2, "MySQL", -1);
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret);
return ret;
}

// native cache_get_value_name(row_idx, const column_name[], destination[],
// max_len=sizeof(destination));
AMX_DECLARE_NATIVE(Native::cache_get_value_name)
Expand Down
3 changes: 3 additions & 0 deletions src/natives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ namespace Native

AMX_DECLARE_NATIVE(cache_get_query_exec_time);
AMX_DECLARE_NATIVE(cache_get_query_string);

// sscanf natives
AMX_DECLARE_NATIVE(cache_get_sscanf);
};
97 changes: 97 additions & 0 deletions src/sscanf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#include <psapi.h>
#else
#include <dlfcn.h>
#include <cstring>
#endif

#include "sscanf.hpp"
#include "sdk.hpp"

sscanf_t PawnSScanf;
extern logprintf_t logprintf;

struct lmap
{
void
* base_address;
char
* path;
void
* unk_1;
struct lmap
* next,
* prev;
};

bool FindSscanf()
{
// Find if sscanf is already loaded:
logprintf(" >> plugin.mysql: finding sscanf...");
#ifdef _WIN32
HANDLE server = GetCurrentProcess();
HMODULE hMods[1024];
DWORD cbNeeded;
HMODULE dll = 0;
if (EnumProcessModules(server, hMods, sizeof(hMods), &cbNeeded))
{
for (unsigned int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
TCHAR szModName[MAX_PATH];
if (GetModuleFileNameEx(server, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR)))
{
int len = strlen(szModName);
if (len >= 11 && strcmp("\\sscanf.DLL", szModName + len - 11) == 0)
{
dll = hMods[i];
break;
}
}
}
}
#else
struct lmap
* pl = (struct lmap *)dlopen(NULL, RTLD_NOW),
* dll = 0;

while (pl)
{
if (strcmp("plugins/sscanf.so", pl->path) == 0)
{
dll = pl;
break;
}
pl = pl->next;
}
#endif

if (dll)
{
#ifdef _WIN32
PawnSScanf = (sscanf_t)GetProcAddress(dll, "PawnSScanf");
#else
*(void **)(&PawnSScanf) = dlsym(dll, "PawnSScanf");
#endif
if (PawnSScanf)
{
logprintf(" >> plugin.mysql: succeeded: %p.\n", PawnSScanf);
return true;
}
else
{
logprintf(" >> plugin.mysql: failed, ensure the plugin version is at least 2.10.3.\n");
logprintf(" >> plugin.mysql: function 'cache_get_sscanf' disabled.\n");
return false;
}
}
else
{
logprintf(" >> plugin.mysql: failed, ensure the sscanf plugin is loaded first.\n");
logprintf(" >> plugin.mysql: function 'cache_get_sscanf' disabled.\n");
return false;
}
}

11 changes: 11 additions & 0 deletions src/sscanf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if defined _WIN32 || defined __CYGWIN__
#define PAWN_NATIVE_API __cdecl
#elif defined __linux__ || defined __APPLE__
#define PAWN_NATIVE_API __attribute__((cdecl))
#endif

typedef cell (PAWN_NATIVE_API *sscanf_t)(AMX * amx, char * string, char * format, cell * params, int paramCount, char * file, int line);
extern sscanf_t PawnSScanf;

bool FindSscanf();