char.cpp 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #pragma warning(disable:4800)
  4. #include "char.hpp"
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include "../common/cbasetypes.hpp"
  11. #include "../common/cli.hpp"
  12. #include "../common/core.hpp"
  13. #include "../common/db.hpp"
  14. #include "../common/malloc.hpp"
  15. #include "../common/mapindex.hpp"
  16. #include "../common/mmo.hpp"
  17. #include "../common/random.hpp"
  18. #include "../common/showmsg.hpp"
  19. #include "../common/socket.hpp"
  20. #include "../common/strlib.hpp"
  21. #include "../common/timer.hpp"
  22. #include "char_clif.hpp"
  23. #include "char_cnslif.hpp"
  24. #include "char_logif.hpp"
  25. #include "char_mapif.hpp"
  26. #include "inter.hpp"
  27. #include "int_elemental.hpp"
  28. #include "int_guild.hpp"
  29. #include "int_homun.hpp"
  30. #include "int_mail.hpp"
  31. #include "int_mercenary.hpp"
  32. #include "int_party.hpp"
  33. #include "int_storage.hpp"
  34. #include "packets.hpp"
  35. //definition of exported var declared in header
  36. int login_fd=-1; //login file descriptor
  37. int char_fd=-1; //char file descriptor
  38. struct Schema_Config schema_config;
  39. struct CharServ_Config charserv_config;
  40. struct mmo_map_server map_server[MAX_MAP_SERVERS];
  41. //Custom limits for the fame lists. [Skotlex]
  42. int fame_list_size_chemist = MAX_FAME_LIST;
  43. int fame_list_size_smith = MAX_FAME_LIST;
  44. int fame_list_size_taekwon = MAX_FAME_LIST;
  45. // Char-server-side stored fame lists [DracoRPG]
  46. struct fame_list smith_fame_list[MAX_FAME_LIST];
  47. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  48. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  49. #define CHAR_MAX_MSG 300 //max number of msg_conf
  50. static char* msg_table[CHAR_MAX_MSG]; // Login Server messages_conf
  51. // check for exit signal
  52. // 0 is saving complete
  53. // other is char_id
  54. unsigned int save_flag = 0;
  55. // Advanced subnet check [LuzZza]
  56. struct s_subnet {
  57. uint32 mask;
  58. uint32 char_ip;
  59. uint32 map_ip;
  60. } subnet[16];
  61. int subnet_count = 0;
  62. DBMap* auth_db; // uint32 account_id -> struct auth_node*
  63. DBMap* online_char_db; // uint32 account_id -> struct online_char_data*
  64. DBMap* char_db_; // uint32 char_id -> struct mmo_charstatus*
  65. DBMap* char_get_authdb() { return auth_db; }
  66. DBMap* char_get_onlinedb() { return online_char_db; }
  67. DBMap* char_get_chardb() { return char_db_; }
  68. /**
  69. * @see DBCreateData
  70. */
  71. DBData char_create_online_data(DBKey key, va_list args){
  72. struct online_char_data* character;
  73. CREATE(character, struct online_char_data, 1);
  74. character->account_id = key.i;
  75. character->char_id = -1;
  76. character->server = -1;
  77. character->fd = -1;
  78. character->waiting_disconnect = INVALID_TIMER;
  79. return db_ptr2data(character);
  80. }
  81. void char_set_charselect(uint32 account_id) {
  82. struct online_char_data* character;
  83. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, char_create_online_data);
  84. if( character->server > -1 )
  85. if( map_server[character->server].users > 0 ) // Prevent this value from going negative.
  86. map_server[character->server].users--;
  87. character->char_id = -1;
  88. character->server = -1;
  89. if(character->waiting_disconnect != INVALID_TIMER) {
  90. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  91. character->waiting_disconnect = INVALID_TIMER;
  92. }
  93. chlogif_send_setacconline(account_id);
  94. }
  95. void char_set_char_online(int map_id, uint32 char_id, uint32 account_id) {
  96. struct online_char_data* character;
  97. struct mmo_charstatus *cp;
  98. //Update DB
  99. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1', `last_login`=NOW() WHERE `char_id`='%d' LIMIT 1", schema_config.char_db, char_id) )
  100. Sql_ShowDebug(sql_handle);
  101. //Check to see for online conflicts
  102. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, char_create_online_data);
  103. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  104. {
  105. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  106. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  107. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  108. }
  109. //Update state data
  110. character->char_id = char_id;
  111. character->server = map_id;
  112. if( character->server > -1 )
  113. map_server[character->server].users++;
  114. //Get rid of disconnect timer
  115. if(character->waiting_disconnect != INVALID_TIMER) {
  116. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  117. character->waiting_disconnect = INVALID_TIMER;
  118. }
  119. //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  120. cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  121. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  122. //Notify login server
  123. chlogif_send_setacconline(account_id);
  124. }
  125. void char_set_char_offline(uint32 char_id, uint32 account_id){
  126. struct online_char_data* character;
  127. if ( char_id == -1 )
  128. {
  129. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", schema_config.char_db, account_id) )
  130. Sql_ShowDebug(sql_handle);
  131. }
  132. else
  133. {
  134. struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  135. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  136. if (cp)
  137. idb_remove(char_db_,char_id);
  138. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d' LIMIT 1", schema_config.char_db, char_id) )
  139. Sql_ShowDebug(sql_handle);
  140. }
  141. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  142. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  143. if( character->server > -1 )
  144. if( map_server[character->server].users > 0 ) // Prevent this value from going negative.
  145. map_server[character->server].users--;
  146. if(character->waiting_disconnect != INVALID_TIMER){
  147. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  148. character->waiting_disconnect = INVALID_TIMER;
  149. }
  150. if(character->char_id == char_id)
  151. {
  152. character->char_id = -1;
  153. character->server = -1;
  154. // needed if player disconnects completely since Skotlex did not want to free the session
  155. character->pincode_success = false;
  156. }
  157. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  158. }
  159. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  160. if (char_id == -1 || character == NULL || character->fd == -1){
  161. chlogif_send_setaccoffline(login_fd,account_id);
  162. }
  163. }
  164. /**
  165. * @see DBApply
  166. */
  167. int char_db_setoffline(DBKey key, DBData *data, va_list ap) {
  168. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  169. int server = va_arg(ap, int);
  170. if (server == -1) {
  171. character->char_id = -1;
  172. character->server = -1;
  173. if(character->waiting_disconnect != INVALID_TIMER){
  174. delete_timer(character->waiting_disconnect, char_chardb_waiting_disconnect);
  175. character->waiting_disconnect = INVALID_TIMER;
  176. }
  177. } else if (character->server == server)
  178. character->server = -2; //In some map server that we aren't connected to.
  179. return 0;
  180. }
  181. /**
  182. * @see DBApply
  183. */
  184. int char_db_kickoffline(DBKey key, DBData *data, va_list ap){
  185. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  186. int server_id = va_arg(ap, int);
  187. if (server_id > -1 && character->server != server_id)
  188. return 0;
  189. //Kick out any connected characters, and set them offline as appropriate.
  190. if (character->server > -1)
  191. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 1);
  192. else if (character->waiting_disconnect == INVALID_TIMER)
  193. char_set_char_offline(character->char_id, character->account_id);
  194. else
  195. return 0; // fail
  196. return 1;
  197. }
  198. void char_set_all_offline(int id){
  199. if (id < 0)
  200. ShowNotice("Sending all users offline.\n");
  201. else
  202. ShowNotice("Sending users of map-server %d offline.\n",id);
  203. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  204. if (id >= 0 || !chlogif_isconnected())
  205. return;
  206. //Tell login-server to also mark all our characters as offline.
  207. chlogif_send_setallaccoffline(login_fd);
  208. }
  209. void char_set_all_offline_sql(void){
  210. //Set all players to 'OFFLINE'
  211. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", schema_config.char_db) )
  212. Sql_ShowDebug(sql_handle);
  213. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", schema_config.guild_db) )
  214. Sql_ShowDebug(sql_handle);
  215. }
  216. /**
  217. * @see DBCreateData
  218. */
  219. DBData char_create_charstatus(DBKey key, va_list args) {
  220. struct mmo_charstatus *cp;
  221. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  222. cp->char_id = key.i;
  223. return db_ptr2data(cp);
  224. }
  225. int char_mmo_char_tosql(uint32 char_id, struct mmo_charstatus* p){
  226. int i = 0;
  227. int count = 0;
  228. int diff = 0;
  229. char save_status[128]; //For displaying save information. [Skotlex]
  230. struct mmo_charstatus *cp;
  231. int errors = 0; //If there are any errors while saving, "cp" will not be updated at the end.
  232. StringBuf buf;
  233. if (char_id!=p->char_id) return 0;
  234. cp = (struct mmo_charstatus *)idb_ensure(char_db_, char_id, char_create_charstatus);
  235. StringBuf_Init(&buf);
  236. memset(save_status, 0, sizeof(save_status));
  237. if (
  238. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  239. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  240. (p->zeny != cp->zeny) ||
  241. (p->last_point.map != cp->last_point.map) ||
  242. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  243. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  244. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  245. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  246. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  247. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  248. (p->option != cp->option) ||
  249. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  250. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) ||
  251. (p->ele_id != cp->ele_id) || (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  252. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom) || (p->delete_date != cp->delete_date) ||
  253. (p->rename != cp->rename) || (p->robe != cp->robe) || (p->character_moves != cp->character_moves) ||
  254. (p->unban_time != cp->unban_time) || (p->font != cp->font) || (p->uniqueitem_counter != cp->uniqueitem_counter) ||
  255. (p->hotkey_rowshift != cp->hotkey_rowshift) || (p->clan_id != cp->clan_id ) || (p->title_id != cp->title_id) ||
  256. (p->show_equip != cp->show_equip) || (p->hotkey_rowshift2 != cp->hotkey_rowshift2) ||
  257. (p->max_ap != cp->max_ap) || (p->ap != cp->ap) || (p->trait_point != cp->trait_point) ||
  258. (p->pow != cp->pow) || (p->sta != cp->sta) || (p->wis != cp->wis) ||
  259. (p->spl != cp->spl) || (p->con != cp->con) || (p->crt != cp->crt)
  260. )
  261. { //Save status
  262. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  263. "`base_exp`='%" PRIu64 "', `job_exp`='%" PRIu64 "', `zeny`='%d',"
  264. "`max_hp`='%u',`hp`='%u',`max_sp`='%u',`sp`='%u',`status_point`='%d',`skill_point`='%d',"
  265. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  266. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',`elemental_id`='%d',"
  267. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  268. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d',"
  269. "`delete_date`='%lu',`robe`='%d',`moves`='%d',`font`='%u',`uniqueitem_counter`='%u',"
  270. "`hotkey_rowshift`='%d', `clan_id`='%d', `title_id`='%lu', `show_equip`='%d', `hotkey_rowshift2`='%d',"
  271. "`max_ap`='%u',`ap`='%u',`trait_point`='%d',"
  272. "`pow`='%d',`sta`='%d',`wis`='%d',`spl`='%d',`con`='%d',`crt`='%d'"
  273. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  274. schema_config.char_db, p->base_level, p->job_level,
  275. p->base_exp, p->job_exp, p->zeny,
  276. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  277. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  278. p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, p->ele_id,
  279. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  280. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  281. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename,
  282. (unsigned long)p->delete_date, // FIXME: platform-dependent size
  283. p->robe, p->character_moves, p->font, p->uniqueitem_counter,
  284. p->hotkey_rowshift, p->clan_id, p->title_id, p->show_equip, p->hotkey_rowshift2,
  285. p->max_ap, p->ap, p->trait_point,
  286. p->pow, p->sta, p->wis, p->spl, p->con, p->crt,
  287. p->account_id, p->char_id) )
  288. {
  289. Sql_ShowDebug(sql_handle);
  290. errors++;
  291. } else
  292. strcat(save_status, " status");
  293. }
  294. //Values that will seldom change (to speed up saving)
  295. if (
  296. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  297. (p->body != cp->body) || (p->class_ != cp->class_) ||
  298. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  299. (p->mother != cp->mother) || (p->child != cp->child) ||
  300. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  301. (p->fame != cp->fame) || (p->inventory_slots != cp->inventory_slots)
  302. )
  303. {
  304. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',"
  305. "`hair`='%d', `hair_color`='%d', `clothes_color`='%d', `body`='%d',"
  306. "`partner_id`='%u', `father`='%u', `mother`='%u', `child`='%u',"
  307. "`karma`='%d',`manner`='%d', `fame`='%d', `inventory_slots`='%hu'"
  308. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  309. schema_config.char_db, p->class_,
  310. p->hair, p->hair_color, p->clothes_color, p->body,
  311. p->partner_id, p->father, p->mother, p->child,
  312. p->karma, p->manner, p->fame, p->inventory_slots,
  313. p->account_id, p->char_id) )
  314. {
  315. Sql_ShowDebug(sql_handle);
  316. errors++;
  317. } else
  318. strcat(save_status, " status2");
  319. }
  320. /* Mercenary Owner */
  321. if( (p->mer_id != cp->mer_id) ||
  322. (p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) ||
  323. (p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) ||
  324. (p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) )
  325. {
  326. if (mercenary_owner_tosql(char_id, p))
  327. strcat(save_status, " mercenary");
  328. else
  329. errors++;
  330. }
  331. //memo points
  332. if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
  333. {
  334. char esc_mapname[NAME_LENGTH*2+1];
  335. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  336. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.memo_db, p->char_id) )
  337. {
  338. Sql_ShowDebug(sql_handle);
  339. errors++;
  340. }
  341. //insert here.
  342. StringBuf_Clear(&buf);
  343. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", schema_config.memo_db);
  344. for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
  345. {
  346. if( p->memo_point[i].map )
  347. {
  348. if( count )
  349. StringBuf_AppendStr(&buf, ",");
  350. Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
  351. StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
  352. ++count;
  353. }
  354. }
  355. if( count )
  356. {
  357. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  358. {
  359. Sql_ShowDebug(sql_handle);
  360. errors++;
  361. }
  362. }
  363. strcat(save_status, " memo");
  364. }
  365. //skills
  366. if( memcmp(p->skill, cp->skill, sizeof(p->skill)) )
  367. {
  368. //`skill` (`char_id`, `id`, `lv`)
  369. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.skill_db, p->char_id) )
  370. {
  371. Sql_ShowDebug(sql_handle);
  372. errors++;
  373. }
  374. StringBuf_Clear(&buf);
  375. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`,`flag`) VALUES ", schema_config.skill_db);
  376. //insert here.
  377. for( i = 0, count = 0; i < MAX_SKILL; ++i ) {
  378. if( p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY ) {
  379. if( p->skill[i].lv == 0 && ( p->skill[i].flag == SKILL_FLAG_PERM_GRANTED || p->skill[i].flag == SKILL_FLAG_PERMANENT ) )
  380. continue;
  381. if( p->skill[i].flag != SKILL_FLAG_PERMANENT && p->skill[i].flag != SKILL_FLAG_PERM_GRANTED && (p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0) == 0 )
  382. continue;
  383. if( count )
  384. StringBuf_AppendStr(&buf, ",");
  385. StringBuf_Printf(&buf, "('%d','%d','%d','%d')", char_id, p->skill[i].id,
  386. ( (p->skill[i].flag == SKILL_FLAG_PERMANENT || p->skill[i].flag == SKILL_FLAG_PERM_GRANTED) ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0),
  387. p->skill[i].flag == SKILL_FLAG_PERM_GRANTED ? p->skill[i].flag : 0);/* other flags do not need to be saved */
  388. ++count;
  389. }
  390. }
  391. if( count )
  392. {
  393. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  394. {
  395. Sql_ShowDebug(sql_handle);
  396. errors++;
  397. }
  398. }
  399. strcat(save_status, " skills");
  400. }
  401. diff = 0;
  402. for(i = 0; i < MAX_FRIENDS; i++){
  403. if(p->friends[i].char_id != cp->friends[i].char_id ||
  404. p->friends[i].account_id != cp->friends[i].account_id){
  405. diff = 1;
  406. break;
  407. }
  408. }
  409. if(diff == 1)
  410. { //Save friends
  411. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.friend_db, char_id) )
  412. {
  413. Sql_ShowDebug(sql_handle);
  414. errors++;
  415. }
  416. StringBuf_Clear(&buf);
  417. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_id`) VALUES ", schema_config.friend_db);
  418. for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
  419. {
  420. if( p->friends[i].char_id > 0 )
  421. {
  422. if( count )
  423. StringBuf_AppendStr(&buf, ",");
  424. StringBuf_Printf(&buf, "('%d','%d')", char_id, p->friends[i].char_id);
  425. count++;
  426. }
  427. }
  428. if( count )
  429. {
  430. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  431. {
  432. Sql_ShowDebug(sql_handle);
  433. errors++;
  434. }
  435. }
  436. strcat(save_status, " friends");
  437. }
  438. #ifdef HOTKEY_SAVING
  439. // hotkeys
  440. StringBuf_Clear(&buf);
  441. StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", schema_config.hotkey_db);
  442. diff = 0;
  443. for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
  444. if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
  445. {
  446. if( diff )
  447. StringBuf_AppendStr(&buf, ",");// not the first hotkey
  448. StringBuf_Printf(&buf, "('%d','%u','%u','%u','%u')", char_id, (unsigned int)i, (unsigned int)p->hotkeys[i].type, p->hotkeys[i].id , (unsigned int)p->hotkeys[i].lv);
  449. diff = 1;
  450. }
  451. }
  452. if(diff) {
  453. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  454. {
  455. Sql_ShowDebug(sql_handle);
  456. errors++;
  457. } else
  458. strcat(save_status, " hotkeys");
  459. }
  460. #endif
  461. StringBuf_Destroy(&buf);
  462. if (save_status[0]!='\0' && charserv_config.save_log)
  463. ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
  464. if (!errors)
  465. memcpy(cp, p, sizeof(struct mmo_charstatus));
  466. return 0;
  467. }
  468. /// Saves an array of 'item' entries into the specified table.
  469. int char_memitemdata_to_sql(const struct item items[], int max, int id, enum storage_type tableswitch, uint8 stor_id) {
  470. StringBuf buf;
  471. SqlStmt* stmt;
  472. int i, j, offset = 0, errors = 0;
  473. const char *tablename, *selectoption, *printname;
  474. struct item item; // temp storage variable
  475. bool* flag; // bit array for inventory matching
  476. bool found;
  477. switch (tableswitch) {
  478. case TABLE_INVENTORY:
  479. printname = "Inventory";
  480. tablename = schema_config.inventory_db;
  481. selectoption = "char_id";
  482. break;
  483. case TABLE_CART:
  484. printname = "Cart";
  485. tablename = schema_config.cart_db;
  486. selectoption = "char_id";
  487. break;
  488. case TABLE_STORAGE:
  489. printname = inter_premiumStorage_getPrintableName(stor_id);
  490. tablename = inter_premiumStorage_getTableName(stor_id);
  491. selectoption = "account_id";
  492. break;
  493. case TABLE_GUILD_STORAGE:
  494. printname = "Guild Storage";
  495. tablename = schema_config.guild_storage_db;
  496. selectoption = "guild_id";
  497. break;
  498. default:
  499. ShowError("Invalid table name!\n");
  500. return 1;
  501. }
  502. // The following code compares inventory with current database values
  503. // and performs modification/deletion/insertion only on relevant rows.
  504. // This approach is more complicated than a trivial delete&insert, but
  505. // it significantly reduces cpu load on the database server.
  506. StringBuf_Init(&buf);
  507. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`, `enchantgrade`");
  508. if (tableswitch == TABLE_INVENTORY) {
  509. StringBuf_Printf(&buf, ", `favorite`, `equip_switch`");
  510. offset = 2;
  511. }
  512. for( i = 0; i < MAX_SLOTS; ++i )
  513. StringBuf_Printf(&buf, ", `card%d`", i);
  514. for( i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  515. StringBuf_Printf(&buf, ", `option_id%d`", i);
  516. StringBuf_Printf(&buf, ", `option_val%d`", i);
  517. StringBuf_Printf(&buf, ", `option_parm%d`", i);
  518. }
  519. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id);
  520. stmt = SqlStmt_Malloc(sql_handle);
  521. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  522. || SQL_ERROR == SqlStmt_Execute(stmt) )
  523. {
  524. SqlStmt_ShowDebug(stmt);
  525. SqlStmt_Free(stmt);
  526. StringBuf_Destroy(&buf);
  527. return 1;
  528. }
  529. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  530. SqlStmt_BindColumn(stmt, 1, SQLDT_UINT, &item.nameid, 0, NULL, NULL);
  531. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  532. SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &item.equip, 0, NULL, NULL);
  533. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  534. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  535. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  536. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  537. SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &item.bound, 0, NULL, NULL);
  538. SqlStmt_BindColumn(stmt, 9, SQLDT_UINT64, &item.unique_id, 0, NULL, NULL);
  539. SqlStmt_BindColumn(stmt,10, SQLDT_INT8, &item.enchantgrade,0, NULL, NULL);
  540. if (tableswitch == TABLE_INVENTORY){
  541. SqlStmt_BindColumn(stmt, 11, SQLDT_CHAR, &item.favorite, 0, NULL, NULL);
  542. SqlStmt_BindColumn(stmt, 12, SQLDT_UINT, &item.equipSwitch, 0, NULL, NULL);
  543. }
  544. for( i = 0; i < MAX_SLOTS; ++i )
  545. SqlStmt_BindColumn(stmt, 11+offset+i, SQLDT_UINT, &item.card[i], 0, NULL, NULL);
  546. for( i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  547. SqlStmt_BindColumn(stmt, 11+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].id, 0, NULL, NULL);
  548. SqlStmt_BindColumn(stmt, 12+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].value, 0, NULL, NULL);
  549. SqlStmt_BindColumn(stmt, 13+offset+MAX_SLOTS+i*3, SQLDT_CHAR, &item.option[i].param, 0, NULL, NULL);
  550. }
  551. // bit array indicating which inventory items have already been matched
  552. flag = (bool*) aCalloc(max, sizeof(bool));
  553. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  554. {
  555. found = false;
  556. // search for the presence of the item in the char's inventory
  557. for( i = 0; i < max; ++i )
  558. {
  559. // skip empty and already matched entries
  560. if( items[i].nameid == 0 || flag[i] )
  561. continue;
  562. if( items[i].nameid == item.nameid
  563. && (memcmp(items[i].card, item.card, sizeof(int) * MAX_SLOTS) == 0)
  564. && (memcmp(items[i].option, item.option, 5 * MAX_ITEM_RDM_OPT) == 0)
  565. && items[i].unique_id == item.unique_id
  566. ) { //They are the same item.
  567. int k;
  568. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  569. ARR_FIND( 0, MAX_ITEM_RDM_OPT, k, items[i].option[k].id != item.option[k].id || items[i].option[k].value != item.option[k].value || items[i].option[k].param != item.option[k].param );
  570. if( j == MAX_SLOTS &&
  571. k == MAX_ITEM_RDM_OPT &&
  572. items[i].amount == item.amount &&
  573. items[i].equip == item.equip &&
  574. items[i].identify == item.identify &&
  575. items[i].refine == item.refine &&
  576. items[i].attribute == item.attribute &&
  577. items[i].expire_time == item.expire_time &&
  578. items[i].bound == item.bound &&
  579. items[i].enchantgrade == item.enchantgrade &&
  580. (tableswitch != TABLE_INVENTORY || (items[i].favorite == item.favorite && items[i].equipSwitch == item.equipSwitch)) )
  581. ; //Do nothing.
  582. else
  583. {
  584. // update all fields.
  585. StringBuf_Clear(&buf);
  586. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%u', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `bound`='%d', `unique_id`='%" PRIu64 "', `enchantgrade`='%d'",
  587. tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound, items[i].unique_id, items[i].enchantgrade);
  588. if (tableswitch == TABLE_INVENTORY)
  589. StringBuf_Printf(&buf, ", `favorite`='%d', `equip_switch`='%u'", items[i].favorite, items[i].equipSwitch);
  590. for( j = 0; j < MAX_SLOTS; ++j )
  591. StringBuf_Printf(&buf, ", `card%d`=%u", j, items[i].card[j]);
  592. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  593. StringBuf_Printf(&buf, ", `option_id%d`=%d", j, items[i].option[j].id);
  594. StringBuf_Printf(&buf, ", `option_val%d`=%d", j, items[i].option[j].value);
  595. StringBuf_Printf(&buf, ", `option_parm%d`=%d", j, items[i].option[j].param);
  596. }
  597. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  598. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  599. {
  600. Sql_ShowDebug(sql_handle);
  601. errors++;
  602. }
  603. }
  604. found = flag[i] = true; //Item dealt with,
  605. break; //skip to next item in the db.
  606. }
  607. }
  608. if( !found )
  609. {// Item not present in inventory, remove it.
  610. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", tablename, item.id) )
  611. {
  612. Sql_ShowDebug(sql_handle);
  613. errors++;
  614. }
  615. }
  616. }
  617. SqlStmt_Free(stmt);
  618. StringBuf_Clear(&buf);
  619. StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`, `enchantgrade`", tablename, selectoption);
  620. if (tableswitch == TABLE_INVENTORY)
  621. StringBuf_Printf(&buf, ", `favorite`, `equip_switch`");
  622. for( j = 0; j < MAX_SLOTS; ++j )
  623. StringBuf_Printf(&buf, ", `card%d`", j);
  624. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  625. StringBuf_Printf(&buf, ", `option_id%d`", j);
  626. StringBuf_Printf(&buf, ", `option_val%d`", j);
  627. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  628. }
  629. StringBuf_AppendStr(&buf, ") VALUES ");
  630. found = false;
  631. // insert non-matched items into the db as new items
  632. for( i = 0; i < max; ++i )
  633. {
  634. // skip empty and already matched entries
  635. if( items[i].nameid == 0 || flag[i] )
  636. continue;
  637. if( found )
  638. StringBuf_AppendStr(&buf, ",");
  639. else
  640. found = true;
  641. StringBuf_Printf(&buf, "('%d', '%u', '%d', '%u', '%d', '%d', '%d', '%u', '%d', '%" PRIu64 "', '%d'",
  642. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound, items[i].unique_id, items[i].enchantgrade);
  643. if (tableswitch == TABLE_INVENTORY)
  644. StringBuf_Printf(&buf, ", '%d', '%u'", items[i].favorite, items[i].equipSwitch);
  645. for( j = 0; j < MAX_SLOTS; ++j )
  646. StringBuf_Printf(&buf, ", '%u'", items[i].card[j]);
  647. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  648. StringBuf_Printf(&buf, ", '%d'", items[i].option[j].id);
  649. StringBuf_Printf(&buf, ", '%d'", items[i].option[j].value);
  650. StringBuf_Printf(&buf, ", '%d'", items[i].option[j].param);
  651. }
  652. StringBuf_AppendStr(&buf, ")");
  653. }
  654. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  655. {
  656. Sql_ShowDebug(sql_handle);
  657. errors++;
  658. }
  659. ShowInfo("Saved %s (%d) data to table %s for %s: %d\n", printname, stor_id, tablename, selectoption, id);
  660. StringBuf_Destroy(&buf);
  661. aFree(flag);
  662. return errors;
  663. }
  664. bool char_memitemdata_from_sql(struct s_storage* p, int max, int id, enum storage_type tableswitch, uint8 stor_id) {
  665. StringBuf buf;
  666. SqlStmt* stmt;
  667. int i,j, offset = 0, max2;
  668. struct item item, *storage;
  669. const char *tablename, *selectoption, *printname;
  670. switch (tableswitch) {
  671. case TABLE_INVENTORY:
  672. printname = "Inventory";
  673. tablename = schema_config.inventory_db;
  674. selectoption = "char_id";
  675. storage = p->u.items_inventory;
  676. max2 = MAX_INVENTORY;
  677. break;
  678. case TABLE_CART:
  679. printname = "Cart";
  680. tablename = schema_config.cart_db;
  681. selectoption = "char_id";
  682. storage = p->u.items_cart;
  683. max2 = MAX_CART;
  684. break;
  685. case TABLE_STORAGE:
  686. printname = "Storage";
  687. tablename = inter_premiumStorage_getTableName(stor_id);
  688. selectoption = "account_id";
  689. storage = p->u.items_storage;
  690. max2 = inter_premiumStorage_getMax(p->stor_id);
  691. break;
  692. case TABLE_GUILD_STORAGE:
  693. printname = "Guild Storage";
  694. tablename = schema_config.guild_storage_db;
  695. selectoption = "guild_id";
  696. storage = p->u.items_guild;
  697. max2 = inter_guild_storagemax(id);
  698. break;
  699. default:
  700. ShowError("Invalid table name!\n");
  701. return false;
  702. }
  703. memset(p, 0, sizeof(struct s_storage)); //clean up memory
  704. p->id = id;
  705. p->type = tableswitch;
  706. p->stor_id = stor_id;
  707. p->max_amount = max2;
  708. stmt = SqlStmt_Malloc(sql_handle);
  709. if (stmt == NULL) {
  710. SqlStmt_ShowDebug(stmt);
  711. return false;
  712. }
  713. StringBuf_Init(&buf);
  714. StringBuf_AppendStr(&buf, "SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`,`expire_time`,`bound`,`unique_id`,`enchantgrade`");
  715. if (tableswitch == TABLE_INVENTORY) {
  716. StringBuf_Printf(&buf, ", `favorite`, `equip_switch`");
  717. offset = 2;
  718. }
  719. for( j = 0; j < MAX_SLOTS; ++j )
  720. StringBuf_Printf(&buf, ",`card%d`", j);
  721. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  722. StringBuf_Printf(&buf, ", `option_id%d`", j);
  723. StringBuf_Printf(&buf, ", `option_val%d`", j);
  724. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  725. }
  726. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`=? ORDER BY `nameid`", tablename, selectoption );
  727. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  728. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &id, 0)
  729. || SQL_ERROR == SqlStmt_Execute(stmt) )
  730. {
  731. SqlStmt_ShowDebug(stmt);
  732. SqlStmt_Free(stmt);
  733. StringBuf_Destroy(&buf);
  734. return false;
  735. }
  736. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  737. SqlStmt_BindColumn(stmt, 1, SQLDT_UINT, &item.nameid, 0, NULL, NULL);
  738. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  739. SqlStmt_BindColumn(stmt, 3, SQLDT_UINT, &item.equip, 0, NULL, NULL);
  740. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  741. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  742. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  743. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  744. SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &item.bound, 0, NULL, NULL);
  745. SqlStmt_BindColumn(stmt, 9, SQLDT_ULONGLONG, &item.unique_id, 0, NULL, NULL);
  746. SqlStmt_BindColumn(stmt,10, SQLDT_INT8, &item.enchantgrade, 0, NULL, NULL);
  747. if (tableswitch == TABLE_INVENTORY){
  748. SqlStmt_BindColumn(stmt, 11, SQLDT_CHAR, &item.favorite, 0, NULL, NULL);
  749. SqlStmt_BindColumn(stmt, 12, SQLDT_UINT, &item.equipSwitch, 0, NULL, NULL);
  750. }
  751. for( i = 0; i < MAX_SLOTS; ++i )
  752. SqlStmt_BindColumn(stmt, 11+offset+i, SQLDT_UINT, &item.card[i], 0, NULL, NULL);
  753. for( i = 0; i < MAX_ITEM_RDM_OPT; ++i ) {
  754. SqlStmt_BindColumn(stmt, 11+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].id, 0, NULL, NULL);
  755. SqlStmt_BindColumn(stmt, 12+offset+MAX_SLOTS+i*3, SQLDT_SHORT, &item.option[i].value, 0, NULL, NULL);
  756. SqlStmt_BindColumn(stmt, 13+offset+MAX_SLOTS+i*3, SQLDT_CHAR, &item.option[i].param, 0, NULL, NULL);
  757. }
  758. for( i = 0; i < max && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  759. memcpy(&storage[i], &item, sizeof(item));
  760. p->amount = i;
  761. ShowInfo("Loaded %s data from table %s for %s: %d (total: %d)\n", printname, tablename, selectoption, id, p->amount);
  762. SqlStmt_FreeResult(stmt);
  763. SqlStmt_Free(stmt);
  764. StringBuf_Destroy(&buf);
  765. return true;
  766. }
  767. /**
  768. * Returns the correct gender ID for the given character and enum value.
  769. *
  770. * If the per-character sex is defined but not supported by the current packetver, the account gender is returned.
  771. *
  772. * @param sd Character data, if available.
  773. * @param p Character status.
  774. * @param sex Character sex (database enum)
  775. *
  776. * @retval SEX_MALE if the per-character sex is male
  777. * @retval SEX_FEMALE if the per-character sex is female
  778. */
  779. int char_mmo_gender( const struct char_session_data *sd, const struct mmo_charstatus *p, char sex ){
  780. #if PACKETVER >= 20141016
  781. switch( sex ){
  782. case 'M':
  783. return SEX_MALE;
  784. case 'F':
  785. return SEX_FEMALE;
  786. default:
  787. #else
  788. // No matter what the database says, always return the account gender
  789. // Per character gender is not supported before 2014-10-16
  790. #endif
  791. // There are calls to this function that do not contain the session
  792. if( sd == nullptr ){
  793. int i;
  794. // Find player session
  795. ARR_FIND( 0, fd_max, i, session[i] && ( sd = (struct char_session_data*)session[i]->session_data ) && sd->account_id == p->account_id );
  796. if( i >= fd_max ){
  797. ShowWarning( "Session was not found for character '%s' (CID: %d, AID: %d), defaulting gender to male...\n", p->name, p->char_id, p->account_id );
  798. return SEX_MALE;
  799. }
  800. }
  801. #if PACKETVER >= 20141016
  802. ShowWarning( "Found unknown gender '%c' for character '%s' (CID: %d, AID: %d), returning account gender...\n", sex, p->name, p->char_id, p->account_id );
  803. #endif
  804. return sd->sex;
  805. #if PACKETVER >= 20141016
  806. }
  807. #endif
  808. }
  809. int char_mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p);
  810. //=====================================================================================================
  811. // Loads the basic character rooster for the given account. Returns total buffer used.
  812. int char_mmo_chars_fromsql(struct char_session_data* sd, uint8* buf, uint8* count ) {
  813. SqlStmt* stmt;
  814. struct mmo_charstatus p;
  815. int j = 0, i;
  816. char last_map[MAP_NAME_LENGTH_EXT];
  817. char sex[2];
  818. stmt = SqlStmt_Malloc(sql_handle);
  819. if( stmt == NULL ) {
  820. SqlStmt_ShowDebug(stmt);
  821. return 0;
  822. }
  823. memset(&p, 0, sizeof(p));
  824. for( i = 0; i < MAX_CHARS; i++ ) {
  825. sd->found_char[i] = -1;
  826. sd->unban_time[i] = 0;
  827. }
  828. // read char data
  829. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  830. "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  831. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  832. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`,"
  833. "`clothes_color`,`body`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`rename`,`delete_date`,"
  834. "`robe`,`moves`,`unban_time`,`font`,`uniqueitem_counter`,`sex`,`hotkey_rowshift`,`title_id`,`show_equip`,"
  835. "`hotkey_rowshift2`,"
  836. "`max_ap`,`ap`,`trait_point`,`pow`,`sta`,`wis`,`spl`,`con`,`crt`,"
  837. "`inventory_slots`"
  838. " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", schema_config.char_db, sd->account_id, MAX_CHARS)
  839. || SQL_ERROR == SqlStmt_Execute(stmt)
  840. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL)
  841. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL)
  842. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL)
  843. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL)
  844. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL)
  845. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL)
  846. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT64, &p.base_exp, 0, NULL, NULL)
  847. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT64, &p.job_exp, 0, NULL, NULL)
  848. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL)
  849. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL)
  850. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL)
  851. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL)
  852. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL)
  853. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL)
  854. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL)
  855. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_UINT, &p.max_hp, 0, NULL, NULL)
  856. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_UINT, &p.hp, 0, NULL, NULL)
  857. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_UINT, &p.max_sp, 0, NULL, NULL)
  858. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_UINT, &p.sp, 0, NULL, NULL)
  859. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p.status_point, 0, NULL, NULL)
  860. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p.skill_point, 0, NULL, NULL)
  861. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL)
  862. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL)
  863. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL)
  864. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL)
  865. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL)
  866. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL)
  867. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.body, 0, NULL, NULL)
  868. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.weapon, 0, NULL, NULL)
  869. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.shield, 0, NULL, NULL)
  870. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_top, 0, NULL, NULL)
  871. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL)
  872. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL)
  873. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  874. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p.rename, 0, NULL, NULL)
  875. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_UINT32, &p.delete_date, 0, NULL, NULL)
  876. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p.robe, 0, NULL, NULL)
  877. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_UINT, &p.character_moves, 0, NULL, NULL)
  878. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_LONG, &p.unban_time, 0, NULL, NULL)
  879. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_UCHAR, &p.font, 0, NULL, NULL)
  880. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_UINT, &p.uniqueitem_counter, 0, NULL, NULL)
  881. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_ENUM, &sex, sizeof(sex), NULL, NULL)
  882. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_UCHAR, &p.hotkey_rowshift, 0, NULL, NULL)
  883. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_ULONG, &p.title_id, 0, NULL, NULL)
  884. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_UINT16, &p.show_equip, 0, NULL, NULL)
  885. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_UCHAR, &p.hotkey_rowshift2, 0, NULL, NULL)
  886. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_UINT, &p.max_ap, 0, NULL, NULL)
  887. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_UINT, &p.ap, 0, NULL, NULL)
  888. || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_UINT, &p.trait_point, 0, NULL, NULL)
  889. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_SHORT, &p.pow, 0, NULL, NULL)
  890. || SQL_ERROR == SqlStmt_BindColumn(stmt, 50, SQLDT_SHORT, &p.sta, 0, NULL, NULL)
  891. || SQL_ERROR == SqlStmt_BindColumn(stmt, 51, SQLDT_SHORT, &p.wis, 0, NULL, NULL)
  892. || SQL_ERROR == SqlStmt_BindColumn(stmt, 52, SQLDT_SHORT, &p.spl, 0, NULL, NULL)
  893. || SQL_ERROR == SqlStmt_BindColumn(stmt, 53, SQLDT_SHORT, &p.con, 0, NULL, NULL)
  894. || SQL_ERROR == SqlStmt_BindColumn(stmt, 54, SQLDT_SHORT, &p.crt, 0, NULL, NULL)
  895. || SQL_ERROR == SqlStmt_BindColumn(stmt, 55, SQLDT_UINT16, &p.inventory_slots, 0, NULL, NULL)
  896. )
  897. {
  898. SqlStmt_ShowDebug(stmt);
  899. SqlStmt_Free(stmt);
  900. return 0;
  901. }
  902. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ )
  903. {
  904. p.last_point.map = mapindex_name2id(last_map);
  905. sd->found_char[p.slot] = p.char_id;
  906. sd->unban_time[p.slot] = p.unban_time;
  907. p.sex = char_mmo_gender(sd, &p, sex[0]);
  908. j += char_mmo_char_tobuf(WBUFP(buf, j), &p);
  909. // Addon System
  910. // store the required info into the session
  911. sd->char_moves[p.slot] = p.character_moves;
  912. }
  913. if( count != nullptr ){
  914. *count = i;
  915. }
  916. memset(sd->new_name,0,sizeof(sd->new_name));
  917. SqlStmt_Free(stmt);
  918. return j;
  919. }
  920. //=====================================================================================================
  921. int char_mmo_char_fromsql(uint32 char_id, struct mmo_charstatus* p, bool load_everything) {
  922. int i;
  923. struct mmo_charstatus* cp;
  924. SqlStmt* stmt;
  925. char last_map[MAP_NAME_LENGTH_EXT];
  926. char save_map[MAP_NAME_LENGTH_EXT];
  927. char point_map[MAP_NAME_LENGTH_EXT];
  928. struct point tmp_point;
  929. struct s_skill tmp_skill;
  930. uint16 skill_count = 0;
  931. struct s_friend tmp_friend;
  932. #ifdef HOTKEY_SAVING
  933. struct hotkey tmp_hotkey;
  934. int hotkey_num;
  935. #endif
  936. StringBuf msg_buf;
  937. char sex[2];
  938. memset(p, 0, sizeof(struct mmo_charstatus));
  939. if (charserv_config.save_log) ShowInfo("Char load request (%d)\n", char_id);
  940. stmt = SqlStmt_Malloc(sql_handle);
  941. if( stmt == NULL )
  942. {
  943. SqlStmt_ShowDebug(stmt);
  944. return 0;
  945. }
  946. // read char data
  947. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  948. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  949. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  950. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,"
  951. "`hair_color`,`clothes_color`,`body`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  952. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`, `moves`,"
  953. "`unban_time`,`font`,`uniqueitem_counter`,`sex`,`hotkey_rowshift`,`clan_id`,`title_id`,`show_equip`,`hotkey_rowshift2`,"
  954. "`max_ap`,`ap`,`trait_point`,`pow`,`sta`,`wis`,`spl`,`con`,`crt`,"
  955. "`inventory_slots`"
  956. " FROM `%s` WHERE `char_id`=? LIMIT 1", schema_config.char_db)
  957. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  958. || SQL_ERROR == SqlStmt_Execute(stmt)
  959. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL)
  960. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL)
  961. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL)
  962. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL)
  963. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL)
  964. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL)
  965. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL)
  966. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT64, &p->base_exp, 0, NULL, NULL)
  967. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT64, &p->job_exp, 0, NULL, NULL)
  968. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL)
  969. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL)
  970. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL)
  971. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL)
  972. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL)
  973. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL)
  974. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL)
  975. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_UINT, &p->max_hp, 0, NULL, NULL)
  976. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_UINT, &p->hp, 0, NULL, NULL)
  977. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_UINT, &p->max_sp, 0, NULL, NULL)
  978. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p->sp, 0, NULL, NULL)
  979. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p->status_point, 0, NULL, NULL)
  980. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p->skill_point, 0, NULL, NULL)
  981. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL)
  982. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL)
  983. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL)
  984. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL)
  985. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL)
  986. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL)
  987. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL)
  988. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_INT, &p->ele_id, 0, NULL, NULL)
  989. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair, 0, NULL, NULL)
  990. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL)
  991. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL)
  992. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->body, 0, NULL, NULL)
  993. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->weapon, 0, NULL, NULL)
  994. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->shield, 0, NULL, NULL)
  995. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_top, 0, NULL, NULL)
  996. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL)
  997. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL)
  998. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  999. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL)
  1000. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL)
  1001. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL)
  1002. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL)
  1003. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL)
  1004. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_UINT32, &p->partner_id, 0, NULL, NULL)
  1005. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_UINT32, &p->father, 0, NULL, NULL)
  1006. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_UINT32, &p->mother, 0, NULL, NULL)
  1007. || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_UINT32, &p->child, 0, NULL, NULL)
  1008. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_INT, &p->fame, 0, NULL, NULL)
  1009. || SQL_ERROR == SqlStmt_BindColumn(stmt, 50, SQLDT_SHORT, &p->rename, 0, NULL, NULL)
  1010. || SQL_ERROR == SqlStmt_BindColumn(stmt, 51, SQLDT_UINT32, &p->delete_date, 0, NULL, NULL)
  1011. || SQL_ERROR == SqlStmt_BindColumn(stmt, 52, SQLDT_SHORT, &p->robe, 0, NULL, NULL)
  1012. || SQL_ERROR == SqlStmt_BindColumn(stmt, 53, SQLDT_UINT32, &p->character_moves, 0, NULL, NULL)
  1013. || SQL_ERROR == SqlStmt_BindColumn(stmt, 54, SQLDT_LONG, &p->unban_time, 0, NULL, NULL)
  1014. || SQL_ERROR == SqlStmt_BindColumn(stmt, 55, SQLDT_UCHAR, &p->font, 0, NULL, NULL)
  1015. || SQL_ERROR == SqlStmt_BindColumn(stmt, 56, SQLDT_UINT, &p->uniqueitem_counter, 0, NULL, NULL)
  1016. || SQL_ERROR == SqlStmt_BindColumn(stmt, 57, SQLDT_ENUM, &sex, sizeof(sex), NULL, NULL)
  1017. || SQL_ERROR == SqlStmt_BindColumn(stmt, 58, SQLDT_UCHAR, &p->hotkey_rowshift, 0, NULL, NULL)
  1018. || SQL_ERROR == SqlStmt_BindColumn(stmt, 59, SQLDT_INT, &p->clan_id, 0, NULL, NULL)
  1019. || SQL_ERROR == SqlStmt_BindColumn(stmt, 60, SQLDT_ULONG, &p->title_id, 0, NULL, NULL)
  1020. || SQL_ERROR == SqlStmt_BindColumn(stmt, 61, SQLDT_UINT16, &p->show_equip, 0, NULL, NULL)
  1021. || SQL_ERROR == SqlStmt_BindColumn(stmt, 62, SQLDT_UCHAR, &p->hotkey_rowshift2, 0, NULL, NULL)
  1022. || SQL_ERROR == SqlStmt_BindColumn(stmt, 63, SQLDT_UINT, &p->max_ap, 0, NULL, NULL)
  1023. || SQL_ERROR == SqlStmt_BindColumn(stmt, 64, SQLDT_UINT, &p->ap, 0, NULL, NULL)
  1024. || SQL_ERROR == SqlStmt_BindColumn(stmt, 65, SQLDT_UINT, &p->trait_point, 0, NULL, NULL)
  1025. || SQL_ERROR == SqlStmt_BindColumn(stmt, 66, SQLDT_SHORT, &p->pow, 0, NULL, NULL)
  1026. || SQL_ERROR == SqlStmt_BindColumn(stmt, 67, SQLDT_SHORT, &p->sta, 0, NULL, NULL)
  1027. || SQL_ERROR == SqlStmt_BindColumn(stmt, 68, SQLDT_SHORT, &p->wis, 0, NULL, NULL)
  1028. || SQL_ERROR == SqlStmt_BindColumn(stmt, 69, SQLDT_SHORT, &p->spl, 0, NULL, NULL)
  1029. || SQL_ERROR == SqlStmt_BindColumn(stmt, 70, SQLDT_SHORT, &p->con, 0, NULL, NULL)
  1030. || SQL_ERROR == SqlStmt_BindColumn(stmt, 71, SQLDT_SHORT, &p->crt, 0, NULL, NULL)
  1031. || SQL_ERROR == SqlStmt_BindColumn(stmt, 72, SQLDT_UINT16, &p->inventory_slots, 0, NULL, NULL)
  1032. )
  1033. {
  1034. SqlStmt_ShowDebug(stmt);
  1035. SqlStmt_Free(stmt);
  1036. return 0;
  1037. }
  1038. if( SQL_ERROR == SqlStmt_NextRow(stmt) )
  1039. {
  1040. ShowError("Requested non-existant character id: %d!\n", char_id);
  1041. SqlStmt_Free(stmt);
  1042. return 0;
  1043. }
  1044. p->sex = char_mmo_gender(NULL, p, sex[0]);
  1045. p->last_point.map = mapindex_name2id(last_map);
  1046. p->save_point.map = mapindex_name2id(save_map);
  1047. if( p->last_point.map == 0 ) {
  1048. p->last_point.map = mapindex_name2id(charserv_config.default_map);
  1049. p->last_point.x = charserv_config.default_map_x;
  1050. p->last_point.y = charserv_config.default_map_y;
  1051. }
  1052. if( p->save_point.map == 0 ) {
  1053. p->save_point.map = mapindex_name2id(charserv_config.default_map);
  1054. p->save_point.x = charserv_config.default_map_x;
  1055. p->save_point.y = charserv_config.default_map_y;
  1056. }
  1057. StringBuf_Init(&msg_buf);
  1058. StringBuf_AppendStr(&msg_buf, " status");
  1059. if (!load_everything) // For quick selection of data when displaying the char menu
  1060. {
  1061. SqlStmt_Free(stmt);
  1062. StringBuf_Destroy(&msg_buf);
  1063. return 1;
  1064. }
  1065. //read memo data
  1066. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  1067. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", schema_config.memo_db, MAX_MEMOPOINTS)
  1068. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1069. || SQL_ERROR == SqlStmt_Execute(stmt)
  1070. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL)
  1071. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL)
  1072. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) )
  1073. SqlStmt_ShowDebug(stmt);
  1074. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1075. {
  1076. tmp_point.map = mapindex_name2id(point_map);
  1077. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  1078. }
  1079. StringBuf_AppendStr(&msg_buf, " memo");
  1080. //read skill
  1081. //`skill` (`char_id`, `id`, `lv`)
  1082. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv`,`flag` FROM `%s` WHERE `char_id`=? LIMIT %d", schema_config.skill_db, MAX_SKILL)
  1083. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1084. || SQL_ERROR == SqlStmt_Execute(stmt)
  1085. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_UINT16, &tmp_skill.id , 0, NULL, NULL)
  1086. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UINT8 , &tmp_skill.lv , 0, NULL, NULL)
  1087. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT8 , &tmp_skill.flag, 0, NULL, NULL) )
  1088. SqlStmt_ShowDebug(stmt);
  1089. if( tmp_skill.flag != SKILL_FLAG_PERM_GRANTED )
  1090. tmp_skill.flag = SKILL_FLAG_PERMANENT;
  1091. for( i = 0; skill_count < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ ) {
  1092. if( tmp_skill.id > 0 && tmp_skill.id < MAX_SKILL_ID ) {
  1093. memcpy(&p->skill[i], &tmp_skill, sizeof(tmp_skill));
  1094. skill_count++;
  1095. }
  1096. else
  1097. ShowWarning("mmo_char_fromsql: ignoring invalid skill (id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", tmp_skill.id, tmp_skill.lv, p->name, p->account_id, p->char_id);
  1098. }
  1099. StringBuf_Printf(&msg_buf, " %d skills", skill_count);
  1100. //read friends
  1101. //`friends` (`char_id`, `friend_id`)
  1102. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT c.`account_id`, c.`char_id`, c.`name` FROM `%s` c LEFT JOIN `%s` f ON f.`friend_id` = c.`char_id` WHERE f.`char_id`=? LIMIT %d", schema_config.char_db, schema_config.friend_db, MAX_FRIENDS)
  1103. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1104. || SQL_ERROR == SqlStmt_Execute(stmt)
  1105. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL)
  1106. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL)
  1107. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) )
  1108. SqlStmt_ShowDebug(stmt);
  1109. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1110. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  1111. StringBuf_AppendStr(&msg_buf, " friends");
  1112. #ifdef HOTKEY_SAVING
  1113. //read hotkeys
  1114. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  1115. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", schema_config.hotkey_db)
  1116. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1117. || SQL_ERROR == SqlStmt_Execute(stmt)
  1118. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL)
  1119. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL)
  1120. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL)
  1121. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) )
  1122. SqlStmt_ShowDebug(stmt);
  1123. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  1124. {
  1125. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS_DB )
  1126. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  1127. else
  1128. ShowWarning("mmo_char_fromsql: ignoring invalid hotkey (hotkey=%d,type=%u,id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", hotkey_num, tmp_hotkey.type, tmp_hotkey.id, tmp_hotkey.lv, p->name, p->account_id, p->char_id);
  1129. }
  1130. StringBuf_AppendStr(&msg_buf, " hotkeys");
  1131. #endif
  1132. /* Mercenary Owner DataBase */
  1133. mercenary_owner_fromsql(char_id, p);
  1134. StringBuf_AppendStr(&msg_buf, " mercenary");
  1135. if (charserv_config.save_log)
  1136. ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, StringBuf_Value(&msg_buf)); //ok. all data load successfully!
  1137. SqlStmt_Free(stmt);
  1138. cp = (struct mmo_charstatus *)idb_ensure(char_db_, char_id, char_create_charstatus);
  1139. memcpy(cp, p, sizeof(struct mmo_charstatus));
  1140. StringBuf_Destroy(&msg_buf);
  1141. return 1;
  1142. }
  1143. //==========================================================================================================
  1144. int char_mmo_sql_init(void) {
  1145. char_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  1146. ShowStatus("Characters per Account: '%d'.\n", charserv_config.char_config.char_per_account);
  1147. //the 'set offline' part is now in check_login_conn ...
  1148. //if the server connects to loginserver
  1149. //it will dc all off players
  1150. //and send the loginserver the new state....
  1151. // Force all users offline in sql when starting char-server
  1152. // (useful when servers crashs and don't clean the database)
  1153. char_set_all_offline_sql();
  1154. return 0;
  1155. }
  1156. //-----------------------------------
  1157. // Function to change chararcter's names
  1158. //-----------------------------------
  1159. int char_rename_char_sql(struct char_session_data *sd, uint32 char_id)
  1160. {
  1161. struct mmo_charstatus char_dat;
  1162. char esc_name[NAME_LENGTH*2+1];
  1163. if( sd->new_name[0] == 0 ) // Not ready for rename
  1164. return 2;
  1165. if( !char_mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed.
  1166. return 2;
  1167. // If the new name is exactly the same as the old one
  1168. if( !strcmp( sd->new_name, char_dat.name ) )
  1169. return 0;
  1170. if( char_dat.rename == 0 )
  1171. return 1;
  1172. if( !charserv_config.char_config.char_rename_party && char_dat.party_id ){
  1173. return 6;
  1174. }
  1175. if( !charserv_config.char_config.char_rename_guild && char_dat.guild_id ){
  1176. return 5;
  1177. }
  1178. Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH));
  1179. switch( char_check_char_name( sd->new_name, esc_name ) ){
  1180. case 0:
  1181. break;
  1182. case -1:
  1183. // character already exists
  1184. return 4;
  1185. default:
  1186. return 8;
  1187. }
  1188. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `name` = '%s', `rename` = '%d' WHERE `char_id` = '%d'", schema_config.char_db, esc_name, --char_dat.rename, char_id) )
  1189. {
  1190. Sql_ShowDebug(sql_handle);
  1191. return 3;
  1192. }
  1193. // Update party and party members with the new player name
  1194. if( char_dat.party_id )
  1195. inter_party_charname_changed(char_dat.party_id, char_id, sd->new_name);
  1196. // Change character's name into guild_db.
  1197. if( char_dat.guild_id )
  1198. inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name);
  1199. safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH);
  1200. memset(sd->new_name,0,sizeof(sd->new_name));
  1201. // log change
  1202. if( charserv_config.log_char )
  1203. {
  1204. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1205. "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')",
  1206. schema_config.charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) )
  1207. Sql_ShowDebug(sql_handle);
  1208. }
  1209. return 0;
  1210. }
  1211. int char_check_char_name(char * name, char * esc_name)
  1212. {
  1213. int i;
  1214. // check length of character name
  1215. if( name[0] == '\0' )
  1216. return -2; // empty character name
  1217. /**
  1218. * The client does not allow you to create names with less than 4 characters, however,
  1219. * the use of WPE can bypass this, and this fixes the exploit.
  1220. **/
  1221. if( strlen( name ) < 4 )
  1222. return -2;
  1223. // check content of character name
  1224. if( remove_control_chars(name) )
  1225. return -2; // control chars in name
  1226. // check for reserved names
  1227. if( strcmpi(name, charserv_config.wisp_server_name) == 0 )
  1228. return -1; // nick reserved for internal server messages
  1229. // check for the channel symbol
  1230. if( name[0] == '#' )
  1231. return -2;
  1232. // Check Authorised letters/symbols in the name of the character
  1233. if( charserv_config.char_config.char_name_option == 1 )
  1234. { // only letters/symbols in char_name_letters are authorised
  1235. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1236. if( strchr(charserv_config.char_config.char_name_letters, name[i]) == NULL )
  1237. return -2;
  1238. }
  1239. else if( charserv_config.char_config.char_name_option == 2 )
  1240. { // letters/symbols in char_name_letters are forbidden
  1241. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1242. if( strchr(charserv_config.char_config.char_name_letters, name[i]) != NULL )
  1243. return -2;
  1244. }
  1245. if( charserv_config.char_config.name_ignoring_case ) {
  1246. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE BINARY `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1247. Sql_ShowDebug(sql_handle);
  1248. return -2;
  1249. }
  1250. } else {
  1251. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1252. Sql_ShowDebug(sql_handle);
  1253. return -2;
  1254. }
  1255. }
  1256. if( Sql_NumRows(sql_handle) > 0 )
  1257. return -1; // name already exists
  1258. return 0;
  1259. }
  1260. //-----------------------------------
  1261. // Function to create a new character
  1262. //-----------------------------------
  1263. int char_make_new_char( struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style, short start_job, int sex ){
  1264. char name[NAME_LENGTH];
  1265. char esc_name[NAME_LENGTH*2+1];
  1266. struct point tmp_start_point[MAX_STARTPOINT];
  1267. struct startitem tmp_start_items[MAX_STARTITEM];
  1268. uint32 char_id;
  1269. int flag, k, start_point_idx = rnd() % charserv_config.start_point_count;
  1270. int status_points;
  1271. safestrncpy(name, name_, NAME_LENGTH);
  1272. normalize_name(name,TRIM_CHARS);
  1273. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1274. memset(tmp_start_point, 0, MAX_STARTPOINT * sizeof(struct point));
  1275. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1276. memcpy(tmp_start_point, charserv_config.start_point, MAX_STARTPOINT * sizeof(struct point));
  1277. memcpy(tmp_start_items, charserv_config.start_items, MAX_STARTITEM * sizeof(struct startitem));
  1278. flag = char_check_char_name(name,esc_name);
  1279. if( flag < 0 )
  1280. return flag;
  1281. // Check inputs from the client - never trust it!
  1282. // Check the slot
  1283. if( slot < 0 || slot >= sd->char_slots ){
  1284. return -4; // invalid slot
  1285. }
  1286. // Check gender
  1287. switch( sex ){
  1288. case SEX_FEMALE:
  1289. sex = 'F';
  1290. break;
  1291. case SEX_MALE:
  1292. sex = 'M';
  1293. break;
  1294. default:
  1295. ShowWarning( "Received unsupported gender '%d'...\n", sex );
  1296. return -2; // invalid input
  1297. }
  1298. // Check status values
  1299. #if PACKETVER < 20120307
  1300. // All stats together always have to add up to a total of 30 points
  1301. if( ( str + agi + vit + int_ + dex + luk ) != 30 ){
  1302. return -2; // invalid input
  1303. }
  1304. // No status can be below 1
  1305. if( str < 1 || agi < 1 || vit < 1 || int_ < 1 || dex < 1 || luk < 1 ){
  1306. return -2; // invalid input
  1307. }
  1308. // No status can be higher than 9
  1309. if( str > 9 || agi > 9 || vit > 9 || int_ > 9 || dex > 9 || luk > 9 ){
  1310. return -2; // invalid input
  1311. }
  1312. // The status pairs always have to add up to a total of 10 points
  1313. if( ( str + int_ ) != 10 || ( agi + luk ) != 10 || ( vit + dex ) != 10 ){
  1314. return -2; // invalid input
  1315. }
  1316. status_points = 0;
  1317. #else
  1318. status_points = charserv_config.start_status_points;
  1319. #endif
  1320. // check the number of already existing chars in this account
  1321. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", schema_config.char_db, sd->account_id) )
  1322. Sql_ShowDebug(sql_handle);
  1323. #ifdef VIP_ENABLE
  1324. if( Sql_NumRows(sql_handle) >= MAX_CHARS )
  1325. return -2; // character account limit exceeded
  1326. #else
  1327. if( Sql_NumRows(sql_handle) >= sd->char_slots )
  1328. return -2; // character account limit exceeded
  1329. #endif
  1330. // check char slot
  1331. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' LIMIT 1", schema_config.char_db, sd->account_id, slot) )
  1332. Sql_ShowDebug(sql_handle);
  1333. if( Sql_NumRows(sql_handle) > 0 )
  1334. return -2; // slot already in use
  1335. // validation success, log result
  1336. if (charserv_config.log_char) {
  1337. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1338. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1339. schema_config.charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1340. Sql_ShowDebug(sql_handle);
  1341. }
  1342. #if PACKETVER >= 20151001
  1343. if(!(start_job == JOB_NOVICE && (charserv_config.allowed_job_flag&1)) &&
  1344. !(start_job == JOB_SUMMONER && (charserv_config.allowed_job_flag&2)))
  1345. return -2; // Invalid job
  1346. // Check for Doram based information.
  1347. if (start_job == JOB_SUMMONER) { // Check for just this job for now.
  1348. memset(tmp_start_point, 0, MAX_STARTPOINT * sizeof(struct point));
  1349. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1350. memcpy(tmp_start_point, charserv_config.start_point_doram, MAX_STARTPOINT * sizeof(struct point));
  1351. memcpy(tmp_start_items, charserv_config.start_items_doram, MAX_STARTITEM * sizeof(struct startitem));
  1352. start_point_idx = rnd() % charserv_config.start_point_count_doram;
  1353. }
  1354. #endif
  1355. //Insert the new char entry to the database
  1356. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `class`, `zeny`, `status_point`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1357. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`, `sex`) VALUES ("
  1358. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%u', '%u', '%u', '%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d', '%c')",
  1359. schema_config.char_db, sd->account_id , slot, esc_name, start_job, charserv_config.start_zeny, status_points, str, agi, vit, int_, dex, luk,
  1360. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1361. mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, mapindex_id2name(tmp_start_point[start_point_idx].map), tmp_start_point[start_point_idx].x, tmp_start_point[start_point_idx].y, sex) )
  1362. {
  1363. Sql_ShowDebug(sql_handle);
  1364. return -2; //No, stop the procedure!
  1365. }
  1366. //Retrieve the newly auto-generated char id
  1367. char_id = (int)Sql_LastInsertId(sql_handle);
  1368. //Give the char the default items
  1369. for (k = 0; k <= MAX_STARTITEM && tmp_start_items[k].nameid != 0; k++) {
  1370. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%u', '%hu', '%u', '%d')", schema_config.inventory_db, char_id, tmp_start_items[k].nameid, tmp_start_items[k].amount, tmp_start_items[k].pos, 1) )
  1371. Sql_ShowDebug(sql_handle);
  1372. }
  1373. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1374. return char_id;
  1375. }
  1376. /*----------------------------------------------------------------------------------------------------------*/
  1377. /* Divorce Players */
  1378. /*----------------------------------------------------------------------------------------------------------*/
  1379. int char_divorce_char_sql(int partner_id1, int partner_id2){
  1380. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d' LIMIT 2", schema_config.char_db, partner_id1, partner_id2) )
  1381. Sql_ShowDebug(sql_handle);
  1382. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%u' OR `nameid`='%u') AND (`char_id`='%d' OR `char_id`='%d') LIMIT 2", schema_config.inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) )
  1383. Sql_ShowDebug(sql_handle);
  1384. chmapif_send_ackdivorce(partner_id1, partner_id2);
  1385. return 0;
  1386. }
  1387. /*----------------------------------------------------------------------------------------------------------*/
  1388. /* Delete char - davidsiaw */
  1389. /*----------------------------------------------------------------------------------------------------------*/
  1390. /* Returns 0 if successful
  1391. * Returns < 0 for error
  1392. */
  1393. enum e_char_del_response char_delete(struct char_session_data* sd, uint32 char_id){
  1394. char name[NAME_LENGTH];
  1395. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1396. uint32 account_id;
  1397. int party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id, elemental_id;
  1398. time_t delete_date;
  1399. char *data;
  1400. size_t len;
  1401. int i;
  1402. ARR_FIND(0, MAX_CHARS, i, sd->found_char[i] == char_id);
  1403. // Such a character does not exist in the account
  1404. if (i == MAX_CHARS) {
  1405. ShowInfo("Char deletion aborted: %s, Account ID: %u, Character ID: %u\n", name, sd->account_id, char_id);
  1406. return CHAR_DELETE_NOTFOUND;
  1407. }
  1408. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `name`,`account_id`,`party_id`,`guild_id`,`base_level`,`homun_id`,`partner_id`,`father`,`mother`,`elemental_id`,`delete_date` FROM `%s` WHERE `account_id`='%u' AND `char_id`='%u'", schema_config.char_db, sd->account_id, char_id)){
  1409. Sql_ShowDebug(sql_handle);
  1410. return CHAR_DELETE_DATABASE;
  1411. }
  1412. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1413. {
  1414. ShowInfo("Char deletion aborted: %s, Account ID: %u, Character ID: %u\n", name, sd->account_id, char_id);
  1415. Sql_FreeResult(sql_handle);
  1416. return CHAR_DELETE_NOTFOUND;
  1417. }
  1418. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1419. Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data);
  1420. Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data);
  1421. Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data);
  1422. Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data);
  1423. Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data);
  1424. Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data);
  1425. Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data);
  1426. Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data);
  1427. Sql_GetData(sql_handle, 9, &data, NULL); elemental_id = atoi(data);
  1428. Sql_GetData(sql_handle,10, &data, NULL); delete_date = strtoul(data, NULL, 10);
  1429. Sql_EscapeStringLen(sql_handle, esc_name, name, zmin(len, NAME_LENGTH));
  1430. Sql_FreeResult(sql_handle);
  1431. //check for config char del condition [Lupus]
  1432. if( ( charserv_config.char_config.char_del_level > 0 && base_level >= charserv_config.char_config.char_del_level )
  1433. || ( charserv_config.char_config.char_del_level < 0 && base_level <= -charserv_config.char_config.char_del_level )
  1434. ) {
  1435. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1436. return CHAR_DELETE_BASELEVEL;
  1437. }
  1438. if (charserv_config.char_config.char_del_restriction&CHAR_DEL_RESTRICT_GUILD && guild_id) // character is in guild
  1439. {
  1440. ShowInfo("Char deletion aborted: %s, Guild ID: %i\n", name, guild_id);
  1441. return CHAR_DELETE_GUILD;
  1442. }
  1443. if (charserv_config.char_config.char_del_restriction&CHAR_DEL_RESTRICT_PARTY && party_id) // character is in party
  1444. {
  1445. ShowInfo("Char deletion aborted: %s, Party ID: %i\n", name, party_id);
  1446. return CHAR_DELETE_PARTY;
  1447. }
  1448. if( charserv_config.char_config.char_del_delay > 0 && ( !delete_date || delete_date > time(NULL) ) ){ // not queued or delay not yet passed
  1449. ShowInfo("Char deletion aborted: %s, Time was not set or has not been reached ye\n", name );
  1450. return CHAR_DELETE_TIME;
  1451. }
  1452. /* Divorce [Wizputer] */
  1453. if( partner_id )
  1454. char_divorce_char_sql(char_id, partner_id);
  1455. /* De-addopt [Zephyrus] */
  1456. if( father_id || mother_id )
  1457. { // Char is Baby
  1458. unsigned char buf[64];
  1459. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", schema_config.char_db, father_id, mother_id) )
  1460. Sql_ShowDebug(sql_handle);
  1461. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410' AND (`char_id`='%d' OR `char_id`='%d')", schema_config.skill_db, father_id, mother_id) )
  1462. Sql_ShowDebug(sql_handle);
  1463. WBUFW(buf,0) = 0x2b25;
  1464. WBUFL(buf,2) = father_id;
  1465. WBUFL(buf,6) = mother_id;
  1466. WBUFL(buf,10) = char_id; // Baby
  1467. chmapif_sendall(buf,14);
  1468. }
  1469. //Make the character leave the party [Skotlex]
  1470. if (party_id)
  1471. inter_party_leave(party_id, account_id, char_id, name);
  1472. /* delete char's pet */
  1473. //Delete the hatched pet if you have one...
  1474. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incubate` = '0'", schema_config.pet_db, char_id) )
  1475. Sql_ShowDebug(sql_handle);
  1476. //Delete all pets that are stored in eggs (inventory + cart)
  1477. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = 256", schema_config.pet_db, schema_config.pet_db, schema_config.inventory_db, schema_config.inventory_db, char_id) )
  1478. Sql_ShowDebug(sql_handle);
  1479. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = 256", schema_config.pet_db, schema_config.pet_db, schema_config.cart_db, schema_config.cart_db, char_id) )
  1480. Sql_ShowDebug(sql_handle);
  1481. /* remove homunculus */
  1482. if( hom_id )
  1483. mapif_homunculus_delete(hom_id);
  1484. /* remove elemental */
  1485. if (elemental_id)
  1486. mapif_elemental_delete(elemental_id);
  1487. /* remove mercenary data */
  1488. mercenary_owner_delete(char_id);
  1489. /* delete char's friends list */
  1490. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.friend_db, char_id) )
  1491. Sql_ShowDebug(sql_handle);
  1492. /* delete char from other's friend list */
  1493. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1494. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", schema_config.friend_db, char_id) )
  1495. Sql_ShowDebug(sql_handle);
  1496. #ifdef HOTKEY_SAVING
  1497. /* delete hotkeys */
  1498. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.hotkey_db, char_id) )
  1499. Sql_ShowDebug(sql_handle);
  1500. #endif
  1501. /* delete inventory */
  1502. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.inventory_db, char_id) )
  1503. Sql_ShowDebug(sql_handle);
  1504. /* delete cart inventory */
  1505. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.cart_db, char_id) )
  1506. Sql_ShowDebug(sql_handle);
  1507. /* delete memo areas */
  1508. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.memo_db, char_id) )
  1509. Sql_ShowDebug(sql_handle);
  1510. /* delete character registry */
  1511. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_str_table, char_id) )
  1512. Sql_ShowDebug(sql_handle);
  1513. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_num_table, char_id) )
  1514. Sql_ShowDebug(sql_handle);
  1515. /* delete skills */
  1516. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.skill_db, char_id) )
  1517. Sql_ShowDebug(sql_handle);
  1518. /* delete mail attachments (only received) */
  1519. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` IN ( SELECT `id` FROM `%s` WHERE `dest_id`='%d' )", schema_config.mail_attachment_db, schema_config.mail_db, char_id))
  1520. Sql_ShowDebug(sql_handle);
  1521. /* delete mails (only received) */
  1522. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `dest_id`='%d'", schema_config.mail_db, char_id))
  1523. Sql_ShowDebug(sql_handle);
  1524. #ifdef ENABLE_SC_SAVING
  1525. /* status changes */
  1526. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", schema_config.scdata_db, account_id, char_id) )
  1527. Sql_ShowDebug(sql_handle);
  1528. #endif
  1529. /* bonus_scripts */
  1530. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.bonus_script_db, char_id) )
  1531. Sql_ShowDebug(sql_handle);
  1532. /* Quest Data */
  1533. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.quest_db, char_id))
  1534. Sql_ShowDebug(sql_handle);
  1535. /* Achievement Data */
  1536. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.achievement_table, char_id))
  1537. Sql_ShowDebug(sql_handle);
  1538. if (charserv_config.log_char) {
  1539. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`char_msg`,`name`) VALUES (NOW(), '%d', '%d', 'Deleted char (CID %d)', '%s')",
  1540. schema_config.charlog_db, account_id, 0, char_id, esc_name) )
  1541. Sql_ShowDebug(sql_handle);
  1542. }
  1543. /* delete character */
  1544. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1545. Sql_ShowDebug(sql_handle);
  1546. /* No need as we used inter_guild_leave [Skotlex]
  1547. // Also delete info from guildtables.
  1548. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1549. Sql_ShowDebug(sql_handle);
  1550. */
  1551. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.guild_db, char_id) )
  1552. Sql_ShowDebug(sql_handle);
  1553. else if( Sql_NumRows(sql_handle) > 0 )
  1554. mapif_parse_BreakGuild(0,guild_id);
  1555. else if( guild_id )
  1556. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1557. // refresh character list cache
  1558. sd->found_char[i] = -1;
  1559. return CHAR_DELETE_OK;
  1560. }
  1561. /**
  1562. * This function parse all map-serv attached to this char-serv and increase user count
  1563. * @return numbers of total users
  1564. */
  1565. int char_count_users(void)
  1566. {
  1567. int i, users;
  1568. users = 0;
  1569. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  1570. if (session_isValid(map_server[i].fd)) {
  1571. users += map_server[i].users;
  1572. }
  1573. }
  1574. return users;
  1575. }
  1576. // Writes char data to the buffer in the format used by the client.
  1577. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1578. // Returns the size
  1579. int char_mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p){
  1580. if( buffer == NULL || p == NULL )
  1581. return 0;
  1582. struct CHARACTER_INFO* info = (struct CHARACTER_INFO*)buffer;
  1583. info->GID = p->char_id;
  1584. #if PACKETVER >= 20170830
  1585. info->exp = u64min( p->base_exp, MAX_EXP );
  1586. #else
  1587. info->exp = (int32)u64min( p->base_exp, MAX_EXP );
  1588. #endif
  1589. info->money = p->zeny;
  1590. #if PACKETVER >= 20170830
  1591. info->jobexp = u64min( p->job_exp, MAX_EXP );
  1592. #else
  1593. info->jobexp = (int32)u64min( p->job_exp, MAX_EXP );
  1594. #endif
  1595. info->joblevel = p->job_level;
  1596. info->bodystate = 0; // probably opt1
  1597. info->healthstate = 0; // probably opt2
  1598. info->effectstate = p->option;
  1599. info->virtue = p->karma;
  1600. info->honor = p->manner;
  1601. info->jobpoint = umin( p->status_point, INT16_MAX );
  1602. info->hp = p->hp;
  1603. info->maxhp = p->max_hp;
  1604. info->sp = min( p->sp, INT16_MAX );
  1605. info->maxsp = min( p->max_sp, INT16_MAX );
  1606. info->speed = DEFAULT_WALK_SPEED; // p->speed;
  1607. info->job = p->class_;
  1608. info->head = p->hair;
  1609. #if PACKETVER >= 20141022
  1610. info->body = p->body;
  1611. #endif
  1612. //When the weapon is sent and your option is riding, the client crashes on login!?
  1613. info->weapon = p->option&(0x20|0x80000|0x100000|0x200000|0x400000|0x800000|0x1000000|0x2000000|0x4000000|0x8000000) ? 0 : p->weapon;
  1614. info->level = p->base_level;
  1615. info->sppoint = umin( p->skill_point, INT16_MAX );
  1616. info->accessory = p->head_bottom;
  1617. info->shield = p->shield;
  1618. info->accessory2 = p->head_top;
  1619. info->accessory3 = p->head_mid;
  1620. info->headpalette = p->hair_color;
  1621. info->bodypalette = p->clothes_color;
  1622. safestrncpy( info->name, p->name, NAME_LENGTH );
  1623. info->Str = (uint8)u16min( p->str, UINT8_MAX );
  1624. info->Agi = (uint8)u16min( p->agi, UINT8_MAX );
  1625. info->Vit = (uint8)u16min( p->vit, UINT8_MAX );
  1626. info->Int = (uint8)u16min( p->int_, UINT8_MAX );
  1627. info->Dex = (uint8)u16min( p->dex, UINT8_MAX );
  1628. info->Luk = (uint8)u16min( p->luk, UINT8_MAX );
  1629. info->CharNum = p->slot;
  1630. info->hairColor = (uint8)u16min( p->hair_color, UINT8_MAX );
  1631. info->bIsChangedCharName = ( p->rename > 0 ) ? 0 : 1;
  1632. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1633. mapindex_getmapname_ext( mapindex_id2name( p->last_point.map ), info->mapName );
  1634. #endif
  1635. #if PACKETVER >= 20100803
  1636. #if PACKETVER_CHAR_DELETEDATE
  1637. info->DelRevDate = ( p->delete_date ? TOL( p->delete_date - time( NULL ) ) : 0 );
  1638. #else
  1639. info->DelRevDate = TOL( p->delete_date );
  1640. #endif
  1641. #endif
  1642. #if PACKETVER >= 20110111
  1643. info->robePalette = p->robe;
  1644. #endif
  1645. #if PACKETVER >= 20110928
  1646. // change slot feature (0 = disabled, otherwise enabled)
  1647. if( charserv_config.charmove_config.char_move_enabled == 0 )
  1648. info->chr_slot_changeCnt = 0;
  1649. else if( charserv_config.charmove_config.char_moves_unlimited )
  1650. info->chr_slot_changeCnt = 1;
  1651. else
  1652. info->chr_slot_changeCnt = max( 0, (int)p->character_moves );
  1653. #endif
  1654. #if PACKETVER >= 20111025
  1655. info->chr_name_changeCnt = ( p->rename > 0 ) ? 1 : 0; // (0 = disabled, otherwise displays "Add-Ons" sidebar)
  1656. #endif
  1657. #if PACKETVER >= 20141016
  1658. info->sex = p->sex; // sex - (0 = female, 1 = male, 99 = logindefined)
  1659. #endif
  1660. return sizeof( struct CHARACTER_INFO );
  1661. }
  1662. int char_married(int pl1, int pl2)
  1663. {
  1664. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, pl1) )
  1665. Sql_ShowDebug(sql_handle);
  1666. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1667. {
  1668. char* data;
  1669. Sql_GetData(sql_handle, 0, &data, NULL);
  1670. if( pl2 == atoi(data) )
  1671. {
  1672. Sql_FreeResult(sql_handle);
  1673. return 1;
  1674. }
  1675. }
  1676. Sql_FreeResult(sql_handle);
  1677. return 0;
  1678. }
  1679. int char_child(int parent_id, int child_id)
  1680. {
  1681. if( parent_id == 0 || child_id == 0) //Failsafe, avoild querys and fix EXP bug dividing with lower level chars
  1682. return 0;
  1683. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, parent_id) )
  1684. Sql_ShowDebug(sql_handle);
  1685. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1686. {
  1687. char* data;
  1688. Sql_GetData(sql_handle, 0, &data, NULL);
  1689. if( child_id == atoi(data) )
  1690. {
  1691. Sql_FreeResult(sql_handle);
  1692. return 1;
  1693. }
  1694. }
  1695. Sql_FreeResult(sql_handle);
  1696. return 0;
  1697. }
  1698. int char_family(int cid1, int cid2, int cid3)
  1699. {
  1700. if ( cid1 == 0 || cid2 == 0 || cid3 == 0 ) //Failsafe, and avoid querys where there is no sense to keep executing if any of the inputs are 0
  1701. return 0;
  1702. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", schema_config.char_db, cid1, cid2, cid3) )
  1703. Sql_ShowDebug(sql_handle);
  1704. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1705. {
  1706. int charid;
  1707. int partnerid;
  1708. int childid;
  1709. char* data;
  1710. Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data);
  1711. Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data);
  1712. Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data);
  1713. if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) ||
  1714. (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) ||
  1715. (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) )
  1716. {
  1717. Sql_FreeResult(sql_handle);
  1718. return childid;
  1719. }
  1720. }
  1721. Sql_FreeResult(sql_handle);
  1722. return 0;
  1723. }
  1724. //----------------------------------------------------------------------
  1725. // Force disconnection of an online player (with account value) by [Yor]
  1726. //----------------------------------------------------------------------
  1727. void char_disconnect_player(uint32 account_id)
  1728. {
  1729. int i;
  1730. struct char_session_data* sd;
  1731. // disconnect player if online on char-server
  1732. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1733. if( i < fd_max )
  1734. set_eof(i);
  1735. }
  1736. /**
  1737. * Set 'flag' value of char_session_data
  1738. * @param account_id
  1739. * @param value
  1740. * @param set True: set the value by using '|= val', False: unset the value by using '&= ~val'
  1741. **/
  1742. void char_set_session_flag_(int account_id, int val, bool set) {
  1743. int i;
  1744. struct char_session_data* sd;
  1745. ARR_FIND(0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id);
  1746. if (i < fd_max) {
  1747. if (set)
  1748. sd->flag |= val;
  1749. else
  1750. sd->flag &= ~val;
  1751. }
  1752. }
  1753. void char_auth_ok(int fd, struct char_session_data *sd) {
  1754. struct online_char_data* character;
  1755. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1756. { // check if character is not online already. [Skotlex]
  1757. if (character->server > -1)
  1758. { //Character already online. KICK KICK KICK
  1759. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  1760. if (character->waiting_disconnect == INVALID_TIMER)
  1761. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, char_chardb_waiting_disconnect, character->account_id, 0);
  1762. chclif_send_auth_result(fd,8);
  1763. return;
  1764. }
  1765. if (session_isValid(character->fd) && character->fd != fd)
  1766. { //There's already a connection from this account that hasn't picked a char yet.
  1767. chclif_send_auth_result(fd,8);
  1768. return;
  1769. }
  1770. character->fd = fd;
  1771. }
  1772. chlogif_send_reqaccdata(login_fd,sd); // request account data
  1773. // mark session as 'authed'
  1774. sd->auth = true;
  1775. // set char online on charserver
  1776. char_set_charselect(sd->account_id);
  1777. // continues when account data is received...
  1778. }
  1779. void char_read_fame_list(void)
  1780. {
  1781. int i;
  1782. char* data;
  1783. size_t len;
  1784. // Empty ranking lists
  1785. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  1786. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  1787. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  1788. // Build Blacksmith ranking list
  1789. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, JOB_MECHANIC, JOB_MECHANIC_T, JOB_BABY_MECHANIC, JOB_MEISTER, fame_list_size_smith) )
  1790. Sql_ShowDebug(sql_handle);
  1791. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1792. {
  1793. // char_id
  1794. Sql_GetData(sql_handle, 0, &data, NULL);
  1795. smith_fame_list[i].id = atoi(data);
  1796. // fame
  1797. Sql_GetData(sql_handle, 1, &data, &len);
  1798. smith_fame_list[i].fame = atoi(data);
  1799. // name
  1800. Sql_GetData(sql_handle, 2, &data, &len);
  1801. memcpy(smith_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1802. }
  1803. // Build Alchemist ranking list
  1804. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, JOB_GENETIC, JOB_GENETIC_T, JOB_BABY_GENETIC, JOB_BIOLO, fame_list_size_chemist) )
  1805. Sql_ShowDebug(sql_handle);
  1806. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1807. {
  1808. // char_id
  1809. Sql_GetData(sql_handle, 0, &data, NULL);
  1810. chemist_fame_list[i].id = atoi(data);
  1811. // fame
  1812. Sql_GetData(sql_handle, 1, &data, &len);
  1813. chemist_fame_list[i].fame = atoi(data);
  1814. // name
  1815. Sql_GetData(sql_handle, 2, &data, &len);
  1816. memcpy(chemist_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1817. }
  1818. // Build Taekwon ranking list
  1819. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", schema_config.char_db, JOB_TAEKWON, JOB_BABY_TAEKWON, fame_list_size_taekwon) )
  1820. Sql_ShowDebug(sql_handle);
  1821. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1822. {
  1823. // char_id
  1824. Sql_GetData(sql_handle, 0, &data, NULL);
  1825. taekwon_fame_list[i].id = atoi(data);
  1826. // fame
  1827. Sql_GetData(sql_handle, 1, &data, &len);
  1828. taekwon_fame_list[i].fame = atoi(data);
  1829. // name
  1830. Sql_GetData(sql_handle, 2, &data, &len);
  1831. memcpy(taekwon_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1832. }
  1833. Sql_FreeResult(sql_handle);
  1834. }
  1835. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  1836. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  1837. int char_loadName(uint32 char_id, char* name){
  1838. char* data;
  1839. size_t len;
  1840. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1841. Sql_ShowDebug(sql_handle);
  1842. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1843. {
  1844. Sql_GetData(sql_handle, 0, &data, &len);
  1845. safestrncpy(name, data, NAME_LENGTH);
  1846. return 1;
  1847. }
  1848. #if PACKETVER < 20180221
  1849. else
  1850. {
  1851. safestrncpy(name, charserv_config.char_config.unknown_char_name, NAME_LENGTH);
  1852. }
  1853. #else
  1854. name[0] = '\0';
  1855. #endif
  1856. return 0;
  1857. }
  1858. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  1859. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  1860. int char_search_mapserver(unsigned short map, uint32 ip, uint16 port){
  1861. for(int i = 0; i < ARRAYLENGTH(map_server); i++)
  1862. {
  1863. if (session_isValid(map_server[i].fd)
  1864. && (ip == (uint32)-1 || map_server[i].ip == ip)
  1865. && (port == (uint16)-1 || map_server[i].port == port))
  1866. {
  1867. for (int j = 0; map_server[i].map[j]; j++)
  1868. if (map_server[i].map[j] == map)
  1869. return i;
  1870. }
  1871. }
  1872. return -1;
  1873. }
  1874. /**
  1875. * Test to know if an IP come from LAN or WAN.
  1876. * @param ip: ip to check if in auth network
  1877. * @return 0 if from wan, or subnet_map_ip if lan
  1878. **/
  1879. int char_lan_subnetcheck(uint32 ip){
  1880. int i;
  1881. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  1882. if( i < subnet_count ) {
  1883. ShowInfo("Subnet check [%u.%u.%u.%u]: Matches " CL_CYAN "%u.%u.%u.%u/%u.%u.%u.%u" CL_RESET"\n", CONVIP(ip), CONVIP(subnet[i].char_ip & subnet[i].mask), CONVIP(subnet[i].mask));
  1884. return subnet[i].map_ip;
  1885. } else {
  1886. ShowInfo("Subnet check [%u.%u.%u.%u]: " CL_CYAN "WAN" CL_RESET "\n", CONVIP(ip));
  1887. return 0;
  1888. }
  1889. }
  1890. // Console Command Parser [Wizputer]
  1891. //FIXME to be remove (moved to cnslif / will be done once map/char/login, all have their cnslif interface ready)
  1892. int parse_console(const char* buf){
  1893. return cnslif_parse(buf);
  1894. }
  1895. #if PACKETVER_SUPPORTS_PINCODE
  1896. //------------------------------------------------
  1897. //Pincode system
  1898. //------------------------------------------------
  1899. int char_pincode_compare( int fd, struct char_session_data* sd, char* pin ){
  1900. if( strcmp( sd->pincode, pin ) == 0 ){
  1901. sd->pincode_try = 0;
  1902. return 1;
  1903. }else{
  1904. chclif_pincode_sendstate( fd, sd, PINCODE_WRONG );
  1905. if( charserv_config.pincode_config.pincode_maxtry && ++sd->pincode_try >= charserv_config.pincode_config.pincode_maxtry ){
  1906. chlogif_pincode_notifyLoginPinError( sd->account_id );
  1907. }
  1908. return 0;
  1909. }
  1910. }
  1911. void char_pincode_decrypt( uint32 userSeed, char* pin ){
  1912. int i;
  1913. char tab[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  1914. char *buf;
  1915. for( i = 1; i < 10; i++ ){
  1916. int pos;
  1917. uint32 multiplier = 0x3498, baseSeed = 0x881234;
  1918. userSeed = baseSeed + userSeed * multiplier;
  1919. pos = userSeed % ( i + 1 );
  1920. if( i != pos ){
  1921. tab[i] ^= tab[pos];
  1922. tab[pos] ^= tab[i];
  1923. tab[i] ^= tab[pos];
  1924. }
  1925. }
  1926. buf = (char *)aMalloc( sizeof(char) * ( PINCODE_LENGTH + 1 ) );
  1927. memset( buf, 0, PINCODE_LENGTH + 1 );
  1928. for( i = 0; i < PINCODE_LENGTH; i++ ){
  1929. sprintf( buf + i, "%d", tab[pin[i] - '0'] );
  1930. }
  1931. strcpy( pin, buf );
  1932. aFree( buf );
  1933. }
  1934. #endif
  1935. //------------------------------------------------
  1936. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  1937. //replies/disconnect the player we tried to kick. [Skotlex]
  1938. //------------------------------------------------
  1939. TIMER_FUNC(char_chardb_waiting_disconnect){
  1940. struct online_char_data* character;
  1941. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  1942. { //Mark it offline due to timeout.
  1943. character->waiting_disconnect = INVALID_TIMER;
  1944. char_set_char_offline(character->char_id, character->account_id);
  1945. }
  1946. return 0;
  1947. }
  1948. /**
  1949. * @see DBApply
  1950. */
  1951. int char_online_data_cleanup_sub(DBKey key, DBData *data, va_list ap)
  1952. {
  1953. struct online_char_data *character= (struct online_char_data *)db_data2ptr(data);
  1954. if (character->fd != -1)
  1955. return 0; //Character still connected
  1956. if (character->server == -2) //Unknown server.. set them offline
  1957. char_set_char_offline(character->char_id, character->account_id);
  1958. if (character->server < 0)
  1959. //Free data from players that have not been online for a while.
  1960. db_remove(online_char_db, key);
  1961. return 0;
  1962. }
  1963. TIMER_FUNC(char_online_data_cleanup){
  1964. online_char_db->foreach(online_char_db, char_online_data_cleanup_sub);
  1965. return 0;
  1966. }
  1967. TIMER_FUNC(char_clan_member_cleanup){
  1968. // Auto removal is disabled
  1969. if( charserv_config.clan_remove_inactive_days <= 0 ){
  1970. return 0;
  1971. }
  1972. if( SQL_ERROR == Sql_Query( sql_handle, "UPDATE `%s` SET `clan_id`='0' WHERE `online`='0' AND `clan_id`<>'0' AND `last_login` IS NOT NULL AND `last_login` <= NOW() - INTERVAL %d DAY", schema_config.char_db, charserv_config.clan_remove_inactive_days ) ){
  1973. Sql_ShowDebug(sql_handle);
  1974. }
  1975. return 0;
  1976. }
  1977. //----------------------------------
  1978. // Reading Lan Support configuration
  1979. // Rewrote: Anvanced subnet check [LuzZza]
  1980. //----------------------------------
  1981. int char_lan_config_read(const char *lancfgName) {
  1982. FILE *fp;
  1983. int line_num = 0, s_subnet=ARRAYLENGTH(subnet);
  1984. char line[1024], w1[64], w2[64], w3[64], w4[64];
  1985. if((fp = fopen(lancfgName, "r")) == NULL) {
  1986. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  1987. return 1;
  1988. }
  1989. while(fgets(line, sizeof(line), fp)) {
  1990. line_num++;
  1991. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  1992. continue;
  1993. if(sscanf(line,"%63[^:]: %63[^:]:%63[^:]:%63[^\r\n]", w1, w2, w3, w4) != 4) {
  1994. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  1995. continue;
  1996. }
  1997. remove_control_chars(w1);
  1998. remove_control_chars(w2);
  1999. remove_control_chars(w3);
  2000. remove_control_chars(w4);
  2001. if( strcmpi(w1, "subnet") == 0 ){
  2002. if(subnet_count>=s_subnet) { //We skip instead of break in case we want to add other conf in that file.
  2003. ShowError("%s: Too many subnets defined, skipping line %d...\n", lancfgName, line_num);
  2004. continue;
  2005. }
  2006. subnet[subnet_count].mask = str2ip(w2);
  2007. subnet[subnet_count].char_ip = str2ip(w3);
  2008. subnet[subnet_count].map_ip = str2ip(w4);
  2009. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  2010. {
  2011. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  2012. continue;
  2013. }
  2014. subnet_count++;
  2015. }
  2016. }
  2017. if( subnet_count > 1 ) /* only useful if there is more than 1 */
  2018. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  2019. fclose(fp);
  2020. return 0;
  2021. }
  2022. /**
  2023. * Check if our table are all ok in sqlserver
  2024. * Char tables to check
  2025. * @return 0:fail, 1:success
  2026. */
  2027. bool char_checkdb(void){
  2028. int i;
  2029. const char* sqltable[] = {
  2030. schema_config.char_db, schema_config.hotkey_db, schema_config.scdata_db, schema_config.cart_db,
  2031. schema_config.inventory_db, schema_config.charlog_db,
  2032. schema_config.char_reg_str_table, schema_config.char_reg_num_table, schema_config.acc_reg_str_table,
  2033. schema_config.acc_reg_num_table, schema_config.skill_db, schema_config.interlog_db, schema_config.memo_db,
  2034. schema_config.guild_db, schema_config.guild_alliance_db, schema_config.guild_castle_db,
  2035. schema_config.guild_expulsion_db, schema_config.guild_member_db,
  2036. schema_config.guild_skill_db, schema_config.guild_position_db, schema_config.guild_storage_db,
  2037. schema_config.party_db, schema_config.pet_db, schema_config.friend_db, schema_config.mail_db,
  2038. schema_config.auction_db, schema_config.quest_db, schema_config.homunculus_db, schema_config.skill_homunculus_db,
  2039. schema_config.mercenary_db, schema_config.mercenary_owner_db,
  2040. schema_config.elemental_db, schema_config.skillcooldown_db, schema_config.bonus_script_db,
  2041. schema_config.clan_table, schema_config.clan_alliance_table, schema_config.mail_attachment_db, schema_config.achievement_table
  2042. };
  2043. ShowInfo("Start checking DB integrity\n");
  2044. for (i=0; i<ARRAYLENGTH(sqltable); i++){ //check if they all exist and we can acces them in sql-server
  2045. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` LIMIT 1;", sqltable[i]) ){
  2046. Sql_ShowDebug(sql_handle);
  2047. return false;
  2048. }
  2049. }
  2050. //checking char_db
  2051. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,"
  2052. "`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,"
  2053. "`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,"
  2054. "`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,`hair_color`,`clothes_color`,`weapon`,"
  2055. "`shield`,`head_top`,`head_mid`,`head_bottom`,`robe`,`last_map`,`last_x`,`last_y`,`save_map`,"
  2056. "`save_x`,`save_y`,`partner_id`,`online`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,"
  2057. "`moves`,`unban_time`,`font`,`sex`,`hotkey_rowshift`,`clan_id`,`last_login`,`title_id`,`show_equip`,"
  2058. "`hotkey_rowshift2`,"
  2059. "`max_ap`,`ap`,`trait_point`,`pow`,`sta`,`wis`,`spl`,`con`,`crt`,"
  2060. "`inventory_slots`"
  2061. " FROM `%s` LIMIT 1;", schema_config.char_db) ){
  2062. Sql_ShowDebug(sql_handle);
  2063. return false;
  2064. }
  2065. //checking charlog_db
  2066. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`char_msg`,`account_id`,`char_num`,`name`,"
  2067. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`"
  2068. " FROM `%s` LIMIT 1;", schema_config.charlog_db) ){
  2069. Sql_ShowDebug(sql_handle);
  2070. return false;
  2071. }
  2072. //checking char_reg_str_table
  2073. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_str_table) ) {
  2074. Sql_ShowDebug(sql_handle);
  2075. return false;
  2076. }
  2077. //checking char_reg_num_table
  2078. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_num_table) ) {
  2079. Sql_ShowDebug(sql_handle);
  2080. return false;
  2081. }
  2082. //checking acc_reg_str_table
  2083. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_str_table) ) {
  2084. Sql_ShowDebug(sql_handle);
  2085. return false;
  2086. }
  2087. //checking acc_reg_num_table
  2088. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_num_table) ) {
  2089. Sql_ShowDebug(sql_handle);
  2090. return false;
  2091. }
  2092. //checking hotkey_db
  2093. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`hotkey`,`type`,`itemskill_id`,`skill_lvl`"
  2094. " FROM `%s` LIMIT 1;", schema_config.hotkey_db) ){
  2095. Sql_ShowDebug(sql_handle);
  2096. return false;
  2097. }
  2098. //checking scdata_db
  2099. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`type`,`tick`,`val1`,`val2`,`val3`,`val4`"
  2100. " FROM `%s` LIMIT 1;", schema_config.scdata_db) ){
  2101. Sql_ShowDebug(sql_handle);
  2102. return false;
  2103. }
  2104. //checking skill_db
  2105. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`id`,`lv`,`flag` FROM `%s` LIMIT 1;", schema_config.skill_db) ){
  2106. Sql_ShowDebug(sql_handle);
  2107. return false;
  2108. }
  2109. //checking interlog_db
  2110. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`log` FROM `%s` LIMIT 1;", schema_config.interlog_db) ){
  2111. Sql_ShowDebug(sql_handle);
  2112. return false;
  2113. }
  2114. //checking memo_db
  2115. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `memo_id`,`char_id`,`map`,`x`,`y` FROM `%s` LIMIT 1;", schema_config.memo_db) ){
  2116. Sql_ShowDebug(sql_handle);
  2117. return false;
  2118. }
  2119. //checking guild_db
  2120. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`name`,`char_id`,`master`,`guild_lv`,"
  2121. "`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`mes1`,`mes2`,"
  2122. "`emblem_len`,`emblem_id`,`emblem_data`,`last_master_change`"
  2123. " FROM `%s` LIMIT 1;", schema_config.guild_db) ){
  2124. Sql_ShowDebug(sql_handle);
  2125. return false;
  2126. }
  2127. //checking guild_alliance_db
  2128. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`opposition`,`alliance_id`,`name` FROM `%s` LIMIT 1;", schema_config.guild_alliance_db) ){
  2129. Sql_ShowDebug(sql_handle);
  2130. return false;
  2131. }
  2132. //checking guild_castle_db
  2133. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `castle_id`,`guild_id`,`economy`,`defense`,`triggerE`,"
  2134. "`triggerD`,`nextTime`,`payTime`,`createTime`,`visibleC`,`visibleG0`,`visibleG1`,`visibleG2`,"
  2135. "`visibleG3`,`visibleG4`,`visibleG5`,`visibleG6`,`visibleG7` "
  2136. " FROM `%s` LIMIT 1;", schema_config.guild_castle_db) ){
  2137. Sql_ShowDebug(sql_handle);
  2138. return false;
  2139. }
  2140. //checking guild_expulsion_db
  2141. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`account_id`,`name`,`mes` FROM `%s` LIMIT 1;", schema_config.guild_expulsion_db) ){
  2142. Sql_ShowDebug(sql_handle);
  2143. return false;
  2144. }
  2145. //checking guild_member_db
  2146. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`char_id`,`exp`,`position` FROM `%s` LIMIT 1;", schema_config.guild_member_db) ){
  2147. Sql_ShowDebug(sql_handle);
  2148. return false;
  2149. }
  2150. //checking guild_skill_db
  2151. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.guild_skill_db) ){
  2152. Sql_ShowDebug(sql_handle);
  2153. return false;
  2154. }
  2155. //checking guild_position_db
  2156. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`position`,`name`,`mode`,`exp_mode` FROM `%s` LIMIT 1;", schema_config.guild_position_db) ){
  2157. Sql_ShowDebug(sql_handle);
  2158. return false;
  2159. }
  2160. //checking party_db
  2161. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `party_id`,`name`,`exp`,`item`,`leader_id`,`leader_char` FROM `%s` LIMIT 1;", schema_config.party_db) ){
  2162. Sql_ShowDebug(sql_handle);
  2163. return false;
  2164. }
  2165. //checking pet_db
  2166. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `pet_id`,`class`,`name`,`account_id`,`char_id`,`level`,"
  2167. "`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`"
  2168. " FROM `%s` LIMIT 1;", schema_config.pet_db) ){
  2169. Sql_ShowDebug(sql_handle);
  2170. return false;
  2171. }
  2172. //checking friend_db
  2173. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`friend_id` FROM `%s` LIMIT 1;", schema_config.friend_db) ){
  2174. Sql_ShowDebug(sql_handle);
  2175. return false;
  2176. }
  2177. //checking mail_db
  2178. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,"
  2179. "`title`,`message`,`time`,`status`,`zeny`,`type`"
  2180. " FROM `%s` LIMIT 1;", schema_config.mail_db) ){
  2181. Sql_ShowDebug(sql_handle);
  2182. return false;
  2183. }
  2184. //checking mail_attachment_db
  2185. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`index`,`nameid`,`amount`,`refine`,`attribute`,`identify`,"
  2186. "`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`unique_id`, `bound`, `enchantgrade`"
  2187. " FROM `%s` LIMIT 1;", schema_config.mail_attachment_db) ){
  2188. Sql_ShowDebug(sql_handle);
  2189. return false;
  2190. }
  2191. //checking auction_db
  2192. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `auction_id`,`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,"
  2193. "`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`card0`,`card1`,"
  2194. "`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`unique_id`, `enchantgrade` "
  2195. "FROM `%s` LIMIT 1;", schema_config.auction_db) ){
  2196. Sql_ShowDebug(sql_handle);
  2197. return false;
  2198. }
  2199. //checking quest_db
  2200. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`quest_id`,`state`,`time`,`count1`,`count2`,`count3` from `%s`;", schema_config.quest_db) ){
  2201. Sql_ShowDebug(sql_handle);
  2202. return false;
  2203. }
  2204. //checking homunculus_db
  2205. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`char_id`,`class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`,"
  2206. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`alive`,`rename_flag`,`vaporize`,`autofeed` "
  2207. " FROM `%s` LIMIT 1;", schema_config.homunculus_db) ){
  2208. Sql_ShowDebug(sql_handle);
  2209. return false;
  2210. }
  2211. //checking skill_homunculus_db
  2212. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.skill_homunculus_db) ){
  2213. Sql_ShowDebug(sql_handle);
  2214. return false;
  2215. }
  2216. //checking mercenary_db
  2217. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `mer_id`,`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time` FROM `%s` LIMIT 1;", schema_config.mercenary_db) ){
  2218. Sql_ShowDebug(sql_handle);
  2219. return false;
  2220. }
  2221. //checking mercenary_owner_db
  2222. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`merc_id`,`arch_calls`,`arch_faith`,"
  2223. "`spear_calls`,`spear_faith`,`sword_calls`,`sword_faith`"
  2224. " FROM `%s` LIMIT 1;", schema_config.mercenary_owner_db) ){
  2225. Sql_ShowDebug(sql_handle);
  2226. return false;
  2227. }
  2228. //checking elemental_db
  2229. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `ele_id`,`char_id`,`class`,`mode`,`hp`,`sp`,`max_hp`,`max_sp`,"
  2230. "`atk1`,`atk2`,`matk`,`aspd`,`def`,`mdef`,`flee`,`hit`,`life_time` "
  2231. " FROM `%s` LIMIT 1;", schema_config.elemental_db) ){
  2232. Sql_ShowDebug(sql_handle);
  2233. return false;
  2234. }
  2235. //checking skillcooldown_db
  2236. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`skill`,`tick` FROM `%s` LIMIT 1;", schema_config.skillcooldown_db) ){
  2237. Sql_ShowDebug(sql_handle);
  2238. return false;
  2239. }
  2240. //checking bonus_script_db
  2241. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`script`,`tick`,`flag`,`type`,`icon` FROM `%s` LIMIT 1;", schema_config.bonus_script_db) ){
  2242. Sql_ShowDebug(sql_handle);
  2243. return false;
  2244. }
  2245. //checking cart_db
  2246. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2247. "`attribute`,`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`expire_time`,`bound`,`unique_id`,`enchantgrade`"
  2248. " FROM `%s` LIMIT 1;", schema_config.cart_db) ){
  2249. Sql_ShowDebug(sql_handle);
  2250. return false;
  2251. }
  2252. //checking inventory_db
  2253. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2254. "`attribute`,`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`expire_time`,`bound`,`unique_id`,`enchantgrade`"
  2255. ",`favorite`,`equip_switch`"
  2256. " FROM `%s` LIMIT 1;", schema_config.inventory_db) ){
  2257. Sql_ShowDebug(sql_handle);
  2258. return false;
  2259. }
  2260. //checking guild_storage_db
  2261. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`guild_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2262. "`attribute`,`card0`,`card1`,`card2`,`card3`,`option_id0`,`option_val0`,`option_parm0`,`option_id1`,`option_val1`,`option_parm1`,`option_id2`,`option_val2`,`option_parm2`,`option_id3`,`option_val3`,`option_parm3`,`option_id4`,`option_val4`,`option_parm4`,`expire_time`,`bound`,`unique_id`,`enchantgrade`"
  2263. " FROM `%s` LIMIT 1;", schema_config.guild_storage_db) ){
  2264. Sql_ShowDebug(sql_handle);
  2265. return false;
  2266. }
  2267. //checking clan table
  2268. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `clan_id`,`name`,`master`,`mapname`,`max_member`"
  2269. " FROM `%s` LIMIT 1;", schema_config.clan_table) ){
  2270. Sql_ShowDebug(sql_handle);
  2271. return false;
  2272. }
  2273. //checking clan alliance table
  2274. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `clan_id`,`opposition`,`alliance_id`,`name`"
  2275. " FROM `%s` LIMIT 1;", schema_config.clan_alliance_table) ){
  2276. Sql_ShowDebug(sql_handle);
  2277. return false;
  2278. }
  2279. //checking achievement_table
  2280. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`id`,`count1`,`count2`,`count3`,`count4`,`count5`,`count6`,`count7`,`count8`,`count9`,`count10`,`completed`,`rewarded`"
  2281. " FROM `%s` LIMIT 1;", schema_config.achievement_table)) {
  2282. Sql_ShowDebug(sql_handle);
  2283. return false;
  2284. }
  2285. Sql_FreeResult(sql_handle);
  2286. ShowInfo("DB integrity check finished with success\n");
  2287. return true;
  2288. }
  2289. void char_sql_config_read(const char* cfgName) {
  2290. char line[1024], w1[1024], w2[1024];
  2291. FILE* fp;
  2292. if ((fp = fopen(cfgName, "r")) == NULL) {
  2293. ShowError("File not found: %s\n", cfgName);
  2294. return;
  2295. }
  2296. while(fgets(line, sizeof(line), fp)) {
  2297. if(line[0] == '/' && line[1] == '/')
  2298. continue;
  2299. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2300. continue;
  2301. if(!strcmpi(w1,"char_db"))
  2302. safestrncpy(schema_config.char_db, w2, sizeof(schema_config.char_db));
  2303. else if(!strcmpi(w1,"scdata_db"))
  2304. safestrncpy(schema_config.scdata_db, w2, sizeof(schema_config.scdata_db));
  2305. else if(!strcmpi(w1,"cart_db"))
  2306. safestrncpy(schema_config.cart_db, w2, sizeof(schema_config.cart_db));
  2307. else if(!strcmpi(w1,"inventory_db"))
  2308. safestrncpy(schema_config.inventory_db, w2, sizeof(schema_config.inventory_db));
  2309. else if(!strcmpi(w1,"charlog_db"))
  2310. safestrncpy(schema_config.charlog_db, w2, sizeof(schema_config.charlog_db));
  2311. else if(!strcmpi(w1,"skill_db"))
  2312. safestrncpy(schema_config.skill_db, w2, sizeof(schema_config.skill_db));
  2313. else if(!strcmpi(w1,"interlog_db"))
  2314. safestrncpy(schema_config.interlog_db, w2, sizeof(schema_config.interlog_db));
  2315. else if(!strcmpi(w1,"memo_db"))
  2316. safestrncpy(schema_config.memo_db, w2, sizeof(schema_config.memo_db));
  2317. else if(!strcmpi(w1,"guild_db"))
  2318. safestrncpy(schema_config.guild_db, w2, sizeof(schema_config.guild_db));
  2319. else if(!strcmpi(w1,"guild_alliance_db"))
  2320. safestrncpy(schema_config.guild_alliance_db, w2, sizeof(schema_config.guild_alliance_db));
  2321. else if(!strcmpi(w1,"guild_castle_db"))
  2322. safestrncpy(schema_config.guild_castle_db, w2, sizeof(schema_config.guild_castle_db));
  2323. else if(!strcmpi(w1,"guild_expulsion_db"))
  2324. safestrncpy(schema_config.guild_expulsion_db, w2, sizeof(schema_config.guild_expulsion_db));
  2325. else if(!strcmpi(w1,"guild_member_db"))
  2326. safestrncpy(schema_config.guild_member_db, w2, sizeof(schema_config.guild_member_db));
  2327. else if(!strcmpi(w1,"guild_skill_db"))
  2328. safestrncpy(schema_config.guild_skill_db, w2, sizeof(schema_config.guild_skill_db));
  2329. else if(!strcmpi(w1,"guild_position_db"))
  2330. safestrncpy(schema_config.guild_position_db, w2, sizeof(schema_config.guild_position_db));
  2331. else if(!strcmpi(w1,"guild_storage_db"))
  2332. safestrncpy(schema_config.guild_storage_db, w2, sizeof(schema_config.guild_storage_db));
  2333. else if(!strcmpi(w1,"party_db"))
  2334. safestrncpy(schema_config.party_db, w2, sizeof(schema_config.party_db));
  2335. else if(!strcmpi(w1,"pet_db"))
  2336. safestrncpy(schema_config.pet_db, w2, sizeof(schema_config.pet_db));
  2337. else if(!strcmpi(w1,"mail_db"))
  2338. safestrncpy(schema_config.mail_db, w2, sizeof(schema_config.mail_db));
  2339. else if (!strcmpi(w1, "mail_attachment_db"))
  2340. safestrncpy(schema_config.mail_attachment_db, w2, sizeof(schema_config.mail_attachment_db));
  2341. else if(!strcmpi(w1,"auction_db"))
  2342. safestrncpy(schema_config.auction_db, w2, sizeof(schema_config.auction_db));
  2343. else if(!strcmpi(w1,"friend_db"))
  2344. safestrncpy(schema_config.friend_db, w2, sizeof(schema_config.friend_db));
  2345. else if(!strcmpi(w1,"hotkey_db"))
  2346. safestrncpy(schema_config.hotkey_db, w2, sizeof(schema_config.hotkey_db));
  2347. else if(!strcmpi(w1,"quest_db"))
  2348. safestrncpy(schema_config.quest_db,w2,sizeof(schema_config.quest_db));
  2349. else if(!strcmpi(w1,"homunculus_db"))
  2350. safestrncpy(schema_config.homunculus_db,w2,sizeof(schema_config.homunculus_db));
  2351. else if(!strcmpi(w1,"skill_homunculus_db"))
  2352. safestrncpy(schema_config.skill_homunculus_db,w2,sizeof(schema_config.skill_homunculus_db));
  2353. else if(!strcmpi(w1,"mercenary_db"))
  2354. safestrncpy(schema_config.mercenary_db,w2,sizeof(schema_config.mercenary_db));
  2355. else if(!strcmpi(w1,"mercenary_owner_db"))
  2356. safestrncpy(schema_config.mercenary_owner_db,w2,sizeof(schema_config.mercenary_owner_db));
  2357. else if(!strcmpi(w1,"elemental_db"))
  2358. safestrncpy(schema_config.elemental_db,w2,sizeof(schema_config.elemental_db));
  2359. else if(!strcmpi(w1,"skillcooldown_db"))
  2360. safestrncpy(schema_config.skillcooldown_db, w2, sizeof(schema_config.skillcooldown_db));
  2361. else if(!strcmpi(w1,"bonus_script_db"))
  2362. safestrncpy(schema_config.bonus_script_db, w2, sizeof(schema_config.bonus_script_db));
  2363. else if(!strcmpi(w1,"char_reg_num_table"))
  2364. safestrncpy(schema_config.char_reg_num_table, w2, sizeof(schema_config.char_reg_num_table));
  2365. else if(!strcmpi(w1,"char_reg_str_table"))
  2366. safestrncpy(schema_config.char_reg_str_table, w2, sizeof(schema_config.char_reg_str_table));
  2367. else if(!strcmpi(w1,"acc_reg_str_table"))
  2368. safestrncpy(schema_config.acc_reg_str_table, w2, sizeof(schema_config.acc_reg_str_table));
  2369. else if(!strcmpi(w1,"acc_reg_num_table"))
  2370. safestrncpy(schema_config.acc_reg_num_table, w2, sizeof(schema_config.acc_reg_num_table));
  2371. else if(!strcmpi(w1,"clan_table"))
  2372. safestrncpy(schema_config.clan_table, w2, sizeof(schema_config.clan_table));
  2373. else if(!strcmpi(w1,"clan_alliance_table"))
  2374. safestrncpy(schema_config.clan_alliance_table, w2, sizeof(schema_config.clan_alliance_table));
  2375. else if(!strcmpi(w1,"achievement_table"))
  2376. safestrncpy(schema_config.achievement_table, w2, sizeof(schema_config.achievement_table));
  2377. else if(!strcmpi(w1, "start_status_points"))
  2378. charserv_config.start_status_points = atoi(w2);
  2379. //support the import command, just like any other config
  2380. else if(!strcmpi(w1,"import"))
  2381. char_sql_config_read(w2);
  2382. }
  2383. fclose(fp);
  2384. ShowInfo("Done reading %s.\n", cfgName);
  2385. }
  2386. void char_set_default_sql(){
  2387. // schema_config.db_use_sqldbs;
  2388. safestrncpy(schema_config.db_path,"db",sizeof(schema_config.db_path));
  2389. safestrncpy(schema_config.char_db,"char",sizeof(schema_config.char_db));
  2390. safestrncpy(schema_config.scdata_db,"sc_data",sizeof(schema_config.scdata_db));
  2391. safestrncpy(schema_config.cart_db,"cart_inventory",sizeof(schema_config.cart_db));
  2392. safestrncpy(schema_config.inventory_db,"inventory",sizeof(schema_config.inventory_db));
  2393. safestrncpy(schema_config.charlog_db,"charlog",sizeof(schema_config.charlog_db));
  2394. safestrncpy(schema_config.storage_db,"storage",sizeof(schema_config.storage_db));
  2395. safestrncpy(schema_config.interlog_db,"interlog",sizeof(schema_config.interlog_db));
  2396. safestrncpy(schema_config.skill_db,"skill",sizeof(schema_config.skill_db));
  2397. safestrncpy(schema_config.memo_db,"memo",sizeof(schema_config.memo_db));
  2398. safestrncpy(schema_config.guild_db,"guild",sizeof(schema_config.guild_db));
  2399. safestrncpy(schema_config.guild_alliance_db,"guild_alliance",sizeof(schema_config.guild_alliance_db));
  2400. safestrncpy(schema_config.guild_castle_db,"guild_castle",sizeof(schema_config.guild_castle_db));
  2401. safestrncpy(schema_config.guild_expulsion_db,"guild_expulsion",sizeof(schema_config.guild_expulsion_db));
  2402. safestrncpy(schema_config.guild_member_db,"guild_member",sizeof(schema_config.guild_member_db));
  2403. safestrncpy(schema_config.guild_position_db,"guild_position",sizeof(schema_config.guild_position_db));
  2404. safestrncpy(schema_config.guild_skill_db,"guild_skill",sizeof(schema_config.guild_skill_db));
  2405. safestrncpy(schema_config.guild_storage_db,"guild_storage",sizeof(schema_config.guild_storage_db));
  2406. safestrncpy(schema_config.party_db,"party",sizeof(schema_config.party_db));
  2407. safestrncpy(schema_config.pet_db,"pet",sizeof(schema_config.pet_db));
  2408. safestrncpy(schema_config.mail_db,"mail",sizeof(schema_config.mail_db)); // MAIL SYSTEM
  2409. safestrncpy(schema_config.auction_db,"auction",sizeof(schema_config.auction_db)); // Auctions System
  2410. safestrncpy(schema_config.friend_db,"friends",sizeof(schema_config.friend_db));
  2411. safestrncpy(schema_config.hotkey_db,"hotkey",sizeof(schema_config.hotkey_db));
  2412. safestrncpy(schema_config.quest_db,"quest",sizeof(schema_config.quest_db));
  2413. safestrncpy(schema_config.homunculus_db,"homunculus",sizeof(schema_config.homunculus_db));
  2414. safestrncpy(schema_config.skill_homunculus_db,"skill_homunculus",sizeof(schema_config.skill_homunculus_db));
  2415. safestrncpy(schema_config.mercenary_db,"mercenary",sizeof(schema_config.mercenary_db));
  2416. safestrncpy(schema_config.mercenary_owner_db,"mercenary_owner",sizeof(schema_config.mercenary_owner_db));
  2417. safestrncpy(schema_config.skillcooldown_db,"skillcooldown",sizeof(schema_config.skillcooldown_db));
  2418. safestrncpy(schema_config.bonus_script_db,"bonus_script",sizeof(schema_config.bonus_script_db));
  2419. safestrncpy(schema_config.char_reg_num_table,"char_reg_num",sizeof(schema_config.char_reg_num_table));
  2420. safestrncpy(schema_config.char_reg_str_table,"char_reg_str",sizeof(schema_config.char_reg_str_table));
  2421. safestrncpy(schema_config.acc_reg_str_table,"acc_reg_str",sizeof(schema_config.acc_reg_str_table));
  2422. safestrncpy(schema_config.acc_reg_num_table,"acc_reg_num",sizeof(schema_config.acc_reg_num_table));
  2423. safestrncpy(schema_config.clan_table,"clan",sizeof(schema_config.clan_table));
  2424. safestrncpy(schema_config.clan_table,"clan_alliance",sizeof(schema_config.clan_alliance_table));
  2425. safestrncpy(schema_config.achievement_table,"achievement",sizeof(schema_config.achievement_table));
  2426. }
  2427. //set default config
  2428. void char_set_defaults(){
  2429. #if PACKETVER_SUPPORTS_PINCODE
  2430. charserv_config.pincode_config.pincode_enabled = true;
  2431. charserv_config.pincode_config.pincode_changetime = 0;
  2432. charserv_config.pincode_config.pincode_maxtry = 3;
  2433. charserv_config.pincode_config.pincode_force = true;
  2434. charserv_config.pincode_config.pincode_allow_repeated = false;
  2435. charserv_config.pincode_config.pincode_allow_sequential = false;
  2436. #endif
  2437. charserv_config.charmove_config.char_move_enabled = true;
  2438. charserv_config.charmove_config.char_movetoused = true;
  2439. charserv_config.charmove_config.char_moves_unlimited = false;
  2440. charserv_config.char_config.char_per_account = 0; //Maximum chars per account (default unlimited) [Sirius]
  2441. charserv_config.char_config.char_del_level = 0; //From which level u can delete character [Lupus]
  2442. charserv_config.char_config.char_del_delay = 86400;
  2443. #if PACKETVER >= 20100803
  2444. charserv_config.char_config.char_del_option = CHAR_DEL_BIRTHDATE;
  2445. #else
  2446. charserv_config.char_config.char_del_option = CHAR_DEL_EMAIL;
  2447. #endif
  2448. charserv_config.char_config.char_del_restriction = CHAR_DEL_RESTRICT_ALL;
  2449. // charserv_config.userid[24];
  2450. // charserv_config.passwd[24];
  2451. // charserv_config.server_name[20];
  2452. safestrncpy(charserv_config.wisp_server_name,"Server",sizeof(charserv_config.wisp_server_name));
  2453. // charserv_config.login_ip_str[128];
  2454. charserv_config.login_ip = 0;
  2455. charserv_config.login_port = 6900;
  2456. // charserv_config.char_ip_str[128];
  2457. charserv_config.char_ip = 0;
  2458. // charserv_config.bind_ip_str[128];
  2459. charserv_config.bind_ip = INADDR_ANY;
  2460. charserv_config.char_port = 6121;
  2461. charserv_config.char_maintenance = 0;
  2462. charserv_config.char_new = true;
  2463. charserv_config.char_new_display = 0;
  2464. charserv_config.char_config.name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  2465. charserv_config.char_config.char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
  2466. safestrncpy(charserv_config.char_config.unknown_char_name,"Unknown",sizeof(charserv_config.char_config.unknown_char_name)); // Name to use when the requested name cannot be determined
  2467. safestrncpy(charserv_config.char_config.char_name_letters,"",sizeof(charserv_config.char_config.char_name_letters)); // list of letters/symbols allowed (or not) in a character name. by [Yor]
  2468. charserv_config.save_log = 1; // show loading/saving messages
  2469. charserv_config.log_char = 1; // loggin char or not [devil]
  2470. charserv_config.log_inter = 1; // loggin inter or not [devil]
  2471. charserv_config.char_check_db =1;
  2472. // See const.hpp to change the default values
  2473. charserv_config.start_point[0].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2474. charserv_config.start_point[0].x = MAP_DEFAULT_X;
  2475. charserv_config.start_point[0].y = MAP_DEFAULT_Y;
  2476. charserv_config.start_point_count = 1;
  2477. #if PACKETVER >= 20151001
  2478. charserv_config.start_point_doram[0].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2479. charserv_config.start_point_doram[0].x = MAP_DEFAULT_X;
  2480. charserv_config.start_point_doram[0].y = MAP_DEFAULT_Y;
  2481. charserv_config.start_point_count_doram = 1;
  2482. #endif
  2483. charserv_config.start_items[0].nameid = 1201;
  2484. charserv_config.start_items[0].amount = 1;
  2485. charserv_config.start_items[0].pos = EQP_HAND_R;
  2486. charserv_config.start_items[1].nameid = 2301;
  2487. charserv_config.start_items[1].amount = 1;
  2488. charserv_config.start_items[1].pos = EQP_ARMOR;
  2489. #if PACKETVER >= 20150101
  2490. charserv_config.start_items_doram[0].nameid = 1681;
  2491. charserv_config.start_items_doram[0].amount = 1;
  2492. charserv_config.start_items_doram[0].pos = EQP_HAND_R;
  2493. charserv_config.start_items_doram[1].nameid = 2301;
  2494. charserv_config.start_items_doram[1].amount = 1;
  2495. charserv_config.start_items_doram[1].pos = EQP_ARMOR;
  2496. #endif
  2497. charserv_config.start_status_points = 48;
  2498. charserv_config.console = 0;
  2499. charserv_config.max_connect_user = -1;
  2500. charserv_config.gm_allow_group = -1;
  2501. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2502. charserv_config.start_zeny = 0;
  2503. charserv_config.guild_exp_rate = 100;
  2504. safestrncpy(charserv_config.default_map, "prontera", MAP_NAME_LENGTH);
  2505. charserv_config.default_map_x = 156;
  2506. charserv_config.default_map_y = 191;
  2507. charserv_config.clan_remove_inactive_days = 14;
  2508. charserv_config.mail_return_days = 14;
  2509. charserv_config.mail_delete_days = 14;
  2510. charserv_config.mail_retrieve = 1;
  2511. #if defined(RENEWAL) && PACKETVER >= 20151001
  2512. charserv_config.allowed_job_flag = 3;
  2513. #else
  2514. charserv_config.allowed_job_flag = 1;
  2515. #endif
  2516. }
  2517. /**
  2518. * Split start_point configuration values.
  2519. * @param w1_value: Value from w1
  2520. * @param w2_value: Value from w2
  2521. * @param start: Start point reference
  2522. * @param count: Start point count reference
  2523. */
  2524. void char_config_split_startpoint(char *w1_value, char *w2_value, struct point start_point[MAX_STARTPOINT], short *count)
  2525. {
  2526. char *lineitem, **fields;
  2527. int i = 0, fields_length = 3 + 1;
  2528. (*count) = 0; // Reset to begin reading
  2529. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2530. if (fields == NULL)
  2531. return; // Failed to allocate memory.
  2532. lineitem = strtok(w2_value, ":");
  2533. while (lineitem != NULL && (*count) < MAX_STARTPOINT) {
  2534. int n = sv_split(lineitem, strlen(lineitem), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE);
  2535. if (n + 1 < fields_length) {
  2536. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2537. lineitem = strtok(NULL, ":"); //next lineitem
  2538. continue;
  2539. }
  2540. start_point[i].map = mapindex_name2id(fields[1]);
  2541. if (!start_point[i].map) {
  2542. ShowError("Start point %s not found in map-index cache. Setting to default location.\n", fields[1]);
  2543. start_point[i].map = mapindex_name2id(MAP_DEFAULT_NAME);
  2544. start_point[i].x = MAP_DEFAULT_X;
  2545. start_point[i].y = MAP_DEFAULT_Y;
  2546. } else {
  2547. start_point[i].x = max(0, atoi(fields[2]));
  2548. start_point[i].y = max(0, atoi(fields[3]));
  2549. }
  2550. (*count)++;
  2551. lineitem = strtok(NULL, ":"); //next lineitem
  2552. i++;
  2553. }
  2554. aFree(fields);
  2555. }
  2556. /**
  2557. * Split start_items configuration values.
  2558. * @param w1_value: Value from w1
  2559. * @param w2_value: Value from w2
  2560. * @param start: Start item reference
  2561. */
  2562. void char_config_split_startitem(char *w1_value, char *w2_value, struct startitem start_items[MAX_STARTITEM])
  2563. {
  2564. char *lineitem, **fields;
  2565. int i = 0, fields_length = 3 + 1;
  2566. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2567. if (fields == NULL)
  2568. return; // Failed to allocate memory.
  2569. lineitem = strtok(w2_value, ":");
  2570. while (lineitem != NULL && i < MAX_STARTITEM) {
  2571. int n = sv_split(lineitem, strlen(lineitem), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE);
  2572. if (n + 1 < fields_length) {
  2573. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2574. lineitem = strtok(NULL, ":"); //next lineitem
  2575. continue;
  2576. }
  2577. // TODO: Item ID verification
  2578. start_items[i].nameid = strtoul( fields[1], nullptr, 10 );
  2579. // TODO: Stack verification
  2580. start_items[i].amount = min( (uint16)strtoul( fields[2], nullptr, 10 ), MAX_AMOUNT );
  2581. start_items[i].pos = strtoul( fields[3], nullptr, 10 );
  2582. lineitem = strtok(NULL, ":"); //next lineitem
  2583. i++;
  2584. }
  2585. aFree(fields);
  2586. }
  2587. bool char_config_read(const char* cfgName, bool normal){
  2588. char line[1024], w1[1024], w2[1024];
  2589. FILE* fp = fopen(cfgName, "r");
  2590. if (fp == NULL) {
  2591. ShowError("Configuration file not found: %s.\n", cfgName);
  2592. return false;
  2593. }
  2594. while(fgets(line, sizeof(line), fp)) {
  2595. if (line[0] == '/' && line[1] == '/')
  2596. continue;
  2597. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2598. continue;
  2599. remove_control_chars(w1);
  2600. remove_control_chars(w2);
  2601. // Config that loaded only when server started, not by reloading config file
  2602. if (normal) {
  2603. if (strcmpi(w1, "userid") == 0) {
  2604. safestrncpy(charserv_config.userid, w2, sizeof(charserv_config.userid));
  2605. } else if (strcmpi(w1, "passwd") == 0) {
  2606. safestrncpy(charserv_config.passwd, w2, sizeof(charserv_config.passwd));
  2607. } else if (strcmpi(w1, "server_name") == 0) {
  2608. safestrncpy(charserv_config.server_name, w2, sizeof(charserv_config.server_name));
  2609. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  2610. if (strlen(w2) >= 4) {
  2611. safestrncpy(charserv_config.wisp_server_name, w2, sizeof(charserv_config.wisp_server_name));
  2612. }
  2613. } else if (strcmpi(w1, "login_ip") == 0) {
  2614. charserv_config.login_ip = host2ip(w2);
  2615. if (charserv_config.login_ip) {
  2616. char ip_str[16];
  2617. safestrncpy(charserv_config.login_ip_str, w2, sizeof(charserv_config.login_ip_str));
  2618. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(charserv_config.login_ip, ip_str));
  2619. }
  2620. } else if (strcmpi(w1, "login_port") == 0) {
  2621. charserv_config.login_port = atoi(w2);
  2622. } else if (strcmpi(w1, "char_ip") == 0) {
  2623. charserv_config.char_ip = host2ip(w2);
  2624. if (charserv_config.char_ip) {
  2625. char ip_str[16];
  2626. safestrncpy(charserv_config.char_ip_str, w2, sizeof(charserv_config.char_ip_str));
  2627. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(charserv_config.char_ip, ip_str));
  2628. }
  2629. } else if (strcmpi(w1, "bind_ip") == 0) {
  2630. charserv_config.bind_ip = host2ip(w2);
  2631. if (charserv_config.bind_ip) {
  2632. char ip_str[16];
  2633. safestrncpy(charserv_config.bind_ip_str, w2, sizeof(charserv_config.bind_ip_str));
  2634. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(charserv_config.bind_ip, ip_str));
  2635. }
  2636. } else if (strcmpi(w1, "char_port") == 0) {
  2637. charserv_config.char_port = atoi(w2);
  2638. } else if (strcmpi(w1, "console") == 0) {
  2639. charserv_config.console = config_switch(w2);
  2640. }
  2641. }
  2642. if(strcmpi(w1,"timestamp_format") == 0) {
  2643. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  2644. } else if(strcmpi(w1,"console_silent")==0){
  2645. msg_silent = atoi(w2);
  2646. if( msg_silent ) /* only bother if its actually enabled */
  2647. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  2648. } else if (strcmpi(w1, "console_msg_log") == 0) {
  2649. console_msg_log = atoi(w2);
  2650. } else if (strcmpi(w1, "console_log_filepath") == 0) {
  2651. safestrncpy(console_log_filepath, w2, sizeof(console_log_filepath));
  2652. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  2653. stdout_with_ansisequence = config_switch(w2);
  2654. } else if (strcmpi(w1, "char_maintenance") == 0) {
  2655. charserv_config.char_maintenance = atoi(w2);
  2656. } else if (strcmpi(w1, "char_new") == 0) {
  2657. charserv_config.char_new = (bool)config_switch(w2);
  2658. } else if (strcmpi(w1, "char_new_display") == 0) {
  2659. charserv_config.char_new_display = atoi(w2);
  2660. } else if (strcmpi(w1, "max_connect_user") == 0) {
  2661. charserv_config.max_connect_user = atoi(w2);
  2662. if (charserv_config.max_connect_user < -1)
  2663. charserv_config.max_connect_user = -1;
  2664. } else if(strcmpi(w1, "gm_allow_group") == 0) {
  2665. charserv_config.gm_allow_group = atoi(w2);
  2666. } else if (strcmpi(w1, "autosave_time") == 0) {
  2667. charserv_config.autosave_interval = atoi(w2)*1000;
  2668. if (charserv_config.autosave_interval <= 0)
  2669. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2670. } else if (strcmpi(w1, "save_log") == 0) {
  2671. charserv_config.save_log = config_switch(w2);
  2672. #ifdef RENEWAL
  2673. } else if (strcmpi(w1, "start_point") == 0) {
  2674. #else
  2675. } else if (strcmpi(w1, "start_point_pre") == 0) {
  2676. #endif
  2677. char_config_split_startpoint(w1, w2, charserv_config.start_point, &charserv_config.start_point_count);
  2678. #if PACKETVER >= 20151001
  2679. } else if (strcmpi(w1, "start_point_doram") == 0) {
  2680. char_config_split_startpoint(w1, w2, charserv_config.start_point_doram, &charserv_config.start_point_count_doram);
  2681. #endif
  2682. } else if (strcmpi(w1, "start_zeny") == 0) {
  2683. charserv_config.start_zeny = atoi(w2);
  2684. if (charserv_config.start_zeny < 0)
  2685. charserv_config.start_zeny = 0;
  2686. } else if (strcmpi(w1, "start_items") == 0) {
  2687. char_config_split_startitem(w1, w2, charserv_config.start_items);
  2688. #if PACKETVER >= 20151001
  2689. } else if (strcmpi(w1, "start_items_doram") == 0) {
  2690. char_config_split_startitem(w1, w2, charserv_config.start_items_doram);
  2691. #endif
  2692. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  2693. charserv_config.log_char = config_switch(w2);
  2694. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  2695. safestrncpy(charserv_config.char_config.unknown_char_name, w2, sizeof(charserv_config.char_config.unknown_char_name));
  2696. charserv_config.char_config.unknown_char_name[NAME_LENGTH-1] = '\0';
  2697. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  2698. charserv_config.char_config.name_ignoring_case = (bool)config_switch(w2);
  2699. } else if (strcmpi(w1, "char_name_option") == 0) {
  2700. charserv_config.char_config.char_name_option = atoi(w2);
  2701. } else if (strcmpi(w1, "char_name_letters") == 0) {
  2702. safestrncpy(charserv_config.char_config.char_name_letters, w2, sizeof(charserv_config.char_config.char_name_letters));
  2703. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  2704. charserv_config.char_config.char_del_level = atoi(w2);
  2705. } else if (strcmpi(w1, "char_del_delay") == 0) {
  2706. charserv_config.char_config.char_del_delay = atoi(w2);
  2707. } else if (strcmpi(w1, "char_del_option") == 0) {
  2708. charserv_config.char_config.char_del_option = atoi(w2);
  2709. } else if (strcmpi(w1, "char_del_restriction") == 0) {
  2710. charserv_config.char_config.char_del_restriction = atoi(w2);
  2711. } else if (strcmpi(w1, "char_rename_party") == 0) {
  2712. charserv_config.char_config.char_rename_party = (bool)config_switch(w2);
  2713. } else if (strcmpi(w1, "char_rename_guild") == 0) {
  2714. charserv_config.char_config.char_rename_guild = (bool)config_switch(w2);
  2715. } else if(strcmpi(w1,"db_path")==0) {
  2716. safestrncpy(schema_config.db_path, w2, sizeof(schema_config.db_path));
  2717. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  2718. fame_list_size_chemist = atoi(w2);
  2719. if (fame_list_size_chemist > MAX_FAME_LIST) {
  2720. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  2721. fame_list_size_chemist = MAX_FAME_LIST;
  2722. }
  2723. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  2724. fame_list_size_smith = atoi(w2);
  2725. if (fame_list_size_smith > MAX_FAME_LIST) {
  2726. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  2727. fame_list_size_smith = MAX_FAME_LIST;
  2728. }
  2729. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  2730. fame_list_size_taekwon = atoi(w2);
  2731. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  2732. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  2733. fame_list_size_taekwon = MAX_FAME_LIST;
  2734. }
  2735. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  2736. charserv_config.guild_exp_rate = atoi(w2);
  2737. } else if (strcmpi(w1, "pincode_enabled") == 0) {
  2738. #if PACKETVER_SUPPORTS_PINCODE
  2739. charserv_config.pincode_config.pincode_enabled = config_switch(w2);
  2740. } else if (strcmpi(w1, "pincode_changetime") == 0) {
  2741. charserv_config.pincode_config.pincode_changetime = atoi(w2)*60*60*24;
  2742. } else if (strcmpi(w1, "pincode_maxtry") == 0) {
  2743. charserv_config.pincode_config.pincode_maxtry = atoi(w2);
  2744. } else if (strcmpi(w1, "pincode_force") == 0) {
  2745. charserv_config.pincode_config.pincode_force = config_switch(w2);
  2746. } else if (strcmpi(w1, "pincode_allow_repeated") == 0) {
  2747. charserv_config.pincode_config.pincode_allow_repeated = config_switch(w2);
  2748. } else if (strcmpi(w1, "pincode_allow_sequential") == 0) {
  2749. charserv_config.pincode_config.pincode_allow_sequential = config_switch(w2);
  2750. #else
  2751. if( config_switch(w2) ) {
  2752. ShowWarning("pincode_enabled requires PACKETVER 20110309 or higher.\n");
  2753. }
  2754. #endif
  2755. } else if (strcmpi(w1, "char_move_enabled") == 0) {
  2756. charserv_config.charmove_config.char_move_enabled = config_switch(w2);
  2757. } else if (strcmpi(w1, "char_movetoused") == 0) {
  2758. charserv_config.charmove_config.char_movetoused = config_switch(w2);
  2759. } else if (strcmpi(w1, "char_moves_unlimited") == 0) {
  2760. charserv_config.charmove_config.char_moves_unlimited = config_switch(w2);
  2761. } else if (strcmpi(w1, "char_checkdb") == 0) {
  2762. charserv_config.char_check_db = config_switch(w2);
  2763. } else if (strcmpi(w1, "default_map") == 0) {
  2764. safestrncpy(charserv_config.default_map, w2, MAP_NAME_LENGTH);
  2765. } else if (strcmpi(w1, "default_map_x") == 0) {
  2766. charserv_config.default_map_x = atoi(w2);
  2767. } else if (strcmpi(w1, "default_map_y") == 0) {
  2768. charserv_config.default_map_y = atoi(w2);
  2769. } else if (strcmpi(w1, "clan_remove_inactive_days") == 0) {
  2770. charserv_config.clan_remove_inactive_days = atoi(w2);
  2771. } else if (strcmpi(w1, "mail_return_days") == 0) {
  2772. charserv_config.mail_return_days = atoi(w2);
  2773. } else if (strcmpi(w1, "mail_delete_days") == 0) {
  2774. charserv_config.mail_delete_days = atoi(w2);
  2775. } else if (strcmpi(w1, "mail_return_empty") == 0) {
  2776. charserv_config.mail_return_empty = config_switch(w2);
  2777. } else if (strcmpi(w1, "allowed_job_flag") == 0) {
  2778. charserv_config.allowed_job_flag = atoi(w2);
  2779. } else if (strcmpi(w1, "import") == 0) {
  2780. char_config_read(w2, normal);
  2781. }
  2782. }
  2783. fclose(fp);
  2784. ShowInfo("Done reading %s.\n", cfgName);
  2785. return true;
  2786. }
  2787. /**
  2788. * Checks for values out of range.
  2789. */
  2790. void char_config_adjust() {
  2791. #if PACKETVER < 20100803
  2792. if (charserv_config.char_config.char_del_option&CHAR_DEL_BIRTHDATE) {
  2793. ShowWarning("conf/char_athena.conf:char_del_option birthdate is enabled but it requires PACKETVER 2010-08-03 or newer, defaulting to email...\n");
  2794. charserv_config.char_config.char_del_option &= ~CHAR_DEL_BIRTHDATE;
  2795. }
  2796. #endif
  2797. }
  2798. /*
  2799. * Message conf function
  2800. */
  2801. int char_msg_config_read(const char *cfgName){
  2802. return _msg_config_read(cfgName,CHAR_MAX_MSG,msg_table);
  2803. }
  2804. const char* char_msg_txt(int msg_number){
  2805. return _msg_txt(msg_number,CHAR_MAX_MSG,msg_table);
  2806. }
  2807. void char_do_final_msg(void){
  2808. _do_final_msg(CHAR_MAX_MSG,msg_table);
  2809. }
  2810. void do_final(void)
  2811. {
  2812. ShowStatus("Terminating...\n");
  2813. char_set_all_offline(-1);
  2814. char_set_all_offline_sql();
  2815. inter_final();
  2816. flush_fifos();
  2817. do_final_msg();
  2818. do_final_chmapif();
  2819. do_final_chlogif();
  2820. char_db_->destroy(char_db_, NULL);
  2821. online_char_db->destroy(online_char_db, NULL);
  2822. auth_db->destroy(auth_db, NULL);
  2823. if( char_fd != -1 )
  2824. {
  2825. do_close(char_fd);
  2826. char_fd = -1;
  2827. }
  2828. Sql_Free(sql_handle);
  2829. mapindex_final();
  2830. ShowStatus("Finished.\n");
  2831. }
  2832. void set_server_type(void){
  2833. SERVER_TYPE = ATHENA_SERVER_CHAR;
  2834. }
  2835. //------------------------------
  2836. // Function called when the server
  2837. // has received a crash signal.
  2838. //------------------------------
  2839. void do_abort(void)
  2840. {
  2841. }
  2842. /// Called when a terminate signal is received.
  2843. void do_shutdown(void) {
  2844. if( runflag != CHARSERVER_ST_SHUTDOWN )
  2845. {
  2846. int id;
  2847. runflag = CHARSERVER_ST_SHUTDOWN;
  2848. ShowStatus("Shutting down...\n");
  2849. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  2850. for( id = 0; id < ARRAYLENGTH(map_server); ++id )
  2851. chmapif_server_reset(id);
  2852. chlogif_check_shutdown();
  2853. flush_fifos();
  2854. runflag = CORE_ST_STOP;
  2855. }
  2856. }
  2857. int do_init(int argc, char **argv)
  2858. {
  2859. //Read map indexes
  2860. runflag = CHARSERVER_ST_STARTING;
  2861. mapindex_init();
  2862. // Init default value
  2863. CHAR_CONF_NAME = "conf/char_athena.conf";
  2864. LAN_CONF_NAME = "conf/subnet_athena.conf";
  2865. SQL_CONF_NAME = "conf/inter_athena.conf";
  2866. MSG_CONF_NAME_EN = "conf/msg_conf/char_msg.conf";
  2867. safestrncpy(console_log_filepath, "./log/char-msg_log.log", sizeof(console_log_filepath));
  2868. cli_get_options(argc,argv);
  2869. char_set_defaults();
  2870. char_config_read(CHAR_CONF_NAME, true);
  2871. char_config_adjust();
  2872. char_lan_config_read(LAN_CONF_NAME);
  2873. char_set_default_sql();
  2874. char_sql_config_read(SQL_CONF_NAME);
  2875. msg_config_read(MSG_CONF_NAME_EN);
  2876. // Skip this check if the server is run with run-once flag
  2877. if (runflag!=CORE_ST_STOP && strcmp(charserv_config.userid, "s1")==0 && strcmp(charserv_config.passwd, "p1")==0) {
  2878. ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  2879. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  2880. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  2881. }
  2882. inter_init_sql((argc > 2) ? argv[2] : SQL_CONF_NAME); // inter server configuration
  2883. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  2884. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  2885. char_mmo_sql_init();
  2886. char_read_fame_list(); //Read fame lists.
  2887. if ((naddr_ != 0) && (!(charserv_config.login_ip) || !(charserv_config.char_ip) ))
  2888. {
  2889. char ip_str[16];
  2890. ip2str(addr_[0], ip_str);
  2891. if (naddr_ > 1)
  2892. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  2893. else
  2894. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  2895. if (!(charserv_config.login_ip) ) {
  2896. safestrncpy(charserv_config.login_ip_str, ip_str, sizeof(charserv_config.login_ip_str));
  2897. charserv_config.login_ip = str2ip(charserv_config.login_ip_str);
  2898. }
  2899. if (!(charserv_config.char_ip)) {
  2900. safestrncpy(charserv_config.char_ip_str, ip_str, sizeof(charserv_config.char_ip_str));
  2901. charserv_config.char_ip = str2ip(charserv_config.char_ip_str);
  2902. }
  2903. }
  2904. do_init_chlogif();
  2905. do_init_chmapif();
  2906. // periodically update the overall user count on all mapservers + login server
  2907. add_timer_func_list(chlogif_broadcast_user_count, "broadcast_user_count");
  2908. add_timer_interval(gettick() + 1000, chlogif_broadcast_user_count, 0, 0, 5 * 1000);
  2909. // Timer to clear (online_char_db)
  2910. add_timer_func_list(char_chardb_waiting_disconnect, "chardb_waiting_disconnect");
  2911. // Online Data timers (checking if char still connected)
  2912. add_timer_func_list(char_online_data_cleanup, "online_data_cleanup");
  2913. add_timer_interval(gettick() + 1000, char_online_data_cleanup, 0, 0, 600 * 1000);
  2914. // periodically remove players that have not logged in for a long time from clans
  2915. add_timer_func_list(char_clan_member_cleanup, "clan_member_cleanup");
  2916. add_timer_interval(gettick() + 1000, char_clan_member_cleanup, 0, 0, 60 * 60 * 1000); // every 60 minutes
  2917. // periodically check if mails need to be returned to their sender
  2918. add_timer_func_list(mail_return_timer, "mail_return_timer");
  2919. add_timer_interval(gettick() + 1000, mail_return_timer, 0, 0, 1 * 60 * 1000); // every minute
  2920. // periodically check if mails need to be deleted completely
  2921. add_timer_func_list(mail_delete_timer, "mail_delete_timer");
  2922. add_timer_interval(gettick() + 1000, mail_delete_timer, 0, 0, 1 * 60 * 1000); // every minute
  2923. //check db tables
  2924. if(charserv_config.char_check_db && char_checkdb() == 0){
  2925. ShowFatalError("char : A tables is missing in sql-server, please fix it, see (sql-files main.sql for structure) \n");
  2926. exit(EXIT_FAILURE);
  2927. }
  2928. //Cleaning the tables for NULL entrys @ startup [Sirius]
  2929. //Chardb clean
  2930. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", schema_config.char_db) )
  2931. Sql_ShowDebug(sql_handle);
  2932. //guilddb clean
  2933. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", schema_config.guild_db) )
  2934. Sql_ShowDebug(sql_handle);
  2935. //guildmemberdb clean
  2936. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `char_id` = '0'", schema_config.guild_member_db) )
  2937. Sql_ShowDebug(sql_handle);
  2938. set_defaultparse(chclif_parse);
  2939. if( (char_fd = make_listen_bind(charserv_config.bind_ip,charserv_config.char_port)) == -1 ) {
  2940. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",charserv_config.char_port);
  2941. exit(EXIT_FAILURE);
  2942. }
  2943. if( runflag != CORE_ST_STOP )
  2944. {
  2945. shutdown_callback = do_shutdown;
  2946. runflag = CHARSERVER_ST_RUNNING;
  2947. }
  2948. do_init_chcnslif();
  2949. mapindex_check_mapdefault(charserv_config.default_map);
  2950. ShowInfo("Default map: '" CL_WHITE "%s %d,%d" CL_RESET "'\n", charserv_config.default_map, charserv_config.default_map_x, charserv_config.default_map_y);
  2951. ShowStatus("The char-server is " CL_GREEN "ready" CL_RESET " (Server is listening on the port %d).\n\n", charserv_config.char_port);
  2952. return 0;
  2953. }