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 0000000..f912260 Binary files /dev/null and b/media/ui_icons/checkmark.png differ diff --git a/w3dhub.rb b/w3dhub.rb index bd0de69..e34807d 100644 --- a/w3dhub.rb +++ b/w3dhub.rb @@ -6,12 +6,16 @@ require "launchy" class W3DHub GAME_ROOT_PATH = File.expand_path(".", __dir__) CACHE_PATH = "#{GAME_ROOT_PATH}/data/cache" + SETTINGS_FILE_PATH = "#{GAME_ROOT_PATH}/data/settings.json" + EMPTY_IMAGE = Gosu::Image.from_blob(1, 1) BLACK_IMAGE = Gosu::Image.from_blob(1, 1, "\x00\x00\x00\xff") end require_relative "lib/version" require_relative "lib/window" +require_relative "lib/cache" +require_relative "lib/settings" require_relative "lib/states/boot" require_relative "lib/states/interface" @@ -38,7 +42,7 @@ require_relative "lib/pages/login" require_relative "lib/pages/settings" require_relative "lib/pages/download_manager" -require_relative "lib/renegade_server" -require_relative "lib/renegade_player" +# require_relative "lib/renegade_server" +# require_relative "lib/renegade_player" W3DHub::Window.new(width: 980, height: 720, borderless: false).show