Refresh token at start up now works, added a bit of a hack to populate account info by opening the login page first and having it populate the data then load the games page, added Cache, and Settings

This commit is contained in:
2021-11-14 18:54:09 -06:00
parent 4b69b60aab
commit b7ccdb2ad3
12 changed files with 207 additions and 35 deletions

View File

@@ -24,7 +24,32 @@ class W3DHub
# #
# On a failed login the service responds with: # On a failed login the service responds with:
# {"error":"login-failed"} # {"error":"login-failed"}
def self.user_refresh_token(refresh_token) def self.refresh_user_login(refresh_token)
response = W3DHUB_API_CONNECTION.post(
path: "apis/launcher/1/user-login",
headers: DEFAULT_HEADERS.merge({"Content-Type": "application/x-www-form-urlencoded"}),
body: "data=#{JSON.dump({refreshToken: refresh_token})}"
)
if response.status == 200
user_data = JSON.parse(response.body, symbolize_names: true)
return false if user_data[:error]
user_details = W3DHUB_API_CONNECTION.post(
path: "apis/w3dhub/1/get-user-details",
headers: DEFAULT_HEADERS.merge({"Content-Type": "application/x-www-form-urlencoded"}),
body: "data=#{JSON.dump({ id: user_data[:userid] })}"
)
if user_details.status == 200
user_details_data = JSON.parse(user_details.body, symbolize_names: true)
end
return Account.new(user_data, user_details_data)
else
false
end
end end
# See #user_refresh_token # See #user_refresh_token

32
lib/cache.rb Normal file
View File

@@ -0,0 +1,32 @@
class W3DHub
class Cache
def self.path(uri)
ext = File.basename(uri).split(".").last
"#{CACHE_PATH}/#{Digest::SHA2.hexdigest(uri)}.#{ext}"
end
def self.fetch(uri)
path = path(uri)
if File.exist?(path)
path
else
response = Excon.get(uri)
if response.status == 200
File.open(path, "wb") do |f|
f.write(response.body)
end
path
end
false
end
end
def self.fetch_package(*args)
end
end
end

View File

@@ -134,18 +134,20 @@ class W3DHub
if news if news
news.items[0..9].each do |item| news.items[0..9].each do |item|
# Cache Image # Cache Image
ext = File.basename(item.image).split(".").last # ext = File.basename(item.image).split(".").last
path = "#{CACHE_PATH}/#{Digest::SHA2.hexdigest(item.image)}.#{ext}" # path = "#{CACHE_PATH}/#{Digest::SHA2.hexdigest(item.image)}.#{ext}"
next if File.exist?(path) # next if File.exist?(path)
response = Excon.get(item.image) # response = Excon.get(item.image)
if response.status == 200 # if response.status == 200
File.open(path, "wb") do |f| # File.open(path, "wb") do |f|
f.write(response.body) # f.write(response.body)
end # end
end # end
Cache.fetch(item.image)
end end
@@game_news[game.id] = news @@game_news[game.id] = news
@@ -161,8 +163,7 @@ class W3DHub
flow(width: 0.5, height: 128, margin: 4) do flow(width: 0.5, height: 128, margin: 4) do
# background 0x88_000000 # background 0x88_000000
ext = File.basename(item.image).split(".").last path = Cache.path(item.image)
path = "#{CACHE_PATH}/#{Digest::SHA2.hexdigest(item.image)}.#{ext}"
if File.exist?(path) if File.exist?(path)
image path, width: 0.4, padding: 4 image path, width: 0.4, padding: 4

View File

