diff options
Diffstat (limited to 'src/map')
-rw-r--r-- | src/map/npc.c | 15 | ||||
-rw-r--r-- | src/map/script.c | 246 | ||||
-rw-r--r-- | src/map/script.h | 30 |
3 files changed, 179 insertions, 112 deletions
diff --git a/src/map/npc.c b/src/map/npc.c index 8d71ee20a..d82bbdfd9 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -3749,14 +3749,15 @@ void npc_read_event_script(void) break; } - if( (p=strchr(p,':')) && p && strcmpi(name,p)==0 ) + if( (p=strchr(p,':')) && strcmp(name,p) == 0 ) { -#ifdef ENABLE_CASE_CHECK - if( strcmp(name, p) != 0 ) DeprecationWarning2("npc_read_event_script", p, name, config[i].event_name); // TODO -#endif // ENABLE_CASE_CHECK script_event[i].event[count] = ed; script_event[i].event_name[count] = key.str; script_event[i].event_count++; +#ifdef ENABLE_CASE_CHECK + } else if( p && strcasecmp(name, p) == 0 ) { + DeprecationWarning2("npc_read_event_script", p, name, config[i].event_name); // TODO +#endif // ENABLE_CASE_CHECK } } dbi_destroy(iter); @@ -3902,7 +3903,7 @@ bool npc_unloadfile( const char* filepath ) { bool found = false; for( nd = dbi_first(iter); dbi_exists(iter); nd = dbi_next(iter) ) { - if( nd->path && strcasecmp(nd->path,filepath) == 0 ) { + if( nd->path && strcasecmp(nd->path,filepath) == 0 ) { // FIXME: This can break in case-sensitive file systems found = true; npc->unload_duplicates(nd);/* unload any npcs which could duplicate this but be in a different file */ npc->unload(nd, true); @@ -3994,8 +3995,8 @@ int do_init_npc(bool minimal) { for( i = MAX_NPC_CLASS2_START; i < MAX_NPC_CLASS2_END; i++ ) npc_viewdb2[i - MAX_NPC_CLASS2_START].class_ = i; - npc->ev_db = stridb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, EVENT_NAME_LENGTH); - npc->ev_label_db = stridb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, NAME_LENGTH); + npc->ev_db = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, EVENT_NAME_LENGTH); + npc->ev_label_db = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, NAME_LENGTH); npc->name_db = strdb_alloc(DB_OPT_BASE, NAME_LENGTH); npc->path_db = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, 0); diff --git a/src/map/script.c b/src/map/script.c index 75ef841d0..3982513d5 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -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,121 @@ 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); +} + +bool 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 true; // 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; } +bool script_global_casecheck_add_str(const char *p) { + return script_casecheck_add_str_sub(&script->global_casecheck, p); +} + +bool 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 + bool alreadyexists = false; if( (strncmp(p, ".@", 2) == 0) ) // Local scope vars are checked separately to decrease false positives - script->local_casecheck_add_str(p, h); + alreadyexists = script->local_casecheck.add_str(p); + else { + alreadyexists = script->global_casecheck.add_str(p); + if( alreadyexists ) { + 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 + alreadyexists = false; + } + } #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 +518,12 @@ 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( alreadyexists ) { + DeprecationWarning2("script_add_str", p, script->get_str(i), script->parser_current_file); // TODO + } +#endif // ENABLE_CASE_CHECK // grow list if neccessary if( script->str_num >= script->str_data_size ) { @@ -2244,7 +2290,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; } @@ -2261,7 +2307,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; } @@ -2279,7 +2325,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; } @@ -2396,7 +2442,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; } @@ -3952,10 +3998,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); @@ -3993,6 +4039,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) ) { @@ -18793,14 +18843,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 } diff --git a/src/map/script.h b/src/map/script.h index aace388ff..4d2c59d92 100644 --- a/src/map/script.h +++ b/src/map/script.h @@ -22,8 +22,8 @@ struct eri; **/ // TODO: Remove temporary code #define ENABLE_CASE_CHECK -#define DeprecationWarning(func, bad, good, file, line) ShowWarning("%s: use of deprecated keyword '%s' (use '%s' instead) in file '%s', line '%d'.\n", (func), (bad), (good), (file), (line)); -#define DeprecationWarning2(func, bad, good, where) ShowWarning("%s: detected possible use of wrong case in a script. Found '%s', probably meant to be '%s' (in '%s'). This will become fatal in a near future.\n", (func), (bad), (good), (where)); +#define DeprecationWarning(func, bad, good, file, line) ShowError("%s: use of deprecated keyword '%s' (use '%s' instead) in file '%s', line '%d'.\n", (func), (bad), (good), (file), (line)); +#define DeprecationWarning2(func, bad, good, where) ShowError("%s: detected possible use of wrong case in a script. Found '%s', probably meant to be '%s' (in '%s').\n", (func), (bad), (good), (where)); #define disp_deprecation_message(func, good, p) disp_warning_message(func": use of deprecated keyword (use '"good"' instead).", (p)); #define NUM_WHISPER_VAR 10 @@ -443,6 +443,19 @@ struct script_syntax_data { int index; // Number of the syntax used in the script }; +struct casecheck_data { + struct str_data_struct *str_data; + int str_data_size; // size of the data + int str_num; // next id to be assigned + // str_buf holds the strings themselves + char *str_buf; + int str_size; // size of the buffer + int str_pos; // next position to be assigned + int str_hash[SCRIPT_HASH_SIZE]; + bool (*add_str) (const char* p); + void (*clear) (void); +}; + /** * Interface **/ @@ -642,16 +655,9 @@ struct script_interface { int (*run_func) (struct script_state *st); const char *(*getfuncname) (struct script_state *st); // for ENABLE_CASE_CHECK - struct str_data_struct *local_casecheck_str_data; - int local_casecheck_str_data_size; // size of the data - int local_casecheck_str_num; // next id to be assigned - // str_buf holds the strings themselves - char *local_casecheck_str_buf; - int local_casecheck_str_size; // size of the buffer - int local_casecheck_str_pos; // next position to be assigned - int local_casecheck_str_hash[SCRIPT_HASH_SIZE]; - bool (*local_casecheck_add_str) (const char* p, int h); - void (*local_casecheck_clear) (void); + unsigned int (*calc_hash_ci) (const char *p); + struct casecheck_data local_casecheck; + struct casecheck_data global_casecheck; // end ENABLE_CASE_CHECK }; |