improved image cache, new example
This commit is contained in:
parent
cbec745704
commit
a3740fab4d
40
ChatContainer.gd
Normal file
40
ChatContainer.gd
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
extends VBoxContainer
|
||||||
|
|
||||||
|
func put_chat(senderdata : SenderData, msg : String):
|
||||||
|
var msgnode : Control = preload("res://ChatMessage.tscn").instance()
|
||||||
|
var time = OS.get_time()
|
||||||
|
var badges : String = ""
|
||||||
|
if ($"../Gift".image_cache):
|
||||||
|
for badge in senderdata.tags["badges"].split(",", false):
|
||||||
|
badges += "[img=center]" + $"../Gift".image_cache.get_badge(badge, senderdata.tags["room-id"]).resource_path + "[/img] "
|
||||||
|
var locations : Array = []
|
||||||
|
for emote in senderdata.tags["emotes"].split("/", false):
|
||||||
|
var data : Array = emote.split(":")
|
||||||
|
for d in data[1].split(","):
|
||||||
|
var start_end = d.split("-")
|
||||||
|
locations.append(EmoteLocation.new(data[0], int(start_end[0]), int(start_end[1])))
|
||||||
|
locations.sort_custom(self, "greater")
|
||||||
|
var offset = 0
|
||||||
|
for loc in locations:
|
||||||
|
var emote_string = "[img=center]" + $"../Gift".image_cache.get_emote(loc.id).resource_path +"[/img]"
|
||||||
|
msg = msg.substr(0, loc.start + offset) + emote_string + msg.substr(loc.end + offset + 1)
|
||||||
|
offset += emote_string.length() - loc.end - loc.start - 1
|
||||||
|
var bottom : bool = $Chat/ScrollContainer.scroll_vertical == $Chat/ScrollContainer.get_v_scrollbar().max_value - $Chat/ScrollContainer.get_v_scrollbar().rect_size.y
|
||||||
|
msgnode.set_msg(str(time["hour"]) + ":" + ("0" + str(time["minute"]) if time["minute"] < 10 else str(time["minute"])), senderdata, msg, badges)
|
||||||
|
$Chat/ScrollContainer/ChatMessagesContainer.add_child(msgnode)
|
||||||
|
yield(get_tree(), "idle_frame")
|
||||||
|
if (bottom):
|
||||||
|
$Chat/ScrollContainer.scroll_vertical = $Chat/ScrollContainer.get_v_scrollbar().max_value
|
||||||
|
|
||||||
|
func smaller(a : EmoteLocation, b : EmoteLocation):
|
||||||
|
return a.start < b.start
|
||||||
|
|
||||||
|
class EmoteLocation extends Reference:
|
||||||
|
var id : String
|
||||||
|
var start : int
|
||||||
|
var end : int
|
||||||
|
|
||||||
|
func _init(emote_id, start_idx, end_idx):
|
||||||
|
self.id = emote_id
|
||||||
|
self.start = start_idx
|
||||||
|
self.end = end_idx
|
4
ChatMessage.gd
Normal file
4
ChatMessage.gd
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
extends HBoxContainer
|
||||||
|
|
||||||
|
func set_msg(stamp : String, data : SenderData, msg : String, badges : String) -> void:
|
||||||
|
$RichTextLabel.bbcode_text = stamp + " " + badges + "[b][color="+ data.tags["color"] + "]" + data.tags["display-name"] +"[/color][/b]: " + msg
|
23
ChatMessage.tscn
Normal file
23
ChatMessage.tscn
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
[gd_scene load_steps=2 format=2]
|
||||||
|
|
||||||
|
[ext_resource path="res://ChatMessage.gd" type="Script" id=1]
|
||||||
|
|
||||||
|
[node name="ChatMessage" type="HBoxContainer"]
|
||||||
|
margin_right = 400.0
|
||||||
|
margin_bottom = 24.0
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
script = ExtResource( 1 )
|
||||||
|
__meta__ = {
|
||||||
|
"_edit_use_anchors_": false
|
||||||
|
}
|
||||||
|
|
||||||
|
[node name="RichTextLabel" type="RichTextLabel" parent="."]
|
||||||
|
margin_right = 400.0
|
||||||
|
margin_bottom = 24.0
|
||||||
|
focus_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
size_flags_vertical = 3
|
||||||
|
bbcode_enabled = true
|
||||||
|
fit_content_height = true
|
||||||
|
scroll_active = false
|
||||||
|
selection_enabled = true
|
26
Node.gd
Normal file
26
Node.gd
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
extends Control
|
||||||
|
|
||||||
|
func chat_message(data : SenderData, msg : String) -> void:
|
||||||
|
$ChatContainer.put_chat(data, msg)
|
||||||
|
|
||||||
|
# Check the CommandInfo class for the available info of the cmd_info.
|
||||||
|
func command_test(cmd_info : CommandInfo) -> void:
|
||||||
|
print("A")
|
||||||
|
|
||||||
|
func hello_world(cmd_info : CommandInfo) -> void:
|
||||||
|
$Gift.chat("HELLO WORLD!")
|
||||||
|
|
||||||
|
func streamer_only(cmd_info : CommandInfo) -> void:
|
||||||
|
$Gift.chat("Streamer command executed")
|
||||||
|
|
||||||
|
func no_permission(cmd_info : CommandInfo) -> void:
|
||||||
|
$Gift.chat("NO PERMISSION!")
|
||||||
|
|
||||||
|
func greet(cmd_info : CommandInfo, arg_ary : PoolStringArray) -> void:
|
||||||
|
$Gift.chat("Greetings, " + arg_ary[0])
|
||||||
|
|
||||||
|
func greet_me(cmd_info : CommandInfo) -> void:
|
||||||
|
$Gift.chat("Greetings, " + cmd_info.sender_data.tags["display-name"] + "!")
|
||||||
|
|
||||||
|
func list(cmd_info : CommandInfo, arg_ary : PoolStringArray) -> void:
|
||||||
|
$Gift.chat(arg_ary.join(", "))
|
0
addons/gift/gift.gd
Normal file → Executable file
0
addons/gift/gift.gd
Normal file → Executable file
@ -28,10 +28,10 @@ signal emote_downloaded(emote_id)
|
|||||||
# Badge has been downloaded
|
# Badge has been downloaded
|
||||||
signal badge_downloaded(badge_name)
|
signal badge_downloaded(badge_name)
|
||||||
|
|
||||||
# Messages starting with one of these symbols are handled. '/' will be ignored, reserved by Twitch.
|
# Messages starting with one of these symbols are handled as commands. '/' will be ignored, reserved by Twitch.
|
||||||
export(Array, String) var command_prefixes : Array = ["!"]
|
export(Array, String) var command_prefixes : Array = ["!"]
|
||||||
# Time to wait after each sent chat message. Values below ~0.31 might lead to a disconnect after 100 messages.
|
# Time to wait in msec after each sent chat message. Values below ~310 might lead to a disconnect after 100 messages.
|
||||||
export(float) var chat_timeout = 0.32
|
export(int) var chat_timeout_ms = 320
|
||||||
export(bool) var get_images : bool = false
|
export(bool) var get_images : bool = false
|
||||||
# If true, caches emotes/badges to disk, so that they don't have to be redownloaded on every restart.
|
# If true, caches emotes/badges to disk, so that they don't have to be redownloaded on every restart.
|
||||||
# This however means that they might not be updated if they change until you clear the cache.
|
# This however means that they might not be updated if they change until you clear the cache.
|
||||||
@ -39,12 +39,12 @@ export(bool) var disk_cache : bool = false
|
|||||||
# Disk Cache has to be enbaled for this to work
|
# Disk Cache has to be enbaled for this to work
|
||||||
export(String, FILE) var disk_cache_path = "user://gift/cache"
|
export(String, FILE) var disk_cache_path = "user://gift/cache"
|
||||||
|
|
||||||
var websocket : WebSocketClient = WebSocketClient.new()
|
var websocket := WebSocketClient.new()
|
||||||
var user_regex = RegEx.new()
|
var user_regex := RegEx.new()
|
||||||
var twitch_restarting
|
var twitch_restarting
|
||||||
# Twitch disconnects connected clients if too many chat messages are being sent. (At about 100 messages/30s)
|
# Twitch disconnects connected clients if too many chat messages are being sent. (At about 100 messages/30s)
|
||||||
var chat_queue = []
|
var chat_queue = []
|
||||||
onready var chat_accu = chat_timeout
|
var last_msg = OS.get_ticks_msec()
|
||||||
# Mapping of channels to their channel info, like available badges.
|
# Mapping of channels to their channel info, like available badges.
|
||||||
var channels : Dictionary = {}
|
var channels : Dictionary = {}
|
||||||
var commands : Dictionary = {}
|
var commands : Dictionary = {}
|
||||||
@ -81,7 +81,7 @@ func _ready() -> void:
|
|||||||
websocket.connect("connection_error", self, "connection_error")
|
websocket.connect("connection_error", self, "connection_error")
|
||||||
if(get_images):
|
if(get_images):
|
||||||
image_cache = ImageCache.new(disk_cache, disk_cache_path)
|
image_cache = ImageCache.new(disk_cache, disk_cache_path)
|
||||||
add_child(image_cache)
|
# add_child(image_cache)
|
||||||
|
|
||||||
func connect_to_twitch() -> void:
|
func connect_to_twitch() -> void:
|
||||||
if(websocket.connect_to_url("wss://irc-ws.chat.twitch.tv:443") != OK):
|
if(websocket.connect_to_url("wss://irc-ws.chat.twitch.tv:443") != OK):
|
||||||
@ -91,11 +91,9 @@ func connect_to_twitch() -> void:
|
|||||||
func _process(delta : float) -> void:
|
func _process(delta : float) -> void:
|
||||||
if(websocket.get_connection_status() != NetworkedMultiplayerPeer.CONNECTION_DISCONNECTED):
|
if(websocket.get_connection_status() != NetworkedMultiplayerPeer.CONNECTION_DISCONNECTED):
|
||||||
websocket.poll()
|
websocket.poll()
|
||||||
if(!chat_queue.empty() && chat_accu >= chat_timeout):
|
if (!chat_queue.empty() && (last_msg + chat_timeout_ms) <= OS.get_ticks_msec()):
|
||||||
send(chat_queue.pop_front())
|
send(chat_queue.pop_front())
|
||||||
chat_accu = 0
|
last_msg = OS.get_ticks_msec()
|
||||||
else:
|
|
||||||
chat_accu += delta
|
|
||||||
|
|
||||||
# Login using a oauth token.
|
# Login using a oauth token.
|
||||||
# You will have to either get a oauth token yourself or use
|
# You will have to either get a oauth token yourself or use
|
||||||
@ -129,7 +127,7 @@ func chat(message : String, channel : String = ""):
|
|||||||
else:
|
else:
|
||||||
print_debug("No channel specified.")
|
print_debug("No channel specified.")
|
||||||
|
|
||||||
func whisper(message : String, target : String):
|
func whisper(message : String, target : String) -> void:
|
||||||
chat("/w " + target + " " + message)
|
chat("/w " + target + " " + message)
|
||||||
|
|
||||||
func data_received() -> void:
|
func data_received() -> void:
|
||||||
@ -194,13 +192,6 @@ func handle_message(message : String, tags : Dictionary) -> void:
|
|||||||
var sender_data : SenderData = SenderData.new(user_regex.search(msg[0]).get_string(), msg[2], tags)
|
var sender_data : SenderData = SenderData.new(user_regex.search(msg[0]).get_string(), msg[2], tags)
|
||||||
handle_command(sender_data, msg)
|
handle_command(sender_data, msg)
|
||||||
emit_signal("chat_message", sender_data, msg[3].right(1))
|
emit_signal("chat_message", sender_data, msg[3].right(1))
|
||||||
if(get_images):
|
|
||||||
if(!image_cache.badge_map.has(tags["room-id"])):
|
|
||||||
image_cache.get_badge_mappings(tags["room-id"])
|
|
||||||
for emote in tags["emotes"].split("/", false):
|
|
||||||
image_cache.get_emote(emote.split(":")[0])
|
|
||||||
for badge in tags["badges"].split(",", false):
|
|
||||||
image_cache.get_badge(badge, tags["room-id"])
|
|
||||||
"WHISPER":
|
"WHISPER":
|
||||||
var sender_data : SenderData = SenderData.new(user_regex.search(msg[0]).get_string(), msg[2], tags)
|
var sender_data : SenderData = SenderData.new(user_regex.search(msg[0]).get_string(), msg[2], tags)
|
||||||
handle_command(sender_data, msg, true)
|
handle_command(sender_data, msg, true)
|
||||||
|
0
addons/gift/icon.png
Normal file → Executable file
0
addons/gift/icon.png
Normal file → Executable file
Before Width: | Height: | Size: 363 B After Width: | Height: | Size: 363 B |
0
addons/gift/icon.png.import
Normal file → Executable file
0
addons/gift/icon.png.import
Normal file → Executable file
BIN
addons/gift/placeholder.png
Normal file
BIN
addons/gift/placeholder.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 120 B |
13
addons/gift/placeholder.png.import
Normal file
13
addons/gift/placeholder.png.import
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[remap]
|
||||||
|
|
||||||
|
importer="image"
|
||||||
|
type="Image"
|
||||||
|
path="res://.import/placeholder.png-7d8d01cafa2d5188ea51e03e3fda7124.image"
|
||||||
|
|
||||||
|
[deps]
|
||||||
|
|
||||||
|
source_file="res://addons/gift/placeholder.png"
|
||||||
|
dest_files=[ "res://.import/placeholder.png-7d8d01cafa2d5188ea51e03e3fda7124.image" ]
|
||||||
|
|
||||||
|
[params]
|
||||||
|
|
0
addons/gift/plugin.cfg
Normal file → Executable file
0
addons/gift/plugin.cfg
Normal file → Executable file
@ -13,4 +13,4 @@ func _init(f_ref : FuncRef, perm_lvl : int, mx_args : int, mn_args : int, whr :
|
|||||||
max_args = mx_args
|
max_args = mx_args
|
||||||
min_args = mn_args
|
min_args = mn_args
|
||||||
where = whr
|
where = whr
|
||||||
|
|
||||||
|
@ -9,4 +9,4 @@ func _init(sndr_dt, cmd, whspr):
|
|||||||
sender_data = sndr_dt
|
sender_data = sndr_dt
|
||||||
command = cmd
|
command = cmd
|
||||||
whisper = whspr
|
whisper = whspr
|
||||||
|
|
||||||
|
@ -1,111 +1,197 @@
|
|||||||
extends Node
|
extends Resource
|
||||||
class_name ImageCache
|
class_name ImageCache
|
||||||
|
|
||||||
signal badge_mapping_available
|
enum RequestType {
|
||||||
|
EMOTE,
|
||||||
|
BADGE,
|
||||||
|
BADGE_MAPPING
|
||||||
|
}
|
||||||
|
|
||||||
var cached_images : Dictionary = {"emotes": {}, "badges": {}}
|
var caches := {
|
||||||
var cache_mutex = Mutex.new()
|
RequestType.EMOTE: {},
|
||||||
var badge_map : Dictionary = {}
|
RequestType.BADGE: {},
|
||||||
var badge_mutex = Mutex.new()
|
RequestType.BADGE_MAPPING: {}
|
||||||
var dl_queue : PoolStringArray = []
|
}
|
||||||
var disk_cache : bool
|
|
||||||
var disk_cache_path : String
|
var queue := []
|
||||||
|
var thread := Thread.new()
|
||||||
|
var mutex := Mutex.new()
|
||||||
|
var active = true
|
||||||
|
var http_client := HTTPClient.new()
|
||||||
|
var host : String
|
||||||
|
|
||||||
var file : File = File.new()
|
var file : File = File.new()
|
||||||
var dir : Directory = Directory.new()
|
var dir : Directory = Directory.new()
|
||||||
|
var cache_path : String
|
||||||
|
var disk_cache : bool
|
||||||
|
|
||||||
|
const HEADERS : PoolStringArray = PoolStringArray([
|
||||||
|
"User-Agent: GIFT/1.0 (Godot Engine)",
|
||||||
|
"Accept: */*"
|
||||||
|
])
|
||||||
|
|
||||||
func _init(do_disk_cache : bool, cache_path : String) -> void:
|
func _init(do_disk_cache : bool, cache_path : String) -> void:
|
||||||
disk_cache = do_disk_cache
|
self.disk_cache = do_disk_cache
|
||||||
disk_cache_path = cache_path
|
self.cache_path = cache_path
|
||||||
|
thread.start(self, "start")
|
||||||
|
|
||||||
func _ready() -> void:
|
func start(params) -> void:
|
||||||
if(disk_cache):
|
var f : File = File.new()
|
||||||
for cache_dir in cached_images.keys():
|
var d : Directory = Directory.new()
|
||||||
cached_images[cache_dir] = {}
|
if (disk_cache):
|
||||||
dir.make_dir_recursive(disk_cache_path + "/" + cache_dir)
|
for type in caches.keys():
|
||||||
dir.open(disk_cache_path + "/" + cache_dir)
|
var cache_dir = RequestType.keys()[type]
|
||||||
dir.list_dir_begin(true)
|
caches[cache_dir] = {}
|
||||||
var current = dir.get_next()
|
var error := d.make_dir_recursive(cache_path + "/" + cache_dir)
|
||||||
while current != "":
|
while active:
|
||||||
if(!dir.current_is_dir()):
|
if (!queue.empty()):
|
||||||
file.open(dir.get_current_dir() + "/" + current, File.READ)
|
mutex.lock()
|
||||||
var img : Image = Image.new()
|
var entry : Entry = queue.pop_front()
|
||||||
img.load_png_from_buffer(file.get_buffer(file.get_len()))
|
mutex.unlock()
|
||||||
file.close()
|
var buffer : PoolByteArray = http_request(entry.path, entry.type)
|
||||||
var img_texture : ImageTexture = ImageTexture.new()
|
if (disk_cache):
|
||||||
img_texture.create_from_image(img, 0)
|
if !d.dir_exists(entry.filename.get_base_dir()):
|
||||||
cache_mutex.lock()
|
d.make_dir(entry.filename.get_base_dir())
|
||||||
cached_images[cache_dir][current.get_basename()] = img_texture
|
f.open(entry.filename, File.WRITE)
|
||||||
cache_mutex.unlock()
|
f.store_buffer(buffer)
|
||||||
current = dir.get_next()
|
f.close()
|
||||||
dir.open(disk_cache_path)
|
var texture = ImageTexture.new()
|
||||||
dir.list_dir_begin(true)
|
var img : Image = Image.new()
|
||||||
var current = dir.get_next()
|
img.load_png_from_buffer(buffer)
|
||||||
while current != "":
|
if entry.type == RequestType.BADGE:
|
||||||
if(!dir.current_is_dir()):
|
caches[RequestType.BADGE][entry.data[0]][entry.data[1]].create_from_image(img, 0)
|
||||||
file.open(disk_cache_path + "/" + current, File.READ)
|
elif entry.type == RequestType.EMOTE:
|
||||||
badge_map[current.get_basename()] = parse_json(file.get_as_text())["badge_sets"]
|
caches[RequestType.EMOTE][entry.data[0]].create_from_image(img, 0)
|
||||||
|
yield(Engine.get_main_loop(), "idle_frame")
|
||||||
|
|
||||||
|
# Gets badge mappings for the specified channel. Default: _global (global mappings)
|
||||||
|
func get_badge_mapping(channel_id : String = "_global") -> Dictionary:
|
||||||
|
if !caches[RequestType.BADGE_MAPPING].has(channel_id):
|
||||||
|
var filename : String = cache_path + "/" + RequestType.keys()[RequestType.BADGE_MAPPING] + "/" + channel_id + ".json"
|
||||||
|
if !disk_cache && file.file_exists(filename):
|
||||||
|
file.open(filename, File.READ)
|
||||||
|
caches[RequestType.BADGE_MAPPING][channel_id] = parse_json(file.get_as_text())["badge_sets"]
|
||||||
|
file.close()
|
||||||
|
var buffer : PoolByteArray = http_request(channel_id, RequestType.BADGE_MAPPING)
|
||||||
|
if !buffer.empty():
|
||||||
|
caches[RequestType.BADGE_MAPPING][channel_id] = parse_json(buffer.get_string_from_utf8())["badge_sets"]
|
||||||
|
if (disk_cache):
|
||||||
|
file.open(filename, File.WRITE)
|
||||||
|
file.store_buffer(buffer)
|
||||||
file.close()
|
file.close()
|
||||||
current = dir.get_next()
|
else:
|
||||||
get_badge_mappings()
|
return {}
|
||||||
yield(self, "badge_mapping_available")
|
return caches[RequestType.BADGE_MAPPING][channel_id]
|
||||||
|
|
||||||
func create_request(url : String, resource : String, res_type : String) -> void:
|
func get_badge(badge_name : String, channel_id : String = "_global", scale : String = "1") -> ImageTexture:
|
||||||
var http_request = HTTPRequest.new()
|
var badge_data : PoolStringArray = badge_name.split("/", true, 1)
|
||||||
http_request.connect("request_completed", self, "downloaded", [http_request, resource, res_type], CONNECT_ONESHOT)
|
var texture : ImageTexture = ImageTexture.new()
|
||||||
add_child(http_request)
|
var cachename = badge_data[0] + "_" + badge_data[1] + "_" + scale
|
||||||
http_request.download_file = disk_cache_path + "/" + res_type + "/" + resource + ".png"
|
var filename : String = cache_path + "/" + RequestType.keys()[RequestType.BADGE] + "/" + channel_id + "/" + cachename + ".png"
|
||||||
http_request.request(url, [], false, HTTPClient.METHOD_GET)
|
if !caches[RequestType.BADGE].has(channel_id):
|
||||||
|
caches[RequestType.BADGE][channel_id] = {}
|
||||||
|
if !caches[RequestType.BADGE][channel_id].has(cachename):
|
||||||
|
if !disk_cache && file.file_exists(filename):
|
||||||
|
file.open(filename, File.READ)
|
||||||
|
var img : Image = Image.new()
|
||||||
|
img.load_png_from_buffer(file.get_buffer(file.get_len()))
|
||||||
|
texture.create_from_image(img)
|
||||||
|
file.close()
|
||||||
|
else:
|
||||||
|
var map : Dictionary = caches[RequestType.BADGE_MAPPING].get(channel_id, get_badge_mapping(channel_id))
|
||||||
|
if !map.empty():
|
||||||
|
if map.has(badge_data[0]):
|
||||||
|
mutex.lock()
|
||||||
|
queue.append(Entry.new(map[badge_data[0]]["versions"][badge_data[1]]["image_url_" + scale + "x"].substr("https://static-cdn.jtvnw.net/badges/v1/".length()), RequestType.BADGE, filename, [channel_id, cachename]))
|
||||||
|
mutex.unlock()
|
||||||
|
var img = preload("res://addons/gift/placeholder.png")
|
||||||
|
texture.create_from_image(img)
|
||||||
|
elif channel_id != "_global":
|
||||||
|
return get_badge(badge_name, "_global", scale)
|
||||||
|
elif channel_id != "_global":
|
||||||
|
return get_badge(badge_name, "_global", scale)
|
||||||
|
texture.take_over_path(filename)
|
||||||
|
caches[RequestType.BADGE][channel_id][cachename] = texture
|
||||||
|
elif channel_id != "_global":
|
||||||
|
return get_badge(badge_name, "_global", scale)
|
||||||
|
return caches[RequestType.BADGE][channel_id][cachename]
|
||||||
|
|
||||||
# Gets badge mappings for the specified channel. Empty String will get the mappings for global badges instead.
|
func get_emote(emote_id : String, scale = "1.0") -> ImageTexture:
|
||||||
func get_badge_mappings(channel_id : String = "") -> void:
|
var texture : ImageTexture = ImageTexture.new()
|
||||||
var url : String
|
var cachename : String = emote_id + "_" + scale
|
||||||
if(channel_id == ""):
|
var filename : String = cache_path + "/" + RequestType.keys()[RequestType.EMOTE] + "/" + cachename + ".png"
|
||||||
channel_id = "_global"
|
if !caches[RequestType.EMOTE].has(cachename):
|
||||||
url = "https://badges.twitch.tv/v1/badges/global/display"
|
if !disk_cache && file.file_exists(filename):
|
||||||
|
file.open(filename, File.READ)
|
||||||
|
var img : Image = Image.new()
|
||||||
|
img.load_png_from_buffer(file.get_buffer(file.get_len()))
|
||||||
|
texture.create_from_image(img)
|
||||||
|
file.close()
|
||||||
|
else:
|
||||||
|
mutex.lock()
|
||||||
|
queue.append(Entry.new(emote_id + "/" + scale, RequestType.EMOTE, filename, [cachename]))
|
||||||
|
mutex.unlock()
|
||||||
|
var img = preload("res://addons/gift/placeholder.png")
|
||||||
|
texture.create_from_image(img)
|
||||||
|
texture.take_over_path(filename)
|
||||||
|
caches[RequestType.EMOTE][cachename] = texture
|
||||||
|
return caches[RequestType.EMOTE][cachename]
|
||||||
|
|
||||||
|
func http_request(path : String, type : int) -> PoolByteArray:
|
||||||
|
var error := 0
|
||||||
|
var buffer = PoolByteArray()
|
||||||
|
var new_host : String
|
||||||
|
match type:
|
||||||
|
RequestType.BADGE_MAPPING:
|
||||||
|
new_host = "badges.twitch.tv"
|
||||||
|
path = "/v1/badges/" + ("global" if path == "_global" else "channels/" + path) + "/display"
|
||||||
|
RequestType.BADGE, RequestType.EMOTE:
|
||||||
|
new_host = "static-cdn.jtvnw.net"
|
||||||
|
if type == RequestType.BADGE:
|
||||||
|
path = "/badges/v1/" + path
|
||||||
|
else:
|
||||||
|
path = "/emoticons/v1/" + path
|
||||||
|
if (host != new_host):
|
||||||
|
error = http_client.connect_to_host(new_host, 443, true)
|
||||||
|
while http_client.get_status() == HTTPClient.STATUS_CONNECTING or http_client.get_status() == HTTPClient.STATUS_RESOLVING:
|
||||||
|
http_client.poll()
|
||||||
|
delay(100)
|
||||||
|
if (error != OK):
|
||||||
|
print("Could not connect to " + new_host + ". Images disabled.")
|
||||||
|
active = false
|
||||||
|
return buffer
|
||||||
|
host = new_host
|
||||||
|
http_client.request(HTTPClient.METHOD_GET, path, HEADERS)
|
||||||
|
while (http_client.get_status() == HTTPClient.STATUS_REQUESTING):
|
||||||
|
http_client.poll()
|
||||||
|
delay(50)
|
||||||
|
if !(http_client.get_status() == HTTPClient.STATUS_BODY or http_client.get_status() == HTTPClient.STATUS_CONNECTED):
|
||||||
|
print("Request failed. Skipped " + path + " (" + RequestType.keys()[type] + ")")
|
||||||
|
return buffer
|
||||||
|
while (http_client.get_status() == HTTPClient.STATUS_BODY):
|
||||||
|
http_client.poll()
|
||||||
|
delay(1)
|
||||||
|
var chunk = http_client.read_response_body_chunk()
|
||||||
|
if (chunk.size() == 0):
|
||||||
|
delay(1)
|
||||||
|
else:
|
||||||
|
buffer += chunk
|
||||||
|
return buffer
|
||||||
|
|
||||||
|
func delay(delay : int):
|
||||||
|
if (OS.has_feature("web")):
|
||||||
|
yield(Engine.get_main_loop(), "idle_frame")
|
||||||
else:
|
else:
|
||||||
url = "https://badges.twitch.tv/v1/badges/channels/" + channel_id + "/display"
|
OS.delay_msec(delay)
|
||||||
if(!badge_map.has(channel_id)):
|
|
||||||
var http_request = HTTPRequest.new()
|
|
||||||
add_child(http_request)
|
|
||||||
http_request.request(url, [], false, HTTPClient.METHOD_GET)
|
|
||||||
http_request.connect("request_completed", self, "badge_mapping_received", [http_request, channel_id], CONNECT_ONESHOT)
|
|
||||||
else:
|
|
||||||
emit_signal("badge_mapping_available")
|
|
||||||
|
|
||||||
func get_emote(id : String) -> ImageTexture:
|
class Entry extends Reference:
|
||||||
cache_mutex.lock()
|
var path : String
|
||||||
if(cached_images["emotes"].has(id)):
|
var type : int
|
||||||
return cached_images["emotes"][id]
|
var filename : String
|
||||||
else:
|
var data : Array
|
||||||
create_request("http://static-cdn.jtvnw.net/emoticons/v1/" + id + "/1.0", id, "emotes")
|
|
||||||
cache_mutex.unlock()
|
func _init(path : String, type : int, filename : String, data : Array):
|
||||||
return null
|
self.path = path
|
||||||
|
self.type = type
|
||||||
func get_badge(badge_name : String, channel_id : String = "") -> ImageTexture:
|
self.filename = filename
|
||||||
cache_mutex.lock()
|
self.data = data
|
||||||
var badge_data : PoolStringArray = badge_name.split("/")
|
|
||||||
if(cached_images["badges"].has(badge_data[0])):
|
|
||||||
return cached_images["badges"][badge_data[0]]
|
|
||||||
var channel : String
|
|
||||||
if(!badge_map[channel_id].has(badge_data[0])):
|
|
||||||
channel_id = "_global"
|
|
||||||
create_request(badge_map[channel_id][badge_data[0]]["versions"][badge_data[1]]["image_url_1x"], badge_data[0], "badges")
|
|
||||||
cache_mutex.unlock()
|
|
||||||
return null
|
|
||||||
|
|
||||||
func downloaded(result : int, response_code : int, headers : PoolStringArray, body : PoolByteArray, request : HTTPRequest, id : String, type : String) -> void:
|
|
||||||
if(type == "emotes"):
|
|
||||||
get_parent().emit_signal("emote_downloaded", id)
|
|
||||||
elif(type == "badges"):
|
|
||||||
get_parent().emit_signal("badge_downloaded", id)
|
|
||||||
request.queue_free()
|
|
||||||
|
|
||||||
func badge_mapping_received(result : int, response_copde : int, headers : PoolStringArray, body : PoolByteArray, request : HTTPRequest, id : String) -> void:
|
|
||||||
badge_map[id] = parse_json(body.get_string_from_utf8())["badge_sets"]
|
|
||||||
if(disk_cache):
|
|
||||||
file.open(disk_cache_path + "/" + id + ".json", File.WRITE)
|
|
||||||
file.store_buffer(body)
|
|
||||||
file.close()
|
|
||||||
emit_signal("badge_mapping_available")
|
|
||||||
request.queue_free()
|
|
||||||
|
@ -8,4 +8,4 @@ var tags : Dictionary
|
|||||||
func _init(usr : String, ch : String, tag_dict : Dictionary):
|
func _init(usr : String, ch : String, tag_dict : Dictionary):
|
||||||
user = usr
|
user = usr
|
||||||
channel = ch
|
channel = ch
|
||||||
tags = tag_dict
|
tags = tag_dict
|
||||||
|
Loading…
x
Reference in New Issue
Block a user