def __init__(self, directory: str, root_tag, wrapper: WorldFormat): self._directory = directory shutil.rmtree(get_temp_dir(self._directory), ignore_errors=True) self._root_tag = root_tag self._wrapper = wrapper self.chunk_cache: Dict[Coordinates, Chunk] = {} self.history_manager = ChunkHistoryManager(get_temp_dir(self._directory)) self._deleted_chunks = set()
def _save_to_undo(self): for chunk in self.blocks_cache.values(): if chunk.previous_unsaved_state is None: continue chunk.previous_unsaved_state.save_to_file( os.path.join( get_temp_dir(self._directory), f"Operation_{self.history_manager.undo_stack.size()}", )) chunk.previous_unsaved_state = None
def undo(self): path = os.path.join( get_temp_dir(self._directory), f"Operation_{self.history_manager.undo_stack.size()}", ) for chunk_name in os.listdir(path): if not chunk_name.startswith("chunk"): continue cx, cz = chunk_name.split("_")[1:] cx, cz = int(cx), int(cz) if (cx, cz) in self.blocks_cache: self.blocks_cache[(cx, cz)].load_from_file(path) self.history_manager.undo()
def exit(self): # TODO: add "unsaved changes" check before exit shutil.rmtree(get_temp_dir(self._directory), ignore_errors=True)