def delete_sarc_file(self, file: str) -> dict: try: self._open_sarc, tree, modded = _sarc.open_sarc( _sarc.delete_file(self._open_sarc, file)) except (ValueError, KeyError) as err: return {"error": {"msg": str(err), "traceback": format_exc(-5)}} return tree, modded
def replace_sarc_file(self, file: str) -> dict: try: new_file = self.browse() if not new_file: return {"error": {"msg": "Cancelled", "traceback": ""}} self._open_sarc, tree, modded = _sarc.open_sarc( _sarc.replace_file(self._open_sarc, file, Path(new_file).read_bytes())) except (ValueError, KeyError) as err: return {"error": {"msg": str(err), "traceback": format_exc(-5)}} return tree, modded
def update_sarc_folder(self) -> dict: result = self.window.create_file_dialog(webview.FOLDER_DIALOG) if not result: return {} try: self._open_sarc, tree, modded = _sarc.open_sarc( _sarc.update_from_folder( self._open_sarc, Path(result if isinstance(result, str) else result[0]), )) except (FileNotFoundError, OSError, ValueError) as err: return {"error": {"msg": str(err), "traceback": format_exc(-5)}} return tree, modded
def create_sarc(self, big_endian: bool, alignment: int) -> dict: tmp_sarc = oead.SarcWriter( oead.Endianness.Big if big_endian else oead.Endianness.Little, oead.SarcWriter.Mode.New if alignment == 4 else oead.SarcWriter.Mode.Legacy, ) self._open_sarc, tree, modded = _sarc.open_sarc( oead.Sarc(tmp_sarc.write()[1])) return { "sarc": tree, "be": big_endian, "path": "", "modded": modded, }
def add_sarc_file(self, file: str, sarc_path: str) -> dict: try: data = memoryview(Path(file).read_bytes()) self._open_sarc, tree, modded = _sarc.open_sarc( _sarc.add_file(self._open_sarc, sarc_path, data)) except ( AttributeError, ValueError, KeyError, OSError, TypeError, FileNotFoundError, ) as err: return {"error": {"msg": str(err), "traceback": format_exc(-5)}} return tree, modded
def open_sarc_file(self, file: Path): try: self._open_sarc, tree, modded = _sarc.open_sarc(file) except (ValueError, RuntimeError, oead.InvalidDataError): return { "error": { "msg": f"{file.name} is not a valid SARC file", "traceback": format_exc(-5), } } return { "path": str(file.resolve()), "sarc": tree, "modded": modded, "be": self._open_sarc.get_endianness() == oead.Endianness.Big, }
def save_yaml(self, yaml: str, obj_type: str, big_endian: bool, path: str) -> dict: if not path: result = self.window.create_file_dialog(webview.SAVE_DIALOG) if result: path = result if isinstance(result, str) else result[0] else: return {"error": {"msg": "Cancelled", "traceback": ""}} try: data = _yaml.save_yaml(yaml, obj_type, big_endian) pathy_path = Path(path) if pathy_path.suffix.startswith(".s"): data = compress(data) if not path.startswith("SARC:"): pathy_path.write_bytes(data) else: self._open_sarc, _, modded = _sarc.open_sarc( _sarc.add_file(self._open_sarc, path, data)) return {"modded": modded} except Exception as err: # pylint: disable=broad-except return {"error": {"msg": str(err), "traceback": format_exc(-5)}} return {}