diff options
Diffstat (limited to 'src/map/npc.c')
-rw-r--r-- | src/map/npc.c | 120 |
1 files changed, 80 insertions, 40 deletions
diff --git a/src/map/npc.c b/src/map/npc.c index 7b616282f..0d3bcffd3 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -1586,10 +1586,10 @@ static int npc_parse_script(char *w1,char *w2,char *w3,char *w4,char *first_line { int x,y,dir=0,m,xs=0,ys=0,class=0; // [Valaris] thanks to fov char mapname[24]; - unsigned char *srcbuf=NULL,*script; + char *srcbuf=NULL,*script; int srcsize=65536; int startline=0; - unsigned char line[1024]; + char line[1024]; int i; struct npc_data *nd; int evflag=0; @@ -1602,51 +1602,91 @@ static int npc_parse_script(char *w1,char *w2,char *w3,char *w4,char *first_line if(strcmp(w1,"-")==0){ x=0;y=0;m=-1; }else{ - // 引数の個数チェック - if (sscanf(w1,"%[^,],%d,%d,%d",mapname,&x,&y,&dir) != 4 || + // 引数の個数チェック + if (sscanf(w1,"%[^,],%d,%d,%d",mapname,&x,&y,&dir) != 4 || ( strcmp(w2,"script")==0 && strchr(w4,',')==NULL) ) { - printf("bad script line : %s\n",w3); - return 1; - } - m = map_mapname2mapid(mapname); + printf("bad script line : %s\n",w3); + return 1; + } + m = map_mapname2mapid(mapname); } - + if(strcmp(w2,"script")==0){ // スクリプトの解析 + // { , } の入れ子許したらこっちでも簡易解析しないといけなくなったりもする + int curly_count = 0; + int string_flag = 0; + int j; srcbuf=(char *)aCalloc(srcsize,sizeof(char)); - if (strchr(first_line,'{')) { - strcpy(srcbuf,strchr(first_line,'{')); - startline=*lines; - } else - srcbuf[0]=0; - while(1) { - for(i=strlen(srcbuf)-1;i>=0 && isspace(srcbuf[i]);i--); - if (i>=0 && srcbuf[i]=='}') - break; - fgets(line,1020,fp); - (*lines)++; - if (feof(fp)) - break; - if (strlen(srcbuf)+strlen(line)+1>=srcsize) { - srcsize += 65536; - srcbuf = (char *)aRealloc(srcbuf, srcsize); - memset(srcbuf + srcsize - 65536, '\0', 65536); - } - if (srcbuf[0]!='{') { - if (strchr(line,'{')) { - strcpy(srcbuf,strchr(line,'{')); - startline=*lines; - } + if (strchr(first_line,'{')) { + strcpy(srcbuf,strchr(first_line,'{')); + startline=*lines; } else - strcat(srcbuf,line); - } - script=parse_script(srcbuf,startline); - if (script==NULL) { - // script parse error? - free(srcbuf); - return 1; - } + srcbuf[0]=0; + while(1) { + fgets(line,1020,fp); + (*lines)++; + if (feof(fp)) + break; + // line の中に文字列 , {} が含まれているか調査 + i = strlen(line); + for(j = 0; j < i ; j++) { + if(string_flag) { + if(line[j] == '\"' && (j <= 0 || line[j-1] != '\\')) { + string_flag = 0; + } + } else { + if(line[j] == '\"') { + string_flag = 1; + } else if(line[j] == '}') { + if(curly_count == 0) { + // 抜けるのはfor だけ + break; + } else { + curly_count--; + } + } else if(line[j] == '{') { + curly_count++; + } else if(line[j] == '/' && line[j+1] == '/') { + // コメント + break; + } else if(*(unsigned char*)(line + j) >= 0x80) { + // 全角文字 + j++; + } + } + } + if (strlen(srcbuf)+strlen(line)+1>=srcsize) { + srcsize += 65536; + srcbuf = (char *)aRealloc(srcbuf, srcsize); + memset(srcbuf + srcsize - 65536, '\0', 65536); + } + if (srcbuf[0]!='{') { + if (strchr(line,'{')) { + strcpy(srcbuf,strchr(line,'{')); + startline=*lines; + } + } else + strcat(srcbuf,line); + if(!string_flag && line[j] == '}' && curly_count == 0) { + break; + } + } + if(curly_count > 0) { + printf("warning: Missing right curly at line %d\n",*lines); + script=NULL; + exit(1); + } else { + // printf("Ok line %d\n",*lines); + script=parse_script(srcbuf,startline); + } + if (script==NULL) { + // script parse error? + free(srcbuf); + return 1; + } + }else{ // duplicateする |