Initial Android commit

This commit is contained in:
OleSTEEP 2024-11-10 03:34:28 +03:00
commit 1e2b80c13d
8521 changed files with 231475 additions and 0 deletions

View file

@ -0,0 +1,96 @@
extends Node
class_name BaseEmotesCache
signal downloaded(content)
var http_request_queue:HttpRequestQueue
var ready_to_deliver_emotes: = false
class DownloadedContent:
const CONTENT_TYPE_IMAGE_PNG = "image/png"
const CONTENT_TYPE_IMAGE_JPEG = "image/jpeg"
var id:String
var type:String
var data:PoolByteArray
var image:Image
func _init(id:String, type:String, data:PoolByteArray):
self.id = id
self.type = type
self.data = data
func get_image_from_data()->Image:
var image:Image = Image.new()
if self.type == CONTENT_TYPE_IMAGE_PNG:
image.load_png_from_buffer(data)
elif self.type == CONTENT_TYPE_IMAGE_JPEG:
image.load_jpg_from_buffer(data)
return image
class BaseEmote:
const TEXTURE_NO_FLAGS = 0
static func create_texture_from_image(image:Image)->ImageTexture:
var image_texture: = ImageTexture.new()
image_texture.create_from_image(image)
image_texture.flags -= ImageTexture.FLAG_FILTER + ImageTexture.FLAG_REPEAT
return image_texture
func _ready()->void :
__initialize()
__initialize_http_request_queue()
__connect_signals()
func _downloaded(downloaded_content:BaseEmotesCache.DownloadedContent)->void :
" \n Override to define behaviour on emote content downloaded. \n "
pass
func _get_emote_url(code:String)->String:
" \n Override to prepare the emote retrieval URL by code. \n "
return ""
func __initialize()->void :
" \n Override for initialization, instead of _ready. \n "
pass
func __connect_signals()->void :
http_request_queue.connect("request_completed", self, "_on_http_request_queue_request_complete")
func __initialize_http_request_queue()->void :
http_request_queue = HttpRequestQueue.new()
add_child(http_request_queue)
func __cache_emote(code)->void :
var url:String = _get_emote_url(code)
__download(code, url)
func __download(id:String, url:String)->void :
http_request_queue.enqueue_request(id, url)
func _on_http_request_queue_request_complete(id:String, result:int, response_code:int, headers:PoolStringArray, body:PoolByteArray)->void :
var downloaded_content: = DownloadedContent.new(id, "", body)
if result == HTTPRequest.RESULT_SUCCESS:
var pretty_headers: = HttpHeaders.new(headers)
var content_type: = pretty_headers.headers.get("Content-Type") as String
downloaded_content.type = content_type
_downloaded(downloaded_content)

View file

@ -0,0 +1,112 @@
extends BaseEmotesCache
class_name BttvEmotesCache
signal emote_retrieved(emote)
class BttvEmote:
var id:String
var code:String
var texture:ImageTexture
func _init(id:String, code:String, image:Image):
self.id = id
self.code = code
self.texture = BaseEmotesCache.BaseEmote.create_texture_from_image(image)
const DEFAULT_URL_PROTOCOL = "https://"
const CHANNEL_NAME_PLACEHOLDER = "{{channel_name}}"
const EMOTE_ID_PLACEHOLDER = "{{id}}"
const EMOTE_SIZE_PLACEHOLDER = "{{image}}"
const DEAFULT_EMOTE_IMAGE_SIZE = "1x"
const GLOBAL_EMOTES_REQUEST_ID = "global_emotes"
const CHANNEL_EMOTES_REQUEST_ID = "channel_emotes"
const EMOTES_REQUEST_IDS = [GLOBAL_EMOTES_REQUEST_ID, CHANNEL_EMOTES_REQUEST_ID]
const GLOBAL_EMOTES_URL = "https://api.betterttv.net/2/emotes/"
const CHANNEL_EMOTES_URL_TEMPLATE = "https://api.betterttv.net/2/channels/{{channel_name}}/"
var available_emotes: = Dictionary()
var emote_download_url_template:String
var cache: = Dictionary()
var available_emotes_parsed_count: = 0
func _downloaded(downloaded_content:BaseEmotesCache.DownloadedContent)->void :
if downloaded_content.id in EMOTES_REQUEST_IDS:
__parse_available_emotes(downloaded_content)
available_emotes_parsed_count += 1
ready_to_deliver_emotes = available_emotes_parsed_count >= EMOTES_REQUEST_IDS.size()
else :
var code:String = str(downloaded_content.id)
var id:String = available_emotes.get(code)
var image:Image = downloaded_content.get_image_from_data()
cache[code] = BttvEmote.new(id, code, image)
emit_signal("emote_retrieved", cache.get(code))
func _get_emote_url(code:String)->String:
var id:String = available_emotes.get(code)
if not id:
return ""
var url: = emote_download_url_template.replace(
EMOTE_ID_PLACEHOLDER, id
).replace(
EMOTE_SIZE_PLACEHOLDER, DEAFULT_EMOTE_IMAGE_SIZE
)
return url
func init_emotes(channel_name:String)->void :
http_request_queue.enqueue_request(GLOBAL_EMOTES_REQUEST_ID, GLOBAL_EMOTES_URL)
http_request_queue.enqueue_request(
CHANNEL_EMOTES_REQUEST_ID,
CHANNEL_EMOTES_URL_TEMPLATE.replace(CHANNEL_NAME_PLACEHOLDER, channel_name)
)
func get_emote(code:String)->void :
if not ready_to_deliver_emotes:
return
if cache.has(code):
emit_signal("emote_retrieved", cache.get(code))
else :
__cache_emote(code)
func get_available_emotes_codes()->Array:
return available_emotes.keys()
func __parse_available_emotes(download_content:BaseEmotesCache.DownloadedContent)->void :
if download_content.type != HttpHeaders.HTTP_CONTENT_TYPE_JSON_UTF8:
return
var data = parse_json(download_content.data.get_string_from_utf8())
emote_download_url_template = data.get("urlTemplate", "").replace("//", DEFAULT_URL_PROTOCOL)
for emote in data.get("emotes", []):
available_emotes[emote.get("code")] = emote.get("id")

View file

@ -0,0 +1,117 @@
extends BaseEmotesCache
class_name FfzEmotesCache
signal emote_retrieved(emote)
class FfzEmote:
var id:String
var code:String
var texture:ImageTexture
func _init(id:String, code:String, image:Image):
self.id = id
self.code = code
self.texture = BaseEmotesCache.BaseEmote.create_texture_from_image(image)
const DEFAULT_URL_PROTOCOL = "https://"
const USER_ID_PLACEHOLDER = "{{user_id}}"
const EMOTE_ID_PLACEHOLDER = "{{id}}"
const EMOTE_SIZE_PLACEHOLDER = "{{image}}"
const DEAFULT_EMOTE_IMAGE_SIZE = "1x"
const GLOBAL_EMOTES_REQUEST_ID = "global_emotes"
const CHANNEL_EMOTES_REQUEST_ID = "channel_emotes"
const EMOTES_REQUEST_IDS = [GLOBAL_EMOTES_REQUEST_ID, CHANNEL_EMOTES_REQUEST_ID]
const GLOBAL_EMOTES_URL = "https://api.frankerfacez.com/v1/set/global"
const CHANNEL_EMOTES_URL_TEMPLATE = "https://api.frankerfacez.com/v1/room/id/{{user_id}}"
var available_emotes: = Dictionary()
var cache: = Dictionary()
var available_emotes_parsed_count: = 0
var user_id:String
func _downloaded(downloaded_content:BaseEmotesCache.DownloadedContent)->void :
if downloaded_content.id in EMOTES_REQUEST_IDS:
__parse_available_emotes(downloaded_content)
available_emotes_parsed_count += 1
ready_to_deliver_emotes = available_emotes_parsed_count >= EMOTES_REQUEST_IDS.size()
else :
var code:String = str(downloaded_content.id)
var id:String = available_emotes.get(code, {}).get("id")
var image:Image = downloaded_content.get_image_from_data()
cache[code] = FfzEmote.new(id, code, image)
emit_signal("emote_retrieved", cache.get(code))
func _get_emote_url(code:String)->String:
var url:String = available_emotes.get(code, {}).get("url", "")
return url
func init_emotes(user_id:String, force:bool = false)->void :
if self.user_id == null or self.user_id != user_id or force:
user_id = user_id
http_request_queue.enqueue_request(GLOBAL_EMOTES_REQUEST_ID, GLOBAL_EMOTES_URL)
http_request_queue.enqueue_request(
CHANNEL_EMOTES_REQUEST_ID,
CHANNEL_EMOTES_URL_TEMPLATE.replace(USER_ID_PLACEHOLDER, user_id)
)
func get_emote(code:String)->void :
if not ready_to_deliver_emotes:
return
if cache.has(code):
emit_signal("emote_retrieved", cache.get(code))
else :
__cache_emote(code)
func get_available_emotes_codes()->Array:
return available_emotes.keys()
func __parse_available_emotes(download_content:BaseEmotesCache.DownloadedContent)->void :
if download_content.type != HttpHeaders.HTTP_CONTENT_TYPE_JSON:
return
var data = parse_json(download_content.data.get_string_from_utf8())
var sets: = data.get("sets") as Dictionary
for set in sets.values():
var emotes: = set.get("emoticons") as Array
for emote in emotes:
var emote_url:String = emote.get("urls", {}).get("1", "").replace(
"//", DEFAULT_URL_PROTOCOL
)
var id: = str(emote.get("id"), "")
available_emotes[emote.get("name")] = {
"id":id,
"url":emote_url
}

View file

@ -0,0 +1,59 @@
extends BaseEmotesCache
class_name TwitchEmotesCache
signal emote_retrieved(emote)
class TwitchEmote:
var id:int
var code:String
var texture:ImageTexture
func _init(id:int, code:String, image:Image):
self.id = id
self.code = code
self.texture = BaseEmotesCache.BaseEmote.create_texture_from_image(image)
const EMOTE_URL_TEMPLATE = "https://static-cdn.jtvnw.net/emoticons/v1/{emote_id}/1.0"
var cache: = Dictionary()
func _ready():
._ready()
ready_to_deliver_emotes = true
func get_emote(id:int):
if not ready_to_deliver_emotes:
return
if cache.has(id):
emit_signal("emote_retrieved", cache.get(id))
else :
__cache_emote(str(id))
func _get_emote_url(code)->String:
var string_id: = str(code)
var url: = EMOTE_URL_TEMPLATE.replace("{emote_id}", string_id)
return url
func _downloaded(downloaded_content:BaseEmotesCache.DownloadedContent)->void :
var id_: = int(downloaded_content.id)
var image:Image = downloaded_content.get_image_from_data()
cache[id_] = TwitchEmote.new(id_, "", image)
emit_signal("emote_retrieved", cache.get(id_))