Compare commits
13 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 436865f3cb | |||
| dfe94b92be | |||
| 494a00bb2a | |||
| d9e59a411f | |||
| 49fdbe8f0f | |||
| 157593e853 | |||
| ce30f647c8 | |||
| 75a6244fd4 | |||
| 2ed80ec8a1 | |||
| a54c90f7c0 | |||
| 3f5c45a99d | |||
| 208dc58f28 | |||
| 9be6847f14 |
4 changed files with 365 additions and 117 deletions
25
core/artprovider_settings.gd
Normal file
25
core/artprovider_settings.gd
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
extends VBoxContainer
|
||||||
|
|
||||||
|
## Settings screen for the itch.io art provider.
|
||||||
|
##
|
||||||
|
## Provides a toggle for animated GIF covers. When enabled, GIF cover art
|
||||||
|
## is extracted frame-by-frame and displayed as an AnimatedTexture that
|
||||||
|
## pauses when the card loses focus.
|
||||||
|
|
||||||
|
var settings_manager := load("res://core/global/settings_manager.tres") as SettingsManager
|
||||||
|
|
||||||
|
const SETTING_SECTION := "plugin.artprovider"
|
||||||
|
const SETTING_ANIMATED_GIFS := "animated_gifs"
|
||||||
|
|
||||||
|
@onready var animated_toggle: Toggle = $%AnimatedGifsToggle
|
||||||
|
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
animated_toggle.button_pressed = settings_manager.get_value(
|
||||||
|
SETTING_SECTION, SETTING_ANIMATED_GIFS, false
|
||||||
|
) as bool
|
||||||
|
animated_toggle.toggled.connect(_on_animated_gifs_toggled)
|
||||||
|
|
||||||
|
|
||||||
|
func _on_animated_gifs_toggled(pressed: bool) -> void:
|
||||||
|
settings_manager.set_value(SETTING_SECTION, SETTING_ANIMATED_GIFS, pressed)
|
||||||
20
core/artprovider_settings.tscn
Normal file
20
core/artprovider_settings.tscn
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
[gd_scene load_steps=3 format=3]
|
||||||
|
|
||||||
|
[ext_resource type="Script" path="res://plugins/itch-artprovider/core/artprovider_settings.gd" id="1"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://d1qb7euwlu7bh" path="res://core/ui/components/toggle.tscn" id="2"]
|
||||||
|
|
||||||
|
[node name="ArtProviderSettings" type="VBoxContainer"]
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
grow_horizontal = 2
|
||||||
|
grow_vertical = 2
|
||||||
|
theme_override_constants/separation = 10
|
||||||
|
script = ExtResource("1")
|
||||||
|
|
||||||
|
[node name="AnimatedGifsToggle" parent="." instance=ExtResource("2")]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
layout_mode = 2
|
||||||
|
text = "Animate GIF covers"
|
||||||
|
description = "Extract all frames from animated GIF covers. Uses more memory but shows the full animation. Pauses when the card is not focused."
|
||||||
|
button_pressed = false
|
||||||
|
|
@ -15,9 +15,13 @@ const META_CACHE_FILE := "itch_art_meta.json"
|
||||||
const HTTP_TIMEOUT_MS := 15000
|
const HTTP_TIMEOUT_MS := 15000
|
||||||
const SCREENSHOT_REGEX_PATTERN := "https://img\\.itch\\.zone/aW1hZ2Uv[\\w+/=]*/original/[\\w+/=]*\\.[a-z]+"
|
const SCREENSHOT_REGEX_PATTERN := "https://img\\.itch\\.zone/aW1hZ2Uv[\\w+/=]*/original/[\\w+/=]*\\.[a-z]+"
|
||||||
const IMAGE_EXTS := ["png", "jpg", "jpeg", "gif", "webp", "bmp"]
|
const IMAGE_EXTS := ["png", "jpg", "jpeg", "gif", "webp", "bmp"]
|
||||||
|
const FFMPEG_BUILDS_URL := "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest"
|
||||||
const CONNECT_POLL_DELAY_MS := 50
|
const CONNECT_POLL_DELAY_MS := 50
|
||||||
const READ_POLL_DELAY_MS := 10
|
const READ_POLL_DELAY_MS := 10
|
||||||
const LOG_URL_MAX_LEN := 80
|
const LOG_URL_MAX_LEN := 80
|
||||||
|
const CACHE_DIR := "images"
|
||||||
|
const BANNER_CACHE_DIR := "banners"
|
||||||
|
const GIF_STATIC_DIR := "gif_static"
|
||||||
|
|
||||||
@export var use_caching: bool = true
|
@export var use_caching: bool = true
|
||||||
var http_image := HTTPImageFetcher.new()
|
var http_image := HTTPImageFetcher.new()
|
||||||
|
|
@ -31,10 +35,14 @@ var layout_map: Dictionary = {
|
||||||
LAYOUT.LOGO: "-logo",
|
LAYOUT.LOGO: "-logo",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Resolved path to the ffmpeg binary (system or bundled). Populated lazily.
|
||||||
|
var _ffmpeg_bin: String = ""
|
||||||
|
|
||||||
|
|
||||||
func _init() -> void:
|
func _init() -> void:
|
||||||
super()
|
super()
|
||||||
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(BOXART_DIR))
|
var globalized := ProjectSettings.globalize_path(BOXART_DIR)
|
||||||
|
DirAccess.make_dir_recursive_absolute(globalized)
|
||||||
provider_id = "itch"
|
provider_id = "itch"
|
||||||
logger_name = "BoxArtItch"
|
logger_name = "BoxArtItch"
|
||||||
|
|
||||||
|
|
@ -48,25 +56,84 @@ func _ready() -> void:
|
||||||
|
|
||||||
## Strips HTML srcset artifacts from a URL. Butlerd sometimes passes through
|
## Strips HTML srcset artifacts from a URL. Butlerd sometimes passes through
|
||||||
## itch.io cover URLs with trailing srcset descriptors like " 1x, h" or " 2x".
|
## 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:
|
func _sanitize_url(url: String) -> String:
|
||||||
if url.is_empty():
|
if url.is_empty():
|
||||||
return url
|
return url
|
||||||
# Srcset descriptors are separated by spaces — take only the URL portion.
|
|
||||||
var clean: String = url.split(" ")[0]
|
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()
|
var ext: String = clean.get_extension().to_lower()
|
||||||
if ext not in IMAGE_EXTS:
|
if ext not in IMAGE_EXTS:
|
||||||
logger.warn("Sanitized URL has unexpected extension '%s': %s" % [ext, clean.left(LOG_URL_MAX_LEN)])
|
logger.warn("Sanitized URL has unexpected extension '%s': %s" % [ext, clean.left(LOG_URL_MAX_LEN)])
|
||||||
return clean
|
return clean
|
||||||
|
|
||||||
|
|
||||||
## Returns whether a URL points to a GIF image. Handles both clean URLs and
|
## Returns whether a URL points to a GIF image.
|
||||||
## URLs contaminated with srcset artifacts (e.g. "image.gif 1x, h").
|
|
||||||
func _is_gif_url(url: String) -> bool:
|
func _is_gif_url(url: String) -> bool:
|
||||||
return _sanitize_url(url).to_lower().ends_with(".gif")
|
return _sanitize_url(url).to_lower().ends_with(".gif")
|
||||||
|
|
||||||
|
|
||||||
|
## Returns whether a URL points to a JPEG image.
|
||||||
|
func _is_jpg_url(url: String) -> bool:
|
||||||
|
var clean := _sanitize_url(url).to_lower()
|
||||||
|
return clean.ends_with(".jpg") or clean.ends_with(".jpeg")
|
||||||
|
|
||||||
|
|
||||||
|
## Removes and frees an HTTPRequest node.
|
||||||
|
func _remove_http(http: HTTPRequest) -> void:
|
||||||
|
remove_child(http)
|
||||||
|
http.queue_free()
|
||||||
|
|
||||||
|
|
||||||
|
## Downloads an image and detects the actual format from content-type headers.
|
||||||
|
## Used as a fallback when HTTPImageFetcher fails due to extension mismatch.
|
||||||
|
func _fetch_image_with_format_detection(url: String, cache_flags: int) -> Texture2D:
|
||||||
|
var http := HTTPRequest.new()
|
||||||
|
http.timeout = HTTP_TIMEOUT_MS / 1000.0
|
||||||
|
add_child.call_deferred(http)
|
||||||
|
await http.ready
|
||||||
|
|
||||||
|
if http.request(url) != OK:
|
||||||
|
_remove_http(http)
|
||||||
|
return null
|
||||||
|
|
||||||
|
var args: Array = await http.request_completed
|
||||||
|
var result: int = args[0]
|
||||||
|
var response_code: int = args[1]
|
||||||
|
var headers: PackedStringArray = args[2]
|
||||||
|
var body: PackedByteArray = args[3]
|
||||||
|
_remove_http(http)
|
||||||
|
|
||||||
|
if result != HTTPRequest.RESULT_SUCCESS or response_code != 200:
|
||||||
|
return null
|
||||||
|
|
||||||
|
var content_type := ""
|
||||||
|
for h in headers:
|
||||||
|
if h.to_lower().begins_with("content-type:"):
|
||||||
|
content_type = h.split(":", true, 1)[1].strip_edges().to_lower()
|
||||||
|
break
|
||||||
|
|
||||||
|
var image := Image.new()
|
||||||
|
var err: int = ERR_INVALID_DATA
|
||||||
|
if content_type.find("png") != -1:
|
||||||
|
err = image.load_png_from_buffer(body)
|
||||||
|
elif content_type.find("jpeg") != -1 or content_type.find("jpg") != -1:
|
||||||
|
err = image.load_jpg_from_buffer(body)
|
||||||
|
elif content_type.find("webp") != -1:
|
||||||
|
err = image.load_webp_from_buffer(body)
|
||||||
|
else:
|
||||||
|
err = image.load_png_from_buffer(body)
|
||||||
|
if err != OK:
|
||||||
|
err = image.load_jpg_from_buffer(body)
|
||||||
|
|
||||||
|
if err != OK:
|
||||||
|
logger.warn("Format detection failed for %s (content-type: %s)" % [url.left(LOG_URL_MAX_LEN), content_type])
|
||||||
|
return null
|
||||||
|
|
||||||
|
var texture := ImageTexture.create_from_image(image)
|
||||||
|
if cache_flags & Cache.FLAGS.SAVE:
|
||||||
|
Cache.save_image(CACHE_DIR, url, texture)
|
||||||
|
return texture
|
||||||
|
|
||||||
|
|
||||||
func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
if not kind in layout_map:
|
if not kind in layout_map:
|
||||||
logger.error("Unsupported boxart layout: {0}".format([kind]))
|
logger.error("Unsupported boxart layout: {0}".format([kind]))
|
||||||
|
|
@ -84,14 +151,39 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
logger.warn("Game title is empty for id %d" % game_id)
|
logger.warn("Game title is empty for id %d" % game_id)
|
||||||
return null
|
return null
|
||||||
|
|
||||||
# Enrich on first access: scrape the itch.io page for screenshots.
|
var meta := await _enrich_metadata(game, game_id, title)
|
||||||
# Cached failures (empty screenshots) are stored to avoid repeated
|
var screenshots: Array = meta.get("screenshots", []).map(_sanitize_url)
|
||||||
# requests against Cloudflare-protected pages.
|
var cover_url := _sanitize_url(_best_portrait_url(game, screenshots))
|
||||||
|
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(LOG_URL_MAX_LEN))
|
||||||
|
|
||||||
|
var cache_flags := Cache.FLAGS.NONE
|
||||||
|
if use_caching:
|
||||||
|
cache_flags = Cache.FLAGS.LOAD | Cache.FLAGS.SAVE
|
||||||
|
|
||||||
|
if kind == LAYOUT.BANNER and screenshots.size() >= 2:
|
||||||
|
return await _fetch_banner_textures(item, screenshots, cache_flags)
|
||||||
|
|
||||||
|
var url := _url_for_layout(kind, cover_url, screenshots)
|
||||||
|
if url.is_empty():
|
||||||
|
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(LOG_URL_MAX_LEN)])
|
||||||
|
var texture: Texture2D = await _fetch_image(url, cache_flags)
|
||||||
|
if texture == null:
|
||||||
|
logger.warn("Image download returned null for: %s url=%s" % [item.name, url.left(LOG_URL_MAX_LEN)])
|
||||||
|
return texture
|
||||||
|
|
||||||
|
|
||||||
|
## Enriches game metadata by scraping the itch.io page for screenshots.
|
||||||
|
func _enrich_metadata(game: Dictionary, game_id: int, title: String) -> Dictionary:
|
||||||
var meta: Dictionary = _enriched_meta.get(game_id, {})
|
var meta: Dictionary = _enriched_meta.get(game_id, {})
|
||||||
if meta.is_empty() and game.has("url"):
|
if meta.is_empty() and game.has("url"):
|
||||||
logger.info("Enriching metadata for: %s (url=%s)" % [title, game.get("url", "")])
|
logger.info("Enriching metadata for: %s (url=%s)" % [title, game.get("url", "")])
|
||||||
meta = _fetch_game_page_metadata(game)
|
meta = await _fetch_game_page_metadata(game)
|
||||||
# Cache both successes and failures to avoid repeated requests.
|
|
||||||
_enriched_meta[game_id] = meta
|
_enriched_meta[game_id] = meta
|
||||||
_save_enriched_meta(game_id, meta)
|
_save_enriched_meta(game_id, meta)
|
||||||
if meta.is_empty():
|
if meta.is_empty():
|
||||||
|
|
@ -102,49 +194,33 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
logger.info("Using cached metadata for: %s (%d screenshots)" % [title, meta.get("screenshots", []).size()])
|
logger.info("Using cached metadata for: %s (%d screenshots)" % [title, meta.get("screenshots", []).size()])
|
||||||
else:
|
else:
|
||||||
logger.warn("No URL in game dict for: %s" % title)
|
logger.warn("No URL in game dict for: %s" % title)
|
||||||
|
return meta
|
||||||
|
|
||||||
var screenshots: Array = meta.get("screenshots", []).map(_sanitize_url)
|
|
||||||
# Filter out GIF screenshots — Godot can't decode them.
|
|
||||||
var non_gif_screenshots: Array = screenshots.filter(
|
|
||||||
func(url): return not _is_gif_url(url)
|
|
||||||
)
|
|
||||||
if screenshots.size() != non_gif_screenshots.size():
|
|
||||||
logger.info("Filtered %d GIF screenshots for: %s" % [screenshots.size() - non_gif_screenshots.size(), title])
|
|
||||||
|
|
||||||
var cover_url := _sanitize_url(_best_portrait_url(game, non_gif_screenshots))
|
## Fetches two screenshots and combines them side-by-side for banner layout.
|
||||||
if cover_url.is_empty():
|
func _fetch_banner_textures(item: LibraryItem, screenshots: Array, cache_flags: int) -> Texture2D:
|
||||||
logger.warn("No cover URL for: %s (id=%d)" % [title, game_id])
|
var banner_key: String = screenshots[0] + "||" + screenshots[1]
|
||||||
return null
|
|
||||||
logger.info("cover_url=%s" % cover_url.left(LOG_URL_MAX_LEN))
|
|
||||||
|
|
||||||
var cache_flags := Cache.FLAGS.NONE
|
if cache_flags & Cache.FLAGS.LOAD and Cache.is_cached(BANNER_CACHE_DIR, banner_key):
|
||||||
if use_caching:
|
var cached := Cache.get_image(BANNER_CACHE_DIR, banner_key)
|
||||||
cache_flags = Cache.FLAGS.LOAD | Cache.FLAGS.SAVE
|
if cached != null:
|
||||||
|
logger.info("Banner: loaded cached stitched banner for %s" % item.name)
|
||||||
|
return cached
|
||||||
|
|
||||||
# Banner: combine two screenshots side-by-side when available.
|
logger.info("Fetching itch.io banner (2 screenshots) for: " + item.name)
|
||||||
if kind == LAYOUT.BANNER and non_gif_screenshots.size() >= 2:
|
var tex_a := await _fetch_image(screenshots[0], cache_flags)
|
||||||
logger.info("Fetching itch.io banner (2 screenshots) for: " + item.name)
|
var tex_b := await _fetch_image(screenshots[1], cache_flags)
|
||||||
var tex_a := await http_image.fetch(non_gif_screenshots[0], cache_flags)
|
if tex_a != null and tex_b != null:
|
||||||
var tex_b := await http_image.fetch(non_gif_screenshots[1], cache_flags)
|
var stitched := _combine_side_by_side(tex_a, tex_b)
|
||||||
if tex_a != null and tex_b != null:
|
if cache_flags & Cache.FLAGS.SAVE and stitched != null:
|
||||||
return _combine_side_by_side(tex_a, tex_b)
|
Cache.save_image(BANNER_CACHE_DIR, banner_key, stitched)
|
||||||
# Banner combine failed — fall back to single-image path.
|
return stitched
|
||||||
logger.warn("Banner screenshot fetch failed: tex_a=%s tex_b=%s" % [str(tex_a != null), str(tex_b != null)])
|
logger.warn("Banner screenshot fetch failed: tex_a=%s tex_b=%s" % [str(tex_a != null), str(tex_b != null)])
|
||||||
if tex_a != null:
|
if tex_a != null:
|
||||||
return tex_a
|
return tex_a
|
||||||
if tex_b != null:
|
if tex_b != null:
|
||||||
return tex_b
|
return tex_b
|
||||||
|
return null
|
||||||
var url := _url_for_layout(kind, cover_url, non_gif_screenshots)
|
|
||||||
if url.is_empty():
|
|
||||||
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(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(LOG_URL_MAX_LEN)])
|
|
||||||
return texture
|
|
||||||
|
|
||||||
|
|
||||||
## Finds the itch.io game dict from the library item's launch items.
|
## Finds the itch.io game dict from the library item's launch items.
|
||||||
|
|
@ -170,19 +246,9 @@ func _url_for_layout(kind: LAYOUT, cover_url: String, screenshots: Array) -> Str
|
||||||
LAYOUT.GRID_PORTRAIT, LAYOUT.LOGO:
|
LAYOUT.GRID_PORTRAIT, LAYOUT.LOGO:
|
||||||
return cover_url
|
return cover_url
|
||||||
LAYOUT.GRID_LANDSCAPE:
|
LAYOUT.GRID_LANDSCAPE:
|
||||||
# Prefer a non-GIF screenshot
|
return screenshots[0] if not screenshots.is_empty() else cover_url
|
||||||
for url in screenshots:
|
|
||||||
if not _is_gif_url(url):
|
|
||||||
return url
|
|
||||||
# cover_url is already sanitized and non-GIF by the time it reaches here
|
|
||||||
return cover_url
|
|
||||||
LAYOUT.BANNER:
|
LAYOUT.BANNER:
|
||||||
# Single screenshot fallback (two-screenshot case handled in get_boxart).
|
return screenshots[0] if not screenshots.is_empty() else cover_url
|
||||||
# Prefer a non-GIF screenshot.
|
|
||||||
for url in screenshots:
|
|
||||||
if not _is_gif_url(url):
|
|
||||||
return url
|
|
||||||
return cover_url
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -196,15 +262,193 @@ func _best_portrait_url(game: Dictionary, _screenshots: Array) -> String:
|
||||||
return game.get("coverUrl", "")
|
return game.get("coverUrl", "")
|
||||||
|
|
||||||
|
|
||||||
|
## Ensures an ffmpeg binary is available. Checks the system PATH first,
|
||||||
|
## then falls back to a bundled copy under user://boxart/itch/.
|
||||||
|
func _ensure_ffmpeg() -> String:
|
||||||
|
if not _ffmpeg_bin.is_empty():
|
||||||
|
return _ffmpeg_bin
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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.
|
||||||
|
func _install_ffmpeg() -> bool:
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
func _detect_os() -> String:
|
||||||
|
if OS.get_name() == "Windows":
|
||||||
|
return "win"
|
||||||
|
if OS.get_name() == "macOS":
|
||||||
|
return "macos"
|
||||||
|
return "linux"
|
||||||
|
|
||||||
|
|
||||||
|
func _detect_arch() -> String:
|
||||||
|
if Engine.has_method("get_architecture_name"):
|
||||||
|
var arch: String = Engine.get_architecture_name()
|
||||||
|
if "arm64" in arch or "aarch64" in arch:
|
||||||
|
return "arm64"
|
||||||
|
return "64"
|
||||||
|
|
||||||
|
|
||||||
|
func _build_ffmpeg_url(os_name: String, arch_name: String) -> String:
|
||||||
|
var platform_slug := os_name + arch_name
|
||||||
|
var archive_name := "ffmpeg-master-latest-%s-gpl" % platform_slug
|
||||||
|
var ext := "zip" if os_name == "win" else "tar.xz"
|
||||||
|
return "%s/%s.%s" % [FFMPEG_BUILDS_URL, archive_name, ext]
|
||||||
|
|
||||||
|
|
||||||
|
## Downloads bytes from a URL. Returns empty PackedByteArray on failure.
|
||||||
|
func _download_url_bytes(url: String) -> PackedByteArray:
|
||||||
|
var http := HTTPRequest.new()
|
||||||
|
http.timeout = HTTP_TIMEOUT_MS / 1000.0
|
||||||
|
add_child.call_deferred(http)
|
||||||
|
await http.ready
|
||||||
|
|
||||||
|
if http.request(url) != OK:
|
||||||
|
logger.error("Error requesting: " + url)
|
||||||
|
remove_child(http)
|
||||||
|
http.queue_free()
|
||||||
|
return PackedByteArray()
|
||||||
|
|
||||||
|
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:
|
||||||
|
logger.error("Download failed: HTTP %d for %s" % [response_code, url])
|
||||||
|
return PackedByteArray()
|
||||||
|
return body
|
||||||
|
|
||||||
|
|
||||||
|
## Extracts the ffmpeg binary from the downloaded archive.
|
||||||
|
func _extract_ffmpeg(body: PackedByteArray, os_name: String) -> bool:
|
||||||
|
var globalized_dir := ProjectSettings.globalize_path(BOXART_DIR)
|
||||||
|
DirAccess.make_dir_recursive_absolute(globalized_dir)
|
||||||
|
var is_win: bool = os_name == "win"
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
## Unified image fetch. GIFs go through ffmpeg for first-frame extraction.
|
||||||
|
## Non-GIF URLs go through HTTPImageFetcher, with a format-detection
|
||||||
|
## fallback for .jpg URLs that are actually served as PNG by itch.zone.
|
||||||
|
func _fetch_image(url: String, cache_flags: int) -> Texture2D:
|
||||||
|
if _is_gif_url(url):
|
||||||
|
return await _fetch_gif_as_texture(url, cache_flags)
|
||||||
|
var texture := await http_image.fetch(url, cache_flags)
|
||||||
|
if texture == null and _is_jpg_url(url):
|
||||||
|
texture = await _fetch_image_with_format_detection(url, cache_flags)
|
||||||
|
return texture
|
||||||
|
|
||||||
|
|
||||||
|
## Downloads a GIF and extracts the first frame as a static ImageTexture.
|
||||||
|
## Results are cached on disk under user://boxart/itch/gif_static/<hash>.png
|
||||||
|
## and in memory for the session.
|
||||||
|
func _fetch_gif_as_texture(url: String, cache_flags: int) -> Texture2D:
|
||||||
|
var globalized_dir := ProjectSettings.globalize_path(BOXART_DIR)
|
||||||
|
DirAccess.make_dir_recursive_absolute(globalized_dir + "/" + GIF_STATIC_DIR)
|
||||||
|
var url_hash := url.sha256_text().left(16)
|
||||||
|
var cached_png := globalized_dir + "/" + GIF_STATIC_DIR + "/" + url_hash + ".png"
|
||||||
|
|
||||||
|
# Check disk cache.
|
||||||
|
if cache_flags & Cache.FLAGS.LOAD and FileAccess.file_exists(cached_png):
|
||||||
|
var image := Image.load_from_file(cached_png)
|
||||||
|
if image != null:
|
||||||
|
logger.info("GIF fetch: loaded cached static frame for %s" % url.left(LOG_URL_MAX_LEN))
|
||||||
|
return ImageTexture.create_from_image(image)
|
||||||
|
|
||||||
|
var ffmpeg_bin := await _ensure_ffmpeg()
|
||||||
|
if ffmpeg_bin.is_empty():
|
||||||
|
return null
|
||||||
|
|
||||||
|
var gif_path := globalized_dir + "/_tmp_%s.gif" % url_hash
|
||||||
|
|
||||||
|
var body := await _download_url_bytes(url)
|
||||||
|
if body.is_empty():
|
||||||
|
return null
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
# Extract first frame via ffmpeg.
|
||||||
|
var ffmpeg_args: PackedStringArray = [
|
||||||
|
"-y", "-i", gif_path,
|
||||||
|
"-frames:v", "1",
|
||||||
|
"-f", "image2", cached_png,
|
||||||
|
]
|
||||||
|
var ret: Array = []
|
||||||
|
OS.execute(ffmpeg_bin, ffmpeg_args, ret)
|
||||||
|
|
||||||
|
DirAccess.remove_absolute(gif_path)
|
||||||
|
|
||||||
|
if not FileAccess.file_exists(cached_png):
|
||||||
|
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(cached_png)
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
## Combines two textures side-by-side into a single banner image.
|
## 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(left: Texture2D, right: Texture2D) -> Texture2D:
|
func _combine_side_by_side(left: Texture2D, right: Texture2D) -> Texture2D:
|
||||||
var img_a := left.get_image()
|
var img_a := left.get_image()
|
||||||
var img_b := right.get_image()
|
var img_b := right.get_image()
|
||||||
if img_a == null or img_b == null:
|
if img_a == null or img_b == null:
|
||||||
return left
|
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())
|
var target_h: int = max(img_a.get_height(), img_b.get_height())
|
||||||
if img_a.get_height() != target_h:
|
if img_a.get_height() != target_h:
|
||||||
var scale: float = float(target_h) / float(img_a.get_height())
|
var scale: float = float(target_h) / float(img_a.get_height())
|
||||||
|
|
@ -220,8 +464,7 @@ func _combine_side_by_side(left: Texture2D, right: Texture2D) -> Texture2D:
|
||||||
return ImageTexture.create_from_image(combined)
|
return ImageTexture.create_from_image(combined)
|
||||||
|
|
||||||
|
|
||||||
## Scrapes the itch.io game page for screenshots. The itch.io API does not
|
## Scrapes the itch.io game page for screenshots.
|
||||||
## expose screenshots, so the page HTML is the only source.
|
|
||||||
func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
||||||
var page_url: String = game.get("url", "")
|
var page_url: String = game.get("url", "")
|
||||||
if page_url.is_empty():
|
if page_url.is_empty():
|
||||||
|
|
@ -229,70 +472,30 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
logger.info("Scraping page for screenshots: " + page_url)
|
logger.info("Scraping page for screenshots: " + page_url)
|
||||||
var http := HTTPClient.new()
|
var body := await _download_url_bytes(page_url)
|
||||||
var parts := page_url.split("/")
|
if body.is_empty():
|
||||||
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:
|
|
||||||
logger.warn("HTTP connect failed for %s: %d" % [page_url, err])
|
|
||||||
return {}
|
return {}
|
||||||
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(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() + 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(READ_POLL_DELAY_MS)
|
|
||||||
|
|
||||||
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()
|
|
||||||
logger.warn("HTTP body read timed out for: " + page_url)
|
|
||||||
return {}
|
|
||||||
OS.delay_msec(READ_POLL_DELAY_MS)
|
|
||||||
continue
|
|
||||||
body.append_array(chunk)
|
|
||||||
http.close()
|
|
||||||
|
|
||||||
var html := body.get_string_from_utf8()
|
var html := body.get_string_from_utf8()
|
||||||
if html.is_empty():
|
if html.is_empty():
|
||||||
logger.warn("Empty HTML response for: " + page_url)
|
logger.warn("Empty HTML response for: " + page_url)
|
||||||
return {}
|
return {}
|
||||||
|
if _is_cloudflare_challenge(html):
|
||||||
# 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)
|
logger.warn("Cloudflare challenge detected for: %s — falling back to cover art" % page_url)
|
||||||
return {}
|
return {}
|
||||||
|
return _extract_screenshots_from_html(html, page_url)
|
||||||
|
|
||||||
|
|
||||||
|
func _is_cloudflare_challenge(html: String) -> bool:
|
||||||
|
return html.find("challenge-platform") != -1 or html.find("cf-browser-verification") != -1
|
||||||
|
|
||||||
|
|
||||||
|
func _extract_screenshots_from_html(html: String, page_url: String) -> Dictionary:
|
||||||
var shot_re := RegEx.new()
|
var shot_re := RegEx.new()
|
||||||
shot_re.compile(SCREENSHOT_REGEX_PATTERN)
|
shot_re.compile(SCREENSHOT_REGEX_PATTERN)
|
||||||
var screenshots: Array = []
|
var screenshots: Array = []
|
||||||
for m in shot_re.search_all(html):
|
for m in shot_re.search_all(html):
|
||||||
var url: String = _sanitize_url(m.get_string(0))
|
var url: String = _sanitize_url(m.get_string(0))
|
||||||
if _is_gif_url(url):
|
|
||||||
logger.debug("Skipping GIF screenshot: " + url)
|
|
||||||
continue
|
|
||||||
screenshots.append(url)
|
screenshots.append(url)
|
||||||
|
|
||||||
logger.info("Found %d screenshots for page %s" % [screenshots.size(), page_url])
|
logger.info("Found %d screenshots for page %s" % [screenshots.size(), page_url])
|
||||||
return {"screenshots": screenshots}
|
return {"screenshots": screenshots}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"plugin.id": "itch-artprovider",
|
"plugin.id": "itch-artprovider",
|
||||||
"plugin.name": "itch.io Art Provider",
|
"plugin.name": "itch.io Art Provider",
|
||||||
"plugin.version": "0.1.11",
|
"plugin.version": "0.1.20",
|
||||||
"plugin.min-api-version": "1.1.0",
|
"plugin.min-api-version": "1.1.0",
|
||||||
"plugin.link": "https://forge.thergic.ar/jose/itchio-opengamepadui-artprovider-plugin",
|
"plugin.link": "https://forge.thergic.ar/jose/itchio-opengamepadui-artprovider-plugin",
|
||||||
"plugin.source": "https://forge.thergic.ar/jose/itchio-opengamepadui-artprovider-plugin",
|
"plugin.source": "https://forge.thergic.ar/jose/itchio-opengamepadui-artprovider-plugin",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue