itemdb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "itemdb.h"
  7. #include "db.h"
  8. #include "inter.h"
  9. #include "char.h"
  10. #include "utils.h"
  11. #include "../common/malloc.h"
  12. #include "../common/showmsg.h"
  13. #define MAX_RANDITEM 2000
  14. // ** ITEMDB_OVERRIDE_NAME_VERBOSE **
  15. // 定義すると、itemdb.txtとgrfで名前が異なる場合、表示します.
  16. //#define ITEMDB_OVERRIDE_NAME_VERBOSE 1
  17. char item_db_db[256]="item_db"; // added to specify item_db sql table [Valaris]
  18. char item_db2_db[256]="item_db2";
  19. static struct dbt* item_db;
  20. static void* create_item(DBKey key, va_list args) {
  21. struct item_data *id;
  22. int nameid = key.i;
  23. CREATE(id, struct item_data, 1);
  24. id->nameid = nameid;
  25. id->type = IT_ETC;
  26. return id;
  27. }
  28. /*==========================================
  29. * DBの検索
  30. *------------------------------------------
  31. */
  32. struct item_data* itemdb_search(int nameid)
  33. {
  34. return idb_ensure(item_db,nameid,create_item);
  35. }
  36. /*==========================================
  37. *
  38. *------------------------------------------
  39. */
  40. int itemdb_isequip(int nameid)
  41. {
  42. int type=itemdb_type(nameid);
  43. if(type==IT_HEALING || type==IT_USABLE || type==IT_ETC || type==IT_CARD || type==IT_AMMO)
  44. return 0;
  45. return 1;
  46. }
  47. /*==========================================
  48. *
  49. *------------------------------------------
  50. */
  51. int itemdb_isequip2(struct item_data *data)
  52. {
  53. if(data) {
  54. int type=data->type;
  55. if(type==IT_HEALING || type==IT_USABLE || type==IT_ETC || type==IT_CARD || type==IT_AMMO)
  56. return 0;
  57. else
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. /*==========================================
  63. * アイテムデータベースの読み込み
  64. *------------------------------------------
  65. */
  66. static int itemdb_readdb(void)
  67. {
  68. FILE *fp;
  69. char line[1024];
  70. int ln=0,lines=0;
  71. int nameid,j;
  72. char *str[32],*p,*np;
  73. struct item_data *id;
  74. int i=0;
  75. char *filename[]={ "item_db.txt","item_db2.txt" };
  76. for(i=0;i<2;i++){
  77. sprintf(line, "%s/%s", db_path, filename[i]);
  78. fp=fopen(line,"r");
  79. if(fp==NULL){
  80. if(i>0)
  81. continue;
  82. ShowFatalError("can't read %s\n",line);
  83. exit(1);
  84. }
  85. lines=0;
  86. while(fgets(line,1020,fp)){
  87. lines++;
  88. if(line[0]=='/' && line[1]=='/')
  89. continue;
  90. memset(str,0,sizeof(str));
  91. for(j=0,np=p=line;j<4 && p;j++){
  92. str[j]=p;
  93. p=strchr(p,',');
  94. if(p){ *p++=0; np=p; }
  95. }
  96. if(str[0]==NULL)
  97. continue;
  98. nameid=atoi(str[0]);
  99. if(nameid<=0)
  100. continue;
  101. if (j < 4)
  102. { //Crash-fix on broken item lines. [Skotlex]
  103. ShowWarning("Reading %s: Insufficient fields for item with id %d, skipping.\n", filename[i], nameid);
  104. continue;
  105. }
  106. ln++;
  107. //ID,Name,Jname,Type,Price,Sell,Weight,ATK,DEF,Range,Slot,Job,Job Upper,Gender,Loc,wLV,eLV,refineable,View
  108. id=itemdb_search(nameid);
  109. strncpy(id->name, str[1], ITEM_NAME_LENGTH-1);
  110. strncpy(id->jname, str[2], ITEM_NAME_LENGTH-1);
  111. id->type=atoi(str[3]);
  112. if (id->type == IT_DELAYCONSUME)
  113. id->type = IT_USABLE;
  114. }
  115. fclose(fp);
  116. if (ln > 0) {
  117. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",ln,filename[i]);
  118. }
  119. ln=0; // reset to 0
  120. }
  121. return 0;
  122. }
  123. static int itemdb_read_sqldb(void) // sql item_db read, shortened version of map-server item_db read [Valaris]
  124. {
  125. unsigned short nameid;
  126. struct item_data *id;
  127. char *item_db_name[] = { item_db_db, item_db2_db };
  128. long unsigned int ln = 0;
  129. int i;
  130. // ----------
  131. for (i = 0; i < 2; i++) {
  132. sprintf(tmp_sql, "SELECT * FROM `%s`", item_db_name[i]);
  133. // Execute the query; if the query execution succeeded...
  134. if (mysql_query(&mysql_handle, tmp_sql) == 0) {
  135. sql_res = mysql_store_result(&mysql_handle);
  136. // If the storage of the query result succeeded...
  137. if (sql_res) {
  138. // Parse each row in the query result into sql_row
  139. while ((sql_row = mysql_fetch_row(sql_res)))
  140. { /*Table structure is:
  141. 00 id
  142. 01 name_english
  143. 02 name_japanese
  144. 03 type
  145. ...
  146. */
  147. nameid = atoi(sql_row[0]);
  148. // If the identifier is not within the valid range, process the next row
  149. if (nameid == 0)
  150. continue;
  151. ln++;
  152. // ----------
  153. id=itemdb_search(nameid);
  154. strncpy(id->name, sql_row[1], ITEM_NAME_LENGTH-1);
  155. strncpy(id->jname, sql_row[2], ITEM_NAME_LENGTH-1);
  156. id->type = atoi(sql_row[3]);
  157. if (id->type == IT_DELAYCONSUME)
  158. id->type = IT_USABLE;
  159. }
  160. ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", ln, item_db_name[i]);
  161. ln = 0;
  162. } else {
  163. ShowSQL("DB error (%s) - %s\n",item_db_name[i], mysql_error(&mysql_handle));
  164. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  165. }
  166. // Free the query result
  167. mysql_free_result(sql_res);
  168. } else {
  169. ShowSQL("DB error (%s) - %s\n",item_db_name[i], mysql_error(&mysql_handle));
  170. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  171. }
  172. }
  173. return 0;
  174. }
  175. /*==========================================
  176. *
  177. *------------------------------------------
  178. */
  179. void do_final_itemdb(void)
  180. {
  181. if(item_db){
  182. item_db->destroy(item_db,NULL);
  183. item_db=NULL;
  184. }
  185. }
  186. int do_init_itemdb(void)
  187. {
  188. item_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  189. if (db_use_sqldbs) // it db_use_sqldbs in inter config are yes, will read from item_db for char server display [Valaris]
  190. itemdb_read_sqldb();
  191. else
  192. itemdb_readdb();
  193. return 0;
  194. }