## SCRIPT BY 누구야

#open FDDE TBL
get TBL_FILENAME filename
get TBL_BASENAME basename
get TBL_EXTENSION extension
get TBL_FILESIZE asize

#string TXT_FILENAME = quickbms_arg1
#open FDSE TXT_FILENAME 1
open FDDE TXT 1
get TXT_FILESIZE asize 1

xmath TBL_LINES "TBL_FILESIZE / 0x454"

math TXT_LINES = 0
for a = 0 < TXT_FILESIZE
    FindLoc FIND_OFFSET string "\x09//TBL_LINE_END\x0D\x0A" 1 "NOT_FIND"
    #print "%TXT_LINES% - %FIND_OFFSET%"
    if FIND_OFFSET == "NOT_FIND"
      math a = TXT_FILESIZE
    else
      math TXT_LINES += 1
      xmath FIND_OFFSET "FIND_OFFSET + 17"
      goto FIND_OFFSET 1
    endif
next a

log MEMORY_FILE 0 0
log MEMORY_FILE2 0 0
log MEMORY_FILE3 0 0

math S_OFFSET = 0
goto S_OFFSET 1

for i = 0 < TBL_LINES
    codepage 932

    math ID = 0
    math IDx = 0
    math NAME = 0
    math TEXT = 0

    get UNKNOWN0 long
    get UNKNOWN1 short
    getdstring IDx 0x10
    get BLANK0 byte
    getdstring NAME 0x40
    get BLANK1 byte
    getdstring TEXT 0x1C0

    savepos SS_OFFSET 1
    FindLoc FIND_OFFSET string "\x09" 1 "NOT_FIND"
    if FIND_OFFSET != "NOT_FIND"
      xmath SIZE "FIND_OFFSET - S_OFFSET"
      getdstring NUM SIZE 1
      math NUM = NUM
    endif

    math checkNUM hex i

    if checkNUM != NUM
      goto SS_OFFSET 1

      put UNKNOWN0 long MEMORY_FILE
      put UNKNOWN1 short MEMORY_FILE

      putdstring IDx 0x10 MEMORY_FILE

      put BLANK0 byte MEMORY_FILE

      log MEMORY_FILE2 0 0
      put NAME string MEMORY_FILE2
      get MEMORY2_SIZE asize MEMORY_FILE2
      goto 0 MEMORY_FILE2
      getdstring NAME MEMORY2_SIZE MEMORY_FILE2
      putdstring NAME 0x40 MEMORY_FILE

      put BLANK1 byte MEMORY_FILE

      log MEMORY_FILE2 0 0
      put TEXT string MEMORY_FILE2
      get MEMORY2_SIZE asize MEMORY_FILE2
      goto 0 MEMORY_FILE2
      getdstring TEXT MEMORY2_SIZE MEMORY_FILE2
      putdstring TEXT 0x1C0 MEMORY_FILE
    elif checkNUM == NUM
      xmath S_OFFSET "FIND_OFFSET + 1"
      goto S_OFFSET 1

      put UNKNOWN0 long MEMORY_FILE
      put UNKNOWN1 short MEMORY_FILE

      FindLoc FIND_OFFSET string "\x09" 1 "NOT_FIND"
      if FIND_OFFSET != "NOT_FIND"
        xmath SIZE "FIND_OFFSET - S_OFFSET"
        getdstring ID SIZE 1
        xmath S_OFFSET "FIND_OFFSET + 1"
        goto S_OFFSET 1
      endif

      putdstring ID 0x10 MEMORY_FILE
      put BLANK0 byte MEMORY_FILE

      FindLoc FIND_OFFSET string "\x09" 1 "NOT_FIND"
      if FIND_OFFSET != "NOT_FIND"
        xmath SIZE "FIND_OFFSET - S_OFFSET"
        getdstring NAME SIZE 1
        xmath S_OFFSET "FIND_OFFSET + 1"
        goto S_OFFSET 1
      endif

      log MEMORY_FILE2 0 0
      put NAME string MEMORY_FILE2
      get MEMORY2_SIZE asize MEMORY_FILE2
      goto 0 MEMORY_FILE2
      getdstring NAME MEMORY2_SIZE MEMORY_FILE2
      putdstring NAME 0x40 MEMORY_FILE

      put BLANK1 byte MEMORY_FILE

      FindLoc FIND_OFFSET string "\x09//TBL_LINE_END\x0D\x0A" 1 "NOT_FIND"
      if FIND_OFFSET != "NOT_FIND"
        xmath SIZE "FIND_OFFSET - S_OFFSET"
        getdstring TEXT SIZE 1
        xmath S_OFFSET "FIND_OFFSET + 17"
        goto S_OFFSET 1
      endif

      log MEMORY_FILE2 0 0
      log MEMORY_FILE3 0 0
      put TEXT string MEMORY_FILE2
      get MEMORY2_SIZE asize MEMORY_FILE2
      goto 0 MEMORY_FILE2
      for j = 0 < MEMORY2_SIZE
        get VALUE byte MEMORY_FILE2
        if VALUE == 0x2A
          put 0x0A byte MEMORY_FILE3
        elif VALUE == 0x24
          put 0x0D byte MEMORY_FILE3
        else
          put VALUE byte MEMORY_FILE3
        endif
      next j
      get MEMORY3_SIZE asize MEMORY_FILE3
      goto 0 MEMORY_FILE3
      getdstring TEXT MEMORY3_SIZE MEMORY_FILE3
      putdstring TEXT 0x1C0 MEMORY_FILE
    endif

    for j = 0 < 0x9C
      get VALUE byte
      put VALUE byte MEMORY_FILE
    next j

    if IDx != ""
      log MEMORY_FILE3 0 0
      put 0x20 byte MEMORY_FILE3
      put NAME string MEMORY_FILE3
      get MEMORY3_SIZE asize MEMORY_FILE3
      goto 0 MEMORY_FILE3
      getdstring NAME_DP MEMORY3_SIZE MEMORY_FILE3
      print "%checkNUM%==%NUM%  %SS_OFFSET|x%  IDx %IDx% / ID %ID% / NAME :%NAME_DP%"
    endif
next i


get MEMORY1_SIZE asize MEMORY_FILE

string EXT_NAME0 = TBL_BASENAME
string EXT_NAME0 += ".tbl"

log EXT_NAME0 0 MEMORY1_SIZE MEMORY_FILE

if MEMORY1_SIZE < TBL_FILESIZE
  xmath CUT_SIZE "TBL_FILESIZE - MEMORY1_SIZE"
  append
  log EXT_NAME0 MEMORY1_SIZE CUT_SIZE
  append
endif


cleanexit
