mirror of
https://github.com/TimeCrafters/timecrafters_configuration_tool_desktop.git
synced 2025-12-13 04:22:35 +00:00
Seperated hostname and port config data from Config into Settings
This commit is contained in:
0
data/configs/.gitkeep
Normal file
0
data/configs/.gitkeep
Normal file
@@ -1,11 +1,13 @@
|
||||
module TAC
|
||||
class Backend
|
||||
attr_reader :config, :tacnet
|
||||
attr_reader :config, :settings, :tacnet
|
||||
def initialize
|
||||
@config = load_config
|
||||
@settings = load_settings
|
||||
@tacnet = TACNET.new
|
||||
|
||||
@config_changed = false
|
||||
@settings_changed = false
|
||||
end
|
||||
|
||||
def config_changed!
|
||||
@@ -82,5 +84,44 @@ module TAC
|
||||
def refresh_tacnet_status
|
||||
$window.current_state.refresh_tacnet_status
|
||||
end
|
||||
|
||||
|
||||
def settings_changed!
|
||||
@settings_changed = true
|
||||
end
|
||||
|
||||
def settings_changed?
|
||||
@settings_changed
|
||||
end
|
||||
|
||||
def load_settings
|
||||
if File.exist?(TAC::SETTINGS_PATH)
|
||||
return TAC::Settings.new
|
||||
else
|
||||
write_default_settings
|
||||
load_settings
|
||||
end
|
||||
end
|
||||
|
||||
def save_settings
|
||||
json = @settings.to_json
|
||||
|
||||
File.open(TAC::SETTINGS_PATH, "w") { |f| f.write json }
|
||||
|
||||
@settings_changed = false
|
||||
end
|
||||
|
||||
def write_default_settings
|
||||
File.open(TAC::SETTINGS_PATH, "w") do |f|
|
||||
f.write JSON.dump(
|
||||
{
|
||||
data: {
|
||||
hostname: TACNET::DEFAULT_HOSTNAME,
|
||||
port: TACNET::DEFAULT_PORT,
|
||||
}
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -38,12 +38,11 @@ module TAC
|
||||
end
|
||||
|
||||
class Configuration
|
||||
attr_accessor :created_at, :updated_at, :spec_version, :hostname, :port
|
||||
attr_accessor :created_at, :updated_at, :spec_version
|
||||
attr_reader :presets
|
||||
def initialize(created_at:, updated_at:, spec_version:, hostname:, port:, presets:)
|
||||
def initialize(created_at:, updated_at:, spec_version:, presets:)
|
||||
@created_at, @updated_at = created_at, updated_at
|
||||
@spec_version = spec_version
|
||||
@hostname, @port = hostname, port
|
||||
@presets = presets
|
||||
end
|
||||
|
||||
@@ -52,8 +51,6 @@ module TAC
|
||||
created_at: @created_at,
|
||||
updated_at: @updated_at,
|
||||
spec_version: @spec_version,
|
||||
hostname: @hostname,
|
||||
port: @port,
|
||||
presets: @presets
|
||||
}.to_json(*args)
|
||||
end
|
||||
@@ -61,8 +58,7 @@ module TAC
|
||||
def self.from_json(hash)
|
||||
Configuration.new(
|
||||
created_at: hash[:created_at], updated_at: hash[:updated_at],
|
||||
spec_version: hash[:spec_version], hostname: hash[:hostname],
|
||||
port: hash[:port], presets: hash[:presets].map { |ps| Preset.from_json(ps) }
|
||||
spec_version: hash[:spec_version], presets: hash[:presets].map { |ps| Preset.from_json(ps) }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
24
lib/settings.rb
Normal file
24
lib/settings.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module TAC
|
||||
class Settings
|
||||
attr_accessor :hostname, :port
|
||||
def initialize
|
||||
parse(File.read(TAC::SETTINGS_PATH))
|
||||
end
|
||||
|
||||
def parse(json)
|
||||
data = JSON.parse(json, symbolize_names: true)
|
||||
|
||||
@hostname = data[:data][:hostname]
|
||||
@port = data[:data][:port]
|
||||
end
|
||||
|
||||
def to_json(*args)
|
||||
{
|
||||
data: {
|
||||
hostname: @hostname,
|
||||
port: @port,
|
||||
}
|
||||
}.to_json(*args)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -32,8 +32,9 @@ module TAC
|
||||
button get_image("#{TAC::ROOT_PATH}/media/icons/menuList.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Manage presets" do
|
||||
push_state(ManagePresets)
|
||||
end
|
||||
button get_image("#{TAC::ROOT_PATH}/media/icons/save.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Save config to disk" do
|
||||
button get_image("#{TAC::ROOT_PATH}/media/icons/save.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Save config and settings to disk" do
|
||||
window.backend.save_config
|
||||
window.backend.save_settings
|
||||
end
|
||||
button get_image("#{TAC::ROOT_PATH}/media/icons/export.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Upload local config to remote, if connected." do
|
||||
window.backend.upload_config
|
||||
@@ -49,18 +50,18 @@ module TAC
|
||||
stack width: 0.5 do
|
||||
label "TACNET v#{TACNET::Packet::PROTOCOL_VERSION}", color: TAC::Palette::TACNET_PRIMARY, text_shadow: true, text_shadow_size: 1, text_shadow_color: Gosu::Color::BLACK
|
||||
flow width: 1.0 do
|
||||
@tacnet_hostname = edit_line "#{window.backend.config.configuration.hostname}", width: 0.5, margin_right: 0
|
||||
@tacnet_hostname = edit_line "#{window.backend.settings.hostname}", width: 0.5, margin_right: 0
|
||||
@tacnet_hostname.subscribe(:changed) do |caller, value|
|
||||
window.backend.config.configuration.hostname = value
|
||||
window.backend.config_changed!
|
||||
window.backend.settings.hostname = value
|
||||
window.backend.settings_changed!
|
||||
end
|
||||
|
||||
label ":", margin: 0, padding: 0, padding_top: 3
|
||||
|
||||
@tacnet_port = edit_line "#{window.backend.config.configuration.port}", width: 0.2, margin_left: 0
|
||||
@tacnet_port = edit_line "#{window.backend.settings.port}", width: 0.2, margin_left: 0
|
||||
@tacnet_port.subscribe(:changed) do |caller, value|
|
||||
window.backend.config.configuration.port = Integer(value)
|
||||
window.backend.config_changed!
|
||||
window.backend.settings.port = Integer(value)
|
||||
window.backend.settings_changed!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
module TAC
|
||||
ROOT_PATH = File.expand_path("../..", __FILE__)
|
||||
CONFIG_PATH = "#{ROOT_PATH}/data/config.json"
|
||||
SETTINGS_PATH = "#{ROOT_PATH}/data/settings.json"
|
||||
|
||||
CONFIG_SPEC_VERSION = 2
|
||||
end
|
||||
@@ -15,7 +15,9 @@ module TAC
|
||||
|
||||
def close
|
||||
if @backend.config_changed?
|
||||
push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Config has unsaved changes!", callback_method: proc { cleanup_and_close })
|
||||
push_state(Dialog::ConfirmDialog, title: "Unsaved Config", message: "Config has unsaved changes\nthat will be lost if\nyou continue!", callback_method: proc { cleanup_and_close })
|
||||
elsif @backend.settings_changed?
|
||||
push_state(Dialog::ConfirmDialog, title: "Unsaved Settings", message: "Settings has unsaved changes\nthat will be lost if\nyou continue!", callback_method: proc { cleanup_and_close })
|
||||
else
|
||||
cleanup_and_close
|
||||
end
|
||||
|
||||
@@ -9,6 +9,7 @@ require_relative "lib/window"
|
||||
require_relative "lib/version"
|
||||
require_relative "lib/backend"
|
||||
require_relative "lib/config"
|
||||
require_relative "lib/settings"
|
||||
require_relative "lib/states/boot"
|
||||
require_relative "lib/states/editor"
|
||||
require_relative "lib/states/simulator"
|
||||
|
||||
Reference in New Issue
Block a user