Class: Sc2::Configuration

Inherits:
Object
  • Object
show all
Includes:
Sc2::Client::ConfigurableOptions
Defined in:
lib/sc2ai/configuration.rb

Overview

Global config manager for runtime

Examples:

Manual configuration block

Sc2.config do |config|
  config.sc2_platform = "WineLinux"
  config.sc2_path = "/c/Program Files (x86)/StarCraft II/"
  config.ports = [5001,5002,5003]
  config.host = '127.0.0.1'
end

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

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

Methods included from Sc2::Client::ConfigurableOptions

#load_default_launch_options

Constructor Details

#initializeSc2::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

#portsArray<Integer>

if empty, a random port will be picked when launching Launch param: -listen

Returns:



53
54
55
# File 'lib/sc2ai/configuration.rb', line 53

def ports
  @ports
end

#sc2_pathString

Sc2 Path config alias will override ENV

Returns:

  • (String)

    sc2_path (see Sc2::Paths#platform)



47
48
49
# File 'lib/sc2ai/configuration.rb', line 47

def sc2_path
  @sc2_path
end

#sc2_platformObject

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_filePathname

Config file location

Returns:

  • (Pathname)

    path



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

Parameters:

  • file (Pathname, String)

    location of config file

Returns:

  • (Boolean)

    success/failure



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_configvoid

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_defaultsvoid

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 = []

  load_default_launch_options
end

#to_hHash

Converts attributes to hash

Returns:

  • (Hash)

    hash matching stringified keys from CONFIG_ATTRIBUTES



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_yamlHash

Converts attributes to yaml

Returns:

  • (Hash)

    yaml matching stringified keys from CONFIG_ATTRIBUTES



93
94
95
# File 'lib/sc2ai/configuration.rb', line 93

def to_yaml
  to_h.to_yaml
end