database.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../common/debug.h"
  17. #include <iostream>
  18. using namespace std;
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errmsg.h>
  23. //#include <mysqld_error.h>
  24. #include <limits.h>
  25. #include <ctype.h>
  26. #include <assert.h>
  27. #include <map>
  28. // Disgrace: for windows compile
  29. #ifdef WIN32
  30. #include <WinSock2.h>
  31. #include <windows.h>
  32. #define snprintf _snprintf
  33. #define strncasecmp _strnicmp
  34. #define strcasecmp _stricmp
  35. #else
  36. #include "unix.h"
  37. #include <netinet/in.h>
  38. #endif
  39. #include "database.h"
  40. #include "EQStream.h"
  41. #include "packet_functions.h"
  42. #include "emu_opcodes.h"
  43. #ifdef WORLD
  44. #include "../WorldServer/WorldDatabase.h"
  45. extern WorldDatabase database;
  46. #endif
  47. #ifdef LOGIN
  48. #include "../LoginServer/LoginDatabase.h"
  49. extern LoginDatabase database;
  50. #endif
  51. #ifdef PARSER
  52. #include "../PacketParser/ParserDatabase.h"
  53. extern ParserDatabase database;
  54. #endif
  55. #ifdef PATCHER
  56. #include "../PatchServer/PatcherDatabase.h"
  57. extern PatcherDatabase database;
  58. #endif
  59. #include "../common/EQEMuError.h"
  60. #include "../common/packet_dump.h"
  61. #include "../common/Log.h"
  62. Database::Database()
  63. {
  64. InitVars();
  65. }
  66. bool Database::Init() {
  67. char host[200], user[200], passwd[200], database[200];
  68. int32 port=0;
  69. bool compression = false;
  70. bool items[6] = {false, false, false, false, false, false};
  71. if(!ReadDBINI(host, user, passwd, database, port, compression, items)) {
  72. //exit(1);
  73. return false;
  74. }
  75. if (!items[0] || !items[1] || !items[2] || !items[3])
  76. {
  77. LogWrite(DATABASE__ERROR, 0, "DB", "Incomplete DB.INI file.");
  78. LogWrite(DATABASE__ERROR, 0, "DB", "Read README.TXT!");
  79. //exit (1);
  80. return false;
  81. }
  82. int32 errnum = 0;
  83. char errbuf[MYSQL_ERRMSG_SIZE];
  84. if (!Open(host, user, passwd, database,port, &errnum, errbuf))
  85. {
  86. LogWrite(DATABASE__ERROR, 0, "DB", "Failed to connect to database: Error: %s", errbuf);
  87. HandleMysqlError(errnum);
  88. //exit(1);
  89. return false;
  90. }
  91. else
  92. {
  93. LogWrite(DATABASE__INFO, 0, "DB", "Using database '%s' at %s", database, host);
  94. }
  95. return true;
  96. }
  97. map<int16, int16> Database::GetVersions(){
  98. map<int16, int16> opcodes;
  99. Query query;
  100. MYSQL_ROW row;
  101. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "select distinct version_range1, version_range2 from opcodes");
  102. while(result && (row = mysql_fetch_row(result))){
  103. if(row[0] && row[1])
  104. opcodes[atoi(row[0])] = atoi(row[1]);
  105. }
  106. return opcodes;
  107. }
  108. map<string, uint16> Database::GetOpcodes(int16 version){
  109. map<string, uint16> opcodes;
  110. Query query;
  111. MYSQL_ROW row;
  112. MYSQL_RES* result = query.RunQuery2(Q_SELECT, "select name, opcode from opcodes where %i between version_range1 and version_range2 order by version_range1, id", version);
  113. while(result && (row = mysql_fetch_row(result))){
  114. opcodes[row[0]] = atoi(row[1]);
  115. }
  116. return opcodes;
  117. }
  118. void Database::HandleMysqlError(int32 errnum) {
  119. switch(errnum) {
  120. case 0:
  121. break;
  122. case 1045: // Access Denied
  123. case 2001: {
  124. AddEQEMuError(EQEMuError_Mysql_1405, true);
  125. break;
  126. }
  127. case 2003: { // Unable to connect
  128. AddEQEMuError(EQEMuError_Mysql_2003, true);
  129. break;
  130. }
  131. case 2005: { // Unable to connect
  132. AddEQEMuError(EQEMuError_Mysql_2005, true);
  133. break;
  134. }
  135. case 2007: { // Unable to connect
  136. AddEQEMuError(EQEMuError_Mysql_2007, true);
  137. break;
  138. }
  139. }
  140. }
  141. void Database::InitVars() {
  142. }
  143. Database::~Database()
  144. {
  145. }
  146. MYSQL_RES* Query::RunQuery2(QUERY_TYPE type, const char* format, ...){
  147. va_list args;
  148. va_start( args, format );
  149. #ifdef WIN32
  150. char * buffer;
  151. int buf_len = _vscprintf( format, args ) + 1;
  152. buffer = new char[buf_len];
  153. vsprintf( buffer, format, args );
  154. #else
  155. char* buffer;
  156. int buf_len;
  157. va_list argcopy;
  158. va_copy(argcopy, args);
  159. buf_len = vsnprintf(NULL, 0, format, argcopy) + 1;
  160. va_end(argcopy);
  161. buffer = new char[buf_len];
  162. vsnprintf(buffer, buf_len, format, args);
  163. #endif
  164. va_end(args);
  165. query = string(buffer);
  166. safe_delete_array( buffer );
  167. return RunQuery2(query.c_str(), type);
  168. }
  169. MYSQL_RES* Query::RunQuery2(string in_query, QUERY_TYPE type){
  170. switch(type){
  171. case Q_SELECT:
  172. break;
  173. case Q_DBMS:
  174. case Q_REPLACE:
  175. case Q_DELETE:
  176. case Q_UPDATE:
  177. safe_delete(affected_rows);
  178. affected_rows = new int32;
  179. break;
  180. case Q_INSERT:
  181. safe_delete(last_insert_id);
  182. last_insert_id = new int32;
  183. }
  184. if(result){
  185. if(!multiple_results)
  186. multiple_results = new vector<MYSQL_RES*>();
  187. multiple_results->push_back(result);
  188. }
  189. query = in_query;
  190. database.RunQuery(query.c_str(), query.length(), errbuf, &result, affected_rows, last_insert_id, &errnum, retry);
  191. return result;
  192. }