Compare commits

...

2 Commits

14 changed files with 320 additions and 212 deletions

View File

@@ -9,6 +9,10 @@ class W3DHub
DEFAULT_HEADERS + [["Content-Type", "application/x-www-form-urlencoded"]]
).freeze
def self.on_fiber(method, *args, &callback)
BackgroundWorker.job(-> { Api.send(method, *args) }, callback)
end
#! === W3D Hub API === !#
ENDPOINT = "https://secure.w3dhub.com".freeze
@@ -165,6 +169,12 @@ class W3DHub
SERVER_LIST_ENDPOINT = "https://gsh.w3dhub.com".freeze
def self.get(url, headers = DEFAULT_HEADERS, body = nil)
@client ||= Async::HTTP::Client.new(Async::HTTP::Endpoint.parse(SERVER_LIST_ENDPOINT, protocol: Async::HTTP::Protocol::HTTP10))
@client.get(url, headers, body)
end
# Method: GET
# FORMAT: JSON
@@ -182,8 +192,8 @@ class W3DHub
# id, name, score, kills, deaths
# ...players[]:
# nick, team (index of teams array), score, kills, deaths
def self.server_list(internet, level = 1)
response = internet.get("#{SERVER_LIST_ENDPOINT}/listings/getAll/v2?statusLevel=#{level}", DEFAULT_HEADERS)
def self.server_list(level = 1)
response = get("#{SERVER_LIST_ENDPOINT}/listings/getAll/v2?statusLevel=#{level}")
if response.success?
data = JSON.parse(response.read, symbolize_names: true)
@@ -204,8 +214,8 @@ class W3DHub
# id, name, score, kills, deaths
# ...players[]:
# nick, team (index of teams array), score, kills, deaths
def self.server_details(internet, id, level)
response = internet.get("#{SERVER_LIST_ENDPOINT}/listings/getStatus/v2/#{id}?statusLevel=#{level}", DEFAULT_HEADERS)
def self.server_details(id, level)
response = get("#{SERVER_LIST_ENDPOINT}/listings/getStatus/v2/#{id}?statusLevel=#{level}")
if response.success?
hash = JSON.parse(response.read, symbolize_names: true)

View File

