Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.20 KB | None | 0 0
  1. /*
  2. GUID OVERFLOW FIXER
  3. =============================================================
  4.  - Changes biggest guids to lower which are free
  5.  - look at line 36
  6.  
  7.  For compile use:
  8.      g++ -o guid $(mysql_config --cflags) guid.cpp $(mysql_config --libs) -g
  9.  
  10. */
  11.  
  12. #include <mysql.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <list>
  16. #include <vector>
  17. #include <string>
  18. #include <memory>
  19. #include <time.h>
  20. #include <math.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <assert.h>
  24. #include <sstream>
  25.  
  26. using namespace std;
  27.  
  28. struct tables
  29. {
  30.     std::string name;
  31.     std::string column;
  32. };
  33.  
  34. int main()
  35. {
  36.     // Leave or change guid in other tables, if false then skip creatures which have record in other tables
  37.     bool changeGUID = false;
  38.  
  39.     static tables tabulky[]=
  40.     {
  41.         {"creature_addon", "guid"},
  42.         {"creature_battleground", "guid"},
  43.         {"creature_movement", "id"},
  44.         {"game_event_creature", "guid"},
  45.         {"game_event_model_equip", "guid"},
  46.         {"npc_gossip", "npc_guid"},
  47.         {"pool_creature", "guid"}
  48.     };
  49.                            
  50.     MYSQL *conn;
  51.     MYSQL_RES *res;
  52.     MYSQL_ROW row;
  53.    
  54.     std::stringstream file;
  55.     file << "guidlog_" << time(NULL);
  56.     FILE *log = fopen(file.str().c_str(), "a");
  57.  
  58.     char *server = "localhost";
  59.     char *user = "user";
  60.     char *password = "pass"; /* set me first */
  61.     char *database = "database";
  62.    
  63.     conn = mysql_init(NULL);
  64.                
  65.     /* Connect to database */
  66.     if (!mysql_real_connect(conn, server,
  67.      user, password, database, 0, NULL, 0)) {
  68.         printf("%s\n", mysql_error(conn));
  69.         return 1;
  70.     }
  71.     if (mysql_query(conn, "SELECT guid FROM creature ORDER BY guid")) {
  72.         fprintf(log, "%s\n", mysql_error(conn));
  73.         return 1;
  74.     }
  75.    
  76.     res = mysql_use_result(conn);
  77.     std::list<long> guids;
  78.  
  79.     printf("\nLOADING GUIDS FROM DB \n ");
  80.     printf("============================ \n");
  81.     fprintf(log, "\nLOADING GUIDS FROM DB \n ");
  82.     fprintf(log, "============================ \n");
  83.        
  84.     while ((row = mysql_fetch_row(res)) != NULL)
  85.     {
  86.         int a = atoi(row[0]);
  87.         guids.push_back(long(a));
  88.     }
  89.     mysql_free_result(res);
  90.  
  91.     printf("\nASSEMBLING FREE GUIDS \n ");
  92.     printf("============================ \n");
  93.     fprintf(log, "\nASSEMBLING FREE GUIDS \n ");
  94.     fprintf(log, "============================ \n");        
  95.     std::list<long> free;
  96.  
  97.     long lastGuid = *(guids.begin())-1;
  98.     fprintf(log, "firstguid %u \n", *(guids.begin()));
  99.     for(std::list<long>::iterator itr = guids.begin(); itr != guids.end(); ++itr)
  100.     {
  101.         if((*itr) - lastGuid > 1)
  102.         {
  103.             ++lastGuid;
  104.             for(;lastGuid < (*itr); ++lastGuid)
  105.                free.push_back(lastGuid);
  106.         }
  107.         else
  108.             ++lastGuid;
  109.     }
  110.    
  111.     printf("\nCLEANING UP 0 GUID \n ");
  112.     printf("============================ \n");
  113.     fprintf(log, "\nCLENING UP 0 GUID \n ");
  114.     fprintf(log, "============================ \n");
  115.     //Cleanup...
  116.     mysql_query(conn, "SELECT guid FROM creature WHERE guid=0");
  117.    
  118.     res = mysql_use_result(conn);
  119.     row = mysql_fetch_row(res);
  120.     if(row != NULL)
  121.     {
  122.         printf("\n coze? \n");
  123.         std::stringstream ss;
  124.         ss << "UPDATE creature SET guid = " << *(free.begin()) <<  " WHERE guid=0";
  125.         fprintf(log, "\n %s \n", ss.str().c_str());
  126.         mysql_free_result(res);
  127.         mysql_query(conn, ss.str().c_str());
  128.         free.pop_front();
  129.         guids.pop_front();
  130.     }
  131.     fprintf(log, " \n error %s \n", mysql_error(conn));
  132.     guids.reverse();
  133.     bool can = true;
  134.     int counta = 0;
  135.    
  136.     printf("\nCHANGING GUIDS \n ");
  137.     printf("============================ \n");
  138.     fprintf(log, "\nCHANGING GUIDS \n ");
  139.     fprintf(log, "============================ \n");    
  140.     fprintf(log, "\n begin: %u, %u \n", *(guids.begin()), *(free.begin()));
  141.    
  142.     for(std::list<long>::iterator itr = guids.begin(); itr != guids.end(); ++itr)
  143.     {
  144.         can = true;
  145.         if(*itr <= *(free.begin()))
  146.             break;
  147.         for(int y = 0; y < 6; ++y)
  148.         {
  149.              std::stringstream ss;
  150.              ss << "SELECT " << tabulky[y].column << " FROM " << tabulky[y].name << " WHERE "
  151.                  << tabulky[y].column <<  "=" << *itr;
  152.              mysql_query(conn, ss.str().c_str());
  153.              res = mysql_use_result(conn);
  154.              if(!res)
  155.              {
  156.               mysql_free_result(res);
  157.               continue;
  158.              }
  159.              if(row = mysql_fetch_row(res))
  160.              {
  161.                  if(changeGUID)
  162.                  {
  163.                     fprintf(log, "\n edit %s for creature %u", (tabulky[y].name).c_str(), *itr);
  164.                     mysql_free_result(res);
  165.                     std::stringstream sss;
  166.                     sss << "UPDATE " << tabulky[y].name << " SET " << tabulky[y].column << "="
  167.                         << *(free.begin())<< " WHERE " << tabulky[y].column << "=" << *itr;
  168.                     mysql_query(conn, sss.str().c_str());
  169.                  }
  170.                  else
  171.                  {
  172.                     can = false;
  173.                     mysql_free_result(res);
  174.                     break;
  175.                  }
  176.              }else
  177.                  mysql_free_result(res);
  178.         }
  179.         if(!can)
  180.             continue;
  181.         std::stringstream ss;
  182.         ss << "UPDATE creature SET guid = " << *(free.begin()) << " WHERE guid =  " << *itr;
  183.         fprintf(log, "\n \n %s", ss.str().c_str());
  184.        
  185.         mysql_query(conn, ss.str().c_str());
  186.  
  187.         fprintf(log, "\n    presunul %u -> %u", *itr, *(free.begin()));
  188.         free.pop_front();
  189.         if(free.empty())
  190.             break;
  191.         ++counta;
  192.     }
  193.     long maxguid = 0;
  194.  
  195.     mysql_query(conn, "SELECT MAX(guid) FROM creature");
  196.    
  197.     res = mysql_use_result(conn);
  198.     row = mysql_fetch_row(res);
  199.     if(row != NULL)
  200.         maxguid = atoi(row[0]);
  201.  
  202.     printf("\n Changed creatures: %u, maxguid: %u, space between overflow: %u\n", counta, maxguid, (0x00FFFFFF - maxguid));
  203.     mysql_free_result(res);
  204.     mysql_close(conn);
  205.     fclose(log);
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement