Class: Sc2::Paths

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2ai/paths.rb

Overview

Helps determine common paths to sc2 install dir, executable and maps. It maintains some semblance of compatibility with python-sc2 config

ENV can be set manually to StarCraft 2 base directory for Linux ENV can and should be manually set to “WineLinux” when running Wine Credit to Hannes, Sean and Burny for setting the standard

Constant Summary collapse

PF_WINDOWS =

Platform name for Windows

"Windows"
PF_WSL1 =

Platform name for WSL1

"WSL1"
PF_WSL2 =

Platform name for WSL2

"WSL2"
PF_DARWIN =

Platform name macOS

"Darwin"
PF_LINUX =

Platform name Linux

"Linux"
PF_WINE =

Platform name for Wine

"WineLinux"
BASE_DIR =

Where within user directory to locate ExecuteInfo.txt

{
  PF_WINDOWS => "C:/Program Files (x86)/StarCraft II",
  PF_WSL1 => "/mnt/c/Program Files (x86)/StarCraft II",
  PF_WSL2 => "/mnt/c/Program Files (x86)/StarCraft II",
  PF_DARWIN => "/Applications/StarCraft II",
  PF_LINUX => "~/StarCraftII",
  PF_WINE => "~/.wine/drive_c/Program Files (x86)/StarCraft II"
}.freeze
EXEC_INFO_PATH =

Where within user directory to locate ExecuteInfo.txt

{
  PF_WINDOWS => "Documents/StarCraft II/ExecuteInfo.txt",
  PF_WSL1 => "Documents/StarCraft II/ExecuteInfo.txt",
  PF_WSL2 => "Documents/StarCraft II/ExecuteInfo.txt",
  PF_DARWIN => "Library/Application Support/Blizzard/StarCraft II/ExecuteInfo.txt",
  PF_LINUX => nil,
  PF_WINE => nil
}.freeze
BIN_PATH =

Path helper for finding executable

{
  PF_WINDOWS => "SC2_x64.exe",
  PF_WSL1 => "SC2_x64.exe",
  PF_WSL2 => "SC2_x64.exe",
  PF_DARWIN => "SC2.app/Contents/MacOS/SC2",
  PF_LINUX => "SC2_x64",
  PF_WINE => "SC2_x64.exe"
}.freeze
WORKING_DIR =

Working directory sub-folder per platform. Used when spawning client process on some platforms.

{
  PF_WINDOWS => "Support64",
  PF_WSL1 => "Support64",
  PF_WSL2 => "Support64",
  PF_DARWIN => nil,
  PF_LINUX => nil,
  PF_WINE => "Support64"
}.freeze

Class Method Summary collapse

Class Method Details

.bot_data_dirString

For local and ladder play, your files modified at runtime should live in ./data

Returns:

  • (String)

    path to ./data



177
178
179
180
181
# File 'lib/sc2ai/paths.rb', line 177

def bot_data_dir
  dir = Pathname("./data")
  dir.mkdir unless dir.exist?
  dir.to_s
end

.bot_data_replay_dirString

Returns the local replay folder For local play, your replay filse are saved to ./replays

Returns:

  • (String)

    path to ./replays



186
187
188
189
190
# File 'lib/sc2ai/paths.rb', line 186

def bot_data_replay_dir
  dir = Pathname("./replays")
  dir.mkdir unless dir.exist?
  dir.to_s
end

.exec_working_dirString?

Working directory if required by platform Some platforms the correct working directory for launch to find linked libraries

Returns:

  • (String, nil)

    string path or nil if not required



141
142
143
144
145
146
# File 'lib/sc2ai/paths.rb', line 141

def exec_working_dir
  cwd = WORKING_DIR[platform]
  return nil if cwd.nil?

  Pathname(install_dir).join(cwd).to_s
end

.executable(base_build: nil) ⇒ String?

Gets a path to latest Sc2 executable, or specific build’s executable if defined noinspection RubyMismatchedReturnType

Parameters:

  • base_build (Integer, nil) (defaults to: nil)

    version to use if installed

Returns:

  • (String, nil)

    path to executable



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sc2ai/paths.rb', line 152

def executable(base_build: nil)
  # Use base_build if supplied
  unless base_build.nil?
    path = Pathname.new(version_dir).join("Base#{base_build}")
    return path.join(BIN_PATH[platform]).to_s if path.exist?
  end
  # Get highest build number for folders /^Base\d$/
  pattern = /Base(\d+)$/
  path = Pathname.new(version_dir).glob("Base*")
    .max { |a, b| a.to_s.match(pattern)[1] <=> b.to_s.match(pattern)[1] }
  return path.join(BIN_PATH[platform]).to_s if path&.exist?

  nil
end

.gem_data_versions_pathPathname

Path to shipped versions.json

Returns:

  • (Pathname)

    path



209
210
211
# File 'lib/sc2ai/paths.rb', line 209

def gem_data_versions_path
  Pathname.new(gem_root).join("data", "versions.json")
end

.gem_rootPathname

Root directory of gem itself for other bundled files

Returns:

  • (Pathname)

    path to gem folder which contains lib/,data/,etc.



195
196
197
# File 'lib/sc2ai/paths.rb', line 195

def gem_root
  Pathname.new(__dir__.to_s).parent.parent.expand_path
end

.generate_temp_dirString

