1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-31 09:57:14 +01:00

Update the MaxmindDB module to work with the modified API.

This commit is contained in:
Sandu Liviu Catalin 2016-06-03 21:29:15 +03:00
parent e3f861ccbd
commit 26d12d601d
9 changed files with 49 additions and 466 deletions

View File

@ -433,9 +433,12 @@
<Unit filename="../modules/mmdb/LookupResult.cpp" />
<Unit filename="../modules/mmdb/LookupResult.hpp" />
<Unit filename="../modules/mmdb/Module.cpp" />
<Unit filename="../modules/mmdb/Module.hpp" />
<Unit filename="../modules/mmdb/SockAddr.cpp" />
<Unit filename="../modules/mmdb/Sockaddr.hpp" />
<Unit filename="../shared/Base/Buffer.cpp" />
<Unit filename="../shared/Base/Buffer.hpp" />
<Unit filename="../shared/Base/Utility.cpp" />
<Unit filename="../shared/Base/Utility.hpp" />
<Unit filename="../shared/SqMod.cpp" />
<Extensions>
<code_completion />

View File

@ -1,120 +1,25 @@
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#include <sqrat.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
static SQChar g_Buffer[4096]; // Common buffer to reduce memory allocations.
// ------------------------------------------------------------------------------------------------
SStr GetTempBuff()
{
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
Uint32 GetTempBuffSize()
{
return sizeof(g_Buffer);
}
// ------------------------------------------------------------------------------------------------
void SqThrowF(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (snprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
strcpy(g_Buffer, "Unknown error has occurred");
// Release the argument list
va_end(args);
// Throw the exception with the resulted message
throw Sqrat::Exception(g_Buffer);
}
// ------------------------------------------------------------------------------------------------
CSStr FmtStr(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (snprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
g_Buffer[0] = 0; /* make sure the string is terminated */
// Release the argument list
va_end(args);
// Return the data from the buffer
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
StackGuard::StackGuard(HSQUIRRELVM vm)
: m_Top(sq_gettop(vm)), m_VM(vm)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
StackGuard::~StackGuard()
{
sq_pop(m_VM, sq_gettop(m_VM) - m_Top);
}
// ------------------------------------------------------------------------------------------------
DbRef::Pointer DbRef::Create()
{
return reinterpret_cast< Pointer >(malloc(sizeof(Type)));
return reinterpret_cast< Pointer >(std::malloc(sizeof(Type)));
}
// ------------------------------------------------------------------------------------------------
void DbRef::Destroy(Pointer db)
{
if (db)
free(db);
{
std::free(db);
}
}
// ------------------------------------------------------------------------------------------------
const char NumLimit< char >::Min = CHAR_MIN;
const signed char NumLimit< signed char >::Min = SCHAR_MIN;
const unsigned char NumLimit< unsigned char >::Min = 0;
const signed short NumLimit< signed short >::Min = SHRT_MIN;
const unsigned short NumLimit< unsigned short >::Min = 0;
const signed int NumLimit< signed int >::Min = INT_MIN;
const unsigned int NumLimit< unsigned int >::Min = 0;
const signed long NumLimit< signed long >::Min = LONG_MIN;
const unsigned long NumLimit< unsigned long >::Min = 0;
const signed long long NumLimit< signed long long >::Min = LLONG_MIN;
const unsigned long long NumLimit< unsigned long long >::Min = 0;
const float NumLimit< float >::Min = FLT_MIN;
const double NumLimit< double >::Min = DBL_MIN;
const long double NumLimit< long double >::Min = LDBL_MIN;
// ------------------------------------------------------------------------------------------------
const char NumLimit< char >::Max = CHAR_MAX;
const signed char NumLimit< signed char >::Max = SCHAR_MAX;
const unsigned char NumLimit< unsigned char >::Max = UCHAR_MAX;
const signed short NumLimit< signed short >::Max = SHRT_MAX;
const unsigned short NumLimit< unsigned short >::Max = USHRT_MAX;
const signed int NumLimit< signed int >::Max = INT_MAX;
const unsigned int NumLimit< unsigned int >::Max = UINT_MAX;
const signed long NumLimit< signed long >::Max = LONG_MAX;
const unsigned long NumLimit< unsigned long >::Max = ULONG_MAX;
const signed long long NumLimit< signed long long >::Max = LLONG_MAX;
const unsigned long long NumLimit< unsigned long long >::Max = ULLONG_MAX;
const float NumLimit< float >::Max = FLT_MAX;
const double NumLimit< double >::Max = DBL_MAX;
const long double NumLimit< long double >::Max = LDBL_MAX;
} // Namespace:: SqMod

View File

@ -2,11 +2,7 @@
#define _SQMMDB_COMMON_HPP_
// ------------------------------------------------------------------------------------------------
#include "ModBase.hpp"
// ------------------------------------------------------------------------------------------------
#include <cassert>
#include <cmath>
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <maxminddb.h>
@ -35,70 +31,6 @@ class SockAddr;
class EntryDataList;
class LookupResult;
/* ------------------------------------------------------------------------------------------------
* Retrieve the temporary buffer.
*/
SStr GetTempBuff();
/* ------------------------------------------------------------------------------------------------
* Retrieve the size of the temporary buffer.
*/
Uint32 GetTempBuffSize();
/* ------------------------------------------------------------------------------------------------
* Throw a formatted exception.
*/
void SqThrowF(CSStr str, ...);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string.
*/
CSStr FmtStr(CSStr str, ...);
/* ------------------------------------------------------------------------------------------------
* Implements RAII to restore the VM stack to it's initial size on function exit.
*/
struct StackGuard
{
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
StackGuard(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~StackGuard();
private:
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
StackGuard(const StackGuard &);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
StackGuard(StackGuard &&);
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
StackGuard & operator = (const StackGuard &);
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
StackGuard & operator = (StackGuard &&);
private:
// --------------------------------------------------------------------------------------------
Int32 m_Top; /* The top of the stack when this instance was created. */
HSQUIRRELVM m_VM; /* The VM where the stack should be restored. */
};
/* ------------------------------------------------------------------------------------------------
* Manages a reference counted INI document instance.
*/
@ -343,111 +275,6 @@ public:
}
};
// ------------------------------------------------------------------------------------------------
template < typename T > struct NumLimit;
// ------------------------------------------------------------------------------------------------
template <> struct NumLimit< char > { static const char Min, Max; };
template <> struct NumLimit< signed char > { static const signed char Min, Max; };
template <> struct NumLimit< unsigned char > { static const unsigned char Min, Max; };
template <> struct NumLimit< signed short > { static const signed short Min, Max; };
template <> struct NumLimit< unsigned short > { static const unsigned short Min, Max; };
template <> struct NumLimit< signed int > { static const signed int Min, Max; };
template <> struct NumLimit< unsigned int > { static const unsigned int Min, Max; };
template <> struct NumLimit< signed long > { static const signed long Min, Max; };
template <> struct NumLimit< unsigned long > { static const unsigned long Min, Max; };
template <> struct NumLimit< signed long long > { static const signed long long Min, Max; };
template <> struct NumLimit< unsigned long long > { static const unsigned long long Min, Max; };
template <> struct NumLimit< float > { static const float Min, Max; };
template <> struct NumLimit< double > { static const double Min, Max; };
template <> struct NumLimit< long double > { static const long double Min, Max; };
// ------------------------------------------------------------------------------------------------
template< typename T > inline bool EpsEq(const T a, const T b)
{
return abs(a - b) <= 0;
}
template <> inline bool EpsEq(const Float32 a, const Float32 b)
{
return fabs(a - b) <= 0.000001f;
}
template <> inline bool EpsEq(const Float64 a, const Float64 b)
{
return fabs(a - b) <= 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
template< typename T > inline bool EpsLt(const T a, const T b)
{
return !EpsEq(a, b) && (a < b);
}
template <> inline bool EpsLt(const Float32 a, const Float32 b)
{
return !EpsEq(a, b) && (a - b) < 0.000001f;
}
template <> inline bool EpsLt(const Float64 a, const Float64 b)
{
return !EpsEq(a, b) && (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
template< typename T > inline bool EpsGt(const T a, const T b)
{
return !EpsEq(a, b) && (a > b);
}
template <> inline bool EpsGt(const Float32 a, const Float32 b)
{
return !EpsEq(a, b) && (a - b) > 0.000001f;
}
template <> inline bool EpsGt(const Float64 a, const Float64 b)
{
return !EpsEq(a, b) && (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
template< typename T > inline bool EpsLtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a < b);
}
template <> inline bool EpsLtEq(const Float32 a, const Float32 b)
{
return !EpsEq(a, b) || (a - b) < 0.000001f;
}
template <> inline bool EpsLtEq(const Float64 a, const Float64 b)
{
return !EpsEq(a, b) || (a - b) < 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
template< typename T > inline bool EpsGtEq(const T a, const T b)
{
return !EpsEq(a, b) || (a > b);
}
template <> inline bool EpsGtEq(const Float32 a, const Float32 b)
{
return !EpsEq(a, b) || (a - b) > 0.000001f;
}
template <> inline bool EpsGtEq(const Float64 a, const Float64 b)
{
return !EpsEq(a, b) || (a - b) > 0.000000001d;
}
// ------------------------------------------------------------------------------------------------
template< typename T > inline T Clamp(T val, T min, T max)
{
return val < min ? min : (val > max ? max : val);
}
} // Namespace:: SqMod
#endif // _SQMMDB_COMMON_HPP_

View File

@ -2,10 +2,6 @@
#include "Database.hpp"
#include "SockAddr.hpp"
#include "LookupResult.hpp"
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
#include <sqrat.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {

View File

@ -1,14 +1,10 @@
// ------------------------------------------------------------------------------------------------
#include "EntryDataList.hpp"
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
#include <sqrat.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {

View File

@ -1,6 +1,5 @@
// ------------------------------------------------------------------------------------------------
#include "LookupResult.hpp"
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {

View File

@ -1,34 +1,15 @@
// --------------------------------------------------------------------------------------------
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
// --------------------------------------------------------------------------------------------
#include <sqrat.h>
// --------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
// --------------------------------------------------------------------------------------------
#if defined(WIN32) || defined(_WIN32)
#include <Windows.h>
#endif
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// --------------------------------------------------------------------------------------------
PluginFuncs* _Func = nullptr;
PluginCallbacks* _Clbk = nullptr;
PluginInfo* _Info = nullptr;
// --------------------------------------------------------------------------------------------
HSQAPI _SqAPI = nullptr;
HSQEXPORTS _SqMod = nullptr;
HSQUIRRELVM _SqVM = nullptr;
/* ------------------------------------------------------------------------------------------------
* Bind speciffic functions to certain server events.
* Bind specific functions to certain server events.
*/
void BindCallbacks();
@ -37,22 +18,22 @@ void BindCallbacks();
*/
void UnbindCallbacks();
/* --------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Register the module API under the specified virtual machine.
*/
void RegisterAPI(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Initialize the plugin by obtaining the API provided by the host plugin.
/* ------------------------------------------------------------------------------------------------
* Initialize the plug-in by obtaining the API provided by the host plug-in.
*/
void OnSquirrelInitialize()
{
// Attempt to import the plugin API exported by the host plugin
// Attempt to import the plug-in API exported by the host plug-in
_SqMod = sq_api_import(_Func);
// Did we failed to obtain the plugin exports?
if(!_SqMod)
// Did we failed to obtain the plug-in exports?
if (!_SqMod)
{
OutputError("Failed to attach [%s] on host plugin.", SQMMDB_NAME);
OutputError("Failed to attach [%s] on host plug-in.", SQMMDB_NAME);
}
else
{
@ -63,12 +44,12 @@ void OnSquirrelInitialize()
}
}
/* --------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Load the module on the virtual machine provided by the host module.
*/
void OnSquirrelLoad()
{
// Make sure that we have a valid plugin API
// Make sure that we have a valid plug-in API
if (!_SqMod)
{
return; // Unable to proceed.
@ -88,24 +69,28 @@ void OnSquirrelLoad()
OutputMessage("Registered: %s", SQMMDB_NAME);
}
/* --------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* The virtual machine is about to be terminated and script resources should be released.
*/
void OnSquirrelTerminate()
{
OutputMessage("Terminating: %s", SQMMDB_NAME);
// Release null objects just in case
NullObject().Release();
NullTable().Release();
NullArray().Release();
NullFunction().ReleaseGently();
// Release the current virtual machine, if any
DefaultVM::Set(nullptr);
// Release script resources...
}
/* --------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Validate the module API to make sure we don't run into issues.
*/
bool CheckAPIVer(CCStr ver)
{
// Obtain the numeric representation of the API version
long vernum = strtol(ver, nullptr, 10);
const LongI vernum = std::strtol(ver, nullptr, 10);
// Check against version mismatch
if (vernum == SQMOD_API_VER)
{
@ -118,8 +103,8 @@ bool CheckAPIVer(CCStr ver)
return false;
}
/* --------------------------------------------------------------------------------------------
* React to command sent by other plugins.
/* ------------------------------------------------------------------------------------------------
* React to command sent by other plug-ins.
*/
static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
{
@ -142,8 +127,8 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
return 1;
}
/* --------------------------------------------------------------------------------------------
* The server was initialized and this plugin was loaded successfully.
/* ------------------------------------------------------------------------------------------------
* The server was initialized and this plug-in was loaded successfully.
*/
static uint8_t OnServerInitialise()
{
@ -172,125 +157,39 @@ void UnbindCallbacks()
_Clbk->OnPluginCommand = nullptr;
}
// --------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void RegisterAPI(HSQUIRRELVM vm)
{
}
// --------------------------------------------------------------------------------------------
void OutputMessageImpl(const char * msg, va_list args)
{
#if defined(WIN32) || defined(_WIN32)
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("%c[0;32m[SQMOD]%c[0m", 27, 27);
std::vprintf(msg, args);
std::puts("");
#endif
}
// --------------------------------------------------------------------------------------------
void OutputErrorImpl(const char * msg, va_list args)
{
#if defined(WIN32) || defined(_WIN32)
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_INTENSITY);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("%c[0;91m[SQMOD]%c[0m", 27, 27);
std::vprintf(msg, args);
std::puts("");
#endif
}
// --------------------------------------------------------------------------------------------
void OutputDebug(const char * msg, ...)
{
#ifdef _DEBUG
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
#else
SQMOD_UNUSED_VAR(msg);
#endif
}
// --------------------------------------------------------------------------------------------
void OutputMessage(const char * msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
// --------------------------------------------------------------------------------------------
void OutputError(const char * msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputErrorImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
} // Namespace:: SqMod
// --------------------------------------------------------------------------------------------
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs* functions, PluginCallbacks* callbacks, PluginInfo* info)
// ------------------------------------------------------------------------------------------------
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * functions, PluginCallbacks * callbacks, PluginInfo * info)
{
using namespace SqMod;
// Output plugin header
// Output plug-in header
puts("");
OutputMessage("--------------------------------------------------------------------");
OutputMessage("Plugin: %s", SQMMDB_NAME);
OutputMessage("Plug-in: %s", SQMMDB_NAME);
OutputMessage("Author: %s", SQMMDB_AUTHOR);
OutputMessage("Legal: %s", SQMMDB_COPYRIGHT);
OutputMessage("--------------------------------------------------------------------");
puts("");
// Attempt to find the host plugin ID
int host_plugin_id = functions->FindPlugin((char *)(SQMOD_HOST_NAME));
// See if our plugin was loaded after the host plugin
// Attempt to find the host plug-in ID
const int host_plugin_id = functions->FindPlugin(SQMOD_HOST_NAME);
// See if our plug-in was loaded after the host plug-in
if (host_plugin_id < 0)
{
OutputError("%s could find the host plugin", SQMMDB_NAME);
OutputError("%s could find the host plug-in", SQMMDB_NAME);
// Don't load!
return SQMOD_FAILURE;
}
// Should never reach this point but just in case
else if (host_plugin_id > (info->nPluginId))
{
OutputError("%s loaded after the host plugin", SQMMDB_NAME);
OutputError("%s loaded after the host plug-in", SQMMDB_NAME);
// Don't load!
return SQMOD_FAILURE;
}
@ -298,15 +197,15 @@ SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs* functions, PluginCallb
_Func = functions;
_Clbk = callbacks;
_Info = info;
// Assign plugin version
// Assign plug-in version
_Info->pluginVersion = SQMMDB_VERSION;
_Info->apiMajorVersion = PLUGIN_API_MAJOR;
_Info->apiMinorVersion = PLUGIN_API_MINOR;
// Assign the plugin name
// Assign the plug-in name
std::snprintf(_Info->name, sizeof(_Info->name), "%s", SQMMDB_HOST_NAME);
// Bind callbacks
BindCallbacks();
// Notify that the plugin was successfully loaded
// Notify that the plug-in was successfully loaded
OutputMessage("Successfully loaded %s", SQMMDB_NAME);
// Dummy spacing
puts("");

View File

@ -1,41 +0,0 @@
#ifndef _SQMMDB_MODULE_HPP_
#define _SQMMDB_MODULE_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqMod.h"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Proxies to comunicate with the server.
*/
extern PluginFuncs* _Func;
extern PluginCallbacks* _Clbk;
extern PluginInfo* _Info;
/* ------------------------------------------------------------------------------------------------
* Proxies to comunicate with the Squirrel plugin.
*/
extern HSQAPI _SqAPI;
extern HSQEXPORTS _SqMod;
extern HSQUIRRELVM _SqVM;
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
} // Namespace:: SqMod
#endif // _SQMMDB_MODULE_HPP_

View File

@ -1,6 +1,5 @@
// ------------------------------------------------------------------------------------------------
#include "Sockaddr.hpp"
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {