def replace_file(root_sarc: Sarc, file: str, new_data: bytes) -> Sarc: if file.endswith("/"): file = file[0:-1] parent = get_parent_sarc(root_sarc, file) filename = file.split("//")[-1] new_sarc: SarcWriter = SarcWriter.from_sarc(parent) new_sarc.files[filename] = new_data while root_sarc != parent: _, child = new_sarc.write() file = file[0:file.rindex("//")] if file.endswith("/"): file = file[:-1] parent = get_parent_sarc(root_sarc, file) new_sarc = SarcWriter.from_sarc(parent) ext = file[file.rindex("."):] new_sarc.files[file] = (child if not (ext.startswith(".s") and ext != ".sarc") else compress(child)) return Sarc(new_sarc.write()[1])
def rename_file(root_sarc: Sarc, file: str, new_name: str) -> Sarc: if file.endswith("/"): file = file[0:-1] if any(char in new_name for char in r"""\/:*?"'<>|"""): raise ValueError(f"{new_name} is not a valid file name.") parent = get_parent_sarc(root_sarc, file) filename = file.split("//")[-1] new_sarc: SarcWriter = SarcWriter.from_sarc(parent) new_sarc.files[(Path(filename).parent / new_name).as_posix()] = Bytes( parent.get_file(filename).data) del new_sarc.files[filename] while root_sarc != parent: _, child = new_sarc.write() file = file[0:file.rindex("//")] if file.endswith("/"): file = file[:-1] parent = get_parent_sarc(root_sarc, file) new_sarc = SarcWriter.from_sarc(parent) ext = file[file.rindex("."):] new_sarc.files[file] = (child if not (ext.startswith(".s") and ext != ".sarc") else compress(child)) return Sarc(new_sarc.write()[1])
def _merge_in_sarc(sarc: Sarc, edits: dict) -> ByteString: new_sarc = SarcWriter.from_sarc(sarc) for file, stuff in edits.items(): if isinstance(stuff, dict): try: ofile = sarc.get_file(file) if ofile == None: raise FileNotFoundError(f"Could not find nested file {file} in SARC") sub_sarc = Sarc(util.unyaz_if_needed(ofile.data)) except ( InvalidDataError, ValueError, AttributeError, RuntimeError, FileNotFoundError, ): util.vprint(f"Couldn't merge into nested SARC {file}") continue nsub_bytes = _merge_in_sarc(sub_sarc, stuff) new_sarc.files[file] = ( util.compress(nsub_bytes) if file[file.rindex(".") :].startswith(".s") else nsub_bytes ) elif isinstance(stuff, ParameterList): try: ofile = sarc.get_file(file) if ofile == None: raise FileNotFoundError(f"Could not find nested file {file} in SARC") pio = ParameterIO.from_binary(ofile.data) except ( AttributeError, ValueError, InvalidDataError, FileNotFoundError, ) as err: util.vprint(f"Couldn't open {file}: {err}") continue new_pio = merge_shopdata(pio, stuff) new_sarc.files[file] = new_pio.to_binary() return new_sarc.write()[1]
def _merge_in_sarc(sarc: Sarc, edits: dict) -> ByteString: new_sarc = SarcWriter.from_sarc(sarc) for file, stuff in edits.items(): if isinstance(stuff, dict): try: sub_sarc = Sarc(util.unyaz_if_needed(sarc.get_file(file).data)) except (InvalidDataError, ValueError, AttributeError, RuntimeError): util.vprint(f"Couldn't merge into nested SARC {file}") continue nsub_bytes = _merge_in_sarc(sub_sarc, stuff) new_sarc.files[file] = (util.compress(nsub_bytes) if file[file.rindex("."):].startswith(".s") else nsub_bytes) elif isinstance(stuff, ParameterList): try: pio = ParameterIO.from_binary(sarc.get_file(file).data) except (AttributeError, ValueError, InvalidDataError) as e: util.vprint(f"Couldn't open {file}: {e}") continue merge_plists(pio, stuff) new_sarc.files[file] = pio.to_binary() return new_sarc.write()[1]
def update_from_folder(sarc: Sarc, folder: Path) -> Sarc: new_sarc: SarcWriter = SarcWriter.from_sarc(sarc) for file in {f for f in folder.rglob("**/*") if f.is_file()}: new_sarc.files[file.relative_to(folder).as_posix()] = file.read_bytes() return Sarc(new_sarc.write()[1])
def __init__(self, file): self.file = file self.data_sarc = self.init() self.data_writer = SarcWriter.from_sarc(self.data_sarc) set_sarc_endian(self.data_writer)
def set_sarc_endian(sarcwriter): if get_endianness(): SarcWriter.set_endianness(sarcwriter, oead.Endianness.Big) else: SarcWriter.set_endianness(sarcwriter, oead.Endianness.Little)
def write_sarc(sarc: oead.SarcWriter, dst: Path) -> int: return write(data=sarc.write()[1], src=None, dst=dst, condition=None, function=None)