diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/CurseForgeApp.kt b/app/src/main/java/com/droiddeck/launcher/runtime/CurseForgeApp.kt index af25344b77..68f258dd4a 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/CurseForgeApp.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/CurseForgeApp.kt @@ -90,13 +90,16 @@ object CurseForgeApp { return out; })()""" - internal fun expression(service: String, method: String, args: JSONArray?, pick: String = "r => r"): String { + private val CORE = JSONObject.quote("CoreService") + + internal fun expression(service: String, method: String, args: JSONArray?, pick: String = "r => r", connect: Boolean = true): String { val name = JSONObject.quote(method) val run = if (args == null) "true" else "($pick)(await s[$name](...$args))" val key = JSONObject.quote(service) + val link = if (connect && args != null) "const c = m[$CORE]; if (!c || typeof c.connect !== 'function') return { missing: true }; await c.connect(); " else "" val inner = "(async () => { let m = globalThis.$CACHE; if (!m || !m[$key]) m = globalThis.$CACHE = $SERVICES; const s = m[$key]; " + "if (!s || typeof s[$name] !== 'function') return { missing: true }; " + - "return { value: $run }; })()" + link + "return { value: $run }; })()" return "(() => { const w = require('electron').BrowserWindow.getAllWindows()" + ".find(w => !w.isDestroyed() && w.webContents.getURL().includes('desktop.html')); " + "if (!w) return JSON.stringify({ missing: true }); " + @@ -173,7 +176,6 @@ object CurseForgeApp { } private val holds = ConcurrentHashMap() - private val connected = ConcurrentHashMap.newKeySet() private fun lines(context: Context, argv: List): List { val out = ArrayList() @@ -181,8 +183,8 @@ object CurseForgeApp { return out } - private fun call(context: Context, inspector: String, service: String, method: String, args: JSONArray?, timeoutMs: Long, pick: String = "r => r"): Any? = - unwrap(lastJson(lines(context, evalArgv(inspector, expression(service, method, args, pick), timeoutMs))) ?: throw Failure("CurseForge gave no answer")) + private fun call(context: Context, inspector: String, service: String, method: String, args: JSONArray?, timeoutMs: Long, pick: String = "r => r", connect: Boolean = true): Any? = + unwrap(lastJson(lines(context, evalArgv(inspector, expression(service, method, args, pick, connect), timeoutMs))) ?: throw Failure("CurseForge gave no answer")) private sealed class Launch { class Url(val inspector: String) : Launch() @@ -234,24 +236,16 @@ object CurseForgeApp { hold.set(true) return } - if (inspector != null) runCatching { call(context, inspector, "CoreService", "shutdown", JSONArray(), 30_000, "r => true") } + if (inspector != null) runCatching { call(context, inspector, "CoreService", "shutdown", JSONArray(), 30_000, "r => true", connect = false) } hold.set(true) - inspector?.let(connected::remove) if (holds.remove(dir, hold)) runCatching { lines(context, helperArgv("stop", dir)) } } - private fun connect(context: Context, inspector: String) { - if (inspector in connected) return - call(context, inspector, "CoreService", "connect", JSONArray(), 120_000, "r => true") - connected += inspector - } - private fun instances(context: Context, inspector: String): Any? = - call(context, inspector, "GameAppService", "getGameInstances", JSONArray().put(WOW), 30_000, + call(context, inspector, "GameAppService", "getGameInstances", JSONArray().put(WOW), 120_000, "r => (r || []).map(i => ({ guid: i.guid, installPath: i.installPath }))") fun instance(context: Context, inspector: String, root: String, flavorDir: String): String { - connect(context, inspector) instanceFor(instances(context, inspector), flavorDir, null)?.let { return it } val detected = call(context, inspector, "GameDetectionAppService", "detectGameFromPath", JSONArray().put(root).put(WOW), 60_000) as? JSONObject return instanceFor(instances(context, inspector), flavorDir, detected?.optString("gameInstanceGuid")) ?: throw Failure("CurseForge did not find $flavorDir") @@ -261,7 +255,6 @@ object CurseForgeApp { parseSearch(call(context, inspector, "ProjectAppService", "search", JSONArray().put(searchRequest(guid, text, sort, page)), 60_000)) fun projects(context: Context, inspector: String, guid: String, rescan: Boolean = false): List { - connect(context, inspector) if (rescan) call(context, inspector, "GameDetectionAppService", "scanGameInstance", JSONArray().put(guid), 120_000) return parseProjects(call(context, inspector, "InstalledProjectsAppService", "getProjects", JSONArray().put(guid), 30_000, PROJECTS)) } @@ -283,19 +276,16 @@ object CurseForgeApp { } fun install(context: Context, inspector: String, guid: String, projectId: String): List? { - connect(context, inspector) if (call(context, inspector, "InstalledProjectsAppService", "installAddon", JSONArray().put(installRequest(guid, projectId)), 300_000) != true) return null return settle(context, inspector, guid) { list -> list.any { it.id == projectId } } } fun update(context: Context, inspector: String, guid: String, projectId: String): List? { - connect(context, inspector) if (call(context, inspector, "InstalledProjectsAppService", "updateMod", JSONArray().put(guid).put(projectId.toLong()), 300_000) != true) return null return settle(context, inspector, guid) { list -> list.any { it.id == projectId && !it.update } } } fun uninstall(context: Context, inspector: String, guid: String, project: Project): List? { - connect(context, inspector) val failed = call(context, inspector, "InstalledProjectsAppService", "deleteMods", JSONArray().put(guid).put(JSONArray().put(project.installedModId)), 120_000) as? JSONArray if (failed == null || failed.length() > 0) return null return settle(context, inspector, guid) { list -> list.none { it.id == project.id } } diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/CurseForgeAppTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/CurseForgeAppTest.kt index 6400c0162e..fb1ce9fd6b 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/CurseForgeAppTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/CurseForgeAppTest.kt @@ -163,8 +163,11 @@ class CurseForgeAppTest { assertFalse(text.contains("show()")) val probe = CurseForgeApp.expression("InstalledProjectsAppService", "installAddon", null) assertFalse(probe.contains("await s[")) - val connect = CurseForgeApp.expression("CoreService", "connect", JSONArray(), "r => true") - assertTrue(connect.contains("(r => true)(await s[\\\"connect\\\"](...[]))")) + assertFalse(probe.contains("connect()")) + assertTrue(text.contains("await c.connect(); return { value: ")) + val shutdown = CurseForgeApp.expression("CoreService", "shutdown", JSONArray(), "r => true", connect = false) + assertTrue(shutdown.contains("(r => true)(await s[\\\"shutdown\\\"](...[]))")) + assertFalse(shutdown.contains("connect()")) } @Test @@ -315,14 +318,39 @@ class CurseForgeAppTest { } @Test - fun curseForgeConnectsOncePerLaunch() { + fun eachCallIsOneRoundTripWithoutASeparateConnect() { + val calls = mutableListOf>() + val curse = fakeCurse(calls = calls) + val context = org.robolectric.RuntimeEnvironment.getApplication() + CurseForgeApp.projects(context, url, "g") + CurseForgeApp.projects(context, url, "g") + CurseForgeApp.instance(context, url, "/root", "") + assertEquals(listOf("getProjects", "getProjects", "getGameInstances"), curse.methods) + assertEquals(3, calls.size) + } + + @Test + fun anInstallAfterCurseForgesAddonBackendDroppedReconnectsAndSucceeds() { val curse = fakeCurse() val context = org.robolectric.RuntimeEnvironment.getApplication() - val launched = "ws://127.0.0.1:43118/${java.util.UUID.randomUUID()}" - CurseForgeApp.projects(context, launched, "g") - CurseForgeApp.projects(context, launched, "g") - CurseForgeApp.instance(context, launched, "/root", "") - assertEquals(1, curse.methods.count { it == "connect" }) + assertNotNull(CurseForgeApp.install(context, url, "g", "1")) + curse.backendDown = true + assertNotNull(CurseForgeApp.install(context, url, "g", "2")) + assertFalse(curse.backendDown) + assertEquals(setOf("1", "2"), CurseForgeApp.projects(context, url, "g").map { it.id }.toSet()) + } + + @Test + fun shuttingDownADroppedAddonBackendDoesNotReconnectIt() { + val calls = java.util.Collections.synchronizedList(mutableListOf>()) + val curse = fakeCurse(calls = calls) + val context = org.robolectric.RuntimeEnvironment.getApplication() + val hold = launch() + assertEquals(CurseForgeApp.Start.READY, CurseForgeApp.start(context, app, hold).state) + curse.backendDown = true + CurseForgeApp.stop(context, app, url, hold) + assertEquals(1, calls.count { it.getOrNull(4)?.contains("shutdown") == true }) + assertTrue(curse.backendDown) } @Test @@ -339,6 +367,7 @@ class CurseForgeAppTest { @Volatile var unreachableAfterDelete = false @Volatile var unreachable = false @Volatile var known = true + @Volatile var backendDown = false fun add(id: String, folder: String, status: Int) = add(id, listOf(folder), status) @@ -358,6 +387,11 @@ class CurseForgeAppTest { true } "getProjects" -> JSONArray(projects.map { JSONObject(it.toString()) }) + "installAddon" -> { + val id = Regex("projectId\\\\\":(\\d+)").find(expression)?.groupValues?.get(1) ?: return false + add(id, "Addon$id", 1) + true + } "updateMod" -> { projects.forEach { it.put("status", 1) } true @@ -396,7 +430,12 @@ class CurseForgeAppTest { onLine(JSONObject().put("ok", false).put("error", JSONObject().put("code", "DEVTOOLS_UNREACHABLE").put("message", "gone")).toString()) 0 } + curse.backendDown && !argv[4].contains("await c.connect()") && argv[4].contains("await s[") -> { + onLine(JSONObject().put("ok", false).put("error", JSONObject().put("message", "Error: Failed to get game instance by id: g")).toString()) + 0 + } else -> { + if (argv[4].contains("await c.connect()")) curse.backendDown = false val value = JSONObject().put("value", curse.answer(argv[4]) ?: JSONObject.NULL) onLine(JSONObject().put("ok", true).put("value", value.toString()).toString()) 0