Skip to content
Merged
15 changes: 14 additions & 1 deletion src/main/kotlin/config/WurstProjectConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import logging.KotlinLogging
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.InvalidPathException
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
/**
Expand Down Expand Up @@ -156,7 +157,20 @@ object WurstProjectConfig {
/** The VS Code extension's `wurst.customMapDataPath`, which the language server's hot reload also uses. */
fun configuredCustomMapDataPath(projectRoot: Path): Path? = configuredPath(projectRoot, "wurst.customMapDataPath")

/** The VS Code extension's `wurst.wc3LaunchMode` (auto, battlenet or legacy), which Run Map also follows. */
fun configuredLaunchMode(projectRoot: Path): String = configuredString(projectRoot, "wurst.wc3LaunchMode") ?: "auto"

/** An optional setting: a path the system cannot represent counts as unset, so callers fall back as before. */
private fun configuredPath(projectRoot: Path, setting: String): Path? {
val value = configuredString(projectRoot, setting) ?: return null
return try {
Paths.get(value)
} catch (_: InvalidPathException) {
null
}
}

private fun configuredString(projectRoot: Path, setting: String): String? {
val settings = projectRoot.resolve(".vscode").resolve("settings.json")
if (!Files.isRegularFile(settings)) {
return null
Expand All @@ -166,7 +180,6 @@ object WurstProjectConfig {
?.get(setting)
?.asText()
?.takeIf(String::isNotBlank)
?.let(Paths::get)
} catch (_: Exception) {
null
}
Expand Down
321 changes: 321 additions & 0 deletions src/main/kotlin/file/BattleNetSignIn.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
package file

import java.io.IOException
import java.net.BindException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.ServerSocket
import java.nio.ByteBuffer
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.security.MessageDigest
import java.util.Locale
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

/**
* Renews the sign-in that Warcraft III 3.x uses for `-editor` launches, from the Battle.net desktop app.
*
* A client started with `-editor` (every `grill run` of a Reforged client) signs in with `EDITOR_TOKEN`
* (`HKCU\Software\Blizzard Entertainment\Warcraft III`) and replaces it with a fresh one each time it signs in. Once
* that token lapses (overnight is enough) every launch stops at the in-game login panel. The app's own session does
* not reach it: a client the app starts with `-loadfile` shows the login panel too.
*
* Just before the app starts the game it writes a one-time `WEB_TOKEN` (`...\Battle.net\Launch Options\W3`). Stored
* as `EDITOR_TOKEN`, that token signs the next `-editor` client in. [renew] asks the app to start the game, closes
* that client before it spends the token, and stores the token; the map is then launched as before.
*
* WurstScript's Run Map (`BattleNetSignIn`) and the wc3-e2e-harness (`docs/BattleNetSignIn.md`) do the same, and all
* hold the same localhost port as a lock from the renewal until the launched client has signed in.
*/
/** What [Wc3MapRunner] needs from the Battle.net app; replaced in tests. */
internal interface BattleNetApp {
fun isRunning(): Boolean

/** @param onWait called once if another launch holds the lock and this one has to wait for it */
fun renew(onWait: () -> Unit = {}): BattleNetSignIn.Renewal
}

internal class BattleNetSignIn private constructor(private val config: Path, private val launcher: Path) : BattleNetApp {

/**
* Another tool holds the renewal lock for longer than a renewal and a sign-in take. Not a reason to launch
* without it: that launch could spend the token the other tool's client has not read yet.
*/
class LockBusyException(message: String) : IOException(message)

/** The lock, held until the launched client has signed in; see [awaitSignIn]. */
class Renewal internal constructor(
private val gate: ServerSocket,
private val token: String,
private val currentToken: () -> String? = { readBinary(GAME_KEY, "EDITOR_TOKEN") },
private val pollMillis: Long = 500,
) : AutoCloseable {
/**
* Blocks until the launched client replaces `EDITOR_TOKEN`, which it does as it signs in (about ten seconds after
* launch), or [SIGN_IN_TIMEOUT_SECONDS] pass, then releases the lock. Released earlier, another tool's renewal
* could replace the token before this client read it, leaving two clients one token.
*/
fun awaitSignIn(): Boolean {
val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(SIGN_IN_TIMEOUT_SECONDS)
try {
while (System.nanoTime() < deadline) {
if (currentToken() != token) return true
Thread.sleep(pollMillis)
}
return false
} finally {
close()
}
}

override fun close() {
try {
gate.close()
} catch (_: IOException) {
}
}
}

override fun isRunning(): Boolean = ProcessHandle.allProcesses().anyMatch { named(command(it), LAUNCHER) }

override fun renew(onWait: () -> Unit): Renewal = renew(GATE_WAIT_SECONDS, onWait)

/**
* Stores a token from the app as `EDITOR_TOKEN` and returns the held lock. Throws, with a message for the user,
* when the app cannot provide one: it is closed, signed out, updating, or running a client of its own.
*/
fun renew(gateWaitSeconds: Long, onWait: () -> Unit = {}): Renewal {
val gate = acquireGate(gateWaitSeconds, onWait)
try {
return Renewal(gate, renewLocked())
} catch (e: Throwable) {
gate.close()
throw e
}
}

private fun renewLocked(): String {
if (!isRunning()) throw IOException("Battle.net is not running.")
val seen = HashSet<Long>()
for (game in games()) {
if (startedByLauncher(game)) {
throw IOException("Close the Warcraft III client started from Battle.net, then run the map again.")
}
seen.add(game.pid())
}
val before = readBinary(LAUNCH_KEY, "WEB_TOKEN")
// Asked to start the game, the app shows its window in front; the watcher minimizes it as soon as it appears
// and brings the map's client forward once that has a window.
watchLauncherWindow()
val start = System.nanoTime()
var askedAt = Long.MIN_VALUE
var token: String? = null
var tokenAt = 0L
var closed = 0
while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(TIMEOUT_SECONDS)) {
if (token == null && (askedAt == Long.MIN_VALUE
|| System.nanoTime() - askedAt >= TimeUnit.SECONDS.toNanos(ASK_AGAIN_SECONDS))) {
askedAt = System.nanoTime()
// An app that is still starting ignores the command, so it is repeated while waiting.
ProcessBuilder(launcher.toString(), "--exec=launch W3")
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectError(ProcessBuilder.Redirect.DISCARD)
.start()
}
for (game in games()) {
if (!seen.add(game.pid()) || !startedByLauncher(game)) continue
game.destroyForcibly()
closed++
}
if (token == null) {
val now = readBinary(LAUNCH_KEY, "WEB_TOKEN")
if (now != null && now != before) {
token = now
tokenAt = System.nanoTime()
}
}
// The app writes the token, then starts the game: wait briefly for that game so it is closed.
if (token != null && (closed > 0
|| System.nanoTime() - tokenAt >= TimeUnit.SECONDS.toNanos(GAME_AFTER_TOKEN_SECONDS))) break
Thread.sleep(50)
}
if (token == null) {
throw IOException("Battle.net did not start Warcraft III. Check that it is signed in and the game is ready to play.")
}
writeBinary(GAME_KEY, "EDITOR_TOKEN", token)
return token
}

private fun acquireGate(waitSeconds: Long, onWait: () -> Unit): ServerSocket {
val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(waitSeconds)
var waiting = false
while (true) {
val socket = ServerSocket()
try {
// 127.0.0.1 exactly, as the harness binds it; the loopback name could resolve to ::1.
socket.bind(InetSocketAddress(InetAddress.getByAddress(byteArrayOf(127, 0, 0, 1)), gatePort(config)))
return socket
} catch (e: BindException) {
socket.close()
if (!waiting) {
waiting = true
onWait()
}
if (System.nanoTime() >= deadline) {
throw LockBusyException("Another Warcraft III launch has held the Battle.net sign-in for over $waitSeconds s.")
}
Thread.sleep(250)
} catch (e: IOException) {
socket.close()
throw e
}
}
}

companion object {
const val LAUNCH_KEY = "HKCU\\Software\\Blizzard Entertainment\\Battle.net\\Launch Options\\W3"
const val GAME_KEY = "HKCU\\Software\\Blizzard Entertainment\\Warcraft III"
private const val LAUNCHER = "Battle.net.exe"
private const val GAME = "Warcraft III.exe"
private const val TIMEOUT_SECONDS = 90L
private const val ASK_AGAIN_SECONDS = 25L
private const val GAME_AFTER_TOKEN_SECONDS = 10L
const val SIGN_IN_TIMEOUT_SECONDS = 120L
/** Another holder renews (up to [TIMEOUT_SECONDS]) and waits for its client's sign-in; outwait both. */
const val GATE_WAIT_SECONDS = TIMEOUT_SECONDS + SIGN_IN_TIMEOUT_SECONDS + 30

/** A renewal for [config] whose launcher is never started; for tests that only touch the lock. */
internal fun forTest(config: Path): BattleNetSignIn = BattleNetSignIn(config, Paths.get(LAUNCHER))
private val REG_BINARY = Regex("""^\s*(\S+)\s+REG_BINARY\s+([0-9A-Fa-f]*)\s*$""", RegexOption.MULTILINE)

/** A copy of the wc3-e2e-harness `node/src/runner/battlenet-window.ps1`; keep the two equal. */
const val WINDOW_SCRIPT = "battlenet-window.ps1"
/** The watcher's limit without a client: every request the renewal may send, then a launch and a slow sign-in. */
const val WINDOW_WATCH_SECONDS = TIMEOUT_SECONDS + 150
private const val WINDOW_READY_SECONDS = 3L

/** The PowerShell command that runs [WINDOW_SCRIPT] with [arguments]; null if the script is missing. */
fun windowWatcherCommand(arguments: List<String>, dir: Path = Paths.get(System.getProperty("java.io.tmpdir"))): List<String>? {
val script = BattleNetSignIn::class.java.classLoader.getResourceAsStream(WINDOW_SCRIPT)?.use { it.readBytes() }
?: return null
// A file, not -EncodedCommand: encoded, the script is close to Windows' 32,767-character command line limit.
// Named by content, so a grill of another version never runs this one's copy.
val hash = MessageDigest.getInstance("SHA-256").digest(script).take(6).joinToString("") { "%02x".format(it) }
val file = dir.resolve("grill-battlenet-window-$hash.ps1")
try {
if (!Files.isRegularFile(file) || !Files.readAllBytes(file).contentEquals(script)) Files.write(file, script)
} catch (_: IOException) {
return null
}
return listOf("powershell.exe", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", file.toString()) + arguments
}

/**
* Starts the window watcher and waits until it has noted the windows already open (at most
* [WINDOW_READY_SECONDS]). It then runs on its own: the app shows its window again as the map's client signs in,
* so it ends a minute after that client appears, or after [WINDOW_WATCH_SECONDS]. Best effort: a watcher that does
* not start only means the app's window may show.
*/
private fun watchLauncherWindow() {
val command = windowWatcherCommand(listOf("-Seconds", WINDOW_WATCH_SECONDS.toString(), "-FocusGame")) ?: return
val process = try {
ProcessBuilder(command).redirectError(ProcessBuilder.Redirect.DISCARD).start()
} catch (_: IOException) {
return
}
val ready = CountDownLatch(1)
Thread {
try {
// The watcher prints nothing after "ready", so its output can be let go then.
process.inputReader().use { reader ->
while (true) {
val line = reader.readLine() ?: break
if (line.trim() == "ready") break
}
}
} catch (_: IOException) {
} finally {
ready.countDown()
}
}.apply { isDaemon = true }.start()
ready.await(WINDOW_READY_SECONDS, TimeUnit.SECONDS)
}

fun find(): BattleNetSignIn? {
if (!System.getProperty("os.name").startsWith("Windows", ignoreCase = true)) return null
val appData = System.getenv("APPDATA") ?: return null
val launcher = launcherExecutable() ?: return null
return BattleNetSignIn(Paths.get(appData, "Battle.net", "Battle.net.config"), launcher)
}

private fun launcherExecutable(): Path? {
ProcessHandle.allProcesses().map { command(it) }.filter { named(it, LAUNCHER) }.findFirst().orElse(null)
?.let { return it }
return listOfNotNull(System.getenv("ProgramFiles(x86)"), System.getenv("ProgramFiles"))
.map { Paths.get(it, "Battle.net", LAUNCHER) }
.firstOrNull(Files::isRegularFile)
}

/** Whether [executable] belongs to the install Battle.net manages; the token only works there. */
fun isRegisteredGame(executable: Path): Boolean {
val key = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Warcraft III"
for (view in listOf("/reg:32", "/reg:64")) {
for (line in readCommand("reg.exe", "query", key, "/v", "InstallLocation", view)) {
val fields = line.trim().split(Regex("\\s{2,}"), 3)
if (fields.size == 3 && fields[0].equals("InstallLocation", ignoreCase = true)
&& executable.toAbsolutePath().normalize().startsWith(Paths.get(fields[2]).toAbsolutePath().normalize())) {
return true
}
}
}
return false
}

fun parseBinary(regQueryOutput: List<String>, value: String): String? =
REG_BINARY.findAll(regQueryOutput.joinToString("\n"))
.firstOrNull { it.groupValues[1].equals(value, ignoreCase = true) }
?.groupValues?.get(2)?.uppercase(Locale.ROOT)

/** The lock port the wc3-e2e-harness and WurstScript use for the same Battle.net profile. */
fun gatePort(config: Path): Int {
val key = config.toAbsolutePath().normalize().toString().lowercase(Locale.ROOT)
val hash = MessageDigest.getInstance("SHA-256").digest(key.toByteArray(Charsets.UTF_8))
return 33000 + Integer.remainderUnsigned(ByteBuffer.wrap(hash).int, 10000)
}

private fun games(): List<ProcessHandle> =
ProcessHandle.allProcesses().filter { named(command(it), GAME) }.toList()

/** A client the app started; anything else is someone else's game and is left alone. */
private fun startedByLauncher(game: ProcessHandle): Boolean =
game.parent().map { named(command(it), LAUNCHER) }.orElse(false)

private fun command(process: ProcessHandle): Path? = process.info().command().map { Paths.get(it) }.orElse(null)

private fun named(executable: Path?, image: String): Boolean =
executable?.fileName?.toString()?.equals(image, ignoreCase = true) == true

private fun readBinary(key: String, value: String): String? =
parseBinary(readCommand("reg.exe", "query", key, "/v", value), value)

private fun writeBinary(key: String, value: String, hex: String) {
val process = ProcessBuilder("reg.exe", "add", key, "/v", value, "/t", "REG_BINARY", "/d", hex, "/f")
.redirectOutput(ProcessBuilder.Redirect.DISCARD).redirectError(ProcessBuilder.Redirect.DISCARD).start()
if (!process.waitFor(8, TimeUnit.SECONDS) || process.exitValue() != 0) {
process.destroyForcibly()
throw IOException("Could not store the Warcraft III sign-in.")
}
}

private fun readCommand(vararg command: String): List<String> {
val process = ProcessBuilder(*command).redirectError(ProcessBuilder.Redirect.DISCARD).start()
if (!process.waitFor(8, TimeUnit.SECONDS)) {
process.destroyForcibly()
throw IOException("Windows registry query timed out.")
}
return process.inputReader().use { it.readLines() }
}
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/file/SetupApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,9 @@ object SetupApp {
private const val HOT_RELOAD_SESSION_CLASS = "de.peeeq.wurstio.languageserver.requests.LuaHotReloadSession"

/** Starts Warcraft III with a built map; replaced in tests, which must never launch the game. */
internal var launchMap: (Wc3ClientDetector.ClientInfo, Path) -> Unit = Wc3MapRunner::launch
internal var launchMap: (Wc3ClientDetector.ClientInfo, Path) -> Unit = { client, map ->
Wc3MapRunner.launch(client, map, WurstProjectConfig.configuredLaunchMode(setup.projectRoot), ::detail)
}

/** What `run --hot` built with, so that `reload` builds the same script. */
private fun hotRunRecord(): Path = compilerOutputDir().resolve("hot-run.properties")
Expand Down
Loading
Loading