Class: Sc2::Paths
- Inherits:
-
Object
- Object
- Sc2::Paths
- 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
-
.bot_data_dir ⇒ String
For local and ladder play, your files modified at runtime should live in ./data.
-
.bot_data_replay_dir ⇒ String
Returns the local replay folder For local play, your replay filse are saved to ./replays.
-
.exec_working_dir ⇒ String?
Working directory if required by platform Some platforms the correct working directory for launch to find linked libraries.
-
.executable(base_build: nil) ⇒ String?
Gets a path to latest Sc2 executable, or specific build’s executable if defined noinspection RubyMismatchedReturnType.
-
.gem_data_versions_path ⇒ Pathname
Path to shipped versions.json.
-
.gem_root ⇒ Pathname
Root directory of gem itself for other bundled files.
-
.generate_temp_dir ⇒ String
Gets system temp directory and creates /sc2ai.
-
.install_dir ⇒ String
Attempts to auto-detect the user’s install directory via ExecuteInfo.txt SC2PATH is required on WineLinux and Linux.
-
.maps_dir ⇒ String
Maps directory based on install_dir.
-
.platform ⇒ String
Bucketed platform names follows convention Uses ENV if configured.
-
.platforms ⇒ Array<String>
An array of available platforms.
-
.project_root_dir ⇒ String
Returns project root directory.
-
.replay_dir ⇒ String
Replays directory based on install_dir.
-
.template_root ⇒ Pathname
Directory of template folders.
-
.win_to_wsl(path:) ⇒ String
Convert a path like “C:\path\To a\Location” to “/mnt/c/path/To a/Location”.
-
.wsl2? ⇒ Boolean
Checks if running WSL2 specifically on top of wsl? being true.
-
.wsl? ⇒ Boolean
Checks if running WSL.
-
.wsl_to_win(path:) ⇒ String
Convert a path like “/mnt/c/path/To a/Location” to “C:\path\To a\Location”.
Class Method Details
.bot_data_dir ⇒ String
For local and ladder play, your files modified at runtime should live in ./data
187 188 189 190 191 |
# File 'lib/sc2ai/paths.rb', line 187 def bot_data_dir dir = Pathname("./data") dir.mkdir unless dir.exist? dir.to_s end |
.bot_data_replay_dir ⇒ String
Returns the local replay folder For local play, your replay filse are saved to ./replays
196 197 198 199 200 |
# File 'lib/sc2ai/paths.rb', line 196 def bot_data_replay_dir dir = Pathname("./replays") dir.mkdir unless dir.exist? dir.to_s end |
.exec_working_dir ⇒ String?
Working directory if required by platform Some platforms the correct working directory for launch to find linked libraries
151 152 153 154 155 156 |
# File 'lib/sc2ai/paths.rb', line 151 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
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sc2ai/paths.rb', line 162 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_path ⇒ Pathname
Path to shipped versions.json
219 220 221 |
# File 'lib/sc2ai/paths.rb', line 219 def gem_data_versions_path Pathname.new(gem_root).join("data", "versions.json") end |
.gem_root ⇒ Pathname
Root directory of gem itself for other bundled files
205 206 207 |
# File 'lib/sc2ai/paths.rb', line 205 def gem_root Pathname.new(__dir__.to_s).parent.parent. end |
.generate_temp_dir ⇒ String
Gets system temp directory and creates /sc2ai
225 226 227 228 |
# File 'lib/sc2ai/paths.rb', line 225 def generate_temp_dir temp_dir = Pathname(Dir.tmpdir).join("sc2ai").mkpath temp_dir.to_s end |
.install_dir ⇒ String
Attempts to auto-detect the user’s install directory via ExecuteInfo.txt
SC2PATH is required on WineLinux and Linux
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sc2ai/paths.rb', line 111 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.(path) if platform != PF_WINDOWS ENV["SC2PATH"] = path end |
.maps_dir ⇒ String
Maps directory based on install_dir
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sc2ai/paths.rb', line 136 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 ⇒ String
Bucketed platform names follows convention Uses ENV if configured. This is the only way to set “WineLinux”
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sc2ai/paths.rb', line 88 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 |
.platforms ⇒ Array<String>
An array of available platforms
81 82 83 |
# File 'lib/sc2ai/paths.rb', line 81 def platforms [PF_WINDOWS, PF_WSL1, PF_WSL2, PF_DARWIN, PF_LINUX, PF_WINE] end |
.project_root_dir ⇒ String
Returns project root directory
179 180 181 182 183 |
# File 'lib/sc2ai/paths.rb', line 179 def project_root_dir return Bundler.root.to_s if defined?(Bundler) Dir.pwd end |
.replay_dir ⇒ String
Replays directory based on install_dir
130 131 132 |
# File 'lib/sc2ai/paths.rb', line 130 def replay_dir Pathname(install_dir).join("Replays").to_s end |
.template_root ⇒ Pathname
Directory of template folders
212 213 214 |
# File 'lib/sc2ai/paths.rb', line 212 def template_root Pathname.new(__dir__.to_s).parent.join("templates"). end |
.win_to_wsl(path:) ⇒ String
Convert a path like “C:\path\To a\Location” to “/mnt/c/path/To a/Location”
245 246 247 |
# File 'lib/sc2ai/paths.rb', line 245 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
238 239 240 |
# File 'lib/sc2ai/paths.rb', line 238 def wsl2? wsl? && ENV.fetch("WSL_INTEROP", false) end |
.wsl? ⇒ Boolean
Checks if running WSL
232 233 234 |
# File 'lib/sc2ai/paths.rb', line 232 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”
252 253 254 |
# File 'lib/sc2ai/paths.rb', line 252 def wsl_to_win(path:) path.sub(%r{\A/mnt/([a-z])}) { "#{::Regexp.last_match(1).to_s.upcase}:" }.tr("/", "\\") end |