Gets system temp directory and creates /sc2ai

Returns:

  • (String)

    temporary directory for use with -tempDir



215
216
217
218
# File 'lib/sc2ai/paths.rb', line 215

def generate_temp_dir
  temp_dir = Pathname(Dir.tmpdir).join("sc2ai").mkpath
  temp_dir.to_s
end

.install_dirString

Attempts to auto-detect the user’s install directory via ExecuteInfo.txt

SC2PATH is required on WineLinux and Linux

Returns:

  • (String)

    path



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sc2ai/paths.rb', line 101

def install_dir
  if ENV.fetch("SC2PATH", false)
    # Use if set via environment
    path = ENV.fetch("SC2PATH")
  else
    # Otherwise try read from ExecuteInfo.txt
    path = read_exec_info
    if path.to_s.strip.empty?
      # If not present in ExecuteInfo, try use sensible default
      path = BASE_DIR[platform]
    end
  end
  path = File.expand_path(path) if platform != PF_WINDOWS

  ENV["SC2PATH"] = path
end

.maps_dirString

Maps directory based on install_dir

Returns:

  • (String)

    maps directory



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sc2ai/paths.rb', line 126

def maps_dir
  # Use Maps if exists, or as fallback if alternative not found
  dir = Pathname(install_dir).join("Maps")
  return dir.to_s if dir.exist?

  # Use lowercase "maps" only if it exists
  dir_alt = Pathname(install_dir).join("maps")
  dir = dir_alt if dir_alt.exist?

  dir.to_s
end

.platform"Windows", ...

Bucketed platform names follows convention Uses ENV if configured. This is the only way to set “WineLinux”

Returns:

  • ("Windows", "WSL1", "WSL2", "Darwin", "Linux", "WineLinux")

    string platform name



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sc2ai/paths.rb', line 78

def platform
  return ENV.fetch("SC2PF", nil) unless ENV["SC2PF"].to_s.strip.empty?

  if Gem.win_platform?
    ENV["SC2PF"] = PF_WINDOWS
  elsif Gem::Platform.local.os == "darwin"
    ENV["SC2PF"] = PF_DARWIN
  elsif Gem::Platform.local.os == "linux"
    ENV["SC2PF"] = PF_LINUX
    if wsl?
      ENV["SC2PF"] = wsl2? ? PF_WSL2 : PF_WSL1
    end
  end

  unless ENV.fetch("SC2PF", false)
    Sc2.logger.warn "unknown platform detected #{Gem::Platform.local.os}. manually set ENV['SC2PF']"
  end
  ENV.fetch("SC2PF", nil)
end

.platformsArray<String>

An array of available platforms

Returns:

  • (Array<String>)

    an array of valid platforms



71
72
73
# File 'lib/sc2ai/paths.rb', line 71

def platforms
  [PF_WINDOWS, PF_WSL1, PF_WSL2, PF_DARWIN, PF_LINUX, PF_WINE]
end

.project_root_dirString

Returns project root directory

Returns:

  • (String)

    path because bundler does.



169
170
171
172
173
# File 'lib/sc2ai/paths.rb', line 169

def project_root_dir
  return Bundler.root.to_s if defined?(Bundler)

  Dir.pwd
end

.replay_dirString

Replays directory based on install_dir

Returns:

  • (String)

    replays directory



120
121
122
# File 'lib/sc2ai/paths.rb', line 120

def replay_dir
  Pathname(install_dir).join("Replays").to_s
end

.template_rootPathname

Directory of template folders

Returns:

  • (Pathname)

    path to where template directories live, i.e. “new” and “ladder”



202
203
204
# File 'lib/sc2ai/paths.rb', line 202

def template_root
  Pathname.new(__dir__.to_s).parent.join("templates").expand_path
end

.win_to_wsl(path:) ⇒ String

Convert a path like “C:\path\To a\Location” to “/mnt/c/path/To a/Location”

Parameters:

  • path (String)

Returns:

  • (String)

    path converted



235
236
237
# File 'lib/sc2ai/paths.rb', line 235

def win_to_wsl(path:)
  "/mnt/" + path.sub(/\A([A-Z])(:)/) { ::Regexp.last_match(1).to_s.downcase }.tr("\\", "/")
end

.wsl2?Boolean

Checks if running WSL2 specifically on top of wsl? being true

Returns:

  • (Boolean)


228
229
230
# File 'lib/sc2ai/paths.rb', line 228

def wsl2?
  wsl? && ENV.fetch("WSL_INTEROP", false)
end

.wsl?Boolean

Checks if running WSL

Returns:

  • (Boolean)


222
223
224
# File 'lib/sc2ai/paths.rb', line 222

def wsl?
  Gem::Platform.local.os == "linux" && (ENV.fetch("IS_WSL", false) || ENV.fetch("WSL_DISTRO_NAME", false))
end

.wsl_to_win(path:) ⇒ String

Convert a path like “/mnt/c/path/To a/Location” to “C:\path\To a\Location”

Parameters:

  • path (String)

Returns:

  • (String)

    path converted



242
243
244
# File 'lib/sc2ai/paths.rb', line 242

def wsl_to_win(path:)
  path.sub(%r{\A/mnt/([a-z])}) { "#{::Regexp.last_match(1).to_s.upcase}:" }.tr("/", "\\")
end