# Path: extract/unique_postfix.py
# def add_unique_postfix(fn, check_fn = os.path.exists):
#     if not check_fn(fn):
#         return fn
# 
#     path, name = os.path.split(fn)
#     name, ext = os.path.splitext(name)
# 
#     make_fn = lambda i: os.path.join(path, '%s_%d%s' % (name, i, ext))
# 
#     for i in xrange(2, sys.maxint):
#         uni_fn = make_fn(i)
#         if not check_fn(uni_fn):
#             return uni_fn
# 
#     return None


from enum import Enum
from .unique_postfix import add_unique_postfix
  toc_count_pos = TOC_INFO[umdimage][game][COUNT]
  toc_name_off  = TOC_INFO[umdimage][game][NAME_OFFSET]
  
  eboot_data.bytepos = toc_count_pos
  toc_count = eboot_data.read("uintle:32")
  
  eboot_data.bytepos = toc_start
  
  toc         = []
  file_starts = []
  filenames   = []
  
  for i in xrange(toc_count):
    name_pos = eboot_data.read("uintle:32")
    
    file_pos_pos  = eboot_data.bytepos
    file_pos      = eboot_data.read("uintle:32")
    file_len_pos  = eboot_data.bytepos
    file_len      = eboot_data.read("uintle:32")
    
    name_pos += toc_name_off
    
    temp_pos = eboot_data.bytepos
    eboot_data.bytepos = name_pos
    
    filename = eboot_data.readto("0x00", bytealigned = True).bytes
    # Strip the null character.
    filename = filename[:-1]
    
    # Ensure the filenames don't conflict.

# Path: extract/unique_postfix.py
# def add_unique_postfix(fn, check_fn = os.path.exists):
#     if not check_fn(fn):
#         return fn
# 
#     path, name = os.path.split(fn)
#     name, ext = os.path.splitext(name)
# 
#     make_fn = lambda i: os.path.join(path, '%s_%d%s' % (name, i, ext))
# 
#     for i in xrange(2, sys.maxint):
#         uni_fn = make_fn(i)
#         if not check_fn(uni_fn):
#             return uni_fn
# 
#     return None


# Path: extract/eboot.py
from enum import Enum
from .unique_postfix import add_unique_postfix

  toc_count_pos = TOC_INFO[umdimage][game][COUNT]
  toc_name_off  = TOC_INFO[umdimage][game][NAME_OFFSET]
  
  eboot_data.bytepos = toc_count_pos
  toc_count = eboot_data.read("uintle:32")
  
  eboot_data.bytepos = toc_start
  
  toc         = []
  file_starts = []
  filenames   = []
  
  for i in xrange(toc_count):
    name_pos = eboot_data.read("uintle:32")
    
    file_pos_pos  = eboot_data.bytepos
    file_pos      = eboot_data.read("uintle:32")
    file_len_pos  = eboot_data.bytepos
    file_len      = eboot_data.read("uintle:32")
    
    name_pos += toc_name_off
    
    temp_pos = eboot_data.bytepos
    eboot_data.bytepos = name_pos
    
    filename = eboot_data.readto("0x00", bytealigned = True).bytes
    # Strip the null character.
    filename = filename[:-1]
    
    # Ensure the filenames don't conflict.

    filename = add_unique_postfix(filename, check_fn = (lambda fn: fn in filenames))