587 lines
19 KiB
GDScript
587 lines
19 KiB
GDScript
extends PanelContainer
|
|
## Visual card widget.
|
|
##
|
|
## Call setup(dto, def) after instantiation, where:
|
|
## dto - PlayerCardDto dictionary from collection.get_cards
|
|
## def - CardDefinitionDto from ContentCache.get_definition() (pass {} if absent)
|
|
##
|
|
## Emits card_pressed(definition_id) on tap/click.
|
|
## Call set_selected(true/false) to highlight the card (used by the arch-spirit picker).
|
|
|
|
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
|
|
const ICON_SET_URL := "%s/Origin-Symbol.png" % GRAPHICS_BASE_URL
|
|
const ICON_MOVE_URL := "%s/movement.svg" % GRAPHICS_BASE_URL
|
|
const ICON_ABILITY_URL := "%s/ability.svg" % GRAPHICS_BASE_URL
|
|
const ICON_DEF_URL := "%s/Shield.png" % GRAPHICS_BASE_URL
|
|
const ICON_HP_URL := "%s/Heart.png" % GRAPHICS_BASE_URL
|
|
|
|
const AFFINITY_BORDER := {
|
|
"Void": Color(0.45, 0.18, 0.65),
|
|
"Aether": Color(0.35, 0.72, 1.00),
|
|
"Lake": Color(0.22, 0.52, 0.92),
|
|
"Hill": Color(0.70, 0.55, 0.30),
|
|
"Forest": Color(0.28, 0.68, 0.25),
|
|
"Field": Color(0.82, 0.72, 0.18),
|
|
"Ocean": Color(0.12, 0.38, 0.82),
|
|
"Island": Color(0.22, 0.65, 0.72),
|
|
"Marsh": Color(0.35, 0.60, 0.38),
|
|
"Swamp": Color(0.50, 0.58, 0.22),
|
|
"Mountain": Color(0.55, 0.55, 0.62),
|
|
"Steppe": Color(0.55, 0.58, 0.25),
|
|
"Fort": Color(0.78, 0.65, 0.35),
|
|
"Plains": Color(0.55, 0.78, 0.35),
|
|
"Grove": Color(0.18, 0.52, 0.20),
|
|
"Jungle": Color(0.32, 0.72, 0.18),
|
|
"PerfectAether": Color(0.70, 0.88, 1.00),
|
|
"PureVoid": Color(0.30, 0.08, 0.48),
|
|
}
|
|
|
|
const AFFINITY_BG := {
|
|
"Void": Color(0.15, 0.04, 0.22),
|
|
"Aether": Color(0.06, 0.18, 0.38),
|
|
"Lake": Color(0.05, 0.15, 0.40),
|
|
"Hill": Color(0.28, 0.20, 0.08),
|
|
"Forest": Color(0.08, 0.24, 0.08),
|
|
"Field": Color(0.25, 0.20, 0.03),
|
|
"Ocean": Color(0.03, 0.10, 0.32),
|
|
"Island": Color(0.05, 0.22, 0.28),
|
|
"Marsh": Color(0.08, 0.18, 0.10),
|
|
"Swamp": Color(0.14, 0.18, 0.05),
|
|
"Mountain": Color(0.15, 0.15, 0.18),
|
|
"Steppe": Color(0.18, 0.20, 0.06),
|
|
"Fort": Color(0.25, 0.20, 0.08),
|
|
"Plains": Color(0.15, 0.26, 0.08),
|
|
"Grove": Color(0.04, 0.16, 0.05),
|
|
"Jungle": Color(0.08, 0.24, 0.04),
|
|
"PerfectAether": Color(0.12, 0.22, 0.42),
|
|
"PureVoid": Color(0.08, 0.02, 0.14),
|
|
}
|
|
|
|
const RARITY_COLOR := {
|
|
"Common": Color(0.67, 0.67, 0.67),
|
|
"Uncommon": Color(0.27, 0.85, 0.27),
|
|
"Rare": Color(0.27, 0.53, 1.00),
|
|
"Epic": Color(0.67, 0.27, 1.00),
|
|
"Legendary": Color(1.00, 0.67, 0.13),
|
|
}
|
|
|
|
const AFFINITY_ICONS := {
|
|
"Void": preload("res://icons/affinity_void.svg"),
|
|
"Aether": preload("res://icons/affinity_aether.svg"),
|
|
"Lake": preload("res://icons/affinity_lake.svg"),
|
|
"Hill": preload("res://icons/affinity_hill.svg"),
|
|
"Forest": preload("res://icons/affinity_forest.svg"),
|
|
"Field": preload("res://icons/affinity_field.svg"),
|
|
}
|
|
|
|
static var _texture_cache: Dictionary = {}
|
|
|
|
signal card_pressed(definition_id: String)
|
|
|
|
@onready var affinity_icon_rect: TextureRect = %AffinityIcon
|
|
@onready var name_label: Label = %NameLabel
|
|
@onready var sp_row: HBoxContainer = %SpRow
|
|
@onready var sp_icon: TextureRect = %SpIcon
|
|
@onready var sp_label: Label = %SpLabel
|
|
@onready var st_row: HBoxContainer = %StRow
|
|
@onready var st_icon: TextureRect = %StIcon
|
|
@onready var st_label: Label = %StLabel
|
|
@onready var art_area: Panel = $Margin/VBox/ArtArea
|
|
@onready var art_texture: TextureRect = %ArtTexture
|
|
@onready var set_icon: TextureRect = %SetIcon
|
|
@onready var genus_label: Label = %GenusLabel
|
|
@onready var type_label: Label = %TypeLabel
|
|
@onready var attack_badge: HBoxContainer = %AttackBadge
|
|
@onready var attack_value: Label = %AttackValue
|
|
@onready var move_badge: HBoxContainer = %MoveBadge
|
|
@onready var move_icon: TextureRect = %MoveIcon
|
|
@onready var move_value: Label = %MoveValue
|
|
@onready var range_badge: HBoxContainer = %RangeBadge
|
|
@onready var range_value: Label = %RangeValue
|
|
@onready var hp_badge: HBoxContainer = %HpBadge
|
|
@onready var hp_icon: TextureRect = %HpIcon
|
|
@onready var hp_value: Label = %HpValue
|
|
@onready var def_badge: HBoxContainer = %DefBadge
|
|
@onready var def_icon: TextureRect = %DefIcon
|
|
@onready var def_value: Label = %DefValue
|
|
@onready var ability_badge: HBoxContainer = %AbilityBadge
|
|
@onready var ability_icon: TextureRect = %AbilityIcon
|
|
@onready var ability_value: Label = %AbilityValue
|
|
|
|
var _affinity: String = ""
|
|
var _definition_id: String = ""
|
|
var _is_selected: bool = false
|
|
var _is_ready: bool = false
|
|
var _pending_dto: Dictionary = {}
|
|
var _pending_def: Dictionary = {}
|
|
|
|
|
|
func _ready() -> void:
|
|
_is_ready = true
|
|
_load_static_icons()
|
|
if not _pending_dto.is_empty():
|
|
_apply_setup(_pending_dto, _pending_def)
|
|
_pending_dto.clear()
|
|
_pending_def.clear()
|
|
|
|
|
|
static func affinity_icon(affinity: String) -> Texture2D:
|
|
var cached : Variant = _texture_cache.get(_affinity_icon_url(affinity), null)
|
|
if cached != null:
|
|
return cached as Texture2D
|
|
|
|
match affinity:
|
|
"Lake", "Ocean", "Island", "Marsh", "Swamp":
|
|
return AFFINITY_ICONS["Lake"]
|
|
"Hill", "Mountain", "Steppe", "Fort":
|
|
return AFFINITY_ICONS["Hill"]
|
|
"Forest", "Plains", "Grove":
|
|
return AFFINITY_ICONS["Forest"]
|
|
"Field", "Jungle":
|
|
return AFFINITY_ICONS["Field"]
|
|
"Aether", "PerfectAether":
|
|
return AFFINITY_ICONS["Aether"]
|
|
"Void", "PureVoid":
|
|
return AFFINITY_ICONS["Void"]
|
|
_:
|
|
return null
|
|
|
|
|
|
func setup(dto: Dictionary, def: Dictionary = {}) -> void:
|
|
if not _is_ready:
|
|
_pending_dto = dto.duplicate(true)
|
|
_pending_def = def.duplicate(true)
|
|
return
|
|
|
|
_apply_setup(dto, def)
|
|
|
|
|
|
func _apply_setup(dto: Dictionary, def: Dictionary = {}) -> void:
|
|
_definition_id = str(dto.get("cardDefinitionId", def.get("id", "")))
|
|
_affinity = str(def.get("affinity", dto.get("affinity", "")))
|
|
var rarity := str(def.get("rarity", dto.get("rarity", "")))
|
|
var name := str(def.get("name", dto.get("name", "")))
|
|
var card_type := str(def.get("cardType", dto.get("cardType", "")))
|
|
|
|
_apply_style()
|
|
|
|
name_label.text = name if not name.is_empty() else str(dto.get("name", "-"))
|
|
name_label.modulate = RARITY_COLOR.get(rarity, Color.WHITE)
|
|
type_label.text = card_type
|
|
type_label.tooltip_text = card_type
|
|
|
|
_apply_art(def)
|
|
_apply_meta(def, dto)
|
|
_load_stats(def)
|
|
_load_abilities(def)
|
|
|
|
|
|
func set_selected(selected: bool) -> void:
|
|
_is_selected = selected
|
|
_apply_style()
|
|
|
|
|
|
func get_definition_id() -> String:
|
|
return _definition_id
|
|
|
|
|
|
func _gui_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
|
|
var mb := event as InputEventMouseButton
|
|
# Consume both press and release so neither leaks to the 3D board's
|
|
# _unhandled_input. Only emit card_pressed on a plain press — not on
|
|
# double-click (which Godot fires as a *second* pressed=true event in
|
|
# the same frame, causing an immediate select → deselect cycle).
|
|
if mb.pressed and not mb.double_click:
|
|
card_pressed.emit(_definition_id)
|
|
accept_event()
|
|
elif event is InputEventScreenTouch and event.pressed:
|
|
card_pressed.emit(_definition_id)
|
|
accept_event()
|
|
|
|
|
|
func _apply_style() -> void:
|
|
var border: Color
|
|
var width: int
|
|
if _is_selected:
|
|
border = Color(1.0, 1.0, 1.0)
|
|
width = 3
|
|
else:
|
|
border = AFFINITY_BORDER.get(_affinity, Color(0.31, 0.38, 0.63))
|
|
width = 2
|
|
|
|
var s := StyleBoxFlat.new()
|
|
s.bg_color = AFFINITY_BG.get(_affinity, Color(0.09, 0.09, 0.14))
|
|
s.border_color = border
|
|
s.set_border_width_all(width)
|
|
s.set_corner_radius_all(8)
|
|
add_theme_stylebox_override("panel", s)
|
|
|
|
var art_style := StyleBoxFlat.new()
|
|
art_style.bg_color = Color(0.05, 0.05, 0.08, 0.92)
|
|
art_style.border_color = border
|
|
art_style.set_border_width_all(1)
|
|
art_style.set_corner_radius_all(6)
|
|
art_area.add_theme_stylebox_override("panel", art_style)
|
|
|
|
|
|
func _apply_art(def: Dictionary) -> void:
|
|
var art_url := _find_card_art_url(def)
|
|
_set_remote_texture(art_texture, art_url, DEFAULT_CARD_ART_URL)
|
|
affinity_icon_rect.texture = affinity_icon(_affinity)
|
|
_set_remote_texture(affinity_icon_rect, _affinity_icon_url(_affinity))
|
|
affinity_icon_rect.tooltip_text = _affinity if not _affinity.is_empty() else "Affinity"
|
|
|
|
|
|
func _apply_meta(def: Dictionary, dto: Dictionary) -> void:
|
|
_set_remote_texture(set_icon, ICON_SET_URL)
|
|
set_icon.tooltip_text = "Card Set"
|
|
|
|
var genus_text := _format_genus(def)
|
|
genus_label.text = genus_text
|
|
genus_label.tooltip_text = genus_text
|
|
|
|
sp_label.text = _display_int_or_unknown(def.get("spCost", dto.get("spCost", null)))
|
|
sp_row.tooltip_text = "Spirit Point Cost"
|
|
sp_label.tooltip_text = "Spirit Point Cost"
|
|
sp_icon.tooltip_text = "Spirit Point Cost"
|
|
|
|
var st_value: Variant = def.get("stCost", dto.get("stCost", null))
|
|
var has_st := st_value != null and _to_int(st_value) > 0
|
|
st_row.visible = has_st
|
|
if has_st:
|
|
st_label.text = _display_int(st_value)
|
|
st_row.tooltip_text = "Sacrifice Token Cost"
|
|
st_label.tooltip_text = "Sacrifice Token Cost"
|
|
st_icon.tooltip_text = "Sacrifice Token Cost"
|
|
|
|
|
|
func _load_stats(def: Dictionary) -> void:
|
|
var dmg: Variant = def.get("damage", null)
|
|
var hp: Variant = def.get("maxHealth", null)
|
|
var dfs: Variant = def.get("maxDefense", null)
|
|
var mov: Variant = def.get("movementPoints", null)
|
|
|
|
attack_value.text = _display_int(dmg)
|
|
move_value.text = _display_int(mov)
|
|
range_value.text = _format_range(def)
|
|
hp_value.text = _display_int(hp)
|
|
def_value.text = _display_int(dfs)
|
|
|
|
attack_badge.tooltip_text = _build_attack_tooltip(def)
|
|
range_badge.tooltip_text = _build_range_tooltip(def)
|
|
move_badge.tooltip_text = _build_move_tooltip(def)
|
|
hp_badge.tooltip_text = "Max Health"
|
|
def_badge.tooltip_text = "Max Defense"
|
|
attack_value.tooltip_text = attack_badge.tooltip_text
|
|
range_value.tooltip_text = range_badge.tooltip_text
|
|
move_value.tooltip_text = move_badge.tooltip_text
|
|
hp_value.tooltip_text = hp_badge.tooltip_text
|
|
def_value.tooltip_text = def_badge.tooltip_text
|
|
|
|
|
|
func _load_abilities(def: Dictionary) -> void:
|
|
var ability_lines := _parse_ability_lines(def)
|
|
var count := ability_lines.size()
|
|
ability_value.text = str(count)
|
|
var tooltip := "Abilities"
|
|
if count > 0:
|
|
tooltip += "\n%s" % "\n".join(ability_lines)
|
|
ability_badge.tooltip_text = tooltip
|
|
ability_icon.tooltip_text = tooltip
|
|
ability_value.tooltip_text = tooltip
|
|
art_texture.tooltip_text = tooltip if count > 0 else ""
|
|
|
|
|
|
func _display_int(value: Variant) -> String:
|
|
return str(_to_int(value)) if value != null else "-"
|
|
|
|
|
|
func _display_int_or_unknown(value: Variant) -> String:
|
|
return str(_to_int(value)) if value != null else "?"
|
|
|
|
|
|
func _format_genus(def: Dictionary) -> String:
|
|
var genus: Variant = def.get("genus", [])
|
|
if typeof(genus) == TYPE_ARRAY and not (genus as Array).is_empty():
|
|
var labels: PackedStringArray = []
|
|
for tag in genus as Array:
|
|
labels.append(str(tag))
|
|
return ", ".join(labels)
|
|
return ""
|
|
|
|
|
|
func _format_range(def: Dictionary) -> String:
|
|
var min_attack: Variant = def.get("minAttackRange", null)
|
|
var max_attack: Variant = def.get("maxAttackRange", null)
|
|
if min_attack != null or max_attack != null:
|
|
return _format_range_values(min_attack, max_attack)
|
|
|
|
var min_channel: Variant = def.get("minChannelRange", null)
|
|
var max_channel: Variant = def.get("maxChannelRange", null)
|
|
if min_channel != null or max_channel != null:
|
|
return _format_range_values(min_channel, max_channel)
|
|
|
|
return "-"
|
|
|
|
|
|
func _format_range_values(min_value: Variant, max_value: Variant) -> String:
|
|
if min_value == null and max_value == null:
|
|
return "-"
|
|
if min_value == null:
|
|
return str(_to_int(max_value))
|
|
if max_value == null:
|
|
return str(_to_int(min_value))
|
|
if _to_int(min_value) == _to_int(max_value):
|
|
return str(_to_int(min_value))
|
|
return "%s-%s" % [str(_to_int(min_value)), str(_to_int(max_value))]
|
|
|
|
|
|
func _build_attack_tooltip(def: Dictionary) -> String:
|
|
var parts: PackedStringArray = []
|
|
parts.append("Attack Damage")
|
|
var attack_type := str(def.get("attackType", ""))
|
|
if not attack_type.is_empty():
|
|
parts.append("Type: %s" % attack_type)
|
|
var can_attack: Variant = def.get("canAttack", null)
|
|
if can_attack != null:
|
|
parts.append("Can Attack: %s" % ("Yes" if bool(can_attack) else "No"))
|
|
return "\n".join(parts)
|
|
|
|
|
|
func _build_move_tooltip(def: Dictionary) -> String:
|
|
var parts: PackedStringArray = []
|
|
parts.append("Movement")
|
|
var movement_type := str(def.get("movementType", ""))
|
|
if not movement_type.is_empty():
|
|
parts.append("Type: %s" % movement_type)
|
|
var can_move: Variant = def.get("canMove", null)
|
|
if can_move != null:
|
|
parts.append("Can Move: %s" % ("Yes" if bool(can_move) else "No"))
|
|
return "\n".join(parts)
|
|
|
|
|
|
func _build_range_tooltip(def: Dictionary) -> String:
|
|
var min_attack: Variant = def.get("minAttackRange", null)
|
|
var max_attack: Variant = def.get("maxAttackRange", null)
|
|
if min_attack != null or max_attack != null:
|
|
return "Attack Range\n%s" % _format_range_values(min_attack, max_attack)
|
|
|
|
var min_channel: Variant = def.get("minChannelRange", null)
|
|
var max_channel: Variant = def.get("maxChannelRange", null)
|
|
if min_channel != null or max_channel != null:
|
|
return "Channel Range\n%s" % _format_range_values(min_channel, max_channel)
|
|
|
|
return "Range"
|
|
|
|
|
|
func _parse_ability_lines(def: Dictionary) -> PackedStringArray:
|
|
var lines: PackedStringArray = []
|
|
var raw_value: Variant = def.get("abilitiesJson", def.get("abilities", ""))
|
|
if raw_value == null:
|
|
return lines
|
|
var json_str := str(raw_value).strip_edges()
|
|
if json_str.is_empty() or json_str in ["[]", "null", "NULL", "<null>"]:
|
|
return lines
|
|
if not json_str.begins_with("["):
|
|
return lines
|
|
|
|
var json := JSON.new()
|
|
var err := json.parse(json_str)
|
|
if err != OK:
|
|
return lines
|
|
|
|
var parsed: Variant = json.data
|
|
if typeof(parsed) != TYPE_ARRAY:
|
|
return lines
|
|
|
|
for ab in parsed as Array:
|
|
if typeof(ab) != TYPE_DICTIONARY:
|
|
continue
|
|
var d := ab as Dictionary
|
|
var name_ := str(d.get("name", d.get("Name", "")))
|
|
var desc := str(d.get("description", d.get("Description", "")))
|
|
if desc.is_empty():
|
|
lines.append("[%s]" % name_)
|
|
else:
|
|
lines.append("[%s] %s" % [name_, desc])
|
|
|
|
return lines
|
|
|
|
|
|
func _find_card_art_url(def: Dictionary) -> String:
|
|
var assets: Variant = def.get("assets", [])
|
|
if typeof(assets) != TYPE_ARRAY:
|
|
return DEFAULT_CARD_ART_URL
|
|
|
|
for asset in assets as Array:
|
|
if typeof(asset) != TYPE_DICTIONARY:
|
|
continue
|
|
var d := asset as Dictionary
|
|
if str(d.get("assetType", "")) == "Art2D":
|
|
return str(d.get("cdnUrl", DEFAULT_CARD_ART_URL))
|
|
|
|
return DEFAULT_CARD_ART_URL
|
|
|
|
|
|
func _load_static_icons() -> void:
|
|
DirAccess.make_dir_recursive_absolute(REMOTE_CACHE_DIR)
|
|
_set_remote_texture(sp_icon, ICON_SP_URL)
|
|
_set_remote_texture(st_icon, ICON_ST_URL)
|
|
_set_remote_texture(set_icon, ICON_SET_URL)
|
|
_set_remote_texture(move_icon, ICON_MOVE_URL)
|
|
_set_remote_texture(hp_icon, ICON_HP_URL)
|
|
_set_remote_texture(def_icon, ICON_DEF_URL)
|
|
_set_remote_texture(ability_icon, ICON_ABILITY_URL)
|
|
|
|
|
|
static func _affinity_icon_url(affinity: String) -> String:
|
|
if affinity.is_empty():
|
|
return ""
|
|
return "%s/%s.png" % [GRAPHICS_BASE_URL, affinity.uri_encode()]
|
|
|
|
|
|
func _set_remote_texture(target: TextureRect, url: String, fallback_url: String = "") -> void:
|
|
var final_url := url
|
|
if final_url.is_empty():
|
|
final_url = fallback_url
|
|
if final_url.is_empty() or not is_instance_valid(target):
|
|
return
|
|
|
|
var cached: Variant = _texture_cache.get(final_url, null)
|
|
if cached != null:
|
|
target.texture = cached as Texture2D
|
|
return
|
|
|
|
var cache_path := _cache_path_for_url(final_url)
|
|
var disk_texture := _load_texture_from_disk(final_url, cache_path)
|
|
if disk_texture != null:
|
|
_texture_cache[final_url] = disk_texture
|
|
target.texture = disk_texture
|
|
return
|
|
|
|
if not final_url.begins_with("http"):
|
|
return
|
|
|
|
var requester := HTTPRequest.new()
|
|
requester.use_threads = true
|
|
add_child(requester)
|
|
requester.request_completed.connect(
|
|
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)
|
|
_texture_cache[final_url] = texture
|
|
if is_instance_valid(target):
|
|
target.texture = texture
|
|
elif not fallback_url.is_empty() and final_url != fallback_url:
|
|
_set_remote_texture(target, fallback_url)
|
|
elif not fallback_url.is_empty() and final_url != fallback_url:
|
|
_set_remote_texture(target, fallback_url)
|
|
requester.queue_free()
|
|
)
|
|
|
|
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:
|
|
_set_remote_texture(target, fallback_url)
|
|
|
|
|
|
func _load_texture_from_disk(url: String, cache_path: String) -> Texture2D:
|
|
if not FileAccess.file_exists(cache_path):
|
|
return null
|
|
var file := FileAccess.open(cache_path, FileAccess.READ)
|
|
if file == null:
|
|
return null
|
|
var bytes := file.get_buffer(file.get_length())
|
|
file.close()
|
|
return _texture_from_bytes(url, bytes)
|
|
|
|
|
|
func _write_cache_file(cache_path: String, bytes: PackedByteArray) -> void:
|
|
DirAccess.make_dir_recursive_absolute(REMOTE_CACHE_DIR)
|
|
var file := FileAccess.open(cache_path, FileAccess.WRITE)
|
|
if file == null:
|
|
return
|
|
file.store_buffer(bytes)
|
|
file.close()
|
|
|
|
|
|
func _cache_path_for_url(url: String) -> String:
|
|
var ext := url.get_file().get_extension().to_lower()
|
|
if ext.is_empty():
|
|
ext = "bin"
|
|
return "%s/%s.%s" % [REMOTE_CACHE_DIR, url.sha256_text(), ext]
|
|
|
|
|
|
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 format:
|
|
"png":
|
|
err = image.load_png_from_buffer(body)
|
|
"jpg", "jpeg":
|
|
err = image.load_jpg_from_buffer(body)
|
|
"webp":
|
|
err = image.load_webp_from_buffer(body)
|
|
"svg":
|
|
err = image.load_svg_from_buffer(body)
|
|
|
|
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 _to_int(value: Variant) -> int:
|
|
match typeof(value):
|
|
TYPE_INT:
|
|
return int(value)
|
|
TYPE_FLOAT:
|
|
return int(round(float(value)))
|
|
TYPE_STRING:
|
|
var text := str(value)
|
|
return int(round(float(text))) if text.contains(".") else int(text)
|
|
_:
|
|
return int(value)
|