char.cpp 137 KB

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