@@ -41,24 +41,14 @@ class W3DHub
if account if account
@host.account = account @host.account = account
window.settings[:account][:refresh_token] = account.refresh_token
window.settings.save_settings
ext = File.basename(account.avatar_uri).split(".").last Cache.fetch(account.avatar_uri)
path = "#{CACHE_PATH}/#{Digest::SHA2.hexdigest(account.avatar_uri)}.#{ext}"
unless File.exist?(path)
response = Excon.get(account.avatar_uri)
if response.status == 200
File.open(path, "wb") do |f|
f.write(response.body)
end
end
end
main_thread_queue << proc { populate_account_info; page(W3DHub::Pages::Games) } main_thread_queue << proc { populate_account_info; page(W3DHub::Pages::Games) }
else else
# An error occurred, enable account entry # An error occurred, enable account entry
# FIXME: Show an error message
# NOTE: Too many incorrect entries causes lock out (Unknown duration) # NOTE: Too many incorrect entries causes lock out (Unknown duration)
main_thread_queue << proc do main_thread_queue << proc do
@username.enabled = true @username.enabled = true
@@ -76,6 +66,11 @@ class W3DHub
end end
end end
end end
if @host.account
populate_account_info
page(W3DHub::Pages::Games)
end
end end
def populate_account_info def populate_account_info
@@ -99,6 +94,9 @@ class W3DHub
end end
def depopulate_account_info def depopulate_account_info
window.settings[:account][:refresh_token] = nil
window.settings.save_settings
@host.instance_variable_get(:"@account_container").clear do @host.instance_variable_get(:"@account_container").clear do
stack(width: 0.7, height: 1.0) do stack(width: 0.7, height: 1.0) do
# background 0xff_222222 # background 0xff_222222

View File

@@ -14,7 +14,7 @@ class W3DHub
para "<b>App Install Folder</b>", width: 0.249 para "<b>App Install Folder</b>", width: 0.249
stack(width: 0.75, height: 1.0) do stack(width: 0.75, height: 1.0) do
edit_line "C:\\Program Files (x86)\\W3D Hub", width: 1.0 edit_line window.settings[:app_install_dir], width: 1.0
inscription "The folder into which new games and apps will be installed by the launcher" inscription "The folder into which new games and apps will be installed by the launcher"
end end
end end
@@ -23,7 +23,7 @@ class W3DHub
para "<b>Package Cache Folder</b>", width: 0.249 para "<b>Package Cache Folder</b>", width: 0.249
stack(width: 0.75, height: 1.0) do stack(width: 0.75, height: 1.0) do
edit_line "C:\\Program Data\\W3D Hub\\Launcher\\package-cache", width: 1.0 edit_line window.settings[:package_cache_dir], width: 1.0
inscription "A folder which will be used to cache downloaded packages used to install games and apps" inscription "A folder which will be used to cache downloaded packages used to install games and apps"
end end
end end
@@ -33,7 +33,9 @@ class W3DHub
check_box "Enable Automatic Error Reporting", text_size: 16 check_box "Enable Automatic Error Reporting", text_size: 16
inscription "If this is enabled the launcher will automatically report errors to the development team, along with basic information about your machine, such as operating system.", width: 1.0 inscription "If this is enabled the launcher will automatically report errors to the development team, along with basic information about your machine, such as operating system.", width: 1.0
button "Save", margin_top: 32 button "Save", margin_top: 32 do
window.settings.save_settings
end
end end
end end
end end

78
lib/settings.rb Normal file
View File

@@ -0,0 +1,78 @@
class W3DHub
class Settings
def self.defaults
{
language: Gosu.user_languages.first.split("_").first,
app_install_dir: default_app_install_dir,
package_cache_dir: default_package_cache_dir,
allow_diagnostic_reports: false,
server_list_username: nil,
account: {},
applications: {},
games: {}
}
end
def self.default_app_install_dir
if windows?
"#{home_directory}/#{W3DHub::DIR_NAME}"
elsif linux?
"#{home_directory}/.local/share/#{W3DHub::DIR_NAME}"
elsif mac?
"#{home_directory}/.local/share/#{W3DHub::DIR_NAME}"
else
raise "Unknown platform: #{RbConfig::CONFIG["host_os"]}"
end
end
def self.default_package_cache_dir
if windows?
"#{home_directory}/#{W3DHub::DIR_NAME}/Launcher/package-cache"
elsif linux?
"#{home_directory}/.local/share/#{W3DHub::DIR_NAME}/package-cache"
elsif mac?
"#{home_directory}/.local/share/#{W3DHub::DIR_NAME}/package-cache"
else
raise "Unknown platform: #{RbConfig::CONFIG["host_os"]}"
end
end
def self.windows?
RbConfig::CONFIG["host_os"] =~ /(mingw|mswin|windows)/i
end
def self.mac?
RbConfig::CONFIG["host_os"] =~ /(darwin|mac os)/i
end
def self.linux?
RbConfig::CONFIG["host_os"] =~ /(linux|bsd|aix|solaris)/i
end
def self.home_directory
File.expand_path("~")
end
def initialize
unless File.exist?(SETTINGS_FILE_PATH)
@settings = Settings.defaults
save_settings
else
load_settings
end
end
def [](*args)
@settings.dig(*args)
end
def load_settings
@settings = JSON.parse(File.read(SETTINGS_FILE_PATH), symbolize_names: true)
end
def save_settings
File.write(SETTINGS_FILE_PATH, @settings.to_json)
end
end
end

View File

@@ -44,7 +44,7 @@ class W3DHub
if @progressbar.value >= 1.0 && @task_index == @tasks.size if @progressbar.value >= 1.0 && @task_index == @tasks.size
push_state( push_state(
States::Interface, States::Interface,
refresh_token: @refresh_token, account: @account,
service_status: @service_status, service_status: @service_status,
applications: @applications applications: @applications
) )
@@ -61,9 +61,23 @@ class W3DHub
end end
def refresh_user_token def refresh_user_token
@tasks[:refresh_user_token][:complete] = true if window.settings[:account, :refresh_token]
Thread.new do
@account = Api.refresh_user_login(window.settings[:account, :refresh_token])
@refresh_token = nil if @account
window.settings[:account][:refresh_token] = @account.refresh_token
window.settings.save_settings
else
window.settings[:account][:refresh_token] = nil
window.settings.save_settings
end
@tasks[:refresh_user_token][:complete] = true
end
else
@tasks[:refresh_user_token][:complete] = true
end
end end
def service_status def service_status

View File

@@ -59,6 +59,13 @@ class W3DHub
active: { active: {
background: 0xff_add5ec background: 0xff_add5ec
} }
},
ToggleButton: {
padding_left: 8,
padding_right: 8,
width: 18,
image_width: 18,
checkmark_image: "#{GAME_ROOT_PATH}/media/ui_icons/checkmark.png"
} }
}) })
@@ -185,7 +192,11 @@ class W3DHub
end end
end end
page(W3DHub::Pages::Games) if @account
page(W3DHub::Pages::Login)
else
page(W3DHub::Pages::Games)
end
end end
def draw def draw

View File

@@ -1,4 +1,5 @@
class W3DHub class W3DHub
DIR_NAME = "W3DHubAlt"
NAME = "W3D Hub Launcher" NAME = "W3D Hub Launcher"
VERSION = "0.1.0" VERSION = "0.1.0"
end end

View File

@@ -1,7 +1,13 @@
class W3DHub class W3DHub
class Window < CyberarmEngine::Window class Window < CyberarmEngine::Window
attr_reader :settings
def setup def setup
self.caption = "W3D Hub Launcher" self.caption = "#{W3DHub::NAME}"
@settings = Settings.new
@settings.save_settings
push_state(W3DHub::States::Boot) push_state(W3DHub::States::Boot)
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -6,12 +6,16 @@ require "launchy"
class W3DHub class W3DHub
GAME_ROOT_PATH = File.expand_path(".", __dir__) GAME_ROOT_PATH = File.expand_path(".", __dir__)
CACHE_PATH = "#{GAME_ROOT_PATH}/data/cache" CACHE_PATH = "#{GAME_ROOT_PATH}/data/cache"
SETTINGS_FILE_PATH = "#{GAME_ROOT_PATH}/data/settings.json"
EMPTY_IMAGE = Gosu::Image.from_blob(1, 1) EMPTY_IMAGE = Gosu::Image.from_blob(1, 1)
BLACK_IMAGE = Gosu::Image.from_blob(1, 1, "\x00\x00\x00\xff") BLACK_IMAGE = Gosu::Image.from_blob(1, 1, "\x00\x00\x00\xff")
end end
require_relative "lib/version" require_relative "lib/version"
require_relative "lib/window" require_relative "lib/window"
require_relative "lib/cache"
require_relative "lib/settings"
require_relative "lib/states/boot" require_relative "lib/states/boot"
require_relative "lib/states/interface" require_relative "lib/states/interface"
@@ -38,7 +42,7 @@ require_relative "lib/pages/login"
require_relative "lib/pages/settings" require_relative "lib/pages/settings"
require_relative "lib/pages/download_manager" require_relative "lib/pages/download_manager"
require_relative "lib/renegade_server" # require_relative "lib/renegade_server"
require_relative "lib/renegade_player" # require_relative "lib/renegade_player"
W3DHub::Window.new(width: 980, height: 720, borderless: false).show W3DHub::Window.new(width: 980, height: 720, borderless: false).show