mirror of
https://github.com/cyberarm/w3d_hub_linux_launcher.git
synced 2026-03-22 12:16:15 +00:00
Compare commits
8 Commits
f98d8c3394
...
refactor-b
| Author | SHA1 | Date | |
|---|---|---|---|
| 3e4b25f0d4 | |||
| ebc045019a | |||
| 79858a02ce | |||
| 68df923bea | |||
| ddbec8d72c | |||
| 70d4e0c40f | |||
| f651143937 | |||
| 1425225eef |
@@ -43,7 +43,7 @@ GEM
|
||||
fiber-storage (1.0.1)
|
||||
fiddle (1.1.8)
|
||||
gosu (1.4.6)
|
||||
io-endpoint (0.17.1)
|
||||
io-endpoint (0.17.2)
|
||||
io-event (1.14.2)
|
||||
io-stream (0.11.1)
|
||||
ircparser (1.0.0)
|
||||
@@ -97,4 +97,4 @@ DEPENDENCIES
|
||||
win32-security
|
||||
|
||||
BUNDLED WITH
|
||||
2.6.8
|
||||
4.0.3
|
||||
|
||||
298
lib/api.rb
298
lib/api.rb
@@ -3,7 +3,7 @@ class W3DHub
|
||||
|
||||
LOG_TAG = "W3DHub::Api".freeze
|
||||
|
||||
API_TIMEOUT = 30 # seconds
|
||||
API_TIMEOUT = 10 # seconds
|
||||
USER_AGENT = "Cyberarm's Linux Friendly W3D Hub Launcher v#{W3DHub::VERSION}".freeze
|
||||
DEFAULT_HEADERS = [
|
||||
["user-agent", USER_AGENT],
|
||||
@@ -16,32 +16,7 @@ class W3DHub
|
||||
].freeze
|
||||
|
||||
def self.on_thread(method, *args, &callback)
|
||||
raise "Renew."
|
||||
BackgroundWorker.foreground_job(-> { Api.send(method, *args) }, callback)
|
||||
end
|
||||
|
||||
class Response
|
||||
def initialize(error: nil, status: -1, body: "")
|
||||
@status = status
|
||||
@body = body
|
||||
@error = error
|
||||
end
|
||||
|
||||
def success?
|
||||
@status == 200
|
||||
end
|
||||
|
||||
def status
|
||||
@status
|
||||
end
|
||||
|
||||
def body
|
||||
@body
|
||||
end
|
||||
|
||||
def error
|
||||
@error
|
||||
end
|
||||
Api.send(method, *args, &callback)
|
||||
end
|
||||
|
||||
#! === W3D Hub API === !#
|
||||
@@ -50,7 +25,9 @@ class W3DHub
|
||||
|
||||
HTTP_CLIENTS = {}
|
||||
|
||||
def self.async_http(method, path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub)
|
||||
def self.async_http(method:, path:, headers:, body:, backend:, async:, &callback)
|
||||
raise "NO CALLBACK DEFINED!" unless callback
|
||||
|
||||
case backend
|
||||
when :w3dhub
|
||||
endpoint = W3DHUB_API_ENDPOINT
|
||||
@@ -79,52 +56,20 @@ class W3DHub
|
||||
headers << ["authorization", "Bearer #{Store.account.access_token}"]
|
||||
end
|
||||
|
||||
Sync do
|
||||
begin
|
||||
response = provision_http_client(endpoint).send(method, path, headers, body)
|
||||
|
||||
Response.new(status: response.status, body: response.read)
|
||||
rescue Async::TimeoutError => e
|
||||
logger.error(LOG_TAG) { "Connection to \"#{url}\" timed out after: #{API_TIMEOUT} seconds" }
|
||||
|
||||
Response.new(error: e)
|
||||
rescue StandardError => e
|
||||
logger.error(LOG_TAG) { "Connection to \"#{url}\" errored:" }
|
||||
logger.error(LOG_TAG) { e }
|
||||
|
||||
Response.new(error: e)
|
||||
ensure
|
||||
response&.close
|
||||
end
|
||||
end
|
||||
Store.network_manager.request(method, url, headers, body, async, &callback)
|
||||
end
|
||||
|
||||
def self.provision_http_client(hostname)
|
||||
# Pin http clients to their host Thread so the fiber scheduler doesn't get upset and raise an error
|
||||
HTTP_CLIENTS[Thread.current] ||= {}
|
||||
return HTTP_CLIENTS[Thread.current][hostname.downcase] if HTTP_CLIENTS[Thread.current][hostname.downcase]
|
||||
|
||||
ssl_context = W3DHub.ca_bundle_path ? OpenSSL::SSL::SSLContext.new : nil
|
||||
ssl_context&.set_params(
|
||||
ca_file: W3DHub.ca_bundle_path,
|
||||
verify_mode: OpenSSL::SSL::VERIFY_PEER
|
||||
)
|
||||
|
||||
endpoint = Async::HTTP::Endpoint.parse(hostname, ssl_context: ssl_context)
|
||||
HTTP_CLIENTS[Thread.current][hostname.downcase] = Async::HTTP::Client.new(endpoint)
|
||||
def self.post(path:, headers: DEFAULT_HEADERS, body: nil, backend: :w3dhub, async: true, &callback)
|
||||
async_http(method: :post, path: path, headers: headers, body: body, backend: backend, async: async, &callback)
|
||||
end
|
||||
|
||||
def self.post(path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub)
|
||||
async_http(:post, path, headers, body, backend)
|
||||
end
|
||||
|
||||
def self.get(path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub)
|
||||
async_http(:get, path, headers, body, backend)
|
||||
def self.get(path:, headers: DEFAULT_HEADERS, body: nil, backend: :w3dhub, async: true, &callback)
|
||||
async_http(method: :get, path: path, headers: headers, body: body, backend: backend, async: async, &callback)
|
||||
end
|
||||
|
||||
# Api.get but handles any URL instead of known hosts
|
||||
def self.fetch(path, headers = DEFAULT_HEADERS, body = nil, backend = nil)
|
||||
async_http(:get, path, headers, body, backend)
|
||||
def self.fetch(path:, headers: DEFAULT_HEADERS, body: nil, backend: :w3dhub, async: true, &callback)
|
||||
async_http(method: :get, path: path, headers: headers, body: body, backend: backend, async: async, &callback)
|
||||
end
|
||||
|
||||
# Method: POST
|
||||
@@ -142,102 +87,139 @@ class W3DHub
|
||||
#
|
||||
# On a failed login the service responds with:
|
||||
# {"error":"login-failed"}
|
||||
def self.refresh_user_login(refresh_token, backend = :w3dhub)
|
||||
def self.refresh_user_login(refresh_token, backend = :w3dhub, &callback)
|
||||
body = URI.encode_www_form("data": JSON.dump({ refreshToken: refresh_token }))
|
||||
response = post("/apis/launcher/1/user-login", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if response.status == 200
|
||||
user_data = JSON.parse(response.body, symbolize_names: true)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
user_data = JSON.parse(result.data, symbolize_names: true)
|
||||
|
||||
return false if user_data[:error]
|
||||
if user_data[:error]
|
||||
callback.call(CyberarmEngine::Result.new(data: false))
|
||||
next
|
||||
end
|
||||
|
||||
user_details_data = user_details(user_data[:userid]) || {}
|
||||
|
||||
Account.new(user_data, user_details_data)
|
||||
callback.call(CyberarmEngine::Result.new(data: Account.new(user_data, user_details_data)))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch refresh user login:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
post(path: "/apis/launcher/1/user-login", headers: FORM_ENCODED_HEADERS, body: body, backend: backend, &handler)
|
||||
end
|
||||
|
||||
# See #user_refresh_token
|
||||
def self.user_login(username, password, backend = :w3dhub)
|
||||
def self.user_login(username, password, backend = :w3dhub, &callback)
|
||||
body = URI.encode_www_form("data": JSON.dump({ username: username, password: password }))
|
||||
response = post("/apis/launcher/1/user-login", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if response.status == 200
|
||||
user_data = JSON.parse(response.body, symbolize_names: true)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
user_data = JSON.parse(result.data, symbolize_names: true)
|
||||
|
||||
return false if user_data[:error]
|
||||
if user_data[:error]
|
||||
callback.call(CyberarmEngine::Result.new(data: false))
|
||||
next
|
||||
end
|
||||
|
||||
user_details_data = user_details(user_data[:userid]) || {}
|
||||
|
||||
Account.new(user_data, user_details_data)
|
||||
callback.call(CyberarmEngine::Result.new(data: Account.new(user_data, user_details_data)))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch user login:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
post(path: "/apis/launcher/1/user-login", headers: FORM_ENCODED_HEADERS, body: body, backend: backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/w3dhub/1/get-user-details
|
||||
#
|
||||
# Response: avatar-uri (Image download uri), id, username
|
||||
def self.user_details(id, backend = :w3dhub)
|
||||
def self.user_details(id, backend = :w3dhub, &callback)
|
||||
body = URI.encode_www_form("data": JSON.dump({ id: id }))
|
||||
user_details = post("/apis/w3dhub/1/get-user-details", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if user_details.status == 200
|
||||
JSON.parse(user_details.body, symbolize_names: true)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(CyberarmEngine::Result.new(data: JSON.parse(result.data, symbolize_names: true)))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch user details:" }
|
||||
logger.error(LOG_TAG) { user_details }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
post(path: "/apis/w3dhub/1/get-user-details", headers: FORM_ENCODED_HEADERS, body: body, backend: backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/w3dhub/1/get-service-status
|
||||
# Service response:
|
||||
# {"services":{"authentication":true,"packageDownload":true}}
|
||||
def self.service_status(backend = :w3dhub)
|
||||
response = post("/apis/w3dhub/1/get-service-status", DEFAULT_HEADERS, nil, backend)
|
||||
|
||||
if response.status == 200
|
||||
ServiceStatus.new(response.body)
|
||||
def self.service_status(backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(CyberarmEngine::Result.new(data: ServiceStatus.new(result.data)))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch service status:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
post(path: "/apis/w3dhub/1/get-service-status", backend: backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/launcher/1/get-applications
|
||||
# Client sends an Authorization header bearer token which is received from logging in (Optional)
|
||||
# Launcher sends an empty data request: data={}
|
||||
# Response is a list of applications/games
|
||||
def self.applications(backend = :w3dhub)
|
||||
response = post("/apis/launcher/1/get-applications", DEFAULT_HEADERS, nil, backend)
|
||||
def self.applications(backend = :w3dhub, &callback)
|
||||
async = !callback.nil?
|
||||
|
||||
if response.status == 200
|
||||
Applications.new(response.body, backend)
|
||||
# Complicated why to "return" direct value
|
||||
callback = ->(result) { result }
|
||||
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(CyberarmEngine::Result.new(data: Applications.new(result.data, backend)))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch applications list:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
post(path: "/apis/launcher/1/get-applications", async: async, backend: backend, &handler)
|
||||
end
|
||||
|
||||
# Populate applications list from primary and alternate backends
|
||||
# (alternate only has latest public builds of _most_ games)
|
||||
def self._applications
|
||||
applications_primary = Store.account ? Api.applications(:w3dhub) : false
|
||||
applications_alternate = Api.applications(:alt_w3dhub)
|
||||
def self._applications(&callback)
|
||||
handler = lambda do |result|
|
||||
# nothing special on offer if we're not logged in
|
||||
applications_primary = Store.account ? Api.applications(:w3dhub).data : false
|
||||
applications_alternate = Api.applications(:alt_w3dhub).data
|
||||
|
||||
# Fail if we fail to fetch applications list from either backend
|
||||
return false unless applications_primary || applications_alternate
|
||||
unless applications_primary || applications_alternate
|
||||
callback.call(CyberarmEngine::Result.new)
|
||||
next
|
||||
end
|
||||
|
||||
return applications_alternate unless applications_primary
|
||||
unless applications_primary
|
||||
callback.call(CyberarmEngine::Result.new(data: applications_alternate))
|
||||
next
|
||||
end
|
||||
|
||||
# Merge the two app lists together
|
||||
apps = applications_alternate
|
||||
@@ -294,70 +276,83 @@ class W3DHub
|
||||
end
|
||||
end
|
||||
|
||||
apps
|
||||
callback.call(CyberarmEngine::Result.new(data: apps))
|
||||
end
|
||||
|
||||
# Bit hacky but we just need to run this handler from the networking thread and async reactor
|
||||
get(path: "", backend: nil, &handler)
|
||||
end
|
||||
|
||||
# /apis/w3dhub/1/get-news
|
||||
# Client sends an Authorization header bearer token which is received from logging in (Optional)
|
||||
# Client requests news for a specific application/game e.g.: data={"category":"ia"} ("launcher-home" retrieves the weekly hub updates)
|
||||
# Response is a JSON hash with a "highlighted" and "news" keys; the "news" one seems to be the desired one
|
||||
def self.news(category, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({category: category}))
|
||||
response = post("/apis/w3dhub/1/get-news", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if response.status == 200
|
||||
News.new(response.body)
|
||||
def self.news(category, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(CyberarmEngine::Result.new(data: News.new(result.data)))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch news for:" }
|
||||
logger.error(LOG_TAG) { category }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ category: category }))
|
||||
post(path: "/apis/w3dhub/1/get-news", headers: FORM_ENCODED_HEADERS, body: body, backend: backend, &handler)
|
||||
end
|
||||
|
||||
# Downloading games
|
||||
|
||||
# /apis/launcher/1/get-package-details
|
||||
# client requests package details: data={"packages":[{"category":"games","name":"apb.ico","subcategory":"apb","version":""}]}
|
||||
def self.package_details(packages, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({ packages: packages }))
|
||||
response = post("/apis/launcher/1/get-package-details", FORM_ENCODED_HEADERS, body, backend)
|
||||
def self.package_details(packages, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
hash = JSON.parse(result.data, symbolize_names: true)
|
||||
|
||||
if response.status == 200
|
||||
hash = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
hash[:packages].map { |pkg| Package.new(pkg) }
|
||||
callback.call(CyberarmEngine::Result.new(data: hash[:packages].map { |pkg| Package.new(pkg) }))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch package details for:" }
|
||||
logger.error(LOG_TAG) { packages }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ packages: packages }))
|
||||
post(path: "/apis/launcher/1/get-package-details", headers: FORM_ENCODED_HEADERS, body: body, backend: backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/launcher/1/get-package
|
||||
# client requests package: data={"category":"games","name":"ECW_Asteroids.zip","subcategory":"ecw","version":"1.0.0.0"}
|
||||
#
|
||||
# server responds with download bytes, probably supports chunked download and resume
|
||||
def self.package(package, &block)
|
||||
Cache.fetch_package(package, block)
|
||||
# FIXME: REFACTOR Cache.fetch_package to use HttpClient
|
||||
def self.package(package, &callback)
|
||||
Cache.fetch_package(package, callback)
|
||||
end
|
||||
|
||||
# /apis/w3dhub/1/get-events
|
||||
#
|
||||
# clients requests events: data={"serverPath":"apb"}
|
||||
def self.events(app_id, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({ serverPath: app_id }))
|
||||
response = post("/apis/w3dhub/1/get-server-events", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if response.status == 200
|
||||
array = JSON.parse(response.body, symbolize_names: true)
|
||||
array.map { |e| Event.new(e) }
|
||||
def self.events(app_id, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
array = JSON.parse(result.data, symbolize_names: true)
|
||||
callback.call(CyberarmEngine::Result.new(data: array.map { |e| Event.new(e) }))
|
||||
else
|
||||
false
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ serverPath: app_id }))
|
||||
post(path: "/apis/w3dhub/1/get-server-events", headers: FORM_ENCODED_HEADERS, body: body, backend: backend, &handler)
|
||||
end
|
||||
|
||||
#! === Server List API === !#
|
||||
|
||||
# SERVER_LIST_ENDPOINT = "https://gsh.w3dhub.com".freeze
|
||||
@@ -381,15 +376,17 @@ class W3DHub
|
||||
# id, name, score, kills, deaths
|
||||
# ...players[]:
|
||||
# nick, team (index of teams array), score, kills, deaths
|
||||
def self.server_list(level = 1, backend = :gsh)
|
||||
response = get("/listings/getAll/v2?statusLevel=#{level}", DEFAULT_HEADERS, nil, backend)
|
||||
|
||||
if response.status == 200
|
||||
data = JSON.parse(response.body, symbolize_names: true)
|
||||
return data.map { |hash| ServerListServer.new(hash) }
|
||||
def self.server_list(level = 1, backend = :gsh, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
data = JSON.parse(result.data, symbolize_names: true)
|
||||
callback.call(CyberarmEngine::Result.new(data: data.map { |hash| ServerListServer.new(hash) }))
|
||||
else
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
false
|
||||
get(path: "/listings/getAll/v2?statusLevel=#{level}", backend: backend, &handler)
|
||||
end
|
||||
|
||||
# /listings/getStatus/v2/:id?statusLevel=#{0-2}
|
||||
@@ -403,23 +400,24 @@ class W3DHub
|
||||
# id, name, score, kills, deaths
|
||||
# ...players[]:
|
||||
# nick, team (index of teams array), score, kills, deaths
|
||||
def self.server_details(id, level, backend = :gsh)
|
||||
def self.server_details(id, level, backend = :gsh, &callback)
|
||||
return false unless id && level
|
||||
|
||||
response = get("/listings/getStatus/v2/#{id}?statusLevel=#{level}", DEFAULT_HEADERS, nil, backend)
|
||||
|
||||
if response.status == 200
|
||||
hash = JSON.parse(response.body, symbolize_names: true)
|
||||
return hash
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(CyberarmEngine::Result.new(data: JSON.parse(result.data, symbolize_names: true)))
|
||||
else
|
||||
callback.call(result)
|
||||
end
|
||||
end
|
||||
|
||||
false
|
||||
get(path: "/listings/getStatus/v2/#{id}?statusLevel=#{level}", backend: backend, &handler)
|
||||
end
|
||||
|
||||
# /listings/push/v2/negotiate?negotiateVersion=1
|
||||
##? /listings/push/v2/?id=#{websocket token?}
|
||||
## Websocket server list listener
|
||||
def self.server_list_push(id)
|
||||
def self.server_list_push(id, &callback)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -61,7 +61,7 @@ class W3DHub
|
||||
States::Interface.instance&.update_server_ping(self)
|
||||
end
|
||||
|
||||
return unless W3DHub.windows?
|
||||
return #unless W3DHub.windows?
|
||||
|
||||
W3DHub::BackgroundWorker.foreground_parallel_job(
|
||||
lambda do
|
||||
|
||||
@@ -23,7 +23,7 @@ class W3DHub
|
||||
@invocation_id = 0
|
||||
|
||||
logger.info(LOG_TAG) { "Starting emulated SignalR Server List Updater..." }
|
||||
run
|
||||
# run
|
||||
end
|
||||
|
||||
def run
|
||||
@@ -32,7 +32,8 @@ class W3DHub
|
||||
begin
|
||||
@auto_reconnect = true
|
||||
|
||||
while W3DHub::BackgroundWorker.alive?
|
||||
# FIXME
|
||||
while true #W3DHub::BackgroundWorker.alive?
|
||||
connect if @auto_reconnect
|
||||
sleep @reconnection_delay
|
||||
end
|
||||
@@ -54,19 +55,31 @@ class W3DHub
|
||||
@auto_reconnect = false
|
||||
|
||||
logger.debug(LOG_TAG) { "Requesting connection token..." }
|
||||
response = Api.post("/listings/push/v2/negotiate?negotiateVersion=1", Api::DEFAULT_HEADERS, "", :gsh)
|
||||
|
||||
if response.status != 200
|
||||
result = nil
|
||||
Api.post("/listings/push/v2/negotiate?negotiateVersion=1", Api::DEFAULT_HEADERS, "", :gsh) do |callback_result|
|
||||
result = callback_result
|
||||
end
|
||||
|
||||
# FIXME: we've introduced ourselves to callback hell, yay!
|
||||
while result.nil?
|
||||
sleep 0.1
|
||||
end
|
||||
|
||||
if result.error?
|
||||
@auto_reconnect = true
|
||||
@reconnection_delay = @reconnection_delay * 2
|
||||
@reconnection_delay *= 2
|
||||
@reconnection_delay = 60 if @reconnection_delay > 60
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
@reconnection_delay = 1
|
||||
|
||||
data = JSON.parse(response.body, symbolize_names: true)
|
||||
connect_websocket(JSON.parse(result.data, symbolize_names: true))
|
||||
end
|
||||
|
||||
def connect_websocket(data)
|
||||
@invocation_id = 0 if @invocation_id > 9095
|
||||
id = data[:connectionToken]
|
||||
endpoint = "#{Api::SERVER_LIST_ENDPOINT}/listings/push/v2?id=#{id}"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
14
lib/cache.rb
14
lib/cache.rb
@@ -9,19 +9,17 @@ class W3DHub
|
||||
end
|
||||
|
||||
# Fetch a generic uri
|
||||
def self.fetch(uri:, force_fetch: false, async: true, backend: :w3dhub)
|
||||
def self.fetch(uri:, force_fetch: false, backend: :w3dhub)
|
||||
path = path(uri)
|
||||
|
||||
if !force_fetch && File.exist?(path)
|
||||
path
|
||||
elsif async
|
||||
BackgroundWorker.job(
|
||||
-> { Api.fetch(uri, W3DHub::Api::DEFAULT_HEADERS, nil, backend) },
|
||||
->(response) { File.open(path, "wb") { |f| f.write response.body } if response.status == 200 }
|
||||
)
|
||||
else
|
||||
response = Api.fetch(uri, W3DHub::Api::DEFAULT_HEADERS, nil, backend)
|
||||
File.open(path, "wb") { |f| f.write response.body } if response.status == 200
|
||||
Api.fetch(path: uri, backend: backend) do |result|
|
||||
if result.okay?
|
||||
File.open(path, "wb") { |f| f.write result.data }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -2,95 +2,91 @@ class W3DHub
|
||||
# all http(s) requests for API calls and downloading images run through here
|
||||
class NetworkManager
|
||||
NetworkEvent = Data.define(:context, :result)
|
||||
Request = Struct.new(:context, :callback)
|
||||
Request = Struct.new(:active, :context, :async, :callback)
|
||||
Context = Data.define(
|
||||
:request_id,
|
||||
:method,
|
||||
:url,
|
||||
:headers,
|
||||
:body,
|
||||
:bearer_token
|
||||
:body
|
||||
)
|
||||
|
||||
def initialize
|
||||
@requests = {}
|
||||
@requests = []
|
||||
@running = true
|
||||
|
||||
@ractor = Ractor.new do
|
||||
raise "Something has gone quite wrong!" if Ractor.main?
|
||||
@thread = Thread.new do
|
||||
@http_client = HttpClient.new
|
||||
|
||||
queue = []
|
||||
api_client = ApiClient.new
|
||||
Sync do
|
||||
while @running
|
||||
request = @requests.find { |r| !r.active }
|
||||
|
||||
# Ractor has no concept of non-blocking send/receive... :cry:
|
||||
Thread.new do
|
||||
while (context = Ractor.receive) # blocking
|
||||
# we cannot (easily) ensure we always are receive expected data
|
||||
next unless context.is_a?(Context)
|
||||
|
||||
queue << context
|
||||
end
|
||||
end
|
||||
|
||||
Async do
|
||||
loop do
|
||||
context = queue.shift
|
||||
|
||||
# goto sleep for an instant if there is no work to be doing
|
||||
unless context
|
||||
sleep 0.1
|
||||
# goto sleep for an second if there is no work to be doing
|
||||
unless request
|
||||
sleep 1
|
||||
next
|
||||
end
|
||||
|
||||
Sync do
|
||||
result = api_client.handle(context)
|
||||
request.active = true
|
||||
|
||||
Ractor.yield(NetworkEvent.new(context, result))
|
||||
Async do |task|
|
||||
assigned_request = request
|
||||
result = if assigned_request.context.url.empty?
|
||||
assigned_request.callback.call(nil)
|
||||
else
|
||||
@http_client.handle(task, assigned_request)
|
||||
end
|
||||
|
||||
@requests.delete(assigned_request)
|
||||
|
||||
# callback for this is already handled!
|
||||
unless assigned_request.context.url.empty?
|
||||
Store.main_thread_queue << -> { assigned_request.callback.call(result) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
monitor
|
||||
end
|
||||
|
||||
def add_request(url, headers, body, bearer_token, &block)
|
||||
def request(method, url, headers, body, async, &block)
|
||||
request_id = SecureRandom.hex
|
||||
|
||||
@requests << Request.new(
|
||||
request = Request.new(
|
||||
false,
|
||||
Context.new(
|
||||
request_id,
|
||||
method,
|
||||
url,
|
||||
headers,
|
||||
body,
|
||||
bearer_token
|
||||
body
|
||||
),
|
||||
async,
|
||||
block
|
||||
)
|
||||
|
||||
@ractor.send(context)
|
||||
@requests << request
|
||||
|
||||
|
||||
if async
|
||||
request_id
|
||||
end
|
||||
else # Not async, process immediately.
|
||||
raise "WTF? This should NOT happen!" unless Async::Task.current?
|
||||
|
||||
def monitor
|
||||
raise "Something has gone quite wrong!!!" unless Ractor.main?
|
||||
Sync do |task|
|
||||
assigned_request = request
|
||||
result = @http_client.handle(task, assigned_request)
|
||||
|
||||
# Thread that spends its days sleeping **yawn**
|
||||
Thread.new do
|
||||
while (event = @ractor.take)
|
||||
pp event
|
||||
|
||||
next unless event.is_a?(NetworkEvent)
|
||||
|
||||
request = @request.find { |r| r.context.request_id == event.context.request_id }
|
||||
|
||||
next if request
|
||||
|
||||
@requests.delete(request)
|
||||
result = event.result
|
||||
|
||||
Store.main_thread_queue << ->(result) { request.callback(result) }
|
||||
@requests.delete(assigned_request)
|
||||
# "return" callback "value"
|
||||
assigned_request.callback.call(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def busy?
|
||||
@requests.any?(&:active)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
class W3DHub
|
||||
class NetworkManager
|
||||
# Api reimplemented in a Ractor friendly manner
|
||||
class ApiClient
|
||||
def initialize
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
57
lib/network_manager/http_client.rb
Normal file
57
lib/network_manager/http_client.rb
Normal file
@@ -0,0 +1,57 @@
|
||||
class W3DHub
|
||||
class NetworkManager
|
||||
# non-blocking, http requests.
|
||||
class HttpClient
|
||||
def initialize
|
||||
@http_clients = {}
|
||||
end
|
||||
|
||||
def handle(task, request)
|
||||
result = CyberarmEngine::Result.new
|
||||
context = request.context
|
||||
|
||||
task.with_timeout(W3DHub::Api::API_TIMEOUT) do
|
||||
uri = URI(context.url)
|
||||
|
||||
response = provision_http_client(uri.origin).send(
|
||||
context.method,
|
||||
uri.request_uri,
|
||||
context.headers,
|
||||
context.body
|
||||
)
|
||||
|
||||
if response.success?
|
||||
result.data = response.read
|
||||
else
|
||||
result.error = response
|
||||
end
|
||||
rescue Async::TimeoutError => e
|
||||
result.error = e
|
||||
rescue StandardError => e
|
||||
result.error = e
|
||||
ensure
|
||||
response&.close
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def provision_http_client(hostname)
|
||||
return @http_clients[hostname.downcase] if @http_clients[hostname.downcase]
|
||||
|
||||
ssl_context = W3DHub.ca_bundle_path ? OpenSSL::SSL::SSLContext.new : nil
|
||||
ssl_context&.set_params(
|
||||
ca_file: W3DHub.ca_bundle_path,
|
||||
verify_mode: OpenSSL::SSL::VERIFY_PEER
|
||||
)
|
||||
|
||||
endpoint = Async::HTTP::Endpoint.parse(hostname, ssl_context: ssl_context)
|
||||
@http_clients[hostname.downcase] = Async::HTTP::Client.new(endpoint)
|
||||
end
|
||||
|
||||
def wrapped_error(error)
|
||||
WrappedError.new(error.class, error.message.to_s, error.backtrace)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -65,15 +65,7 @@ class W3DHub
|
||||
para I18n.t(:"games.fetching_news"), padding: 8
|
||||
end
|
||||
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { fetch_w3dhub_news },
|
||||
lambda do |result|
|
||||
if result
|
||||
populate_w3dhub_news
|
||||
Cache.release_net_lock(result)
|
||||
end
|
||||
end
|
||||
)
|
||||
fetch_w3dhub_news
|
||||
end
|
||||
end
|
||||
|
||||
@@ -89,15 +81,7 @@ class W3DHub
|
||||
title I18n.t(:"games.fetching_news"), padding: 8
|
||||
end
|
||||
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { fetch_w3dhub_news },
|
||||
lambda do |result|
|
||||
if result
|
||||
populate_w3dhub_news
|
||||
Cache.release_net_lock(result)
|
||||
end
|
||||
end
|
||||
)
|
||||
fetch_w3dhub_news
|
||||
end
|
||||
end
|
||||
|
||||
@@ -105,19 +89,24 @@ class W3DHub
|
||||
lock = Cache.acquire_net_lock("w3dhub_news")
|
||||
return false unless lock
|
||||
|
||||
news = Api.news("launcher-home")
|
||||
Api.news("launcher-home") do |result|
|
||||
news = result.data
|
||||
|
||||
Cache.release_net_lock("w3dhub_news") unless news
|
||||
|
||||
return unless news
|
||||
next false unless news
|
||||
|
||||
news.items[0..15].each do |item|
|
||||
Cache.fetch(uri: item.image, async: false, backend: :w3dhub)
|
||||
Cache.fetch(uri: item.image, backend: :w3dhub)
|
||||
end
|
||||
|
||||
@w3dhub_news = news
|
||||
@w3dhub_news_expires = Gosu.milliseconds + (60 * 60 * 1000) # 1 hour (in ms)
|
||||
|
||||
"w3dhub_news"
|
||||
populate_w3dhub_news
|
||||
ensure
|
||||
Cache.release_net_lock("w3dhub_news")
|
||||
end
|
||||
end
|
||||
|
||||
def populate_w3dhub_news
|
||||
@@ -125,37 +114,7 @@ class W3DHub
|
||||
|
||||
if (feed = @w3dhub_news)
|
||||
@wd3hub_news_container.clear do
|
||||
# feed.items.sort_by { |i| i.timestamp }.reverse[0..9].each do |item|
|
||||
# flow(width: 0.5, max_width: 312, height: 128, margin: 4) do
|
||||
# # background 0x88_000000
|
||||
|
||||
# path = Cache.path(item.image)
|
||||
|
||||
# if File.exist?(path)
|
||||
# image path, height: 1.0, padding: 4
|
||||
# else
|
||||
# image BLACK_IMAGE, height: 1.0, padding: 4
|
||||
# end
|
||||
|
||||
# stack(width: 0.6, height: 1.0) do
|
||||
# stack(width: 1.0, height: 112) do
|
||||
# link "<b>#{item.title}</b>", text_size: 22 do
|
||||
# W3DHub.url(item.uri)
|
||||
# end
|
||||
# para item.blurb.gsub(/\n+/, "\n").strip[0..180]
|
||||
# end
|
||||
|
||||
# flow(width: 1.0) do
|
||||
# para item.timestamp.strftime("%Y-%m-%d"), width: 0.499
|
||||
# link I18n.t(:"games.read_more"), width: 0.5, text_align: :right, text_size: 22 do
|
||||
# W3DHub.url(item.uri)
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
|
||||
feed.items.sort_by { |i| i.timestamp }.reverse[0..9].each do |item|
|
||||
feed.items.sort_by(&:timestamp).reverse[0..9].each do |item|
|
||||
image_path = Cache.path(item.image)
|
||||
|
||||
flow(width: 1.0, max_width: 1230, height: 200, margin: 8, border_thickness: 1, border_color: lighten(Gosu::Color.new(0xff_252525))) do
|
||||
|
||||
@@ -31,30 +31,16 @@ class W3DHub
|
||||
def update
|
||||
super
|
||||
|
||||
|
||||
@game_news.each do |key, value|
|
||||
next if key.end_with?("_expires")
|
||||
next unless key.end_with?("_expires")
|
||||
|
||||
if Gosu.milliseconds >= @game_news["#{key}_expires"]
|
||||
@game_news.delete(key)
|
||||
@game_news["#{key}_expires"] = Gosu.milliseconds + 30_000 # seconds
|
||||
next unless Gosu.milliseconds >= value
|
||||
|
||||
if @focused_game && @focused_game.id == key
|
||||
@game_news_container.clear do
|
||||
title I18n.t(:"games.fetching_news"), padding: 8
|
||||
end
|
||||
# try to refresh game news after last data 'expired', every 30 seconds until success
|
||||
@game_news[key] = Gosu.milliseconds + 30_000 # seconds
|
||||
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { fetch_game_news(@focused_game) },
|
||||
lambda do |result|
|
||||
if result
|
||||
populate_game_news(@focused_game)
|
||||
Cache.release_net_lock(result)
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
game = Store.applications.games.find { |g| g.id == key.split("_").first }
|
||||
fetch_game_news(game)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -292,22 +278,6 @@ class W3DHub
|
||||
return if Store.offline_mode
|
||||
|
||||
unless Cache.net_lock?("game_news_#{game.id}")
|
||||
if @game_events[game.id]
|
||||
populate_game_events(game)
|
||||
else
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { fetch_game_events(game) },
|
||||
lambda do |result|
|
||||
if result
|
||||
populate_game_events(game)
|
||||
Cache.release_net_lock(result)
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
unless Cache.net_lock?("game_events_#{game.id}")
|
||||
if @game_news[game.id]
|
||||
populate_game_news(game)
|
||||
else
|
||||
@@ -315,15 +285,15 @@ class W3DHub
|
||||
title I18n.t(:"games.fetching_news"), padding: 8
|
||||
end
|
||||
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { fetch_game_news(game) },
|
||||
lambda do |result|
|
||||
if result
|
||||
populate_game_news(game)
|
||||
Cache.release_net_lock(result)
|
||||
fetch_game_news(game)
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
unless Cache.net_lock?("game_events_#{game.id}")
|
||||
if @game_events[game.id]
|
||||
populate_game_events(game)
|
||||
else
|
||||
fetch_game_events(game)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -413,19 +383,25 @@ class W3DHub
|
||||
lock = Cache.acquire_net_lock("game_news_#{game.id}")
|
||||
return false unless lock
|
||||
|
||||
news = Api.news(game.id)
|
||||
Cache.release_net_lock("game_news_#{game.id}") unless news
|
||||
Api.news(game.id) do |result|
|
||||
news = result.data
|
||||
|
||||
return false unless news
|
||||
unless news
|
||||
@game_news["#{game.id}_expires"] = Gosu.milliseconds + 30_000 # retry in 30 seconds
|
||||
next false
|
||||
end
|
||||
|
||||
news.items[0..15].each do |item|
|
||||
Cache.fetch(uri: item.image, async: false, backend: :w3dhub)
|
||||
Cache.fetch(uri: item.image, backend: :w3dhub)
|
||||
end
|
||||
|
||||
@game_news[game.id] = news
|
||||
@game_news["#{game.id}_expires"] = Gosu.milliseconds + (60 * 60 * 1000) # 1 hour (in ms)
|
||||
|
||||
"game_news_#{game.id}"
|
||||
populate_game_news(@focused_game)
|
||||
ensure
|
||||
Cache.release_net_lock("game_news_#{game.id}")
|
||||
end
|
||||
end
|
||||
|
||||
def populate_game_news(game)
|
||||
@@ -436,38 +412,11 @@ class W3DHub
|
||||
game_color.alpha = 0xaa
|
||||
|
||||
@game_news_container.clear do
|
||||
# Patch Notes
|
||||
if false # Patch notes
|
||||
flow(width: 1.0, max_width: 346 * 3 + (8 * 4), height: 346, margin: 8, margin_right: 32, border_thickness: 1, border_color: darken(Gosu::Color.new(game.color))) do
|
||||
background darken(Gosu::Color.new(game.color), 10)
|
||||
|
||||
stack(width: 346, height: 1.0, padding: 8) do
|
||||
background 0xff_181d22
|
||||
|
||||
para "Patch Notes"
|
||||
|
||||
tagline "<b>Patch 2.0 is now out!</b>"
|
||||
|
||||
para "words go here " * 20
|
||||
|
||||
flow(fill: true)
|
||||
|
||||
button "Read More", width: 1.0
|
||||
end
|
||||
|
||||
flow(fill: true)
|
||||
|
||||
title "Eye Candy Banner Goes Here."
|
||||
end
|
||||
end
|
||||
|
||||
feed.items.sort_by { |i| i.timestamp }.reverse[0..9].each do |item|
|
||||
feed.items.sort_by(&:timestamp).reverse[0..9].each do |item|
|
||||
image_path = Cache.path(item.image)
|
||||
|
||||
flow(width: 1.0, max_width: 869, height: 200, margin: 8, background: game_color, border_thickness: 1, border_color: lighten(Gosu::Color.new(game.color))) do
|
||||
if File.file?(image_path)
|
||||
image image_path, height: 1.0
|
||||
end
|
||||
image image_path, height: 1.0 if File.file?(image_path)
|
||||
|
||||
stack(fill: true, height: 1.0, padding: 4, border_thickness_left: 1, border_color_left: lighten(Gosu::Color.new(game.color))) do
|
||||
tagline "<b>#{item.title}</b>", width: 1.0
|
||||
@@ -494,14 +443,14 @@ class W3DHub
|
||||
lock = Cache.acquire_net_lock("game_events_#{game.id}")
|
||||
return false unless lock
|
||||
|
||||
events = Api.events(game.id)
|
||||
Cache.release_net_lock("game_events_#{game.id}") unless events
|
||||
Api.events(game.id) do |result|
|
||||
next unless result.okay?
|
||||
|
||||
return false unless events
|
||||
|
||||
@game_events[game.id] = events
|
||||
|
||||
"game_events_#{game.id}"
|
||||
@game_events[game.id] = result.data
|
||||
populate_game_events(game)
|
||||
ensure
|
||||
Cache.release_net_lock("game_events_#{game.id}")
|
||||
end
|
||||
end
|
||||
|
||||
def populate_game_events(game)
|
||||
|
||||
@@ -34,9 +34,8 @@ class W3DHub
|
||||
|
||||
# Do network stuff
|
||||
|
||||
BackgroundWorker.foreground_job(
|
||||
lambda do
|
||||
account = Api.user_login(@username.value, @password.value)
|
||||
Api.user_login(@username.value, @password.value) do |result|
|
||||
if result.okay?
|
||||
applications = nil
|
||||
|
||||
if account
|
||||
@@ -45,14 +44,15 @@ class W3DHub
|
||||
Store.settings.save_settings
|
||||
|
||||
if account
|
||||
Cache.fetch(uri: account.avatar_uri, force_fetch: true, async: false, backend: :w3dhub)
|
||||
applications = Api._applications
|
||||
Cache.fetch(uri: account.avatar_uri, force_fetch: true, backend: :w3dhub) {}
|
||||
Api._applications do |r|
|
||||
applications = r.result if r.okay?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
[account, applications]
|
||||
end,
|
||||
lambda do |result|
|
||||
else
|
||||
account, applications = result
|
||||
|
||||
if account
|
||||
@@ -70,7 +70,7 @@ class W3DHub
|
||||
@error_label.value = "Incorrect username or password.\nOr too many failed login attempts, try again in a few minutes."
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@error_label = caption "", width: 1.0, text_align: :center, color: 0xff_800000
|
||||
@@ -80,13 +80,10 @@ class W3DHub
|
||||
end
|
||||
|
||||
if Store.account
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { Cache.fetch(uri: Store.account.avatar_uri, async: false, backend: :w3dhub) },
|
||||
->(result) {
|
||||
Cache.fetch(uri: Store.account.avatar_uri, backend: :w3dhub) do |result|
|
||||
populate_account_info
|
||||
page(W3DHub::Pages::Games)
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -153,11 +150,9 @@ class W3DHub
|
||||
Store.settings.save_settings
|
||||
Store.account = nil
|
||||
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { Api._applications },
|
||||
lambda do |applications|
|
||||
if applications
|
||||
Store.applications = applications
|
||||
Api._applications do |result|
|
||||
if result.okay?
|
||||
Store.applications = result.data
|
||||
page(W3DHub::Pages::Games) if @host.current_page.is_a?(W3DHub::Pages::Games)
|
||||
page(W3DHub::Pages::ServerBrowser) if @host.current_page.is_a?(W3DHub::Pages::ServerBrowser)
|
||||
end
|
||||
@@ -175,7 +170,6 @@ class W3DHub
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -145,15 +145,7 @@ class W3DHub
|
||||
reorder_server_list
|
||||
|
||||
if @selected_server&.id == @refresh_server&.id
|
||||
if @refresh_server
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { fetch_server_details(@refresh_server) },
|
||||
->(result) {
|
||||
populate_server_info(@refresh_server) if @refresh_server == @selected_server
|
||||
@refresh_server = nil
|
||||
}
|
||||
)
|
||||
end
|
||||
fetch_server_details(@refresh_server) if @refresh_server
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -353,10 +345,7 @@ class W3DHub
|
||||
|
||||
reorder_server_list if @selected_server_container
|
||||
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { fetch_server_details(server) },
|
||||
->(result) { populate_server_info(server) if server == @selected_server }
|
||||
)
|
||||
fetch_server_details(server)
|
||||
end
|
||||
|
||||
stylize_selected_server(server_container) if server.id == @selected_server&.id
|
||||
@@ -523,10 +512,13 @@ class W3DHub
|
||||
end
|
||||
|
||||
def fetch_server_details(server)
|
||||
BackgroundWorker.foreground_job(
|
||||
-> { Api.server_details(server.id, 2) },
|
||||
->(server_data) { server.update(server_data) if server_data }
|
||||
)
|
||||
Api.server_details(server.id, 2) do |result|
|
||||
if result.okay?
|
||||
server.update(result.data)
|
||||
populate_server_info(server) if server == @selected_server
|
||||
@refresh_server = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def game_icon(server)
|
||||
|
||||
@@ -115,9 +115,8 @@ class W3DHub
|
||||
Api.on_thread(:refresh_user_login, account.refresh_token) do |refreshed_account|
|
||||
update_account_data(refreshed_account)
|
||||
end
|
||||
|
||||
else
|
||||
BackgroundWorker.foreground_job(-> { update_account_data(account) }, ->(_) {})
|
||||
Store.main_thread_queue << -> { update_account_data(account) }
|
||||
end
|
||||
|
||||
else
|
||||
@@ -131,7 +130,7 @@ class W3DHub
|
||||
|
||||
Store.settings[:account][:data] = account
|
||||
|
||||
Cache.fetch(uri: account.avatar_uri, force_fetch: true, async: false, backend: :w3dhub)
|
||||
Cache.fetch(uri: account.avatar_uri, force_fetch: true, backend: :w3dhub)
|
||||
else
|
||||
Store.settings[:account] = {}
|
||||
end
|
||||
@@ -175,8 +174,10 @@ class W3DHub
|
||||
end
|
||||
|
||||
def service_status
|
||||
Api.on_thread(:service_status) do |service_status|
|
||||
@service_status = service_status
|
||||
@status_label.value = "Checking service status..." #I18n.t(:"server_browser.fetching_server_list")
|
||||
|
||||
Api.on_thread(:service_status) do |result|
|
||||
@service_status = result.okay? ? result.data : nil
|
||||
|
||||
if @service_status
|
||||
Store.service_status = @service_status
|
||||
@@ -187,9 +188,7 @@ class W3DHub
|
||||
|
||||
@tasks[:service_status][:complete] = true
|
||||
else
|
||||
BackgroundWorker.foreground_job(-> {}, lambda { |_|
|
||||
@status_label.value = I18n.t(:"boot.w3dhub_service_is_down")
|
||||
})
|
||||
Store.main_thread_queue << -> { @status_label.value = I18n.t(:"boot.w3dhub_service_is_down") }
|
||||
@tasks[:service_status][:complete] = true
|
||||
|
||||
@offline_mode = true
|
||||
@@ -201,9 +200,9 @@ class W3DHub
|
||||
def launcher_updater
|
||||
@status_label.value = "Checking for Launcher updates..." # I18n.t(:"boot.checking_for_updates")
|
||||
|
||||
Api.on_thread(:fetch, "https://api.github.com/repos/cyberarm/w3d_hub_linux_launcher/releases/latest") do |response|
|
||||
if response.status == 200
|
||||
hash = JSON.parse(response.body, symbolize_names: true)
|
||||
Api.on_thread(:fetch, "https://api.github.com/repos/cyberarm/w3d_hub_linux_launcher/releases/latest") do |result|
|
||||
if result.okay?
|
||||
hash = JSON.parse(result.data, symbolize_names: true)
|
||||
available_version = hash[:tag_name].downcase.sub("v", "")
|
||||
|
||||
pp Gem::Version.new(available_version) > Gem::Version.new(W3DHub::VERSION)
|
||||
@@ -227,14 +226,13 @@ class W3DHub
|
||||
def applications
|
||||
@status_label.value = I18n.t(:"boot.checking_for_updates")
|
||||
|
||||
Api.on_thread(:_applications) do |applications|
|
||||
if applications
|
||||
Store.applications = applications
|
||||
Store.settings.save_application_cache(applications.data.to_json)
|
||||
Api.on_thread(:_applications) do |result|
|
||||
if result.okay?
|
||||
Store.applications = result.data
|
||||
Store.settings.save_application_cache(Store.applications.data.to_json)
|
||||
@tasks[:applications][:complete] = true
|
||||
else
|
||||
# FIXME: Failed to retreive!
|
||||
BackgroundWorker.foreground_job(-> {}, ->(_) { @status_label.value = "FAILED TO RETREIVE APPS LIST" })
|
||||
@status_label.value = "FAILED TO RETRIEVE APPS LIST"
|
||||
|
||||
@offline_mode = true
|
||||
Store.offline_mode = true
|
||||
@@ -253,10 +251,9 @@ class W3DHub
|
||||
packages << { category: app.category, subcategory: app.id, name: "#{app.id}.ico", version: "" }
|
||||
end
|
||||
|
||||
Api.on_thread(:package_details, packages, :alt_w3dhub) do |package_details|
|
||||
package_details ||= nil
|
||||
|
||||
package_details&.each do |package|
|
||||
Api.on_thread(:package_details, packages, :alt_w3dhub) do |result|
|
||||
if result.okay?
|
||||
result.data.each do |package|
|
||||
next if package.error?
|
||||
|
||||
path = Cache.package_path(package.category, package.subcategory, package.name, package.version)
|
||||
@@ -284,9 +281,9 @@ class W3DHub
|
||||
|
||||
next unless regenerate
|
||||
|
||||
BackgroundWorker.foreground_job(-> { ICO.new(file: path) }, lambda { |result|
|
||||
result.save(result.images.max_by(&:width), generated_icon_path)
|
||||
})
|
||||
icon = ICO.new(file: path)
|
||||
icon.save(icon.images.max_by(&:width), generated_icon_path)
|
||||
end
|
||||
end
|
||||
|
||||
@tasks[:app_icons][:complete] = true unless failure
|
||||
@@ -304,10 +301,9 @@ class W3DHub
|
||||
packages << { category: app.category, subcategory: app.id, name: "background.png", version: "" }
|
||||
end
|
||||
|
||||
Api.on_thread(:package_details, packages, :alt_w3dhub) do |package_details|
|
||||
package_details ||= nil
|
||||
|
||||
package_details&.each do |package|
|
||||
Api.on_thread(:package_details, packages, :alt_w3dhub) do |result|
|
||||
if result.okay?
|
||||
result.data.each do |package|
|
||||
next if package.error?
|
||||
|
||||
package_cache_path = Cache.package_path(package.category, package.subcategory, package.name,
|
||||
@@ -317,6 +313,7 @@ class W3DHub
|
||||
|
||||
Cache.fetch_package(package, proc {}) if missing_or_broken_image
|
||||
end
|
||||
end
|
||||
|
||||
@tasks[:app_logos_and_backgrounds][:complete] = true
|
||||
end
|
||||
@@ -325,15 +322,15 @@ class W3DHub
|
||||
def server_list
|
||||
@status_label.value = I18n.t(:"server_browser.fetching_server_list")
|
||||
|
||||
Api.on_thread(:server_list, 2) do |list|
|
||||
if list
|
||||
Store.server_list = list.sort_by! { |s| s&.status&.players&.size }.reverse
|
||||
Api.on_thread(:server_list, 2) do |result|
|
||||
if result.okay?
|
||||
Store.server_list = result.data.sort_by! { |s| s&.status&.players&.size }.reverse
|
||||
|
||||
Store.server_list_last_fetch = Gosu.milliseconds
|
||||
|
||||
Api::ServerListUpdater.instance
|
||||
|
||||
list.each do |server|
|
||||
Store.server_list.each do |server|
|
||||
server.send_ping(true)
|
||||
end
|
||||
else
|
||||
|
||||
@@ -164,15 +164,15 @@ class W3DHub
|
||||
if Gosu.milliseconds >= @server_list_expire
|
||||
@server_list_expire = Gosu.milliseconds + 30_000
|
||||
|
||||
Api.on_thread(:server_list, 2) do |list|
|
||||
if list
|
||||
Api.on_thread(:server_list, 2) do |result|
|
||||
if result.okay?
|
||||
@server_list_expire = Gosu.milliseconds + SERVER_LIST_UPDATE_INTERVAL # five minutes
|
||||
|
||||
Store.server_list_last_fetch = Gosu.milliseconds
|
||||
|
||||
Api::ServerListUpdater.instance.refresh_server_list(list)
|
||||
Api::ServerListUpdater.instance.refresh_server_list(result.data)
|
||||
|
||||
BackgroundWorker.foreground_job(-> {}, ->(_) { States::Interface.instance&.update_server_browser(nil, :refresh_all) })
|
||||
Store.main_thread_queue << -> { States::Interface.instance&.update_server_browser(nil, :refresh_all) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class W3DHub
|
||||
DIR_NAME = "W3DHubAlt".freeze
|
||||
VERSION = "0.9.0".freeze
|
||||
VERSION = "0.9.1".freeze
|
||||
end
|
||||
|
||||
@@ -36,6 +36,8 @@ class W3DHub
|
||||
while (block = Store.main_thread_queue.shift)
|
||||
block&.call
|
||||
end
|
||||
|
||||
sleep(update_interval / 1000.0) if Store.application_manager.busy? || Store.network_manager.busy?
|
||||
end
|
||||
|
||||
def needs_redraw?
|
||||
|
||||
@@ -228,27 +228,34 @@ class W3DHub
|
||||
@encrypted
|
||||
end
|
||||
|
||||
def add_file(path:)
|
||||
def add_file(path:, replace: false)
|
||||
return false unless File.exist?(path)
|
||||
return false if File.directory?(path)
|
||||
|
||||
info = EntryInfoHeader.new(0, 0, File.size(path))
|
||||
@entries << Entry.new(name: File.basename(path), path: path, info: info)
|
||||
|
||||
true
|
||||
entry = Entry.new(name: File.basename(path), path: path, info: EntryInfoHeader.new(0, 0, File.size(path)))
|
||||
add_entry(entry: entry, replace: replace)
|
||||
end
|
||||
|
||||
def add_blob(path:, blob:)
|
||||
def add_blob(path:, blob:, replace: false)
|
||||
info = EntryInfoHeader.new(0, 0, blob.size)
|
||||
@entries << Entry.new(name: File.basename(path), path: path, info: info, blob: blob)
|
||||
entry = Entry.new(name: File.basename(path), path: path, info: info, blob: blob)
|
||||
into.crc32 = @entries.last.calculate_crc32
|
||||
|
||||
true
|
||||
add_entry(entry: entry, replace: replace)
|
||||
end
|
||||
|
||||
def add_entry(entry:)
|
||||
@entries << entry
|
||||
def add_entry(entry:, replace: false)
|
||||
duplicate = @entries.find { |e| e.name.upcase == entry.name.upcase }
|
||||
|
||||
if duplicate
|
||||
if replace
|
||||
@entries.delete(duplicate)
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
@entries << entry
|
||||
true
|
||||
end
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ require_relative "lib/hardware_survey"
|
||||
require_relative "lib/game_settings"
|
||||
require_relative "lib/websocket_client"
|
||||
require_relative "lib/network_manager"
|
||||
require_relative "lib/network_manager/api_client"
|
||||
require_relative "lib/network_manager/http_client"
|
||||
require_relative "lib/application_manager"
|
||||
require_relative "lib/application_manager/manifest"
|
||||
require_relative "lib/application_manager/status"
|
||||
|
||||
Reference in New Issue
Block a user