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

Instance Method Summary collapse

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

#data_dirString? Originally defined in module Sc2::Client::ConfigurableOptions

Override the path to find the data package.

Required if the binary is not in the standard versions folder location.

Launch param: -dataDir ../../

Returns:

  • (String, nil)

#display_mode0, ... Originally defined in module Sc2::Client::ConfigurableOptions

Launch param: -displayMode

Returns:

  • (0, 1, nil)

    0 for window, 1 for fullscreen, nil for system default

#egl_pathString? Originally defined in module Sc2::Client::ConfigurableOptions

Sets the path the to hardware rendering library.

Required for using the rendered interface with hardware rendering

Launch param: -eglpath

Example: /usr/lib/nvidia-384/libEGL.so

Returns:

  • (String, nil)

#hostString? Originally defined in module Sc2::Client::ConfigurableOptions

Sc2 host param on which to listen for connections, default ‘0.0.0.0’

Launch param: -host 0.0.0.0

Returns:

  • (String, nil)

#osmesa_pathString? Originally defined in module Sc2::Client::ConfigurableOptions

Sets the path the to software rendering library.

Required for using the rendered interface with software rendering

Launch param: -eglpath

Example: /usr/lib/x86_64-linux-gnu/libOSMesa.so

Returns:

  • (String, nil)

#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

#temp_dirString? Originally defined in module Sc2::Client::ConfigurableOptions

Override the path if you really need to set your own temp dir

Implicit default is /tmp/

Launch param: -tempDir ../../

Returns:

  • (String, nil)

#verboseBoolean Originally defined in module Sc2::Client::ConfigurableOptions

If set to true, will send param to client.

Enables logging of all protocol requests/responses to std::err.

Launch param: -verbose

Returns:

  • (Boolean)

#versionString? Originally defined in module Sc2::Client::ConfigurableOptions

Version number such as “4.10”. Leave blank to use latest

Returns:

  • (String, nil)

#windowheightInteger Originally defined in module Sc2::Client::ConfigurableOptions

pixel height of game window

Returns:

  • (Integer)

#windowwidthInteger Originally defined in module Sc2::Client::ConfigurableOptions

pixel width of game window

Returns:

  • (Integer)

#windowxInteger Originally defined in module Sc2::Client::ConfigurableOptions

left position of window if windowed

Returns:

  • (Integer)

#windowyInteger Originally defined in module Sc2::Client::ConfigurableOptions

top position of window if windowed

Returns:

  • (Integer)

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

#load_default_launch_optionsObject Originally defined in module Sc2::Client::ConfigurableOptions

Resets configurable launch options to their defaults

#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