@@ -74,45 +74,47 @@ class W3DHub
end
def run
Async do |task|
internet = Async::HTTP::Internet.instance
Thread.new do
Async do |task|
internet = Async::HTTP::Internet.instance
response = internet.post("https://gsh.w3dhub.com/listings/push/v2/negotiate?negotiateVersion=1", Api::DEFAULT_HEADERS, [""])
data = JSON.parse(response.read, symbolize_names: true)
response = internet.post("https://gsh.w3dhub.com/listings/push/v2/negotiate?negotiateVersion=1", Api::DEFAULT_HEADERS, [""])
data = JSON.parse(response.read, symbolize_names: true)
id = data[:connectionToken]
endpoint = Async::HTTP::Endpoint.parse("https://gsh.w3dhub.com/listings/push/v2?id=#{id}", alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
id = data[:connectionToken]
endpoint = Async::HTTP::Endpoint.parse("https://gsh.w3dhub.com/listings/push/v2?id=#{id}", alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
Async::WebSocket::Client.connect(endpoint, headers: Api::DEFAULT_HEADERS, handler: PatchedConnection) do |connection|
connection.write({ protocol: "json", version: 1 })
connection.flush
pp connection.read
connection.write({ "type": 6 })
Async::WebSocket::Client.connect(endpoint, headers: Api::DEFAULT_HEADERS, handler: PatchedConnection) do |connection|
connection.write({ protocol: "json", version: 1 })
connection.flush
pp connection.read
connection.write({ "type": 6 })
Store.server_list.each_with_index do |server, i|
i += 1
mode = 1 # 2 full details, 1 basic details
out = { "type": 1, "invocationId": "#{i}", "target": "SubscribeToServerStatusUpdates", "arguments": [server.id, mode] }
connection.write(out)
end
Store.server_list.each_with_index do |server, i|
i += 1
mode = 1 # 2 full details, 1 basic details
out = { "type": 1, "invocationId": "#{i}", "target": "SubscribeToServerStatusUpdates", "arguments": [server.id, mode] }
connection.write(out)
end
while (message = connection.read)
connection.write({ type: 6 }) if message.first[:type] == 6
while (message = connection.read)
connection.write({ type: 6 }) if message.first[:type] == 6
if message&.first&.fetch(:type) == 1
message.each do |rpc|
next unless rpc[:target] == "ServerStatusChanged"
if message&.first&.fetch(:type) == 1
message.each do |rpc|
next unless rpc[:target] == "ServerStatusChanged"
id, data = rpc[:arguments]
server = Store.server_list.find { |s| s.id == id }
server_updated = server&.update(data)
States::Interface.instance&.update_server_browser(server) if server_updated
id, data = rpc[:arguments]
server = Store.server_list.find { |s| s.id == id }
server_updated = server&.update(data)
States::Interface.instance&.update_server_browser(server) if server_updated
end
end
end
end
ensure
@@instance = nil
end
ensure
@@instance = nil
end
end
end

View File

@@ -41,6 +41,10 @@ class W3DHub
@task_state
end
def log(string)
log string if W3DHUB_DEBUG
end
# Start task, inside its own thread
# FIXME: Ruby 3 has parallelism now: Use a Ractor to do work on a seperate core to
# prevent the UI from locking up while doing computation heavy work, i.e building
@@ -201,7 +205,7 @@ class W3DHub
packages = []
manifests.reverse.each do |manifest|
puts "#{manifest.game}-#{manifest.type}: #{manifest.version} (#{manifest.base_version})"
log "#{manifest.game}-#{manifest.type}: #{manifest.version} (#{manifest.base_version})"
manifest.files.each do |file|
@files["#{file.name}:#{manifest.version}"] = file
@@ -250,7 +254,11 @@ class W3DHub
file_count = manifests.map { |m| m.files.count }.sum
processed_files = 0
folder_exists = File.directory?(path)
manifests.each do |manifest|
break unless folder_exists
manifest.files.each do |file|
safe_file_name = file.name.gsub("\\", "/")
# Fix borked data -> Data 'cause Windows don't care about capitalization
@@ -266,7 +274,7 @@ class W3DHub
unless File.exist?(file_path)
rejected_files << { file: file, manifest_version: manifest.version }
puts "[#{manifest.version}] File missing: #{file_path}"
log "[#{manifest.version}] File missing: #{file_path}"
next
end
@@ -279,21 +287,20 @@ class W3DHub
f.close
pp file if file.checksum.nil?
log file.inspect if file.checksum.nil?
if digest.hexdigest.upcase == file.checksum.upcase
accepted_files[safe_file_name] = manifest.version
# puts "[#{manifest.version}] Verified file: #{file_path}"
log "[#{manifest.version}] Verified file: #{file_path}"
else
rejected_files << { file: file, manifest_version: manifest.version }
puts "[#{manifest.version}] File failed Verification: #{file_path}"
log "[#{manifest.version}] File failed Verification: #{file_path}"
end
end
end
puts "#{rejected_files.count} missing or corrupt files"
log "#{rejected_files.count} missing or corrupt files"
# TODO: Filter packages to only the required ones
selected_packages = []
selected_packages_hash = {}
@@ -310,10 +317,8 @@ class W3DHub
end
end
# FIXME: Order `selected_packages` like `packages`
# Removed packages that don't need to be fetched or processed
packages.delete_if { |package| !selected_packages.find { |pkg| pkg == package } }
packages.delete_if { |package| !selected_packages.find { |pkg| pkg == package } } if folder_exists
packages
end
@@ -421,7 +426,7 @@ class W3DHub
def unpack_packages(packages)
path = Cache.install_path(@application, @channel)
puts "Unpacking packages in '#{path}'..."
log "Unpacking packages in '#{path}'..."
Cache.create_directories(path, true)
@status.operations.clear
@@ -468,7 +473,7 @@ class W3DHub
update_interface_task_status
else
puts "COMMAND FAILED!"
log "COMMAND FAILED!"
fail!("Failed to unpack #{package.name}")
break
@@ -508,7 +513,7 @@ class W3DHub
@status.step = :mark_application_installed
puts "#{@app_id} has been installed."
log "#{@app_id} has been installed."
end
#############
@@ -540,7 +545,7 @@ class W3DHub
end
def package_fetch(package, &block)
puts "Downloading: #{package.category}:#{package.subcategory}:#{package.name}-#{package.version}"
log "Downloading: #{package.category}:#{package.subcategory}:#{package.name}-#{package.version}"
Api.package(package) do |chunk, remaining_bytes, total_bytes|
block&.call(chunk, remaining_bytes, total_bytes)
@@ -548,7 +553,7 @@ class W3DHub
end
def verify_package(package, &block)
puts "Verifying: #{package.category}:#{package.subcategory}:#{package.name}-#{package.version}"
log "Verifying: #{package.category}:#{package.subcategory}:#{package.name}-#{package.version}"
digest = Digest::SHA256.new
path = Cache.package_path(package.category, package.subcategory, package.name, package.version)
@@ -559,7 +564,7 @@ class W3DHub
operation&.value = "Verifying..."
file_size = File.size(path)
puts " File size: #{file_size}"
log " File size: #{file_size}"
chunk_size = package.checksum_chunk_size
chunks = package.checksum_chunks.size
@@ -584,11 +589,11 @@ class W3DHub
if Digest::SHA256.new.hexdigest(chunk).upcase == checksum.upcase
valid_at = chunk_start + read_length
# puts " Passed chunk: #{chunk_start}"
# log " Passed chunk: #{chunk_start}"
# package.partially_valid_at_bytes = valid_at
package.partially_valid_at_bytes = chunk_start
else
puts " FAILED chunk: #{chunk_start}"
log " FAILED chunk: #{chunk_start}"
break
end
end
@@ -602,25 +607,25 @@ class W3DHub
end
def unpack_package(package, path)
puts " #{package.name}:#{package.version}"
log " #{package.name}:#{package.version}"
package_path = Cache.package_path(package.category, package.subcategory, package.name, package.version)
puts " Running #{W3DHub.tar_command} command: #{W3DHub.tar_command} -xf \"#{package_path}\" -C \"#{path}\""
log " Running #{W3DHub.tar_command} command: #{W3DHub.tar_command} -xf \"#{package_path}\" -C \"#{path}\""
return system("#{W3DHub.tar_command} -xf \"#{package_path}\" -C \"#{path}\"")
end
def apply_patch(package, path)
puts " #{package.name}:#{package.version}"
log " #{package.name}:#{package.version}"
package_path = Cache.package_path(package.category, package.subcategory, package.name, package.version)
temp_path = "#{Store.settings[:package_cache_dir]}/temp"
manifest_file = package.custom_is_patch
Cache.create_directories(temp_path, true)
puts " Running #{W3DHub.tar_command} command: #{W3DHub.tar_command} -xf \"#{package_path}\" -C \"#{temp_path}\""
log " Running #{W3DHub.tar_command} command: #{W3DHub.tar_command} -xf \"#{package_path}\" -C \"#{temp_path}\""
system("#{W3DHub.tar_command} -xf \"#{package_path}\" -C \"#{temp_path}\"")
puts " Loading #{temp_path}/#{manifest_file.name}.patch..."
log " Loading #{temp_path}/#{manifest_file.name}.patch..."
patch_mix = W3DHub::Mixer::Reader.new(file_path: "#{temp_path}/#{manifest_file.name}.patch", ignore_crc_mismatches: false)
patch_info = JSON.parse(patch_mix.package.files.find { |f| f.name == ".w3dhub.patch" || f.name == ".bhppatch" }.data, symbolize_names: true)
@@ -628,15 +633,15 @@ class W3DHub
# Fix borked data -> Data 'cause Windows don't care about capitalization
repaired_path = "#{path}/#{manifest_file.name.sub('data', 'Data')}" unless File.exist?(repaired_path) && path
puts " Loading #{repaired_path}..."
log " Loading #{repaired_path}..."
target_mix = W3DHub::Mixer::Reader.new(file_path: repaired_path, ignore_crc_mismatches: false)
puts " Removing files..." if patch_info[:removedFiles].size.positive?
log " Removing files..." if patch_info[:removedFiles].size.positive?
patch_info[:removedFiles].each do |file|
target_mix.package.files.delete_if { |f| f.name == file }
end
puts " Adding/Updating files..." if patch_info[:updatedFiles].size.positive?
log " Adding/Updating files..." if patch_info[:updatedFiles].size.positive?
patch_info[:updatedFiles].each do |file|
patch = patch_mix.package.files.find { |f| f.name == file }
target = target_mix.package.files.find { |f| f.name == file }
@@ -649,7 +654,7 @@ class W3DHub
end
puts " Writing updated #{repaired_path}..." if patch_info[:updatedFiles].size.positive?
log " Writing updated #{repaired_path}..." if patch_info[:updatedFiles].size.positive?
W3DHub::Mixer::Writer.new(file_path: repaired_path, package: target_mix.package, memory_buffer: true)
FileUtils.remove_dir(temp_path)
@@ -666,7 +671,7 @@ class W3DHub
# Force data/ to Data/
return true unless File.exist?("#{path}/data") && File.directory?("#{path}/data")
puts " Moving #{path}/data/ to #{path}/Data/"
log " Moving #{path}/data/ to #{path}/Data/"
FileUtils.mv(Dir.glob("#{path}/data/**"), "#{path}/Data", force: true)
FileUtils.remove_dir("#{path}/data", force: true)

View File

@@ -33,9 +33,11 @@ class W3DHub
@status.value = "Purging installation folder..."
@status.progress = Float::INFINITY
@status.step = :uninstalling_application
path = Cache.install_path(@application, @channel)
puts path
log path
# TODO: Do some sanity checking, i.e. DO NOT start launcher if `whoami` returns root, path makes sense,
# we're not on Windows trying to uninstall a game likely installed by the official launcher
FileUtils.remove_dir(path)
@@ -51,7 +53,7 @@ class W3DHub
@status.step = :mark_application_uninstalled
puts "#{@app_id} has been uninstalled."
log "#{@app_id} has been uninstalled."
end
end
end

81
lib/background_worker.rb Normal file
View File

@@ -0,0 +1,81 @@
class W3DHub
class BackgroundWorker
@@instance = nil
@@alive = false
def self.create
raise "BackgroundWorker instance already exists!" if @@instance
@@alive = true
@@instance = self.new
Async do
@@instance.handle_jobs
end
end
def self.instance
@@instance
end
def self.alive?
@@alive
end
def self.shutdown!
@@alive = false
end
def self.job(job, callback)
@@instance.add_job(Job.new(job, callback))
end
def self.foreground_job(job, callback)
@@instance.add_job(Job.new(job, callback, true))
end
def initialize
@jobs = []
end
def handle_jobs
while BackgroundWorker.alive?
job = @jobs.shift
begin
job&.do
rescue => e
pp e
end
sleep 0.1
end
end
def add_job(job)
@jobs << job
end
class Job
def initialize(job, callback, deliver_to_queue = false)
@job = job
@callback = callback
@deliver_to_queue = deliver_to_queue
end
def do
result = @job.call
deliver(result)
end
def deliver(result)
if @deliver_to_queue
$window.main_thread_queue << -> { @callback.call(result) }
else
@callback.call(result)
end
end
end
end
end

View File

@@ -13,17 +13,10 @@ class W3DHub
if !force_fetch && File.exist?(path)
path
else
internet = Async::HTTP::Internet.instance
response = internet.get(uri, W3DHub::Api::DEFAULT_HEADERS)
if response.success?
response.save(path, "wb")
return path
end
false
BackgroundWorker.job(
-> { Async::HTTP::Internet.instance.get(uri, W3DHub::Api::DEFAULT_HEADERS) },
->(response) { response.save(path, "wb") if response.success? }
)
end
end
@@ -119,7 +112,7 @@ class W3DHub
tcp_nodelay: true,
headers: headers,
body: "data=#{JSON.dump({ category: package.category, subcategory: package.subcategory, name: package.name, version: package.version })}",
chunk_size: 4_000_000,
chunk_size: 50_000,
response_block: streamer
)

View File

@@ -60,25 +60,20 @@ class W3DHub
para I18n.t(:"games.fetching_news"), padding: 8
end
Async do
internet = Async::HTTP::Internet.instance
fetch_w3dhub_news
populate_w3dhub_news
end
BackgroundWorker.foreground_job(-> { fetch_w3dhub_news }, ->(result) { populate_w3dhub_news })
end
end
def fetch_w3dhub_news
news = Api.news("launcher-home")
if news
news.items[0..9].each do |item|
Cache.fetch(item.image)
end
return unless news
@w3dhub_news = news
news.items[0..9].each do |item|
Cache.fetch(item.image)
end
@w3dhub_news = news
end
def populate_w3dhub_news

View File

@@ -171,23 +171,20 @@ class W3DHub
title I18n.t(:"games.fetching_news"), padding: 8
end
Async do
fetch_game_news(game)
populate_game_news(game)
end
BackgroundWorker.foreground_job(-> { fetch_game_news(game) }, ->(result) { populate_game_news(game) })
end
end
def fetch_game_news(game)
news = Api.news(game.id)
if news
news.items[0..9].each do |item|
Cache.fetch(item.image)
end
return unless news
@game_news[game.id] = news
news.items[0..9].each do |item|
Cache.fetch(item.image)
end
@game_news[game.id] = news
end
def populate_game_news(game)

View File

@@ -36,32 +36,41 @@ class W3DHub
# Do network stuff
Async do
account = Api.user_login(@username.value, @password.value)
BackgroundWorker.foreground_job(
lambda do
account = Api.user_login(@username.value, @password.value)
applications = nil
if account
Store.account = account
Store.settings[:account][:data] = account
Store.settings.save_settings
if account
Store.account = account
Store.settings[:account][:data] = account
Store.settings.save_settings
internet = Async::HTTP::Internet.instance
Cache.fetch(account.avatar_uri, true)
Cache.fetch(account.avatar_uri, true) if account
applications = Api.applications if account
end
populate_account_info
applications = Api.applications
Store.applications = applications if applications
[account, applications]
end,
lambda do |result|
account, applications = result
page(W3DHub::Pages::Games)
else
# An error occurred, enable account entry
# NOTE: Too many incorrect entries causes lock out (Unknown duration)
@username.enabled = true
@password.enabled = true
btn.enabled = true
if account
populate_account_info
Store.applications = applications if applications
@error_label.value = "Incorrect username or password.\nOr too many failed login attempts, try again in a few minutes."
page(W3DHub::Pages::Games)
else
# An error occurred, enable account entry
# NOTE: Too many incorrect entries causes lock out (Unknown duration)
@username.enabled = true
@password.enabled = true
btn.enabled = true
@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
@@ -71,12 +80,13 @@ class W3DHub
end
if Store.account
Async do
Cache.fetch(Store.account.avatar_uri)
populate_account_info
page(W3DHub::Pages::Games)
end
BackgroundWorker.foreground_job(
-> { Cache.fetch(Store.account.avatar_uri) },
->(result) {
populate_account_info
page(W3DHub::Pages::Games)
}
)
end
end
@@ -103,22 +113,30 @@ class W3DHub
Store.settings.save_settings
Store.account = nil
applications = Api.applications
Store.applications if applications
BackgroundWorker.foreground_job(
-> { Api.applications },
lambda do |applications|
if applications
Store.applications = applications
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
@host.instance_variable_get(:"@account_container").clear do
stack(width: 0.7, height: 1.0) do
# background 0xff_222222
tagline "<b>#{I18n.t(:"interface.not_logged_in")}</b>", text_wrap: :none
@host.instance_variable_get(:"@account_container").clear do
stack(width: 0.7, height: 1.0) do
# background 0xff_222222
tagline "<b>#{I18n.t(:"interface.not_logged_in")}</b>", text_wrap: :none
flow(width: 1.0) do
link(I18n.t(:"interface.log_in"), text_size: 16, width: 0.5) { page(W3DHub::Pages::Login) }
link I18n.t(:"interface.register"), text_size: 16, width: 0.49 do
Launchy.open("https://secure.w3dhub.com/forum/index.php?app=core&module=global&section=register")
flow(width: 1.0) do
link(I18n.t(:"interface.log_in"), text_size: 16, width: 0.5) { page(W3DHub::Pages::Login) }
link I18n.t(:"interface.register"), text_size: 16, width: 0.49 do
Launchy.open("https://secure.w3dhub.com/forum/index.php?app=core&module=global&section=register")
end
end
end
end
end
end
)
end
end
end

View File

@@ -25,6 +25,7 @@ class W3DHub
flow(width: 0.75, height: 1.0) do
@filters.each do |app_id, enabled|
app = Store.applications.games.find { |a| a.id == app_id.to_s }
next unless app
image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{app_id}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png"
@@ -130,11 +131,14 @@ class W3DHub
populate_server_list
if @selected_server&.id == @refresh_server&.id
Async do
fetch_server_details(@refresh_server) if @refresh_server
populate_server_info(@refresh_server) if @refresh_server && @refresh_server == @selected_server
@refresh_server = nil
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
end
end
@@ -222,10 +226,10 @@ class W3DHub
@selected_server = server
Async do
fetch_server_details(server)
populate_server_info(server) if server == @selected_server
end
BackgroundWorker.foreground_job(
-> { fetch_server_details(server) },
->(result) { populate_server_info(server) if server == @selected_server }
)
end
stylize_selected_server(server_container) if server.id == @selected_server&.id
@@ -352,12 +356,10 @@ class W3DHub
end
def fetch_server_details(server)
Async do
internet = Async::HTTP::Internet.instance
server_data = Api.server_details(internet, server.id, 2)
server.update(server_data) if server_data
end
BackgroundWorker.foreground_job(
-> { Api.server_details(server.id, 2) },
->(server_data) { server.update(server_data) if server_data }
)
end
def game_icon(server)

View File

@@ -31,17 +31,10 @@ class W3DHub
inscription "#{I18n.t(:app_name)} #{W3DHub::VERSION}", width: 0.5, text_align: :right
end
end
Async do
@tasks.keys.each do |key|
Sync do
send(key)
end
end
end
end
def draw
Gosu.draw_circle(window.width / 2, window.height / 2, @w3dhub_logo.width * (0.6 + Math.cos(Gosu.milliseconds / 1000.0 * Math::PI).abs * 0.05), 128, 0x44_000000, 32)
@w3dhub_logo.draw_rot(window.width / 2, window.height / 2, 32)
super
@@ -50,13 +43,19 @@ class W3DHub
def update
super
# @fraction += 1.0 * window.dt
@fraction = 1.0 / (@tasks.size / @task_index.to_f)
@progressbar.value = @fraction
push_state(States::Interface) if @progressbar.value >= 1.0 && @task_index == @tasks.size
task = @tasks[@tasks.keys[@task_index]]
if task && !task[:started]
task[:started] = true
send(@tasks.keys[@task_index])
end
@task_index += 1 if @tasks.dig(@tasks.keys[@task_index], :complete)
end
@@ -66,56 +65,66 @@ class W3DHub
if (account.access_token_expiry - Time.now) / 60 <= 60 * 3 # Refresh if token expires within 3 hours
puts "Refreshing user login..."
@account = Api.refresh_user_login(account.refresh_token)
Api.on_fiber(:refresh_user_login, account.refresh_token) do |refreshed_account|
update_account_data(refreshed_account)
end
else
@account = account
update_account_data(account)
end
if @account
Store.account = @account
Store.settings[:account][:data] = @account
Cache.fetch(@account.avatar_uri, true)
else
Store.settings[:account] = {}
end
Store.settings.save_settings
@tasks[:refresh_user_token][:complete] = true
else
@tasks[:refresh_user_token][:complete] = true
end
end
def service_status
@service_status = Api.service_status
def update_account_data(account)
if account
Store.account = account
if @service_status
Store.service_status = @service_status
Store.settings[:account][:data] = account
if !@service_status.authentication? || !@service_status.package_download?
@status_label.value = "Authentication is #{@service_status.authentication? ? 'Okay' : 'Down'}. Package Download is #{@service_status.package_download? ? 'Okay' : 'Down'}."
end
@tasks[:service_status][:complete] = true
Cache.fetch(account.avatar_uri, true)
else
@status_label.value = I18n.t(:"boot.w3dhub_service_is_down")
Store.settings[:account] = {}
end
Store.settings.save_settings
@tasks[:refresh_user_token][:complete] = true
end
def service_status
Api.on_fiber(:service_status) do |service_status|
@service_status = service_status
if @service_status
Store.service_status = @service_status
if !@service_status.authentication? || !@service_status.package_download?
@status_label.value = "Authentication is #{@service_status.authentication? ? 'Okay' : 'Down'}. Package Download is #{@service_status.package_download? ? 'Okay' : 'Down'}."
end
@tasks[:service_status][:complete] = true
else
@status_label.value = I18n.t(:"boot.w3dhub_service_is_down")
end
end
end
def applications
@status_label.value = I18n.t(:"boot.checking_for_updates")
@applications = Api.applications
Api.on_fiber(:applications) do |applications|
if applications
Store.applications = applications
if @applications
Store.applications = @applications
@tasks[:applications][:complete] = true
else
# FIXME: Failed to retreive!
@tasks[:applications][:complete] = true
else
# FIXME: Failed to retreive!
@status_label.value = "FAILED TO RETREIVE APPS LIST"
end
end
end
@@ -127,8 +136,8 @@ class W3DHub
packages << { category: app.category, subcategory: app.id, name: "#{app.id}.ico", version: "" }
end
if (package_details = Api.package_details(packages))
package_details.each do |package|
Api.on_fiber(:package_details, packages) do |package_details|
package_details&.each do |package|
path = Cache.package_path(package.category, package.subcategory, package.name, package.version)
generated_icon_path = "#{GAME_ROOT_PATH}/media/icons/#{package.subcategory}.png"
@@ -145,37 +154,27 @@ class W3DHub
if regenerate
ico = ICO.new(file: path)
image = ico.images.sort_by(&:width).last
image = ico.images.max_by(&:width)
ico.save(image, generated_icon_path)
end
end
end
@tasks[:app_icons][:complete] = true
@tasks[:app_icons][:complete] = true
end
end
def server_list
@status_label.value = I18n.t(:"server_browser.fetching_server_list")
begin
internet = Async::HTTP::Internet.instance
list = Api.server_list(internet, 2)
if list
Store.server_list = list.sort_by! { |s| s&.status&.players&.size }.reverse
end
Api.on_fiber(:server_list, 2) do |list|
Store.server_list = list.sort_by! { |s| s&.status&.players&.size }.reverse if list
Store.server_list_last_fetch = Gosu.milliseconds
Api::ServerListUpdater.instance
@tasks[:server_list][:complete] = true
rescue => e
# Something went wrong!
pp e
Store.server_list = []
end
end
end

View File

@@ -158,6 +158,10 @@ class W3DHub
@page.focus
end
def current_page
@page
end
def update_server_browser(server)
return unless @page.is_a?(Pages::ServerBrowser)

View File

@@ -23,9 +23,6 @@ class W3DHub
super
Store.application_manager.start_next_available_task if Store.application_manager.idle?
current = Async::Task.current?
current&.yield
end
def gain_focus
@@ -64,4 +61,4 @@ class W3DHub
end
end
end
end
end

View File

@@ -27,6 +27,8 @@ I18n.load_path << Dir[File.expand_path("locales") + "/*.yml"]
I18n.default_locale = :en
class W3DHub
W3DHUB_DEBUG = ARGV.join.include?("--debug")
GAME_ROOT_PATH = File.expand_path(".", __dir__)
CACHE_PATH = "#{GAME_ROOT_PATH}/data/cache"
SETTINGS_FILE_PATH = "#{GAME_ROOT_PATH}/data/settings.json"
@@ -45,6 +47,7 @@ require_relative "lib/settings"
require_relative "lib/mixer"
require_relative "lib/ico"
require_relative "lib/multicast_server"
require_relative "lib/background_worker"
require_relative "lib/application_manager"
require_relative "lib/application_manager/manifest"
require_relative "lib/application_manager/status"
@@ -79,8 +82,8 @@ require_relative "lib/pages/login"
require_relative "lib/pages/settings"
require_relative "lib/pages/download_manager"
Async do
W3DHub::Window.new(width: 980, height: 720, borderless: false).show
exit # ensure reactor is shutdown when window is closed
Thread.new do
W3DHub::BackgroundWorker.create
end
W3DHub::Window.new(width: 980, height: 720, borderless: false).show