Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined WIN32
- #include <Windows.h> /* Windows socket required for MySQL */
- #endif
- #include "extdll.h" /* MetaMod extension */
- #include "meta_api.h" /* MetaMod extension */
- #include "String.h" /* String class */
- #include "mysql.h" /* MySQL connector */
- /* MetaMod global variables */
- enginefuncs_t g_engfuncs;
- globalvars_t * gpGlobals;
- meta_globals_t * gpMetaGlobals;
- gamedll_funcs_t * gpGamedllFuncs;
- mutil_funcs_t * gpMetaUtilFuncs;
- /* MetaMod plugin informations */
- plugin_info_t Plugin_info = {
- META_INTERFACE_VERSION, /* Interface version */
- "Hattrick", /* Name */
- "1.0", /* Version */
- __DATE__, /* Date */
- "Hattrick (claudiuhks) Ltd.", /* Author */
- "Skype: h_clds, Y!M: hattrick4096", /* Contact */
- "HATTRICK", /* Tag */
- PT_ANYTIME, /* Load time */
- PT_ANYTIME /* Unload time */
- };
- /* MySQL Class */
- class MySQL {
- private: /* Inaccessible */
- /* Variables */
- MYSQL Connection;
- MYSQL_RES * pResult;
- MYSQL_ROW Row;
- int Fields, Iterator;
- String Phrase;
- bool Done;
- public: /* Accessible */
- /* Initiates a new MySQL connection */
- bool Initiate( size_t StringResultSize ) {
- Phrase.Allocate( StringResultSize );
- return mysql_init( &Connection ) != NULL ? true : false;
- }
- /* Connects to a MySQL database */
- bool Connect( char * pHostname, char * pUsername, char * pPassword, char * pDatabase ) {
- return mysql_real_connect( &Connection, pHostname, pUsername, pPassword, pDatabase, 0, NULL, 0 ) != NULL ? true : false;
- }
- /* Query */
- bool Query( char * pQuery, bool StoreResultIntoString = false ) {
- Done = mysql_query( &Connection, pQuery ) != 0 ? false : true;
- if( StoreResultIntoString && Done ) {
- pResult = mysql_store_result( &Connection );
- Fields = mysql_num_fields( pResult );
- Phrase.Assign( " " );
- while( ( Row = mysql_fetch_row( pResult ) ) ) {
- for( Iterator = 0; Iterator < Fields; Iterator++ ) {
- Phrase.Append( "\"" );
- Phrase.Append( Row[ Iterator ] ? Row[ Iterator ] : "NULL" );
- Phrase.Append( "\" " );
- }
- }
- Phrase.Trim( true );
- mysql_free_result( pResult );
- }
- return Done;
- }
- /* Disconnects */
- void Disconnect( void ) {
- mysql_close( &Connection );
- }
- /* Frees the connection */
- void Free( void ) {
- Phrase.Free( );
- }
- /* Returns query result content */
- const char * Content( void ) {
- return Phrase.Content( );
- }
- /* Print error */
- void Error( void ) {
- printf( "MySQL Error: %s\n", mysql_error( &Connection ) );
- }
- };
- C_DLLEXPORT int Meta_Query( char *, plugin_info_t ** pPlugInfo, mutil_funcs_t * pMetaUtilFuncs ) {
- gpMetaUtilFuncs = pMetaUtilFuncs;
- *pPlugInfo = PLID;
- return 1;
- }
- void Query( void ) {
- if( g_engfuncs.pfnCmd_Argc( ) == 6 ) {
- MySQL * pMySQL = new MySQL;
- String * pString = new String;
- const char * pQuery = g_engfuncs.pfnCmd_Argv( 5 );
- pString -> Allocate( 1024 );
- pString -> Assign( ( char * ) pQuery );
- if( pMySQL -> Initiate( 1024 ) ) {
- if( pMySQL -> Connect( ( char * ) g_engfuncs.pfnCmd_Argv( 1 ), ( char * ) g_engfuncs.pfnCmd_Argv( 2 ), ( char * ) g_engfuncs.pfnCmd_Argv( 3 ), ( char * ) g_engfuncs.pfnCmd_Argv( 4 ) ) ) {
- if( pMySQL -> Query( ( char * ) pString -> Content( ), ( bool ) ( ( pQuery[ 0 ] == 'S' && pQuery[ 1 ] == 'E' ) || ( pQuery[ 0 ] == 's' && pQuery[ 1 ] == 'e' ) ) ) ) {
- if( ( pQuery[ 0 ] == 'S' && pQuery[ 1 ] == 'E' ) || ( pQuery[ 0 ] == 's' && pQuery[ 1 ] == 'e' ) ) {
- char Game[ 32 ], Line[ 128 ];
- g_engfuncs.pfnGetGameDir( Game );
- sprintf( Line, "%s/addons/QueryResult.ini", Game );
- FILE * pFile = fopen( Line, "r" );
- if( pFile ) {
- fclose( pFile );
- remove( Line );
- }
- pFile = fopen( Line, "a+" );
- if( pFile ) {
- fprintf( pFile, pMySQL -> Content( ) );
- fclose( pFile );
- }
- }
- }
- else
- pMySQL -> Error( );
- pMySQL -> Disconnect( );
- }
- else
- pMySQL -> Error( );
- pMySQL -> Free( );
- }
- else
- pMySQL -> Error( );
- pString -> Free( );
- delete [ ] pString;
- delete [ ] pMySQL;
- }
- else {
- printf( "-----------------------------------\n" );
- printf( "Your command does not have 5 arguments!\n" );
- printf( "-----------------------------------\n" );
- printf( "Usage: Query <hostname> <username> <password> <database> <query>\n" );
- printf( "-----------------------------------\n" );
- printf( "Examples:\n" );
- printf( "-----------------------------------\n" );
- printf( "[1] Hostname: www.something.com | 89.44.44.44\n" );
- printf( "[2] Username: hattrick | ever_a12\n" );
- printf( "[3] Password: a2gge | d_qdqdq\n" );
- printf( "[4] Database: hatt_rick | vending\n" );
- printf( "[5] Query: \"SELECT name FROM players\" | \"CREATE TABLE a ( b TEXT )\"\n" );
- printf( "-----------------------------------\n" );
- printf( "Warnings:\n" );
- printf( "-----------------------------------\n" );
- printf( "[1] When you insert a string delete or replace `, ', \\ and \"\n" );
- printf( "-----------------------------------\n" );
- }
- }
- C_DLLEXPORT int Meta_Attach( PLUG_LOADTIME, META_FUNCTIONS *, meta_globals_t * pMetaGlobals, gamedll_funcs_t * pGamedllFuncs ) {
- gpMetaGlobals = pMetaGlobals;
- gpGamedllFuncs = pGamedllFuncs;
- g_engfuncs.pfnAddServerCommand( "Query", Query );
- return 1;
- }
- C_DLLEXPORT int Meta_Detach( PLUG_LOADTIME, PL_UNLOAD_REASON ) {
- return 1;
- }
- #if defined WIN32
- C_DLLEXPORT __declspec ( naked ) void GiveFnptrsToDll( enginefuncs_t * engfuncs, globalvars_t * pGlobals ) {
- __asm {
- push ebp
- mov ebp, esp
- sub esp, __LOCAL_SIZE
- push ebx
- push esi
- push edi
- }
- #else
- C_DLLEXPORT void GiveFnptrsToDll( enginefuncs_t * engfuncs, globalvars_t * pGlobals ) {
- #endif
- memcpy( &g_engfuncs, engfuncs, sizeof( enginefuncs_t ) );
- gpGlobals = pGlobals;
- #if defined WIN32
- __asm {
- pop edi
- pop esi
- pop ebx
- mov esp, ebp
- pop ebp
- ret 8
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement