// Copyright (c) rAthena Dev Teams - Licensed under GNU GPL // For more information, see LICENCE in the main folder //#define DEBUG_DISP //#define DEBUG_DISASM //#define DEBUG_RUN //#define DEBUG_HASH //#define DEBUG_DUMP_STACK #include "script.hpp" #include #include #include #include // atoi, strtol, strtoll, exit #ifdef PCRE_SUPPORT #include "../../3rdparty/pcre/include/pcre.h" // preg_match #endif #include "../common/cbasetypes.hpp" #include "../common/ers.hpp" // ers_destroy #include "../common/malloc.hpp" #include "../common/md5calc.hpp" #include "../common/nullpo.hpp" #include "../common/random.hpp" #include "../common/showmsg.hpp" #include "../common/socket.hpp" #include "../common/strlib.hpp" #include "../common/timer.hpp" #include "../common/utils.hpp" #include "achievement.hpp" #include "atcommand.hpp" #include "battle.hpp" #include "battleground.hpp" #include "channel.hpp" #include "chat.hpp" #include "chrif.hpp" #include "clan.hpp" #include "clif.hpp" #include "date.hpp" // date type enum, date_get() #include "elemental.hpp" #include "guild.hpp" #include "homunculus.hpp" #include "instance.hpp" #include "intif.hpp" #include "itemdb.hpp" #include "log.hpp" #include "mail.hpp" #include "map.hpp" #include "mapreg.hpp" #include "mercenary.hpp" #include "mob.hpp" #include "npc.hpp" #include "party.hpp" #include "path.hpp" #include "pc.hpp" #include "pc_groups.hpp" #include "pet.hpp" #include "quest.hpp" #include "storage.hpp" struct eri *array_ers; DBMap *st_db; unsigned int active_scripts; unsigned int next_id; struct eri *st_ers; struct eri *stack_ers; static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ); /** * Get `sd` from a account id in `loc` param instead of attached rid * @param st Script * @param loc Location to look account id in script parameter * @param sd Variable that will be assigned * @return True if `sd` is assigned, false otherwise **/ static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { if (script_hasdata(st, loc)) { int id_ = script_getnum(st, loc); if (!(*sd = map_id2sd(id_))){ ShowError("%s: Player with account id '%d' is not found.\n", func, id_); return false; }else{ return true; } } else return script_rid2sd_(st,sd,func); } /** * Get `sd` from a char id in `loc` param instead of attached rid * @param st Script * @param loc Location to look char id in script parameter * @param sd Variable that will be assigned * @return True if `sd` is assigned, false otherwise **/ static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { if (script_hasdata(st, loc)) { int id_ = script_getnum(st, loc); if (!(*sd = map_charid2sd(id_))){ ShowError("%s: Player with char id '%d' is not found.\n", func, id_); return false; }else{ return true; } } else return script_rid2sd_(st,sd,func); } /** * Get `sd` from a nick in `loc` param instead of attached rid * @param st Script * @param loc Location to look nick in script parameter * @param sd Variable that will be assigned * @return True if `sd` is assigned, false otherwise **/ static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { if (script_hasdata(st, loc)) { const char *name_ = script_getstr(st, loc); if (!(*sd = map_nick2sd(name_,false))){ ShowError("%s: Player with nick '%s' is not found.\n", func, name_); return false; }else{ return true; } } else return script_rid2sd_(st,sd,func); } /** * Get `sd` from a mapid in `loc` param instead of attached rid * @param st Script * @param loc Location to look mapid in script parameter * @param sd Variable that will be assigned * @return True if `sd` is assigned, false otherwise **/ static bool script_mapid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { if (script_hasdata(st, loc)) { int id_ = script_getnum(st, loc); if (!(*sd = map_id2sd(id_))){ ShowError("%s: Player with map id '%d' is not found.\n", func, id_); return false; }else{ return true; } } else return script_rid2sd_(st,sd,func); } /** * Get `bl` from an ID in `loc` param, if ID is 0 will return the `bl` of the script's activator. * @param st Script * @param loc Location to look for ID in script parameter * @param bl Variable that will be assigned * @return True if `bl` is assigned, false otherwise **/ static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list **bl, const char *func) { int unit_id; if ( !script_hasdata(st, loc) || ( unit_id = script_getnum(st, loc) ) == 0) unit_id = st->rid; *bl = map_id2bl(unit_id); if ( *bl ) return true; else { ShowError("%s: Unit with ID '%d' is not found.\n", func, unit_id); return false; } } #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__) #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__) #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__) #define script_mapid2sd(loc,sd) script_mapid2sd_(st,(loc),&(sd),__FUNCTION__) #define script_rid2sd(sd) script_rid2sd_(st,&(sd),__FUNCTION__) #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__) /// temporary buffer for passing around compiled bytecode /// @see add_scriptb, set_label, parse_script static unsigned char* script_buf = NULL; static int script_pos = 0, script_size = 0; static inline int GETVALUE(const unsigned char* buf, int i) { return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0)); } static inline void SETVALUE(unsigned char* buf, int i, int n) { buf[i] = GetByte(n, 0); buf[i+1] = GetByte(n, 1); buf[i+2] = GetByte(n, 2); } // String buffer structures. // str_data stores string information static struct str_data_struct { enum c_op type; int str; int backpatch; int label; int (*func)(struct script_state *st); int val; int next; bool deprecated; } *str_data = NULL; static int str_data_size = 0; // size of the data static int str_num = LABEL_START; // next id to be assigned // str_buf holds the strings themselves static char *str_buf; static int str_size = 0; // size of the buffer static int str_pos = 0; // next position to be assigned // Using a prime number for SCRIPT_HASH_SIZE should give better distributions #define SCRIPT_HASH_SIZE 1021 int str_hash[SCRIPT_HASH_SIZE]; // Specifies which string hashing method to use //#define SCRIPT_HASH_DJB2 //#define SCRIPT_HASH_SDBM #define SCRIPT_HASH_ELF static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code* static int parse_options = 0; DBMap* script_get_label_db(void) { return scriptlabel_db; } DBMap* script_get_userfunc_db(void) { return userfunc_db; } // important buildin function references for usage in scripts static int buildin_set_ref = 0; static int buildin_callsub_ref = 0; static int buildin_callfunc_ref = 0; static int buildin_getelementofarray_ref = 0; // Caches compiled autoscript item code. // Note: This is not cleared when reloading itemdb. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode struct Script_Config script_config = { 1, // warn_func_mismatch_argtypes 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount 0, INT_MAX, // input_min_value/input_max_value // NOTE: None of these event labels should be longer than characters // PC related "OnPCDieEvent", //die_event_name "OnPCKillEvent", //kill_pc_event_name "OnNPCKillEvent", //kill_mob_event_name "OnPCLoginEvent", //login_event_name "OnPCLogoutEvent", //logout_event_name "OnPCLoadMapEvent", //loadmap_event_name "OnPCBaseLvUpEvent", //baselvup_event_name "OnPCJobLvUpEvent", //joblvup_event_name "OnPCStatCalcEvent", //stat_calc_event_name // NPC related "OnTouch_", //ontouch_event_name (runs on first visible char to enter area, picks another char if the first char leaves) "OnTouch", //ontouch2_event_name (run whenever a char walks into the OnTouch area) "OnTouchNPC", //ontouchnpc_event_name (run whenever a monster walks into the OnTouch area) "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC) "OnCommand", //oncommand_event_name (is executed by script command cmdothernpc) "OnBuyItem", //onbuy_event_name (is executed when items are bought) "OnSellItem", //onsell_event_name (is executed when items are sold) // Init related "OnInit", //init_event_name (is executed on all npcs when all npcs were loaded) "OnInterIfInit", //inter_init_event_name (is executed on inter server connection) "OnInterIfInitOnce", //inter_init_once_event_name (is only executed on the first inter server connection) // Guild related "OnGuildBreak", //guild_break_event_name (is executed on all castles of the guild that is broken) "OnAgitStart", //agit_start_event_name (is executed when WoE FE is started) "OnAgitInit", //agit_init_event_name (is executed after all castle owning guilds have been loaded) "OnAgitEnd", //agit_end_event_name (is executed when WoE FE has ended) "OnAgitStart2", //agit_start2_event_name (is executed when WoE SE is started) "OnAgitInit2", //agit_init2_event_name (is executed after all castle owning guilds have been loaded) "OnAgitEnd2", //agit_end2_event_name (is executed when WoE SE has ended) "OnAgitStart3", //agit_start3_event_name (is executed when WoE TE is started) "OnAgitInit3", //agit_init3_event_name (is executed after all castle owning guilds have been loaded) "OnAgitEnd3", //agit_end3_event_name (is executed when WoE TE has ended) // Timer related "OnTimer", //timer_event_name (is executed by a timer at the specific second) "OnTimerQuit", //timer_quit_event_name (is executed when a timer is aborted) "OnMinute", //timer_minute_event_name (is executed by a timer at the specific minute) "OnHour", //timer_hour_event_name (is executed by a timer at the specific hour) "OnClock", //timer_clock_event_name (is executed by a timer at the specific hour and minute) "OnDay", //timer_day_event_name (is executed by a timer at the specific month and day) "OnSun", //timer_sunday_event_name (is executed by a timer on sunday at the specific hour and minute) "OnMon", //timer_monday_event_name (is executed by a timer on monday at the specific hour and minute) "OnTue", //timer_tuesday_event_name (is executed by a timer on tuesday at the specific hour and minute) "OnWed", //timer_wednesday_event_name (is executed by a timer on wednesday at the specific hour and minute) "OnThu", //timer_thursday_event_name (is executed by a timer on thursday at the specific hour and minute) "OnFri", //timer_friday_event_name (is executed by a timer on friday at the specific hour and minute) "OnSat", //timer_saturday_event_name (is executed by a timer on saturday at the specific hour and minute) // Instance related "OnInstanceInit", //instance_init_event_name (is executed right after instance creation) "OnInstanceDestroy", //instance_destroy_event_name (is executed right before instance destruction) }; static jmp_buf error_jump; static char* error_msg; static const char* error_pos; static int error_report; // if the error should produce output // Used by disp_warning_message static const char* parser_current_src; static const char* parser_current_file; static int parser_current_line; // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc ) // [Eoe / jA 1080, 1081, 1094, 1164] enum curly_type { TYPE_NULL = 0, TYPE_IF, TYPE_SWITCH, TYPE_WHILE, TYPE_FOR, TYPE_DO, TYPE_USERFUNC, TYPE_ARGLIST // function argument list }; enum e_arglist { ARGLIST_UNDEFINED = 0, ARGLIST_NO_PAREN = 1, ARGLIST_PAREN = 2, }; static struct { struct { enum curly_type type; int index; int count; int flag; struct linkdb_node *case_label; } curly[256]; // Information right parenthesis int curly_count; // The number of right brackets int index; // Number of the syntax used in the script } syntax; const char* parse_curly_close(const char* p); const char* parse_syntax_close(const char* p); const char* parse_syntax_close_sub(const char* p,int* flag); const char* parse_syntax(const char* p); static int parse_syntax_for_flag = 0; extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus] extern unsigned int current_equip_combo_pos; int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex] int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0; int potion_target = 0; unsigned int *generic_ui_array = NULL; unsigned int generic_ui_array_size = 0; c_op get_com(unsigned char *script,int *pos); int get_num(unsigned char *script,int *pos); typedef struct script_function { int (*func)(struct script_state *st); const char *name; const char *arg; const char *deprecated; } script_function; extern script_function buildin_func[]; static struct linkdb_node *sleep_db; // int oid -> struct script_state * /*========================================== * (Only those needed) local declaration prototype *------------------------------------------*/ const char* parse_subexpr(const char* p,int limit); int run_func(struct script_state *st); unsigned short script_instancegetid(struct script_state *st); enum { MF_NOMEMO, //0 MF_NOTELEPORT, MF_NOSAVE, MF_NOBRANCH, MF_NOPENALTY, MF_NOZENYPENALTY, MF_PVP, MF_PVP_NOPARTY, MF_PVP_NOGUILD, MF_GVG, MF_GVG_NOPARTY, //10 MF_NOTRADE, MF_NOSKILL, MF_NOWARP, MF_PARTYLOCK, MF_NOICEWALL, MF_SNOW, MF_FOG, MF_SAKURA, MF_LEAVES, /** * No longer available, keeping here just in case it's back someday. [Ind] **/ //MF_RAIN, //20 // 21 free MF_NOGO = 22, MF_CLOUDS, MF_CLOUDS2, MF_FIREWORKS, MF_GVG_CASTLE, MF_GVG_DUNGEON, MF_NIGHTENABLED, MF_NOBASEEXP, MF_NOJOBEXP, //30 MF_NOMOBLOOT, MF_NOMVPLOOT, MF_NORETURN, MF_NOWARPTO, MF_NIGHTMAREDROP, MF_RESTRICTED, MF_NOCOMMAND, MF_NODROP, MF_JEXP, MF_BEXP, //40 MF_NOVENDING, MF_LOADEVENT, MF_NOCHAT, MF_NOEXPPENALTY, MF_GUILDLOCK, MF_TOWN, MF_AUTOTRADE, MF_ALLOWKS, MF_MONSTER_NOTELEPORT, MF_PVP_NOCALCRANK, //50 MF_BATTLEGROUND, MF_RESET, MF_CHANNELAUTOJOIN, MF_NOUSECART, MF_NOITEMCONSUMPTION, MF_SUMSTARTMIRACLE, MF_NOMINEEFFECT, MF_NOLOCKON, MF_NOTOMB, MF_SKILL_DAMAGE, //60 MF_NOCOSTUME, MF_GVG_TE_CASTLE, MF_GVG_TE, MF_HIDEMOBHPBAR, }; const char* script_op2name(int op) { #define RETURN_OP_NAME(type) case type: return #type switch( op ) { RETURN_OP_NAME(C_NOP); RETURN_OP_NAME(C_POS); RETURN_OP_NAME(C_INT); RETURN_OP_NAME(C_PARAM); RETURN_OP_NAME(C_FUNC); RETURN_OP_NAME(C_STR); RETURN_OP_NAME(C_CONSTSTR); RETURN_OP_NAME(C_ARG); RETURN_OP_NAME(C_NAME); RETURN_OP_NAME(C_EOL); RETURN_OP_NAME(C_RETINFO); RETURN_OP_NAME(C_USERFUNC); RETURN_OP_NAME(C_USERFUNC_POS); RETURN_OP_NAME(C_REF); // operators RETURN_OP_NAME(C_OP3); RETURN_OP_NAME(C_LOR); RETURN_OP_NAME(C_LAND); RETURN_OP_NAME(C_LE); RETURN_OP_NAME(C_LT); RETURN_OP_NAME(C_GE); RETURN_OP_NAME(C_GT); RETURN_OP_NAME(C_EQ); RETURN_OP_NAME(C_NE); RETURN_OP_NAME(C_XOR); RETURN_OP_NAME(C_OR); RETURN_OP_NAME(C_AND); RETURN_OP_NAME(C_ADD); RETURN_OP_NAME(C_SUB); RETURN_OP_NAME(C_MUL); RETURN_OP_NAME(C_DIV); RETURN_OP_NAME(C_MOD); RETURN_OP_NAME(C_NEG); RETURN_OP_NAME(C_LNOT); RETURN_OP_NAME(C_NOT); RETURN_OP_NAME(C_R_SHIFT); RETURN_OP_NAME(C_L_SHIFT); RETURN_OP_NAME(C_ADD_POST); RETURN_OP_NAME(C_SUB_POST); RETURN_OP_NAME(C_ADD_PRE); RETURN_OP_NAME(C_SUB_PRE); default: ShowDebug("script_op2name: unexpected op=%d\n", op); return "???"; } #undef RETURN_OP_NAME } #ifdef DEBUG_DUMP_STACK static void script_dump_stack(struct script_state* st) { int i; ShowMessage("\tstart = %d\n", st->start); ShowMessage("\tend = %d\n", st->end); ShowMessage("\tdefsp = %d\n", st->stack->defsp); ShowMessage("\tsp = %d\n", st->stack->sp); for( i = 0; i < st->stack->sp; ++i ) { struct script_data* data = &st->stack->stack_data[i]; ShowMessage("\t[%d] %s", i, script_op2name(data->type)); switch( data->type ) { case C_INT: case C_POS: ShowMessage(" %d\n", data->u.num); break; case C_STR: case C_CONSTSTR: ShowMessage(" \"%s\"\n", data->u.str); break; case C_NAME: ShowMessage(" \"%s\" (id=%d ref=%p subtype=%s)\n", reference_getname(data), data->u.num, data->ref, script_op2name(str_data[data->u.num].type)); break; case C_RETINFO: { struct script_retinfo* ri = data->u.ri; ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp); } break; default: ShowMessage("\n"); break; } } } #endif /// Reports on the console the src of a script error. static void script_reportsrc(struct script_state *st) { struct block_list* bl; if( st->oid == 0 ) return; //Can't report source. bl = map_id2bl(st->oid); if( bl == NULL ) return; switch( bl->type ) { case BL_NPC: if( bl->m >= 0 ) ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y); else ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name); break; default: if( bl->m >= 0 ) ShowDebug("Source (Non-NPC type %d): name %s at %s (%d,%d)\n", bl->type, status_get_name(bl), map[bl->m].name, bl->x, bl->y); else ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl)); break; } } /// Reports on the console information about the script data. static void script_reportdata(struct script_data* data) { if( data == NULL ) return; switch( data->type ) { case C_NOP:// no value ShowDebug("Data: nothing (nil)\n"); break; case C_INT:// number ShowDebug("Data: number value=%" PRId64 "\n", data->u.num); break; case C_STR: case C_CONSTSTR:// string if( data->u.str ) { ShowDebug("Data: string value=\"%s\"\n", data->u.str); } else { ShowDebug("Data: string value=NULL\n"); } break; case C_NAME:// reference if( reference_tovariable(data) ) {// variable const char* name = reference_getname(data); ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data)); } else if( reference_toconstant(data) ) {// constant ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data)); } else if( reference_toparam(data) ) {// param ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data)); } else {// ??? ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type)); ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type)); } break; case C_POS:// label ShowDebug("Data: label pos=%" PRId64 "\n", data->u.num); break; default: ShowDebug("Data: %s\n", script_op2name(data->type)); break; } } /// Reports on the console information about the current built-in function. static void script_reportfunc(struct script_state* st) { int params, id; struct script_data* data; if( !script_hasdata(st,0) ) {// no stack return; } data = script_getdata(st,0); if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC ) {// script currently not executing a built-in function or corrupt stack return; } id = reference_getid(data); params = script_lastdata(st)-1; if( params > 0 ) { int i; ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s"); for( i = 2; i <= script_lastdata(st); i++ ) { script_reportdata(script_getdata(st,i)); } } else { ShowDebug("Function: %s (no parameters)\n", get_str(id)); } } /*========================================== * Output error message *------------------------------------------*/ static void disp_error_message2(const char *mes,const char *pos,int report) { error_msg = aStrdup(mes); error_pos = pos; error_report = report; longjmp( error_jump, 1 ); } #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1) static void disp_warning_message(const char *mes, const char *pos) { script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos); } /// Checks event parameter validity static void check_event(struct script_state *st, const char *evt) { if( evt && evt[0] && !stristr(evt, "::On") ) { ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt); script_reportsrc(st); } } /*========================================== * Hashes the input string *------------------------------------------*/ static unsigned int calc_hash(const char* p) { unsigned int h; #if defined(SCRIPT_HASH_DJB2) h = 5381; while( *p ) // hash*33 + c h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++)); #elif defined(SCRIPT_HASH_SDBM) h = 0; while( *p ) // hash*65599 + c h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++)); #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash h = 0; while( *p ){ unsigned int g; h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++)); g = h & 0xF0000000; if( g ) { h ^= g >> 24; h &= ~g; } } #else // athena hash h = 0; while( *p ) h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++); #endif return h % SCRIPT_HASH_SIZE; } bool script_check_RegistryVariableLength(int pType, const char *val, size_t* vlen) { size_t len = strlen(val); if (vlen) *vlen = len; switch (pType) { case 0: return (len < 33); // key check case 1: return (len < 255); // value check default: return false; } } /*========================================== * str_data manipulation functions *------------------------------------------*/ /// Looks up string using the provided id. const char* get_str(int id) { Assert( id >= LABEL_START && id < str_size ); return str_buf+str_data[id].str; } /// Returns the uid of the string, or -1. static int search_str(const char* p) { int i; for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next ) if( strcasecmp(get_str(i),p) == 0 ) return i; return -1; } /// Stores a copy of the string and returns its id. /// If an identical string is already present, returns its id instead. int add_str(const char* p) { int h; int len; h = calc_hash(p); if( str_hash[h] == 0 ) {// empty bucket, add new node here str_hash[h] = str_num; } else {// scan for end of list, or occurence of identical string int i; for( i = str_hash[h]; ; i = str_data[i].next ) { if( strcasecmp(get_str(i),p) == 0 ) return i; // string already in list if( str_data[i].next == 0 ) break; // reached the end } // append node to end of list str_data[i].next = str_num; } // grow list if neccessary if( str_num >= str_data_size ) { str_data_size += 128; RECREATE(str_data,struct str_data_struct,str_data_size); memset(str_data + (str_data_size - 128), '\0', 128); } len=(int)strlen(p); // grow string buffer if neccessary while( str_pos+len+1 >= str_size ) { str_size += 256; RECREATE(str_buf,char,str_size); memset(str_buf + (str_size - 256), '\0', 256); } safestrncpy(str_buf+str_pos, p, len+1); str_data[str_num].type = C_NOP; str_data[str_num].str = str_pos; str_data[str_num].next = 0; str_data[str_num].func = NULL; str_data[str_num].backpatch = -1; str_data[str_num].label = -1; str_pos += len+1; return str_num++; } /// Appends 1 byte to the script buffer. static void add_scriptb(int a) { if( script_pos+1 >= script_size ) { script_size += SCRIPT_BLOCK_SIZE; RECREATE(script_buf,unsigned char,script_size); } script_buf[script_pos++] = (uint8)(a); } /// Appends a c_op value to the script buffer. /// The value is variable-length encoded into 8-bit blocks. /// The encoding scheme is ( 01?????? )* 00??????, LSB first. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries). static void add_scriptc(int a) { while( a >= 0x40 ) { add_scriptb((a&0x3f)|0x40); a = (a - 0x40) >> 6; } add_scriptb(a); } /// Appends an integer value to the script buffer. /// The value is variable-length encoded into 8-bit blocks. /// The encoding scheme is ( 11?????? )* 10??????, LSB first. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries). static void add_scripti(int a) { while( a >= 0x40 ) { add_scriptb((a&0x3f)|0xc0); a = (a - 0x40) >> 6; } add_scriptb(a|0x80); } /// Appends a str_data object (label/function/variable/integer) to the script buffer. /// /// @param l The id of the str_data entry // Maximum up to 16M static void add_scriptl(int l) { int backpatch = str_data[l].backpatch; switch(str_data[l].type){ case C_POS: case C_USERFUNC_POS: add_scriptc(C_POS); add_scriptb(str_data[l].label); add_scriptb(str_data[l].label>>8); add_scriptb(str_data[l].label>>16); break; case C_NOP: case C_USERFUNC: // Embedded data backpatch there is a possibility of label add_scriptc(C_NAME); str_data[l].backpatch = script_pos; add_scriptb(backpatch); add_scriptb(backpatch>>8); add_scriptb(backpatch>>16); break; case C_INT: add_scripti(abs(str_data[l].val)); if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce) add_scriptc(C_NEG); break; default: // assume C_NAME add_scriptc(C_NAME); add_scriptb(l); add_scriptb(l>>8); add_scriptb(l>>16); break; } } /*========================================== * Resolve the label *------------------------------------------*/ void set_label(int l,int pos, const char* script_pos_cur) { int i; if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC) { //Prevent overwriting constants values, parameters and built-in functions [Skotlex] disp_error_message("set_label: invalid label name",script_pos_cur); return; } if(str_data[l].label!=-1){ disp_error_message("set_label: dup label ",script_pos_cur); return; } str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS); str_data[l].label=pos; for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){ int next=GETVALUE(script_buf,i); script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS); SETVALUE(script_buf,i,pos); i=next; } } /// Skips spaces and/or comments. const char* skip_space(const char* p) { if( p == NULL ) return NULL; for(;;) { while( ISSPACE(*p) ) ++p; if( *p == '/' && p[1] == '/' ) {// line comment while(*p && *p!='\n') ++p; } else if( *p == '/' && p[1] == '*' ) {// block comment p += 2; for(;;) { if( *p == '\0' ) { disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected " CL_BOLD "*/" CL_NORM, p); return p; } if( *p == '*' && p[1] == '/' ) {// end of block comment p += 2; break; } ++p; } } else break; } return p; } /// Skips a word. /// A word consists of undercores and/or alphanumeric characters, /// and valid variable prefixes/postfixes. static const char* skip_word(const char* p) { // prefix switch( *p ) { case '@':// temporary char variable ++p; break; case '#':// account variable p += ( p[1] == '#' ? 2 : 1 ); break; case '\'':// instance variable ++p; break; case '.':// npc variable p += ( p[1] == '@' ? 2 : 1 ); break; case '$':// global variable p += ( p[1] == '@' ? 2 : 1 ); break; } while( ISALNUM(*p) || *p == '_' ) ++p; // postfix if( *p == '$' )// string p++; return p; } /// Adds a word to str_data. /// @see skip_word /// @see add_str static int add_word(const char* p) { char* word; int len; int i; // Check for a word len = skip_word(p) - p; if( len == 0 ) disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p); // Duplicate the word word = (char*)aMalloc(len+1); memcpy(word, p, len); word[len] = 0; // add the word i = add_str(word); aFree(word); return i; } /// Parses a function call. /// The argument list can have parenthesis or not. /// The number of arguments is checked. static const char* parse_callfunc(const char* p, int require_paren, int is_custom) { const char* p2; const char* arg=NULL; int func; func = add_word(p); if( str_data[func].type == C_FUNC ){ // buildin function add_scriptl(func); add_scriptc(C_ARG); arg = buildin_func[str_data[func].val].arg; #if defined(SCRIPT_COMMAND_DEPRECATION) if( str_data[func].deprecated ){ ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) ); ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated ); } #endif } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){ // script defined function add_scriptl(buildin_callsub_ref); add_scriptc(C_ARG); add_scriptl(func); arg = buildin_func[str_data[buildin_callsub_ref].val].arg; if( *arg == 0 ) disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p); if( *arg != '*' ) ++arg; // count func as argument } else { const char* name = get_str(func); if( !is_custom && strdb_get(userfunc_db, name) == NULL ) { disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p); } else {; add_scriptl(buildin_callfunc_ref); add_scriptc(C_ARG); add_scriptc(C_STR); while( *name ) add_scriptb(*name ++); add_scriptb(0); arg = buildin_func[str_data[buildin_callfunc_ref].val].arg; if( *arg != '*' ) ++ arg; } } p = skip_word(p); p = skip_space(p); syntax.curly[syntax.curly_count].type = TYPE_ARGLIST; syntax.curly[syntax.curly_count].count = 0; if( *p == ';' ) {// ';' syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' ) {// '(' ')' syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; p = p2; /* } else if( 0 && require_paren && *p != '(' ) {// syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; */ } else {// if( require_paren ){ if( *p != '(' ) disp_error_message("need '('",p); ++p; // skip '(' syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; } else if( *p == '(' ){ syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED; } else { syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; } ++syntax.curly_count; while( *arg ) { p2=parse_subexpr(p,-1); if( p == p2 ) break; // not an argument if( *arg != '*' ) ++arg; // next argument p=skip_space(p2); if( *arg == 0 || *p != ',' ) break; // no more arguments ++p; // skip comma } --syntax.curly_count; } if( *arg && *arg != '?' && *arg != '*' ) disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum); if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST ) disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p); if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){ if( *p != ')' ) disp_error_message("parse_callfunc: expected ')' to close argument list",p); ++p; } add_scriptc(C_FUNC); return p; } /// Processes end of logical script line. /// @param first When true, only fix up scheduling data is initialized /// @param p Script position for error reporting in set_label static void parse_nextline(bool first, const char* p) { if( !first ) { add_scriptc(C_EOL); // mark end of line for stack cleanup set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels } // initialize data for new '-' label fix up scheduling str_data[LABEL_NEXTLINE].type = C_NOP; str_data[LABEL_NEXTLINE].backpatch = -1; str_data[LABEL_NEXTLINE].label = -1; } /** * Pushes a variable into stack, processing its array index if needed. * @see parse_variable */ void parse_variable_sub_push(int word, const char *p2) { if( p2 ) { // Process the variable index const char *p3 = NULL; // Push the getelementofarray method into the stack add_scriptl(buildin_getelementofarray_ref); add_scriptc(C_ARG); add_scriptl(word); // Process the sub-expression for this assignment p3 = parse_subexpr(p2 + 1, 1); p3 = skip_space(p3); if( *p3 != ']' ) // Closing parenthesis is required for this script disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3); // Push the closing function stack operator onto the stack add_scriptc(C_FUNC); p3++; } else // No array index, simply push the variable or value onto the stack add_scriptl(word); } /// Parse a variable assignment using the direct equals operator /// @param p script position where the function should run from /// @return NULL if not a variable assignment, the new position otherwise const char* parse_variable(const char* p) { int word; c_op type = C_NOP; const char *p2 = NULL; const char *var = p; if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++ || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre -- var = p = skip_space(&p[2]); // skip the variable where applicable p = skip_word(p); p = skip_space(p); if( p == NULL ) {// end of the line or invalid buffer return NULL; } if( *p == '[' ) {// array variable so process the array as appropriate int i,j; for( p2 = p, i = 0, j = 1; p; ++ i ) { if( *p ++ == ']' && --(j) == 0 ) break; if( *p == '[' ) ++ j; } if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining disp_error_message("Missing right expression or closing bracket for variable.", p); } } if( type == C_NOP && !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // = || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // += || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -= || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^= || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |= || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &= || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *= || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /= || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %= || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~= || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++ || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post -- || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<= || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>= ) ) {// failed to find a matching operator combination so invalid return NULL; } switch( type ) { case C_ADD_PRE: // pre ++ case C_SUB_PRE: // pre -- // Nothing more to skip break; case C_EQ: {// incremental modifier p = skip_space( &p[1] ); } break; case C_L_SHIFT: case C_R_SHIFT: {// left or right shift modifier p = skip_space( &p[3] ); } break; default: {// normal incremental command p = skip_space( &p[2] ); } } if( p == NULL ) {// end of line or invalid buffer return NULL; } // push the set function onto the stack add_scriptl(buildin_set_ref); add_scriptc(C_ARG); // always append parenthesis to avoid errors syntax.curly[syntax.curly_count].type = TYPE_ARGLIST; syntax.curly[syntax.curly_count].count = 0; syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; // increment the total curly count for the position in the script ++ syntax.curly_count; // parse the variable currently being modified word = add_word(var); if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS ) // cannot assign a variable which exists as a function or label disp_error_message("Cannot modify a variable which has the same name as a function or label.", p); parse_variable_sub_push(word, p2); // Push variable onto the stack if( type != C_EQ ) parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr) if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / -- add_scripti(1); add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB); parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr) } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / -- add_scripti(1); add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB); } else { // Process the value as an expression p = parse_subexpr(p, -1); if( type != C_EQ ) {// push the type of modifier onto the stack add_scriptc(type); } } // decrement the curly count for the position within the script -- syntax.curly_count; // close the script by appending the function operator add_scriptc(C_FUNC); // push the buffer from the method return p; } /* * Checks whether the gives string is a number literal * * Mainly necessary to differentiate between number literals and NPC name * constants, since several of those start with a digit. * * All this does is to check if the string begins with an optional + or - sign, * followed by a hexadecimal or decimal number literal literal and is NOT * followed by a underscore or letter. * * @author : Hercules.ws * @param p Pointer to the string to check * @return Whether the string is a number literal */ bool is_number(const char *p) { const char *np; if (!p) return false; if (*p == '-' || *p == '+') p++; np = p; if (*p == '0' && p[1] == 'x') { p+=2; np = p; // Hexadecimal while (ISXDIGIT(*np)) np++; } else { // Decimal while (ISDIGIT(*np)) np++; } if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _ return true; return false; } /*========================================== * Analysis section *------------------------------------------*/ const char* parse_simpleexpr(const char *p) { long long i; p=skip_space(p); if(*p==';' || *p==',') disp_error_message("parse_simpleexpr: unexpected end of expression",p); if(*p=='('){ if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST ) ++syntax.curly[i].count; p=parse_subexpr(p+1,-1); p=skip_space(p); if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST && syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0 ){ if( *p == ',' ){ syntax.curly[i].flag = ARGLIST_PAREN; return p; } else syntax.curly[i].flag = ARGLIST_NO_PAREN; } if( *p != ')' ) disp_error_message("parse_simpleexpr: unmatched ')'",p); ++p; } else if(is_number(p)) { char *np; while(*p == '0' && ISDIGIT(p[1])) p++; i=strtoll(p,&np,0); if( i < INT_MIN ) { i = INT_MIN; disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p); } else if( i > INT_MAX ) { i = INT_MAX; disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p); } add_scripti((int)i); p=np; } else if(*p=='"'){ add_scriptc(C_STR); p++; while( *p && *p != '"' ){ if( (unsigned char)p[-1] <= 0x7e && *p == '\\' ) { char buf[8]; size_t len = skip_escaped_c(p) - p; size_t n = sv_unescape_c(buf, p, len); if( n != 1 ) ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf); p += len; add_scriptb(*buf); continue; } else if( *p == '\n' ) disp_error_message("parse_simpleexpr: unexpected newline @ string",p); add_scriptb(*p++); } if(!*p) disp_error_message("parse_simpleexpr: unexpected eof @ string",p); add_scriptb(0); p++; //'"' } else { int l; const char* pv; // label , register , function etc if(skip_word(p)==p) disp_error_message("parse_simpleexpr: unexpected character",p); l=add_word(p); if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS) return parse_callfunc(p,1,0); else { const char* name = get_str(l); if( strdb_get(userfunc_db,name) != NULL ) { return parse_callfunc(p,1,1); } } if( (pv = parse_variable(p)) ) {// successfully processed a variable assignment return pv; } #if defined(SCRIPT_CONSTANT_DEPRECATION) if( str_data[l].type == C_INT && str_data[l].deprecated ){ ShowWarning( "Usage of deprecated constant '%s'.\n", get_str(l) ); ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" ); } #endif p=skip_word(p); if( *p == '[' ){ // array(name[i] => getelementofarray(name,i) ) add_scriptl(buildin_getelementofarray_ref); add_scriptc(C_ARG); add_scriptl(l); p=parse_subexpr(p+1,-1); p=skip_space(p); if( *p != ']' ) disp_error_message("parse_simpleexpr: unmatched ']'",p); ++p; add_scriptc(C_FUNC); }else add_scriptl(l); } return p; } /*========================================== * Analysis of the expression *------------------------------------------*/ const char* parse_subexpr(const char* p,int limit) { int op,opl,len; p=skip_space(p); if( *p == '-' ){ const char* tmpp = skip_space(p+1); if( *tmpp == ';' || *tmpp == ',' ){ add_scriptl(LABEL_NEXTLINE); p++; return p; } } if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators p = parse_variable(p); else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators p = parse_subexpr(p + 1, 11); add_scriptc(op); } else p = parse_simpleexpr(p); p = skip_space(p); while(( (op=C_OP3,opl=0,len=1,*p=='?') || ((op=C_ADD,opl=9,len=1,*p=='+') && p[1]!='+') || ((op=C_SUB,opl=9,len=1,*p=='-') && p[1]!='-') || (op=C_MUL,opl=10,len=1,*p=='*') || (op=C_DIV,opl=10,len=1,*p=='/') || (op=C_MOD,opl=10,len=1,*p=='%') || (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') || (op=C_AND,opl=5,len=1,*p=='&') || (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') || (op=C_OR,opl=3,len=1,*p=='|') || (op=C_XOR,opl=4,len=1,*p=='^') || (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') || (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') || (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') || (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') || (op=C_GT,opl=7,len=1,*p=='>') || (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') || (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') || (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){ p+=len; if(op == C_OP3) { p=parse_subexpr(p,-1); p=skip_space(p); if( *(p++) != ':') disp_error_message("parse_subexpr: expected ':'", p-1); p=parse_subexpr(p,-1); } else { p=parse_subexpr(p,opl); } add_scriptc(op); p=skip_space(p); } return p; /* return first untreated operator */ } /*========================================== * Evaluation of the expression *------------------------------------------*/ const char* parse_expr(const char *p) { switch(*p){ case ')': case ';': case ':': case '[': case ']': case '}': disp_error_message("parse_expr: unexpected character",p); } p=parse_subexpr(p,-1); return p; } /*========================================== * Analysis of the line *------------------------------------------*/ const char* parse_line(const char* p) { const char* p2; p=skip_space(p); if(*p==';') { //Close decision for if(); for(); while(); p = parse_syntax_close(p + 1); return p; } if(*p==')' && parse_syntax_for_flag) return p+1; p = skip_space(p); if(p[0] == '{') { syntax.curly[syntax.curly_count].type = TYPE_NULL; syntax.curly[syntax.curly_count].count = -1; syntax.curly[syntax.curly_count].index = -1; syntax.curly_count++; return p + 1; } else if(p[0] == '}') { return parse_curly_close(p); } // Syntax-related processing p2 = parse_syntax(p); if(p2 != NULL) return p2; // attempt to process a variable assignment p2 = parse_variable(p); if( p2 != NULL ) {// variable assignment processed so leave the method return parse_syntax_close(p2 + 1); } p = parse_callfunc(p,0,0); p = skip_space(p); if(parse_syntax_for_flag) { if( *p != ')' ) disp_error_message("parse_line: expected ')'",p); } else { if( *p != ';' ) disp_error_message("parse_line: expected ';'",p); } //Binding decision for if(), for(), while() p = parse_syntax_close(p+1); return p; } // { ... } Closing process const char* parse_curly_close(const char* p) { if(syntax.curly_count <= 0) { disp_error_message("parse_curly_close: unexpected string",p); return p + 1; } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) { syntax.curly_count--; //Close decision if, for , while p = parse_syntax_close(p + 1); return p; } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) { //Closing switch() int pos = syntax.curly_count-1; char label[256]; int l; // Remove temporary variables sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // Go to the end pointer unconditionally sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // You are here labeled sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); l=add_str(label); set_label(l,script_pos, p); if(syntax.curly[pos].flag) { //Exists default sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; } // Label end sprintf(label,"__SW%x_FIN",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos, p); linkdb_final(&syntax.curly[pos].case_label); // free the list of case label syntax.curly_count--; //Closing decision if, for , while p = parse_syntax_close(p + 1); return p; } else { disp_error_message("parse_curly_close: unexpected string",p); return p + 1; } } // Syntax-related processing // break, case, continue, default, do, for, function, // if, switch, while ? will handle this internally. const char* parse_syntax(const char* p) { const char *p2 = skip_word(p); switch(*p) { case 'B': case 'b': if(p2 - p == 5 && !strncasecmp(p,"break",5)) { // break Processing char label[256]; int pos = syntax.curly_count - 1; while(pos >= 0) { if(syntax.curly[pos].type == TYPE_DO) { sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index); break; } else if(syntax.curly[pos].type == TYPE_FOR) { sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index); break; } else if(syntax.curly[pos].type == TYPE_WHILE) { sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index); break; } else if(syntax.curly[pos].type == TYPE_SWITCH) { sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index); break; } pos--; } if(pos < 0) { disp_error_message("parse_syntax: unexpected 'break'",p); } else { syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; } p = skip_space(p2); if(*p != ';') disp_error_message("parse_syntax: expected ';'",p); // Closing decision if, for , while p = parse_syntax_close(p + 1); return p; } break; case 'c': case 'C': if(p2 - p == 4 && !strncasecmp(p,"case",4)) { //Processing case int pos = syntax.curly_count-1; if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) { disp_error_message("parse_syntax: unexpected 'case' ",p); return p+1; } else { char label[256]; int l,v; char *np; if(syntax.curly[pos].count != 1) { //Jump for FALLTHRU sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // You are here labeled sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); l=add_str(label); set_label(l,script_pos, p); } //Decision statement switch p = skip_space(p2); if(p == p2) { disp_error_message("parse_syntax: expected a space ' '",p); } // check whether case label is integer or not if(is_number(p)) { //Numeric value v = (int)strtol(p,&np,0); if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word p++; p = skip_word(p); if(np != p) disp_error_message("parse_syntax: 'case' label is not an integer",np); } else { //Check for constants p2 = skip_word(p); v = (int)(size_t) (p2-p); // length of word at p2 memcpy(label,p,v); label[v]='\0'; if( !script_get_constant(label, &v) ) disp_error_message("parse_syntax: 'case' label is not an integer",p); p = skip_word(p); } p = skip_space(p); if(*p != ':') disp_error_message("parse_syntax: expect ':'",p); sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;", v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1); syntax.curly[syntax.curly_count++].type = TYPE_NULL; // Bad I do not parse twice p2 = parse_line(label); parse_line(p2); syntax.curly_count--; if(syntax.curly[pos].count != 1) { // Label after the completion of FALLTHRU sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count); l=add_str(label); set_label(l,script_pos,p); } // check duplication of case label [Rayce] if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL) disp_error_message("parse_syntax: dup 'case'",p); linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1); sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; syntax.curly[pos].count++; } return p + 1; } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) { // Processing continue char label[256]; int pos = syntax.curly_count - 1; while(pos >= 0) { if(syntax.curly[pos].type == TYPE_DO) { sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index); syntax.curly[pos].flag = 1; //Flag put the link for continue break; } else if(syntax.curly[pos].type == TYPE_FOR) { sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index); break; } else if(syntax.curly[pos].type == TYPE_WHILE) { sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index); break; } pos--; } if(pos < 0) { disp_error_message("parse_syntax: unexpected 'continue'",p); } else { syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; } p = skip_space(p2); if(*p != ';') disp_error_message("parse_syntax: expected ';'",p); //Closing decision if, for , while p = parse_syntax_close(p + 1); return p; } break; case 'd': case 'D': if(p2 - p == 7 && !strncasecmp(p,"default",7)) { // Switch - default processing int pos = syntax.curly_count-1; if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) { disp_error_message("parse_syntax: unexpected 'default'",p); } else if(syntax.curly[pos].flag) { disp_error_message("parse_syntax: dup 'default'",p); } else { char label[256]; int l; // Put the label location p = skip_space(p2); if(*p != ':') { disp_error_message("parse_syntax: expected ':'",p); } sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); l=add_str(label); set_label(l,script_pos,p); // Skip to the next link w/o condition sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // The default label sprintf(label,"__SW%x_DEF",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos,p); syntax.curly[syntax.curly_count - 1].flag = 1; syntax.curly[pos].count++; } return p + 1; } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) { int l; char label[256]; p=skip_space(p2); syntax.curly[syntax.curly_count].type = TYPE_DO; syntax.curly[syntax.curly_count].count = 1; syntax.curly[syntax.curly_count].index = syntax.index++; syntax.curly[syntax.curly_count].flag = 0; // Label of the (do) form here sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index); l=add_str(label); set_label(l,script_pos,p); syntax.curly_count++; return p; } break; case 'f': case 'F': if(p2 - p == 3 && !strncasecmp(p,"for",3)) { int l; char label[256]; int pos = syntax.curly_count; syntax.curly[syntax.curly_count].type = TYPE_FOR; syntax.curly[syntax.curly_count].count = 1; syntax.curly[syntax.curly_count].index = syntax.index++; syntax.curly[syntax.curly_count].flag = 0; syntax.curly_count++; p=skip_space(p2); if(*p != '(') disp_error_message("parse_syntax: expected '('",p); p++; // Execute the initialization statement syntax.curly[syntax.curly_count++].type = TYPE_NULL; p=parse_line(p); syntax.curly_count--; // Form the start of label decision sprintf(label,"__FR%x_J",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos,p); p=skip_space(p); if(*p == ';') { // For (; Because the pattern of always true ;) ; } else { // Skip to the end point if the condition is false sprintf(label,"__FR%x_FIN",syntax.curly[pos].index); add_scriptl(add_str("jump_zero")); add_scriptc(C_ARG); p=parse_expr(p); p=skip_space(p); add_scriptl(add_str(label)); add_scriptc(C_FUNC); } if(*p != ';') disp_error_message("parse_syntax: expected ';'",p); p++; // Skip to the beginning of the loop sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // Labels to form the next loop sprintf(label,"__FR%x_NXT",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos,p); // Process the next time you enter the loop // A ')' last for; flag to be treated as' parse_syntax_for_flag = 1; syntax.curly[syntax.curly_count++].type = TYPE_NULL; p=parse_line(p); syntax.curly_count--; parse_syntax_for_flag = 0; // Skip to the determination process conditions sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // Loop start labeling sprintf(label,"__FR%x_BGN",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos,p); return p; } else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 ) {// internal script function const char *func_name; func_name = skip_space(p2); p = skip_word(func_name); if( p == func_name ) disp_error_message("parse_syntax:function: function name is missing or invalid", p); p2 = skip_space(p); if( *p2 == ';' ) {// function ; // function declaration - just register the name int l; l = add_word(func_name); if( str_data[l].type == C_NOP )// register only, if the name was not used by something else str_data[l].type = C_USERFUNC; else if( str_data[l].type == C_USERFUNC ) ; // already registered else disp_error_message("parse_syntax:function: function name is invalid", func_name); // Close condition of if, for, while p = parse_syntax_close(p2 + 1); return p; } else if(*p2 == '{') {// function char label[256]; int l; syntax.curly[syntax.curly_count].type = TYPE_USERFUNC; syntax.curly[syntax.curly_count].count = 1; syntax.curly[syntax.curly_count].index = syntax.index++; syntax.curly[syntax.curly_count].flag = 0; ++syntax.curly_count; // Jump over the function code sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index); syntax.curly[syntax.curly_count].type = TYPE_NULL; ++syntax.curly_count; parse_line(label); --syntax.curly_count; // Set the position of the function (label) l=add_word(func_name); if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else { str_data[l].type = C_USERFUNC; set_label(l, script_pos, p); if( parse_options&SCRIPT_USE_LABEL_DB ) strdb_iput(scriptlabel_db, get_str(l), script_pos); } else disp_error_message("parse_syntax:function: function name is invalid", func_name); return skip_space(p); } else { disp_error_message("expect ';' or '{' at function syntax",p); } } break; case 'i': case 'I': if(p2 - p == 2 && !strncasecmp(p,"if",2)) { // If process char label[256]; p=skip_space(p2); if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA) disp_error_message("need '('",p); } syntax.curly[syntax.curly_count].type = TYPE_IF; syntax.curly[syntax.curly_count].count = 1; syntax.curly[syntax.curly_count].index = syntax.index++; syntax.curly[syntax.curly_count].flag = 0; sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count); syntax.curly_count++; add_scriptl(add_str("jump_zero")); add_scriptc(C_ARG); p=parse_expr(p); p=skip_space(p); add_scriptl(add_str(label)); add_scriptc(C_FUNC); return p; } break; case 's': case 'S': if(p2 - p == 6 && !strncasecmp(p,"switch",6)) { // Processing of switch () char label[256]; p=skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } syntax.curly[syntax.curly_count].type = TYPE_SWITCH; syntax.curly[syntax.curly_count].count = 1; syntax.curly[syntax.curly_count].index = syntax.index++; syntax.curly[syntax.curly_count].flag = 0; sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index); syntax.curly_count++; add_scriptl(add_str("set")); add_scriptc(C_ARG); add_scriptl(add_str(label)); p=parse_expr(p); p=skip_space(p); if(*p != '{') { disp_error_message("parse_syntax: expected '{'",p); } add_scriptc(C_FUNC); return p + 1; } break; case 'w': case 'W': if(p2 - p == 5 && !strncasecmp(p,"while",5)) { int l; char label[256]; p=skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } syntax.curly[syntax.curly_count].type = TYPE_WHILE; syntax.curly[syntax.curly_count].count = 1; syntax.curly[syntax.curly_count].index = syntax.index++; syntax.curly[syntax.curly_count].flag = 0; // Form the start of label decision sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index); l=add_str(label); set_label(l,script_pos,p); // Skip to the end point if the condition is false sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index); syntax.curly_count++; add_scriptl(add_str("jump_zero")); add_scriptc(C_ARG); p=parse_expr(p); p=skip_space(p); add_scriptl(add_str(label)); add_scriptc(C_FUNC); return p; } break; } return NULL; } const char* parse_syntax_close(const char *p) { // If (...) for (...) hoge (); as to make sure closed closed once again int flag; do { p = parse_syntax_close_sub(p,&flag); } while(flag); return p; } // Close judgment if, for, while, of do // flag == 1 : closed // flag == 0 : not closed const char* parse_syntax_close_sub(const char* p,int* flag) { char label[256]; int pos = syntax.curly_count - 1; int l; *flag = 1; if(syntax.curly_count <= 0) { *flag = 0; return p; } else if(syntax.curly[pos].type == TYPE_IF) { const char *bp = p; const char *p2; // if-block and else-block end is a new line parse_nextline(false, p); // Skip to the last location if sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // Put the label of the location sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); l=add_str(label); set_label(l,script_pos,p); syntax.curly[pos].count++; p = skip_space(p); p2 = skip_word(p); if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) { // else or else - if p = skip_space(p2); p2 = skip_word(p); if(p2 - p == 2 && !strncasecmp(p,"if",2)) { // else - if p=skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); add_scriptl(add_str("jump_zero")); add_scriptc(C_ARG); p=parse_expr(p); p=skip_space(p); add_scriptl(add_str(label)); add_scriptc(C_FUNC); *flag = 0; return p; } else { // else if(!syntax.curly[pos].flag) { syntax.curly[pos].flag = 1; *flag = 0; return p; } } } // Close if syntax.curly_count--; // Put the label of the final location sprintf(label,"__IF%x_FIN",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos,p); if(syntax.curly[pos].flag == 1) { // Because the position of the pointer is the same if not else for this return bp; } return p; } else if(syntax.curly[pos].type == TYPE_DO) { int l2; char label2[256]; const char *p2; if(syntax.curly[pos].flag) { // (Come here continue) to form the label here sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index); l2=add_str(label2); set_label(l2,script_pos,p); } // Skip to the end point if the condition is false p = skip_space(p); p2 = skip_word(p); if(p2 - p != 5 || strncasecmp(p,"while",5)) disp_error_message("parse_syntax: expected 'while'",p); p = skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } // do-block end is a new line parse_nextline(false, p); sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index); add_scriptl(add_str("jump_zero")); add_scriptc(C_ARG); p=parse_expr(p); p=skip_space(p); add_scriptl(add_str(label2)); add_scriptc(C_FUNC); // Skip to the starting point sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label2); syntax.curly_count--; // Form label of the end point conditions sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index); l2=add_str(label2); set_label(l2,script_pos,p); p = skip_space(p); if(*p != ';') { disp_error_message("parse_syntax: expected ';'",p); return p+1; } p++; syntax.curly_count--; return p; } else if(syntax.curly[pos].type == TYPE_FOR) { // for-block end is a new line parse_nextline(false, p); // Skip to the next loop sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // End for labeling sprintf(label,"__FR%x_FIN",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos,p); syntax.curly_count--; return p; } else if(syntax.curly[pos].type == TYPE_WHILE) { // while-block end is a new line parse_nextline(false, p); // Skip to the decision while sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label); syntax.curly_count--; // End while labeling sprintf(label,"__WL%x_FIN",syntax.curly[pos].index); l=add_str(label); set_label(l,script_pos,p); syntax.curly_count--; return p; } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) { int pos2 = syntax.curly_count-1; char label2[256]; int l2; // Back sprintf(label2,"return;"); syntax.curly[syntax.curly_count++].type = TYPE_NULL; parse_line(label2); syntax.curly_count--; // Put the label of the location sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index); l2=add_str(label2); set_label(l2,script_pos,p); syntax.curly_count--; return p; } else { *flag = 0; return p; } } /*========================================== * Added built-in functions *------------------------------------------*/ static void add_buildin_func(void) { int i; for( i = 0; buildin_func[i].func; i++ ) { // arg must follow the pattern: (v|s|i|r|l)*\?*\*? // 'v' - value (either string or int or reference) // 's' - string // 'i' - int // 'r' - reference (of a variable) // 'l' - label // '?' - one optional parameter // '*' - unknown number of optional parameters const char* p = buildin_func[i].arg; while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p; while( *p == '?' ) ++p; if( *p == '*' ) ++p; if( *p != 0){ ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg); } else if( *skip_word(buildin_func[i].name) != 0 ){ ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name); } else { int n = add_str(buildin_func[i].name); str_data[n].type = C_FUNC; str_data[n].val = i; str_data[n].func = buildin_func[i].func; str_data[n].deprecated = (buildin_func[i].deprecated != NULL); if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n; else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n; else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n; else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n; } } } /// Retrieves the value of a constant parameter. bool script_get_parameter(const char* name, int* value) { int n = search_str(name); if (n == -1 || str_data[n].type != C_PARAM) {// not found or not a parameter return false; } value[0] = str_data[n].val; return true; } /// Retrieves the value of a constant. bool script_get_constant(const char* name, int* value) { int n = search_str(name); if( n == -1 || str_data[n].type != C_INT ) {// not found or not a constant return false; } value[0] = str_data[n].val; #if defined(SCRIPT_CONSTANT_DEPRECATION) if( str_data[n].deprecated ){ ShowWarning( "Usage of deprecated constant '%s'.\n", name ); ShowWarning( "This constant was deprecated and could become unavailable anytime soon.\n" ); } #endif return true; } /// Creates new constant or parameter with given value. void script_set_constant(const char* name, int value, bool isparameter, bool deprecated) { int n = add_str(name); if( str_data[n].type == C_NOP ) {// new str_data[n].type = isparameter ? C_PARAM : C_INT; str_data[n].val = value; str_data[n].deprecated = deprecated; } else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT ) {// existing parameter or constant ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value); } else {// existing name ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type)); } } static bool read_constdb_sub( char* fields[], int columns, int current ){ char name[1024], val[1024]; int type = 0; if( columns > 1 ){ if( sscanf(fields[0], "%1023[A-Za-z0-9/_]", name) != 1 || sscanf(fields[1], "%1023[A-Za-z0-9/_]", val) != 1 || ( columns >= 2 && sscanf(fields[2], "%11d", &type) != 1 ) ){ ShowWarning("Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current); return false; } }else{ if( sscanf(fields[0], "%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d", name, val, &type) < 2 ){ ShowWarning( "Skipping line '" CL_WHITE "%d" CL_RESET "', invalid constant definition\n", current ); return false; } } script_set_constant(name, (int)strtol(val, NULL, 0), (type != 0), false); return true; } /*========================================== * Reading constant databases * const.txt *------------------------------------------*/ static void read_constdb(void){ const char* dbsubpath[] = { "", "/" DBIMPORT, }; for( int i = 0; i < ARRAYLENGTH(dbsubpath); i++ ){ int n2 = strlen(db_path) + strlen(dbsubpath[i]) + 1; char* dbsubpath2 = (char*)aMalloc(n2 + 1); bool silent = i > 0; safesnprintf(dbsubpath2, n2, "%s%s", db_path, dbsubpath[i]); sv_readdb(dbsubpath2, "const.txt", ',', 1, 3, -1, &read_constdb_sub, silent); aFree(dbsubpath2); } } /** * Sets source-end constants for NPC scripts to access. **/ void script_hardcoded_constants(void) { #include "script_constants.hpp" } /*========================================== * Display emplacement line of script *------------------------------------------*/ static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line) { int i; if( p == NULL || !p[0] ) return NULL; if( line < 0 ) StringBuf_Printf(buf, "*% 5d : ", -line); else StringBuf_Printf(buf, " % 5d : ", line); for(i=0;p[i] && p[i] != '\n';i++){ if(p + i != mark) StringBuf_Printf(buf, "%c", p[i]); else StringBuf_Printf(buf, "\'%c\'", p[i]); } StringBuf_AppendStr(buf, "\n"); return p+i+(p[i] == '\n' ? 1 : 0); } void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) { // Find the line where the error occurred int j; int line = start_line; const char *p; const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL }; for(p=src;p && *p;line++){ const char *lineend=strchr(p,'\n'); if(lineend==NULL || error_pos_cur=0 && j!=0x00ffffff;){ int next=GETVALUE(script_buf,j); SETVALUE(script_buf,j,i); j=next; } } else if( str_data[i].type == C_USERFUNC ) {// 'function name;' without follow-up code ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str); unresolved_names = true; } } if( unresolved_names ) { disp_error_message("parse_script: unresolved function references", p); } #ifdef DEBUG_DISP for(i=0;iscript_buf = script_buf; code->script_size = script_size; code->local.vars = NULL; code->local.arrays = NULL; return code; } /// Returns the player attached to this script, identified by the rid. /// If there is no player attached, the script is terminated. static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){ *sd = map_id2sd( st->rid ); if( *sd ){ return true; }else{ ShowError("%s: fatal error ! player not attached!\n",func); script_reportfunc(st); script_reportsrc(st); st->state = END; return false; } } /** * Dereferences a variable/constant, replacing it with a copy of the value. * @param st Script state * @param data Variable/constant * @param sd If NULL, will try to use sd from st->rid (for player's variables) */ void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd) { const char* name; char prefix; char postfix; if( !data_isreference(data) ) return;// not a variable/constant name = reference_getname(data); prefix = name[0]; postfix = name[strlen(name) - 1]; //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS] if( !reference_toconstant(data) && not_server_variable(prefix) ) { if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached if( postfix == '$' ) {// string variable ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name); data->type = C_CONSTSTR; data->u.str = const_cast(""); } else {// integer variable ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name); data->type = C_INT; data->u.num = 0; } return; } } if( postfix == '$' ) {// string variable switch( prefix ) { case '@': data->u.str = pc_readregstr(sd, data->u.num); break; case '$': data->u.str = mapreg_readregstr(data->u.num); break; case '#': if( name[1] == '#' ) data->u.str = pc_readaccountreg2str(sd, data->u.num);// global else data->u.str = pc_readaccountregstr(sd, data->u.num);// local break; case '.': { struct DBMap* n = data->ref ? data->ref->vars : name[1] == '@' ? st->stack->scope.vars : // instance/scope variable st->script->local.vars; // npc variable if( n ) data->u.str = (char*)i64db_get(n,reference_getuid(data)); else data->u.str = NULL; } break; case '\'': { unsigned short instance_id = script_instancegetid(st); if( instance_id ) data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data)); else { ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name); data->u.str = NULL; } } break; default: data->u.str = pc_readglobalreg_str(sd, data->u.num); break; } if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string data->type = C_CONSTSTR; data->u.str = const_cast(""); } else {// duplicate string data->type = C_STR; data->u.str = aStrdup(data->u.str); } } else {// integer variable data->type = C_INT; if( reference_toconstant(data) ) { data->u.num = reference_getconstant(data); } else if( reference_toparam(data) ) { data->u.num = pc_readparam(sd, reference_getparamtype(data)); } else switch( prefix ) { case '@': data->u.num = pc_readreg(sd, data->u.num); break; case '$': data->u.num = mapreg_readreg(data->u.num); break; case '#': if( name[1] == '#' ) data->u.num = pc_readaccountreg2(sd, data->u.num);// global else data->u.num = pc_readaccountreg(sd, data->u.num);// local break; case '.': { struct DBMap* n = data->ref ? data->ref->vars : name[1] == '@' ? st->stack->scope.vars : // instance/scope variable st->script->local.vars; // npc variable if( n ) data->u.num = (int)i64db_iget(n,reference_getuid(data)); else data->u.num = 0; } break; case '\'': { unsigned short instance_id = script_instancegetid(st); if( instance_id ) data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data)); else { ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name); data->u.num = 0; } } break; default: data->u.num = pc_readglobalreg(sd, data->u.num); break; } } data->ref = NULL; return; } void get_val(struct script_state* st, struct script_data* data) { get_val_(st,data,NULL); } struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref); /// Retrieves the value of a reference identified by uid (variable, constant, param) /// The value is left in the top of the stack and needs to be removed manually. /// @param st[in]: script state. /// @param uid[in]: reference identifier. /// @param ref[in]: the container to look up the reference into. /// @return: the retrieved value of the reference. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref) { struct script_data* data; push_val2(st->stack, C_NAME, uid, ref); data = script_getdatatop(st, -1); get_val(st, data); //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int). return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str)); } /** * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in * Will be gone as soon as undefined var feature is implemented **/ void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref) { const char *name = get_str(script_getvarid(uid)); // is here st can be null pointer and st->rid is wrong? struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref); bool insert = false; if (sd && !st) { // when sd comes, st isn't available insert = true; } else { if( is_string_variable(name) ) { char* str = (char*)get_val2(st, uid, ref); if( str && *str ) insert = true; script_removetop(st, -1, 0); } else { int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref)); if( num ) insert = true; script_removetop(st, -1, 0); } } if (src && src->arrays) { struct script_array *sa = static_cast(idb_get(src->arrays, script_getvarid(uid))); if (sa) { unsigned int i; ARR_FIND(0, sa->size, i, sa->members[i] == 0); if( i != sa->size ) { if( !insert ) script_array_remove_member(src,sa,i); return; } script_array_add_member(sa,0); } else if (insert) { script_array_update(src,reference_uid(script_getvarid(uid), 0),false); } } } /** * Returns array size by ID **/ unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref) { struct script_array *sa = NULL; struct reg_db *src = script_array_src(st, sd, name, ref); if (src && src->arrays) sa = static_cast(idb_get(src->arrays, search_str(name))); return sa ? sa->size : 0; } /** * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size) **/ unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref) { struct script_array *sa = NULL; struct reg_db *src = script_array_src(st, sd, name, ref); if (src && src->arrays) { int key = add_word(name); script_array_ensure_zero(st,sd,reference_uid(key, 0), ref); if( ( sa = static_cast(idb_get(src->arrays, key)) ) ) { unsigned int i, highest_key = 0; for(i = 0; i < sa->size; i++) { if( sa->members[i] > highest_key ) highest_key = sa->members[i]; } return sa->size ? highest_key + 1 : 0; } } return SCRIPT_CMD_SUCCESS; } int script_free_array_db(DBKey key, DBData *data, va_list ap) { struct script_array *sa = static_cast(db_data2ptr(data)); aFree(sa->members); ers_free(array_ers, sa); return SCRIPT_CMD_SUCCESS; } /** * Clears script_array and removes it from script->array_db **/ void script_array_delete(struct reg_db *src, struct script_array *sa) { aFree(sa->members); idb_remove(src->arrays, sa->id); ers_free(array_ers, sa); } /** * Removes a member from a script_array list * * @param idx the index of the member in script_array struct list, not of the actual array member **/ void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx) { unsigned int i, cursor; // it's the only member left, no need to do anything other than delete the array data if( sa->size == 1 ) { script_array_delete(src,sa); return; } sa->members[idx] = UINT_MAX; for(i = 0, cursor = 0; i < sa->size; i++) { if( sa->members[i] == UINT_MAX ) continue; if( i != cursor ) sa->members[cursor] = sa->members[i]; cursor++; } sa->size = cursor; } /** * Appends a new array index to the list in script_array * * @param idx the index of the array member being inserted **/ void script_array_add_member(struct script_array *sa, unsigned int idx) { RECREATE(sa->members, unsigned int, ++sa->size); sa->members[sa->size - 1] = idx; } /** * Obtains the source of the array database for this type and scenario * Initializes such database when not yet initialized. **/ struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref) { struct reg_db *src = NULL; switch( name[0] ) { // from player default: // char reg case '@': // temp char reg case '#': // account reg src = &sd->regs; break; case '$': // map reg src = ®s; break; case '.': // npc/script if (ref) src = ref; else src = (name[1] == '@') ? &st->stack->scope : &st->script->local; break; case '\'': // instance { unsigned short instance_id = script_instancegetid(st); if( instance_id ) { src = &instance_data[instance_id].regs; } break; } } if( src ) { if( !src->arrays ) src->arrays = idb_alloc(DB_OPT_BASE); return src; } return NULL; } /** * Processes a array member modification, and update data accordingly * * @param src[in,out] Variable source database. If the array database doesn't exist, it is created. * @param num[in] Variable ID * @param empty[in] Whether the modified member is empty (needs to be removed) **/ void script_array_update(struct reg_db *src, int64 num, bool empty) { struct script_array *sa = NULL; int id = script_getvarid(num); unsigned int index = script_getvaridx(num); if (!src->arrays) { src->arrays = idb_alloc(DB_OPT_BASE); } else { sa = static_cast(idb_get(src->arrays, id)); } if( sa ) { unsigned int i; // search for(i = 0; i < sa->size; i++) { if( sa->members[i] == index ) break; } // if existent if( i != sa->size ) { // if empty, we gotta remove it if( empty ) { script_array_remove_member(src, sa, i); } } else if( !empty ) { /* new entry */ script_array_add_member(sa,index); // we do nothing if its empty, no point in modifying array data for a new empty member } } else if ( !empty ) { // we only move to create if not empty sa = ers_alloc(array_ers, struct script_array); sa->id = id; sa->members = NULL; sa->size = 0; script_array_add_member(sa,index); idb_put(src->arrays, id, sa); } } /** * Stores the value of a script variable * * @param st: current script state. * @param sd: current character data. * @param num: variable identifier. * @param name: variable name. * @param value: new value. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope. * @return: 0 failure, 1 success. * * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success. *------------------------------------------*/ int set_reg(struct script_state* st, struct map_session_data* sd, int64 num, const char* name, const void* value, struct reg_db *ref) { char prefix = name[0]; size_t vlen = 0; if ( !script_check_RegistryVariableLength(0,name,&vlen) ) { ShowError("set_reg: Variable name length is too long (aid: %d, cid: %d): '%s' sz=%d\n", sd?sd->status.account_id:-1, sd?sd->status.char_id:-1, name, vlen); return 0; } if( is_string_variable(name) ) {// string variable const char *str = (const char*)value; switch (prefix) { case '@': pc_setregstr(sd, num, str); return 1; case '$': return mapreg_setregstr(num, str); case '#': return (name[1] == '#') ? pc_setaccountreg2str(sd, num, str) : pc_setaccountregstr(sd, num, str); case '.': { struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local; if( n ) { if (str[0]) { i64db_put(n->vars, num, aStrdup(str)); if( script_getvaridx(num) ) script_array_update(n, num, false); } else { i64db_remove(n->vars, num); if( script_getvaridx(num) ) script_array_update(n, num, true); } } } return 1; case '\'': { unsigned short instance_id = script_instancegetid(st); if( instance_id ) { if( str[0] ) { i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str)); if( script_getvaridx(num) ) script_array_update(&instance_data[instance_id].regs, num, false); } else { i64db_remove(instance_data[instance_id].regs.vars, num); if (script_getvaridx(num)) script_array_update(&instance_data[instance_id].regs, num, true); } } else { ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name); script_reportsrc(st); } return 1; } default: return pc_setglobalreg_str(sd, num, str); } } else {// integer variable int val = (int)__64BPRTSIZE(value); if(str_data[script_getvarid(num)].type == C_PARAM) { if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) { if( st != NULL ) { ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val); script_reportsrc(st); st->state = END; } return 0; } return 1; } switch (prefix) { case '@': pc_setreg(sd, num, val); return 1; case '$': return mapreg_setreg(num, val); case '#': return (name[1] == '#') ? pc_setaccountreg2(sd, num, val) : pc_setaccountreg(sd, num, val); case '.': { struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local; if( n ) { if( val != 0 ) { i64db_iput(n->vars, num, val); if( script_getvaridx(num) ) script_array_update(n, num, false); } else { i64db_remove(n->vars, num); if( script_getvaridx(num) ) script_array_update(n, num, true); } } } return 1; case '\'': { unsigned short instance_id = script_instancegetid(st); if( instance_id ) { if( val != 0 ) { i64db_iput(instance_data[instance_id].regs.vars, num, val); if( script_getvaridx(num) ) script_array_update(&instance_data[instance_id].regs, num, false); } else { i64db_remove(instance_data[instance_id].regs.vars, num); if (script_getvaridx(num)) script_array_update(&instance_data[instance_id].regs, num, true); } } else { ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name); script_reportsrc(st); } return 1; } default: return pc_setglobalreg(sd, num, val); } } } int set_var(struct map_session_data* sd, char* name, void* val) { return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL); } void setd_sub(struct script_state *st, struct map_session_data *sd, const char *varname, int elem, void *value, struct reg_db *ref) { set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref); } /** * Converts the data to a string * @param st * @param data * @param sd */ const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd) { char* p; get_val_(st, data, sd); if( data_isstring(data) ) {// nothing to convert } else if( data_isint(data) ) {// int -> string CREATE(p, char, ITEM_NAME_LENGTH); snprintf(p, ITEM_NAME_LENGTH, "%" PRId64 "", data->u.num); p[ITEM_NAME_LENGTH-1] = '\0'; data->type = C_STR; data->u.str = p; } else if( data_isreference(data) ) {// reference -> string //##TODO when does this happen (check get_val) [FlavioJS] data->type = C_CONSTSTR; data->u.str = reference_getname(data); } else {// unsupported data type ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n"); script_reportdata(data); script_reportsrc(st); data->type = C_CONSTSTR; data->u.str = const_cast(""); } return data->u.str; } const char* conv_str(struct script_state* st, struct script_data* data) { return conv_str_(st, data, NULL); } /** * Converts the data to an int * @param st * @param data * @param sd */ int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd) { get_val_(st, data, sd); if( data_isint(data) ) {// nothing to convert } else if( data_isstring(data) ) {// string -> int // the result does not overflow or underflow, it is capped instead // ex: 999999999999 is capped to INT_MAX (2147483647) char* p = data->u.str; long num; errno = 0; num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF" if( errno == ERANGE #if LONG_MAX > INT_MAX || num < INT_MIN || num > INT_MAX #endif ) { if( num <= INT_MIN ) { num = INT_MIN; ShowError("script:conv_num: underflow detected, capping to %ld\n", num); } else//if( num >= INT_MAX ) { num = INT_MAX; ShowError("script:conv_num: overflow detected, capping to %ld\n", num); } script_reportdata(data); script_reportsrc(st); } if( data->type == C_STR ) aFree(p); data->type = C_INT; data->u.num = (int)num; } #if 0 // FIXME this function is being used to retrieve the position of labels and // probably other stuff [FlavioJS] else {// unsupported data type ShowError("script:conv_num: cannot convert to number, defaulting to 0\n"); script_reportdata(data); script_reportsrc(st); data->type = C_INT; data->u.num = 0; } #endif return (int)data->u.num; } int conv_num(struct script_state* st, struct script_data* data) { return conv_num_(st, data, NULL); } // // Stack operations // /// Increases the size of the stack void stack_expand(struct script_stack* stack) { stack->sp_max += 64; stack->stack_data = (struct script_data*)aRealloc(stack->stack_data, stack->sp_max * sizeof(stack->stack_data[0]) ); memset(stack->stack_data + (stack->sp_max - 64), 0, 64 * sizeof(stack->stack_data[0]) ); } /// Pushes a value into the stack #define push_val(stack,type,val) push_val2(stack, type, val, NULL) /// Pushes a value into the stack (with reference) struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref) { if( stack->sp >= stack->sp_max ) stack_expand(stack); stack->stack_data[stack->sp].type = type; stack->stack_data[stack->sp].u.num = val; stack->stack_data[stack->sp].ref = ref; stack->sp++; return &stack->stack_data[stack->sp-1]; } /// Pushes a string into the stack struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str) { if( stack->sp >= stack->sp_max ) stack_expand(stack); stack->stack_data[stack->sp].type = type; stack->stack_data[stack->sp].u.str = str; stack->stack_data[stack->sp].ref = NULL; stack->sp++; return &stack->stack_data[stack->sp-1]; } /// Pushes a retinfo into the stack struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref) { if( stack->sp >= stack->sp_max ) stack_expand(stack); stack->stack_data[stack->sp].type = C_RETINFO; stack->stack_data[stack->sp].u.ri = ri; stack->stack_data[stack->sp].ref = ref; stack->sp++; return &stack->stack_data[stack->sp-1]; } /// Pushes a copy of the target position into the stack struct script_data* push_copy(struct script_stack* stack, int pos) { switch( stack->stack_data[pos].type ) { case C_CONSTSTR: return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str); break; case C_STR: return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str)); break; case C_RETINFO: ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n"); exit(1); break; default: return push_val2( stack,stack->stack_data[pos].type, stack->stack_data[pos].u.num, stack->stack_data[pos].ref ); break; } } /// Removes the values in indexes [start,end] from the stack. /// Adjusts all stack pointers. void pop_stack(struct script_state* st, int start, int end) { struct script_stack* stack = st->stack; struct script_data* data; int i; if( start < 0 ) start = 0; if( end > stack->sp ) end = stack->sp; if( start >= end ) return;// nothing to pop // free stack elements for( i = start; i < end; i++ ) { data = &stack->stack_data[i]; if( data->type == C_STR ) aFree(data->u.str); if( data->type == C_RETINFO ) { struct script_retinfo* ri = data->u.ri; if (ri->scope.vars) { script_free_vars(ri->scope.vars); ri->scope.vars = NULL; } if (ri->scope.arrays) { ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db); ri->scope.arrays = NULL; } if( data->ref ) aFree(data->ref); aFree(ri); } data->type = C_NOP; } // move the rest of the elements if( stack->sp > end ) { memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end)); for( i = start + stack->sp - end; i < stack->sp; ++i ) stack->stack_data[i].type = C_NOP; } // adjust stack pointers if( st->start > end ){ st->start -= end - start; }else if( st->start > start ){ st->start = start; } if( st->end > end ){ st->end -= end - start; }else if( st->end > start ){ st->end = start; } if( stack->defsp > end ){ stack->defsp -= end - start; }else if( stack->defsp > start ){ stack->defsp = start; } stack->sp -= end - start; } /// /// /// /*========================================== * Release script dependent variable, dependent variable of function *------------------------------------------*/ void script_free_vars(struct DBMap* storage) { if( storage ) { // destroy the storage construct containing the variables db_destroy(storage); } } void script_free_code(struct script_code* code) { nullpo_retv(code); if (code->instances) script_stop_scriptinstances(code); script_free_vars(code->local.vars); if (code->local.arrays) code->local.arrays->destroy(code->local.arrays, script_free_array_db); aFree(code->script_buf); aFree(code); } /// Creates a new script state. /// /// @param script Script code /// @param pos Position in the code /// @param rid Who is running the script (attached player) /// @param oid Where the code is being run (npc 'object') /// @return Script state struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid) { struct script_state* st; st = ers_alloc(st_ers, struct script_state); st->stack = ers_alloc(stack_ers, struct script_stack); st->stack->sp = 0; st->stack->sp_max = 64; CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max); st->stack->defsp = st->stack->sp; st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA); st->stack->scope.arrays = NULL; st->state = RUN; st->script = rootscript; st->pos = pos; st->rid = rid; st->oid = oid; st->sleep.timer = INVALID_TIMER; st->npc_item_flag = battle_config.item_enabled_npc; if( st->script->instances != USHRT_MAX ) st->script->instances++; else { struct npc_data *nd = map_id2nd(oid); ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown"); } if (!st->script->local.vars) st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA); st->id = next_id++; active_scripts++; idb_put(st_db, st->id, st); return st; } /// Frees a script state. /// /// @param st Script state void script_free_state(struct script_state* st) { if (idb_exists(st_db, st->id)) { struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL; if (st->bk_st) // backup was not restored ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid); if (sd && sd->st == st) { // Current script is aborted. if(sd->state.using_fake_npc) { clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd); sd->state.using_fake_npc = 0; } sd->st = NULL; sd->npc_id = 0; } if (st->sleep.timer != INVALID_TIMER) delete_timer(st->sleep.timer, run_script_timer); if (st->stack) { script_free_vars(st->stack->scope.vars); if (st->stack->scope.arrays) st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db); pop_stack(st, 0, st->stack->sp); aFree(st->stack->stack_data); ers_free(stack_ers, st->stack); st->stack = NULL; } if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) { if (st->script->local.vars && !db_size(st->script->local.vars)) { script_free_vars(st->script->local.vars); st->script->local.vars = NULL; } if (st->script->local.arrays && !db_size(st->script->local.arrays)) { st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db); st->script->local.arrays = NULL; } } st->pos = -1; idb_remove(st_db, st->id); ers_free(st_ers, st); if (--active_scripts == 0) next_id = 0; } } // // Main execution unit // /*========================================== * Read command *------------------------------------------*/ c_op get_com(unsigned char *script,int *pos) { int i = 0, j = 0; if(script[*pos]>=0x80){ return C_INT; } while(script[*pos]>=0x40){ i=script[(*pos)++]<=0xc0){ i+=(script[(*pos)++]&0x7f)<u.str[0];// "" -> false else if( data_isint(data) ) flag = data->u.num == 0 ? 0 : 1;// 0 -> false else { ShowError("script:op_3: invalid data for the ternary operator test\n"); script_reportdata(data); script_reportsrc(st); script_removetop(st, -3, 0); script_pushnil(st); return; } if( flag ) script_pushcopytop(st, -2); else script_pushcopytop(st, -1); script_removetop(st, -4, -1); } /// Binary string operators /// s1 EQ s2 -> i /// s1 NE s2 -> i /// s1 GT s2 -> i /// s1 GE s2 -> i /// s1 LT s2 -> i /// s1 LE s2 -> i /// s1 ADD s2 -> s void op_2str(struct script_state* st, int op, const char* s1, const char* s2) { int a = 0; switch(op){ case C_EQ: a = (strcmp(s1,s2) == 0); break; case C_NE: a = (strcmp(s1,s2) != 0); break; case C_GT: a = (strcmp(s1,s2) > 0); break; case C_GE: a = (strcmp(s1,s2) >= 0); break; case C_LT: a = (strcmp(s1,s2) < 0); break; case C_LE: a = (strcmp(s1,s2) <= 0); break; case C_ADD: { char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char)); strcpy(buf, s1); strcat(buf, s2); script_pushstr(st, buf); return; } default: ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op)); script_reportsrc(st); script_pushnil(st); st->state = END; return; } script_pushint(st,a); } /// Binary number operators /// i OP i -> i void op_2num(struct script_state* st, int op, int i1, int i2) { int ret; double ret_double; switch( op ) { case C_AND: ret = i1 & i2; break; case C_OR: ret = i1 | i2; break; case C_XOR: ret = i1 ^ i2; break; case C_LAND: ret = (i1 && i2); break; case C_LOR: ret = (i1 || i2); break; case C_EQ: ret = (i1 == i2); break; case C_NE: ret = (i1 != i2); break; case C_GT: ret = (i1 > i2); break; case C_GE: ret = (i1 >= i2); break; case C_LT: ret = (i1 < i2); break; case C_LE: ret = (i1 <= i2); break; case C_R_SHIFT: ret = i1>>i2; break; case C_L_SHIFT: ret = i1<state = END; return; } else if( op == C_DIV ) ret = i1 / i2; else//if( op == C_MOD ) ret = i1 % i2; break; default: switch( op ) {// operators that can overflow/underflow case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break; case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break; case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break; default: ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2); script_reportsrc(st); script_pushnil(st); return; } if( ret_double < (double)INT_MIN ) { ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2); script_reportsrc(st); ret = INT_MIN; } else if( ret_double > (double)INT_MAX ) { ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2); script_reportsrc(st); ret = INT_MAX; } } script_pushint(st, ret); } /// Binary operators void op_2(struct script_state *st, int op) { struct script_data* left, leftref; struct script_data* right; leftref.type = C_NOP; left = script_getdatatop(st, -2); right = script_getdatatop(st, -1); if (st->op2ref) { if (data_isreference(left)) { leftref = *left; } st->op2ref = 0; } get_val(st, left); get_val(st, right); // automatic conversions switch( op ) { case C_ADD: if( data_isint(left) && data_isstring(right) ) {// convert int-string to string-string conv_str(st, left); } else if( data_isstring(left) && data_isint(right) ) {// convert string-int to string-string conv_str(st, right); } break; } if( data_isstring(left) && data_isstring(right) ) {// ss => op_2str op_2str(st, op, left->u.str, right->u.str); script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one if (leftref.type != C_NOP) { if (left->type == C_STR) // don't free C_CONSTSTR aFree(left->u.str); *left = leftref; } } else if( data_isint(left) && data_isint(right) ) {// ii => op_2num int i1 = (int)left->u.num; int i2 = (int)right->u.num; script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0); op_2num(st, op, i1, i2); if (leftref.type != C_NOP) *left = leftref; } else {// invalid argument ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op)); script_reportdata(left); script_reportdata(right); script_reportsrc(st); script_removetop(st, -2, 0); script_pushnil(st); st->state = END; } } /// Unary operators /// NEG i -> i /// NOT i -> i /// LNOT i -> i void op_1(struct script_state* st, int op) { struct script_data* data; int i1; data = script_getdatatop(st, -1); get_val(st, data); if( !data_isint(data) ) {// not a number ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op)); script_reportdata(data); script_reportsrc(st); script_pushnil(st); st->state = END; return; } i1 = (int)data->u.num; script_removetop(st, -1, 0); switch( op ) { case C_NEG: i1 = -i1; break; case C_NOT: i1 = ~i1; break; case C_LNOT: i1 = !i1; break; default: ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1); script_reportsrc(st); script_pushnil(st); st->state = END; return; } script_pushint(st, i1); } /// Checks the type of all arguments passed to a built-in function. /// /// @param st Script state whose stack arguments should be inspected. /// @param func Built-in function for which the arguments are intended. static void script_check_buildin_argtype(struct script_state* st, int func) { int idx, invalid = 0; for( idx = 2; script_hasdata(st, idx); idx++ ) { struct script_data* data = script_getdata(st, idx); script_function* sf = &buildin_func[str_data[func].val]; char type = sf->arg[idx-2]; if( type == '?' || type == '*' ) {// optional argument or unknown number of optional parameters ( no types are after this ) break; } else if( type == 0 ) {// more arguments than necessary ( should not happen, as it is checked before ) ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type)); invalid++; break; } else { const char* name = NULL; if( data_isreference(data) ) {// get name for variables to determine the type they refer to name = reference_getname(data); } switch( type ) { case 'v': if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) ) {// variant ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1); script_reportdata(data); invalid++; } break; case 's': if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) ) {// string ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1); script_reportdata(data); invalid++; } break; case 'i': if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) ) {// int ( params and constants are always int ) ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1); script_reportdata(data); invalid++; } break; case 'r': if( !data_isreference(data) ) {// variables ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type)); script_reportdata(data); invalid++; } break; case 'l': if( !data_islabel(data) && !data_isfunclabel(data) ) {// label ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type)); script_reportdata(data); invalid++; } break; } } } if(invalid) { ShowDebug("Function: %s\n", get_str(func)); script_reportsrc(st); } } /// Executes a buildin command. /// Stack: C_NAME() C_ARG ... int run_func(struct script_state *st) { struct script_data* data; int i,start_sp,end_sp,func; end_sp = st->stack->sp;// position after the last argument for( i = end_sp-1; i > 0 ; --i ) if( st->stack->stack_data[i].type == C_ARG ) break; if( i == 0 ) { ShowError("script:run_func: C_ARG not found. please report this!!!\n"); st->state = END; script_reportsrc(st); return 1; } start_sp = i-1;// C_NAME of the command st->start = start_sp; st->end = end_sp; data = &st->stack->stack_data[st->start]; if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) { func = (int)data->u.num; st->funcname = reference_getname(data); } else { ShowError("script:run_func: not a buildin command.\n"); script_reportdata(data); script_reportsrc(st); st->state = END; return 1; } if( script_config.warn_func_mismatch_argtypes ) { script_check_buildin_argtype(st, func); } if(str_data[func].func) { #if defined(SCRIPT_COMMAND_DEPRECATION) if( buildin_func[str_data[func].val].deprecated ){ ShowWarning( "Usage of deprecated script function '%s'.\n", get_str(func) ); ShowWarning( "This function was deprecated on '%s' and could become unavailable anytime soon.\n", buildin_func[str_data[func].val].deprecated ); script_reportsrc(st); } #endif if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error script_reportsrc(st); } else { ShowError("script:run_func: '%s' (id=%d type=%s) has no C function. please report this!!!\n", get_str(func), func, script_op2name(str_data[func].type)); script_reportsrc(st); st->state = END; } // Stack's datum are used when re-running functions [Eoe] if( st->state == RERUNLINE ) return 0; pop_stack(st, st->start, st->end); if( st->state == RETFUNC ) {// return from a user-defined function struct script_retinfo* ri; int olddefsp = st->stack->defsp; int nargs; pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO ) { ShowWarning("script:run_func: return without callfunc or callsub!\n"); script_reportsrc(st); st->state = END; return 1; } script_free_vars(st->stack->scope.vars); st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db); ri = st->stack->stack_data[st->stack->defsp-1].u.ri; nargs = ri->nargs; st->pos = ri->pos; st->script = ri->script; st->stack->scope.vars = ri->scope.vars; st->stack->scope.arrays = ri->scope.arrays; st->stack->defsp = ri->defsp; memset(ri, 0, sizeof(struct script_retinfo)); pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo st->state = GOTO; } return 0; } /*========================================== * script execution *------------------------------------------*/ void run_script(struct script_code *rootscript, int pos, int rid, int oid) { struct script_state *st; if( rootscript == NULL || pos < 0 ) return; // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS] // It is unclear how that can be triggered, so it needs the be traced/checked in more detail. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set. st = script_alloc_state(rootscript, pos, rid, oid); run_script_main(st); } /** * Free all related script code * @param code: Script code to free */ void script_stop_scriptinstances(struct script_code *code) { DBIterator *iter; struct script_state* st; if( !active_scripts ) return; // Don't even bother. iter = db_iterator(st_db); for( st = static_cast(dbi_first(iter)); dbi_exists(iter); st = static_cast(dbi_next(iter)) ) { if( st->script == code ) script_free_state(st); } dbi_destroy(iter); } /*========================================== * Timer function for sleep *------------------------------------------*/ int run_script_timer(int tid, unsigned int tick, int id, intptr_t data) { struct script_state *st = (struct script_state *)data; struct linkdb_node *node = (struct linkdb_node *)sleep_db; // If it was a player before going to sleep and there is still a unit attached to the script if( id != 0 && st->rid ){ struct map_session_data *sd = map_id2sd(st->rid); // Attached player is offline(logout) or another unit type(should not happen) if( !sd ){ st->rid = 0; st->state = END; // Character mismatch. Cancel execution. }else if( sd->status.char_id != id ){ ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id ); script_reportsrc(st); st->rid = 0; st->state = END; } } while (node && st->sleep.timer != INVALID_TIMER) { if ((int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer) { script_erase_sleepdb(node); st->sleep.timer = INVALID_TIMER; break; } node = node->next; } if(st->state != RERUNLINE) st->sleep.tick = 0; run_script_main(st); return 0; } /** * Remove sleep timers from the NPC * @param id: NPC ID */ void script_stop_sleeptimers(int id) { for (;;) { struct script_state *st = (struct script_state *)linkdb_erase(&sleep_db, (void *)__64BPRTSIZE(id)); if (!st) break; // No more sleep timers if (st->oid == id) script_free_state(st); } } /** * Delete the specified node from sleep_db * @param n: Linked list of sleep timers */ struct linkdb_node *script_erase_sleepdb(struct linkdb_node *n) { struct linkdb_node *retnode; if (!n) return NULL; if (!n->prev) sleep_db = n->next; else n->prev->next = n->next; if (n->next) n->next->prev = n->prev; retnode = n->next; aFree(n); return retnode; } /// Detaches script state from possibly attached character and restores it's previous script if any. /// /// @param st Script state to detach. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script. static void script_detach_state(struct script_state* st, bool dequeue_event) { struct map_session_data* sd; if(st->rid && (sd = map_id2sd(st->rid))!=NULL) { sd->st = st->bk_st; sd->npc_id = st->bk_npcid; sd->state.disable_atcommand_on_npc = 0; if(st->bk_st) { //Remove tag for removal. st->bk_st = NULL; st->bk_npcid = 0; } else if(dequeue_event) { #ifdef SECURE_NPCTIMEOUT /** * We're done with this NPC session, so we cancel the timer (if existent) and move on **/ if( sd->npc_idle_timer != INVALID_TIMER ) { delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer); sd->npc_idle_timer = INVALID_TIMER; } #endif npc_event_dequeue(sd); } } else if(st->bk_st) {// rid was set to 0, before detaching the script state ShowError("script_detach_state: Found previous script state without attached player (rid=%d, oid=%d, state=%d, bk_npcid=%d)\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid); script_reportsrc(st->bk_st); script_free_state(st->bk_st); st->bk_st = NULL; } } /// Attaches script state to possibly attached character and backups it's previous script, if any. /// /// @param st Script state to attach. static void script_attach_state(struct script_state* st) { struct map_session_data* sd; if(st->rid && (sd = map_id2sd(st->rid))!=NULL) { if(st!=sd->st) { if(st->bk_st) {// there is already a backup ShowDebug("script_attach_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid); } st->bk_st = sd->st; st->bk_npcid = sd->npc_id; } sd->st = st; sd->npc_id = st->oid; sd->npc_item_flag = st->npc_item_flag; // load default. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND)); #ifdef SECURE_NPCTIMEOUT if( sd->npc_idle_timer == INVALID_TIMER ) sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0); sd->npc_idle_tick = gettick(); #endif } } /*========================================== * The main part of the script execution *------------------------------------------*/ void run_script_main(struct script_state *st) { int cmdcount = script_config.check_cmdcount; int gotocount = script_config.check_gotocount; TBL_PC *sd; struct script_stack *stack = st->stack; script_attach_state(st); if(st->state == RERUNLINE) { run_func(st); if(st->state == GOTO) st->state = RUN; } else if(st->state != END) st->state = RUN; while(st->state == RUN) { enum c_op c = get_com(st->script->script_buf,&st->pos); switch(c){ case C_EOL: if( stack->defsp > stack->sp ) ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp); else pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value) break; case C_INT: push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos)); break; case C_POS: case C_NAME: push_val(stack,c,GETVALUE(st->script->script_buf,st->pos)); st->pos+=3; break; case C_ARG: push_val(stack,c,0); break; case C_STR: push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos)); while(st->script->script_buf[st->pos++]); break; case C_FUNC: run_func(st); if(st->state==GOTO){ st->state = RUN; if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){ ShowError("script:run_script_main: infinity loop !\n"); script_reportsrc(st); st->state=END; } } break; case C_REF: st->op2ref = 1; break; case C_NEG: case C_NOT: case C_LNOT: op_1(st ,c); break; case C_ADD: case C_SUB: case C_MUL: case C_DIV: case C_MOD: case C_EQ: case C_NE: case C_GT: case C_GE: case C_LT: case C_LE: case C_AND: case C_OR: case C_XOR: case C_LAND: case C_LOR: case C_R_SHIFT: case C_L_SHIFT: op_2(st, c); break; case C_OP3: op_3(st, c); break; case C_NOP: st->state=END; break; default: ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos); st->state=END; break; } if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){ ShowError("script:run_script_main: infinity loop !\n"); script_reportsrc(st); st->state=END; } } if(st->sleep.tick > 0) { //Restore previous script script_detach_state(st, false); //Delay execution sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish] st->sleep.charid = sd?sd->status.char_id:0; st->sleep.timer = add_timer(gettick() + st->sleep.tick, run_script_timer, st->sleep.charid, (intptr_t)st); linkdb_insert(&sleep_db, (void *)__64BPRTSIZE(st->oid), st); } else if(st->state != END && st->rid) { //Resume later (st is already attached to player). if(st->bk_st) { ShowWarning("Unable to restore stack! Double continuation!\n"); //Report BOTH scripts to see if that can help somehow. ShowDebug("Previous script (lost):\n"); script_reportsrc(st->bk_st); ShowDebug("Current script:\n"); script_reportsrc(st); script_free_state(st->bk_st); st->bk_st = NULL; } } else { //Dispose of script. if ((sd = map_id2sd(st->rid))!=NULL) { //Restore previous stack and save char. if(sd->state.using_fake_npc){ clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd); sd->state.using_fake_npc = 0; } //Restore previous script if any. script_detach_state(st, true); if (sd->vars_dirty) intif_saveregistry(sd); } script_free_state(st); } } int script_config_read(const char *cfgName) { int i; char line[1024],w1[1024],w2[1024]; FILE *fp; fp=fopen(cfgName,"r"); if(fp==NULL){ ShowError("File not found: %s\n", cfgName); return 1; } while(fgets(line, sizeof(line), fp)) { if(line[0] == '/' && line[1] == '/') continue; i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2); if(i!=2) continue; if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) { script_config.warn_func_mismatch_paramnum = config_switch(w2); } else if(strcmpi(w1,"check_cmdcount")==0) { script_config.check_cmdcount = config_switch(w2); } else if(strcmpi(w1,"check_gotocount")==0) { script_config.check_gotocount = config_switch(w2); } else if(strcmpi(w1,"input_min_value")==0) { script_config.input_min_value = config_switch(w2); } else if(strcmpi(w1,"input_max_value")==0) { script_config.input_max_value = config_switch(w2); } else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) { script_config.warn_func_mismatch_argtypes = config_switch(w2); } else if(strcmpi(w1,"import")==0){ script_config_read(w2); } else { ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName); } } fclose(fp); return 0; } /** * @see DBApply */ static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap) { struct script_code *code = static_cast(db_data2ptr(data)); if (code) script_free_code(code); return 0; } void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos) { struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus); if( script ) { int j; ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos ); if( j < EQI_MAX ) { //Single item autobonus current_equip_item_index = sd->equip_index[j]; current_equip_combo_pos = 0; } else { //Combo autobonus current_equip_item_index = -1; current_equip_combo_pos = pos; } run_script(script,0,sd->bl.id,0); } } void script_add_autobonus(const char *autobonus) { if( strdb_get(autobonus_db, autobonus) == NULL ) { struct script_code *script = parse_script(autobonus, "autobonus", 0, 0); if( script ) strdb_put(autobonus_db, autobonus, script); } } /// resets a temporary character array variable to given value void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value) { struct script_array *sa = NULL; struct reg_db *src = NULL; unsigned int i, *list = NULL, size = 0; int key; key = add_str(varname); if( !(src = script_array_src(NULL,sd,varname,NULL) ) ) return; if( value ) script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL); if( !(sa = static_cast(idb_get(src->arrays, key))) ) /* non-existent array, nothing to empty */ return; size = sa->size; list = script_array_cpy_list(sa); for(i = 0; i < size; i++) { set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL); } } /// sets a temporary character array variable element idx to given value /// @param refcache Pointer to an int variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be NULL if only one element is set. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache) { int key; if( idx >= SCRIPT_MAX_ARRAYSIZE ) { ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id); return; } key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname); set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL); if( refcache ) { // save to avoid repeated script->add_str calls refcache[0] = key; } } /** * Clears persistent variables from memory **/ int script_reg_destroy(DBKey key, DBData *data, va_list ap) { struct script_reg_state *src; if( data->type != DB_DATA_PTR ) // got no need for those! return 0; src = static_cast(db_data2ptr(data)); if( src->type ) { struct script_reg_str *p = (struct script_reg_str *)src; if( p->value ) aFree(p->value); ers_free(str_reg_ers,p); } else { ers_free(num_reg_ers,(struct script_reg_num*)src); } return 0; } /** * Clears a single persistent variable **/ void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data) { i64db_remove(sd->regs.vars, reg); if( data->type ) { struct script_reg_str *p = (struct script_reg_str*)data; if( p->value ) aFree(p->value); ers_free(str_reg_ers,p); } else { ers_free(num_reg_ers,(struct script_reg_num*)data); } } unsigned int *script_array_cpy_list(struct script_array *sa) { if( sa->size > generic_ui_array_size ) script_generic_ui_array_expand(sa->size); memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size); return generic_ui_array; } void script_generic_ui_array_expand (unsigned int plus) { generic_ui_array_size += plus + 100; RECREATE(generic_ui_array, unsigned int, generic_ui_array_size); } /*========================================== * Destructor *------------------------------------------*/ void do_final_script() { int i; DBIterator *iter; struct script_state *st; #ifdef DEBUG_HASH if (battle_config.etc_log) { FILE *fp = fopen("hash_dump.txt","wt"); if(fp) { int count[SCRIPT_HASH_SIZE]; int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items int n=0; int min=INT_MAX,max=0,zero=0; double mean=0.0f; double median=0.0f; ShowNotice("Dumping script str hash information to hash_dump.txt\n"); memset(count, 0, sizeof(count)); fprintf(fp,"num : hash : data_name\n"); fprintf(fp,"---------------------------------------------------------------\n"); for(i=LABEL_START; i count[i]) min = count[i]; // minimun count of collision if(max < count[i]) max = count[i]; // maximun count of collision if(count[i] == 0) zero++; ++count2[count[i]]; } fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n"); for( i=min; i <= max; ++i ){ fprintf(fp," %5d : %7d\n",i,count2[i]); mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in / } for( i=min; i <= max; ++i ){ n += count2[i]; if( n*2 >= SCRIPT_HASH_SIZE ) { if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n ) median = (i+i+1)/2.0f; else median = i; break; } } fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median); fclose(fp); } } #endif mapreg_final(); db_destroy(scriptlabel_db); userfunc_db->destroy(userfunc_db, db_script_free_code_sub); autobonus_db->destroy(autobonus_db, db_script_free_code_sub); ers_destroy(array_ers); if (generic_ui_array) aFree(generic_ui_array); iter = db_iterator(st_db); for( st = static_cast(dbi_first(iter)); dbi_exists(iter); st = static_cast(dbi_next(iter)) ) script_free_state(st); dbi_destroy(iter); if (str_data) aFree(str_data); if (str_buf) aFree(str_buf); for( i = 0; i < atcmd_binding_count; i++ ) { aFree(atcmd_binding[i]); } if( atcmd_binding_count != 0 ) aFree(atcmd_binding); ers_destroy(st_ers); ers_destroy(stack_ers); db_destroy(st_db); } /*========================================== * Initialization *------------------------------------------*/ void do_init_script(void) { st_db = idb_alloc(DB_OPT_BASE); userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0); scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50); autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0); st_ers = ers_new(sizeof(struct script_state), "script.cpp::st_ers", ERS_CACHE_OPTIONS); stack_ers = ers_new(sizeof(struct script_stack), "script.cpp::script_stack", ERS_OPT_FLEX_CHUNK); array_ers = ers_new(sizeof(struct script_array), "script.cpp:array_ers", ERS_CLEAN_OPTIONS); ers_chunk_size(st_ers, 10); ers_chunk_size(stack_ers, 10); active_scripts = 0; next_id = 0; mapreg_init(); } void script_reload(void) { int i; DBIterator *iter; struct script_state *st; userfunc_db->clear(userfunc_db, db_script_free_code_sub); db_clear(scriptlabel_db); // @commands (script based) // Clear bindings for( i = 0; i < atcmd_binding_count; i++ ) { aFree(atcmd_binding[i]); } if( atcmd_binding_count != 0 ) aFree(atcmd_binding); atcmd_binding_count = 0; iter = db_iterator(st_db); for( st = static_cast(dbi_first(iter)); dbi_exists(iter); st = static_cast(dbi_next(iter)) ) script_free_state(st); dbi_destroy(iter); db_clear(st_db); mapreg_reload(); } //----------------------------------------------------------------------------- // buildin functions // #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args, NULL } #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args, NULL } #define BUILDIN_DEF_DEPRECATED(x,args,deprecationdate) { buildin_ ## x , #x , args, deprecationdate } #define BUILDIN_DEF2_DEPRECATED(x,x2,args,deprecationdate) { buildin_ ## x , x2 , args, deprecationdate } #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st) ///////////////////////////////////////////////////////////////////// // NPC interaction // /// Appends a message to the npc dialog. /// If a dialog doesn't exist yet, one is created. /// /// mes ""; BUILDIN_FUNC(mes) { TBL_PC* sd; if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; if( !script_hasdata(st, 3) ) {// only a single line detected in the script clif_scriptmes(sd, st->oid, script_getstr(st, 2)); } else {// parse multiple lines as they exist int i; for( i = 2; script_hasdata(st, i); i++ ) { // send the message to the client clif_scriptmes(sd, st->oid, script_getstr(st, i)); } } st->mes_active = 1; // Invoking character has a NPC dialog box open. return SCRIPT_CMD_SUCCESS; } /// Displays the button 'next' in the npc dialog. /// The dialog text is cleared and the script continues when the button is pressed. /// /// next; BUILDIN_FUNC(next) { TBL_PC* sd; if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_WAIT; #endif st->state = STOP; clif_scriptnext(sd, st->oid); return SCRIPT_CMD_SUCCESS; } /// Clears the dialog and continues the script without a next button. /// /// clear; BUILDIN_FUNC(clear) { TBL_PC* sd; if (!script_rid2sd(sd)) return SCRIPT_CMD_FAILURE; clif_scriptclear(sd, st->oid); return SCRIPT_CMD_SUCCESS; } /// Ends the script and displays the button 'close' on the npc dialog. /// The dialog is closed when the button is pressed. /// /// close; BUILDIN_FUNC(close) { TBL_PC* sd; if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; if( !st->mes_active ) { TBL_NPC* nd = map_id2nd(st->oid); st->state = END; // Keep backwards compatibility. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown"); } else { st->state = CLOSE; st->mes_active = 0; } clif_scriptclose(sd, st->oid); return SCRIPT_CMD_SUCCESS; } /// Displays the button 'close' on the npc dialog. /// The dialog is closed and the script continues when the button is pressed. /// /// close2; BUILDIN_FUNC(close2) { TBL_PC* sd; if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; st->state = STOP; if( st->mes_active ) st->mes_active = 0; clif_scriptclose(sd, st->oid); return SCRIPT_CMD_SUCCESS; } /// Counts the number of valid and total number of options in 'str' /// If max_count > 0 the counting stops when that valid option is reached /// total is incremented for each option (NULL is supported) static int menu_countoptions(const char* str, int max_count, int* total) { int count = 0; int bogus_total; if( total == NULL ) total = &bogus_total; ++(*total); // initial empty options while( *str == ':' ) { ++str; ++(*total); } // count menu options while( *str != '\0' ) { ++count; --max_count; if( max_count == 0 ) break; while( *str != ':' && *str != '\0' ) ++str; while( *str == ':' ) { ++str; ++(*total); } } return count; } /// Displays a menu with options and goes to the target label. /// The script is stopped if cancel is pressed. /// Options with no text are not displayed in the client. /// /// Options can be grouped together, separated by the character ':' in the text: /// ex: menu "A:B:C",L_target; /// All these options go to the specified target label. /// /// The index of the selected option is put in the variable @menu. /// Indexes start with 1 and are consistent with grouped and empty options. /// ex: menu "A::B",-,"",L_Impossible,"C",-; /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5 /// /// NOTE: the client closes the npc dialog when cancel is pressed /// /// menu "",{,"",,...}; BUILDIN_FUNC(menu) { int i; const char* text; TBL_PC* sd; if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_MENU; #endif // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens if( sd->state.menu_or_input == 0 ) { struct StringBuf buf; if( script_lastdata(st) % 2 == 0 ) {// argument count is not even (1st argument is at index 2) ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1)); st->state = END; return SCRIPT_CMD_FAILURE; } StringBuf_Init(&buf); sd->npc_menu = 0; for( i = 2; i < script_lastdata(st); i += 2 ) { struct script_data* data; // menu options text = script_getstr(st, i); // target label data = script_getdata(st, i+1); if( !data_islabel(data) ) {// not a label StringBuf_Destroy(&buf); ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i); script_reportdata(data); st->state = END; return SCRIPT_CMD_FAILURE; } // append option(s) if( text[0] == '\0' ) continue;// empty string, ignore if( sd->npc_menu > 0 ) StringBuf_AppendStr(&buf, ":"); StringBuf_AppendStr(&buf, text); sd->npc_menu += menu_countoptions(text, 0, NULL); } st->state = RERUNLINE; sd->state.menu_or_input = 1; /** * menus beyond this length crash the client (see bugreport:6402) **/ if( StringBuf_Length(&buf) >= 2047 ) { struct npc_data * nd = map_id2nd(st->oid); char* menu; CREATE(menu, char, 2048); safestrncpy(menu, StringBuf_Value(&buf), 2047); ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf)); clif_scriptmenu(sd, st->oid, menu); aFree(menu); } else clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf)); StringBuf_Destroy(&buf); if( sd->npc_menu >= 0xff ) {// client supports only up to 254 entries; 0 is not used and 255 is reserved for cancel; excess entries are displayed but cause 'uint8' overflow ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu); script_reportsrc(st); } } else if( sd->npc_menu == 0xff ) {// Cancel was pressed sd->state.menu_or_input = 0; st->state = END; } else {// goto target label int menu = 0; sd->state.menu_or_input = 0; if( sd->npc_menu <= 0 ) { ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu); st->state = END; return SCRIPT_CMD_FAILURE; } // get target label for( i = 2; i < script_lastdata(st); i += 2 ) { text = script_getstr(st, i); sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu); if( sd->npc_menu <= 0 ) break;// entry found } if( sd->npc_menu > 0 ) {// Invalid selection ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu); st->state = END; return SCRIPT_CMD_FAILURE; } if( !data_islabel(script_getdata(st, i + 1)) ) {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input) ShowError("buildin_menu: Unexpected data in label argument\n"); script_reportdata(script_getdata(st, i + 1)); st->state = END; return SCRIPT_CMD_FAILURE; } pc_setreg(sd, add_str("@menu"), menu); st->pos = script_getnum(st, i + 1); st->state = GOTO; } return SCRIPT_CMD_SUCCESS; } /// Displays a menu with options and returns the selected option. /// Behaves like 'menu' without the target labels. /// /// select({,,...}) -> /// /// @see menu BUILDIN_FUNC(select) { int i; const char* text; TBL_PC* sd; if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_MENU; #endif if( sd->state.menu_or_input == 0 ) { struct StringBuf buf; StringBuf_Init(&buf); sd->npc_menu = 0; for( i = 2; i <= script_lastdata(st); ++i ) { text = script_getstr(st, i); if( sd->npc_menu > 0 ) StringBuf_AppendStr(&buf, ":"); StringBuf_AppendStr(&buf, text); sd->npc_menu += menu_countoptions(text, 0, NULL); } st->state = RERUNLINE; sd->state.menu_or_input = 1; /** * menus beyond this length crash the client (see bugreport:6402) **/ if( StringBuf_Length(&buf) >= 2047 ) { struct npc_data * nd = map_id2nd(st->oid); char* menu; CREATE(menu, char, 2048); safestrncpy(menu, StringBuf_Value(&buf), 2047); ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf)); clif_scriptmenu(sd, st->oid, menu); aFree(menu); } else clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf)); StringBuf_Destroy(&buf); if( sd->npc_menu >= 0xff ) { ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu); script_reportsrc(st); } } else if( sd->npc_menu == 0xff ) {// Cancel was pressed sd->state.menu_or_input = 0; st->state = END; } else {// return selected option int menu = 0; sd->state.menu_or_input = 0; for( i = 2; i <= script_lastdata(st); ++i ) { text = script_getstr(st, i); sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu); if( sd->npc_menu <= 0 ) break;// entry found } pc_setreg(sd, add_str("@menu"), menu); script_pushint(st, menu); st->state = RUN; } return SCRIPT_CMD_SUCCESS; } /// Displays a menu with options and returns the selected option. /// Behaves like 'menu' without the target labels, except when cancel is /// pressed. /// When cancel is pressed, the script continues and 255 is returned. /// /// prompt({,,...}) -> /// /// @see menu BUILDIN_FUNC(prompt) { int i; const char *text; TBL_PC* sd; if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_MENU; #endif if( sd->state.menu_or_input == 0 ) { struct StringBuf buf; StringBuf_Init(&buf); sd->npc_menu = 0; for( i = 2; i <= script_lastdata(st); ++i ) { text = script_getstr(st, i); if( sd->npc_menu > 0 ) StringBuf_AppendStr(&buf, ":"); StringBuf_AppendStr(&buf, text); sd->npc_menu += menu_countoptions(text, 0, NULL); } st->state = RERUNLINE; sd->state.menu_or_input = 1; /** * menus beyond this length crash the client (see bugreport:6402) **/ if( StringBuf_Length(&buf) >= 2047 ) { struct npc_data * nd = map_id2nd(st->oid); char* menu; CREATE(menu, char, 2048); safestrncpy(menu, StringBuf_Value(&buf), 2047); ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf)); clif_scriptmenu(sd, st->oid, menu); aFree(menu); } else clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf)); StringBuf_Destroy(&buf); if( sd->npc_menu >= 0xff ) { ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu); script_reportsrc(st); } } else if( sd->npc_menu == 0xff ) {// Cancel was pressed sd->state.menu_or_input = 0; pc_setreg(sd, add_str("@menu"), 0xff); script_pushint(st, 0xff); st->state = RUN; } else {// return selected option int menu = 0; sd->state.menu_or_input = 0; for( i = 2; i <= script_lastdata(st); ++i ) { text = script_getstr(st, i); sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu); if( sd->npc_menu <= 0 ) break;// entry found } pc_setreg(sd, add_str("@menu"), menu); script_pushint(st, menu); st->state = RUN; } return SCRIPT_CMD_SUCCESS; } ///////////////////////////////////////////////////////////////////// // ... // /// Jumps to the target script label. /// /// goto