# Path: script_pack.py
# class ScriptPack():
#   def __init__(self, directory = None, umdimage = "umdimage"):
#     self.script_files = []
#     self.directory    = directory
#     self.wrd          = None
#     self.wrd_file     = None
#     
#     if not directory == None:
#       self.load_dir(directory, umdimage)
#       
#   def __getitem__(self, index):
#     return self.script_files[index]
#   
#   def __len__(self):
#     return len(self.script_files)
#   
#   def get_index(self, filename):
#     for index, file in enumerate(self.script_files):
#       if os.path.split(file.filename)[1] == filename:
#         return index
#     
#     return None
#   
#   def get_script(self, filename):
#     index = self.get_index(filename)
#     
#     if not index == None:
#       return self.__getitem__(index)
#     
#     else:
#       return None
#   
#   def get_real_dir(self):
#     # Rather than the easy to look at directory name we usually store,
#     # get the actual, untampered directory name where you can find the files.
#     return dir_tools.expand_dir(self.directory)
#   
#   def load_dir(self, directory, umdimage = "umdimage"):
#     
#     self.script_files = []
#     self.directory    = directory
#     self.wrd          = None
#     self.wrd_file     = None
#     self.py_file      = None
#     
#     base_name = directory
#     directory, wrd_file = dir_tools.parse_dir(directory, umdimage)
#     
#     if directory == None:
#       self.directory = ""
#       raise Exception("Directory \"" + base_name + "\" not found.")
#     
#     full_dir = os.path.join(umdimage, directory)
#     
#     scene_info = []
#     
#     if not wrd_file == None and os.path.isfile(wrd_file):
#       
#       # Even of the first directory existed and we have a .wrd file,
#       # it's possible there aren't actually any text files here.
#       if not os.path.isdir(full_dir):
#         raise Exception("There are no text files in \"" + directory + "\".")
#       
#       self.wrd  = WrdFile()
#       
#       py_file   = os.path.splitext(wrd_file)[0] + ".py"
#       
#       if os.path.isfile(py_file):
#         try:
#           self.wrd.load_python(py_file)
#         except:
#           _LOGGER.warning("%s failed to load. Parsing wrd file instead. Exception info:\n%s" % (py_file, traceback.format_exc()))
#           self.wrd.load_bin(wrd_file)
#         else:
#           # If we succeeded in loading the python file, compile it to binary.
#           # _LOGGER.info("%s loaded successfully. Compiling to binary." % py_file)
#           # self.wrd.save_bin(wrd_file)
#           _LOGGER.info("%s loaded successfully." % py_file)
#       
#       else:
#         _LOGGER.info("Decompiled wrd file not found. Generating %s" % py_file)
#         self.wrd.load_bin(wrd_file)
#         self.wrd.save_python(py_file)
#       
#       scene_info    = self.wrd.to_scene_info()
#       self.wrd_file = wrd_file
#       self.py_file  = py_file
#     
#     else:
#       scene_info    = None
#       self.wrd      = None
#       self.wrd_file = None
#     
#     self.script_files = []
#     if scene_info == None:
#       text_files = list_all_files(full_dir)
#       for file in text_files:
#         self.script_files.append(ScriptFile(file))
#         
#     else:
#       # Get our files in the order listed by the wrd.
#       for info in scene_info:
#         filename = os.path.join(full_dir, "%04d.txt" % info.file_id)
#         script_file = ScriptFile(filename, info)
#         
#         if script_file.filename == None:
#           _LOGGER.warning("File %s referenced by %s does not exist." % (filename, wrd_file))
#           continue
#         
#         self.script_files.append(script_file)
#     
#     chapter, scene, room, mode = common.get_dir_info(base_name)
#     
#     for file in self.script_files:
#       if file.scene_info.chapter == -1: file.scene_info.chapter = chapter
#       if file.scene_info.scene == -1:   file.scene_info.scene   = scene
#       if file.scene_info.room == -1:    file.scene_info.room    = room
#       if file.scene_info.mode == None:  file.scene_info.mode    = mode
# 
# Path: voice.py
# class VoiceId():
#   def __init__(
#     self,
#     char_id = -1,
#     chapter = -1,
#     voice_id = -1
#   ):
#     self.char_id  = char_id
#     self.chapter  = chapter
#     self.voice_id = voice_id


from bitstring import ConstBitStream, BitStream
from enum import Enum
from script_pack import ScriptPack
from sprite import SpriteId, SPRITE_TYPE
from voice import VoiceId
import logging
import os
import common
    
    return data

class NonstopParser():
  def __init__(self):
    self.script_pack = ScriptPack()
    self.filename = ""
    self.magic = None
    self.lines = []
  
  def save(self, filename):
    
    data = BitStream(self.magic) + BitStream(uintle = len(self.lines), length = 16)
    
    for line in self.lines:
      data += line.to_data()
    
    with open(filename, "wb") as f:
      data.tofile(f)
  
  def load(self, filename):
    filename = filename.lower()
    
    if not filename in NONSTOP_DIR:
      _LOGGER.error("Invalid nonstop file: %s" % filename)
      return
    
    self.filename = filename
    
    script_dir = NONSTOP_DIR[filename]

