2026-08-16 20:17:35 -03:00
|
|
|
extends BoxArtProvider
|
|
|
|
|
|
|
|
|
|
## itch.io box art provider for OpenGamepadUI
|
|
|
|
|
##
|
|
|
|
|
## Fetches cover art and screenshots from itch.io game pages. Works alongside
|
|
|
|
|
## the itch.io library plugin to provide per-layout artwork: the cover for
|
|
|
|
|
## portrait/logo, full-res screenshots for landscape/banner.
|
|
|
|
|
##
|
|
|
|
|
## Enriches game metadata by scraping the itch.io page HTML for screenshots
|
|
|
|
|
## (the itch.io API does not expose them), caches the result in
|
|
|
|
|
## itch_art_meta.json, and uses HTTPImageFetcher for async image downloads.
|
|
|
|
|
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
const BOXART_DIR := "user://boxart/itch"
|
|
|
|
|
const META_CACHE_FILE := "itch_art_meta.json"
|
2026-08-19 16:20:21 -03:00
|
|
|
const HTTP_TIMEOUT_MS := 15000
|
2026-08-19 18:08:35 -03:00
|
|
|
const SCREENSHOT_REGEX_PATTERN := "https://img\\.itch\\.zone/aW1hZ2Uv[\\w+/=]*/original/[\\w+/=]*\\.[a-z]+"
|
|
|
|
|
const IMAGE_EXTS := ["png", "jpg", "jpeg", "gif", "webp", "bmp"]
|
2026-08-19 20:48:59 -03:00
|
|
|
const FFMPEG_BUILDS_URL := "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest"
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
const CONNECT_POLL_DELAY_MS := 50
|
|
|
|
|
const READ_POLL_DELAY_MS := 10
|
|
|
|
|
const LOG_URL_MAX_LEN := 80
|
2026-08-19 21:11:04 -03:00
|
|
|
const SETTING_SECTION := "plugin.artprovider"
|
|
|
|
|
const SETTING_ANIMATED_GIFS := "animated_gifs"
|
|
|
|
|
const GIF_DEFAULT_FRAME_DELAY_MS := 100
|
2026-08-16 20:17:35 -03:00
|
|
|
|
|
|
|
|
@export var use_caching: bool = true
|
|
|
|
|
var http_image := HTTPImageFetcher.new()
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
var _meta_cache_dir := "itch_art"
|
|
|
|
|
var _enriched_meta: Dictionary = {}
|
2026-08-19 21:11:04 -03:00
|
|
|
var settings_manager := load("res://core/global/settings_manager.tres") as SettingsManager
|
|
|
|
|
var _animated_texture_cache: Dictionary = {}
|
|
|
|
|
var _last_animated_setting: bool = false
|
|
|
|
|
var _focused_animated_texture: AnimatedTexture = null
|
2026-08-16 20:17:35 -03:00
|
|
|
|
|
|
|
|
var layout_map: Dictionary = {
|
|
|
|
|
LAYOUT.GRID_PORTRAIT: "-portrait",
|
|
|
|
|
LAYOUT.GRID_LANDSCAPE: "-landscape",
|
|
|
|
|
LAYOUT.BANNER: "-banner",
|
|
|
|
|
LAYOUT.LOGO: "-logo",
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-19 20:48:59 -03:00
|
|
|
## Resolved path to the ffmpeg binary (system or bundled). Populated lazily.
|
|
|
|
|
var _ffmpeg_bin: String = ""
|
|
|
|
|
|
2026-08-16 20:17:35 -03:00
|
|
|
|
|
|
|
|
func _init() -> void:
|
|
|
|
|
super()
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(BOXART_DIR))
|
2026-08-16 20:17:35 -03:00
|
|
|
provider_id = "itch"
|
|
|
|
|
logger_name = "BoxArtItch"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
|
super()
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
_enriched_meta = _load_enriched_meta()
|
2026-08-19 21:11:04 -03:00
|
|
|
_last_animated_setting = _is_animated_gifs_enabled()
|
|
|
|
|
logger.info("itch.io Art Provider loaded (animated_gifs=%s)" % str(_last_animated_setting))
|
2026-08-16 20:17:35 -03:00
|
|
|
add_child(http_image)
|
2026-08-19 21:11:04 -03:00
|
|
|
get_viewport().gui_focus_changed.connect(_on_focus_changed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Returns whether animated GIF covers are enabled in settings.
|
|
|
|
|
func _is_animated_gifs_enabled() -> bool:
|
|
|
|
|
return settings_manager.get_value(SETTING_SECTION, SETTING_ANIMATED_GIFS, false) as bool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Handles focus changes across the UI. Pauses the previously focused
|
|
|
|
|
## card's AnimatedTexture and unpauses the newly focused one.
|
|
|
|
|
func _on_focus_changed(control: Control) -> void:
|
|
|
|
|
# Pause the previously focused animation.
|
|
|
|
|
if _focused_animated_texture != null:
|
|
|
|
|
_focused_animated_texture.pause = true
|
|
|
|
|
_focused_animated_texture = null
|
|
|
|
|
|
|
|
|
|
if control == null:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Walk up the tree to find the GameCard parent.
|
|
|
|
|
var card: Control = control
|
|
|
|
|
while card != null:
|
|
|
|
|
if card is GameCard:
|
|
|
|
|
break
|
|
|
|
|
card = card.get_parent()
|
|
|
|
|
|
|
|
|
|
if card == null:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Check if the card's TextureRect has an AnimatedTexture.
|
|
|
|
|
var texture_rect := card.get_node_or_null("%TextureRect") as TextureRect
|
|
|
|
|
if texture_rect == null:
|
|
|
|
|
return
|
|
|
|
|
if texture_rect.texture is AnimatedTexture:
|
|
|
|
|
_focused_animated_texture = texture_rect.texture as AnimatedTexture
|
|
|
|
|
_focused_animated_texture.pause = false
|
2026-08-16 20:17:35 -03:00
|
|
|
|
|
|
|
|
|
2026-08-19 18:08:35 -03:00
|
|
|
## Strips HTML srcset artifacts from a URL. Butlerd sometimes passes through
|
|
|
|
|
## itch.io cover URLs with trailing srcset descriptors like " 1x, h" or " 2x".
|
|
|
|
|
## These cause HTTPImageFetcher to fail because the URL is no longer valid.
|
|
|
|
|
func _sanitize_url(url: String) -> String:
|
|
|
|
|
if url.is_empty():
|
|
|
|
|
return url
|
|
|
|
|
# Srcset descriptors are separated by spaces — take only the URL portion.
|
|
|
|
|
var clean: String = url.split(" ")[0]
|
|
|
|
|
# Validate that the cleaned URL ends with a known image extension.
|
|
|
|
|
var ext: String = clean.get_extension().to_lower()
|
|
|
|
|
if ext not in IMAGE_EXTS:
|
|
|
|
|
logger.warn("Sanitized URL has unexpected extension '%s': %s" % [ext, clean.left(LOG_URL_MAX_LEN)])
|
|
|
|
|
return clean
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Returns whether a URL points to a GIF image. Handles both clean URLs and
|
|
|
|
|
## URLs contaminated with srcset artifacts (e.g. "image.gif 1x, h").
|
|
|
|
|
func _is_gif_url(url: String) -> bool:
|
|
|
|
|
return _sanitize_url(url).to_lower().ends_with(".gif")
|
|
|
|
|
|
|
|
|
|
|
2026-08-16 20:17:35 -03:00
|
|
|
func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
|
|
|
|
if not kind in layout_map:
|
|
|
|
|
logger.error("Unsupported boxart layout: {0}".format([kind]))
|
|
|
|
|
return null
|
|
|
|
|
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
var game := _find_game_dict(item)
|
2026-08-16 20:17:35 -03:00
|
|
|
if game.is_empty():
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("No itch.io game metadata found for: " + item.name)
|
2026-08-16 20:17:35 -03:00
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
var game_id := int(game.get("id", 0))
|
|
|
|
|
var title: String = game.get("title", "")
|
2026-08-17 11:07:54 -03:00
|
|
|
logger.info("get_boxart: title=%s id=%d layout=%s" % [title, game_id, str(kind)])
|
2026-08-17 10:55:56 -03:00
|
|
|
if title.is_empty():
|
|
|
|
|
logger.warn("Game title is empty for id %d" % game_id)
|
|
|
|
|
return null
|
2026-08-16 20:17:35 -03:00
|
|
|
|
|
|
|
|
# Enrich on first access: scrape the itch.io page for screenshots.
|
2026-08-19 17:32:25 -03:00
|
|
|
# Cached failures (empty screenshots) are stored to avoid repeated
|
|
|
|
|
# requests against Cloudflare-protected pages.
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
var meta: Dictionary = _enriched_meta.get(game_id, {})
|
2026-08-16 20:17:35 -03:00
|
|
|
if meta.is_empty() and game.has("url"):
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.info("Enriching metadata for: %s (url=%s)" % [title, game.get("url", "")])
|
2026-08-16 20:17:35 -03:00
|
|
|
meta = _fetch_game_page_metadata(game)
|
2026-08-19 17:32:25 -03:00
|
|
|
# Cache both successes and failures to avoid repeated requests.
|
|
|
|
|
_enriched_meta[game_id] = meta
|
|
|
|
|
_save_enriched_meta(game_id, meta)
|
2026-08-17 10:55:56 -03:00
|
|
|
if meta.is_empty():
|
2026-08-19 17:32:25 -03:00
|
|
|
logger.info("No screenshots available for: %s — will use cover art" % title)
|
2026-08-17 10:55:56 -03:00
|
|
|
else:
|
2026-08-19 17:32:25 -03:00
|
|
|
logger.info("Scraped %d screenshots for: %s" % [meta.get("screenshots", []).size(), title])
|
2026-08-17 10:55:56 -03:00
|
|
|
elif not meta.is_empty():
|
|
|
|
|
logger.info("Using cached metadata for: %s (%d screenshots)" % [title, meta.get("screenshots", []).size()])
|
|
|
|
|
else:
|
|
|
|
|
logger.warn("No URL in game dict for: %s" % title)
|
2026-08-16 20:17:35 -03:00
|
|
|
|
2026-08-19 18:08:35 -03:00
|
|
|
var screenshots: Array = meta.get("screenshots", []).map(_sanitize_url)
|
2026-08-19 20:48:59 -03:00
|
|
|
var cover_url := _sanitize_url(_best_portrait_url(game, screenshots))
|
2026-08-17 11:07:54 -03:00
|
|
|
if cover_url.is_empty():
|
|
|
|
|
logger.warn("No cover URL for: %s (id=%d)" % [title, game_id])
|
|
|
|
|
return null
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
logger.info("cover_url=%s" % cover_url.left(LOG_URL_MAX_LEN))
|
2026-08-17 11:07:54 -03:00
|
|
|
|
2026-08-16 20:17:35 -03:00
|
|
|
var cache_flags := Cache.FLAGS.NONE
|
|
|
|
|
if use_caching:
|
|
|
|
|
cache_flags = Cache.FLAGS.LOAD | Cache.FLAGS.SAVE
|
|
|
|
|
|
2026-08-17 01:51:54 -03:00
|
|
|
# Banner: combine two screenshots side-by-side when available.
|
2026-08-19 20:48:59 -03:00
|
|
|
if kind == LAYOUT.BANNER and screenshots.size() >= 2:
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.info("Fetching itch.io banner (2 screenshots) for: " + item.name)
|
2026-08-19 20:48:59 -03:00
|
|
|
var tex_a := await _fetch_image(screenshots[0], cache_flags)
|
|
|
|
|
var tex_b := await _fetch_image(screenshots[1], cache_flags)
|
2026-08-17 01:51:54 -03:00
|
|
|
if tex_a != null and tex_b != null:
|
|
|
|
|
return _combine_side_by_side(tex_a, tex_b)
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
# Banner combine failed — fall back to single-image path.
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("Banner screenshot fetch failed: tex_a=%s tex_b=%s" % [str(tex_a != null), str(tex_b != null)])
|
2026-08-17 01:51:54 -03:00
|
|
|
if tex_a != null:
|
|
|
|
|
return tex_a
|
|
|
|
|
if tex_b != null:
|
|
|
|
|
return tex_b
|
|
|
|
|
|
2026-08-19 20:48:59 -03:00
|
|
|
var url := _url_for_layout(kind, cover_url, screenshots)
|
2026-08-17 01:51:54 -03:00
|
|
|
if url.is_empty():
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("URL for layout %s is empty (cover=%s)" % [str(kind), cover_url])
|
2026-08-17 01:51:54 -03:00
|
|
|
return null
|
|
|
|
|
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
logger.info("Fetching itch.io box art for: %s layout=%s url=%s" % [item.name, str(kind), url.left(LOG_URL_MAX_LEN)])
|
2026-08-19 20:48:59 -03:00
|
|
|
var texture: Texture2D = await _fetch_image(url, cache_flags)
|
2026-08-16 20:17:35 -03:00
|
|
|
if texture == null:
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
logger.warn("Image download returned null for: %s url=%s" % [item.name, url.left(LOG_URL_MAX_LEN)])
|
2026-08-16 20:17:35 -03:00
|
|
|
return texture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Finds the itch.io game dict from the library item's launch items.
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
func _find_game_dict(item: LibraryItem) -> Dictionary:
|
2026-08-17 10:55:56 -03:00
|
|
|
if item.launch_items.is_empty():
|
|
|
|
|
logger.warn("Item has no launch items: " + item.name)
|
|
|
|
|
return {}
|
2026-08-16 20:17:35 -03:00
|
|
|
for launch_item in item.launch_items:
|
|
|
|
|
if launch_item._provider_id != "itch":
|
|
|
|
|
continue
|
|
|
|
|
var game: Dictionary = launch_item.metadata.get("game", {})
|
|
|
|
|
if not game.is_empty():
|
|
|
|
|
return game
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("No itch.io launch item found for: %s (providers: %s)" % [
|
|
|
|
|
item.name,
|
|
|
|
|
PackedStringArray(item.launch_items.map(func(l): return l._provider_id))
|
|
|
|
|
])
|
2026-08-16 20:17:35 -03:00
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _url_for_layout(kind: LAYOUT, cover_url: String, screenshots: Array) -> String:
|
|
|
|
|
match kind:
|
|
|
|
|
LAYOUT.GRID_PORTRAIT, LAYOUT.LOGO:
|
|
|
|
|
return cover_url
|
|
|
|
|
LAYOUT.GRID_LANDSCAPE:
|
2026-08-19 20:48:59 -03:00
|
|
|
return screenshots[0] if not screenshots.is_empty() else cover_url
|
2026-08-16 20:17:35 -03:00
|
|
|
LAYOUT.BANNER:
|
2026-08-17 01:51:54 -03:00
|
|
|
# Single screenshot fallback (two-screenshot case handled in get_boxart).
|
2026-08-19 20:48:59 -03:00
|
|
|
return screenshots[0] if not screenshots.is_empty() else cover_url
|
2026-08-16 20:17:35 -03:00
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
2026-08-19 18:49:29 -03:00
|
|
|
## Returns the best portrait/logo URL from the game data.
|
2026-08-19 18:58:10 -03:00
|
|
|
## Prefers stillCoverUrl (the still/non-animated version of the cover),
|
|
|
|
|
## then coverUrl. Screenshots are never used as capsule art.
|
2026-08-19 18:49:29 -03:00
|
|
|
func _best_portrait_url(game: Dictionary, _screenshots: Array) -> String:
|
2026-08-17 11:07:54 -03:00
|
|
|
var still: String = game.get("stillCoverUrl", "")
|
2026-08-19 18:58:10 -03:00
|
|
|
if not still.is_empty():
|
2026-08-17 11:07:54 -03:00
|
|
|
return still
|
2026-08-19 18:58:10 -03:00
|
|
|
return game.get("coverUrl", "")
|
2026-08-17 11:07:54 -03:00
|
|
|
|
|
|
|
|
|
2026-08-19 20:48:59 -03:00
|
|
|
## Ensures an ffmpeg binary is available. Checks the system PATH first,
|
|
|
|
|
## then falls back to a bundled copy under user://boxart/itch/. Downloads
|
|
|
|
|
## the static build on first use if neither is found.
|
|
|
|
|
func _ensure_ffmpeg() -> String:
|
|
|
|
|
if not _ffmpeg_bin.is_empty():
|
|
|
|
|
return _ffmpeg_bin
|
|
|
|
|
# 1. Check system PATH by probing directly.
|
|
|
|
|
var probe: Array = []
|
|
|
|
|
OS.execute("ffmpeg", ["-version"], probe, true)
|
|
|
|
|
if probe.size() > 0 and probe[0].find("ffmpeg version") != -1:
|
|
|
|
|
_ffmpeg_bin = "ffmpeg"
|
|
|
|
|
logger.info("Using system ffmpeg")
|
|
|
|
|
return _ffmpeg_bin
|
|
|
|
|
# 2. Check bundled location.
|
|
|
|
|
var bundled: String = ProjectSettings.globalize_path(BOXART_DIR) + "/ffmpeg"
|
|
|
|
|
if FileAccess.file_exists(bundled):
|
|
|
|
|
_ffmpeg_bin = bundled
|
|
|
|
|
logger.info("Using bundled ffmpeg: " + _ffmpeg_bin)
|
|
|
|
|
return _ffmpeg_bin
|
|
|
|
|
# 3. Download.
|
|
|
|
|
logger.info("ffmpeg not found — downloading static build")
|
|
|
|
|
if await _install_ffmpeg():
|
|
|
|
|
_ffmpeg_bin = ProjectSettings.globalize_path(BOXART_DIR) + "/ffmpeg"
|
|
|
|
|
return _ffmpeg_bin
|
|
|
|
|
logger.warn("ffmpeg unavailable — GIF covers will not load")
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Downloads a static ffmpeg build. Detects the platform and architecture
|
|
|
|
|
## the same way the itch plugin's butler installer does, then fetches
|
|
|
|
|
## the matching BtbN pre-built binary.
|
|
|
|
|
func _install_ffmpeg() -> bool:
|
2026-08-19 21:17:00 -03:00
|
|
|
var os_name := _detect_os()
|
|
|
|
|
var arch_name := _detect_arch()
|
|
|
|
|
var archive_url := _build_ffmpeg_url(os_name, arch_name)
|
|
|
|
|
var body := await _download_url_bytes(archive_url)
|
|
|
|
|
if body.is_empty():
|
|
|
|
|
return false
|
|
|
|
|
return _extract_ffmpeg(body, os_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Detects the current OS as a short name for ffmpeg builds.
|
|
|
|
|
func _detect_os() -> String:
|
2026-08-19 20:48:59 -03:00
|
|
|
if OS.get_name() == "Windows":
|
2026-08-19 21:17:00 -03:00
|
|
|
return "win"
|
2026-08-19 20:48:59 -03:00
|
|
|
if OS.get_name() == "macOS":
|
2026-08-19 21:17:00 -03:00
|
|
|
return "macos"
|
|
|
|
|
return "linux"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Detects the CPU architecture as a short name for ffmpeg builds.
|
|
|
|
|
func _detect_arch() -> String:
|
2026-08-19 20:48:59 -03:00
|
|
|
if Engine.has_method("get_architecture_name"):
|
|
|
|
|
var arch: String = Engine.get_architecture_name()
|
|
|
|
|
if "arm64" in arch or "aarch64" in arch:
|
2026-08-19 21:17:00 -03:00
|
|
|
return "arm64"
|
|
|
|
|
return "64"
|
|
|
|
|
|
2026-08-19 20:48:59 -03:00
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
## Builds the download URL for the ffmpeg archive matching the given platform.
|
|
|
|
|
func _build_ffmpeg_url(os_name: String, arch_name: String) -> String:
|
|
|
|
|
var platform_slug := os_name + arch_name
|
2026-08-19 20:48:59 -03:00
|
|
|
var archive_name := "ffmpeg-master-latest-%s-gpl" % platform_slug
|
2026-08-19 21:17:00 -03:00
|
|
|
var ext := "zip" if os_name == "win" else "tar.xz"
|
|
|
|
|
return "%s/%s.%s" % [FFMPEG_BUILDS_URL, archive_name, ext]
|
|
|
|
|
|
2026-08-19 20:48:59 -03:00
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
## Downloads bytes from a URL. Returns empty PackedByteArray on failure.
|
|
|
|
|
func _download_url_bytes(url: String) -> PackedByteArray:
|
2026-08-19 20:48:59 -03:00
|
|
|
var http := HTTPRequest.new()
|
|
|
|
|
add_child.call_deferred(http)
|
|
|
|
|
await http.ready
|
|
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
if http.request(url) != OK:
|
|
|
|
|
logger.error("Error requesting: " + url)
|
2026-08-19 20:48:59 -03:00
|
|
|
remove_child(http)
|
|
|
|
|
http.queue_free()
|
2026-08-19 21:17:00 -03:00
|
|
|
return PackedByteArray()
|
2026-08-19 20:48:59 -03:00
|
|
|
|
|
|
|
|
var args: Array = await http.request_completed
|
|
|
|
|
var result: int = args[0]
|
|
|
|
|
var response_code: int = args[1]
|
|
|
|
|
var body: PackedByteArray = args[3]
|
|
|
|
|
remove_child(http)
|
|
|
|
|
http.queue_free()
|
|
|
|
|
|
|
|
|
|
if result != HTTPRequest.RESULT_SUCCESS or response_code != 200:
|
2026-08-19 21:17:00 -03:00
|
|
|
logger.error("Download failed: HTTP %d for %s" % [response_code, url])
|
|
|
|
|
return PackedByteArray()
|
|
|
|
|
return body
|
2026-08-19 20:48:59 -03:00
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
|
|
|
|
|
## Extracts the ffmpeg binary from the downloaded archive.
|
|
|
|
|
func _extract_ffmpeg(body: PackedByteArray, os_name: String) -> bool:
|
2026-08-19 20:48:59 -03:00
|
|
|
var globalized_dir := ProjectSettings.globalize_path(BOXART_DIR)
|
|
|
|
|
DirAccess.make_dir_recursive_absolute(globalized_dir)
|
2026-08-19 21:17:00 -03:00
|
|
|
var is_win: bool = os_name == "win"
|
2026-08-19 20:48:59 -03:00
|
|
|
var ext := "zip" if is_win else "tar.xz"
|
|
|
|
|
var archive_path := globalized_dir + "/ffmpeg." + ext
|
|
|
|
|
var file := FileAccess.open(archive_path, FileAccess.WRITE)
|
|
|
|
|
if file == null:
|
|
|
|
|
logger.error("Cannot write ffmpeg archive to " + archive_path)
|
|
|
|
|
return false
|
|
|
|
|
file.store_buffer(body)
|
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
|
|
var out := []
|
|
|
|
|
if is_win:
|
|
|
|
|
OS.execute("unzip", ["-o", archive_path, "-d", globalized_dir], out)
|
|
|
|
|
OS.execute("chmod", ["+x", globalized_dir + "/ffmpeg.exe"], out)
|
|
|
|
|
else:
|
|
|
|
|
OS.execute("tar", ["xf", archive_path, "-C", globalized_dir,
|
|
|
|
|
"--strip-components=1", "--wildcards", "*/ffmpeg"], out)
|
|
|
|
|
OS.execute("chmod", ["+x", globalized_dir + "/ffmpeg"], out)
|
|
|
|
|
DirAccess.remove_absolute(archive_path)
|
|
|
|
|
logger.info("ffmpeg installed to " + globalized_dir)
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
2026-08-19 21:11:04 -03:00
|
|
|
## Unified image fetch that routes GIF URLs through ffmpeg and non-GIF URLs
|
|
|
|
|
## through the normal HTTPImageFetcher path. Checks the animated GIF setting
|
|
|
|
|
## on each call so toggling takes effect immediately.
|
2026-08-19 20:48:59 -03:00
|
|
|
func _fetch_image(url: String, cache_flags: int) -> Texture2D:
|
|
|
|
|
if _is_gif_url(url):
|
2026-08-19 21:11:04 -03:00
|
|
|
# Invalidate cache when the animated setting changes.
|
|
|
|
|
var animated: bool = _is_animated_gifs_enabled()
|
|
|
|
|
if animated != _last_animated_setting:
|
|
|
|
|
_animated_texture_cache.clear()
|
|
|
|
|
_last_animated_setting = animated
|
|
|
|
|
logger.info("Animated GIF setting changed to %s — cache cleared" % str(animated))
|
|
|
|
|
return await _fetch_gif_as_texture(url, cache_flags)
|
2026-08-19 20:48:59 -03:00
|
|
|
return await http_image.fetch(url, cache_flags)
|
|
|
|
|
|
|
|
|
|
|
2026-08-19 21:11:04 -03:00
|
|
|
## Downloads a GIF and converts it to a Godot texture. When animated GIFs
|
|
|
|
|
## are enabled, extracts all frames and returns an AnimatedTexture (paused
|
|
|
|
|
## by default — the focus handler unpauses it). Otherwise extracts only
|
|
|
|
|
## the first frame and returns a static ImageTexture.
|
|
|
|
|
## Results are cached in _animated_texture_cache keyed by URL.
|
|
|
|
|
func _fetch_gif_as_texture(url: String, cache_flags: int) -> Texture2D:
|
|
|
|
|
# Return cached result if available.
|
|
|
|
|
if _animated_texture_cache.has(url):
|
|
|
|
|
return _animated_texture_cache[url]
|
|
|
|
|
|
|
|
|
|
var animated: bool = _is_animated_gifs_enabled()
|
2026-08-19 20:48:59 -03:00
|
|
|
var ffmpeg_bin := await _ensure_ffmpeg()
|
|
|
|
|
if ffmpeg_bin.is_empty():
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
var globalized_dir := ProjectSettings.globalize_path(BOXART_DIR)
|
|
|
|
|
var gif_path := globalized_dir + "/_tmp.gif"
|
2026-08-19 21:11:04 -03:00
|
|
|
var frames_dir := globalized_dir + "/_tmp_frames"
|
|
|
|
|
|
|
|
|
|
# Download the GIF bytes via HTTPClient.
|
|
|
|
|
var body := await _download_gif_bytes(url)
|
|
|
|
|
if body.is_empty():
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
# Write GIF to temp file.
|
|
|
|
|
var gif_file := FileAccess.open(gif_path, FileAccess.WRITE)
|
|
|
|
|
if gif_file == null:
|
|
|
|
|
logger.warn("GIF fetch: cannot write temp gif")
|
|
|
|
|
return null
|
|
|
|
|
gif_file.store_buffer(body)
|
|
|
|
|
gif_file.close()
|
|
|
|
|
|
|
|
|
|
var result: Texture2D
|
|
|
|
|
|
|
|
|
|
if animated:
|
|
|
|
|
result = await _build_animated_gif_texture(ffmpeg_bin, gif_path, frames_dir, body, url)
|
|
|
|
|
else:
|
|
|
|
|
result = await _build_static_gif_texture(ffmpeg_bin, gif_path, globalized_dir + "/_tmp.png", url)
|
|
|
|
|
|
|
|
|
|
# Clean up.
|
|
|
|
|
DirAccess.remove_absolute(gif_path)
|
|
|
|
|
|
|
|
|
|
# Cache the result.
|
|
|
|
|
if result != null:
|
|
|
|
|
_animated_texture_cache[url] = result
|
|
|
|
|
|
|
|
|
|
return result
|
2026-08-19 20:48:59 -03:00
|
|
|
|
2026-08-19 21:11:04 -03:00
|
|
|
|
|
|
|
|
## Downloads GIF bytes from the given URL. Returns empty PackedByteArray on failure.
|
|
|
|
|
func _download_gif_bytes(url: String) -> PackedByteArray:
|
2026-08-19 21:17:00 -03:00
|
|
|
return await _download_url_bytes(url)
|
2026-08-19 20:48:59 -03:00
|
|
|
|
|
|
|
|
|
2026-08-19 21:11:04 -03:00
|
|
|
## Extracts the first frame of a GIF using ffmpeg. Returns a static ImageTexture.
|
|
|
|
|
func _build_static_gif_texture(ffmpeg_bin: String, gif_path: String, png_path: String, url: String) -> Texture2D:
|
2026-08-19 20:48:59 -03:00
|
|
|
var ffmpeg_args: PackedStringArray = [
|
|
|
|
|
"-y", "-i", gif_path,
|
|
|
|
|
"-frames:v", "1",
|
|
|
|
|
"-f", "image2", png_path,
|
|
|
|
|
]
|
|
|
|
|
var ret: Array = []
|
|
|
|
|
OS.execute(ffmpeg_bin, ffmpeg_args, ret)
|
|
|
|
|
|
|
|
|
|
if not FileAccess.file_exists(png_path):
|
|
|
|
|
logger.warn("GIF fetch: ffmpeg did not produce output for %s" % url.left(LOG_URL_MAX_LEN))
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
var image := Image.load_from_file(png_path)
|
|
|
|
|
DirAccess.remove_absolute(png_path)
|
|
|
|
|
|
|
|
|
|
if image == null:
|
|
|
|
|
logger.warn("GIF fetch: failed to load extracted PNG")
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
logger.info("GIF fetch: extracted first frame from %s" % url.left(LOG_URL_MAX_LEN))
|
|
|
|
|
return ImageTexture.create_from_image(image)
|
|
|
|
|
|
|
|
|
|
|
2026-08-19 21:11:04 -03:00
|
|
|
## Extracts all frames from a GIF using ffmpeg and builds an AnimatedTexture.
|
|
|
|
|
## The animation is paused by default — the focus handler unpauses it.
|
|
|
|
|
func _build_animated_gif_texture(ffmpeg_bin: String, gif_path: String, frames_dir: String, gif_bytes: PackedByteArray, url: String) -> AnimatedTexture:
|
|
|
|
|
var delays: Array = _parse_gif_frame_delays(gif_bytes)
|
2026-08-19 21:17:00 -03:00
|
|
|
var frame_files: PackedStringArray = _extract_gif_frames(ffmpeg_bin, gif_path, frames_dir)
|
|
|
|
|
if frame_files.is_empty():
|
|
|
|
|
return null
|
|
|
|
|
return _assemble_animated_texture(frame_files, frames_dir, delays, url)
|
2026-08-19 21:11:04 -03:00
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
|
|
|
|
|
## Extracts all frames from a GIF file using ffmpeg. Returns sorted frame filenames.
|
|
|
|
|
func _extract_gif_frames(ffmpeg_bin: String, gif_path: String, frames_dir: String) -> PackedStringArray:
|
2026-08-19 21:11:04 -03:00
|
|
|
DirAccess.make_dir_recursive_absolute(frames_dir)
|
|
|
|
|
var ffmpeg_args: PackedStringArray = [
|
|
|
|
|
"-y", "-i", gif_path,
|
|
|
|
|
"-vsync", "0",
|
|
|
|
|
frames_dir + "/frame_%04d.png",
|
|
|
|
|
]
|
|
|
|
|
var ret: Array = []
|
|
|
|
|
OS.execute(ffmpeg_bin, ffmpeg_args, ret)
|
|
|
|
|
|
|
|
|
|
var dir := DirAccess.open(frames_dir)
|
|
|
|
|
if dir == null:
|
|
|
|
|
logger.warn("GIF fetch: cannot open frames dir")
|
2026-08-19 21:17:00 -03:00
|
|
|
return PackedStringArray()
|
2026-08-19 21:11:04 -03:00
|
|
|
dir.list_dir_begin()
|
|
|
|
|
var frame_files: PackedStringArray = []
|
|
|
|
|
var fname := dir.get_next()
|
|
|
|
|
while fname != "":
|
|
|
|
|
if fname.begins_with("frame_") and fname.ends_with(".png"):
|
|
|
|
|
frame_files.append(fname)
|
|
|
|
|
fname = dir.get_next()
|
|
|
|
|
dir.list_dir_end()
|
|
|
|
|
frame_files.sort()
|
2026-08-19 21:17:00 -03:00
|
|
|
return frame_files
|
2026-08-19 21:11:04 -03:00
|
|
|
|
|
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
## Loads extracted frame images and assembles them into an AnimatedTexture.
|
|
|
|
|
func _assemble_animated_texture(frame_files: PackedStringArray, frames_dir: String, delays: Array, url: String) -> AnimatedTexture:
|
2026-08-19 21:11:04 -03:00
|
|
|
var anim_tex := AnimatedTexture.new()
|
|
|
|
|
anim_tex.frames_count = frame_files.size()
|
|
|
|
|
anim_tex.pause = true # Start paused — focus handler unpauses.
|
|
|
|
|
|
|
|
|
|
for i in frame_files.size():
|
|
|
|
|
var frame_path := frames_dir + "/" + frame_files[i]
|
|
|
|
|
var image := Image.load_from_file(frame_path)
|
|
|
|
|
DirAccess.remove_absolute(frame_path)
|
|
|
|
|
if image == null:
|
|
|
|
|
logger.warn("GIF fetch: failed to load frame %d" % i)
|
|
|
|
|
continue
|
|
|
|
|
anim_tex.set_frame_texture(i, ImageTexture.create_from_image(image))
|
|
|
|
|
var delay_ms: int = delays[i] if i < delays.size() else GIF_DEFAULT_FRAME_DELAY_MS
|
|
|
|
|
anim_tex.set_frame_delay(i, delay_ms / 1000.0) # AnimatedTexture uses seconds.
|
|
|
|
|
|
|
|
|
|
DirAccess.remove_absolute(frames_dir)
|
|
|
|
|
logger.info("GIF fetch: built AnimatedTexture with %d frames from %s" % [frame_files.size(), url.left(LOG_URL_MAX_LEN)])
|
|
|
|
|
return anim_tex
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Parses frame delays from a GIF89a binary. Returns an array of delays
|
|
|
|
|
## in milliseconds, one per frame. Falls back to GIF_DEFAULT_FRAME_DELAY_MS
|
|
|
|
|
## for frames without a Graphics Control Extension.
|
|
|
|
|
func _parse_gif_frame_delays(data: PackedByteArray) -> Array:
|
|
|
|
|
var delays: Array = []
|
|
|
|
|
if data.size() < 13:
|
|
|
|
|
return delays
|
|
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
var pos := _skip_gif_header(data)
|
2026-08-19 21:11:04 -03:00
|
|
|
while pos < data.size() - 1:
|
|
|
|
|
var byte: int = data[pos]
|
|
|
|
|
if byte == 0x21:
|
2026-08-19 21:17:00 -03:00
|
|
|
var result: Array = _parse_gif_extension(data, pos)
|
|
|
|
|
if result[0] >= 0:
|
|
|
|
|
delays.append(result[0])
|
|
|
|
|
pos = result[1]
|
2026-08-19 21:11:04 -03:00
|
|
|
elif byte == 0x2C:
|
2026-08-19 21:17:00 -03:00
|
|
|
pos = _skip_gif_image_descriptor(data, pos)
|
2026-08-19 21:11:04 -03:00
|
|
|
elif byte == 0x3B:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
pos += 1
|
|
|
|
|
|
|
|
|
|
return delays
|
|
|
|
|
|
|
|
|
|
|
2026-08-19 21:17:00 -03:00
|
|
|
## Skips the GIF header, Logical Screen Descriptor, and Global Color Table.
|
|
|
|
|
## Returns the byte offset past all header data.
|
|
|
|
|
func _skip_gif_header(data: PackedByteArray) -> int:
|
|
|
|
|
var pos := 13 # Header (6) + Logical Screen Descriptor (7).
|
|
|
|
|
var packed_byte: int = data[10]
|
|
|
|
|
if packed_byte & 0x80 != 0:
|
|
|
|
|
var gct_size := 3 * (1 << ((packed_byte & 0x07) + 1))
|
|
|
|
|
pos += gct_size
|
|
|
|
|
return pos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Parses an extension block starting at `pos`. Returns [delay_ms, new_pos].
|
|
|
|
|
## If the extension is not a Graphics Control Extension, returns [-1, new_pos].
|
|
|
|
|
func _parse_gif_extension(data: PackedByteArray, pos: int) -> Array:
|
|
|
|
|
var label: int = data[pos + 1]
|
|
|
|
|
var delay_ms := -1
|
|
|
|
|
if label == 0xF9:
|
|
|
|
|
var delay_cs: int = data[pos + 4] | (data[pos + 5] << 8)
|
|
|
|
|
delay_ms = delay_cs * 10 if delay_cs > 0 else GIF_DEFAULT_FRAME_DELAY_MS
|
|
|
|
|
pos += 2
|
|
|
|
|
while pos < data.size():
|
|
|
|
|
var block_size: int = data[pos]
|
|
|
|
|
pos += 1
|
|
|
|
|
if block_size == 0:
|
|
|
|
|
break
|
|
|
|
|
pos += block_size
|
|
|
|
|
return [delay_ms, pos]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Skips an image descriptor and its associated data at `pos`.
|
|
|
|
|
func _skip_gif_image_descriptor(data: PackedByteArray, pos: int) -> int:
|
|
|
|
|
pos += 10
|
|
|
|
|
var img_packed: int = data[pos - 1]
|
|
|
|
|
if img_packed & 0x80 != 0:
|
|
|
|
|
var lct_size := 3 * (1 << ((img_packed & 0x07) + 1))
|
|
|
|
|
pos += lct_size
|
|
|
|
|
pos += 1 # LZW Minimum Code Size.
|
|
|
|
|
while pos < data.size():
|
|
|
|
|
var block_size: int = data[pos]
|
|
|
|
|
pos += 1
|
|
|
|
|
if block_size == 0:
|
|
|
|
|
break
|
|
|
|
|
pos += block_size
|
|
|
|
|
return pos
|
|
|
|
|
|
|
|
|
|
|
2026-08-17 01:51:54 -03:00
|
|
|
## Combines two textures side-by-side into a single banner image.
|
|
|
|
|
## Both images are scaled to the same height, then placed left and right.
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
func _combine_side_by_side(left: Texture2D, right: Texture2D) -> Texture2D:
|
|
|
|
|
var img_a := left.get_image()
|
|
|
|
|
var img_b := right.get_image()
|
2026-08-17 01:51:54 -03:00
|
|
|
if img_a == null or img_b == null:
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
return left
|
2026-08-17 01:51:54 -03:00
|
|
|
|
|
|
|
|
# Scale both to the same height (use the taller one).
|
|
|
|
|
var target_h: int = max(img_a.get_height(), img_b.get_height())
|
|
|
|
|
if img_a.get_height() != target_h:
|
|
|
|
|
var scale: float = float(target_h) / float(img_a.get_height())
|
|
|
|
|
img_a.resize(int(img_a.get_width() * scale), target_h, Image.INTERPOLATE_BILINEAR)
|
|
|
|
|
if img_b.get_height() != target_h:
|
|
|
|
|
var scale: float = float(target_h) / float(img_b.get_height())
|
|
|
|
|
img_b.resize(int(img_b.get_width() * scale), target_h, Image.INTERPOLATE_BILINEAR)
|
|
|
|
|
|
|
|
|
|
var combined := Image.create(img_a.get_width() + img_b.get_width(), target_h, false, img_a.get_format())
|
|
|
|
|
combined.blit_rect(img_a, Rect2i(0, 0, img_a.get_width(), target_h), Vector2i.ZERO)
|
|
|
|
|
combined.blit_rect(img_b, Rect2i(0, 0, img_b.get_width(), target_h), Vector2i(img_a.get_width(), 0))
|
|
|
|
|
|
|
|
|
|
return ImageTexture.create_from_image(combined)
|
|
|
|
|
|
|
|
|
|
|
2026-08-16 20:17:35 -03:00
|
|
|
## Scrapes the itch.io game page for screenshots. The itch.io API does not
|
|
|
|
|
## expose screenshots, so the page HTML is the only source.
|
|
|
|
|
func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
|
|
|
|
var page_url: String = game.get("url", "")
|
|
|
|
|
if page_url.is_empty():
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("No page URL in game dict")
|
2026-08-16 20:17:35 -03:00
|
|
|
return {}
|
|
|
|
|
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.info("Scraping page for screenshots: " + page_url)
|
2026-08-16 20:17:35 -03:00
|
|
|
var http := HTTPClient.new()
|
|
|
|
|
var parts := page_url.split("/")
|
|
|
|
|
var use_tls := parts[0] == "https:"
|
|
|
|
|
var err := http.connect_to_host(parts[2], 443 if use_tls else 80, TLSOptions.client() if use_tls else null)
|
|
|
|
|
if err != OK:
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("HTTP connect failed for %s: %d" % [page_url, err])
|
2026-08-16 20:17:35 -03:00
|
|
|
return {}
|
2026-08-19 16:20:21 -03:00
|
|
|
var deadline := Time.get_ticks_msec() + HTTP_TIMEOUT_MS
|
2026-08-16 20:17:35 -03:00
|
|
|
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
|
|
|
|
|
http.poll()
|
|
|
|
|
if Time.get_ticks_msec() > deadline:
|
|
|
|
|
http.close()
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("HTTP connect timed out for: " + page_url)
|
2026-08-16 20:17:35 -03:00
|
|
|
return {}
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
OS.delay_msec(CONNECT_POLL_DELAY_MS)
|
2026-08-16 20:17:35 -03:00
|
|
|
if http.get_status() != HTTPClient.STATUS_CONNECTED:
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("HTTP not connected for %s: status=%d" % [page_url, http.get_status()])
|
2026-08-16 20:17:35 -03:00
|
|
|
http.close()
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
http.request(HTTPClient.METHOD_GET, "/" + "/".join(parts.slice(3)), PackedStringArray())
|
2026-08-19 16:20:21 -03:00
|
|
|
deadline = Time.get_ticks_msec() + HTTP_TIMEOUT_MS
|
2026-08-16 20:17:35 -03:00
|
|
|
while http.get_status() == HTTPClient.STATUS_REQUESTING:
|
|
|
|
|
http.poll()
|
|
|
|
|
if Time.get_ticks_msec() > deadline:
|
|
|
|
|
http.close()
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("HTTP request timed out for: " + page_url)
|
2026-08-16 20:17:35 -03:00
|
|
|
return {}
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
OS.delay_msec(READ_POLL_DELAY_MS)
|
2026-08-16 20:17:35 -03:00
|
|
|
|
|
|
|
|
var body := PackedByteArray()
|
|
|
|
|
while http.get_status() == HTTPClient.STATUS_BODY:
|
|
|
|
|
http.poll()
|
|
|
|
|
var chunk: PackedByteArray = http.read_response_body_chunk()
|
|
|
|
|
if chunk.is_empty():
|
|
|
|
|
if Time.get_ticks_msec() > deadline:
|
|
|
|
|
http.close()
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("HTTP body read timed out for: " + page_url)
|
2026-08-16 20:17:35 -03:00
|
|
|
return {}
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
OS.delay_msec(READ_POLL_DELAY_MS)
|
2026-08-16 20:17:35 -03:00
|
|
|
continue
|
|
|
|
|
body.append_array(chunk)
|
|
|
|
|
http.close()
|
|
|
|
|
|
|
|
|
|
var html := body.get_string_from_utf8()
|
|
|
|
|
if html.is_empty():
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.warn("Empty HTML response for: " + page_url)
|
2026-08-16 20:17:35 -03:00
|
|
|
return {}
|
|
|
|
|
|
2026-08-19 17:32:25 -03:00
|
|
|
# Detect Cloudflare challenge pages — no point extracting screenshots.
|
|
|
|
|
if html.find("challenge-platform") != -1 or html.find("cf-browser-verification") != -1:
|
|
|
|
|
logger.warn("Cloudflare challenge detected for: %s — falling back to cover art" % page_url)
|
|
|
|
|
return {}
|
|
|
|
|
|
2026-08-16 20:17:35 -03:00
|
|
|
var shot_re := RegEx.new()
|
2026-08-19 16:20:21 -03:00
|
|
|
shot_re.compile(SCREENSHOT_REGEX_PATTERN)
|
2026-08-16 20:17:35 -03:00
|
|
|
var screenshots: Array = []
|
|
|
|
|
for m in shot_re.search_all(html):
|
2026-08-19 18:08:35 -03:00
|
|
|
var url: String = _sanitize_url(m.get_string(0))
|
2026-08-17 01:48:46 -03:00
|
|
|
screenshots.append(url)
|
2026-08-16 20:17:35 -03:00
|
|
|
|
2026-08-17 10:55:56 -03:00
|
|
|
logger.info("Found %d screenshots for page %s" % [screenshots.size(), page_url])
|
2026-08-16 20:17:35 -03:00
|
|
|
return {"screenshots": screenshots}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _load_enriched_meta() -> Dictionary:
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
var raw: Variant = Cache.get_json(_meta_cache_dir, META_CACHE_FILE)
|
2026-08-16 20:17:35 -03:00
|
|
|
if typeof(raw) != TYPE_DICTIONARY:
|
|
|
|
|
return {}
|
|
|
|
|
var out := {}
|
|
|
|
|
for key in raw:
|
|
|
|
|
out[int(key)] = raw[key]
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _save_enriched_meta(game_id: int, meta: Dictionary) -> void:
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
var raw: Variant = Cache.get_json(_meta_cache_dir, META_CACHE_FILE)
|
2026-08-16 20:17:35 -03:00
|
|
|
if typeof(raw) != TYPE_DICTIONARY:
|
|
|
|
|
raw = {}
|
|
|
|
|
raw[str(game_id)] = meta
|
readability: rename constants, extract poll intervals, remove dead code
- BOXART_DIR, META_CACHE_FILE, _meta_cache_dir, _enriched_meta
- _find_game_dict, _combine_side_by_side(left, right)
- CONNECT_POLL_DELAY_MS, READ_POLL_DELAY_MS, LOG_URL_MAX_LEN
- Remove dead _cover_url function
- Fix fall-through comment, boxart_provider variable name
2026-08-19 16:41:33 -03:00
|
|
|
Cache.save_json(_meta_cache_dir, META_CACHE_FILE, raw)
|