Advertisement
Guest User

Hattrick

a guest
Oct 13th, 2012
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.90 KB | None | 0 0
  1. #if defined WIN32
  2.     #include <Windows.h> /* Windows socket required for MySQL */
  3. #endif
  4.  
  5. #include "extdll.h" /* MetaMod extension */
  6. #include "meta_api.h" /* MetaMod extension */
  7. #include "String.h" /* String class */
  8. #include "mysql.h" /* MySQL connector */
  9.  
  10. /* MetaMod global variables */
  11. enginefuncs_t g_engfuncs;
  12. globalvars_t * gpGlobals;
  13. meta_globals_t * gpMetaGlobals;
  14. gamedll_funcs_t * gpGamedllFuncs;
  15. mutil_funcs_t * gpMetaUtilFuncs;
  16.  
  17. /* MetaMod plugin informations */
  18. plugin_info_t Plugin_info = {
  19.     META_INTERFACE_VERSION, /* Interface version */
  20.     "Hattrick", /* Name */
  21.     "1.0", /* Version */
  22.     __DATE__, /* Date */
  23.     "Hattrick (claudiuhks) Ltd.", /* Author */
  24.     "Skype: h_clds, Y!M: hattrick4096", /* Contact */
  25.     "HATTRICK", /* Tag */
  26.     PT_ANYTIME, /* Load time */
  27.     PT_ANYTIME /* Unload time */
  28. };
  29.  
  30. /* MySQL Class */
  31. class MySQL {
  32. private: /* Inaccessible */
  33.     /* Variables */
  34.     MYSQL Connection;
  35.     MYSQL_RES * pResult;
  36.     MYSQL_ROW Row;
  37.     int Fields, Iterator;
  38.     String Phrase;
  39.     bool Done;
  40.  
  41. public: /* Accessible */
  42.     /* Initiates a new MySQL connection */
  43.     bool Initiate( size_t StringResultSize ) {
  44.         Phrase.Allocate( StringResultSize );
  45.  
  46.         return mysql_init( &Connection ) != NULL ? true : false;
  47.     }
  48.  
  49.     /* Connects to a MySQL database */
  50.     bool Connect( char * pHostname, char * pUsername, char * pPassword, char * pDatabase ) {
  51.         return mysql_real_connect( &Connection, pHostname, pUsername, pPassword, pDatabase, 0, NULL, 0 ) != NULL ? true : false;
  52.     }
  53.  
  54.     /* Query */
  55.     bool Query( char * pQuery, bool StoreResultIntoString = false ) {
  56.         Done = mysql_query( &Connection, pQuery ) != 0 ? false : true;
  57.  
  58.         if( StoreResultIntoString && Done ) {
  59.             pResult = mysql_store_result( &Connection );
  60.  
  61.             Fields = mysql_num_fields( pResult );
  62.  
  63.             Phrase.Assign( " " );
  64.  
  65.             while( ( Row = mysql_fetch_row( pResult ) ) ) {
  66.                 for( Iterator = 0; Iterator < Fields; Iterator++ ) {
  67.                     Phrase.Append( "\"" );
  68.                     Phrase.Append( Row[ Iterator ] ? Row[ Iterator ] : "NULL" );
  69.                     Phrase.Append( "\" " );
  70.                 }
  71.             }
  72.  
  73.             Phrase.Trim( true );
  74.  
  75.             mysql_free_result( pResult );
  76.         }
  77.  
  78.         return Done;
  79.     }
  80.  
  81.     /* Disconnects */
  82.     void Disconnect( void ) {
  83.         mysql_close( &Connection );
  84.     }
  85.  
  86.     /* Frees the connection */
  87.     void Free( void ) {
  88.         Phrase.Free( );
  89.     }
  90.  
  91.     /* Returns query result content */
  92.     const char * Content( void ) {
  93.         return Phrase.Content( );
  94.     }
  95.  
  96.     /* Print error */
  97.     void Error( void ) {
  98.         printf( "MySQL Error: %s\n", mysql_error( &Connection ) );
  99.     }
  100. };
  101.  
  102. C_DLLEXPORT int Meta_Query( char *, plugin_info_t ** pPlugInfo, mutil_funcs_t * pMetaUtilFuncs ) {
  103.     gpMetaUtilFuncs = pMetaUtilFuncs;
  104.  
  105.     *pPlugInfo = PLID;
  106.  
  107.     return 1;
  108. }
  109.  
  110. void Query( void ) {
  111.     if( g_engfuncs.pfnCmd_Argc( ) == 6 ) {
  112.         MySQL * pMySQL = new MySQL;
  113.         String * pString = new String;
  114.  
  115.         const char * pQuery = g_engfuncs.pfnCmd_Argv( 5 );
  116.  
  117.         pString -> Allocate( 1024 );
  118.         pString -> Assign( ( char * ) pQuery );
  119.  
  120.         if( pMySQL -> Initiate( 1024 ) ) {
  121.             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 ) ) ) {
  122.                 if( pMySQL -> Query( ( char * ) pString -> Content( ), ( bool ) ( ( pQuery[ 0 ] == 'S' && pQuery[ 1 ] == 'E' ) || ( pQuery[ 0 ] == 's' && pQuery[ 1 ] == 'e' ) ) ) ) {
  123.                     if( ( pQuery[ 0 ] == 'S' && pQuery[ 1 ] == 'E' ) || ( pQuery[ 0 ] == 's' && pQuery[ 1 ] == 'e' ) ) {
  124.                         char Game[ 32 ], Line[ 128 ];
  125.  
  126.                         g_engfuncs.pfnGetGameDir( Game );
  127.  
  128.                         sprintf( Line, "%s/addons/QueryResult.ini", Game );
  129.  
  130.                         FILE * pFile = fopen( Line, "r" );
  131.  
  132.                         if( pFile ) {
  133.                             fclose( pFile );
  134.  
  135.                             remove( Line );
  136.                         }
  137.  
  138.                         pFile = fopen( Line, "a+" );
  139.  
  140.                         if( pFile ) {
  141.                             fprintf( pFile, pMySQL -> Content( ) );
  142.  
  143.                             fclose( pFile );
  144.                         }
  145.                     }
  146.                 }
  147.  
  148.                 else
  149.                     pMySQL -> Error( );
  150.  
  151.                 pMySQL -> Disconnect( );
  152.             }
  153.  
  154.             else
  155.                 pMySQL -> Error( );
  156.  
  157.             pMySQL -> Free( );
  158.         }
  159.  
  160.         else
  161.             pMySQL -> Error( );
  162.  
  163.         pString -> Free( );
  164.  
  165.         delete [ ] pString;
  166.         delete [ ] pMySQL;
  167.     }
  168.  
  169.     else {
  170.         printf( "-----------------------------------\n" );
  171.         printf( "Your command does not have 5 arguments!\n" );
  172.         printf( "-----------------------------------\n" );
  173.         printf( "Usage: Query <hostname> <username> <password> <database> <query>\n" );
  174.         printf( "-----------------------------------\n" );
  175.         printf( "Examples:\n" );
  176.         printf( "-----------------------------------\n" );
  177.         printf( "[1] Hostname: www.something.com | 89.44.44.44\n" );
  178.         printf( "[2] Username: hattrick | ever_a12\n" );
  179.         printf( "[3] Password: a2gge | d_qdqdq\n" );
  180.         printf( "[4] Database: hatt_rick | vending\n" );
  181.         printf( "[5] Query: \"SELECT name FROM players\" | \"CREATE TABLE a ( b TEXT )\"\n" );
  182.         printf( "-----------------------------------\n" );
  183.         printf( "Warnings:\n" );
  184.         printf( "-----------------------------------\n" );
  185.         printf( "[1] When you insert a string delete or replace `, ', \\ and \"\n" );
  186.         printf( "-----------------------------------\n" );
  187.     }
  188. }
  189.  
  190. C_DLLEXPORT int Meta_Attach( PLUG_LOADTIME, META_FUNCTIONS *, meta_globals_t * pMetaGlobals, gamedll_funcs_t * pGamedllFuncs ) {
  191.     gpMetaGlobals = pMetaGlobals;
  192.     gpGamedllFuncs = pGamedllFuncs;
  193.  
  194.     g_engfuncs.pfnAddServerCommand( "Query", Query );
  195.  
  196.     return 1;
  197. }
  198.  
  199. C_DLLEXPORT int Meta_Detach( PLUG_LOADTIME, PL_UNLOAD_REASON ) {
  200.     return 1;
  201. }
  202.  
  203. #if defined WIN32
  204. C_DLLEXPORT __declspec ( naked ) void GiveFnptrsToDll( enginefuncs_t * engfuncs, globalvars_t * pGlobals ) {
  205.     __asm {
  206.         push ebp
  207.         mov ebp, esp
  208.         sub esp, __LOCAL_SIZE
  209.         push ebx
  210.         push esi
  211.         push edi
  212.     }
  213. #else
  214. C_DLLEXPORT void GiveFnptrsToDll( enginefuncs_t * engfuncs, globalvars_t * pGlobals ) {
  215. #endif
  216.  
  217.     memcpy( &g_engfuncs, engfuncs, sizeof( enginefuncs_t ) );
  218.     gpGlobals = pGlobals;
  219.  
  220. #if defined WIN32
  221.     __asm {
  222.         pop edi
  223.         pop esi
  224.         pop ebx
  225.         mov esp, ebp
  226.         pop ebp
  227.         ret 8
  228.     }
  229. #endif
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement