Class: Sc2::Configuration
- Inherits:
-
Object
- Object
- Sc2::Configuration
- Includes:
- Sc2::Client::ConfigurableOptions
- Defined in:
- lib/sc2ai/configuration.rb
Overview
Global config manager for runtime
Constant Summary collapse
- CONFIG_ATTRIBUTES =
Attributes permitted to be read and save from config yaml
%i[ sc2_platform sc2_path version ports host display_mode windowwidth windowheight windowx windowy verbose data_dir temp_dir egl_path osmesa_path ].freeze
Instance Attribute Summary collapse
-
#ports ⇒ Array<Integer>
if empty, a random port will be picked when launching Launch param: -listen.
-
#sc2_path ⇒ String
Sc2 Path config alias will override ENV.
-
#sc2_platform ⇒ Object
Sc2 platform config alias will override ENV @return [String] (see Sc2::Paths#platform).
Attributes included from Sc2::Client::ConfigurableOptions
#data_dir, #display_mode, #egl_path, #host, #osmesa_path, #temp_dir, #verbose, #version, #windowheight, #windowwidth, #windowx, #windowy
Instance Method Summary collapse
-
#config_file ⇒ Pathname
Config file location.
-
#initialize ⇒ Sc2::Configuration
constructor
Create a new Configuration and sets defaults and loads config from yaml.
-
#load_config(file) ⇒ Boolean
Loads YAML config.
-
#save_config ⇒ void
Writes this instance’s attributes to yaml config_file.
-
#set_defaults ⇒ void
Sets defaults when initializing.
-
#to_h ⇒ Hash
Converts attributes to hash.
-
#to_yaml ⇒ Hash
Converts attributes to yaml.
Methods included from Sc2::Client::ConfigurableOptions
Constructor Details
#initialize ⇒ Sc2::Configuration
Create a new Configuration and sets defaults and loads config from yaml
57 58 59 60 61 62 63 64 |
# File 'lib/sc2ai/configuration.rb', line 57 def initialize set_defaults load_config(config_file) if config_file.exist? # create temp dir on linux ensure_temp_dir end |
Instance Attribute Details
#ports ⇒ Array<Integer>
if empty, a random port will be picked when launching Launch param: -listen
53 54 55 |
# File 'lib/sc2ai/configuration.rb', line 53 def ports @ports end |
#sc2_path ⇒ String
Sc2 Path config alias will override ENV
47 48 49 |
# File 'lib/sc2ai/configuration.rb', line 47 def sc2_path @sc2_path end |
#sc2_platform ⇒ Object
Sc2 platform config alias will override ENV
@return [String] (see Sc2::Paths#platform)
42 43 44 |
# File 'lib/sc2ai/configuration.rb', line 42 def sc2_platform @sc2_platform end |
Instance Method Details
#config_file ⇒ Pathname
Config file location
68 69 70 71 |
# File 'lib/sc2ai/configuration.rb', line 68 def config_file # Pathname(Paths.project_root_dir).join("config", "sc2ai.yml") Pathname(Paths.project_root_dir).join("sc2ai.yml") end |
#load_config(file) ⇒ Boolean
Loads YAML config
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/sc2ai/configuration.rb', line 108 def load_config(file) file = Pathname(file) unless file.is_a? Pathname return false if !file.exist? || file.size.nil? begin content = ::Psych.safe_load(file.read) unless content.is_a? Hash Sc2.logger.warn "Failed to load #{file} because it doesn't contain valid YAML hash" return false end CONFIG_ATTRIBUTES.map(&:to_s).each do |attribute| next unless content.key?(attribute.to_s) instance_variable_set(:"@#{attribute}", content[attribute]) end return true rescue ArgumentError, Psych::SyntaxError => e Sc2.logger.warn "Failed to load #{file}, #{e}" rescue Errno::EACCES Sc2.logger.warn "Failed to load #{file} due to permissions problem." end false end |
#save_config ⇒ void
This method returns an undefined value.
Writes this instance’s attributes to yaml config_file
85 86 87 88 89 |
# File 'lib/sc2ai/configuration.rb', line 85 def save_config # config_file.dirname.mkpath unless config_file.dirname.exist? config_file.write(to_yaml.to_s) nil end |
#set_defaults ⇒ void
This method returns an undefined value.
Sets defaults when initializing
75 76 77 78 79 80 81 |
# File 'lib/sc2ai/configuration.rb', line 75 def set_defaults @sc2_platform = Paths.platform @sc2_path = Paths.install_dir @ports = [] end |
#to_h ⇒ Hash
Converts attributes to hash
99 100 101 102 103 |
# File 'lib/sc2ai/configuration.rb', line 99 def to_h CONFIG_ATTRIBUTES.map do |name| [name.to_s, instance_variable_get(:"@#{name}")] end.to_h end |
#to_yaml ⇒ Hash
Converts attributes to yaml
93 94 95 |
# File 'lib/sc2ai/configuration.rb', line 93 def to_yaml to_h.to_yaml end |