From b7ccdb2ad3d1491abb081d2196ab3fa10b4d0fa4 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sun, 14 Nov 2021 18:54:09 -0600 Subject: [PATCH] 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 --- lib/api.rb | 27 +++++++++++- lib/cache.rb | 32 ++++++++++++++ lib/pages/games.rb | 23 ++++++----- lib/pages/login.rb | 24 +++++------ lib/pages/settings.rb | 8 ++-- lib/settings.rb | 78 +++++++++++++++++++++++++++++++++++ lib/states/boot.rb | 20 +++++++-- lib/states/interface.rb | 13 +++++- lib/version.rb | 1 + lib/window.rb | 8 +++- media/ui_icons/checkmark.png | Bin 0 -> 15751 bytes w3dhub.rb | 8 +++- 12 files changed, 207 insertions(+), 35 deletions(-) create mode 100644 lib/cache.rb create mode 100644 lib/settings.rb create mode 100644 media/ui_icons/checkmark.png diff --git a/lib/api.rb b/lib/api.rb index f0aed19..e08a0f3 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -24,7 +24,32 @@ class W3DHub # # On a failed login the service responds with: # {"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 # See #user_refresh_token diff --git a/lib/cache.rb b/lib/cache.rb new file mode 100644 index 0000000..30da39a --- /dev/null +++ b/lib/cache.rb @@ -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 diff --git a/lib/pages/games.rb b/lib/pages/games.rb index f9ce83e..bd002fd 100644 --- a/lib/pages/games.rb +++ b/lib/pages/games.rb @@ -134,18 +134,20 @@ class W3DHub if news news.items[0..9].each do |item| # Cache Image - ext = File.basename(item.image).split(".").last - path = "#{CACHE_PATH}/#{Digest::SHA2.hexdigest(item.image)}.#{ext}" + # ext = File.basename(item.image).split(".").last + # 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 - File.open(path, "wb") do |f| - f.write(response.body) - end - end + # if response.status == 200 + # File.open(path, "wb") do |f| + # f.write(response.body) + # end + # end + + Cache.fetch(item.image) end @@game_news[game.id] = news @@ -161,8 +163,7 @@ class W3DHub flow(width: 0.5, height: 128, margin: 4) do # background 0x88_000000 - ext = File.basename(item.image).split(".").last - path = "#{CACHE_PATH}/#{Digest::SHA2.hexdigest(item.image)}.#{ext}" + path = Cache.path(item.image) if File.exist?(path) image path, width: 0.4, padding: 4 diff --git a/lib/pages/login.rb b/lib/pages/login.rb index 60dc076..2322406 100644 --- a/lib/pages/login.rb +++ b/lib/pages/login.rb @@ -41,24 +41,14 @@ class W3DHub if account @host.account = account + window.settings[:account][:refresh_token] = account.refresh_token + window.settings.save_settings - ext = File.basename(account.avatar_uri).split(".").last - 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 + Cache.fetch(account.avatar_uri) main_thread_queue << proc { populate_account_info; page(W3DHub::Pages::Games) } else # An error occurred, enable account entry - # FIXME: Show an error message # NOTE: Too many incorrect entries causes lock out (Unknown duration) main_thread_queue << proc do @username.enabled = true @@ -76,6 +66,11 @@ class W3DHub end end end + + if @host.account + populate_account_info + page(W3DHub::Pages::Games) + end end def populate_account_info @@ -99,6 +94,9 @@ class W3DHub end def depopulate_account_info + window.settings[:account][:refresh_token] = nil + window.settings.save_settings + @host.instance_variable_get(:"@account_container").clear do stack(width: 0.7, height: 1.0) do # background 0xff_222222 diff --git a/lib/pages/settings.rb b/lib/pages/settings.rb index 4170623..94e056b 100644 --- a/lib/pages/settings.rb +++ b/lib/pages/settings.rb @@ -14,7 +14,7 @@ class W3DHub para "App Install Folder", width: 0.249 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" end end @@ -23,7 +23,7 @@ class W3DHub para "Package Cache Folder", width: 0.249 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" end end @@ -33,7 +33,9 @@ class W3DHub 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 - button "Save", margin_top: 32 + button "Save", margin_top: 32 do + window.settings.save_settings + end end end end diff --git a/lib/settings.rb b/lib/settings.rb new file mode 100644 index 0000000..f616111 --- /dev/null +++ b/lib/settings.rb @@ -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 diff --git a/lib/states/boot.rb b/lib/states/boot.rb index 8342342..3447161 100644 --- a/lib/states/boot.rb +++ b/lib/states/boot.rb @@ -44,7 +44,7 @@ class W3DHub if @progressbar.value >= 1.0 && @task_index == @tasks.size push_state( States::Interface, - refresh_token: @refresh_token, + account: @account, service_status: @service_status, applications: @applications ) @@ -61,9 +61,23 @@ class W3DHub end 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 def service_status diff --git a/lib/states/interface.rb b/lib/states/interface.rb index 2ce7cb4..59015c0 100644 --- a/lib/states/interface.rb +++ b/lib/states/interface.rb @@ -59,6 +59,13 @@ class W3DHub active: { 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 - page(W3DHub::Pages::Games) + if @account + page(W3DHub::Pages::Login) + else + page(W3DHub::Pages::Games) + end end def draw diff --git a/lib/version.rb b/lib/version.rb index 8098436..95e1c69 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,4 +1,5 @@ class W3DHub + DIR_NAME = "W3DHubAlt" NAME = "W3D Hub Launcher" VERSION = "0.1.0" end \ No newline at end of file diff --git a/lib/window.rb b/lib/window.rb index becb5d0..a45104a 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -1,7 +1,13 @@ class W3DHub class Window < CyberarmEngine::Window + attr_reader :settings + def setup - self.caption = "W3D Hub Launcher" + self.caption = "#{W3DHub::NAME}" + + @settings = Settings.new + + @settings.save_settings push_state(W3DHub::States::Boot) end diff --git a/media/ui_icons/checkmark.png b/media/ui_icons/checkmark.png new file mode 100644 index 0000000000000000000000000000000000000000..f912260ee119064367e650942b925d90a9ae2c54 GIT binary patch literal 15751 zcmeI3dr(tX9>*_YQ9)Lz)%s|6uW73lOm1!-K<*|Xn1^7EA_N6jT0(M#D@kt5y^w%T z3w8$Gu8dgUk9Hg%712s{hFa0uIKI}Q+HGCb)mm6}VWC=D9akN9*FAYa4uN>xnf`e* zljN6kzQ6Ce=W~AN~n;T5vYlJf+pFxng zF}`;cvUU4-1W|0HOnGdcaW-M0?IN?4E+R!9yA!ApM49Gsnk}Uyix!c^ltU%xIrfzR zrK~E!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