UI Cache issue
This commit is contained in:
@@ -10,6 +10,8 @@ extends PanelContainer
|
||||
|
||||
const DEFAULT_CARD_ART_URL := "https://cdn.aeternalisgames.com/sor-assets/cardart/0_No-Card-Art.jpg"
|
||||
const GRAPHICS_BASE_URL := "https://cdn.aeternalisgames.com/sor-assets/graphics"
|
||||
const CDN_BASE_URL := "https://cdn.aeternalisgames.com"
|
||||
const CDN_PROXY_BASE_URL := "https://hub.aeternalisgames.com/cdn"
|
||||
const REMOTE_CACHE_DIR := "user://remote_texture_cache"
|
||||
const ICON_SP_URL := "%s/card_SP_Icon.png" % GRAPHICS_BASE_URL
|
||||
const ICON_ST_URL := "%s/card_ST_Icon.png" % GRAPHICS_BASE_URL
|
||||
@@ -471,8 +473,8 @@ func _set_remote_texture(target: TextureRect, url: String, fallback_url: String
|
||||
requester.use_threads = true
|
||||
add_child(requester)
|
||||
requester.request_completed.connect(
|
||||
func(_result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
|
||||
if response_code >= 200 and response_code < 300:
|
||||
func(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
|
||||
if result == HTTPRequest.RESULT_SUCCESS and response_code >= 200 and response_code < 300:
|
||||
var texture := _texture_from_bytes(final_url, body)
|
||||
if texture != null:
|
||||
_write_cache_file(cache_path, body)
|
||||
@@ -486,7 +488,7 @@ func _set_remote_texture(target: TextureRect, url: String, fallback_url: String
|
||||
requester.queue_free()
|
||||
)
|
||||
|
||||
var err := requester.request(final_url)
|
||||
var err := requester.request(_fetch_url(final_url))
|
||||
if err != OK:
|
||||
requester.queue_free()
|
||||
if not fallback_url.is_empty() and final_url != fallback_url:
|
||||
@@ -522,10 +524,26 @@ func _cache_path_for_url(url: String) -> String:
|
||||
|
||||
func _texture_from_bytes(url: String, body: PackedByteArray) -> Texture2D:
|
||||
var image := Image.new()
|
||||
var err := _load_image_from_bytes(image, url, body)
|
||||
if err != OK:
|
||||
return null
|
||||
|
||||
return ImageTexture.create_from_image(image)
|
||||
|
||||
|
||||
func _fetch_url(url: String) -> String:
|
||||
if OS.has_feature("web") and url.begins_with(CDN_BASE_URL + "/"):
|
||||
return CDN_PROXY_BASE_URL + "/" + url.substr(CDN_BASE_URL.length() + 1)
|
||||
return url
|
||||
|
||||
|
||||
func _load_image_from_bytes(image: Image, url: String, body: PackedByteArray) -> Error:
|
||||
var ext := url.get_file().get_extension().to_lower()
|
||||
var err := ERR_FILE_UNRECOGNIZED
|
||||
var sniffed := _sniff_image_format(body)
|
||||
var format := sniffed if not sniffed.is_empty() else ext
|
||||
|
||||
match ext:
|
||||
match format:
|
||||
"png":
|
||||
err = image.load_png_from_buffer(body)
|
||||
"jpg", "jpeg":
|
||||
@@ -534,15 +552,25 @@ func _texture_from_bytes(url: String, body: PackedByteArray) -> Texture2D:
|
||||
err = image.load_webp_from_buffer(body)
|
||||
"svg":
|
||||
err = image.load_svg_from_buffer(body)
|
||||
_:
|
||||
err = image.load_png_from_buffer(body)
|
||||
if err != OK:
|
||||
err = image.load_jpg_from_buffer(body)
|
||||
|
||||
if err != OK:
|
||||
return null
|
||||
return err
|
||||
|
||||
return ImageTexture.create_from_image(image)
|
||||
|
||||
func _sniff_image_format(body: PackedByteArray) -> String:
|
||||
if body.size() >= 8 \
|
||||
and body[0] == 0x89 and body[1] == 0x50 and body[2] == 0x4E and body[3] == 0x47 \
|
||||
and body[4] == 0x0D and body[5] == 0x0A and body[6] == 0x1A and body[7] == 0x0A:
|
||||
return "png"
|
||||
if body.size() >= 3 and body[0] == 0xFF and body[1] == 0xD8 and body[2] == 0xFF:
|
||||
return "jpg"
|
||||
if body.size() >= 12 \
|
||||
and body[0] == 0x52 and body[1] == 0x49 and body[2] == 0x46 and body[3] == 0x46 \
|
||||
and body[8] == 0x57 and body[9] == 0x45 and body[10] == 0x42 and body[11] == 0x50:
|
||||
return "webp"
|
||||
var preview := body.slice(0, mini(body.size(), 128)).get_string_from_utf8().strip_edges().to_lower()
|
||||
if preview.begins_with("<svg") or preview.begins_with("<?xml"):
|
||||
return "svg"
|
||||
return ""
|
||||
|
||||
|
||||
func _to_int(value: Variant) -> int:
|
||||
|
||||
@@ -2,6 +2,8 @@ class_name PlayerDuelHud
|
||||
extends Control
|
||||
|
||||
const DEFAULT_CARD_ART_URL := "https://cdn.aeternalisgames.com/sor-assets/cardart/0_No-Card-Art.jpg"
|
||||
const CDN_BASE_URL := "https://cdn.aeternalisgames.com"
|
||||
const CDN_PROXY_BASE_URL := "https://hub.aeternalisgames.com/cdn"
|
||||
static var _texture_cache: Dictionary = {}
|
||||
|
||||
signal own_discard_requested
|
||||
@@ -123,25 +125,47 @@ func _set_remote_texture(target: TextureRect, url: String) -> void:
|
||||
requester.use_threads = true
|
||||
add_child(requester)
|
||||
requester.request_completed.connect(
|
||||
func(_result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
|
||||
if response_code >= 200 and response_code < 300:
|
||||
func(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
|
||||
if result == HTTPRequest.RESULT_SUCCESS and response_code >= 200 and response_code < 300:
|
||||
var texture := _texture_from_bytes(url, body)
|
||||
if texture != null:
|
||||
_texture_cache[url] = texture
|
||||
if is_instance_valid(target):
|
||||
target.texture = texture
|
||||
elif url != DEFAULT_CARD_ART_URL:
|
||||
_set_remote_texture(target, DEFAULT_CARD_ART_URL)
|
||||
elif url != DEFAULT_CARD_ART_URL:
|
||||
_set_remote_texture(target, DEFAULT_CARD_ART_URL)
|
||||
requester.queue_free()
|
||||
)
|
||||
var err := requester.request(url)
|
||||
var err := requester.request(_fetch_url(url))
|
||||
if err != OK:
|
||||
requester.queue_free()
|
||||
if url != DEFAULT_CARD_ART_URL:
|
||||
_set_remote_texture(target, DEFAULT_CARD_ART_URL)
|
||||
|
||||
|
||||
func _texture_from_bytes(url: String, body: PackedByteArray) -> Texture2D:
|
||||
var image := Image.new()
|
||||
var err := _load_image_from_bytes(image, url, body)
|
||||
if err != OK:
|
||||
return null
|
||||
return ImageTexture.create_from_image(image)
|
||||
|
||||
|
||||
func _fetch_url(url: String) -> String:
|
||||
if OS.has_feature("web") and url.begins_with(CDN_BASE_URL + "/"):
|
||||
return CDN_PROXY_BASE_URL + "/" + url.substr(CDN_BASE_URL.length() + 1)
|
||||
return url
|
||||
|
||||
|
||||
func _load_image_from_bytes(image: Image, url: String, body: PackedByteArray) -> Error:
|
||||
var ext := url.get_file().get_extension().to_lower()
|
||||
var err := ERR_FILE_UNRECOGNIZED
|
||||
match ext:
|
||||
var sniffed := _sniff_image_format(body)
|
||||
var format := sniffed if not sniffed.is_empty() else ext
|
||||
|
||||
match format:
|
||||
"png":
|
||||
err = image.load_png_from_buffer(body)
|
||||
"jpg", "jpeg":
|
||||
@@ -150,13 +174,24 @@ func _texture_from_bytes(url: String, body: PackedByteArray) -> Texture2D:
|
||||
err = image.load_webp_from_buffer(body)
|
||||
"svg":
|
||||
err = image.load_svg_from_buffer(body)
|
||||
_:
|
||||
err = image.load_png_from_buffer(body)
|
||||
if err != OK:
|
||||
err = image.load_jpg_from_buffer(body)
|
||||
if err != OK:
|
||||
return null
|
||||
return ImageTexture.create_from_image(image)
|
||||
return err
|
||||
|
||||
|
||||
func _sniff_image_format(body: PackedByteArray) -> String:
|
||||
if body.size() >= 8 \
|
||||
and body[0] == 0x89 and body[1] == 0x50 and body[2] == 0x4E and body[3] == 0x47 \
|
||||
and body[4] == 0x0D and body[5] == 0x0A and body[6] == 0x1A and body[7] == 0x0A:
|
||||
return "png"
|
||||
if body.size() >= 3 and body[0] == 0xFF and body[1] == 0xD8 and body[2] == 0xFF:
|
||||
return "jpg"
|
||||
if body.size() >= 12 \
|
||||
and body[0] == 0x52 and body[1] == 0x49 and body[2] == 0x46 and body[3] == 0x46 \
|
||||
and body[8] == 0x57 and body[9] == 0x45 and body[10] == 0x42 and body[11] == 0x50:
|
||||
return "webp"
|
||||
var preview := body.slice(0, mini(body.size(), 128)).get_string_from_utf8().strip_edges().to_lower()
|
||||
if preview.begins_with("<svg") or preview.begins_with("<?xml"):
|
||||
return "svg"
|
||||
return ""
|
||||
|
||||
|
||||
func _make_portrait_mask_material() -> ShaderMaterial:
|
||||
|
||||
Reference in New Issue
Block a user