コード例 #1
0
ファイル: bdt.py プロジェクト: Shgck/DarkSoulsDev
 def open(self, file_path, mode = "rb"):
     try:
         self.bdt_file = open(file_path, mode)
     except OSError as exc:
         LOG.error("Error opening {}: {}".format(file_path, exc))
         return
     self.opened = True
コード例 #2
0
    def export_file(self, entry, output_dir):
        """ Export the file corresponding to that BHD data entry, return the
        relative file path on success, None on failure """
        if not self.is_entry_valid(entry):
            LOG.error("Tried to extract a file not from this archive.")
            return None

        rel_path = self.filelist.get(entry.hash) or "{:08X}".format(entry.hash)
        LOG.info("Extracting {}".format(rel_path))

        file_content = self.bdt.read_entry(entry.offset, entry.size)
        content_len = len(file_content)
        if content_len != entry.size:
            LOG.error( "Tried to read {} bytes but only {} were available "
                       "(file '{}').".format(
                entry.size, content_len, rel_path
            ))
            return None

        output_path = os.path.join(output_dir, rel_path.lstrip("/"))
        if not os.path.isdir(os.path.dirname(output_path)):
            os.makedirs(os.path.dirname(output_path))
        with open(output_path, "wb") as output_file:
            output_file.write(file_content)
        return rel_path
コード例 #3
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
 def _load_infos(self, bnd_info_path):
     try:
         with open(bnd_info_path, "r") as infos_file:
             infos = json.load(infos_file)
         self.magic = infos["magic"].encode("ascii")
         self.flags = infos["flags"]
     except OSError as exc:
         LOG.error("Error reading {}: {}".format(bnd_info_path, exc))
     self._set_entry_bin()
コード例 #4
0
ファイル: dcx.py プロジェクト: Shgck/DarkSoulsDev
 def load(self, file_path):
     """ Load a DCX file, return True on success. """
     try:
         with open(file_path, "rb") as dcx_file:
             self._load_header(dcx_file)
             self._load_content(dcx_file)
     except OSError as exc:
         LOG.error("Error reading '{}': {}".format(file_path, exc))
         return False
     return True
コード例 #5
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
 def _load_infos(self, infos_path):
     try:
         with open(infos_path, "r") as infos_file:
             infos = json.load(infos_file)
     except OSError as exc:
         LOG.error("Error reading {}: {}".format(infos_path, exc))
         return
     self.ident = infos["ident"]
     self.decoded_path = infos["path"]
     self.set_has_absolute_path()
コード例 #6
0
ファイル: dcx.py プロジェクト: Shgck/DarkSoulsDev
 def save(self, output_path):
     """ Save the DCX file at output_path, return True on success. """
     try:
         with open(output_path, "wb") as dcx_file:
             self._save_header(dcx_file)
             self._save_content(dcx_file)
     except OSError as exc:
         LOG.error("Error writing '{}': {}".format(output_path, exc))
         return False
     return True
コード例 #7
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
 def load(self, file_path):
     """ Load the whole BND archive in memory, return True on success. """
     self.reset()
     try:
         with open(file_path, "rb") as bnd_file:
             self._load_header(bnd_file)
             self._load_entries(bnd_file)
     except OSError as exc:
         LOG.error("Error reading {}: {}".format(file_path, exc))
         return False
     return True
コード例 #8
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
 def _write_infos(self, output_path):
     infos = {
         "ident": self.ident,
         "path": self.decoded_path
     }
     json_path = output_path + ".json"
     try:
         with open(json_path, "w") as infos_file:
             json.dump(infos, infos_file)
     except OSError as exc:
         LOG.error("Error writing {}: {}".format(json_path, exc))
コード例 #9
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
 def _write_infos(self, output_dir):
     infos = {
         "magic": self.magic.decode("ascii"),
         "flags": self.flags
     }
     json_path = os.path.join(output_dir, self.INFOS_FILE_NAME)
     try:
         with open(json_path, "w") as infos_file:
             json.dump(infos, infos_file)
     except OSError as exc:
         LOG.error("Error writing {}: {}".format(json_path, exc))
コード例 #10
0
 def load_records_map(self, input_dir):
     """ Load the archive's records map that will be used to generate an
     archive with original record-to-entries map, return True on success. """
     map_path = os.path.join(input_dir, self.RECORDS_MAP_NAME)
     if not os.path.isfile(map_path):
         LOG.error("Records map file can't be found.")
         return False
     else:
         with open(map_path, "r") as records_map_file:
             self.records_map = json.load(records_map_file)
         return True
コード例 #11
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
 def save(self, output_path):
     """ Save the BND file at output_path, return True on success. """
     strings_block, files_block = self._generate_data()
     try:
         with open(output_path, "wb") as bnd_file:
             self._save_header(bnd_file)
             self._save_entries(bnd_file)
             bnd_file.write(strings_block)
             bnd_file.write(files_block)
     except OSError as exc:
         LOG.error("Error writing {}: {}".format(output_path, exc))
         return False
コード例 #12
0
 def _update_record(self, rel_path, data_entry):
     """ Add the data entry to the record associated with that relative path,
     return True on success. """
     try:
         index = next(( int(index)
                        for index, files in self.records_map.items()
                        if rel_path in files ))
     except StopIteration:
         LOG.error("File {} not in any record.".format(rel_path))
         return False
     record = self.bhd.records[index]
     record.entries.append(data_entry)
     return True
コード例 #13
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
    def extract_file(self, output_path, write_infos = True):
        """ Write entry data at output_path, return True on success. """
        try:
            if not os.path.isdir(os.path.dirname(output_path)):
                os.makedirs(os.path.dirname(output_path))
            with open(output_path, "wb") as output_file:
                output_file.write(self.data)
        except OSError as exc:
            LOG.error("Error writing {}: {}".format(output_path, exc))
            return False

        if write_infos:
            self._write_infos(output_path)

        return True
コード例 #14
0
ファイル: bdt.py プロジェクト: Shgck/DarkSoulsDev
    def import_file(self, file_path):
        position = self.bdt_file.tell()
        try:
            with open(file_path, "rb") as input_file:
                file_content = input_file.read()
            num_written = self.bdt_file.write(file_content)

            # Pad the BDT file to 16-byte if needed.
            pad_file(self.bdt_file, 16)
        except OSError as exc:
            LOG.error("Error importing {}: {}".format(
                file_path, exc
            ))
            return position, -1
        else:
            return position, num_written
コード例 #15
0
ファイル: dcx.py プロジェクト: Shgck/DarkSoulsDev
    def save_decompressed(self, output_path):
        """ Save the decompressed content at output_path, return True on
        success and False if an error occured with zlib or the export. """
        try:
            decompressed = zlib.decompress(self.zlib_data)
        except zlib.error as exc:
            LOG.error("Zlib error: {}".format(exc))
            return False

        try:
            with open(output_path, "wb") as output_file:
                output_file.write(decompressed)
        except OSError as exc:
            LOG.error("Error writing '{}': {}".format(output_path, exc))
            return False

        return True
コード例 #16
0
ファイル: bnd.py プロジェクト: Shgck/DarkSoulsDev
    def import_file(self, file_path):
        """ Load the file at file_path and the associated informations,
        return True on success. """
        self.reset()

        file_infos_path = file_path + ".json"
        if os.path.isfile(file_infos_path):
            self._load_infos(file_infos_path)

        try:
            with open(file_path, "rb") as input_file:
                self.data = input_file.read()
        except OSError as exc:
            LOG.error("Error writing {}: {}".format(file_path, exc))
            return False

        self.data_size = os.stat(file_path).st_size
        self.unk2 = self.data_size

        return True
コード例 #17
0
ファイル: dcx.py プロジェクト: Shgck/DarkSoulsDev
    def load_decompressed(self, file_path):
        """ Compress the file content, import its content and update the
        different sizes variables. Return True on success and False if an error
        occured with zlib or the import. """
        try:
            with open(file_path, "rb") as file_to_compress:
                data = file_to_compress.read()
        except OSError as exc:
            LOG.error("Error reading '{}': {}".format(file_path, exc))
            return False

        try:
            self.zlib_data = zlib.compress(data, 9)
        except zlib.error as exc:
            LOG.error("Zlib error: {}".format(exc))
            return False

        file_size = os.stat(file_path).st_size
        self.sizes.uncompressed_size = file_size
        self.sizes.compressed_size = len(self.zlib_data)
        return True