class_name ThumbnailCache extends RefCounted const STICKMEN_DIR := "user://thumbnails/stickmen" const PROP_DIR := "user://thumbnails/props" const PROP_VERSION := 1 func stickman_key(path: String) -> String: return "%s_%d" % [path.get_file().get_basename(), FileAccess.get_modified_time(path)] func stickman_png(key: String) -> String: return STICKMEN_DIR + "/" + key + ".png" func prop_png(id: String) -> String: return PROP_DIR + "/" + id + "_v" + str(PROP_VERSION) + ".png" func load_png(png_path: String) -> Texture2D: if not FileAccess.file_exists(png_path): return null var img := Image.load_from_file(png_path) if img == null: return null return ImageTexture.create_from_image(img) func save_png(tex: Texture2D, png_path: String) -> Error: if tex == null: return ERR_INVALID_PARAMETER var img := tex.get_image() if img == null: return ERR_CANT_CREATE ensure_dir(png_path.get_base_dir()) return img.save_png(png_path) func ensure_dir(dir: String) -> void: DirAccess.make_dir_recursive_absolute(dir) func clean_stale_stickmen(valid_keys: Dictionary) -> void: if not DirAccess.dir_exists_absolute(STICKMEN_DIR): return var dir := DirAccess.open(STICKMEN_DIR) if dir == null: return dir.list_dir_begin() var fname := dir.get_next() while fname != "": if not dir.current_is_dir() and fname.ends_with(".png"): var key := fname.trim_suffix(".png") if not valid_keys.has(key): DirAccess.remove_absolute(STICKMEN_DIR + "/" + fname) fname = dir.get_next() dir.list_dir_end()