char.cpp 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278
  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) || (p->disable_partyinvite != cp->disable_partyinvite)
  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',`disable_partyinvite`='%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, p->disable_partyinvite,
  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_INT32, &item.id, 0, nullptr, nullptr);
  526. stmt.BindColumn( 1, SQLDT_UINT32, &item.nameid, 0, nullptr, nullptr);
  527. stmt.BindColumn( 2, SQLDT_INT16, &item.amount, 0, nullptr, nullptr);
  528. stmt.BindColumn( 3, SQLDT_UINT32, &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_UINT32, &item.expire_time, 0, nullptr, nullptr);
  533. stmt.BindColumn( 8, SQLDT_UINT32, &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_UINT32, &item.equipSwitch, 0, nullptr, nullptr);
  539. }
  540. for( i = 0; i < MAX_SLOTS; ++i )
  541. stmt.BindColumn(11+offset+i, SQLDT_UINT32, &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_INT16, &item.option[i].id, 0, nullptr, nullptr);
  544. stmt.BindColumn(12+offset+MAX_SLOTS+i*3, SQLDT_INT16, &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 `id`", tablename, selectoption );
  718. if( SQL_ERROR == stmt.PrepareStr(StringBuf_Value(&buf))
  719. || SQL_ERROR == stmt.BindParam(0, SQLDT_INT32, &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_INT32, &item.id, 0, nullptr, nullptr);
  727. stmt.BindColumn( 1, SQLDT_UINT32, &item.nameid, 0, nullptr, nullptr);
  728. stmt.BindColumn( 2, SQLDT_INT16, &item.amount, 0, nullptr, nullptr);
  729. stmt.BindColumn( 3, SQLDT_UINT32, &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_UINT32, &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_UINT32, &item.equipSwitch, 0, nullptr, nullptr);
  740. }
  741. for( i = 0; i < MAX_SLOTS; ++i )
  742. stmt.BindColumn(11+offset+i, SQLDT_UINT32, &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_INT16, &item.option[i].id, 0, nullptr, nullptr);
  745. stmt.BindColumn(12+offset+MAX_SLOTS+i*3, SQLDT_INT16, &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`,`disable_partyinvite`"
  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_INT32, &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_INT16, &p.class_, 0, nullptr, nullptr)
  826. || SQL_ERROR == stmt.BindColumn( 4, SQLDT_UINT32, &p.base_level, 0, nullptr, nullptr)
  827. || SQL_ERROR == stmt.BindColumn( 5, SQLDT_UINT32, &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_INT32, &p.zeny, 0, nullptr, nullptr)
  831. || SQL_ERROR == stmt.BindColumn( 9, SQLDT_INT16, &p.str, 0, nullptr, nullptr)
  832. || SQL_ERROR == stmt.BindColumn( 10, SQLDT_INT16, &p.agi, 0, nullptr, nullptr)
  833. || SQL_ERROR == stmt.BindColumn( 11, SQLDT_INT16, &p.vit, 0, nullptr, nullptr)
  834. || SQL_ERROR == stmt.BindColumn( 12, SQLDT_INT16, &p.int_, 0, nullptr, nullptr)
  835. || SQL_ERROR == stmt.BindColumn( 13, SQLDT_INT16, &p.dex, 0, nullptr, nullptr)
  836. || SQL_ERROR == stmt.BindColumn( 14, SQLDT_INT16, &p.luk, 0, nullptr, nullptr)
  837. || SQL_ERROR == stmt.BindColumn( 15, SQLDT_UINT32, &p.max_hp, 0, nullptr, nullptr)
  838. || SQL_ERROR == stmt.BindColumn( 16, SQLDT_UINT32, &p.hp, 0, nullptr, nullptr)
  839. || SQL_ERROR == stmt.BindColumn( 17, SQLDT_UINT32, &p.max_sp, 0, nullptr, nullptr)
  840. || SQL_ERROR == stmt.BindColumn( 18, SQLDT_UINT32, &p.sp, 0, nullptr, nullptr)
  841. || SQL_ERROR == stmt.BindColumn( 19, SQLDT_UINT32, &p.status_point, 0, nullptr, nullptr)
  842. || SQL_ERROR == stmt.BindColumn( 20, SQLDT_UINT32, &p.skill_point, 0, nullptr, nullptr)
  843. || SQL_ERROR == stmt.BindColumn( 21, SQLDT_UINT32, &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_INT16, &p.manner, 0, nullptr, nullptr)
  846. || SQL_ERROR == stmt.BindColumn( 24, SQLDT_INT16, &p.hair, 0, nullptr, nullptr)
  847. || SQL_ERROR == stmt.BindColumn( 25, SQLDT_INT16, &p.hair_color, 0, nullptr, nullptr)
  848. || SQL_ERROR == stmt.BindColumn( 26, SQLDT_INT16, &p.clothes_color, 0, nullptr, nullptr)
  849. || SQL_ERROR == stmt.BindColumn( 27, SQLDT_INT16, &p.body, 0, nullptr, nullptr)
  850. || SQL_ERROR == stmt.BindColumn( 28, SQLDT_INT16, &p.weapon, 0, nullptr, nullptr)
  851. || SQL_ERROR == stmt.BindColumn( 29, SQLDT_INT16, &p.shield, 0, nullptr, nullptr)
  852. || SQL_ERROR == stmt.BindColumn( 30, SQLDT_INT16, &p.head_top, 0, nullptr, nullptr)
  853. || SQL_ERROR == stmt.BindColumn( 31, SQLDT_INT16, &p.head_mid, 0, nullptr, nullptr)
  854. || SQL_ERROR == stmt.BindColumn( 32, SQLDT_INT16, &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_INT16, &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_INT16, &p.robe, 0, nullptr, nullptr)
  859. || SQL_ERROR == stmt.BindColumn( 37, SQLDT_UINT32, &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_UINT32, &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_UINT32, &p.max_ap, 0, nullptr, nullptr)
  869. || SQL_ERROR == stmt.BindColumn( 47, SQLDT_UINT32, &p.ap, 0, nullptr, nullptr)
  870. || SQL_ERROR == stmt.BindColumn( 48, SQLDT_UINT32, &p.trait_point, 0, nullptr, nullptr)
  871. || SQL_ERROR == stmt.BindColumn( 49, SQLDT_INT16, &p.pow, 0, nullptr, nullptr)
  872. || SQL_ERROR == stmt.BindColumn( 50, SQLDT_INT16, &p.sta, 0, nullptr, nullptr)
  873. || SQL_ERROR == stmt.BindColumn( 51, SQLDT_INT16, &p.wis, 0, nullptr, nullptr)
  874. || SQL_ERROR == stmt.BindColumn( 52, SQLDT_INT16, &p.spl, 0, nullptr, nullptr)
  875. || SQL_ERROR == stmt.BindColumn( 53, SQLDT_INT16, &p.con, 0, nullptr, nullptr)
  876. || SQL_ERROR == stmt.BindColumn( 54, SQLDT_INT16, &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. || SQL_ERROR == stmt.BindColumn( 58, SQLDT_UINT8, &p.disable_partyinvite, 0, nullptr, nullptr)
  881. )
  882. {
  883. SqlStmt_ShowDebug(stmt);
  884. return 0;
  885. }
  886. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == stmt.NextRow(); i++ )
  887. {
  888. sd->found_char[p.slot] = p.char_id;
  889. sd->unban_time[p.slot] = p.unban_time;
  890. p.sex = char_mmo_gender(sd, &p, sex[0]);
  891. j += char_mmo_char_tobuf(WBUFP(buf, j), &p);
  892. // Addon System
  893. // store the required info into the session
  894. sd->char_moves[p.slot] = p.character_moves;
  895. }
  896. if( count != nullptr ){
  897. *count = i;
  898. }
  899. memset(sd->new_name,0,sizeof(sd->new_name));
  900. return j;
  901. }
  902. //=====================================================================================================
  903. int32 char_mmo_char_fromsql(uint32 char_id, struct mmo_charstatus* p, bool load_everything) {
  904. int32 i;
  905. SqlStmt stmt{ *sql_handle };
  906. struct s_point_str tmp_point;
  907. struct s_skill tmp_skill;
  908. uint16 skill_count = 0;
  909. struct s_friend tmp_friend;
  910. #ifdef HOTKEY_SAVING
  911. struct hotkey tmp_hotkey;
  912. int32 hotkey_num;
  913. #endif
  914. StringBuf msg_buf;
  915. char sex[2];
  916. memset(p, 0, sizeof(struct mmo_charstatus));
  917. if (charserv_config.save_log) ShowInfo("Char load request (%d)\n", char_id);
  918. // read char data
  919. if( SQL_ERROR == stmt.Prepare( "SELECT "
  920. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  921. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  922. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,"
  923. "`hair_color`,`clothes_color`,`body`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  924. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`, `moves`,"
  925. "`unban_time`,`font`,`uniqueitem_counter`,`sex`,`hotkey_rowshift`,`clan_id`,`title_id`,`show_equip`,`hotkey_rowshift2`,"
  926. "`max_ap`,`ap`,`trait_point`,`pow`,`sta`,`wis`,`spl`,`con`,`crt`,"
  927. "`inventory_slots`,`body_direction`,`disable_call`,`last_instanceid`,`disable_partyinvite`"
  928. " FROM `%s` WHERE `char_id`=? LIMIT 1", schema_config.char_db)
  929. || SQL_ERROR == stmt.BindParam(0, SQLDT_INT32, &char_id, 0)
  930. || SQL_ERROR == stmt.Execute()
  931. || SQL_ERROR == stmt.BindColumn(0, SQLDT_INT32, &p->char_id, 0, nullptr, nullptr)
  932. || SQL_ERROR == stmt.BindColumn(1, SQLDT_INT32, &p->account_id, 0, nullptr, nullptr)
  933. || SQL_ERROR == stmt.BindColumn(2, SQLDT_UCHAR, &p->slot, 0, nullptr, nullptr)
  934. || SQL_ERROR == stmt.BindColumn(3, SQLDT_STRING, &p->name, sizeof(p->name), nullptr, nullptr)
  935. || SQL_ERROR == stmt.BindColumn(4, SQLDT_INT16, &p->class_, 0, nullptr, nullptr)
  936. || SQL_ERROR == stmt.BindColumn(5, SQLDT_UINT32, &p->base_level, 0, nullptr, nullptr)
  937. || SQL_ERROR == stmt.BindColumn(6, SQLDT_UINT32, &p->job_level, 0, nullptr, nullptr)
  938. || SQL_ERROR == stmt.BindColumn(7, SQLDT_UINT64, &p->base_exp, 0, nullptr, nullptr)
  939. || SQL_ERROR == stmt.BindColumn(8, SQLDT_UINT64, &p->job_exp, 0, nullptr, nullptr)
  940. || SQL_ERROR == stmt.BindColumn(9, SQLDT_INT32, &p->zeny, 0, nullptr, nullptr)
  941. || SQL_ERROR == stmt.BindColumn(10, SQLDT_INT16, &p->str, 0, nullptr, nullptr)
  942. || SQL_ERROR == stmt.BindColumn(11, SQLDT_INT16, &p->agi, 0, nullptr, nullptr)
  943. || SQL_ERROR == stmt.BindColumn(12, SQLDT_INT16, &p->vit, 0, nullptr, nullptr)
  944. || SQL_ERROR == stmt.BindColumn(13, SQLDT_INT16, &p->int_, 0, nullptr, nullptr)
  945. || SQL_ERROR == stmt.BindColumn(14, SQLDT_INT16, &p->dex, 0, nullptr, nullptr)
  946. || SQL_ERROR == stmt.BindColumn(15, SQLDT_INT16, &p->luk, 0, nullptr, nullptr)
  947. || SQL_ERROR == stmt.BindColumn(16, SQLDT_UINT32, &p->max_hp, 0, nullptr, nullptr)
  948. || SQL_ERROR == stmt.BindColumn(17, SQLDT_UINT32, &p->hp, 0, nullptr, nullptr)
  949. || SQL_ERROR == stmt.BindColumn(18, SQLDT_UINT32, &p->max_sp, 0, nullptr, nullptr)
  950. || SQL_ERROR == stmt.BindColumn(19, SQLDT_UINT32, &p->sp, 0, nullptr, nullptr)
  951. || SQL_ERROR == stmt.BindColumn(20, SQLDT_UINT32, &p->status_point, 0, nullptr, nullptr)
  952. || SQL_ERROR == stmt.BindColumn(21, SQLDT_UINT32, &p->skill_point, 0, nullptr, nullptr)
  953. || SQL_ERROR == stmt.BindColumn(22, SQLDT_UINT32, &p->option, 0, nullptr, nullptr)
  954. || SQL_ERROR == stmt.BindColumn(23, SQLDT_UCHAR, &p->karma, 0, nullptr, nullptr)
  955. || SQL_ERROR == stmt.BindColumn(24, SQLDT_INT16, &p->manner, 0, nullptr, nullptr)
  956. || SQL_ERROR == stmt.BindColumn(25, SQLDT_INT32, &p->party_id, 0, nullptr, nullptr)
  957. || SQL_ERROR == stmt.BindColumn(26, SQLDT_INT32, &p->guild_id, 0, nullptr, nullptr)
  958. || SQL_ERROR == stmt.BindColumn(27, SQLDT_INT32, &p->pet_id, 0, nullptr, nullptr)
  959. || SQL_ERROR == stmt.BindColumn(28, SQLDT_INT32, &p->hom_id, 0, nullptr, nullptr)
  960. || SQL_ERROR == stmt.BindColumn(29, SQLDT_INT32, &p->ele_id, 0, nullptr, nullptr)
  961. || SQL_ERROR == stmt.BindColumn(30, SQLDT_INT16, &p->hair, 0, nullptr, nullptr)
  962. || SQL_ERROR == stmt.BindColumn(31, SQLDT_INT16, &p->hair_color, 0, nullptr, nullptr)
  963. || SQL_ERROR == stmt.BindColumn(32, SQLDT_INT16, &p->clothes_color, 0, nullptr, nullptr)
  964. || SQL_ERROR == stmt.BindColumn(33, SQLDT_INT16, &p->body, 0, nullptr, nullptr)
  965. || SQL_ERROR == stmt.BindColumn(34, SQLDT_INT16, &p->weapon, 0, nullptr, nullptr)
  966. || SQL_ERROR == stmt.BindColumn(35, SQLDT_INT16, &p->shield, 0, nullptr, nullptr)
  967. || SQL_ERROR == stmt.BindColumn(36, SQLDT_INT16, &p->head_top, 0, nullptr, nullptr)
  968. || SQL_ERROR == stmt.BindColumn(37, SQLDT_INT16, &p->head_mid, 0, nullptr, nullptr)
  969. || SQL_ERROR == stmt.BindColumn(38, SQLDT_INT16, &p->head_bottom, 0, nullptr, nullptr)
  970. || SQL_ERROR == stmt.BindColumn(39, SQLDT_STRING, &p->last_point.map, sizeof(p->last_point.map), nullptr, nullptr)
  971. || SQL_ERROR == stmt.BindColumn(40, SQLDT_INT16, &p->last_point.x, 0, nullptr, nullptr)
  972. || SQL_ERROR == stmt.BindColumn(41, SQLDT_INT16, &p->last_point.y, 0, nullptr, nullptr)
  973. || SQL_ERROR == stmt.BindColumn(42, SQLDT_STRING, &p->save_point.map, sizeof(p->save_point.map), nullptr, nullptr)
  974. || SQL_ERROR == stmt.BindColumn(43, SQLDT_INT16, &p->save_point.x, 0, nullptr, nullptr)
  975. || SQL_ERROR == stmt.BindColumn(44, SQLDT_INT16, &p->save_point.y, 0, nullptr, nullptr)
  976. || SQL_ERROR == stmt.BindColumn(45, SQLDT_UINT32, &p->partner_id, 0, nullptr, nullptr)
  977. || SQL_ERROR == stmt.BindColumn(46, SQLDT_UINT32, &p->father, 0, nullptr, nullptr)
  978. || SQL_ERROR == stmt.BindColumn(47, SQLDT_UINT32, &p->mother, 0, nullptr, nullptr)
  979. || SQL_ERROR == stmt.BindColumn(48, SQLDT_UINT32, &p->child, 0, nullptr, nullptr)
  980. || SQL_ERROR == stmt.BindColumn(49, SQLDT_INT32, &p->fame, 0, nullptr, nullptr)
  981. || SQL_ERROR == stmt.BindColumn(50, SQLDT_INT16, &p->rename, 0, nullptr, nullptr)
  982. || SQL_ERROR == stmt.BindColumn(51, SQLDT_UINT32, &p->delete_date, 0, nullptr, nullptr)
  983. || SQL_ERROR == stmt.BindColumn(52, SQLDT_INT16, &p->robe, 0, nullptr, nullptr)
  984. || SQL_ERROR == stmt.BindColumn(53, SQLDT_UINT32, &p->character_moves, 0, nullptr, nullptr)
  985. || SQL_ERROR == stmt.BindColumn(54, SQLDT_LONG, &p->unban_time, 0, nullptr, nullptr)
  986. || SQL_ERROR == stmt.BindColumn(55, SQLDT_UCHAR, &p->font, 0, nullptr, nullptr)
  987. || SQL_ERROR == stmt.BindColumn(56, SQLDT_UINT32, &p->uniqueitem_counter, 0, nullptr, nullptr)
  988. || SQL_ERROR == stmt.BindColumn(57, SQLDT_ENUM, &sex, sizeof(sex), nullptr, nullptr)
  989. || SQL_ERROR == stmt.BindColumn(58, SQLDT_UCHAR, &p->hotkey_rowshift, 0, nullptr, nullptr)
  990. || SQL_ERROR == stmt.BindColumn(59, SQLDT_INT32, &p->clan_id, 0, nullptr, nullptr)
  991. || SQL_ERROR == stmt.BindColumn(60, SQLDT_ULONG, &p->title_id, 0, nullptr, nullptr)
  992. || SQL_ERROR == stmt.BindColumn(61, SQLDT_UINT16, &p->show_equip, 0, nullptr, nullptr)
  993. || SQL_ERROR == stmt.BindColumn(62, SQLDT_UCHAR, &p->hotkey_rowshift2, 0, nullptr, nullptr)
  994. || SQL_ERROR == stmt.BindColumn(63, SQLDT_UINT32, &p->max_ap, 0, nullptr, nullptr)
  995. || SQL_ERROR == stmt.BindColumn(64, SQLDT_UINT32, &p->ap, 0, nullptr, nullptr)
  996. || SQL_ERROR == stmt.BindColumn(65, SQLDT_UINT32, &p->trait_point, 0, nullptr, nullptr)
  997. || SQL_ERROR == stmt.BindColumn(66, SQLDT_INT16, &p->pow, 0, nullptr, nullptr)
  998. || SQL_ERROR == stmt.BindColumn(67, SQLDT_INT16, &p->sta, 0, nullptr, nullptr)
  999. || SQL_ERROR == stmt.BindColumn(68, SQLDT_INT16, &p->wis, 0, nullptr, nullptr)
  1000. || SQL_ERROR == stmt.BindColumn(69, SQLDT_INT16, &p->spl, 0, nullptr, nullptr)
  1001. || SQL_ERROR == stmt.BindColumn(70, SQLDT_INT16, &p->con, 0, nullptr, nullptr)
  1002. || SQL_ERROR == stmt.BindColumn(71, SQLDT_INT16, &p->crt, 0, nullptr, nullptr)
  1003. || SQL_ERROR == stmt.BindColumn(72, SQLDT_UINT16, &p->inventory_slots, 0, nullptr, nullptr)
  1004. || SQL_ERROR == stmt.BindColumn(73, SQLDT_UINT8, &p->body_direction, 0, nullptr, nullptr)
  1005. || SQL_ERROR == stmt.BindColumn(74, SQLDT_UINT8, &p->disable_call, 0, nullptr, nullptr)
  1006. || SQL_ERROR == stmt.BindColumn(75, SQLDT_INT32, &p->last_point_instanceid, 0, nullptr, nullptr)
  1007. || SQL_ERROR == stmt.BindColumn(76, SQLDT_UINT8, &p->disable_partyinvite, 0, nullptr, nullptr)
  1008. )
  1009. {
  1010. SqlStmt_ShowDebug(stmt);
  1011. return 0;
  1012. }
  1013. if( SQL_ERROR == stmt.NextRow() )
  1014. {
  1015. ShowError("Requested non-existant character id: %d!\n", char_id);
  1016. return 0;
  1017. }
  1018. p->sex = char_mmo_gender(nullptr, p, sex[0]);
  1019. StringBuf_Init(&msg_buf);
  1020. StringBuf_AppendStr(&msg_buf, " status");
  1021. if (!load_everything) // For quick selection of data when displaying the char menu
  1022. {
  1023. StringBuf_Destroy(&msg_buf);
  1024. return 1;
  1025. }
  1026. //read memo data
  1027. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  1028. 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)
  1029. || SQL_ERROR == stmt.BindParam(0, SQLDT_INT32, &char_id, 0)
  1030. || SQL_ERROR == stmt.Execute()
  1031. || SQL_ERROR == stmt.BindColumn(0, SQLDT_STRING, &tmp_point.map, sizeof(tmp_point.map), nullptr, nullptr)
  1032. || SQL_ERROR == stmt.BindColumn(1, SQLDT_INT16, &tmp_point.x, 0, nullptr, nullptr)
  1033. || SQL_ERROR == stmt.BindColumn(2, SQLDT_INT16, &tmp_point.y, 0, nullptr, nullptr) )
  1034. SqlStmt_ShowDebug(stmt);
  1035. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == stmt.NextRow(); ++i )
  1036. {
  1037. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  1038. }
  1039. StringBuf_AppendStr(&msg_buf, " memo");
  1040. //read skill
  1041. //`skill` (`char_id`, `id`, `lv`)
  1042. if( SQL_ERROR == stmt.Prepare("SELECT `id`, `lv`,`flag` FROM `%s` WHERE `char_id`=? LIMIT %d", schema_config.skill_db, MAX_SKILL)
  1043. || SQL_ERROR == stmt.BindParam(0, SQLDT_INT32, &char_id, 0)
  1044. || SQL_ERROR == stmt.Execute()
  1045. || SQL_ERROR == stmt.BindColumn(0, SQLDT_UINT16, &tmp_skill.id , 0, nullptr, nullptr)
  1046. || SQL_ERROR == stmt.BindColumn(1, SQLDT_UINT8 , &tmp_skill.lv , 0, nullptr, nullptr)
  1047. || SQL_ERROR == stmt.BindColumn(2, SQLDT_UINT8 , &tmp_skill.flag, 0, nullptr, nullptr) )
  1048. SqlStmt_ShowDebug(stmt);
  1049. if( tmp_skill.flag != SKILL_FLAG_PERM_GRANTED )
  1050. tmp_skill.flag = SKILL_FLAG_PERMANENT;
  1051. for( i = 0; skill_count < MAX_SKILL && SQL_SUCCESS == stmt.NextRow(); i++ ) {
  1052. if( tmp_skill.id > 0 && tmp_skill.id < MAX_SKILL_ID ) {
  1053. memcpy(&p->skill[i], &tmp_skill, sizeof(tmp_skill));
  1054. skill_count++;
  1055. }
  1056. else
  1057. 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);
  1058. }
  1059. StringBuf_Printf(&msg_buf, " %d skills", skill_count);
  1060. //read friends
  1061. //`friends` (`char_id`, `friend_id`)
  1062. 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)
  1063. || SQL_ERROR == stmt.BindParam(0, SQLDT_INT32, &char_id, 0)
  1064. || SQL_ERROR == stmt.Execute()
  1065. || SQL_ERROR == stmt.BindColumn(0, SQLDT_INT32, &tmp_friend.account_id, 0, nullptr, nullptr)
  1066. || SQL_ERROR == stmt.BindColumn(1, SQLDT_INT32, &tmp_friend.char_id, 0, nullptr, nullptr)
  1067. || SQL_ERROR == stmt.BindColumn(2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), nullptr, nullptr) )
  1068. SqlStmt_ShowDebug(stmt);
  1069. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == stmt.NextRow(); ++i )
  1070. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  1071. StringBuf_AppendStr(&msg_buf, " friends");
  1072. #ifdef HOTKEY_SAVING
  1073. //read hotkeys
  1074. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  1075. if( SQL_ERROR == stmt.Prepare("SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", schema_config.hotkey_db)
  1076. || SQL_ERROR == stmt.BindParam(0, SQLDT_INT32, &char_id, 0)
  1077. || SQL_ERROR == stmt.Execute()
  1078. || SQL_ERROR == stmt.BindColumn(0, SQLDT_INT32, &hotkey_num, 0, nullptr, nullptr)
  1079. || SQL_ERROR == stmt.BindColumn(1, SQLDT_UCHAR, &tmp_hotkey.type, 0, nullptr, nullptr)
  1080. || SQL_ERROR == stmt.BindColumn(2, SQLDT_UINT32, &tmp_hotkey.id, 0, nullptr, nullptr)
  1081. || SQL_ERROR == stmt.BindColumn(3, SQLDT_UINT16, &tmp_hotkey.lv, 0, nullptr, nullptr) )
  1082. SqlStmt_ShowDebug(stmt);
  1083. while( SQL_SUCCESS == stmt.NextRow() )
  1084. {
  1085. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS_DB )
  1086. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  1087. else
  1088. 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);
  1089. }
  1090. StringBuf_AppendStr(&msg_buf, " hotkeys");
  1091. #endif
  1092. /* Mercenary Owner DataBase */
  1093. mercenary_owner_fromsql(char_id, p);
  1094. StringBuf_AppendStr(&msg_buf, " mercenary");
  1095. if (charserv_config.save_log)
  1096. ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, StringBuf_Value(&msg_buf)); //ok. all data load successfully!
  1097. std::shared_ptr<struct mmo_charstatus> cp = util::umap_find( char_get_chardb(), char_id );
  1098. if( cp == nullptr ){
  1099. cp = std::make_shared<struct mmo_charstatus>();
  1100. cp->char_id = char_id;
  1101. char_get_chardb()[cp->char_id] = cp;
  1102. }
  1103. memcpy( cp.get(), p, sizeof( struct mmo_charstatus ) );
  1104. StringBuf_Destroy(&msg_buf);
  1105. return 1;
  1106. }
  1107. //==========================================================================================================
  1108. int32 char_mmo_sql_init(void) {
  1109. ShowStatus("Characters per Account: '%d'.\n", charserv_config.char_config.char_per_account);
  1110. //the 'set offline' part is now in check_login_conn ...
  1111. //if the server connects to loginserver
  1112. //it will dc all off players
  1113. //and send the loginserver the new state....
  1114. // Force all users offline in sql when starting char-server
  1115. // (useful when servers crashs and don't clean the database)
  1116. char_set_all_offline_sql();
  1117. return 0;
  1118. }
  1119. //-----------------------------------
  1120. // Function to change chararcter's names
  1121. //-----------------------------------
  1122. int32 char_rename_char_sql(struct char_session_data *sd, uint32 char_id)
  1123. {
  1124. struct mmo_charstatus char_dat;
  1125. char esc_name[NAME_LENGTH*2+1];
  1126. if( sd->new_name[0] == 0 ) // Not ready for rename
  1127. return 2;
  1128. if( !char_mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed.
  1129. return 2;
  1130. // If the new name is exactly the same as the old one
  1131. if( !strcmp( sd->new_name, char_dat.name ) )
  1132. return 0;
  1133. if( char_dat.rename == 0 )
  1134. return 1;
  1135. if( !charserv_config.char_config.char_rename_party && char_dat.party_id ){
  1136. return 6;
  1137. }
  1138. if( !charserv_config.char_config.char_rename_guild && char_dat.guild_id ){
  1139. return 5;
  1140. }
  1141. Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH));
  1142. switch( char_check_char_name( sd->new_name, esc_name ) ){
  1143. case 0:
  1144. break;
  1145. case -1:
  1146. // character already exists
  1147. return 4;
  1148. default:
  1149. return 8;
  1150. }
  1151. 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) )
  1152. {
  1153. Sql_ShowDebug(sql_handle);
  1154. return 3;
  1155. }
  1156. // Update party and party members with the new player name
  1157. if( char_dat.party_id )
  1158. inter_party_charname_changed(char_dat.party_id, char_id, sd->new_name);
  1159. // Change character's name into guild_db.
  1160. if( char_dat.guild_id )
  1161. inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name);
  1162. safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH);
  1163. memset(sd->new_name,0,sizeof(sd->new_name));
  1164. // log change
  1165. if( charserv_config.log_char )
  1166. {
  1167. 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`)"
  1168. "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')",
  1169. schema_config.charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) )
  1170. Sql_ShowDebug(sql_handle);
  1171. }
  1172. return 0;
  1173. }
  1174. int32 char_check_char_name(char * name, char * esc_name)
  1175. {
  1176. int32 i;
  1177. // check length of character name
  1178. if( name[0] == '\0' )
  1179. return -2; // empty character name
  1180. /**
  1181. * By default the client does not allow you to create names with less than 4 characters,
  1182. * however the use of WPE can bypass this, and this fixes the exploit.
  1183. * It can also be changed in the configuration file in conjunction with the
  1184. * 'Remove 4/6 letter Character Name Limit' client diff patch.
  1185. **/
  1186. if( strlen( name ) < charserv_config.char_config.char_name_min_length )
  1187. return -2;
  1188. // check content of character name
  1189. if( remove_control_chars(name) )
  1190. return -2; // control chars in name
  1191. // check for reserved names
  1192. if( strcmpi(name, charserv_config.wisp_server_name) == 0 )
  1193. return -1; // nick reserved for internal server messages
  1194. // check for the channel symbol
  1195. if( name[0] == '#' )
  1196. return -2;
  1197. // Check Authorised letters/symbols in the name of the character
  1198. if( charserv_config.char_config.char_name_option == 1 )
  1199. { // only letters/symbols in char_name_letters are authorised
  1200. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1201. if( strchr(charserv_config.char_config.char_name_letters, name[i]) == nullptr )
  1202. return -2;
  1203. }
  1204. else if( charserv_config.char_config.char_name_option == 2 )
  1205. { // letters/symbols in char_name_letters are forbidden
  1206. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1207. if( strchr(charserv_config.char_config.char_name_letters, name[i]) != nullptr )
  1208. return -2;
  1209. }
  1210. if( charserv_config.char_config.name_ignoring_case ) {
  1211. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE BINARY `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1212. Sql_ShowDebug(sql_handle);
  1213. return -2;
  1214. }
  1215. } else {
  1216. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1217. Sql_ShowDebug(sql_handle);
  1218. return -2;
  1219. }
  1220. }
  1221. if( Sql_NumRows(sql_handle) > 0 )
  1222. return -1; // name already exists
  1223. return 0;
  1224. }
  1225. //-----------------------------------
  1226. // Function to create a new character
  1227. //-----------------------------------
  1228. 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, int16 start_job, int32 sex ){
  1229. char name[NAME_LENGTH];
  1230. char esc_name[NAME_LENGTH*2+1];
  1231. struct s_point_str tmp_start_point[MAX_STARTPOINT];
  1232. struct startitem tmp_start_items[MAX_STARTITEM];
  1233. uint32 char_id;
  1234. int32 flag, k, start_point_idx = rnd() % charserv_config.start_point_count;
  1235. int32 status_points;
  1236. safestrncpy(name, name_, NAME_LENGTH);
  1237. normalize_name(name,TRIM_CHARS);
  1238. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1239. memset( tmp_start_point, 0, sizeof( tmp_start_point ) );
  1240. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1241. memcpy( tmp_start_point, charserv_config.start_point, sizeof( tmp_start_point ) );
  1242. memcpy(tmp_start_items, charserv_config.start_items, MAX_STARTITEM * sizeof(struct startitem));
  1243. flag = char_check_char_name(name,esc_name);
  1244. if( flag < 0 )
  1245. return flag;
  1246. // Check inputs from the client - never trust it!
  1247. // Check the slot
  1248. if( slot < 0 || slot >= sd->char_slots ){
  1249. return -4; // invalid slot
  1250. }
  1251. // Check gender
  1252. switch( sex ){
  1253. case SEX_FEMALE:
  1254. sex = 'F';
  1255. break;
  1256. case SEX_MALE:
  1257. sex = 'M';
  1258. break;
  1259. default:
  1260. ShowWarning( "Received unsupported gender '%d'...\n", sex );
  1261. return -2; // invalid input
  1262. }
  1263. // Check status values
  1264. #if PACKETVER < 20120307
  1265. // All stats together always have to add up to a total of 30 points
  1266. if( ( str + agi + vit + int_ + dex + luk ) != 30 ){
  1267. return -2; // invalid input
  1268. }
  1269. // No status can be below 1
  1270. if( str < 1 || agi < 1 || vit < 1 || int_ < 1 || dex < 1 || luk < 1 ){
  1271. return -2; // invalid input
  1272. }
  1273. // No status can be higher than 9
  1274. if( str > 9 || agi > 9 || vit > 9 || int_ > 9 || dex > 9 || luk > 9 ){
  1275. return -2; // invalid input
  1276. }
  1277. // The status pairs always have to add up to a total of 10 points
  1278. if( ( str + int_ ) != 10 || ( agi + luk ) != 10 || ( vit + dex ) != 10 ){
  1279. return -2; // invalid input
  1280. }
  1281. status_points = 0;
  1282. #else
  1283. status_points = charserv_config.start_status_points;
  1284. #endif
  1285. // check the number of already existing chars in this account
  1286. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", schema_config.char_db, sd->account_id) )
  1287. Sql_ShowDebug(sql_handle);
  1288. #ifdef VIP_ENABLE
  1289. if( Sql_NumRows(sql_handle) >= MAX_CHARS )
  1290. return -2; // character account limit exceeded
  1291. #else
  1292. if( Sql_NumRows(sql_handle) >= sd->char_slots )
  1293. return -2; // character account limit exceeded
  1294. #endif
  1295. // check char slot
  1296. 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) )
  1297. Sql_ShowDebug(sql_handle);
  1298. if( Sql_NumRows(sql_handle) > 0 )
  1299. return -2; // slot already in use
  1300. // validation success, log result
  1301. if (charserv_config.log_char) {
  1302. 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`)"
  1303. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1304. schema_config.charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1305. Sql_ShowDebug(sql_handle);
  1306. }
  1307. #if PACKETVER >= 20151001
  1308. if(!(start_job == JOB_NOVICE && (charserv_config.allowed_job_flag&1)) &&
  1309. !(start_job == JOB_SUMMONER && (charserv_config.allowed_job_flag&2)))
  1310. return -2; // Invalid job
  1311. // Check for Doram based information.
  1312. if (start_job == JOB_SUMMONER) { // Check for just this job for now.
  1313. memset( tmp_start_point, 0, sizeof( tmp_start_point ) );
  1314. memset(tmp_start_items, 0, MAX_STARTITEM * sizeof(struct startitem));
  1315. memcpy( tmp_start_point, charserv_config.start_point_doram, sizeof( tmp_start_point ) );
  1316. memcpy(tmp_start_items, charserv_config.start_items_doram, MAX_STARTITEM * sizeof(struct startitem));
  1317. start_point_idx = rnd() % charserv_config.start_point_count_doram;
  1318. }
  1319. #endif
  1320. //Insert the new char entry to the database
  1321. 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`,"
  1322. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`, `sex`, `last_instanceid`) VALUES ("
  1323. "'%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')",
  1324. schema_config.char_db, sd->account_id , slot, esc_name, start_job, charserv_config.start_zeny, status_points, str, agi, vit, int_, dex, luk,
  1325. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1326. 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 ) )
  1327. {
  1328. Sql_ShowDebug(sql_handle);
  1329. return -2; //No, stop the procedure!
  1330. }
  1331. //Retrieve the newly auto-generated char id
  1332. char_id = (int32)Sql_LastInsertId(sql_handle);
  1333. //Give the char the default items
  1334. for (k = 0; k <= MAX_STARTITEM && tmp_start_items[k].nameid != 0; k++) {
  1335. 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) )
  1336. Sql_ShowDebug(sql_handle);
  1337. }
  1338. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1339. return char_id;
  1340. }
  1341. /*----------------------------------------------------------------------------------------------------------*/
  1342. /* Divorce Players */
  1343. /*----------------------------------------------------------------------------------------------------------*/
  1344. int32 char_divorce_char_sql(int32 partner_id1, int32 partner_id2){
  1345. 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) )
  1346. Sql_ShowDebug(sql_handle);
  1347. 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) )
  1348. Sql_ShowDebug(sql_handle);
  1349. chmapif_send_ackdivorce(partner_id1, partner_id2);
  1350. return 0;
  1351. }
  1352. /*----------------------------------------------------------------------------------------------------------*/
  1353. /* Delete char - davidsiaw */
  1354. /*----------------------------------------------------------------------------------------------------------*/
  1355. /* Returns 0 if successful
  1356. * Returns < 0 for error
  1357. */
  1358. enum e_char_del_response char_delete(struct char_session_data* sd, uint32 char_id){
  1359. char name[NAME_LENGTH];
  1360. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1361. uint32 account_id;
  1362. int32 party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id, elemental_id;
  1363. time_t delete_date;
  1364. char *data;
  1365. size_t len;
  1366. int32 i;
  1367. ARR_FIND(0, MAX_CHARS, i, sd->found_char[i] == char_id);
  1368. // Such a character does not exist in the account
  1369. if (i == MAX_CHARS) {
  1370. ShowInfo("Char deletion aborted: %s, Account ID: %u, Character ID: %u\n", name, sd->account_id, char_id);
  1371. return CHAR_DELETE_NOTFOUND;
  1372. }
  1373. 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)){
  1374. Sql_ShowDebug(sql_handle);
  1375. return CHAR_DELETE_DATABASE;
  1376. }
  1377. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1378. {
  1379. ShowInfo("Char deletion aborted: %s, Account ID: %u, Character ID: %u\n", name, sd->account_id, char_id);
  1380. Sql_FreeResult(sql_handle);
  1381. return CHAR_DELETE_NOTFOUND;
  1382. }
  1383. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1384. Sql_GetData(sql_handle, 1, &data, nullptr); account_id = atoi(data);
  1385. Sql_GetData(sql_handle, 2, &data, nullptr); party_id = atoi(data);
  1386. Sql_GetData(sql_handle, 3, &data, nullptr); guild_id = atoi(data);
  1387. Sql_GetData(sql_handle, 4, &data, nullptr); base_level = atoi(data);
  1388. Sql_GetData(sql_handle, 5, &data, nullptr); hom_id = atoi(data);
  1389. Sql_GetData(sql_handle, 6, &data, nullptr); partner_id = atoi(data);
  1390. Sql_GetData(sql_handle, 7, &data, nullptr); father_id = atoi(data);
  1391. Sql_GetData(sql_handle, 8, &data, nullptr); mother_id = atoi(data);
  1392. Sql_GetData(sql_handle, 9, &data, nullptr); elemental_id = atoi(data);
  1393. Sql_GetData(sql_handle,10, &data, nullptr); delete_date = strtoul(data, nullptr, 10);
  1394. Sql_EscapeStringLen(sql_handle, esc_name, name, zmin(len, NAME_LENGTH));
  1395. Sql_FreeResult(sql_handle);
  1396. //check for config char del condition [Lupus]
  1397. if( ( charserv_config.char_config.char_del_level > 0 && base_level >= charserv_config.char_config.char_del_level )
  1398. || ( charserv_config.char_config.char_del_level < 0 && base_level <= -charserv_config.char_config.char_del_level )
  1399. ) {
  1400. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1401. return CHAR_DELETE_BASELEVEL;
  1402. }
  1403. if (charserv_config.char_config.char_del_restriction&CHAR_DEL_RESTRICT_GUILD && guild_id) // character is in guild
  1404. {
  1405. ShowInfo("Char deletion aborted: %s, Guild ID: %i\n", name, guild_id);
  1406. return CHAR_DELETE_GUILD;
  1407. }
  1408. if (charserv_config.char_config.char_del_restriction&CHAR_DEL_RESTRICT_PARTY && party_id) // character is in party
  1409. {
  1410. ShowInfo("Char deletion aborted: %s, Party ID: %i\n", name, party_id);
  1411. return CHAR_DELETE_PARTY;
  1412. }
  1413. if( charserv_config.char_config.char_del_delay > 0 && ( !delete_date || delete_date > time(nullptr) ) ){ // not queued or delay not yet passed
  1414. ShowInfo("Char deletion aborted: %s, Time was not set or has not been reached ye\n", name );
  1415. return CHAR_DELETE_TIME;
  1416. }
  1417. /* Divorce [Wizputer] */
  1418. if( partner_id )
  1419. char_divorce_char_sql(char_id, partner_id);
  1420. /* De-addopt [Zephyrus] */
  1421. if( father_id || mother_id )
  1422. { // Char is Baby
  1423. unsigned char buf[64];
  1424. 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) )
  1425. Sql_ShowDebug(sql_handle);
  1426. 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) )
  1427. Sql_ShowDebug(sql_handle);
  1428. WBUFW(buf,0) = 0x2b25;
  1429. WBUFL(buf,2) = father_id;
  1430. WBUFL(buf,6) = mother_id;
  1431. WBUFL(buf,10) = char_id; // Baby
  1432. chmapif_sendall(buf,14);
  1433. }
  1434. //Make the character leave the party [Skotlex]
  1435. if (party_id)
  1436. inter_party_leave(party_id, account_id, char_id, name);
  1437. /* delete char's pet */
  1438. //Delete the hatched pet if you have one...
  1439. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incubate` = '0'", schema_config.pet_db, char_id) )
  1440. Sql_ShowDebug(sql_handle);
  1441. //Delete all pets that are stored in eggs (inventory + cart)
  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.inventory_db, schema_config.inventory_db, char_id) )
  1443. Sql_ShowDebug(sql_handle);
  1444. 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) )
  1445. Sql_ShowDebug(sql_handle);
  1446. /* remove homunculus */
  1447. if( hom_id )
  1448. mapif_homunculus_delete(hom_id);
  1449. /* remove elemental */
  1450. if (elemental_id)
  1451. mapif_elemental_delete(elemental_id);
  1452. /* remove mercenary data */
  1453. mercenary_owner_delete(char_id);
  1454. /* delete char's friends list */
  1455. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.friend_db, char_id) )
  1456. Sql_ShowDebug(sql_handle);
  1457. /* delete char from other's friend list */
  1458. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1459. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", schema_config.friend_db, char_id) )
  1460. Sql_ShowDebug(sql_handle);
  1461. #ifdef HOTKEY_SAVING
  1462. /* delete hotkeys */
  1463. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.hotkey_db, char_id) )
  1464. Sql_ShowDebug(sql_handle);
  1465. #endif
  1466. /* delete inventory */
  1467. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.inventory_db, char_id) )
  1468. Sql_ShowDebug(sql_handle);
  1469. /* delete cart inventory */
  1470. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.cart_db, char_id) )
  1471. Sql_ShowDebug(sql_handle);
  1472. /* delete memo areas */
  1473. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.memo_db, char_id) )
  1474. Sql_ShowDebug(sql_handle);
  1475. /* delete character registry */
  1476. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_str_table, char_id) )
  1477. Sql_ShowDebug(sql_handle);
  1478. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_reg_num_table, char_id) )
  1479. Sql_ShowDebug(sql_handle);
  1480. /* delete skills */
  1481. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.skill_db, char_id) )
  1482. Sql_ShowDebug(sql_handle);
  1483. /* delete mail attachments (only received) */
  1484. 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))
  1485. Sql_ShowDebug(sql_handle);
  1486. /* delete mails (only received) */
  1487. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `dest_id`='%d'", schema_config.mail_db, char_id))
  1488. Sql_ShowDebug(sql_handle);
  1489. /* mark mails as sent from server, if a character gets deleted */
  1490. if (SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `send_id`='0' WHERE `send_id`='%d'", schema_config.mail_db, char_id))
  1491. Sql_ShowDebug(sql_handle);
  1492. #ifdef ENABLE_SC_SAVING
  1493. /* status changes */
  1494. 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) )
  1495. Sql_ShowDebug(sql_handle);
  1496. #endif
  1497. /* bonus_scripts */
  1498. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.bonus_script_db, char_id) )
  1499. Sql_ShowDebug(sql_handle);
  1500. /* Quest Data */
  1501. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.quest_db, char_id))
  1502. Sql_ShowDebug(sql_handle);
  1503. /* Achievement Data */
  1504. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.achievement_table, char_id))
  1505. Sql_ShowDebug(sql_handle);
  1506. if (charserv_config.log_char) {
  1507. 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')",
  1508. schema_config.charlog_db, account_id, 0, char_id, esc_name) )
  1509. Sql_ShowDebug(sql_handle);
  1510. }
  1511. /* delete character */
  1512. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1513. Sql_ShowDebug(sql_handle);
  1514. /* No need as we used inter_guild_leave [Skotlex]
  1515. // Also delete info from guildtables.
  1516. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1517. Sql_ShowDebug(sql_handle);
  1518. */
  1519. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.guild_db, char_id) )
  1520. Sql_ShowDebug(sql_handle);
  1521. else if( Sql_NumRows(sql_handle) > 0 )
  1522. mapif_parse_BreakGuild(0,guild_id);
  1523. else if( guild_id )
  1524. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1525. // refresh character list cache
  1526. sd->found_char[i] = -1;
  1527. return CHAR_DELETE_OK;
  1528. }
  1529. /**
  1530. * This function parse all map-serv attached to this char-serv and increase user count
  1531. * @return numbers of total users
  1532. */
  1533. int32 char_count_users(void)
  1534. {
  1535. int32 i, users;
  1536. users = 0;
  1537. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  1538. if (session_isValid(map_server[i].fd)) {
  1539. users += map_server[i].users;
  1540. }
  1541. }
  1542. return users;
  1543. }
  1544. // Writes char data to the buffer in the format used by the client.
  1545. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1546. // Returns the size
  1547. int32 char_mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p){
  1548. if( buffer == nullptr || p == nullptr )
  1549. return 0;
  1550. struct CHARACTER_INFO* info = (struct CHARACTER_INFO*)buffer;
  1551. info->GID = p->char_id;
  1552. #if PACKETVER >= 20170830
  1553. info->exp = u64min( p->base_exp, MAX_EXP );
  1554. #else
  1555. info->exp = (int32)u64min( p->base_exp, MAX_EXP );
  1556. #endif
  1557. info->money = p->zeny;
  1558. #if PACKETVER >= 20170830
  1559. info->jobexp = u64min( p->job_exp, MAX_EXP );
  1560. #else
  1561. info->jobexp = (int32)u64min( p->job_exp, MAX_EXP );
  1562. #endif
  1563. info->joblevel = p->job_level;
  1564. info->bodystate = 0; // probably opt1
  1565. info->healthstate = 0; // probably opt2
  1566. info->effectstate = p->option;
  1567. info->virtue = p->karma;
  1568. info->honor = p->manner;
  1569. info->jobpoint = umin( p->status_point, INT16_MAX );
  1570. info->hp = p->hp;
  1571. info->maxhp = p->max_hp;
  1572. info->sp = min( p->sp, INT16_MAX );
  1573. info->maxsp = min( p->max_sp, INT16_MAX );
  1574. info->speed = DEFAULT_WALK_SPEED; // p->speed;
  1575. info->job = p->class_;
  1576. info->head = p->hair;
  1577. #if PACKETVER >= 20141022
  1578. info->body = p->body;
  1579. #endif
  1580. //When the weapon is sent and your option is riding, the client crashes on login!?
  1581. info->weapon = p->option&(0x20|0x80000|0x100000|0x200000|0x400000|0x800000|0x1000000|0x2000000|0x4000000|0x8000000) ? 0 : p->weapon;
  1582. info->level = p->base_level;
  1583. info->sppoint = umin( p->skill_point, INT16_MAX );
  1584. info->accessory = p->head_bottom;
  1585. info->shield = p->shield;
  1586. info->accessory2 = p->head_top;
  1587. info->accessory3 = p->head_mid;
  1588. info->headpalette = p->hair_color;
  1589. info->bodypalette = p->clothes_color;
  1590. safestrncpy( info->name, p->name, NAME_LENGTH );
  1591. info->Str = (uint8)u16min( p->str, UINT8_MAX );
  1592. info->Agi = (uint8)u16min( p->agi, UINT8_MAX );
  1593. info->Vit = (uint8)u16min( p->vit, UINT8_MAX );
  1594. info->Int = (uint8)u16min( p->int_, UINT8_MAX );
  1595. info->Dex = (uint8)u16min( p->dex, UINT8_MAX );
  1596. info->Luk = (uint8)u16min( p->luk, UINT8_MAX );
  1597. info->CharNum = p->slot;
  1598. info->hairColor = (uint8)u16min( p->hair_color, UINT8_MAX );
  1599. info->bIsChangedCharName = ( p->rename > 0 ) ? 0 : 1;
  1600. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1601. mapindex_getmapname_ext( p->last_point.map, info->mapName );
  1602. #endif
  1603. #if PACKETVER >= 20100803
  1604. #if PACKETVER_CHAR_DELETEDATE
  1605. info->DelRevDate = ( p->delete_date ? TOL( p->delete_date - time( nullptr ) ) : 0 );
  1606. #else
  1607. info->DelRevDate = TOL( p->delete_date );
  1608. #endif
  1609. #endif
  1610. #if PACKETVER >= 20110111
  1611. info->robePalette = p->robe;
  1612. #endif
  1613. #if PACKETVER >= 20110928
  1614. // change slot feature (0 = disabled, otherwise enabled)
  1615. if( charserv_config.charmove_config.char_move_enabled == 0 )
  1616. info->chr_slot_changeCnt = 0;
  1617. else if( charserv_config.charmove_config.char_moves_unlimited )
  1618. info->chr_slot_changeCnt = 1;
  1619. else
  1620. info->chr_slot_changeCnt = max( 0, (int32)p->character_moves );
  1621. #endif
  1622. #if PACKETVER >= 20111025
  1623. info->chr_name_changeCnt = ( p->rename > 0 ) ? 1 : 0; // (0 = disabled, otherwise displays "Add-Ons" sidebar)
  1624. #endif
  1625. #if PACKETVER >= 20141016
  1626. info->sex = p->sex; // sex - (0 = female, 1 = male, 99 = logindefined)
  1627. #endif
  1628. return sizeof( struct CHARACTER_INFO );
  1629. }
  1630. int32 char_married(int32 pl1, int32 pl2)
  1631. {
  1632. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, pl1) )
  1633. Sql_ShowDebug(sql_handle);
  1634. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1635. {
  1636. char* data;
  1637. Sql_GetData(sql_handle, 0, &data, nullptr);
  1638. if( pl2 == atoi(data) )
  1639. {
  1640. Sql_FreeResult(sql_handle);
  1641. return 1;
  1642. }
  1643. }
  1644. Sql_FreeResult(sql_handle);
  1645. return 0;
  1646. }
  1647. int32 char_child(int32 parent_id, int32 child_id)
  1648. {
  1649. if( parent_id == 0 || child_id == 0) //Failsafe, avoild querys and fix EXP bug dividing with lower level chars
  1650. return 0;
  1651. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", schema_config.char_db, parent_id) )
  1652. Sql_ShowDebug(sql_handle);
  1653. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1654. {
  1655. char* data;
  1656. Sql_GetData(sql_handle, 0, &data, nullptr);
  1657. if( child_id == atoi(data) )
  1658. {
  1659. Sql_FreeResult(sql_handle);
  1660. return 1;
  1661. }
  1662. }
  1663. Sql_FreeResult(sql_handle);
  1664. return 0;
  1665. }
  1666. int32 char_family(int32 cid1, int32 cid2, int32 cid3)
  1667. {
  1668. 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
  1669. return 0;
  1670. 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) )
  1671. Sql_ShowDebug(sql_handle);
  1672. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1673. {
  1674. int32 charid;
  1675. int32 partnerid;
  1676. int32 childid;
  1677. char* data;
  1678. Sql_GetData(sql_handle, 0, &data, nullptr); charid = atoi(data);
  1679. Sql_GetData(sql_handle, 1, &data, nullptr); partnerid = atoi(data);
  1680. Sql_GetData(sql_handle, 2, &data, nullptr); childid = atoi(data);
  1681. if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) ||
  1682. (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) ||
  1683. (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) )
  1684. {
  1685. Sql_FreeResult(sql_handle);
  1686. return childid;
  1687. }
  1688. }
  1689. Sql_FreeResult(sql_handle);
  1690. return 0;
  1691. }
  1692. //----------------------------------------------------------------------
  1693. // Force disconnection of an online player (with account value) by [Yor]
  1694. //----------------------------------------------------------------------
  1695. void char_disconnect_player(uint32 account_id)
  1696. {
  1697. int32 i;
  1698. struct char_session_data* sd;
  1699. // disconnect player if online on char-server
  1700. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1701. if( i < fd_max )
  1702. set_eof(i);
  1703. }
  1704. /**
  1705. * Set 'flag' value of char_session_data
  1706. * @param account_id
  1707. * @param value
  1708. * @param set True: set the value by using '|= val', False: unset the value by using '&= ~val'
  1709. **/
  1710. void char_set_session_flag_(int32 account_id, int32 val, bool set) {
  1711. int32 i;
  1712. struct char_session_data* sd;
  1713. ARR_FIND(0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id);
  1714. if (i < fd_max) {
  1715. if (set)
  1716. sd->flag |= val;
  1717. else
  1718. sd->flag &= ~val;
  1719. }
  1720. }
  1721. void char_auth_ok(int32 fd, struct char_session_data *sd) {
  1722. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), sd->account_id );
  1723. // Check if character is not online already. [Skotlex]
  1724. if( character != nullptr ){
  1725. if (character->server > -1)
  1726. { //Character already online. KICK KICK KICK
  1727. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  1728. if (character->waiting_disconnect == INVALID_TIMER)
  1729. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, char_chardb_waiting_disconnect, character->account_id, 0);
  1730. chclif_send_auth_result(fd,8);
  1731. return;
  1732. }
  1733. if (session_isValid(character->fd) && character->fd != fd)
  1734. { //There's already a connection from this account that hasn't picked a char yet.
  1735. chclif_send_auth_result(fd,8);
  1736. return;
  1737. }
  1738. character->fd = fd;
  1739. }
  1740. chlogif_send_reqaccdata(login_fd,sd); // request account data
  1741. // mark session as 'authed'
  1742. sd->auth = true;
  1743. // set char online on charserver
  1744. char_set_charselect(sd->account_id);
  1745. // continues when account data is received...
  1746. }
  1747. void char_read_fame_list(void)
  1748. {
  1749. int32 i;
  1750. char* data;
  1751. size_t len;
  1752. // Empty ranking lists
  1753. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  1754. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  1755. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  1756. // Build Blacksmith ranking list
  1757. 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) )
  1758. Sql_ShowDebug(sql_handle);
  1759. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1760. {
  1761. // char_id
  1762. Sql_GetData(sql_handle, 0, &data, nullptr);
  1763. smith_fame_list[i].id = atoi(data);
  1764. // fame
  1765. Sql_GetData(sql_handle, 1, &data, &len);
  1766. smith_fame_list[i].fame = atoi(data);
  1767. // name
  1768. Sql_GetData(sql_handle, 2, &data, &len);
  1769. memcpy(smith_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1770. }
  1771. // Build Alchemist ranking list
  1772. 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) )
  1773. Sql_ShowDebug(sql_handle);
  1774. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1775. {
  1776. // char_id
  1777. Sql_GetData(sql_handle, 0, &data, nullptr);
  1778. chemist_fame_list[i].id = atoi(data);
  1779. // fame
  1780. Sql_GetData(sql_handle, 1, &data, &len);
  1781. chemist_fame_list[i].fame = atoi(data);
  1782. // name
  1783. Sql_GetData(sql_handle, 2, &data, &len);
  1784. memcpy(chemist_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1785. }
  1786. // Build Taekwon ranking list
  1787. 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) )
  1788. Sql_ShowDebug(sql_handle);
  1789. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  1790. {
  1791. // char_id
  1792. Sql_GetData(sql_handle, 0, &data, nullptr);
  1793. taekwon_fame_list[i].id = atoi(data);
  1794. // fame
  1795. Sql_GetData(sql_handle, 1, &data, &len);
  1796. taekwon_fame_list[i].fame = atoi(data);
  1797. // name
  1798. Sql_GetData(sql_handle, 2, &data, &len);
  1799. memcpy(taekwon_fame_list[i].name, data, zmin(len, NAME_LENGTH));
  1800. }
  1801. Sql_FreeResult(sql_handle);
  1802. }
  1803. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  1804. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  1805. int32 char_loadName(uint32 char_id, char* name){
  1806. char* data;
  1807. size_t len;
  1808. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", schema_config.char_db, char_id) )
  1809. Sql_ShowDebug(sql_handle);
  1810. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1811. {
  1812. Sql_GetData(sql_handle, 0, &data, &len);
  1813. safestrncpy(name, data, NAME_LENGTH);
  1814. return 1;
  1815. }
  1816. #if PACKETVER < 20180221
  1817. else
  1818. {
  1819. safestrncpy(name, charserv_config.char_config.unknown_char_name, NAME_LENGTH);
  1820. }
  1821. #else
  1822. name[0] = '\0';
  1823. #endif
  1824. return 0;
  1825. }
  1826. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  1827. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  1828. int32 char_search_mapserver( const std::string& map, uint32 ip, uint16 port ){
  1829. for(int32 i = 0; i < ARRAYLENGTH(map_server); i++)
  1830. {
  1831. if (session_isValid(map_server[i].fd)
  1832. && (ip == (uint32)-1 || map_server[i].ip == ip)
  1833. && (port == (uint16)-1 || map_server[i].port == port))
  1834. {
  1835. for( std::string& m : map_server[i].maps ){
  1836. if( m == map ){
  1837. return i;
  1838. }
  1839. }
  1840. }
  1841. }
  1842. return -1;
  1843. }
  1844. /**
  1845. * Test to know if an IP come from LAN or WAN.
  1846. * @param ip: ip to check if in auth network
  1847. * @return 0 if from wan, or subnet_map_ip if lan
  1848. **/
  1849. int32 char_lan_subnetcheck(uint32 ip){
  1850. int32 i;
  1851. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  1852. if( i < subnet_count ) {
  1853. 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));
  1854. return subnet[i].map_ip;
  1855. } else {
  1856. ShowInfo("Subnet check [%u.%u.%u.%u]: " CL_CYAN "WAN" CL_RESET "\n", CONVIP(ip));
  1857. return 0;
  1858. }
  1859. }
  1860. // Console Command Parser [Wizputer]
  1861. //FIXME to be remove (moved to cnslif / will be done once map/char/login, all have their cnslif interface ready)
  1862. int32 parse_console(const char* buf){
  1863. return cnslif_parse(buf);
  1864. }
  1865. #if PACKETVER_SUPPORTS_PINCODE
  1866. //------------------------------------------------
  1867. //Pincode system
  1868. //------------------------------------------------
  1869. int32 char_pincode_compare( int32 fd, struct char_session_data* sd, char* pin ){
  1870. if( strcmp( sd->pincode, pin ) == 0 ){
  1871. sd->pincode_try = 0;
  1872. return 1;
  1873. }else{
  1874. chclif_pincode_sendstate( fd, sd, PINCODE_WRONG );
  1875. if( charserv_config.pincode_config.pincode_maxtry && ++sd->pincode_try >= charserv_config.pincode_config.pincode_maxtry ){
  1876. chlogif_pincode_notifyLoginPinError( sd->account_id );
  1877. }
  1878. return 0;
  1879. }
  1880. }
  1881. bool char_pincode_decrypt( uint32 userSeed, char* pin ){
  1882. int32 i;
  1883. char tab[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  1884. char *buf;
  1885. if (safestrnlen(pin, 4) != PINCODE_LENGTH)
  1886. return false;
  1887. for (i = 0; i < PINCODE_LENGTH; ++i) {
  1888. if (!ISDIGIT(pin[i]))
  1889. return false;
  1890. }
  1891. for( i = 1; i < 10; i++ ){
  1892. int32 pos;
  1893. uint32 multiplier = 0x3498, baseSeed = 0x881234;
  1894. userSeed = baseSeed + userSeed * multiplier;
  1895. pos = userSeed % ( i + 1 );
  1896. if( i != pos ){
  1897. tab[i] ^= tab[pos];
  1898. tab[pos] ^= tab[i];
  1899. tab[i] ^= tab[pos];
  1900. }
  1901. }
  1902. buf = (char *)aMalloc( sizeof(char) * ( PINCODE_LENGTH + 1 ) );
  1903. memset( buf, 0, PINCODE_LENGTH + 1 );
  1904. for( i = 0; i < PINCODE_LENGTH; i++ ){
  1905. sprintf( buf + i, "%d", tab[pin[i] - '0'] );
  1906. }
  1907. strcpy( pin, buf );
  1908. aFree( buf );
  1909. return true;
  1910. }
  1911. #endif
  1912. //------------------------------------------------
  1913. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  1914. //replies/disconnect the player we tried to kick. [Skotlex]
  1915. //------------------------------------------------
  1916. TIMER_FUNC(char_chardb_waiting_disconnect){
  1917. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), static_cast<uint32>( id ) );
  1918. // Mark it offline due to timeout.
  1919. if( character != nullptr && character->waiting_disconnect == tid ){
  1920. character->waiting_disconnect = INVALID_TIMER;
  1921. char_set_char_offline(character->char_id, character->account_id);
  1922. }
  1923. return 0;
  1924. }
  1925. TIMER_FUNC(char_online_data_cleanup){
  1926. for( auto it = char_get_onlinedb().begin(); it != char_get_onlinedb().end(); ){
  1927. std::shared_ptr<struct online_char_data> character = it->second;
  1928. // Character still connected
  1929. if( character->fd != -1 ){
  1930. return 0;
  1931. }
  1932. // Unknown server - set them offline
  1933. if( character->server == -2 ){
  1934. char_set_char_offline( character->char_id, character->account_id );
  1935. }
  1936. // Free data from players that have not been online for a while.
  1937. if( character->server < 0 ){
  1938. it = char_get_onlinedb().erase( it );
  1939. }else{
  1940. it++;
  1941. }
  1942. }
  1943. return 0;
  1944. }
  1945. TIMER_FUNC(char_clan_member_cleanup){
  1946. // Auto removal is disabled
  1947. if( charserv_config.clan_remove_inactive_days <= 0 ){
  1948. return 0;
  1949. }
  1950. 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 ) ){
  1951. Sql_ShowDebug(sql_handle);
  1952. }
  1953. return 0;
  1954. }
  1955. //----------------------------------
  1956. // Reading Lan Support configuration
  1957. // Rewrote: Anvanced subnet check [LuzZza]
  1958. //----------------------------------
  1959. int32 char_lan_config_read(const char *lancfgName) {
  1960. FILE *fp;
  1961. int32 line_num = 0, s_subnet=ARRAYLENGTH(subnet);
  1962. char line[1024], w1[64], w2[64], w3[64], w4[64];
  1963. if((fp = fopen(lancfgName, "r")) == nullptr) {
  1964. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  1965. return 1;
  1966. }
  1967. while(fgets(line, sizeof(line), fp)) {
  1968. line_num++;
  1969. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  1970. continue;
  1971. if(sscanf(line,"%63[^:]: %63[^:]:%63[^:]:%63[^\r\n]", w1, w2, w3, w4) != 4) {
  1972. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  1973. continue;
  1974. }
  1975. remove_control_chars(w1);
  1976. remove_control_chars(w2);
  1977. remove_control_chars(w3);
  1978. remove_control_chars(w4);
  1979. if( strcmpi(w1, "subnet") == 0 ){
  1980. if(subnet_count>=s_subnet) { //We skip instead of break in case we want to add other conf in that file.
  1981. ShowError("%s: Too many subnets defined, skipping line %d...\n", lancfgName, line_num);
  1982. continue;
  1983. }
  1984. subnet[subnet_count].mask = str2ip(w2);
  1985. subnet[subnet_count].char_ip = str2ip(w3);
  1986. subnet[subnet_count].map_ip = str2ip(w4);
  1987. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  1988. {
  1989. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  1990. continue;
  1991. }
  1992. subnet_count++;
  1993. }
  1994. }
  1995. if( subnet_count > 1 ) /* only useful if there is more than 1 */
  1996. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  1997. fclose(fp);
  1998. return 0;
  1999. }
  2000. /**
  2001. * Check if our table are all ok in sqlserver
  2002. * Char tables to check
  2003. * @return 0:fail, 1:success
  2004. */
  2005. bool char_checkdb(void){
  2006. int32 i;
  2007. const char* sqltable[] = {
  2008. schema_config.char_db, schema_config.hotkey_db, schema_config.scdata_db, schema_config.cart_db,
  2009. schema_config.inventory_db, schema_config.charlog_db,
  2010. schema_config.char_reg_str_table, schema_config.char_reg_num_table, schema_config.acc_reg_str_table,
  2011. schema_config.acc_reg_num_table, schema_config.skill_db, schema_config.interlog_db, schema_config.memo_db,
  2012. schema_config.guild_db, schema_config.guild_alliance_db, schema_config.guild_castle_db,
  2013. schema_config.guild_expulsion_db, schema_config.guild_member_db,
  2014. schema_config.guild_skill_db, schema_config.guild_position_db, schema_config.guild_storage_db,
  2015. schema_config.party_db, schema_config.pet_db, schema_config.friend_db, schema_config.mail_db,
  2016. schema_config.auction_db, schema_config.quest_db,
  2017. schema_config.homunculus_db, schema_config.skill_homunculus_db, schema_config.skillcooldown_homunculus_db,
  2018. schema_config.mercenary_db, schema_config.mercenary_owner_db, schema_config.skillcooldown_mercenary_db,
  2019. schema_config.elemental_db, schema_config.skillcooldown_db, schema_config.bonus_script_db,
  2020. schema_config.clan_table, schema_config.clan_alliance_table, schema_config.mail_attachment_db, schema_config.achievement_table
  2021. };
  2022. ShowInfo("Start checking DB integrity\n");
  2023. for (i=0; i<ARRAYLENGTH(sqltable); i++){ //check if they all exist and we can acces them in sql-server
  2024. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` LIMIT 1;", sqltable[i]) ){
  2025. Sql_ShowDebug(sql_handle);
  2026. return false;
  2027. }
  2028. }
  2029. //checking char_db
  2030. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,"
  2031. "`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,"
  2032. "`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,"
  2033. "`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,`hair_color`,`clothes_color`,`weapon`,"
  2034. "`shield`,`head_top`,`head_mid`,`head_bottom`,`robe`,`last_map`,`last_x`,`last_y`,`save_map`,"
  2035. "`save_x`,`save_y`,`partner_id`,`online`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,"
  2036. "`moves`,`unban_time`,`font`,`sex`,`hotkey_rowshift`,`clan_id`,`last_login`,`title_id`,`show_equip`,"
  2037. "`hotkey_rowshift2`,"
  2038. "`max_ap`,`ap`,`trait_point`,`pow`,`sta`,`wis`,`spl`,`con`,`crt`,"
  2039. "`inventory_slots`,`body_direction`,`disable_call`,`last_instanceid`,`disable_partyinvite`"
  2040. " FROM `%s` LIMIT 1;", schema_config.char_db) ){
  2041. Sql_ShowDebug(sql_handle);
  2042. return false;
  2043. }
  2044. //checking charlog_db
  2045. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`char_msg`,`account_id`,`char_num`,`name`,"
  2046. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`"
  2047. " FROM `%s` LIMIT 1;", schema_config.charlog_db) ){
  2048. Sql_ShowDebug(sql_handle);
  2049. return false;
  2050. }
  2051. //checking char_reg_str_table
  2052. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_str_table) ) {
  2053. Sql_ShowDebug(sql_handle);
  2054. return false;
  2055. }
  2056. //checking char_reg_num_table
  2057. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.char_reg_num_table) ) {
  2058. Sql_ShowDebug(sql_handle);
  2059. return false;
  2060. }
  2061. //checking acc_reg_str_table
  2062. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_str_table) ) {
  2063. Sql_ShowDebug(sql_handle);
  2064. return false;
  2065. }
  2066. //checking acc_reg_num_table
  2067. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`key`,`index`,`value` from `%s` LIMIT 1;", schema_config.acc_reg_num_table) ) {
  2068. Sql_ShowDebug(sql_handle);
  2069. return false;
  2070. }
  2071. //checking hotkey_db
  2072. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`hotkey`,`type`,`itemskill_id`,`skill_lvl`"
  2073. " FROM `%s` LIMIT 1;", schema_config.hotkey_db) ){
  2074. Sql_ShowDebug(sql_handle);
  2075. return false;
  2076. }
  2077. //checking scdata_db
  2078. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`type`,`tick`,`val1`,`val2`,`val3`,`val4`"
  2079. " FROM `%s` LIMIT 1;", schema_config.scdata_db) ){
  2080. Sql_ShowDebug(sql_handle);
  2081. return false;
  2082. }
  2083. //checking skill_db
  2084. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`id`,`lv`,`flag` FROM `%s` LIMIT 1;", schema_config.skill_db) ){
  2085. Sql_ShowDebug(sql_handle);
  2086. return false;
  2087. }
  2088. //checking interlog_db
  2089. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `time`,`log` FROM `%s` LIMIT 1;", schema_config.interlog_db) ){
  2090. Sql_ShowDebug(sql_handle);
  2091. return false;
  2092. }
  2093. //checking memo_db
  2094. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `memo_id`,`char_id`,`map`,`x`,`y` FROM `%s` LIMIT 1;", schema_config.memo_db) ){
  2095. Sql_ShowDebug(sql_handle);
  2096. return false;
  2097. }
  2098. //checking guild_db
  2099. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`name`,`char_id`,`master`,`guild_lv`,"
  2100. "`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`mes1`,`mes2`,"
  2101. "`emblem_len`,`emblem_id`,`emblem_data`,`last_master_change`"
  2102. " FROM `%s` LIMIT 1;", schema_config.guild_db) ){
  2103. Sql_ShowDebug(sql_handle);
  2104. return false;
  2105. }
  2106. //checking guild_alliance_db
  2107. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`opposition`,`alliance_id`,`name` FROM `%s` LIMIT 1;", schema_config.guild_alliance_db) ){
  2108. Sql_ShowDebug(sql_handle);
  2109. return false;
  2110. }
  2111. //checking guild_castle_db
  2112. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `castle_id`,`guild_id`,`economy`,`defense`,`triggerE`,"
  2113. "`triggerD`,`nextTime`,`payTime`,`createTime`,`visibleC`,`visibleG0`,`visibleG1`,`visibleG2`,"
  2114. "`visibleG3`,`visibleG4`,`visibleG5`,`visibleG6`,`visibleG7` "
  2115. " FROM `%s` LIMIT 1;", schema_config.guild_castle_db) ){
  2116. Sql_ShowDebug(sql_handle);
  2117. return false;
  2118. }
  2119. //checking guild_expulsion_db
  2120. 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) ){
  2121. Sql_ShowDebug(sql_handle);
  2122. return false;
  2123. }
  2124. //checking guild_member_db
  2125. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`char_id`,`exp`,`position` FROM `%s` LIMIT 1;", schema_config.guild_member_db) ){
  2126. Sql_ShowDebug(sql_handle);
  2127. return false;
  2128. }
  2129. //checking guild_skill_db
  2130. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.guild_skill_db) ){
  2131. Sql_ShowDebug(sql_handle);
  2132. return false;
  2133. }
  2134. //checking guild_position_db
  2135. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id`,`position`,`name`,`mode`,`exp_mode` FROM `%s` LIMIT 1;", schema_config.guild_position_db) ){
  2136. Sql_ShowDebug(sql_handle);
  2137. return false;
  2138. }
  2139. //checking party_db
  2140. 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) ){
  2141. Sql_ShowDebug(sql_handle);
  2142. return false;
  2143. }
  2144. //checking pet_db
  2145. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `pet_id`,`class`,`name`,`account_id`,`char_id`,`level`,"
  2146. "`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`"
  2147. " FROM `%s` LIMIT 1;", schema_config.pet_db) ){
  2148. Sql_ShowDebug(sql_handle);
  2149. return false;
  2150. }
  2151. //checking friend_db
  2152. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`friend_id` FROM `%s` LIMIT 1;", schema_config.friend_db) ){
  2153. Sql_ShowDebug(sql_handle);
  2154. return false;
  2155. }
  2156. //checking mail_db
  2157. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,"
  2158. "`title`,`message`,`time`,`status`,`zeny`,`type`"
  2159. " FROM `%s` LIMIT 1;", schema_config.mail_db) ){
  2160. Sql_ShowDebug(sql_handle);
  2161. return false;
  2162. }
  2163. //checking mail_attachment_db
  2164. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`index`,`nameid`,`amount`,`refine`,`attribute`,`identify`,"
  2165. "`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`"
  2166. " FROM `%s` LIMIT 1;", schema_config.mail_attachment_db) ){
  2167. Sql_ShowDebug(sql_handle);
  2168. return false;
  2169. }
  2170. //checking auction_db
  2171. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `auction_id`,`seller_id`,`seller_name`,`buyer_id`,`buyer_name`,"
  2172. "`price`,`buynow`,`hours`,`timestamp`,`nameid`,`item_name`,`type`,`refine`,`attribute`,`card0`,`card1`,"
  2173. "`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` "
  2174. "FROM `%s` LIMIT 1;", schema_config.auction_db) ){
  2175. Sql_ShowDebug(sql_handle);
  2176. return false;
  2177. }
  2178. //checking quest_db
  2179. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`quest_id`,`state`,`time`,`count1`,`count2`,`count3` from `%s`;", schema_config.quest_db) ){
  2180. Sql_ShowDebug(sql_handle);
  2181. return false;
  2182. }
  2183. //checking homunculus_db
  2184. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`char_id`,`class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`,"
  2185. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hp`,`max_hp`,`sp`,`max_sp`,`skill_point`,`alive`,`rename_flag`,`vaporize`,`autofeed` "
  2186. " FROM `%s` LIMIT 1;", schema_config.homunculus_db) ){
  2187. Sql_ShowDebug(sql_handle);
  2188. return false;
  2189. }
  2190. //checking skill_homunculus_db
  2191. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`id`,`lv` FROM `%s` LIMIT 1;", schema_config.skill_homunculus_db) ){
  2192. Sql_ShowDebug(sql_handle);
  2193. return false;
  2194. }
  2195. //checking skillcooldown_homunculus_db
  2196. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `homun_id`,`skill`,`tick` FROM `%s` LIMIT 1;", schema_config.skillcooldown_homunculus_db)) {
  2197. Sql_ShowDebug(sql_handle);
  2198. return false;
  2199. }
  2200. //checking mercenary_db
  2201. 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) ){
  2202. Sql_ShowDebug(sql_handle);
  2203. return false;
  2204. }
  2205. //checking mercenary_owner_db
  2206. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`merc_id`,`arch_calls`,`arch_faith`,"
  2207. "`spear_calls`,`spear_faith`,`sword_calls`,`sword_faith`"
  2208. " FROM `%s` LIMIT 1;", schema_config.mercenary_owner_db) ){
  2209. Sql_ShowDebug(sql_handle);
  2210. return false;
  2211. }
  2212. //checking skillcooldown_mercenary_db
  2213. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `mer_id`,`skill`,`tick` FROM `%s` LIMIT 1;", schema_config.skillcooldown_mercenary_db)) {
  2214. Sql_ShowDebug(sql_handle);
  2215. return false;
  2216. }
  2217. //checking elemental_db
  2218. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `ele_id`,`char_id`,`class`,`mode`,`hp`,`sp`,`max_hp`,`max_sp`,"
  2219. "`atk1`,`atk2`,`matk`,`aspd`,`def`,`mdef`,`flee`,`hit`,`life_time` "
  2220. " FROM `%s` LIMIT 1;", schema_config.elemental_db) ){
  2221. Sql_ShowDebug(sql_handle);
  2222. return false;
  2223. }
  2224. //checking skillcooldown_db
  2225. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`skill`,`tick` FROM `%s` LIMIT 1;", schema_config.skillcooldown_db) ){
  2226. Sql_ShowDebug(sql_handle);
  2227. return false;
  2228. }
  2229. //checking bonus_script_db
  2230. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`script`,`tick`,`flag`,`type`,`icon` FROM `%s` LIMIT 1;", schema_config.bonus_script_db) ){
  2231. Sql_ShowDebug(sql_handle);
  2232. return false;
  2233. }
  2234. //checking cart_db
  2235. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2236. "`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`"
  2237. " FROM `%s` LIMIT 1;", schema_config.cart_db) ){
  2238. Sql_ShowDebug(sql_handle);
  2239. return false;
  2240. }
  2241. //checking inventory_db
  2242. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`char_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2243. "`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`"
  2244. ",`favorite`,`equip_switch`"
  2245. " FROM `%s` LIMIT 1;", schema_config.inventory_db) ){
  2246. Sql_ShowDebug(sql_handle);
  2247. return false;
  2248. }
  2249. //checking guild_storage_db
  2250. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`guild_id`,`nameid`,`amount`,`equip`,`identify`,`refine`,"
  2251. "`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`"
  2252. " FROM `%s` LIMIT 1;", schema_config.guild_storage_db) ){
  2253. Sql_ShowDebug(sql_handle);
  2254. return false;
  2255. }
  2256. //checking clan table
  2257. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `clan_id`,`name`,`master`,`mapname`,`max_member`"
  2258. " FROM `%s` LIMIT 1;", schema_config.clan_table) ){
  2259. Sql_ShowDebug(sql_handle);
  2260. return false;
  2261. }
  2262. //checking clan alliance table
  2263. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `clan_id`,`opposition`,`alliance_id`,`name`"
  2264. " FROM `%s` LIMIT 1;", schema_config.clan_alliance_table) ){
  2265. Sql_ShowDebug(sql_handle);
  2266. return false;
  2267. }
  2268. //checking achievement_table
  2269. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`id`,`count1`,`count2`,`count3`,`count4`,`count5`,`count6`,`count7`,`count8`,`count9`,`count10`,`completed`,`rewarded`"
  2270. " FROM `%s` LIMIT 1;", schema_config.achievement_table)) {
  2271. Sql_ShowDebug(sql_handle);
  2272. return false;
  2273. }
  2274. Sql_FreeResult(sql_handle);
  2275. ShowInfo("DB integrity check finished with success\n");
  2276. return true;
  2277. }
  2278. void char_sql_config_read(const char* cfgName) {
  2279. char line[1024], w1[1024], w2[1024];
  2280. FILE* fp;
  2281. if ((fp = fopen(cfgName, "r")) == nullptr) {
  2282. ShowError("File not found: %s\n", cfgName);
  2283. return;
  2284. }
  2285. while(fgets(line, sizeof(line), fp)) {
  2286. if(line[0] == '/' && line[1] == '/')
  2287. continue;
  2288. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2289. continue;
  2290. if(!strcmpi(w1,"char_db"))
  2291. safestrncpy(schema_config.char_db, w2, sizeof(schema_config.char_db));
  2292. else if(!strcmpi(w1,"scdata_db"))
  2293. safestrncpy(schema_config.scdata_db, w2, sizeof(schema_config.scdata_db));
  2294. else if(!strcmpi(w1,"cart_db"))
  2295. safestrncpy(schema_config.cart_db, w2, sizeof(schema_config.cart_db));
  2296. else if(!strcmpi(w1,"inventory_db"))
  2297. safestrncpy(schema_config.inventory_db, w2, sizeof(schema_config.inventory_db));
  2298. else if(!strcmpi(w1,"charlog_db"))
  2299. safestrncpy(schema_config.charlog_db, w2, sizeof(schema_config.charlog_db));
  2300. else if(!strcmpi(w1,"skill_db"))
  2301. safestrncpy(schema_config.skill_db, w2, sizeof(schema_config.skill_db));
  2302. else if(!strcmpi(w1,"interlog_db"))
  2303. safestrncpy(schema_config.interlog_db, w2, sizeof(schema_config.interlog_db));
  2304. else if(!strcmpi(w1,"memo_db"))
  2305. safestrncpy(schema_config.memo_db, w2, sizeof(schema_config.memo_db));
  2306. else if(!strcmpi(w1,"guild_db"))
  2307. safestrncpy(schema_config.guild_db, w2, sizeof(schema_config.guild_db));
  2308. else if(!strcmpi(w1,"guild_alliance_db"))
  2309. safestrncpy(schema_config.guild_alliance_db, w2, sizeof(schema_config.guild_alliance_db));
  2310. else if(!strcmpi(w1,"guild_castle_db"))
  2311. safestrncpy(schema_config.guild_castle_db, w2, sizeof(schema_config.guild_castle_db));
  2312. else if(!strcmpi(w1,"guild_expulsion_db"))
  2313. safestrncpy(schema_config.guild_expulsion_db, w2, sizeof(schema_config.guild_expulsion_db));
  2314. else if(!strcmpi(w1,"guild_member_db"))
  2315. safestrncpy(schema_config.guild_member_db, w2, sizeof(schema_config.guild_member_db));
  2316. else if(!strcmpi(w1,"guild_skill_db"))
  2317. safestrncpy(schema_config.guild_skill_db, w2, sizeof(schema_config.guild_skill_db));
  2318. else if(!strcmpi(w1,"guild_position_db"))
  2319. safestrncpy(schema_config.guild_position_db, w2, sizeof(schema_config.guild_position_db));
  2320. else if(!strcmpi(w1,"guild_storage_db"))
  2321. safestrncpy(schema_config.guild_storage_db, w2, sizeof(schema_config.guild_storage_db));
  2322. else if(!strcmpi(w1,"party_db"))
  2323. safestrncpy(schema_config.party_db, w2, sizeof(schema_config.party_db));
  2324. else if(!strcmpi(w1,"pet_db"))
  2325. safestrncpy(schema_config.pet_db, w2, sizeof(schema_config.pet_db));
  2326. else if(!strcmpi(w1,"mail_db"))
  2327. safestrncpy(schema_config.mail_db, w2, sizeof(schema_config.mail_db));
  2328. else if (!strcmpi(w1, "mail_attachment_db"))
  2329. safestrncpy(schema_config.mail_attachment_db, w2, sizeof(schema_config.mail_attachment_db));
  2330. else if(!strcmpi(w1,"auction_db"))
  2331. safestrncpy(schema_config.auction_db, w2, sizeof(schema_config.auction_db));
  2332. else if(!strcmpi(w1,"friend_db"))
  2333. safestrncpy(schema_config.friend_db, w2, sizeof(schema_config.friend_db));
  2334. else if(!strcmpi(w1,"hotkey_db"))
  2335. safestrncpy(schema_config.hotkey_db, w2, sizeof(schema_config.hotkey_db));
  2336. else if(!strcmpi(w1,"quest_db"))
  2337. safestrncpy(schema_config.quest_db,w2,sizeof(schema_config.quest_db));
  2338. else if(!strcmpi(w1,"homunculus_db"))
  2339. safestrncpy(schema_config.homunculus_db,w2,sizeof(schema_config.homunculus_db));
  2340. else if(!strcmpi(w1,"skill_homunculus_db"))
  2341. safestrncpy(schema_config.skill_homunculus_db,w2,sizeof(schema_config.skill_homunculus_db));
  2342. else if (!strcmpi(w1, "skillcooldown_homunculus_db"))
  2343. safestrncpy(schema_config.skillcooldown_homunculus_db, w2, sizeof(schema_config.skillcooldown_homunculus_db));
  2344. else if(!strcmpi(w1,"mercenary_db"))
  2345. safestrncpy(schema_config.mercenary_db,w2,sizeof(schema_config.mercenary_db));
  2346. else if(!strcmpi(w1,"mercenary_owner_db"))
  2347. safestrncpy(schema_config.mercenary_owner_db,w2,sizeof(schema_config.mercenary_owner_db));
  2348. else if (!strcmpi(w1, "skillcooldown_mercenary_db"))
  2349. safestrncpy(schema_config.skillcooldown_mercenary_db, w2, sizeof(schema_config.skillcooldown_mercenary_db));
  2350. else if(!strcmpi(w1,"elemental_db"))
  2351. safestrncpy(schema_config.elemental_db,w2,sizeof(schema_config.elemental_db));
  2352. else if(!strcmpi(w1,"skillcooldown_db"))
  2353. safestrncpy(schema_config.skillcooldown_db, w2, sizeof(schema_config.skillcooldown_db));
  2354. else if(!strcmpi(w1,"bonus_script_db"))
  2355. safestrncpy(schema_config.bonus_script_db, w2, sizeof(schema_config.bonus_script_db));
  2356. else if(!strcmpi(w1,"char_reg_num_table"))
  2357. safestrncpy(schema_config.char_reg_num_table, w2, sizeof(schema_config.char_reg_num_table));
  2358. else if(!strcmpi(w1,"char_reg_str_table"))
  2359. safestrncpy(schema_config.char_reg_str_table, w2, sizeof(schema_config.char_reg_str_table));
  2360. else if(!strcmpi(w1,"acc_reg_str_table"))
  2361. safestrncpy(schema_config.acc_reg_str_table, w2, sizeof(schema_config.acc_reg_str_table));
  2362. else if(!strcmpi(w1,"acc_reg_num_table"))
  2363. safestrncpy(schema_config.acc_reg_num_table, w2, sizeof(schema_config.acc_reg_num_table));
  2364. else if(!strcmpi(w1,"clan_table"))
  2365. safestrncpy(schema_config.clan_table, w2, sizeof(schema_config.clan_table));
  2366. else if(!strcmpi(w1,"clan_alliance_table"))
  2367. safestrncpy(schema_config.clan_alliance_table, w2, sizeof(schema_config.clan_alliance_table));
  2368. else if(!strcmpi(w1,"achievement_table"))
  2369. safestrncpy(schema_config.achievement_table, w2, sizeof(schema_config.achievement_table));
  2370. else if(!strcmpi(w1, "start_status_points"))
  2371. charserv_config.start_status_points = atoi(w2);
  2372. //support the import command, just like any other config
  2373. else if(!strcmpi(w1,"import"))
  2374. char_sql_config_read(w2);
  2375. }
  2376. fclose(fp);
  2377. ShowInfo("Done reading %s.\n", cfgName);
  2378. }
  2379. void char_set_default_sql(){
  2380. // schema_config.db_use_sqldbs;
  2381. safestrncpy(schema_config.db_path,"db",sizeof(schema_config.db_path));
  2382. safestrncpy(schema_config.char_db,"char",sizeof(schema_config.char_db));
  2383. safestrncpy(schema_config.scdata_db,"sc_data",sizeof(schema_config.scdata_db));
  2384. safestrncpy(schema_config.cart_db,"cart_inventory",sizeof(schema_config.cart_db));
  2385. safestrncpy(schema_config.inventory_db,"inventory",sizeof(schema_config.inventory_db));
  2386. safestrncpy(schema_config.charlog_db,"charlog",sizeof(schema_config.charlog_db));
  2387. safestrncpy(schema_config.storage_db,"storage",sizeof(schema_config.storage_db));
  2388. safestrncpy(schema_config.interlog_db,"interlog",sizeof(schema_config.interlog_db));
  2389. safestrncpy(schema_config.skill_db,"skill",sizeof(schema_config.skill_db));
  2390. safestrncpy(schema_config.memo_db,"memo",sizeof(schema_config.memo_db));
  2391. safestrncpy(schema_config.guild_db,"guild",sizeof(schema_config.guild_db));
  2392. safestrncpy(schema_config.guild_alliance_db,"guild_alliance",sizeof(schema_config.guild_alliance_db));
  2393. safestrncpy(schema_config.guild_castle_db,"guild_castle",sizeof(schema_config.guild_castle_db));
  2394. safestrncpy(schema_config.guild_expulsion_db,"guild_expulsion",sizeof(schema_config.guild_expulsion_db));
  2395. safestrncpy(schema_config.guild_member_db,"guild_member",sizeof(schema_config.guild_member_db));
  2396. safestrncpy(schema_config.guild_position_db,"guild_position",sizeof(schema_config.guild_position_db));
  2397. safestrncpy(schema_config.guild_skill_db,"guild_skill",sizeof(schema_config.guild_skill_db));
  2398. safestrncpy(schema_config.guild_storage_db,"guild_storage",sizeof(schema_config.guild_storage_db));
  2399. safestrncpy(schema_config.party_db,"party",sizeof(schema_config.party_db));
  2400. safestrncpy(schema_config.pet_db,"pet",sizeof(schema_config.pet_db));
  2401. safestrncpy(schema_config.mail_db,"mail",sizeof(schema_config.mail_db)); // MAIL SYSTEM
  2402. safestrncpy(schema_config.auction_db,"auction",sizeof(schema_config.auction_db)); // Auctions System
  2403. safestrncpy(schema_config.friend_db,"friends",sizeof(schema_config.friend_db));
  2404. safestrncpy(schema_config.hotkey_db,"hotkey",sizeof(schema_config.hotkey_db));
  2405. safestrncpy(schema_config.quest_db,"quest",sizeof(schema_config.quest_db));
  2406. safestrncpy(schema_config.homunculus_db,"homunculus",sizeof(schema_config.homunculus_db));
  2407. safestrncpy(schema_config.skill_homunculus_db,"skill_homunculus",sizeof(schema_config.skill_homunculus_db));
  2408. safestrncpy(schema_config.skillcooldown_homunculus_db,"skillcooldown_homunculus",sizeof(schema_config.skillcooldown_homunculus_db));
  2409. safestrncpy(schema_config.mercenary_db,"mercenary",sizeof(schema_config.mercenary_db));
  2410. safestrncpy(schema_config.mercenary_owner_db,"mercenary_owner",sizeof(schema_config.mercenary_owner_db));
  2411. safestrncpy(schema_config.skillcooldown_mercenary_db, "skillcooldown_mercenary", sizeof(schema_config.skillcooldown_mercenary_db));
  2412. safestrncpy(schema_config.skillcooldown_db,"skillcooldown",sizeof(schema_config.skillcooldown_db));
  2413. safestrncpy(schema_config.bonus_script_db,"bonus_script",sizeof(schema_config.bonus_script_db));
  2414. safestrncpy(schema_config.char_reg_num_table,"char_reg_num",sizeof(schema_config.char_reg_num_table));
  2415. safestrncpy(schema_config.char_reg_str_table,"char_reg_str",sizeof(schema_config.char_reg_str_table));
  2416. safestrncpy(schema_config.acc_reg_str_table,"acc_reg_str",sizeof(schema_config.acc_reg_str_table));
  2417. safestrncpy(schema_config.acc_reg_num_table,"acc_reg_num",sizeof(schema_config.acc_reg_num_table));
  2418. safestrncpy(schema_config.clan_table,"clan",sizeof(schema_config.clan_table));
  2419. safestrncpy(schema_config.clan_table,"clan_alliance",sizeof(schema_config.clan_alliance_table));
  2420. safestrncpy(schema_config.achievement_table,"achievement",sizeof(schema_config.achievement_table));
  2421. }
  2422. //set default config
  2423. void char_set_defaults(){
  2424. #if PACKETVER_SUPPORTS_PINCODE
  2425. charserv_config.pincode_config.pincode_enabled = true;
  2426. charserv_config.pincode_config.pincode_changetime = 0;
  2427. charserv_config.pincode_config.pincode_maxtry = 3;
  2428. charserv_config.pincode_config.pincode_force = true;
  2429. charserv_config.pincode_config.pincode_allow_repeated = false;
  2430. charserv_config.pincode_config.pincode_allow_sequential = false;
  2431. #endif
  2432. charserv_config.charmove_config.char_move_enabled = true;
  2433. charserv_config.charmove_config.char_movetoused = true;
  2434. charserv_config.charmove_config.char_moves_unlimited = false;
  2435. charserv_config.char_config.char_per_account = 0; //Maximum chars per account (default unlimited) [Sirius]
  2436. charserv_config.char_config.char_del_level = 0; //From which level u can delete character [Lupus]
  2437. charserv_config.char_config.char_del_delay = 86400;
  2438. #if PACKETVER >= 20100803
  2439. charserv_config.char_config.char_del_option = CHAR_DEL_BIRTHDATE;
  2440. #else
  2441. charserv_config.char_config.char_del_option = CHAR_DEL_EMAIL;
  2442. #endif
  2443. charserv_config.char_config.char_del_restriction = CHAR_DEL_RESTRICT_ALL;
  2444. // charserv_config.userid[24];
  2445. // charserv_config.passwd[24];
  2446. // charserv_config.server_name[20];
  2447. safestrncpy(charserv_config.wisp_server_name,"Server",sizeof(charserv_config.wisp_server_name));
  2448. // charserv_config.login_ip_str[128];
  2449. charserv_config.login_ip = 0;
  2450. charserv_config.login_port = 6900;
  2451. // charserv_config.char_ip_str[128];
  2452. charserv_config.char_ip = 0;
  2453. // charserv_config.bind_ip_str[128];
  2454. charserv_config.bind_ip = INADDR_ANY;
  2455. charserv_config.char_port = 6121;
  2456. charserv_config.char_maintenance = 0;
  2457. charserv_config.char_new = true;
  2458. charserv_config.char_new_display = 0;
  2459. charserv_config.char_config.name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  2460. 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]
  2461. 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
  2462. 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]
  2463. charserv_config.char_config.char_name_min_length = 4; // Minimum character name length
  2464. charserv_config.save_log = 1; // show loading/saving messages
  2465. charserv_config.log_char = 1; // loggin char or not [devil]
  2466. charserv_config.log_inter = 1; // loggin inter or not [devil]
  2467. charserv_config.char_check_db =1;
  2468. // See const.hpp to change the default values
  2469. safestrncpy( charserv_config.start_point[0].map, MAP_DEFAULT_NAME, sizeof( charserv_config.start_point[0].map ) );
  2470. charserv_config.start_point[0].x = MAP_DEFAULT_X;
  2471. charserv_config.start_point[0].y = MAP_DEFAULT_Y;
  2472. charserv_config.start_point_count = 1;
  2473. #if PACKETVER >= 20151001
  2474. safestrncpy( charserv_config.start_point_doram[0].map, MAP_DEFAULT_NAME, sizeof( charserv_config.start_point_doram[0].map ) );
  2475. charserv_config.start_point_doram[0].x = MAP_DEFAULT_X;
  2476. charserv_config.start_point_doram[0].y = MAP_DEFAULT_Y;
  2477. charserv_config.start_point_count_doram = 1;
  2478. #endif
  2479. charserv_config.start_items[0].nameid = 1201;
  2480. charserv_config.start_items[0].amount = 1;
  2481. charserv_config.start_items[0].pos = EQP_HAND_R;
  2482. charserv_config.start_items[1].nameid = 2301;
  2483. charserv_config.start_items[1].amount = 1;
  2484. charserv_config.start_items[1].pos = EQP_ARMOR;
  2485. #if PACKETVER >= 20150101
  2486. charserv_config.start_items_doram[0].nameid = 1681;
  2487. charserv_config.start_items_doram[0].amount = 1;
  2488. charserv_config.start_items_doram[0].pos = EQP_HAND_R;
  2489. charserv_config.start_items_doram[1].nameid = 2301;
  2490. charserv_config.start_items_doram[1].amount = 1;
  2491. charserv_config.start_items_doram[1].pos = EQP_ARMOR;
  2492. #endif
  2493. charserv_config.start_status_points = 48;
  2494. charserv_config.console = 0;
  2495. charserv_config.max_connect_user = -1;
  2496. charserv_config.gm_allow_group = -1;
  2497. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2498. charserv_config.start_zeny = 0;
  2499. charserv_config.guild_exp_rate = 100;
  2500. charserv_config.clan_remove_inactive_days = 14;
  2501. charserv_config.mail_return_days = 14;
  2502. charserv_config.mail_delete_days = 14;
  2503. charserv_config.mail_retrieve = 1;
  2504. #if defined(RENEWAL) && PACKETVER >= 20151001
  2505. charserv_config.allowed_job_flag = 3;
  2506. #else
  2507. charserv_config.allowed_job_flag = 1;
  2508. #endif
  2509. charserv_config.clear_parties = 0;
  2510. }
  2511. /**
  2512. * Split start_point configuration values.
  2513. * @param w1_value: Value from w1
  2514. * @param w2_value: Value from w2
  2515. * @param start: Start point reference
  2516. * @param count: Start point count reference
  2517. */
  2518. void char_config_split_startpoint( char* w1_value, char* w2_value, struct s_point_str start_point[MAX_STARTPOINT], int16* count ){
  2519. char *lineitem, **fields;
  2520. int32 i = 0;
  2521. size_t fields_length = 3 + 1;
  2522. (*count) = 0; // Reset to begin reading
  2523. memset(start_point, 0, sizeof(struct s_point_str) * MAX_STARTPOINT);
  2524. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2525. if (fields == nullptr)
  2526. return; // Failed to allocate memory.
  2527. lineitem = strtok(w2_value, ":");
  2528. while (lineitem != nullptr && (*count) < MAX_STARTPOINT) {
  2529. bool error;
  2530. size_t n = sv_split( lineitem, strlen( lineitem ), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE, error );
  2531. if( error || ( n + 1 ) < fields_length ){
  2532. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2533. lineitem = strtok(nullptr, ":"); //next lineitem
  2534. continue;
  2535. }
  2536. safestrncpy( start_point[i].map, fields[1], sizeof( start_point[i].map ) );
  2537. start_point[i].x = max( 0, atoi( fields[2] ) );
  2538. start_point[i].y = max( 0, atoi( fields[3] ) );
  2539. (*count)++;
  2540. lineitem = strtok(nullptr, ":"); //next lineitem
  2541. i++;
  2542. }
  2543. aFree(fields);
  2544. }
  2545. /**
  2546. * Split start_items configuration values.
  2547. * @param w1_value: Value from w1
  2548. * @param w2_value: Value from w2
  2549. * @param start: Start item reference
  2550. */
  2551. void char_config_split_startitem(char *w1_value, char *w2_value, struct startitem start_items[MAX_STARTITEM])
  2552. {
  2553. char *lineitem, **fields;
  2554. int32 i = 0;
  2555. size_t fields_length = 3 + 1;
  2556. memset(start_items, 0, sizeof(struct startitem) * MAX_STARTITEM);
  2557. fields = (char **)aMalloc(fields_length * sizeof(char *));
  2558. if (fields == nullptr)
  2559. return; // Failed to allocate memory.
  2560. lineitem = strtok(w2_value, ":");
  2561. while (lineitem != nullptr && i < MAX_STARTITEM) {
  2562. bool error;
  2563. size_t n = sv_split( lineitem, strlen( lineitem ), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE, error );
  2564. if( error || ( n + 1 ) < fields_length ){
  2565. ShowDebug("%s: not enough arguments for %s! Skipping...\n", w1_value, lineitem);
  2566. lineitem = strtok(nullptr, ":"); //next lineitem
  2567. continue;
  2568. }
  2569. // TODO: Item ID verification
  2570. start_items[i].nameid = strtoul( fields[1], nullptr, 10 );
  2571. // TODO: Stack verification
  2572. start_items[i].amount = min( (uint16)strtoul( fields[2], nullptr, 10 ), MAX_AMOUNT );
  2573. start_items[i].pos = strtoul( fields[3], nullptr, 10 );
  2574. lineitem = strtok(nullptr, ":"); //next lineitem
  2575. i++;
  2576. }
  2577. aFree(fields);
  2578. }
  2579. bool char_config_read(const char* cfgName, bool normal){
  2580. char line[1024], w1[1024], w2[1024];
  2581. FILE* fp = fopen(cfgName, "r");
  2582. if (fp == nullptr) {
  2583. ShowError("Configuration file not found: %s.\n", cfgName);
  2584. return false;
  2585. }
  2586. while(fgets(line, sizeof(line), fp)) {
  2587. if (line[0] == '/' && line[1] == '/')
  2588. continue;
  2589. if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2)
  2590. continue;
  2591. remove_control_chars(w1);
  2592. remove_control_chars(w2);
  2593. // Config that loaded only when server started, not by reloading config file
  2594. if (normal) {
  2595. if (strcmpi(w1, "userid") == 0) {
  2596. safestrncpy(charserv_config.userid, w2, sizeof(charserv_config.userid));
  2597. } else if (strcmpi(w1, "passwd") == 0) {
  2598. safestrncpy(charserv_config.passwd, w2, sizeof(charserv_config.passwd));
  2599. } else if (strcmpi(w1, "server_name") == 0) {
  2600. safestrncpy(charserv_config.server_name, w2, sizeof(charserv_config.server_name));
  2601. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  2602. if (strlen(w2) >= 4) {
  2603. safestrncpy(charserv_config.wisp_server_name, w2, sizeof(charserv_config.wisp_server_name));
  2604. }
  2605. } else if (strcmpi(w1, "login_ip") == 0) {
  2606. charserv_config.login_ip = host2ip(w2);
  2607. if (charserv_config.login_ip) {
  2608. char ip_str[16];
  2609. safestrncpy(charserv_config.login_ip_str, w2, sizeof(charserv_config.login_ip_str));
  2610. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(charserv_config.login_ip, ip_str));
  2611. }
  2612. } else if (strcmpi(w1, "login_port") == 0) {
  2613. charserv_config.login_port = atoi(w2);
  2614. } else if (strcmpi(w1, "char_ip") == 0) {
  2615. charserv_config.char_ip = host2ip(w2);
  2616. if (charserv_config.char_ip) {
  2617. char ip_str[16];
  2618. safestrncpy(charserv_config.char_ip_str, w2, sizeof(charserv_config.char_ip_str));
  2619. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(charserv_config.char_ip, ip_str));
  2620. }
  2621. } else if (strcmpi(w1, "bind_ip") == 0) {
  2622. charserv_config.bind_ip = host2ip(w2);
  2623. if (charserv_config.bind_ip) {
  2624. char ip_str[16];
  2625. safestrncpy(charserv_config.bind_ip_str, w2, sizeof(charserv_config.bind_ip_str));
  2626. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(charserv_config.bind_ip, ip_str));
  2627. }
  2628. } else if (strcmpi(w1, "char_port") == 0) {
  2629. charserv_config.char_port = atoi(w2);
  2630. } else if (strcmpi(w1, "console") == 0) {
  2631. charserv_config.console = config_switch(w2);
  2632. }
  2633. }
  2634. if(strcmpi(w1,"timestamp_format") == 0) {
  2635. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  2636. } else if(strcmpi(w1,"console_silent")==0){
  2637. msg_silent = atoi(w2);
  2638. if( msg_silent ) /* only bother if its actually enabled */
  2639. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  2640. } else if (strcmpi(w1, "console_msg_log") == 0) {
  2641. console_msg_log = atoi(w2);
  2642. } else if (strcmpi(w1, "console_log_filepath") == 0) {
  2643. safestrncpy(console_log_filepath, w2, sizeof(console_log_filepath));
  2644. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  2645. stdout_with_ansisequence = config_switch(w2);
  2646. } else if (strcmpi(w1, "char_maintenance") == 0) {
  2647. charserv_config.char_maintenance = atoi(w2);
  2648. } else if (strcmpi(w1, "char_new") == 0) {
  2649. charserv_config.char_new = (bool)config_switch(w2);
  2650. } else if (strcmpi(w1, "char_new_display") == 0) {
  2651. charserv_config.char_new_display = atoi(w2);
  2652. } else if (strcmpi(w1, "max_connect_user") == 0) {
  2653. charserv_config.max_connect_user = atoi(w2);
  2654. if (charserv_config.max_connect_user < -1)
  2655. charserv_config.max_connect_user = -1;
  2656. } else if(strcmpi(w1, "gm_allow_group") == 0) {
  2657. charserv_config.gm_allow_group = atoi(w2);
  2658. } else if (strcmpi(w1, "autosave_time") == 0) {
  2659. charserv_config.autosave_interval = atoi(w2)*1000;
  2660. if (charserv_config.autosave_interval <= 0)
  2661. charserv_config.autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  2662. } else if (strcmpi(w1, "save_log") == 0) {
  2663. charserv_config.save_log = config_switch(w2);
  2664. #ifdef RENEWAL
  2665. } else if (strcmpi(w1, "start_point") == 0) {
  2666. #else
  2667. } else if (strcmpi(w1, "start_point_pre") == 0) {
  2668. #endif
  2669. char_config_split_startpoint(w1, w2, charserv_config.start_point, &charserv_config.start_point_count);
  2670. #if PACKETVER >= 20151001
  2671. } else if (strcmpi(w1, "start_point_doram") == 0) {
  2672. char_config_split_startpoint(w1, w2, charserv_config.start_point_doram, &charserv_config.start_point_count_doram);
  2673. #endif
  2674. } else if (strcmpi(w1, "start_zeny") == 0) {
  2675. charserv_config.start_zeny = atoi(w2);
  2676. if (charserv_config.start_zeny < 0)
  2677. charserv_config.start_zeny = 0;
  2678. #ifdef RENEWAL
  2679. } else if (strcmpi(w1, "start_items") == 0) {
  2680. #else
  2681. } else if (strcmpi(w1, "start_items_pre") == 0) {
  2682. #endif
  2683. char_config_split_startitem(w1, w2, charserv_config.start_items);
  2684. #if PACKETVER >= 20151001
  2685. } else if (strcmpi(w1, "start_items_doram") == 0) {
  2686. char_config_split_startitem(w1, w2, charserv_config.start_items_doram);
  2687. #endif
  2688. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  2689. charserv_config.log_char = config_switch(w2);
  2690. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  2691. safestrncpy(charserv_config.char_config.unknown_char_name, w2, sizeof(charserv_config.char_config.unknown_char_name));
  2692. charserv_config.char_config.unknown_char_name[NAME_LENGTH-1] = '\0';
  2693. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  2694. charserv_config.char_config.name_ignoring_case = (bool)config_switch(w2);
  2695. } else if (strcmpi(w1, "char_name_option") == 0) {
  2696. charserv_config.char_config.char_name_option = atoi(w2);
  2697. } else if (strcmpi(w1, "char_name_letters") == 0) {
  2698. safestrncpy(charserv_config.char_config.char_name_letters, w2, sizeof(charserv_config.char_config.char_name_letters));
  2699. } else if (strcmpi(w1, "char_name_min_length") == 0) {
  2700. charserv_config.char_config.char_name_min_length = cap_value(atoi(w2), 0, NAME_LENGTH - 1);
  2701. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  2702. charserv_config.char_config.char_del_level = atoi(w2);
  2703. } else if (strcmpi(w1, "char_del_delay") == 0) {
  2704. charserv_config.char_config.char_del_delay = atoi(w2);
  2705. } else if (strcmpi(w1, "char_del_option") == 0) {
  2706. charserv_config.char_config.char_del_option = atoi(w2);
  2707. } else if (strcmpi(w1, "char_del_restriction") == 0) {
  2708. charserv_config.char_config.char_del_restriction = atoi(w2);
  2709. } else if (strcmpi(w1, "char_rename_party") == 0) {
  2710. charserv_config.char_config.char_rename_party = (bool)config_switch(w2);
  2711. } else if (strcmpi(w1, "char_rename_guild") == 0) {
  2712. charserv_config.char_config.char_rename_guild = (bool)config_switch(w2);
  2713. } else if(strcmpi(w1,"db_path")==0) {
  2714. safestrncpy(schema_config.db_path, w2, sizeof(schema_config.db_path));
  2715. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  2716. fame_list_size_chemist = atoi(w2);
  2717. if (fame_list_size_chemist > MAX_FAME_LIST) {
  2718. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  2719. fame_list_size_chemist = MAX_FAME_LIST;
  2720. }
  2721. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  2722. fame_list_size_smith = atoi(w2);
  2723. if (fame_list_size_smith > MAX_FAME_LIST) {
  2724. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  2725. fame_list_size_smith = MAX_FAME_LIST;
  2726. }
  2727. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  2728. fame_list_size_taekwon = atoi(w2);
  2729. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  2730. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  2731. fame_list_size_taekwon = MAX_FAME_LIST;
  2732. }
  2733. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  2734. charserv_config.guild_exp_rate = atoi(w2);
  2735. } else if (strcmpi(w1, "pincode_enabled") == 0) {
  2736. #if PACKETVER_SUPPORTS_PINCODE
  2737. charserv_config.pincode_config.pincode_enabled = config_switch(w2);
  2738. } else if (strcmpi(w1, "pincode_changetime") == 0) {
  2739. charserv_config.pincode_config.pincode_changetime = atoi(w2)*60*60*24;
  2740. } else if (strcmpi(w1, "pincode_maxtry") == 0) {
  2741. charserv_config.pincode_config.pincode_maxtry = atoi(w2);
  2742. } else if (strcmpi(w1, "pincode_force") == 0) {
  2743. charserv_config.pincode_config.pincode_force = config_switch(w2);
  2744. } else if (strcmpi(w1, "pincode_allow_repeated") == 0) {
  2745. charserv_config.pincode_config.pincode_allow_repeated = config_switch(w2);
  2746. } else if (strcmpi(w1, "pincode_allow_sequential") == 0) {
  2747. charserv_config.pincode_config.pincode_allow_sequential = config_switch(w2);
  2748. #else
  2749. if( config_switch(w2) ) {
  2750. ShowWarning("pincode_enabled requires PACKETVER 20110309 or higher.\n");
  2751. }
  2752. #endif
  2753. } else if (strcmpi(w1, "char_move_enabled") == 0) {
  2754. charserv_config.charmove_config.char_move_enabled = config_switch(w2);
  2755. } else if (strcmpi(w1, "char_movetoused") == 0) {
  2756. charserv_config.charmove_config.char_movetoused = config_switch(w2);
  2757. } else if (strcmpi(w1, "char_moves_unlimited") == 0) {
  2758. charserv_config.charmove_config.char_moves_unlimited = config_switch(w2);
  2759. } else if (strcmpi(w1, "char_checkdb") == 0) {
  2760. charserv_config.char_check_db = config_switch(w2);
  2761. } else if (strcmpi(w1, "clan_remove_inactive_days") == 0) {
  2762. charserv_config.clan_remove_inactive_days = atoi(w2);
  2763. } else if (strcmpi(w1, "mail_return_days") == 0) {
  2764. charserv_config.mail_return_days = atoi(w2);
  2765. } else if (strcmpi(w1, "mail_delete_days") == 0) {
  2766. charserv_config.mail_delete_days = atoi(w2);
  2767. } else if (strcmpi(w1, "mail_return_empty") == 0) {
  2768. charserv_config.mail_return_empty = config_switch(w2);
  2769. } else if (strcmpi(w1, "allowed_job_flag") == 0) {
  2770. charserv_config.allowed_job_flag = atoi(w2);
  2771. } else if (strcmpi(w1, "clear_parties") == 0) {
  2772. charserv_config.clear_parties = config_switch(w2);
  2773. } else if (strcmpi(w1, "import") == 0) {
  2774. char_config_read(w2, normal);
  2775. }
  2776. }
  2777. fclose(fp);
  2778. ShowInfo("Done reading %s.\n", cfgName);
  2779. return true;
  2780. }
  2781. /**
  2782. * Checks for values out of range.
  2783. */
  2784. void char_config_adjust() {
  2785. #if PACKETVER < 20100803
  2786. if (charserv_config.char_config.char_del_option&CHAR_DEL_BIRTHDATE) {
  2787. ShowWarning("conf/char_athena.conf:char_del_option birthdate is enabled but it requires PACKETVER 2010-08-03 or newer, defaulting to email...\n");
  2788. charserv_config.char_config.char_del_option &= ~CHAR_DEL_BIRTHDATE;
  2789. }
  2790. #endif
  2791. }
  2792. /*
  2793. * Message conf function
  2794. */
  2795. int32 char_msg_config_read(const char *cfgName){
  2796. return _msg_config_read(cfgName,CHAR_MAX_MSG,msg_table);
  2797. }
  2798. const char* char_msg_txt(int32 msg_number){
  2799. return _msg_txt(msg_number,CHAR_MAX_MSG,msg_table);
  2800. }
  2801. void char_do_final_msg(void){
  2802. _do_final_msg(CHAR_MAX_MSG,msg_table);
  2803. }
  2804. void CharacterServer::finalize(){
  2805. ShowStatus("Terminating...\n");
  2806. char_set_all_offline(-1);
  2807. char_set_all_offline_sql();
  2808. inter_final();
  2809. flush_fifos();
  2810. do_final_msg();
  2811. do_final_chmapif();
  2812. do_final_chlogif();
  2813. char_get_chardb().clear();
  2814. char_get_onlinedb().clear();
  2815. char_get_authdb().clear();
  2816. if( char_fd != -1 )
  2817. {
  2818. do_close(char_fd);
  2819. char_fd = -1;
  2820. }
  2821. Sql_Free(sql_handle);
  2822. ShowStatus("Finished.\n");
  2823. }
  2824. /// Called when a terminate signal is received.
  2825. void CharacterServer::handle_shutdown(){
  2826. ShowStatus("Shutting down...\n");
  2827. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  2828. for( int32 id = 0; id < ARRAYLENGTH(map_server); ++id )
  2829. chmapif_server_reset(id);
  2830. flush_fifos();
  2831. }
  2832. bool CharacterServer::initialize( int32 argc, char *argv[] ){
  2833. // Init default value
  2834. safestrncpy(console_log_filepath, "./log/char-msg_log.log", sizeof(console_log_filepath));
  2835. cli_get_options(argc,argv);
  2836. char_set_defaults();
  2837. char_config_read(CHAR_CONF_NAME, true);
  2838. char_config_adjust();
  2839. char_lan_config_read(LAN_CONF_NAME);
  2840. char_set_default_sql();
  2841. char_sql_config_read(INTER_CONF_NAME);
  2842. msg_config_read(MSG_CONF_NAME_EN);
  2843. #if !defined(BUILDBOT)
  2844. if (strcmp(charserv_config.userid, "s1")==0 && strcmp(charserv_config.passwd, "p1")==0) {
  2845. ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  2846. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  2847. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  2848. }
  2849. #endif
  2850. inter_init_sql(INTER_CONF_NAME); // inter server configuration
  2851. char_mmo_sql_init();
  2852. char_read_fame_list(); //Read fame lists.
  2853. if ((naddr_ != 0) && (!(charserv_config.login_ip) || !(charserv_config.char_ip) ))
  2854. {
  2855. char ip_str[16];
  2856. ip2str(addr_[0], ip_str);
  2857. if (naddr_ > 1)
  2858. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  2859. else
  2860. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  2861. if (!(charserv_config.login_ip) ) {
  2862. safestrncpy(charserv_config.login_ip_str, ip_str, sizeof(charserv_config.login_ip_str));
  2863. charserv_config.login_ip = str2ip(charserv_config.login_ip_str);
  2864. }
  2865. if (!(charserv_config.char_ip)) {
  2866. safestrncpy(charserv_config.char_ip_str, ip_str, sizeof(charserv_config.char_ip_str));
  2867. charserv_config.char_ip = str2ip(charserv_config.char_ip_str);
  2868. }
  2869. }
  2870. do_init_chlogif();
  2871. do_init_chmapif();
  2872. // periodically update the overall user count on all mapservers + login server
  2873. add_timer_func_list(chlogif_broadcast_user_count, "broadcast_user_count");
  2874. add_timer_interval(gettick() + 1000, chlogif_broadcast_user_count, 0, 0, 5 * 1000);
  2875. // Timer to clear (online_char_db)
  2876. add_timer_func_list(char_chardb_waiting_disconnect, "chardb_waiting_disconnect");
  2877. // Online Data timers (checking if char still connected)
  2878. add_timer_func_list(char_online_data_cleanup, "online_data_cleanup");
  2879. add_timer_interval(gettick() + 1000, char_online_data_cleanup, 0, 0, 600 * 1000);
  2880. // periodically remove players that have not logged in for a long time from clans
  2881. add_timer_func_list(char_clan_member_cleanup, "clan_member_cleanup");
  2882. add_timer_interval(gettick() + 1000, char_clan_member_cleanup, 0, 0, 60 * 60 * 1000); // every 60 minutes
  2883. // periodically check if mails need to be returned to their sender
  2884. add_timer_func_list(mail_return_timer, "mail_return_timer");
  2885. add_timer_interval(gettick() + 1000, mail_return_timer, 0, 0, 1 * 60 * 1000); // every minute
  2886. // periodically check if mails need to be deleted completely
  2887. add_timer_func_list(mail_delete_timer, "mail_delete_timer");
  2888. add_timer_interval(gettick() + 1000, mail_delete_timer, 0, 0, 1 * 60 * 1000); // every minute
  2889. //check db tables
  2890. if(charserv_config.char_check_db && char_checkdb() == 0){
  2891. ShowFatalError("char : A tables is missing in sql-server, please fix it, see (sql-files main.sql for structure) \n");
  2892. return false;
  2893. }
  2894. //Cleaning the tables for nullptr entrys @ startup [Sirius]
  2895. //Chardb clean
  2896. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", schema_config.char_db) )
  2897. Sql_ShowDebug(sql_handle);
  2898. //guilddb clean
  2899. 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) )
  2900. Sql_ShowDebug(sql_handle);
  2901. //guildmemberdb clean
  2902. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `char_id` = '0'", schema_config.guild_member_db) )
  2903. Sql_ShowDebug(sql_handle);
  2904. set_defaultparse(chclif_parse);
  2905. if( (char_fd = make_listen_bind(charserv_config.bind_ip,charserv_config.char_port)) == -1 ) {
  2906. ShowFatalError("Failed to bind to port '" CL_WHITE "%d" CL_RESET "'\n",charserv_config.char_port);
  2907. return false;
  2908. }
  2909. do_init_chcnslif();
  2910. ShowStatus("The char-server is " CL_GREEN "ready" CL_RESET " (Server is listening on the port %d).\n\n", charserv_config.char_port);
  2911. return true;
  2912. }
  2913. int32 main( int32 argc, char *argv[] ){
  2914. return main_core<CharacterServer>( argc, argv );
  2915. }