diff options
Diffstat (limited to 'src/map/script.c')
-rw-r--r-- | src/map/script.c | 734 |
1 files changed, 403 insertions, 331 deletions
diff --git a/src/map/script.c b/src/map/script.c index d0b69d594..074348ef0 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -275,14 +275,14 @@ void script_reportfunc(struct script_state* st) /*========================================== * Output error message *------------------------------------------*/ -void disp_error_message2(const char *mes,const char *pos,int report) -{ +static void disp_error_message2(const char *mes,const char *pos,int report) analyzer_noreturn; +static void disp_error_message2(const char *mes,const char *pos,int report) { script->error_msg = aStrdup(mes); script->error_pos = pos; script->error_report = report; longjmp( script->error_jump, 1 ); } -#define disp_error_message(mes,pos) (script->disp_error_message2((mes),(pos),1)) +#define disp_error_message(mes,pos) (disp_error_message2((mes),(pos),1)) void disp_warning_message(const char *mes, const char *pos) { script->warning(script->parser_current_src,script->parser_current_file,script->parser_current_line,mes,pos); @@ -301,13 +301,47 @@ void check_event(struct script_state *st, const char *evt) /*========================================== * Hashes the input string *------------------------------------------*/ -unsigned int calc_hash(const char* p) -{ +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)(*p++)); +#elif defined(SCRIPT_HASH_SDBM) + h = 0; + while( *p ) // hash*65599 + c + h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)(*p++)); +#elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash + h = 0; + while( *p ) { + unsigned int g; + h = ( h << 4 ) + ((unsigned char)(*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)(*p++); +#endif + + return h % SCRIPT_HASH_SIZE; +} + +/*========================================== + * Hashes the input string in a case insensitive way + *------------------------------------------*/ +unsigned int calc_hash_ci(const char* p) { + unsigned int h = 0; +#ifdef ENABLE_CASE_CHECK + +#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; @@ -315,12 +349,11 @@ unsigned int calc_hash(const char* p) h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++)); #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash h = 0; - while( *p ){ + while( *p ) { unsigned int g; h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++)); g = h & 0xF0000000; - if( g ) - { + if( g ) { h ^= g >> 24; h &= ~g; } @@ -331,6 +364,7 @@ unsigned int calc_hash(const char* p) h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++); #endif +#endif // ENABLE_CASE_CHECK return h % SCRIPT_HASH_SIZE; } @@ -352,15 +386,7 @@ int script_search_str(const char* p) int i; for( i = script->str_hash[script->calc_hash(p)]; i != 0; i = script->str_data[i].next ) { - if( strcasecmp(script->get_str(i),p) == 0 ) { -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, ".@", 2) != 0 // Local scope vars are checked separately to decrease false positives - && strcasecmp(p, "disguise") != 0 && strcasecmp(p, "Poison_Spore") != 0 - && strcasecmp(p, "PecoPeco_Egg") != 0 && strcasecmp(p, "Soccer_Ball") != 0 - && strcasecmp(p, "Horn") != 0 && strcasecmp(p, "Treasure_Box_") != 0 - && strcasecmp(p, "Lord_of_Death") != 0 - && strcmp(script->get_str(i),p) != 0 ) DeprecationWarning2("script_search_str", p, script->get_str(i), script->parser_current_file); // TODO -#endif // ENABLE_CASE_CHECK + if( strcmp(script->get_str(i),p) == 0 ) { return i; } } @@ -368,107 +394,107 @@ int script_search_str(const char* p) return -1; } -void script_local_casecheck_clear(void) { +void script_casecheck_clear_sub(struct casecheck_data *ccd) { #ifdef ENABLE_CASE_CHECK - if (script->local_casecheck_str_data) { - aFree(script->local_casecheck_str_data); - script->local_casecheck_str_data = NULL; - } - script->local_casecheck_str_data_size = 0; - script->local_casecheck_str_num = 1; - if (script->local_casecheck_str_buf) { - aFree(script->local_casecheck_str_buf); - script->local_casecheck_str_buf = NULL; - } - script->local_casecheck_str_pos = 0; - script->local_casecheck_str_size = 0; - memset(script->local_casecheck_str_hash, 0, sizeof(script->local_casecheck_str_hash)); + if (ccd->str_data) { + aFree(ccd->str_data); + ccd->str_data = NULL; + } + ccd->str_data_size = 0; + ccd->str_num = 1; + if (ccd->str_buf) { + aFree(ccd->str_buf); + ccd->str_buf = NULL; + } + ccd->str_pos = 0; + ccd->str_size = 0; + memset(ccd->str_hash, 0, sizeof(ccd->str_hash)); #endif // ENABLE_CASE_CHECK } -bool script_local_casecheck_add_str(const char *p, int h) { +void script_global_casecheck_clear(void) { + script_casecheck_clear_sub(&script->global_casecheck); +} + +void script_local_casecheck_clear(void) { + script_casecheck_clear_sub(&script->local_casecheck); +} + +const char *script_casecheck_add_str_sub(struct casecheck_data *ccd, const char *p) { #ifdef ENABLE_CASE_CHECK int len, i; - if( script->local_casecheck_str_hash[h] == 0 ) { //empty bucket, add new node here - script->local_casecheck_str_hash[h] = script->local_casecheck_str_num; + int h = script->calc_hash_ci(p); + if( ccd->str_hash[h] == 0 ) { //empty bucket, add new node here + ccd->str_hash[h] = ccd->str_num; } else { const char *s = NULL; - for( i = script->local_casecheck_str_hash[h]; ; i = script->local_casecheck_str_data[i].next ) { - Assert( i >= 0 && i < script->local_casecheck_str_size ); - s = script->local_casecheck_str_buf+script->local_casecheck_str_data[i].str; + for( i = ccd->str_hash[h]; ; i = ccd->str_data[i].next ) { + Assert( i >= 0 && i < ccd->str_size ); + s = ccd->str_buf+ccd->str_data[i].str; if( strcasecmp(s,p) == 0 ) { - if ( strcmp(s,p) != 0 ) { - DeprecationWarning2("script_add_str", p, s, script->parser_current_file); - return true; - } - return false; // string already in list + return s; // string already in list } - if( script->local_casecheck_str_data[i].next == 0 ) + if( ccd->str_data[i].next == 0 ) break; // reached the end } // append node to end of list - script->local_casecheck_str_data[i].next = script->local_casecheck_str_num; + ccd->str_data[i].next = ccd->str_num; } // grow list if neccessary - if( script->local_casecheck_str_num >= script->local_casecheck_str_data_size ) { - script->local_casecheck_str_data_size += 1280; - RECREATE(script->local_casecheck_str_data,struct str_data_struct,script->local_casecheck_str_data_size); - memset(script->local_casecheck_str_data + (script->local_casecheck_str_data_size - 1280), '\0', 1280); + if( ccd->str_num >= ccd->str_data_size ) { + ccd->str_data_size += 1280; + RECREATE(ccd->str_data,struct str_data_struct,ccd->str_data_size); + memset(ccd->str_data + (ccd->str_data_size - 1280), '\0', 1280); } len=(int)strlen(p); // grow string buffer if neccessary - while( script->local_casecheck_str_pos+len+1 >= script->local_casecheck_str_size ) { - script->local_casecheck_str_size += 10240; - RECREATE(script->local_casecheck_str_buf,char,script->local_casecheck_str_size); - memset(script->local_casecheck_str_buf + (script->local_casecheck_str_size - 10240), '\0', 10240); - } - - safestrncpy(script->local_casecheck_str_buf+script->local_casecheck_str_pos, p, len+1); - script->local_casecheck_str_data[script->local_casecheck_str_num].type = C_NOP; - script->local_casecheck_str_data[script->local_casecheck_str_num].str = script->local_casecheck_str_pos; - script->local_casecheck_str_data[script->local_casecheck_str_num].val = 0; - script->local_casecheck_str_data[script->local_casecheck_str_num].next = 0; - script->local_casecheck_str_data[script->local_casecheck_str_num].func = NULL; - script->local_casecheck_str_data[script->local_casecheck_str_num].backpatch = -1; - script->local_casecheck_str_data[script->local_casecheck_str_num].label = -1; - script->local_casecheck_str_pos += len+1; - - script->local_casecheck_str_num++; + while( ccd->str_pos+len+1 >= ccd->str_size ) { + ccd->str_size += 10240; + RECREATE(ccd->str_buf,char,ccd->str_size); + memset(ccd->str_buf + (ccd->str_size - 10240), '\0', 10240); + } + + safestrncpy(ccd->str_buf+ccd->str_pos, p, len+1); + ccd->str_data[ccd->str_num].type = C_NOP; + ccd->str_data[ccd->str_num].str = ccd->str_pos; + ccd->str_data[ccd->str_num].val = 0; + ccd->str_data[ccd->str_num].next = 0; + ccd->str_data[ccd->str_num].func = NULL; + ccd->str_data[ccd->str_num].backpatch = -1; + ccd->str_data[ccd->str_num].label = -1; + ccd->str_pos += len+1; + + ccd->str_num++; #endif // ENABLE_CASE_CHECK - return false; + return NULL; +} + +const char *script_global_casecheck_add_str(const char *p) { + return script_casecheck_add_str_sub(&script->global_casecheck, p); +} + +const char *script_local_casecheck_add_str(const char *p) { + return script_casecheck_add_str_sub(&script->local_casecheck, p); } /// Stores a copy of the string and returns its id. /// If an identical string is already present, returns its id instead. int script_add_str(const char* p) { - int i, h; - int len; - - h = script->calc_hash(p); - + int i, len, h = script->calc_hash(p); #ifdef ENABLE_CASE_CHECK - if( (strncmp(p, ".@", 2) == 0) ) // Local scope vars are checked separately to decrease false positives - script->local_casecheck_add_str(p, h); + const char *existingentry = NULL; #endif // ENABLE_CASE_CHECK if( script->str_hash[h] == 0 ) {// empty bucket, add new node here script->str_hash[h] = script->str_num; } else {// scan for end of list, or occurence of identical string for( i = script->str_hash[h]; ; i = script->str_data[i].next ) { - if( strcasecmp(script->get_str(i),p) == 0 ) { -#ifdef ENABLE_CASE_CHECK - if( (strncmp(p, ".@", 2) != 0) // Local scope vars are checked separately to decrease false positives - && strcasecmp(p, "disguise") != 0 && strcasecmp(p, "Poison_Spore") != 0 - && strcasecmp(p, "PecoPeco_Egg") != 0 && strcasecmp(p, "Soccer_Ball") != 0 - && strcasecmp(p, "Horn") != 0 && strcasecmp(p, "Treasure_Box_") != 0 - && strcasecmp(p, "Lord_of_Death") != 0 - && strcmp(script->get_str(i),p) != 0 ) DeprecationWarning2("script_add_str", p, script->get_str(i), script->parser_current_file); // TODO -#endif // ENABLE_CASE_CHECK + if( strcmp(script->get_str(i),p) == 0 ) { return i; // string already in list } if( script->str_data[i].next == 0 ) @@ -478,6 +504,25 @@ int script_add_str(const char* p) // append node to end of list script->str_data[i].next = script->str_num; } + +#ifdef ENABLE_CASE_CHECK + if( (strncmp(p, ".@", 2) == 0) ) // Local scope vars are checked separately to decrease false positives + existingentry = script->local_casecheck.add_str(p); + else { + existingentry = script->global_casecheck.add_str(p); + if( existingentry ) { + if( strcasecmp(p, "disguise") == 0 || strcasecmp(p, "Poison_Spore") == 0 + || strcasecmp(p, "PecoPeco_Egg") == 0 || strcasecmp(p, "Soccer_Ball") == 0 + || strcasecmp(p, "Horn") == 0 || strcasecmp(p, "Treasure_Box_") == 0 + || strcasecmp(p, "Lord_of_Death") == 0 + ) // Known duplicates, don't bother warning the user + existingentry = NULL; + } + } + if( existingentry ) { + DeprecationWarning2("script_add_str", p, existingentry, script->parser_current_file); // TODO + } +#endif // ENABLE_CASE_CHECK // grow list if neccessary if( script->str_num >= script->str_data_size ) { @@ -683,7 +728,7 @@ const char* skip_word(const char* p) { /// @see skip_word /// @see script->add_str int add_word(const char* p) { - int len; + size_t len; int i; // Check for a word @@ -1159,7 +1204,7 @@ const char* script_parse_subexpr(const char* p,int limit) { } } - if( (op=C_ADD_PRE,p[0]=='+'&&p[1]=='+') || (op=C_SUB_PRE,p[0]=='-'&&p[1]=='-') ) { // Pre ++ -- operators + if( (p[0]=='+' && p[1]=='+') /* C_ADD_PRE */ || (p[0]=='-'&&p[1]=='-') /* C_SUB_PRE */ ) { // Pre ++ -- operators p=script->parse_variable(p); } else if( (op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~') ) { // Unary - ! ~ operators p=script->parse_subexpr(p+1,11); @@ -1343,13 +1388,10 @@ const char* parse_syntax(const char* p) switch(*p) { case 'B': case 'b': - if(p2 - p == 5 && !strncasecmp(p,"break",5)) { + if( p2 - p == 5 && strncmp(p,"break",5) == 0 ) { // break Processing char label[256]; int pos = script->syntax.curly_count - 1; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "break", 5) != 0 ) disp_deprecation_message("parse_syntax", "break", p); // TODO -#endif // ENABLE_CASE_CHECK while(pos >= 0) { if(script->syntax.curly[pos].type == TYPE_DO) { sprintf(label,"goto __DO%x_FIN;",script->syntax.curly[pos].index); @@ -1379,16 +1421,17 @@ const char* parse_syntax(const char* p) // Closing decision if, for , while p = script->parse_syntax_close(p + 1); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 5 && strncasecmp(p, "break", 5) == 0 ) { + disp_deprecation_message("parse_syntax", "break", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'c': case 'C': - if(p2 - p == 4 && !strncasecmp(p,"case",4)) { + if( p2 - p == 4 && strncmp(p, "case", 4) == 0 ) { //Processing case int pos = script->syntax.curly_count-1; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "case", 4) != 0 ) disp_deprecation_message("parse_syntax", "case", p); // TODO -#endif // ENABLE_CASE_CHECK if(pos < 0 || script->syntax.curly[pos].type != TYPE_SWITCH) { disp_error_message("parse_syntax: unexpected 'case' ",p); return p+1; @@ -1416,7 +1459,7 @@ const char* parse_syntax(const char* p) // check whether case label is integer or not if(is_number(p)) { //Numeric value - v = strtol(p,&np,0); + v = (int)strtol(p,&np,0); if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word p++; p = script->skip_word(p); @@ -1425,7 +1468,7 @@ const char* parse_syntax(const char* p) } else { //Check for constants p2 = script->skip_word(p); - v = p2-p; // length of word at p2 + v = (int)(size_t) (p2-p); // length of word at p2 memcpy(label,p,v); label[v]='\0'; if( !script->get_constant(label, &v) ) @@ -1461,13 +1504,10 @@ const char* parse_syntax(const char* p) script->syntax.curly[pos].count++; } return p + 1; - } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) { + } else if( p2 - p == 8 && strncmp(p, "continue", 8) == 0 ) { // Processing continue char label[256]; int pos = script->syntax.curly_count - 1; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "continue", 8) != 0 ) disp_deprecation_message("parse_syntax", "continue", p); // TODO -#endif // ENABLE_CASE_CHECK while(pos >= 0) { if(script->syntax.curly[pos].type == TYPE_DO) { sprintf(label,"goto __DO%x_NXT;",script->syntax.curly[pos].index); @@ -1495,16 +1535,19 @@ const char* parse_syntax(const char* p) //Closing decision if, for , while p = script->parse_syntax_close(p + 1); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 4 && strncasecmp(p, "case", 4) == 0 ) { + disp_deprecation_message("parse_syntax", "case", p); // TODO + } else if( p2 - p == 8 && strncasecmp(p, "continue", 8) == 0 ) { + disp_deprecation_message("parse_syntax", "continue", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'd': case 'D': - if(p2 - p == 7 && !strncasecmp(p,"default",7)) { + if( p2 - p == 7 && strncmp(p, "default", 7) == 0 ) { // Switch - default processing int pos = script->syntax.curly_count-1; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "default", 7) != 0 ) disp_deprecation_message("parse_syntax", "default", p); // TODO -#endif // ENABLE_CASE_CHECK if(pos < 0 || script->syntax.curly[pos].type != TYPE_SWITCH) { disp_error_message("parse_syntax: unexpected 'default'",p); } else if(script->syntax.curly[pos].flag) { @@ -1536,12 +1579,9 @@ const char* parse_syntax(const char* p) script->syntax.curly[pos].count++; } return p + 1; - } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) { + } else if( p2 - p == 2 && strncmp(p, "do", 2) == 0 ) { int l; char label[256]; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "do", 2) != 0 ) disp_deprecation_message("parse_syntax", "do", p); // TODO -#endif // ENABLE_CASE_CHECK p=script->skip_space(p2); script->syntax.curly[script->syntax.curly_count].type = TYPE_DO; @@ -1554,17 +1594,20 @@ const char* parse_syntax(const char* p) script->set_label(l,script->pos,p); script->syntax.curly_count++; return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 7 && strncasecmp(p, "default", 7) == 0 ) { + disp_deprecation_message("parse_syntax", "default", p); // TODO + } else if( p2 - p == 2 && strncasecmp(p, "do", 2) == 0 ) { + disp_deprecation_message("parse_syntax", "do", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'f': case 'F': - if(p2 - p == 3 && !strncasecmp(p,"for",3)) { + if( p2 - p == 3 && strncmp(p, "for", 3) == 0 ) { int l; char label[256]; int pos = script->syntax.curly_count; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "for", 3) != 0 ) disp_deprecation_message("parse_syntax", "for", p); // TODO -#endif // ENABLE_CASE_CHECK script->syntax.curly[script->syntax.curly_count].type = TYPE_FOR; script->syntax.curly[script->syntax.curly_count].count = 1; script->syntax.curly[script->syntax.curly_count].index = script->syntax.index++; @@ -1635,14 +1678,10 @@ const char* parse_syntax(const char* p) l=script->add_str(label); script->set_label(l,script->pos,p); return p; - } - else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 ) - {// internal script function + } else if( p2 - p == 8 && strncmp(p, "function", 8) == 0 ) { + // internal script function const char *func_name; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "function", 8) != 0 ) disp_deprecation_message("parse_syntax", "function", p); // TODO -#endif // ENABLE_CASE_CHECK func_name = script->skip_space(p2); p = script->skip_word(func_name); if( p == func_name ) @@ -1700,16 +1739,19 @@ const char* parse_syntax(const char* p) { disp_error_message("expect ';' or '{' at function syntax",p); } +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 3 && strncasecmp(p, "for", 3) == 0 ) { + disp_deprecation_message("parse_syntax", "for", p); // TODO + } else if( p2 - p == 8 && strncasecmp(p, "function", 8) == 0 ) { + disp_deprecation_message("parse_syntax", "function", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'i': case 'I': - if(p2 - p == 2 && !strncasecmp(p,"if",2)) { + if( p2 - p == 2 && strncmp(p, "if", 2) == 0 ) { // If process char label[256]; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "if", 2) != 0 ) disp_deprecation_message("parse_syntax", "if", p); // TODO -#endif // ENABLE_CASE_CHECK p=script->skip_space(p2); if(*p != '(') { //Prevent if this {} non-c script->syntax. from Rayce (jA) disp_error_message("need '('",p); @@ -1727,16 +1769,17 @@ const char* parse_syntax(const char* p) script->addl(script->add_str(label)); script->addc(C_FUNC); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 2 && strncasecmp(p, "if", 2) == 0 ) { + disp_deprecation_message("parse_syntax", "if", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 's': case 'S': - if(p2 - p == 6 && !strncasecmp(p,"switch",6)) { + if( p2 - p == 6 && strncmp(p, "switch", 6) == 0 ) { // Processing of switch () char label[256]; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "switch", 6) != 0 ) disp_deprecation_message("parse_syntax", "switch", p); // TODO -#endif // ENABLE_CASE_CHECK p=script->skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); @@ -1757,16 +1800,17 @@ const char* parse_syntax(const char* p) } script->addc(C_FUNC); return p + 1; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 6 && strncasecmp(p, "switch", 6) == 0 ) { + disp_deprecation_message("parse_syntax", "switch", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'w': case 'W': - if(p2 - p == 5 && !strncasecmp(p,"while",5)) { + if( p2 - p == 5 && strncmp(p, "while", 5) == 0 ) { int l; char label[256]; -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "while", 5) != 0 ) disp_deprecation_message("parse_syntax", "while", p); // TODO -#endif // ENABLE_CASE_CHECK p=script->skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); @@ -1790,6 +1834,10 @@ const char* parse_syntax(const char* p) script->addl(script->add_str(label)); script->addc(C_FUNC); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 5 && strncasecmp(p, "while", 5) == 0 ) { + disp_deprecation_message("parse_syntax", "while", p); // TODO +#endif // ENABLE_CASE_CHECK } break; } @@ -1840,14 +1888,11 @@ const char* parse_syntax_close_sub(const char* p,int* flag) script->syntax.curly[pos].count++; p = script->skip_space(p); p2 = script->skip_word(p); - if(!script->syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) { -#ifdef ENABLE_CASE_CHECK - if( strncmp(p, "else", 4) != 0 ) disp_deprecation_message("parse_syntax", "else", p); // TODO -#endif // ENABLE_CASE_CHECK + if( !script->syntax.curly[pos].flag && p2 - p == 4 && strncmp(p, "else", 4) == 0 ) { // else or else - if p = script->skip_space(p2); p2 = script->skip_word(p); - if(p2 - p == 2 && !strncasecmp(p,"if",2)) { + if( p2 - p == 2 && strncmp(p, "if", 2) == 0 ) { // else - if p=script->skip_space(p2); if(*p != '(') { @@ -1862,6 +1907,10 @@ const char* parse_syntax_close_sub(const char* p,int* flag) script->addc(C_FUNC); *flag = 0; return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 2 && strncasecmp(p, "if", 2) == 0 ) { + disp_deprecation_message("parse_syntax", "if", p); // TODO +#endif // ENABLE_CASE_CHECK } else { // else if(!script->syntax.curly[pos].flag) { @@ -1870,6 +1919,10 @@ const char* parse_syntax_close_sub(const char* p,int* flag) return p; } } +#ifdef ENABLE_CASE_CHECK + } else if( !script->syntax.curly[pos].flag && p2 - p == 4 && strncasecmp(p, "else", 4) == 0 ) { + disp_deprecation_message("parse_syntax", "else", p); // TODO +#endif // ENABLE_CASE_CHECK } // Close if script->syntax.curly_count--; @@ -1883,8 +1936,6 @@ const char* parse_syntax_close_sub(const char* p,int* flag) } return p; } else if(script->syntax.curly[pos].type == TYPE_DO) { - int l; - char label[256]; const char *p2; if(script->syntax.curly[pos].flag) { @@ -1897,12 +1948,13 @@ const char* parse_syntax_close_sub(const char* p,int* flag) // Skip to the end point if the condition is false p = script->skip_space(p); p2 = script->skip_word(p); - if(p2 - p != 5 || strncasecmp(p,"while",5)) - disp_error_message("parse_syntax: need 'while'",p); - + if( p2 - p != 5 || strncmp(p, "while", 5) != 0 ) { #ifdef ENABLE_CASE_CHECK - if( strncmp(p, "while", 5) != 0 ) disp_deprecation_message("parse_syntax", "while", p); // TODO + if( p2 - p == 5 && strncasecmp(p, "while", 5) == 0 ) disp_deprecation_message("parse_syntax", "while", p); // TODO #endif // ENABLE_CASE_CHECK + disp_error_message("parse_syntax: need 'while'",p); + } + p = script->skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); @@ -1969,10 +2021,7 @@ const char* parse_syntax_close_sub(const char* p,int* flag) script->set_label(l,script->pos,p); script->syntax.curly_count--; return p; - } else if(script->syntax.curly[script->syntax.curly_count-1].type == TYPE_USERFUNC) { - int pos = script->syntax.curly_count-1; - char label[256]; - int l; + } else if(script->syntax.curly[pos].type == TYPE_USERFUNC) { // Back sprintf(label,"return;"); script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; @@ -2221,7 +2270,6 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o if( setjmp( script->error_jump ) != 0 ) { //Restore program state when script has problems. [from jA] - int i; const int size = ARRAYLENGTH(script->syntax.curly); if( script->error_report ) script->error(src,file,line,script->error_msg,script->error_pos); @@ -2235,7 +2283,7 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o for(i=0; i<size; i++) linkdb_final(&script->syntax.curly[i].case_label); #ifdef ENABLE_CASE_CHECK - script->local_casecheck_clear(); + script->local_casecheck.clear(); #endif // ENABLE_CASE_CHECK return NULL; } @@ -2252,7 +2300,7 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o script->size = 0; script->buf = NULL; #ifdef ENABLE_CASE_CHECK - script->local_casecheck_clear(); + script->local_casecheck.clear(); #endif // ENABLE_CASE_CHECK return NULL; } @@ -2270,7 +2318,7 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o script->size = 0; script->buf = NULL; #ifdef ENABLE_CASE_CHECK - script->local_casecheck_clear(); + script->local_casecheck.clear(); #endif // ENABLE_CASE_CHECK return NULL; } @@ -2295,7 +2343,7 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o disp_error_message("unexpected end of script",p); // Special handling only label tmpp=script->skip_space(script->skip_word(p)); - if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){ + if(*tmpp==':' && !(strncmp(p,"default:",8) == 0 && p + 7 == tmpp)) { i=script->add_word(p); script->set_label(i,script->pos,p); if( script->parse_options&SCRIPT_USE_LABEL_DB ) @@ -2387,7 +2435,7 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o code->script_size = script->size; code->script_vars = NULL; #ifdef ENABLE_CASE_CHECK - script->local_casecheck_clear(); + script->local_casecheck.clear(); #endif // ENABLE_CASE_CHECK return code; } @@ -3637,7 +3685,7 @@ void run_script_main(struct script_state *st) { break; } if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){ - ShowError("run_script: infinity loop !\n"); + ShowError("run_script: too many opeartions being processed non-stop !\n"); script->reportsrc(st); st->state=END; } @@ -3943,10 +3991,10 @@ void do_final_script(void) { if( script->word_buf != NULL ) aFree(script->word_buf); - if( script->local_casecheck_str_buf ) - aFree(script->local_casecheck_str_buf); - if( script->local_casecheck_str_data ) - aFree(script->local_casecheck_str_data); +#ifdef ENABLE_CASE_CHECK + script->global_casecheck.clear(); + script->local_casecheck.clear(); +#endif // ENABLE_CASE_CHECK ers_destroy(script->st_ers); ers_destroy(script->stack_ers); @@ -3964,7 +4012,7 @@ void do_init_script(bool minimal) { script->userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0); script->autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0); - script->st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_OPT_NONE); + script->st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_OPT_CLEAN); script->stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_NONE); ers_chunk_size(script->st_ers, 10); @@ -3984,6 +4032,10 @@ int script_reload(void) { DBIterator *iter; struct script_state *st; +#ifdef ENABLE_CASE_CHECK + script->global_casecheck.clear(); +#endif // ENABLE_CASE_CHECK + iter = db_iterator(script->st_db); for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) { @@ -4696,7 +4748,7 @@ BUILDIN(warp) else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0) ret = pc->setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT); else - ret = pc->setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT); + ret = pc->setpos(sd,mapindex->name2id(str),x,y,CLR_OUTSIGHT); if( ret ) { ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y); @@ -4773,7 +4825,7 @@ BUILDIN(areawarp) if( strcmp(str,"Random") == 0 ) index = 0; - else if( !(index=mapindex_name2id(str)) ) + else if( !(index=mapindex->name2id(str)) ) return true; map->foreachinarea(script->buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3); @@ -4837,7 +4889,7 @@ BUILDIN(warpchar) { if(strcmp(str, "SavePoint") == 0) pc->setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT); else - pc->setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT); + pc->setpos(sd, mapindex->name2id(str), x, y, CLR_TELEPORT); return true; } @@ -4852,7 +4904,7 @@ BUILDIN(warpparty) TBL_PC *pl_sd; struct party_data* p; int type; - int mapindex; + int map_index; int i; const char* str = script_getstr(st,2); @@ -4880,19 +4932,19 @@ BUILDIN(warpparty) if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online return true; pl_sd = p->data[i].sd; - mapindex = pl_sd->mapindex; + map_index = pl_sd->mapindex; x = pl_sd->bl.x; y = pl_sd->bl.y; break; case 4: - mapindex = mapindex_name2id(str); + map_index = mapindex->name2id(str); break; case 2: //"SavePoint" uses save point of the currently attached player if (( sd = script->rid2sd(st) ) == NULL ) return true; default: - mapindex = 0; + map_index = 0; break; } @@ -4923,7 +4975,7 @@ BUILDIN(warpparty) case 3: // Leader case 4: // m,x,y if(!map->list[pl_sd->bl.m].flag.noreturn && !map->list[pl_sd->bl.m].flag.nowarp) - pc->setpos(pl_sd,mapindex,x,y,CLR_TELEPORT); + pc->setpos(pl_sd,map_index,x,y,CLR_TELEPORT); break; } } @@ -4983,7 +5035,7 @@ BUILDIN(warpguild) break; case 3: // m,x,y if(!map->list[pl_sd->bl.m].flag.noreturn && !map->list[pl_sd->bl.m].flag.nowarp) - pc->setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT); + pc->setpos(pl_sd,mapindex->name2id(str),x,y,CLR_TELEPORT); break; } } @@ -6107,10 +6159,8 @@ BUILDIN(getitem) *------------------------------------------*/ BUILDIN(getitem2) { - int nameid,amount,get_count,i,flag = 0, offset = 0; + int nameid,amount,i,flag = 0, offset = 0; int iden,ref,attr,c1,c2,c3,c4, bound = 0; - struct item_data *item_data; - struct item item_tmp; TBL_PC *sd; struct script_data *data; @@ -6163,8 +6213,10 @@ BUILDIN(getitem2) } if(nameid > 0) { + struct item item_tmp; + struct item_data *item_data = itemdb->exists(nameid); + int get_count; memset(&item_tmp,0,sizeof(item_tmp)); - item_data=itemdb->exists(nameid); if (item_data == NULL) return -1; if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){ @@ -7938,7 +7990,7 @@ BUILDIN(getgmlevel) if( sd == NULL ) return true;// no player attached, report source - script_pushint(st, pc->get_group_level(sd)); + script_pushint(st, pc_get_group_level(sd)); return true; } @@ -8278,7 +8330,7 @@ BUILDIN(savepoint) { str = script_getstr(st,2); x = script_getnum(st,3); y = script_getnum(st,4); - mapid = mapindex_name2id(str); + mapid = mapindex->name2id(str); if( mapid ) pc->setsavepoint(sd, mapid, x, y); @@ -9274,12 +9326,12 @@ BUILDIN(announce) { } if (fontColor) - clif->broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target); + clif->broadcast2(bl, mes, (int)strlen(mes)+1, (unsigned int)strtoul(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target); else clif->broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target); } else { if (fontColor) - intif->broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY); + intif->broadcast2(mes, (int)strlen(mes)+1, (unsigned int)strtoul(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY); else intif->broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK); } @@ -9298,7 +9350,7 @@ int buildin_announce_sub(struct block_list *bl, va_list ap) short fontAlign = (short)va_arg(ap, int); short fontY = (short)va_arg(ap, int); if (fontColor) - clif->broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF); + clif->broadcast2(bl, mes, len, (unsigned int)strtoul(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF); else clif->broadcast(bl, mes, len, type, SELF); return 0; @@ -9432,11 +9484,11 @@ BUILDIN(getusersname) sd = script->rid2sd(st); if (!sd) return true; - group_level = pc->get_group_level(sd); + group_level = pc_get_group_level(sd); iter = mapit_getallusers(); for( pl_sd = (TBL_PC*)mapit->first(iter); mapit->exists(iter); pl_sd = (TBL_PC*)mapit->next(iter) ) { - if (pc->has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc->get_group_level(pl_sd) > group_level) + if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level) continue; // skip hidden sessions /* Temporary fix for bugreport:1023. @@ -10376,7 +10428,7 @@ BUILDIN(warpwaitingpc) { else if( strcmp(map_name,"SavePoint") == 0 ) pc->setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT); else - pc->setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT); + pc->setpos(sd, mapindex->name2id(map_name), x, y, CLR_OUTSIGHT); } mapreg->setreg(script->add_str("$@warpwaitingpcnum"), i); return true; @@ -10438,7 +10490,7 @@ BUILDIN(isloggedin) { *------------------------------------------*/ BUILDIN(setmapflagnosave) { int16 m,x,y; - unsigned short mapindex; + unsigned short map_index; const char *str,*str2; str=script_getstr(st,2); @@ -10446,11 +10498,11 @@ BUILDIN(setmapflagnosave) { x=script_getnum(st,4); y=script_getnum(st,5); m = map->mapname2mapid(str); - mapindex = mapindex_name2id(str2); + map_index = mapindex->name2id(str2); - if(m >= 0 && mapindex) { + if(m >= 0 && map_index) { map->list[m].flag.nosave=1; - map->list[m].save.map=mapindex; + map->list[m].save.map=map_index; map->list[m].save.x=x; map->list[m].save.y=y; } @@ -10611,13 +10663,14 @@ BUILDIN(setmapflag) { case MF_NORETURN: map->list[m].flag.noreturn = 1; break; case MF_NOWARPTO: map->list[m].flag.nowarpto = 1; break; case MF_NIGHTMAREDROP: map->list[m].flag.pvp_nightmaredrop = 1; break; - case MF_ZONE: { - char zone[6] = "zone\0"; - char empty[1] = "\0"; - char params[MAP_ZONE_MAPFLAG_LENGTH]; - memcpy(params, val2, MAP_ZONE_MAPFLAG_LENGTH); - npc->parse_mapflag(map->list[m].name, empty, zone, params, empty, empty, empty); - } + case MF_ZONE: + if( val2 ) { + char zone[6] = "zone\0"; + char empty[1] = "\0"; + char params[MAP_ZONE_MAPFLAG_LENGTH]; + memcpy(params, val2, MAP_ZONE_MAPFLAG_LENGTH); + npc->parse_mapflag(map->list[m].name, empty, zone, params, empty, empty, empty); + } break; case MF_NOCOMMAND: map->list[m].nocommand = (val <= 0) ? 100 : val; break; case MF_NODROP: map->list[m].flag.nodrop = 1; break; @@ -11002,7 +11055,7 @@ BUILDIN(flagemblem) { BUILDIN(getcastlename) { - const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL); + const char* mapname = mapindex->getmapname(script_getstr(st,2),NULL); struct guild_castle* gc = guild->mapname2gc(mapname); const char* name = (gc) ? gc->castle_name : ""; script_pushstrcopy(st,name); @@ -11011,7 +11064,7 @@ BUILDIN(getcastlename) BUILDIN(getcastledata) { - const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL); + const char *mapname = mapindex->getmapname(script_getstr(st,2),NULL); int index = script_getnum(st,3); struct guild_castle *gc = guild->mapname2gc(mapname); @@ -11054,7 +11107,7 @@ BUILDIN(getcastledata) BUILDIN(setcastledata) { - const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL); + const char *mapname = mapindex->getmapname(script_getstr(st,2),NULL); int index = script_getnum(st,3); int value = script_getnum(st,4); struct guild_castle *gc = guild->mapname2gc(mapname); @@ -11285,7 +11338,7 @@ BUILDIN(mapwarp) { if((m=map->mapname2mapid(mapname))< 0) return true; - if(!(index=mapindex_name2id(str))) + if(!(index=mapindex->name2id(str))) return true; switch(check_val){ @@ -11460,7 +11513,7 @@ BUILDIN(getfatherid) BUILDIN(warppartner) { int x,y; - unsigned short mapindex; + unsigned short map_index; const char *str; TBL_PC *sd=script->rid2sd(st); TBL_PC *p_sd=NULL; @@ -11475,9 +11528,9 @@ BUILDIN(warppartner) x=script_getnum(st,3); y=script_getnum(st,4); - mapindex = mapindex_name2id(str); - if (mapindex) { - pc->setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT); + map_index = mapindex->name2id(str); + if (map_index) { + pc->setpos(p_sd,map_index,x,y,CLR_OUTSIGHT); script_pushint(st,1); } else script_pushint(st,0); @@ -11597,7 +11650,7 @@ BUILDIN(delwall) { /// 2 - current hp /// BUILDIN(guardianinfo) { - const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL); + const char* mapname = mapindex->getmapname(script_getstr(st,2),NULL); int id = script_getnum(st,3); int type = script_getnum(st,4); @@ -13443,7 +13496,7 @@ BUILDIN(insertchar) if(index < 0) index = 0; else if(index > len) - index = len; + index = (int)len; output = (char*)aMalloc(len + 2); @@ -13626,9 +13679,9 @@ BUILDIN(implode) { struct script_data* data = script_getdata(st, 2); const char *glue = NULL, *name, *temp; - int32 glue_len = 0, array_size, id; - size_t len = 0; - int i, k = 0; + int32 array_size, id; + size_t len = 0, glue_len = 0, k = 0; + int i; TBL_PC* sd = NULL; @@ -13723,13 +13776,14 @@ BUILDIN(implode) //------------------------------------------------------- BUILDIN(sprintf) { - unsigned int len, argc = 0, arg = 0, buf2_len = 0; + unsigned int argc = 0, arg = 0; const char* format; char* p; char* q; char* buf = NULL; char* buf2 = NULL; struct script_data* data; + size_t len, buf2_len = 0; StringBuf final_buf; // Fetch init data @@ -13854,7 +13908,7 @@ BUILDIN(sprintf) // Implements C sscanf. //------------------------------------------------------- BUILDIN(sscanf){ - unsigned int argc, arg = 0, len; + unsigned int argc, arg = 0; struct script_data* data; struct map_session_data* sd = NULL; const char* str; @@ -13865,6 +13919,7 @@ BUILDIN(sscanf){ char* buf_p; char* ref_str = NULL; int ref_int; + size_t len; // Get data str = script_getstr(st, 2); @@ -15481,7 +15536,7 @@ BUILDIN(getvariableofnpc) BUILDIN(warpportal) { int spx; int spy; - unsigned short mapindex; + unsigned short map_index; int tpx; int tpy; struct skill_unit_group* group; @@ -15496,11 +15551,11 @@ BUILDIN(warpportal) { spx = script_getnum(st,2); spy = script_getnum(st,3); - mapindex = mapindex_name2id(script_getstr(st, 4)); + map_index = mapindex->name2id(script_getstr(st, 4)); tpx = script_getnum(st,5); tpy = script_getnum(st,6); - if( mapindex == 0 ) + if( map_index == 0 ) return true;// map not found group = skill->unitsetting(bl, AL_WARP, 4, spx, spy, 0); @@ -15508,7 +15563,7 @@ BUILDIN(warpportal) { return true;// failed group->val1 = (group->val1<<16)|(short)0; group->val2 = (tpx<<16) | tpy; - group->val3 = mapindex; + group->val3 = map_index; return true; } @@ -15780,13 +15835,13 @@ BUILDIN(readbook) BUILDIN(questinfo) { struct npc_data *nd = map->id2nd(st->oid); - int quest, icon, job, color = 0; + int quest_id, icon, job, color = 0; struct questinfo qi; if( nd == NULL || nd->bl.m == -1 ) return true; - quest = script_getnum(st, 2); + quest_id = script_getnum(st, 2); icon = script_getnum(st, 3); #if PACKETVER >= 20120410 @@ -15799,7 +15854,7 @@ BUILDIN(questinfo) icon = icon + 1; #endif - qi.quest_id = quest; + qi.quest_id = quest_id; qi.icon = (unsigned char)icon; qi.nd = nd; @@ -15831,20 +15886,20 @@ BUILDIN(questinfo) return true; } -BUILDIN(setquest) -{ +BUILDIN(setquest) { struct map_session_data *sd = script->rid2sd(st); unsigned short i; - - if (!sd) - return false; + int quest_id; + nullpo_retr(false,sd); + + quest_id = script_getnum(st, 2); - quest->add(sd, script_getnum(st, 2)); + quest->add(sd, quest_id); // If questinfo is set, remove quest bubble once quest is set. for(i = 0; i < map->list[sd->bl.m].qi_count; i++) { struct questinfo *qi = &map->list[sd->bl.m].qi_data[i]; - if( qi->quest_id == script_getnum(st, 2) ) { + if( qi->quest_id == quest_id ) { #if PACKETVER >= 20120410 clif->quest_show_event(sd, &qi->nd->bl, 9999, 0); #else @@ -15856,8 +15911,7 @@ BUILDIN(setquest) return true; } -BUILDIN(erasequest) -{ +BUILDIN(erasequest) { struct map_session_data *sd = script->rid2sd(st); nullpo_retr(false,sd); @@ -15865,8 +15919,7 @@ BUILDIN(erasequest) return true; } -BUILDIN(completequest) -{ +BUILDIN(completequest) { struct map_session_data *sd = script->rid2sd(st); nullpo_retr(false,sd); @@ -15874,8 +15927,7 @@ BUILDIN(completequest) return true; } -BUILDIN(changequest) -{ +BUILDIN(changequest) { struct map_session_data *sd = script->rid2sd(st); nullpo_retr(false,sd); @@ -15883,15 +15935,14 @@ BUILDIN(changequest) return true; } -BUILDIN(checkquest) -{ +BUILDIN(checkquest) { struct map_session_data *sd = script->rid2sd(st); - quest_check_type type = HAVEQUEST; + enum quest_check_type type = HAVEQUEST; nullpo_retr(false,sd); if( script_hasdata(st, 3) ) - type = (quest_check_type)script_getnum(st, 3); + type = (enum quest_check_type)script_getnum(st, 3); script_pushint(st, quest->check(sd, script_getnum(st, 2), type)); @@ -15937,7 +15988,7 @@ BUILDIN(waitingroom2bg) { struct npc_data *nd; struct chat_data *cd; const char *map_name, *ev = "", *dev = ""; - int x, y, i, mapindex = 0, bg_id, n; + int x, y, i, map_index = 0, bg_id, n; struct map_session_data *sd; if( script_hasdata(st,7) ) @@ -15953,8 +16004,8 @@ BUILDIN(waitingroom2bg) { map_name = script_getstr(st,2); if( strcmp(map_name,"-") != 0 ) { - mapindex = mapindex_name2id(map_name); - if( mapindex == 0 ) + map_index = mapindex->name2id(map_name); + if( map_index == 0 ) { // Invalid Map script_pushint(st,0); return true; @@ -15966,7 +16017,7 @@ BUILDIN(waitingroom2bg) { ev = script_getstr(st,5); // Logout Event dev = script_getstr(st,6); // Die Event - if( (bg_id = bg->create(mapindex, x, y, ev, dev)) == 0 ) + if( (bg_id = bg->create(map_index, x, y, ev, dev)) == 0 ) { // Creation failed script_pushint(st,0); return true; @@ -15991,11 +16042,11 @@ BUILDIN(waitingroom2bg_single) { struct npc_data *nd; struct chat_data *cd; struct map_session_data *sd; - int x, y, mapindex, bg_id; + int x, y, map_index, bg_id; bg_id = script_getnum(st,2); map_name = script_getstr(st,3); - if( (mapindex = mapindex_name2id(map_name)) == 0 ) + if( (map_index = mapindex->name2id(map_name)) == 0 ) return true; // Invalid Map x = script_getnum(st,4); @@ -16010,7 +16061,7 @@ BUILDIN(waitingroom2bg_single) { if( bg->team_join(bg_id, sd) ) { - pc->setpos(sd, mapindex, x, y, CLR_TELEPORT); + pc->setpos(sd, map_index, x, y, CLR_TELEPORT); script_pushint(st,1); } else @@ -16035,16 +16086,16 @@ BUILDIN(bg_team_setxy) BUILDIN(bg_warp) { - int x, y, mapindex, bg_id; + int x, y, map_index, bg_id; const char* map_name; bg_id = script_getnum(st,2); map_name = script_getstr(st,3); - if( (mapindex = mapindex_name2id(map_name)) == 0 ) + if( (map_index = mapindex->name2id(map_name)) == 0 ) return true; // Invalid Map x = script_getnum(st,4); y = script_getnum(st,5); - bg->team_warp(bg_id, mapindex, x, y); + bg->team_warp(bg_id, map_index, x, y); return true; } @@ -16447,11 +16498,11 @@ BUILDIN(has_instance) { } int buildin_instance_warpall_sub(struct block_list *bl,va_list ap) { struct map_session_data *sd = ((TBL_PC*)bl); - int mapindex = va_arg(ap,int); + int map_index = va_arg(ap,int); int x = va_arg(ap,int); int y = va_arg(ap,int); - pc->setpos(sd,mapindex,x,y,CLR_TELEPORT); + pc->setpos(sd,map_index,x,y,CLR_TELEPORT); return 0; } @@ -16460,7 +16511,7 @@ BUILDIN(instance_warpall) { int instance_id = -1; const char *mapn; int x, y; - int mapindex; + int map_index; mapn = script_getstr(st,2); x = script_getnum(st,3); @@ -16476,9 +16527,9 @@ BUILDIN(instance_warpall) { if( (m = map->mapname2mapid(mapn)) < 0 || (map->list[m].flag.src4instance && (m = instance->mapid2imapid(m, instance_id)) < 0) ) return true; - mapindex = map_id2index(m); + map_index = map_id2index(m); - map->foreachininstance(script->buildin_instance_warpall_sub, instance_id, BL_PC,mapindex,x,y); + map->foreachininstance(script->buildin_instance_warpall_sub, instance_id, BL_PC,map_index,x,y); return true; } @@ -16649,7 +16700,7 @@ BUILDIN(progressbar) sd->progressbar.timeout = timer->gettick() + second*1000; sd->state.workinprogress = 3; - clif->progressbar(sd, strtol(color, (char **)NULL, 0), second); + clif->progressbar(sd, (unsigned int)strtoul(color, (char **)NULL, 0), second); return true; } @@ -17804,12 +17855,12 @@ BUILDIN(packageitem) { /* returns created team id or -1 when fails */ BUILDIN(bg_create_team) { const char *map_name, *ev = "", *dev = "";//ev and dev will be dropped. - int x, y, mapindex = 0, bg_id; + int x, y, map_index = 0, bg_id; map_name = script_getstr(st,2); if( strcmp(map_name,"-") != 0 ) { - mapindex = mapindex_name2id(map_name); - if( mapindex == 0 ) { // Invalid Map + map_index = mapindex->name2id(map_name); + if( map_index == 0 ) { // Invalid Map script_pushint(st,0); return true; } @@ -17818,7 +17869,7 @@ BUILDIN(bg_create_team) { x = script_getnum(st,3); y = script_getnum(st,4); - if( (bg_id = bg->create(mapindex, x, y, ev, dev)) == 0 ) { // Creation failed + if( (bg_id = bg->create(map_index, x, y, ev, dev)) == 0 ) { // Creation failed script_pushint(st,-1); } else script_pushint(st,bg_id); @@ -17970,45 +18021,97 @@ BUILDIN(instance_set_respawn) { BUILDIN(deletepset); #endif -bool script_hp_add(char *name, char *args, bool (*func)(struct script_state *st)) { - int n = script->add_str(name), i = 0; - - if( script->str_data[n].type == C_FUNC ) { - script->str_data[n].func = func; - i = script->str_data[n].val; - if( args ) { - int slen = strlen(args); - if( script->buildin[i] ) { - aFree(script->buildin[i]); - } - CREATE(script->buildin[i], char, slen + 1); - safestrncpy(script->buildin[i], args, slen + 1); - } else { - if( script->buildin[i] ) - aFree(script->buildin[i]); - script->buildin[i] = NULL; +/** + * Adds a built-in script function. + * + * @param buildin Script function data + * @param force Whether to override an existing function with the same name + * (i.e. a plugin overriding a built-in function) + * @return Whether the function was successfully added. + */ +bool script_add_builtin(const struct script_function *buildin, bool override) { + int n = 0, offset = 0; + size_t slen; + if( !buildin ) { + return false; + } + if( buildin->arg ) { + // 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 + char *p = buildin->arg; + while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p; + while( *p == '?' ) ++p; + if( *p == '*' ) ++p; + if( *p != 0 ) { + ShowWarning("add_builtin: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin->name, buildin->arg); + return false; } + } + if( !buildin->name || *script->skip_word(buildin->name) != 0 ) { + ShowWarning("add_builtin: ignoring function with invalid name \"%s\" (must be a word).\n", buildin->name); + return false; + } + if ( !buildin->func ) { + ShowWarning("add_builtin: ignoring function \"%s\" with invalid source function.\n", buildin->name); + return false; + } + slen = buildin->arg ? strlen(buildin->arg) : 0; + n = script->add_str(buildin->name); + if( !override && script->str_data[n].func && script->str_data[n].func != buildin->func ) { + return false; /* something replaced it, skip. */ + } + + if( override && script->str_data[n].type == C_FUNC ) { + // Overriding + offset = script->str_data[n].val; + if( script->buildin[offset] ) + aFree(script->buildin[offset]); + script->buildin[offset] = NULL; } else { - i = script->buildin_count; + // Adding new function + if( strcmp(buildin->name, "setr") == 0 ) script->buildin_set_ref = n; + else if( strcmp(buildin->name, "callsub") == 0 ) script->buildin_callsub_ref = n; + else if( strcmp(buildin->name, "callfunc") == 0 ) script->buildin_callfunc_ref = n; + else if( strcmp(buildin->name, "getelementofarray") == 0 ) script->buildin_getelementofarray_ref = n; + + offset = script->buildin_count; + script->str_data[n].type = C_FUNC; - script->str_data[n].val = i; - script->str_data[n].func = func; - + script->str_data[n].val = offset; + + // Note: This is a no-op if script->buildin is already large enough + // (it'll only have effect when a plugin adds a new command) RECREATE(script->buildin, char *, ++script->buildin_count); - - /* we only store the arguments, its the only thing used out of this */ - if( args != NULL ) { - int slen = strlen(args); - CREATE(script->buildin[i], char, slen + 1); - safestrncpy(script->buildin[i], args, slen + 1); - } else - script->buildin[i] = NULL; } - + + script->str_data[n].func = buildin->func; + + /* we only store the arguments, its the only thing used out of this */ + if( slen ) { + CREATE(script->buildin[offset], char, slen + 1); + safestrncpy(script->buildin[offset], buildin->arg, slen + 1); + } else { + script->buildin[offset] = NULL; + } + return true; } +bool script_hp_add(char *name, char *args, bool (*func)(struct script_state *st)) { + struct script_function buildin; + buildin.name = name; + buildin.arg = args; + buildin.func = func; + return script->add_builtin(&buildin, true); +} + #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args } #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args } void script_parse_builtin(void) { @@ -18507,52 +18610,11 @@ void script_parse_builtin(void) { BUILDIN_DEF(bg_join_team,"i?"), BUILDIN_DEF(bg_match_over,"s?"), }; - int i,n, len = ARRAYLENGTH(BUILDIN), start = script->buildin_count; - char* p; - RECREATE(script->buildin, char *, start + len); + int i, len = ARRAYLENGTH(BUILDIN); + RECREATE(script->buildin, char *, script->buildin_count + len); // Pre-alloc to speed up + memset(script->buildin + script->buildin_count, '\0', sizeof(char *) * len); for( i = 0; i < len; 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 - p = BUILDIN[i].arg; - while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p; - while( *p == '?' ) ++p; - if( *p == '*' ) ++p; - if( *p != 0 ){ - ShowWarning("parse_builtin: ignoring function \"%s\" with invalid arg \"%s\".\n", BUILDIN[i].name, BUILDIN[i].arg); - } else if( *script->skip_word(BUILDIN[i].name) != 0 ){ - ShowWarning("parse_builtin: ignoring function with invalid name \"%s\" (must be a word).\n", BUILDIN[i].name); - } else { - int slen = strlen(BUILDIN[i].arg), offset = start + i; - n = script->add_str(BUILDIN[i].name); - - if (!strcmp(BUILDIN[i].name, "setr")) script->buildin_set_ref = n; - else if (!strcmp(BUILDIN[i].name, "callsub")) script->buildin_callsub_ref = n; - else if (!strcmp(BUILDIN[i].name, "callfunc")) script->buildin_callfunc_ref = n; - else if (!strcmp(BUILDIN[i].name, "getelementofarray") ) script->buildin_getelementofarray_ref = n; - - if( script->str_data[n].func && script->str_data[n].func != BUILDIN[i].func ) - continue;/* something replaced it, skip. */ - - script->str_data[n].type = C_FUNC; - script->str_data[n].val = offset; - script->str_data[n].func = BUILDIN[i].func; - - /* we only store the arguments, its the only thing used out of this */ - if( slen ) { - CREATE(script->buildin[offset], char, slen + 1); - safestrncpy(script->buildin[offset], BUILDIN[i].arg, slen + 1); - } else - script->buildin[offset] = NULL; - - script->buildin_count++; - - } + script->add_builtin(&BUILDIN[i], false); } } #undef BUILDIN_DEF @@ -18645,6 +18707,7 @@ void script_defaults(void) { /* parse */ script->parse = parse_script; + script->add_builtin = script_add_builtin; script->parse_builtin = script_parse_builtin; script->skip_space = script_skip_space; script->error = script_error; @@ -18703,7 +18766,6 @@ void script_defaults(void) { script->reportsrc = script_reportsrc; script->reportdata = script_reportdata; script->reportfunc = script_reportfunc; - script->disp_error_message2 = disp_error_message2; script->disp_warning_message = disp_warning_message; script->check_event = check_event; script->calc_hash = calc_hash; @@ -18784,14 +18846,24 @@ void script_defaults(void) { script->config.ontouch2_name = "OnTouch";//ontouch2_name (run whenever a char walks into the OnTouch area) // for ENABLE_CASE_CHECK - script->local_casecheck_add_str = script_local_casecheck_add_str; - script->local_casecheck_clear = script_local_casecheck_clear; - script->local_casecheck_str_data = NULL; - script->local_casecheck_str_data_size = 0; - script->local_casecheck_str_num = 1; - script->local_casecheck_str_buf = NULL; - script->local_casecheck_str_size = 0; - script->local_casecheck_str_pos = 0; - memset(script->local_casecheck_str_hash, 0, sizeof(script->local_casecheck_str_hash)); + script->calc_hash_ci = calc_hash_ci; + script->local_casecheck.add_str = script_local_casecheck_add_str; + script->local_casecheck.clear = script_local_casecheck_clear; + script->local_casecheck.str_data = NULL; + script->local_casecheck.str_data_size = 0; + script->local_casecheck.str_num = 1; + script->local_casecheck.str_buf = NULL; + script->local_casecheck.str_size = 0; + script->local_casecheck.str_pos = 0; + memset(script->local_casecheck.str_hash, 0, sizeof(script->local_casecheck.str_hash)); + script->global_casecheck.add_str = script_global_casecheck_add_str; + script->global_casecheck.clear = script_global_casecheck_clear; + script->global_casecheck.str_data = NULL; + script->global_casecheck.str_data_size = 0; + script->global_casecheck.str_num = 1; + script->global_casecheck.str_buf = NULL; + script->global_casecheck.str_size = 0; + script->global_casecheck.str_pos = 0; + memset(script->global_casecheck.str_hash, 0, sizeof(script->global_casecheck.str_hash)); // end ENABLE_CASE_CHECK } |