char.cpp 136 KB

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