# Path: script_pack.py
# class ScriptPack():
#   def __init__(self, directory = None, umdimage = "umdimage"):
#     self.script_files = []
#     self.directory    = directory
#     self.wrd          = None
#     self.wrd_file     = None
#     
#     if not directory == None:
#       self.load_dir(directory, umdimage)
#       
#   def __getitem__(self, index):
#     return self.script_files[index]
#   
#   def __len__(self):
#     return len(self.script_files)
#   
#   def get_index(self, filename):
#     for index, file in enumerate(self.script_files):
#       if os.path.split(file.filename)[1] == filename:
#         return index
#     
#     return None
#   
#   def get_script(self, filename):
#     index = self.get_index(filename)
#     
#     if not index == None:
#       return self.__getitem__(index)
#     
#     else:
#       return None
#   
#   def get_real_dir(self):
#     # Rather than the easy to look at directory name we usually store,
#     # get the actual, untampered directory name where you can find the files.
#     return dir_tools.expand_dir(self.directory)
#   
#   def load_dir(self, directory, umdimage = "umdimage"):
#     
#     self.script_files = []
#     self.directory    = directory
#     self.wrd          = None
#     self.wrd_file     = None
#     self.py_file      = None
#     
#     base_name = directory
#     directory, wrd_file = dir_tools.parse_dir(directory, umdimage)
#     
#     if directory == None:
#       self.directory = ""
#       raise Exception("Directory \"" + base_name + "\" not found.")
#     
#     full_dir = os.path.join(umdimage, directory)
#     
#     scene_info = []
#     
#     if not wrd_file == None and os.path.isfile(wrd_file):
#       
#       # Even of the first directory existed and we have a .wrd file,
#       # it's possible there aren't actually any text files here.
#       if not os.path.isdir(full_dir):
#         raise Exception("There are no text files in \"" + directory + "\".")
#       
#       self.wrd  = WrdFile()
#       
#       py_file   = os.path.splitext(wrd_file)[0] + ".py"
#       
#       if os.path.isfile(py_file):
#         try:
#           self.wrd.load_python(py_file)
#         except:
#           _LOGGER.warning("%s failed to load. Parsing wrd file instead. Exception info:\n%s" % (py_file, traceback.format_exc()))
#           self.wrd.load_bin(wrd_file)
#         else:
#           # If we succeeded in loading the python file, compile it to binary.
#           # _LOGGER.info("%s loaded successfully. Compiling to binary." % py_file)
#           # self.wrd.save_bin(wrd_file)
#           _LOGGER.info("%s loaded successfully." % py_file)
#       
#       else:
#         _LOGGER.info("Decompiled wrd file not found. Generating %s" % py_file)
#         self.wrd.load_bin(wrd_file)
#         self.wrd.save_python(py_file)
#       
#       scene_info    = self.wrd.to_scene_info()
#       self.wrd_file = wrd_file
#       self.py_file  = py_file
#     
#     else:
#       scene_info    = None
#       self.wrd      = None
#       self.wrd_file = None
#     
#     self.script_files = []
#     if scene_info == None:
#       text_files = list_all_files(full_dir)
#       for file in text_files:
#         self.script_files.append(ScriptFile(file))
#         
#     else:
#       # Get our files in the order listed by the wrd.
#       for info in scene_info:
#         filename = os.path.join(full_dir, "%04d.txt" % info.file_id)
#         script_file = ScriptFile(filename, info)
#         
#         if script_file.filename == None:
#           _LOGGER.warning("File %s referenced by %s does not exist." % (filename, wrd_file))
#           continue
#         
#         self.script_files.append(script_file)
#     
#     chapter, scene, room, mode = common.get_dir_info(base_name)
#     
#     for file in self.script_files:
#       if file.scene_info.chapter == -1: file.scene_info.chapter = chapter
#       if file.scene_info.scene == -1:   file.scene_info.scene   = scene
#       if file.scene_info.room == -1:    file.scene_info.room    = room
#       if file.scene_info.mode == None:  file.scene_info.mode    = mode
# 
# Path: voice.py
# class VoiceId():
#   def __init__(
#     self,
#     char_id = -1,
#     chapter = -1,
#     voice_id = -1
#   ):
#     self.char_id  = char_id
#     self.chapter  = chapter
#     self.voice_id = voice_id


# Path: nonstop.py
from bitstring import ConstBitStream, BitStream
from enum import Enum
from script_pack import ScriptPack
from sprite import SpriteId, SPRITE_TYPE
from voice import VoiceId
import logging
import os
import common

    
    return data

class NonstopParser():
  def __init__(self):
    self.script_pack = ScriptPack()
    self.filename = ""
    self.magic = None
    self.lines = []
  
  def save(self, filename):
    
    data = BitStream(self.magic) + BitStream(uintle = len(self.lines), length = 16)
    
    for line in self.lines:
      data += line.to_data()
    
    with open(filename, "wb") as f:
      data.tofile(f)
  
  def load(self, filename):
    filename = filename.lower()
    
    if not filename in NONSTOP_DIR:
      _LOGGER.error("Invalid nonstop file: %s" % filename)
      return
    
    self.filename = filename
    
    script_dir = NONSTOP_DIR[filename]

    self.script_pack = ScriptPack(script_dir, common.editor_config.umdimage_dir)