mapreg_sql.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/db.h"
  5. #include "../common/malloc.h"
  6. #include "../common/showmsg.h"
  7. #include "../common/sql.h"
  8. #include "../common/strlib.h"
  9. #include "../common/timer.h"
  10. #include "map.h" // mmysql_handle
  11. #include "script.h"
  12. #include <stdlib.h>
  13. #include <string.h>
  14. static DBMap* mapreg_db = NULL; // int var_id -> int value
  15. static DBMap* mapregstr_db = NULL; // int var_id -> char* value
  16. static char mapreg_table[32] = "mapreg";
  17. static bool mapreg_dirty = false;
  18. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  19. /// Looks up the value of an integer variable using its uid.
  20. int mapreg_readreg(int uid)
  21. {
  22. return (int)idb_get(mapreg_db, uid);
  23. }
  24. /// Looks up the value of a string variable using its uid.
  25. char* mapreg_readregstr(int uid)
  26. {
  27. return (char*)idb_get(mapregstr_db, uid);
  28. }
  29. /// Modifies the value of an integer variable.
  30. bool mapreg_setreg(int uid, int val)
  31. {
  32. int num = (uid & 0x00ffffff);
  33. int i = (uid & 0xff000000) >> 24;
  34. const char* name = get_str(num);
  35. if( val != 0 )
  36. {
  37. if( idb_put(mapreg_db,uid,(void*)val) )
  38. mapreg_dirty = true; // already exists, delay write
  39. else if(name[1] != '@')
  40. {// write new wariable to database
  41. char tmp_str[32*2+1];
  42. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  43. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%d','%d')", mapreg_table, tmp_str, i, val) )
  44. Sql_ShowDebug(mmysql_handle);
  45. }
  46. }
  47. else // val == 0
  48. {
  49. idb_remove(mapreg_db,uid);
  50. if( name[1] != '@' )
  51. {// Remove from database because it is unused.
  52. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%d'", mapreg_table, name, i) )
  53. Sql_ShowDebug(mmysql_handle);
  54. }
  55. }
  56. return true;
  57. }
  58. /// Modifies the value of a string variable.
  59. bool mapreg_setregstr(int uid, const char* str)
  60. {
  61. int num = (uid & 0x00ffffff);
  62. int i = (uid & 0xff000000) >> 24;
  63. const char* name = get_str(num);
  64. if( str == NULL || *str == 0 )
  65. {
  66. if(name[1] != '@') {
  67. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%d'", mapreg_table, name, i) )
  68. Sql_ShowDebug(mmysql_handle);
  69. }
  70. idb_remove(mapregstr_db,uid);
  71. }
  72. else
  73. {
  74. if (idb_put(mapregstr_db,uid, aStrdup(str)))
  75. mapreg_dirty = true;
  76. else if(name[1] != '@') { //put returned null, so we must insert.
  77. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  78. char tmp_str[32*2+1];
  79. char tmp_str2[255*2+1];
  80. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  81. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  82. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%d','%s')", mapreg_table, tmp_str, i, tmp_str2) )
  83. Sql_ShowDebug(mmysql_handle);
  84. }
  85. }
  86. return true;
  87. }
  88. /// Loads permanent variables from database
  89. static void script_load_mapreg(void)
  90. {
  91. /*
  92. 0 1 2
  93. +-------------------------+
  94. | varname | index | value |
  95. +-------------------------+
  96. */
  97. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  98. char varname[32+1];
  99. int index;
  100. char value[255+1];
  101. uint32 length;
  102. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `varname`, `index`, `value` FROM `%s`", mapreg_table)
  103. || SQL_ERROR == SqlStmt_Execute(stmt)
  104. ) {
  105. SqlStmt_ShowDebug(stmt);
  106. SqlStmt_Free(stmt);
  107. return;
  108. }
  109. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], sizeof(varname), &length, NULL);
  110. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  111. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], sizeof(value), NULL, NULL);
  112. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  113. {
  114. int s = add_str(varname);
  115. int i = index;
  116. if( varname[length-1] == '$' )
  117. idb_put(mapregstr_db, (i<<24)|s, aStrdup(value));
  118. else
  119. idb_put(mapreg_db, (i<<24)|s, (void *)atoi(value));
  120. }
  121. SqlStmt_Free(stmt);
  122. mapreg_dirty = false;
  123. }
  124. /// Saves permanent variables to database
  125. static void script_save_mapreg(void)
  126. {
  127. DBIterator* iter;
  128. void* data;
  129. DBKey key;
  130. iter = mapreg_db->iterator(mapreg_db);
  131. for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) )
  132. {
  133. int num = (key.i & 0x00ffffff);
  134. int i = (key.i & 0xff000000) >> 24;
  135. const char* name = get_str(num);
  136. if( name[1] == '@' )
  137. continue;
  138. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%d' WHERE `varname`='%s' AND `index`='%d'", mapreg_table, (int)data, name, i) )
  139. Sql_ShowDebug(mmysql_handle);
  140. }
  141. iter->destroy(iter);
  142. iter = mapregstr_db->iterator(mapregstr_db);
  143. for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) )
  144. {
  145. int num = (key.i & 0x00ffffff);
  146. int i = (key.i & 0xff000000) >> 24;
  147. const char* name = get_str(num);
  148. char tmp_str2[2*255+1];
  149. if( name[1] == '@' )
  150. continue;
  151. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  152. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%s' WHERE `varname`='%s' AND `index`='%d'", mapreg_table, tmp_str2, name, i) )
  153. Sql_ShowDebug(mmysql_handle);
  154. }
  155. iter->destroy(iter);
  156. mapreg_dirty = false;
  157. }
  158. static int script_autosave_mapreg(int tid, unsigned int tick, int id, intptr data)
  159. {
  160. if( mapreg_dirty )
  161. script_save_mapreg();
  162. return 0;
  163. }
  164. void mapreg_reload(void)
  165. {
  166. if( mapreg_dirty )
  167. script_save_mapreg();
  168. mapreg_db->clear(mapreg_db, NULL);
  169. mapregstr_db->clear(mapregstr_db, NULL);
  170. script_load_mapreg();
  171. }
  172. void mapreg_final(void)
  173. {
  174. if( mapreg_dirty )
  175. script_save_mapreg();
  176. mapreg_db->destroy(mapreg_db,NULL);
  177. mapregstr_db->destroy(mapregstr_db,NULL);
  178. }
  179. void mapreg_init(void)
  180. {
  181. mapreg_db = idb_alloc(DB_OPT_BASE);
  182. mapregstr_db = idb_alloc(DB_OPT_RELEASE_DATA);
  183. script_load_mapreg();
  184. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  185. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  186. }
  187. bool mapreg_config_read(const char* w1, const char* w2)
  188. {
  189. if(!strcmpi(w1, "mapreg_db"))
  190. safestrncpy(mapreg_table, w2, sizeof(mapreg_table));
  191. else
  192. return false;
  193. return true;
  194. }