Compare commits

...

3 commits

Author SHA1 Message Date
d842605098 Bump version to v0.1.5
All checks were successful
Build plugin / build (push) Successful in 1m7s
2026-08-19 16:42:25 -03:00
e3281f7587 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
75f0463bc6 readability: extract magic constants
- HTTP_TIMEOUT_MS for HTTP connection/request timeout
- SCREENSHOT_REGEX_PATTERN for itch.io screenshot URL regex
2026-08-19 16:20:21 -03:00
3 changed files with 35 additions and 41 deletions

View file

@ -10,13 +10,18 @@ extends BoxArtProvider
## (the itch.io API does not expose them), caches the result in
## itch_art_meta.json, and uses HTTPImageFetcher for async image downloads.
const _boxart_dir := "user://boxart/itch"
const _meta_cache_file := "itch_art_meta.json"
const BOXART_DIR := "user://boxart/itch"
const META_CACHE_FILE := "itch_art_meta.json"
const HTTP_TIMEOUT_MS := 15000
const SCREENSHOT_REGEX_PATTERN := "https://img\\.itch\\.zone/aW1hZ2Uv[^\"]*/original/[^\"]*"
const CONNECT_POLL_DELAY_MS := 50
const READ_POLL_DELAY_MS := 10
const LOG_URL_MAX_LEN := 80
@export var use_caching: bool = true
var http_image := HTTPImageFetcher.new()
var _cache_dir := "itch_art"
var _enriched: Dictionary = {}
var _meta_cache_dir := "itch_art"
var _enriched_meta: Dictionary = {}
var layout_map: Dictionary = {
LAYOUT.GRID_PORTRAIT: "-portrait",
@ -28,14 +33,14 @@ var layout_map: Dictionary = {
func _init() -> void:
super()
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(_boxart_dir))
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(BOXART_DIR))
provider_id = "itch"
logger_name = "BoxArtItch"
func _ready() -> void:
super()
_enriched = _load_enriched_meta()
_enriched_meta = _load_enriched_meta()
logger.info("itch.io Art Provider loaded")
add_child(http_image)
@ -45,7 +50,7 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
logger.error("Unsupported boxart layout: {0}".format([kind]))
return null
var game := _itch_game(item)
var game := _find_game_dict(item)
if game.is_empty():
logger.warn("No itch.io game metadata found for: " + item.name)
return null
@ -58,14 +63,14 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
return null
# Enrich on first access: scrape the itch.io page for screenshots.
var meta: Dictionary = _enriched.get(game_id, {})
var meta: Dictionary = _enriched_meta.get(game_id, {})
if meta.is_empty() and game.has("url"):
logger.info("Enriching metadata for: %s (url=%s)" % [title, game.get("url", "")])
meta = _fetch_game_page_metadata(game)
if meta.is_empty():
logger.warn("Page scrape returned no metadata for: %s" % title)
else:
_enriched[game_id] = meta
_enriched_meta[game_id] = meta
_save_enriched_meta(game_id, meta)
elif not meta.is_empty():
logger.info("Using cached metadata for: %s (%d screenshots)" % [title, meta.get("screenshots", []).size()])
@ -84,7 +89,7 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
if cover_url.is_empty():
logger.warn("No cover URL for: %s (id=%d)" % [title, game_id])
return null
logger.info("cover_url=%s" % cover_url.left(80))
logger.info("cover_url=%s" % cover_url.left(LOG_URL_MAX_LEN))
var cache_flags := Cache.FLAGS.NONE
if use_caching:
@ -97,7 +102,7 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
var tex_b := await http_image.fetch(non_gif_screenshots[1], cache_flags)
if tex_a != null and tex_b != null:
return _combine_side_by_side(tex_a, tex_b)
# Fall through to single-image path if either failed.
# Banner combine failed — fall back to single-image path.
logger.warn("Banner screenshot fetch failed: tex_a=%s tex_b=%s" % [str(tex_a != null), str(tex_b != null)])
if tex_a != null:
return tex_a
@ -109,15 +114,15 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
logger.warn("URL for layout %s is empty (cover=%s)" % [str(kind), cover_url])
return null
logger.info("Fetching itch.io box art for: %s layout=%s url=%s" % [item.name, str(kind), url.left(80)])
logger.info("Fetching itch.io box art for: %s layout=%s url=%s" % [item.name, str(kind), url.left(LOG_URL_MAX_LEN)])
var texture: Texture2D = await http_image.fetch(url, cache_flags)
if texture == null:
logger.warn("Image download returned null for: %s url=%s" % [item.name, url.left(80)])
logger.warn("Image download returned null for: %s url=%s" % [item.name, url.left(LOG_URL_MAX_LEN)])
return texture
## Finds the itch.io game dict from the library item's launch items.
func _itch_game(item: LibraryItem) -> Dictionary:
func _find_game_dict(item: LibraryItem) -> Dictionary:
if item.launch_items.is_empty():
logger.warn("Item has no launch items: " + item.name)
return {}
@ -154,17 +159,6 @@ func _url_for_layout(kind: LAYOUT, cover_url: String, screenshots: Array) -> Str
return ""
func _cover_url(game: Dictionary) -> String:
var still: String = game.get("stillCoverUrl", "")
if not still.is_empty() and not still.to_lower().ends_with(".gif"):
return still
# stillCoverUrl is empty or is a GIF — prefer it if non-GIF alternatives
# aren't available, but log a warning.
if not still.is_empty():
logger.debug("stillCoverUrl is a GIF, will try alternatives: " + still)
return game.get("coverUrl", "")
## Returns a non-GIF URL suitable for portrait/logo from the game data.
## Falls back through: stillCoverUrl → coverUrl (non-GIF) → first non-GIF
## screenshot.
@ -187,11 +181,11 @@ func _best_portrait_url(game: Dictionary, screenshots: Array) -> String:
## Combines two textures side-by-side into a single banner image.
## Both images are scaled to the same height, then placed left and right.
func _combine_side_by_side(a: Texture2D, b: Texture2D) -> Texture2D:
var img_a := a.get_image()
var img_b := b.get_image()
func _combine_side_by_side(left: Texture2D, right: Texture2D) -> Texture2D:
var img_a := left.get_image()
var img_b := right.get_image()
if img_a == null or img_b == null:
return a
return left
# Scale both to the same height (use the taller one).
var target_h: int = max(img_a.get_height(), img_b.get_height())
@ -225,28 +219,28 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
if err != OK:
logger.warn("HTTP connect failed for %s: %d" % [page_url, err])
return {}
var deadline := Time.get_ticks_msec() + 15000
var deadline := Time.get_ticks_msec() + HTTP_TIMEOUT_MS
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
http.poll()
if Time.get_ticks_msec() > deadline:
http.close()
logger.warn("HTTP connect timed out for: " + page_url)
return {}
OS.delay_msec(50)
OS.delay_msec(CONNECT_POLL_DELAY_MS)
if http.get_status() != HTTPClient.STATUS_CONNECTED:
logger.warn("HTTP not connected for %s: status=%d" % [page_url, http.get_status()])
http.close()
return {}
http.request(HTTPClient.METHOD_GET, "/" + "/".join(parts.slice(3)), PackedStringArray())
deadline = Time.get_ticks_msec() + 15000
deadline = Time.get_ticks_msec() + HTTP_TIMEOUT_MS
while http.get_status() == HTTPClient.STATUS_REQUESTING:
http.poll()
if Time.get_ticks_msec() > deadline:
http.close()
logger.warn("HTTP request timed out for: " + page_url)
return {}
OS.delay_msec(10)
OS.delay_msec(READ_POLL_DELAY_MS)
var body := PackedByteArray()
while http.get_status() == HTTPClient.STATUS_BODY:
@ -257,7 +251,7 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
http.close()
logger.warn("HTTP body read timed out for: " + page_url)
return {}
OS.delay_msec(10)
OS.delay_msec(READ_POLL_DELAY_MS)
continue
body.append_array(chunk)
http.close()
@ -268,7 +262,7 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
return {}
var shot_re := RegEx.new()
shot_re.compile("https://img\\.itch\\.zone/aW1hZ2Uv[^\"]*/original/[^\"]*")
shot_re.compile(SCREENSHOT_REGEX_PATTERN)
var screenshots: Array = []
for m in shot_re.search_all(html):
var url: String = m.get_string(0)
@ -282,7 +276,7 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
func _load_enriched_meta() -> Dictionary:
var raw: Variant = Cache.get_json(_cache_dir, _meta_cache_file)
var raw: Variant = Cache.get_json(_meta_cache_dir, META_CACHE_FILE)
if typeof(raw) != TYPE_DICTIONARY:
return {}
var out := {}
@ -292,8 +286,8 @@ func _load_enriched_meta() -> Dictionary:
func _save_enriched_meta(game_id: int, meta: Dictionary) -> void:
var raw: Variant = Cache.get_json(_cache_dir, _meta_cache_file)
var raw: Variant = Cache.get_json(_meta_cache_dir, META_CACHE_FILE)
if typeof(raw) != TYPE_DICTIONARY:
raw = {}
raw[str(game_id)] = meta
Cache.save_json(_cache_dir, _meta_cache_file, raw)
Cache.save_json(_meta_cache_dir, META_CACHE_FILE, raw)

View file

@ -10,6 +10,6 @@ var icon := preload("res://plugins/itch-artprovider/assets/itch.svg")
func _ready() -> void:
logger = Log.get_logger("ItchArt", Log.LEVEL.INFO)
var boxart: BoxArtProvider = load("res://plugins/itch-artprovider/core/boxart_itch.tscn").instantiate()
add_child(boxart)
var boxart_provider: BoxArtProvider = load("res://plugins/itch-artprovider/core/boxart_itch.tscn").instantiate()
add_child(boxart_provider)
logger.info("itch.io Art Provider loaded")

View file

@ -1,7 +1,7 @@
{
"plugin.id": "itch-artprovider",
"plugin.name": "itch.io Art Provider",
"plugin.version": "0.1.4",
"plugin.version": "0.1.5",
"plugin.min-api-version": "1.1.0",
"plugin.link": "https://forge.thergic.ar/jose/itchio-opengamepadui-artprovider-plugin",
"plugin.source": "https://forge.thergic.ar/jose/itchio-opengamepadui-artprovider-plugin",