Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <thread>
- #include <vector>
- #include <string>
- #include <time.h>
- #include "../SDK/plugin.h"
- #include "../Invoke/Invoke.h"
- using namespace std;
- typedef void
- (*logprintf_t)(char* format, ...)
- ;
- logprintf_t
- logprintf
- ;
- void
- **ppPluginData
- ;
- extern void
- *pAMXFunctions
- ;
- static time_t lasttime = 0;
- class ThreadQuerysList
- {
- public:
- AMX* address;
- int database, extraid;
- char *query, *callback;
- ThreadQuerysList(AMX* amxaddress, int databaseid, char *query_, int extraid_, char *callback_)
- {
- address = amxaddress;
- database = databaseid;
- extraid = extraid_;
- query = query_;
- callback = callback_;
- }
- };
- vector<ThreadQuerysList> PendingQuerys;
- PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
- {
- g_Invoke = new Invoke;
- pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
- logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
- return 1;
- }
- bool finishedquery = true;
- void ThreadedQuery()
- {
- g_Invoke->getAddresses();
- ThreadQuerysList list = PendingQuerys[0];
- int result = g_Invoke->callNative(&PAWN::db_query, list.database, list.query);
- int idx;
- if ((time(NULL) - lasttime) < 1)
- {
- do
- {
- this_thread::sleep_for(chrono::milliseconds(500));
- } while ((time(NULL) - lasttime) < 1);
- }
- if (!amx_FindPublic(list.address, list.callback, &idx))
- {
- cell ret;
- amx_Push(list.address, result);
- amx_Push(list.address, list.extraid);
- amx_Exec(list.address, &ret, idx);
- g_Invoke->callNative(&PAWN::db_free_result, result);
- }
- if ((time(NULL) - lasttime) < 1)
- {
- do
- {
- this_thread::sleep_for(chrono::milliseconds(500));
- } while ((time(NULL) - lasttime) < 1);
- }
- PendingQuerys.erase(PendingQuerys.begin());
- if (PendingQuerys.size())
- {
- do
- {
- ThreadQuerysList list2 = PendingQuerys[0];
- int result2 = g_Invoke->callNative(&PAWN::db_query, list2.database, list2.query);
- if ((time(NULL) - lasttime) < 1)
- {
- do
- {
- this_thread::sleep_for(chrono::milliseconds(500));
- } while ((time(NULL) - lasttime) < 1);
- }
- int idx2 = 0;
- if (!amx_FindPublic(list2.address, list2.callback, &idx2))
- {
- cell ret2;
- amx_Push(list2.address, result2);
- amx_Push(list2.address, list2.extraid);
- amx_Exec(list2.address, &ret2, idx2);
- g_Invoke->callNative(&PAWN::db_free_result, result2);
- }
- if ((time(NULL) - lasttime) < 1)
- {
- do
- {
- this_thread::sleep_for(chrono::milliseconds(500));
- } while ((time(NULL) - lasttime) < 1);
- }
- PendingQuerys.erase(PendingQuerys.begin());
- } while (PendingQuerys.size() > 0);
- }
- finishedquery = true;
- }
- #define CHECK_PARAMS(m, n) \
- if (params[0] != (m * 4)) \
- { \
- logprintf("DBTQ: %s: Expecting %d parameter(s), but found %d", n, m, params[0] / sizeof(cell)); \
- return 0; \
- }
- cell AMX_NATIVE_CALL db_tquery(AMX *amx, cell *params)
- {
- CHECK_PARAMS(4, "db_tquery");
- int database_ID = static_cast<int>(params[1]);
- int extraid = static_cast<int>(params[3]);
- int
- len = NULL,
- ret = NULL;
- cell *addr = NULL;
- amx_GetAddr(amx, params[2], &addr);
- amx_StrLen(addr, &len);
- if (len)
- {
- len++;
- char *query = new char[len];
- amx_GetString(query, addr, 0, len);
- amx_GetAddr(amx, params[4], &addr);
- amx_StrLen(addr, &len);
- len++;
- char *callback = new char[len];
- if (len)
- {
- amx_GetString(callback, addr, 0, len);
- }
- if (finishedquery)
- {
- finishedquery = false;
- PendingQuerys.push_back(ThreadQuerysList(amx, database_ID, query, extraid, callback));
- thread mythread(ThreadedQuery);
- mythread.detach();
- }
- else
- {
- PendingQuerys.push_back(ThreadQuerysList(amx, database_ID, query, extraid, callback));
- lasttime = time(NULL);
- }
- }
- return 1;
- }
- PLUGIN_EXPORT void PLUGIN_CALL Unload()
- {
- }
- AMX_NATIVE_INFO projectNatives[] =
- {
- { "db_tquery", db_tquery },
- { 0, 0 }
- };
- PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
- {
- return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
- }
- PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
- {
- g_Invoke->amx_list.push_back(amx);
- return amx_Register(amx, projectNatives, -1);
- }
- PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
- {
- for (std::list<AMX *>::iterator i = g_Invoke->amx_list.begin(); i != g_Invoke->amx_list.end(); ++i)
- {
- if (*i == amx)
- {
- g_Invoke->amx_list.erase(i);
- break;
- }
- }
- return AMX_ERR_NONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement