From a4dd3755112fa12a089fd6457ee9f05fe71f390d Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 22 Nov 2021 20:38:24 -0600 Subject: [PATCH] Removed window as central storage, added Store class to be central memory store --- lib/application_manager.rb | 29 ++++++++++++++-------------- lib/application_manager/task.rb | 8 ++++---- lib/cache.rb | 4 ++-- lib/pages/download_manager.rb | 2 +- lib/pages/games.rb | 34 ++++++++++++++++----------------- lib/pages/login.rb | 18 ++++++++--------- lib/pages/server_browser.rb | 28 +++++++++++++-------------- lib/pages/settings.rb | 6 +++--- lib/states/boot.rb | 18 ++++++++--------- lib/states/interface.rb | 4 ++-- lib/store.rb | 27 ++++++++++++++++++++++++++ lib/window.rb | 15 ++++++--------- w3dhub.rb | 1 + 13 files changed, 109 insertions(+), 85 deletions(-) create mode 100644 lib/store.rb diff --git a/lib/application_manager.rb b/lib/application_manager.rb index 7befa18..bbe08b8 100644 --- a/lib/application_manager.rb +++ b/lib/application_manager.rb @@ -58,8 +58,8 @@ class W3DHub # open wwconfig.exe or config.exe for ecw if (app_data = installed?(app_id, channel) && W3DHub.unix?) - exe = if window.settings[:wine_prefix] - "WINEPREFIX=\"#{window.settings[:wine_prefix]}\" winecfg" + exe = if Store.settings[:wine_prefix] + "WINEPREFIX=\"#{Store.settings[:wine_prefix]}\" winecfg" else "winecfg" end @@ -106,28 +106,27 @@ class W3DHub def wine_command(app_id, channel) return "" if W3DHub.windows? - if window.settings[:wine_prefix] - "WINEPREFIX=\"#{window.settings[:wine_prefix]}\" \"#{window.settings[:wine_command]}\" " + if Store.settings[:wine_prefix] + "WINEPREFIX=\"#{Store.settings[:wine_prefix]}\" \"#{Store.settings[:wine_command]}\" " else - "#{window.settings[:wine_command]} " + "#{Store.settings[:wine_command]} " end end def run(app_id, channel, *args) if (app_data = installed?(app_id, channel)) - pp "#{wine_command(app_id, channel)}#{app_data[:install_path]}", *args pid = Process.spawn("#{wine_command(app_id, channel)}#{app_data[:install_path]}", *args) Process.detach(pid) end end def join_server(app_id, channel, server, password = nil) - if installed?(app_id, channel) && window.settings[:server_list_username].to_s.length.positive? + if installed?(app_id, channel) && Store.settings[:server_list_username].to_s.length.positive? run( app_id, channel, "-launcher", "+connect #{server.address}:#{server.port}", - "+netplayername \"#{window.settings[:server_list_username]}\"", + "+netplayername \"#{Store.settings[:server_list_username]}\"", password ? "+password \"#{password}\"" : "" ) end @@ -179,9 +178,9 @@ class W3DHub wine_prefix: nil } - window.settings[:games] ||= {} - window.settings[:games][:"#{app_id}_#{channel_id}"] = application_data - window.settings.save_settings + Store.settings[:games] ||= {} + Store.settings[:games][:"#{app_id}_#{channel_id}"] = application_data + Store.settings.save_settings end end end @@ -206,13 +205,13 @@ class W3DHub wine_prefix: task.wine_prefix } - window.settings[:games] ||= {} - window.settings[:games][:"#{task.app_id}_#{task.release_channel}"] = application_data - window.settings.save_settings + Store.settings[:games] ||= {} + Store.settings[:games][:"#{task.app_id}_#{task.release_channel}"] = application_data + Store.settings.save_settings end def installed?(app_id, channel) - window.settings[:games, :"#{app_id}_#{channel}"] + Store.settings[:games, :"#{app_id}_#{channel}"] end def installing?(app_id, channel) diff --git a/lib/application_manager/task.rb b/lib/application_manager/task.rb index 0b65b03..b2c268e 100644 --- a/lib/application_manager/task.rb +++ b/lib/application_manager/task.rb @@ -13,7 +13,7 @@ class W3DHub @task_state = :not_started # :not_started, :running, :paused, :halted, :complete, :failed - @application = window.applications.games.find { |g| g.id == app_id } + @application = Store.applications.games.find { |g| g.id == app_id } @channel = @application.channels.find { |c| c.id == release_channel } @packages_to_download = [] @@ -102,8 +102,8 @@ class W3DHub fail!("FAIL FAST: `#{W3DHub.tar_command} --help` command failed, #{W3DHub.tar_command} is not installed. Will be unable to unpack packages.") unless bsdtar_present if W3DHub.unix? - wine_present = system("which #{window.settings[:wine_command]}") - fail!("FAIL FAST: `which #{window.settings[:wine_command]}` command failed, wine is not installed. Will be unable to create prefixes or launch games.") unless wine_present + wine_present = system("which #{Store.settings[:wine_command]}") + fail!("FAIL FAST: `which #{Store.settings[:wine_command]}` command failed, wine is not installed. Will be unable to create prefixes or launch games.") unless wine_present end end @@ -284,7 +284,7 @@ class W3DHub end def mark_application_installed - window.application_manager.installed!(self) + Store.application_manager.installed!(self) puts "#{@app_id} has been installed." end diff --git a/lib/cache.rb b/lib/cache.rb index 7bb6966..bf4caf3 100644 --- a/lib/cache.rb +++ b/lib/cache.rb @@ -34,13 +34,13 @@ class W3DHub end def self.package_path(category, subcategory, name, version) - package_cache_dir = $window.settings[:package_cache_dir] + package_cache_dir = $Store.settings[:package_cache_dir] "#{package_cache_dir}/#{category}/#{subcategory}/#{version}/#{name}.package" end def self.install_path(application, channel) - app_install_dir = $window.settings[:app_install_dir] + app_install_dir = $Store.settings[:app_install_dir] "#{app_install_dir}/#{application.category}/#{application.id}/#{channel.id}" end diff --git a/lib/pages/download_manager.rb b/lib/pages/download_manager.rb index 178a0ec..0c06dc2 100644 --- a/lib/pages/download_manager.rb +++ b/lib/pages/download_manager.rb @@ -5,7 +5,7 @@ class W3DHub def setup @download_package_info ||= {} - @task = window.application_manager.current_task + @task = Store.application_manager.current_task return unless @task diff --git a/lib/pages/games.rb b/lib/pages/games.rb index aba35d1..a2a35ff 100644 --- a/lib/pages/games.rb +++ b/lib/pages/games.rb @@ -3,7 +3,7 @@ class W3DHub class Games < Page def setup @game_news ||= {} - @focused_game ||= window.applications.games.first + @focused_game ||= Store.applications.games.first body.clear do # Games List @@ -23,7 +23,7 @@ class W3DHub @games_list_container.clear do background 0xff_121920 - window.applications.games.each do |game| + Store.applications.games.each do |game| selected = game == @focused_game game_button = stack(width: 1.0, border_thickness_left: 4, @@ -34,7 +34,7 @@ class W3DHub flow(width: 1.0, height: 48) do stack(width: 0.3) - image "#{GAME_ROOT_PATH}/media/icons/#{game.id}.png", height: 48, color: window.application_manager.installed?(game.id, game.channels.first.id) ? 0xff_ffffff : 0xee_444444 + image "#{GAME_ROOT_PATH}/media/icons/#{game.id}.png", height: 48, color: Store.application_manager.installed?(game.id, game.channels.first.id) ? 0xff_ffffff : 0xee_444444 end inscription game.name, width: 1.0, text_align: :center end @@ -85,17 +85,17 @@ class W3DHub # end # end - if window.application_manager.installed?(game.id, channel.id) + if Store.application_manager.installed?(game.id, channel.id) Hash.new.tap { |hash| - hash[I18n.t(:"games.game_settings")] = { icon: "gear", block: proc { window.application_manager.settings(game.id, channel.id) } } - hash[I18n.t(:"games.wine_configuration")] = { icon: "gear", block: proc { window.application_manager.wine_configuration(game.id, channel.id) } } if W3DHub.unix? + hash[I18n.t(:"games.game_settings")] = { icon: "gear", block: proc { Store.application_manager.settings(game.id, channel.id) } } + hash[I18n.t(:"games.wine_configuration")] = { icon: "gear", block: proc { Store.application_manager.wine_configuration(game.id, channel.id) } } if W3DHub.unix? if game.id != "ren" - hash[I18n.t(:"games.repair_installation")] = { icon: "wrench", block: proc { window.application_manager.repair(game.id, channel.id) } } - hash[I18n.t(:"games.uninstall_game")] = { icon: "trashCan", block: proc { window.application_manager.uninstall(game.id, channel.id) } } + hash[I18n.t(:"games.repair_installation")] = { icon: "wrench", block: proc { Store.application_manager.repair(game.id, channel.id) } } + hash[I18n.t(:"games.uninstall_game")] = { icon: "trashCan", block: proc { Store.application_manager.uninstall(game.id, channel.id) } } end - hash[I18n.t(:"games.install_folder")] = { icon: nil, block: proc { window.application_manager.show_folder(game.id, channel.id, :installation) } } - hash[I18n.t(:"games.user_data_folder")] = { icon: nil, block: proc { window.application_manager.show_folder(game.id, channel.id, :user_data) } } - hash[I18n.t(:"games.view_screenshots")] = { icon: nil, block: proc { window.application_manager.show_folder(game.id, channel.id, :screenshots) } } + hash[I18n.t(:"games.install_folder")] = { icon: nil, block: proc { Store.application_manager.show_folder(game.id, channel.id, :installation) } } + hash[I18n.t(:"games.user_data_folder")] = { icon: nil, block: proc { Store.application_manager.show_folder(game.id, channel.id, :user_data) } } + hash[I18n.t(:"games.view_screenshots")] = { icon: nil, block: proc { Store.application_manager.show_folder(game.id, channel.id, :screenshots) } } }.each do |key, hash| flow(width: 1.0, height: 22, margin_bottom: 8) do image "#{GAME_ROOT_PATH}/media/ui_icons/#{hash[:icon]}.png", width: 0.11 if hash[:icon] @@ -127,24 +127,24 @@ class W3DHub flow(width: 1.0, height: 0.08) do # background 0xff_551100 - if window.application_manager.installed?(game.id, channel.id) + if Store.application_manager.installed?(game.id, channel.id) button "#{I18n.t(:"interface.play_now")}", margin_left: 24 do - window.application_manager.run(game.id, channel.id) + Store.application_manager.run(game.id, channel.id) end button "#{I18n.t(:"interface.single_player")}", margin_left: 24 do - window.application_manager.run(game.id, channel.id) + Store.application_manager.run(game.id, channel.id) end else unless game.id == "ren" - button "#{I18n.t(:"interface.install")}", margin_left: 24, enabled: !window.application_manager.task?(:installer, game.id, channel.id) do |button| + button "#{I18n.t(:"interface.install")}", margin_left: 24, enabled: !Store.application_manager.task?(:installer, game.id, channel.id) do |button| button.enabled = false - window.application_manager.install(game.id, channel.id) + Store.application_manager.install(game.id, channel.id) end end button "#{I18n.t(:"interface.import")}", margin_left: 24, enabled: false do - window.application_manager.import(game.id, channel.id, "?") + Store.application_manager.import(game.id, channel.id, "?") end end end diff --git a/lib/pages/login.rb b/lib/pages/login.rb index 587dbfd..af4c2e6 100644 --- a/lib/pages/login.rb +++ b/lib/pages/login.rb @@ -40,9 +40,9 @@ class W3DHub account = Api.user_login(@username.value, @password.value) if account - window.account = account - window.settings[:account][:refresh_token] = account.refresh_token - window.settings.save_settings + Store.account = account + Store.settings[:account][:refresh_token] = account.refresh_token + Store.settings.save_settings Cache.fetch(account.avatar_uri) @@ -67,7 +67,7 @@ class W3DHub end end - if window.account + if Store.account populate_account_info page(W3DHub::Pages::Games) end @@ -77,23 +77,23 @@ class W3DHub @host.instance_variable_get(:"@account_container").clear do stack(width: 0.7, height: 1.0) do # background 0xff_222222 - tagline "#{window.account.username}" + tagline "#{Store.account.username}" flow(width: 1.0) do link(I18n.t(:"interface.log_out"), text_size: 16, width: 0.5) { depopulate_account_info } link I18n.t(:"interface.profile"), text_size: 16, width: 0.49 do - Launchy.open("https://secure.w3dhub.com/forum/index.php?showuser=#{window.account.id}") + Launchy.open("https://secure.w3dhub.com/forum/index.php?showuser=#{Store.account.id}") end end end - image Cache.path(window.account.avatar_uri), height: 1.0 + image Cache.path(Store.account.avatar_uri), height: 1.0 end end def depopulate_account_info - window.settings[:account][:refresh_token] = nil - window.settings.save_settings + Store.settings[:account][:refresh_token] = nil + Store.settings.save_settings @host.instance_variable_get(:"@account_container").clear do stack(width: 0.7, height: 1.0) do diff --git a/lib/pages/server_browser.rb b/lib/pages/server_browser.rb index 92f8e30..1ecf5e1 100644 --- a/lib/pages/server_browser.rb +++ b/lib/pages/server_browser.rb @@ -8,7 +8,7 @@ class W3DHub @filters = {} @filter_region = "Any" # "Any", "North America", "Europe" - window.applications.games.each { |game| @filters[game.id] = true } + Store.applications.games.each { |game| @filters[game.id] = true } body.clear do stack(width: 1.0, height: 1.0, padding: 8) do @@ -19,7 +19,7 @@ class W3DHub flow(width: 1.0, height: 0.06) do flow(width: 0.75, height: 1.0) do @filters.each do |app_id, enabled| - app = window.applications.games.find { |a| a.id == app_id } + app = Store.applications.games.find { |a| a.id == app_id } image "#{GAME_ROOT_PATH}/media/icons/#{app_id}.png", tip: "#{app.name}", height: 1.0, border_thickness_bottom: 1, border_color_bottom: 0x00_000000, @@ -48,14 +48,14 @@ class W3DHub flow(width: 0.249, height: 1.0) do inscription "#{I18n.t(:"server_browser.nickname")}:", width: 0.32 - @nickname_label = inscription "#{window.settings[:server_list_username]}", width: 0.6 + @nickname_label = inscription "#{Store.settings[:server_list_username]}", width: 0.6 image "#{GAME_ROOT_PATH}/media/ui_icons/wrench.png", height: 16, hover: { color: 0xaa_ffffff }, tip: I18n.t(:"server_browser.set_nickname") do # Prompt for player name prompt_for_nickname( accept_callback: proc do |entry| @nickname_label.value = entry - window.settings[:server_list_username] = entry - window.settings.save_settings + Store.settings[:server_list_username] = entry + Store.settings.save_settings end ) end @@ -189,7 +189,7 @@ class W3DHub end stack(width: 1.0, height: 0.25) do - game_installed = window.application_manager.installed?(server.game, window.applications.games.find { |g| g.id == server.game }.channels.first.id) + game_installed = Store.application_manager.installed?(server.game, Store.applications.games.find { |g| g.id == server.game }.channels.first.id) button "#{I18n.t(:"server_browser.join_server")}", enabled: !game_installed.nil? do # Check for nickname @@ -198,12 +198,12 @@ class W3DHub # Check if password needed # prompt for password # Launch game - if window.settings[:server_list_username].to_s.length.zero? + if Store.settings[:server_list_username].to_s.length.zero? prompt_for_nickname( accept_callback: proc do |entry| @nickname_label.value = entry - window.settings[:server_list_username] = entry - window.settings.save_settings + Store.settings[:server_list_username] = entry + Store.settings.save_settings if server.status.password prompt_for_password( @@ -315,7 +315,7 @@ class W3DHub end def game_name(game) - window.applications.games.detect { |g| g.id == game }&.name + Store.applications.games.detect { |g| g.id == game }&.name end def prompt_for_nickname(accept_callback: nil, cancel_callback: nil) @@ -323,7 +323,7 @@ class W3DHub W3DHub::States::PromptDialog, title: I18n.t(:"server_browser.set_nickname"), message: I18n.t(:"server_browser.set_nickname_message"), - prefill: window.settings[:server_list_username], + prefill: Store.settings[:server_list_username], accept_callback: accept_callback, cancel_callback: cancel_callback, valid_callback: proc { |entry| entry.length.positive? } @@ -346,11 +346,11 @@ class W3DHub if ( (server.status.password && password.length.positive?) || !server.status.password) && - window.settings[:server_list_username].to_s.length.positive? + Store.settings[:server_list_username].to_s.length.positive? - window.application_manager.join_server( + Store.application_manager.join_server( server.game, - window.applications.games.find { |g| g.id == server.game }.channels.first.id, server, password + Store.applications.games.find { |g| g.id == server.game }.channels.first.id, server, password ) else window.push_state(W3DHub::States::MessageDialog, type: "?", title: "?", message: "?") diff --git a/lib/pages/settings.rb b/lib/pages/settings.rb index 94e056b..dd49f02 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 window.settings[:app_install_dir], width: 1.0 + edit_line Store.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 window.settings[:package_cache_dir], width: 1.0 + edit_line Store.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 @@ -34,7 +34,7 @@ class W3DHub 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 do - window.settings.save_settings + Store.settings.save_settings end end end diff --git a/lib/states/boot.rb b/lib/states/boot.rb index e9c352e..82270ef 100644 --- a/lib/states/boot.rb +++ b/lib/states/boot.rb @@ -44,9 +44,9 @@ class W3DHub @progressbar.value = @fraction if @progressbar.value >= 1.0 && @task_index == @tasks.size - window.account = @account - window.service_status = @service_status - window.applications = @applications + Store.account = @account + Store.service_status = @service_status + Store.applications = @applications push_state(States::Interface) end @@ -61,18 +61,18 @@ class W3DHub end def refresh_user_token - if window.settings[:account, :refresh_token] + if Store.settings[:account, :refresh_token] Thread.new do - @account = Api.refresh_user_login(window.settings[:account, :refresh_token]) + @account = Api.refresh_user_login(Store.settings[:account, :refresh_token]) if @account - window.settings[:account][:refresh_token] = @account.refresh_token - window.settings.save_settings + Store.settings[:account][:refresh_token] = @account.refresh_token else - window.settings[:account][:refresh_token] = nil - window.settings.save_settings + Store.settings[:account][:refresh_token] = nil end + Store.settings.save_settings + @tasks[:refresh_user_token][:complete] = true end else diff --git a/lib/states/interface.rb b/lib/states/interface.rb index d788e00..23ad9c2 100644 --- a/lib/states/interface.rb +++ b/lib/states/interface.rb @@ -15,7 +15,7 @@ class W3DHub @main_thread_queue = [] - window.application_manager.auto_import + Store.application_manager.auto_import theme(W3DHub::THEME) @@ -118,7 +118,7 @@ class W3DHub end end - if window.account + if Store.account page(W3DHub::Pages::Login) else page(W3DHub::Pages::Games) diff --git a/lib/store.rb b/lib/store.rb new file mode 100644 index 0000000..c88176b --- /dev/null +++ b/lib/store.rb @@ -0,0 +1,27 @@ +class W3DHub + class Store + @@store = {} + + def self.get(*args) + @@store.get(*args) + end + + def self.[]=(key, value) + @@store[key] = value + end + + def self.[](key) + @@store[key] + end + + def self.method_missing(sym, *args) + if args.size == 1 + self[:"#{sym.to_s.sub('=', '')}"] = args.first + elsif args.size.zero? + self[sym] + else + raise "Only one argument suppported" + end + end + end +end diff --git a/lib/window.rb b/lib/window.rb index eb7cc15..9c4adfd 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -1,15 +1,12 @@ class W3DHub class Window < CyberarmEngine::Window - attr_reader :settings, :application_manager - attr_accessor :account, :service_status, :applications - def setup self.caption = I18n.t(:app_name) - @settings = Settings.new - @application_manager = ApplicationManager.new + Store[:settings] = Settings.new + Store[:application_manager] = ApplicationManager.new - @settings.save_settings + Store.settings.save_settings push_state(W3DHub::States::Boot) end @@ -17,14 +14,14 @@ class W3DHub def update super - @application_manager.start_next_available_task if @application_manager.idle? + Store.application_manager.start_next_available_task if Store.application_manager.idle? current_state.update_application_manager_status if current_state.is_a?(States::Interface) end def close - @settings.save_settings + Store.settings.save_settings - super if @application_manager.idle? + super if Store.application_manager.idle? end def main_thread_queue diff --git a/w3dhub.rb b/w3dhub.rb index 4f44059..3b72948 100644 --- a/w3dhub.rb +++ b/w3dhub.rb @@ -28,6 +28,7 @@ end require_relative "lib/version" require_relative "lib/theme" require_relative "lib/common" +require_relative "lib/store" require_relative "lib/window" require_relative "lib/cache" require_relative "lib/settings"