class_name FolderCharacters extends RefCounted ## Characters read from a folder on this computer instead of the server: every PNG, JPG ## or WebP picture in it is a character, named by its file name. ## ## Has the same two calls as [CharacterCache], so the character select popup uses either. ## The file endings of the pictures that are characters. const PICTURE_EXTENSIONS: Array[String] = ["png", "jpg", "jpeg", "webp"] ## The folder the pictures are in. var path: String = "" func _init(folder_path: String) -> void: path = folder_path ## The file names of the pictures in [param folder], in natural order. Hidden files and ## other files are left out. static func picture_names(folder: String) -> Array[String]: var names: Array[String] = [] for file_name: String in DirAccess.get_files_at(folder): if !file_name.begins_with(".") \ && PICTURE_EXTENSIONS.has(file_name.get_extension().to_lower()): names.append(file_name) names.sort_custom(func(a: String, b: String) -> bool: return a.naturalnocasecmp_to(b) < 0) return names ## Calls [param on_done] with the names of the characters as an [code]Array[String][/code]. func load_list(on_done: Callable) -> void: on_done.call(picture_names(path)) ## Calls [param on_done] with the picture of [param character_name] as an [Image]. Nothing ## is called when the picture is gone or can't be read. func fetch_image(character_name: String, on_done: Callable) -> void: var file: String = path.path_join(character_name.validate_filename()) if !FileAccess.file_exists(file): GameLog.warning("The picture of ", character_name, " is not in ", path) return var picture: Image = CharacterCard.image_from_bytes(FileAccess.get_file_as_bytes(file)) if picture == null: GameLog.warning("Could not read the picture ", file) return on_done.call(picture)