Removed window as central storage, added Store class to be central memory store

This commit is contained in:
2021-11-22 20:38:24 -06:00
parent 3a269cbaae
commit a4dd375511
13 changed files with 109 additions and 85 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 "<b>#{I18n.t(:"interface.play_now")}</b>", margin_left: 24 do
window.application_manager.run(game.id, channel.id)
Store.application_manager.run(game.id, channel.id)
end
button "<b>#{I18n.t(:"interface.single_player")}</b>", 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 "<b>#{I18n.t(:"interface.install")}</b>", margin_left: 24, enabled: !window.application_manager.task?(:installer, game.id, channel.id) do |button|
button "<b>#{I18n.t(:"interface.install")}</b>", 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 "<b>#{I18n.t(:"interface.import")}</b>", 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

View File

@@ -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 "<b>#{window.account.username}</b>"
tagline "<b>#{Store.account.username}</b>"
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

View File

@@ -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 "<b>#{I18n.t(:"server_browser.join_server")}</b>", 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: "?")

View File

@@ -14,7 +14,7 @@ class W3DHub
para "<b>App Install Folder</b>", 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 "<b>Package Cache Folder</b>", 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

View File

@@ -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

View File

@@ -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)

27
lib/store.rb Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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"