class_name ThemeAssets extends RefCounted ## Finds and loads the files a theme names: pictures, sounds and mascot SVGs. ## A path is relative to the theme's folder (built in: res://themes//; ## the user's own: user://themes//) or a full res:// or user:// path. ## Each file loads once; ThemeStore.apply clears the cache. See docs/THEMES.md. static var _cache: Dictionary = {} ## The folder a theme's relative paths start from. static func folder(theme_set: Dictionary) -> String: var id := str(theme_set.get("id", "")) if bool(theme_set.get("builtin", false)): return "res://themes/%s/" % id return ThemeStore.DIR + id + "/" ## The full path for a file named in a theme ("" stays ""). static func resolve(theme_set: Dictionary, file: String) -> String: if file == "": return "" if file.begins_with("res://") or file.begins_with("user://"): return file return folder(theme_set) + file static func clear_cache() -> void: _cache.clear() ## A picture (SVG, PNG, JPG or WebP), or null when the file is missing. static func texture(theme_set: Dictionary, file: String) -> Texture2D: var path := resolve(theme_set, file) if path == "": return null if not _cache.has(path): _cache[path] = _load_texture(path) return _cache[path] ## A sound (OGG, WAV or MP3), or null when the file is missing. static func sound(theme_set: Dictionary, file: String) -> AudioStream: var path := resolve(theme_set, file) if path == "": return null if not _cache.has(path): _cache[path] = _load_sound(path) return _cache[path] ## A file's text ("" when missing). Built-in files read this way must use the ## Keep File importer so they are in the export. static func text(path: String) -> String: if path == "" or not FileAccess.file_exists(path): return "" return FileAccess.get_file_as_string(path) ## A size pair from a theme ([w, h]), or `fallback`. static func vec2(value: Variant, fallback: Vector2) -> Vector2: if value is Array and (value as Array).size() == 2: return Vector2(float(value[0]), float(value[1])) return fallback static func _load_texture(path: String) -> Texture2D: if path.begins_with("res://") and ResourceLoader.exists(path): return load(path) as Texture2D if not FileAccess.file_exists(path): return null var image := Image.new() var err: int if path.get_extension().to_lower() == "svg": err = image.load_svg_from_string(FileAccess.get_file_as_string(path)) else: err = image.load(path) if err != OK: return null image.generate_mipmaps() return ImageTexture.create_from_image(image) static func _load_sound(path: String) -> AudioStream: if path.begins_with("res://") and ResourceLoader.exists(path): return load(path) as AudioStream if not FileAccess.file_exists(path): return null match path.get_extension().to_lower(): "ogg": return AudioStreamOggVorbis.load_from_file(path) "wav": return AudioStreamWAV.load_from_file(path) "mp3": return AudioStreamMP3.load_from_file(path) return null ## Every key that names a file, as [key] or [key, subkey]. const FILE_KEYS := [["mascot"], ["background_scene", "file"], ["background_tile", "file"], ["frame", "corner"], ["frame", "corner_fixed"], ["frame", "edge"], ["thread", "strip"], ["thread", "pin"]] ## A copy of a theme whose relative file names are made full paths from ## `base`, so the files are still found after the copy moves (a user copy of a ## built-in theme keeps using the built-in's folder). static func pin_paths(theme_set: Dictionary, base: String) -> Dictionary: var out := theme_set.duplicate(true) var full := func(file: String) -> String: return file if file == "" or file.begins_with("res://") or file.begins_with("user://") else base + file for key: Array in FILE_KEYS: if key.size() == 1: if out.has(key[0]): out[key[0]] = full.call(str(out[key[0]])) elif out.get(key[0]) is Dictionary and (out[key[0]] as Dictionary).has(key[1]): out[key[0]][key[1]] = full.call(str(out[key[0]][key[1]])) if out.get("sounds") is Dictionary: for event: String in out["sounds"]: out["sounds"][event] = full.call(str(out["sounds"][event])) return out