def pack_lin(dir):
  
  # Collect our files.
  file_list = sorted(list_all_files(dir))
  
  txt = [filename for filename in file_list if os.path.splitext(filename)[1].lower() == ".txt"]
  wrd = [filename for filename in file_list if os.path.splitext(filename)[1].lower() == ".wrd"]
  py  = [filename for filename in file_list if os.path.splitext(filename)[1].lower() == ".py"]
  
  # If there are more than one for whatever reason, just take the first.
  # We only have use for a single wrd or python file.
  wrd = wrd[0] if wrd else None
  py  = py[0]  if py  else None
  
  # Prepare our temporary output directory.
  temp_dir = tempfile.mkdtemp(prefix = "sdse-")
  
  # Where we're outputting our wrd file, regardless of whether it's a python
  # file or a raw binary data file.
  wrd_dst = os.path.join(temp_dir, "0.wrd")
  
  if py:
    try:
      wrd_file = WrdFile(py)
    except:
      _LOGGER.warning("%s failed to compile. Parsing wrd file instead. Exception info:\n%s" % (py, traceback.format_exc()))
      shutil.copy(wrd, wrd_dst)
    else:
      # If we succeeded in loading the python file, compile it to binary.
      wrd_file.save_bin(wrd_dst)
  
  else:
    shutil.copy(wrd, wrd_dst)
  
  # Pack the text files in-place to save us a bunch of copying
  # and then move it to the tmp directory with the wrd file.
  if txt:
    data = pack_pak(dir, file_list = txt, eof = True)
    with open(os.path.join(temp_dir, "1.dat"), "wb") as f:
      data.tofile(f)
  
  # Then pack it like normal.
  data = pack_pak(temp_dir, eof = True)
  shutil.rmtree(temp_dir)
  
  return data
 def load_dir(self, directory, base_dir = "data01"):
   
   if not directory:
     return
   
   # directory, wrd_file = dir_tools.parse_dir(directory, base_dir)
   # Only expands if necessary.
   full_dir = dir_tools.expand_script_pak(directory)
   full_dir = os.path.join(base_dir, SCRIPT_DIR, full_dir)
   
   if not os.path.isdir(full_dir):
     raise Exception("Directory \"" + directory + "\" not found.")
   
   self.script_files = []
   self.directory    = directory
   self.wrd          = None
   self.wrd_file     = None
   self.py_file      = None
   
   scene_info = []
   wrd_file   = os.path.join(full_dir, os.path.splitext(directory)[0] + ".scp.wrd")
   
   if os.path.isfile(wrd_file):
   
     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.py_file  = None
   
   self.script_files = []
   if scene_info == None:
     text_files = [filename for filename in os.listdir(full_dir) if os.path.splitext(filename)[1].lower() == ".txt"]
     for filename in text_files:
       self.script_files.append(ScriptFile(os.path.join(full_dir, filename)))
       
   else:
     # Get our files in the order listed by the wrd.
     for info in scene_info:
       if info.file_id == None:
         script_file = ScriptJump(info)
       
       else:
         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(directory)
   
   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
    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
Example #4
0
    def load_dir(self, directory, base_dir="data01"):

        if not directory:
            return

        # directory, wrd_file = dir_tools.parse_dir(directory, base_dir)
        # Only expands if necessary.
        full_dir = dir_tools.expand_script_pak(directory)

        directory1 = normalize(directory)
        match = RE_FONT_PAK.match(directory1)
        if match:
            full_dir = os.path.join(base_dir, SCRIPT_BIN_DIR, full_dir)

    #     if not os.path.isdir(full_dir):
    #       full_dir = os.path.join(base_dir, SCRIPT_BIN_DIR, directory)
        else:
            full_dir = os.path.join(base_dir, SCRIPT_DIR, full_dir)

        if not os.path.isdir(full_dir):
            raise Exception("Directory \"" + full_dir + "\" not found.")

        self.script_files = []
        self.directory = directory
        self.wrd = None
        self.wrd_file = None
        self.py_file = None

        scene_info = []
        wrd_file = os.path.join(full_dir,
                                os.path.splitext(directory)[0] + ".scp.wrd")

        if os.path.isfile(wrd_file):

            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.py_file = None

        self.script_files = []
        if scene_info == None:
            text_files = [
                filename for filename in os.listdir(full_dir)
                if os.path.splitext(filename)[1].lower() == ".txt"
            ]
            for filename in text_files:
                self.script_files.append(
                    ScriptFile(os.path.join(full_dir, filename)))

        else:
            # Get our files in the order listed by the wrd.
            for info in scene_info:
                if info.file_id == None:
                    script_file = ScriptJump(info)

                else:
                    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(directory)

        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
class ScriptPack():
    def __init__(self, directory=None, base_dir="data01"):
        self.script_files = []
        self.directory = directory
        self.wrd = None
        self.wrd_file = None
        self.py_file = None

        if not directory == None:
            self.load_dir(directory, base_dir)

    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, base_dir="data01"):

        if not directory:
            return

        # directory, wrd_file = dir_tools.parse_dir(directory, base_dir)
        # Only expands if necessary.
        full_dir = dir_tools.expand_script_pak(directory)
        full_dir = os.path.join(base_dir, SCRIPT_DIR, full_dir)

        if not os.path.isdir(full_dir):
            raise Exception("Directory \"" + directory + "\" not found.")

        self.script_files = []
        self.directory = directory
        self.wrd = None
        self.wrd_file = None
        self.py_file = None

        scene_info = []
        wrd_file = os.path.join(full_dir,
                                os.path.splitext(directory)[0] + ".scp.wrd")

        if os.path.isfile(wrd_file):

            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.py_file = None

        self.script_files = []
        if scene_info == None:
            text_files = [
                filename for filename in os.listdir(full_dir)
                if os.path.splitext(filename)[1].lower() == ".txt"
            ]
            for filename in text_files:
                self.script_files.append(
                    ScriptFile(os.path.join(full_dir, filename)))

        else:
            # Get our files in the order listed by the wrd.
            for info in scene_info:
                if info.file_id == None:
                    script_file = ScriptJump(info)

                else:
                    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(directory)

        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