Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Tidy up impl
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed May 23, 2024
1 parent def43c8 commit d904ec9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.sanmer.mrepo.compat.impl

import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import dev.sanmer.mrepo.compat.stub.IInstallCallback
import dev.sanmer.mrepo.compat.stub.IModuleOpsCallback
Expand Down Expand Up @@ -48,26 +47,10 @@ internal class APatchModuleManagerImpl(
}

override fun install(path: String, callback: IInstallCallback) {
val cmd = "apd module install '${path}'"

val stdout = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStdout)
}
}

val stderr = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStderr)
}
}

val result = shell.newJob().add(cmd).to(stdout, stderr).exec()
if (result.isSuccess) {
val module = getModuleInfo(path)
callback.onSuccess(module)
} else {
callback.onFailure()
}
install(
cmd = "apd module install '${path}'",
path = path,
callback = callback
)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.sanmer.mrepo.compat.impl

import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import dev.sanmer.mrepo.compat.content.LocalModule
import dev.sanmer.mrepo.compat.content.State
import dev.sanmer.mrepo.compat.stub.IInstallCallback
import dev.sanmer.mrepo.compat.stub.IModuleManager
import java.io.File
import java.util.zip.ZipFile
Expand Down Expand Up @@ -33,38 +35,37 @@ internal abstract class BaseModuleManagerImpl(
return mVersionCode
}

override fun getModules() = modulesDir.listFiles().orEmpty()
.mapNotNull { moduleDir ->
runCatching {
readProps(moduleDir)
?.toModule(
state = readState(moduleDir),
lastUpdated = readLastUpdated(moduleDir)
)
}.getOrNull()
override fun getModules() = modulesDir.listFiles()
.orEmpty()
.mapNotNull { dir ->
readProps(dir)
?.toModule(
state = readState(dir),
lastUpdated = readLastUpdated(dir)
)
}

override fun getModuleById(id: String) = runCatching {
val moduleDir = modulesDir.resolve(id)
readProps(moduleDir)
override fun getModuleById(id: String): LocalModule? {
val dir = modulesDir.resolve(id)

return readProps(dir)
?.toModule(
state = readState(moduleDir),
lastUpdated = readLastUpdated(moduleDir)
state = readState(dir),
lastUpdated = readLastUpdated(dir)
)
}.getOrNull()
}

override fun getModuleInfo(zipPath: String) = runCatching {
override fun getModuleInfo(zipPath: String): LocalModule? {
val zipFile = ZipFile(zipPath)
val entry = zipFile.getEntry(PROP_FILE) ?: return@runCatching null
val entry = zipFile.getEntry(PROP_FILE) ?: return null

zipFile.getInputStream(entry).use {
return zipFile.getInputStream(entry).use {
it.bufferedReader()
.readText()
.let(::readProps)

}.toModule()

}.getOrNull()
.toModule()
}
}

private fun readProps(props: String) = props.lines()
.associate { line ->
Expand Down Expand Up @@ -128,6 +129,28 @@ internal abstract class BaseModuleManagerImpl(

private fun String.exec() = ShellUtils.fastCmd(shell, this)

internal fun install(cmd: String, path: String, callback: IInstallCallback) {
val stdout = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStdout)
}
}

val stderr = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStderr)
}
}

val result = shell.newJob().add(cmd).to(stdout, stderr).exec()
if (result.isSuccess) {
val module = getModuleInfo(path)
callback.onSuccess(module)
} else {
callback.onFailure()
}
}

internal fun String.submit(cb: Shell.ResultCallback) = shell
.newJob().add(this).to(ArrayList(), null)
.submit(cb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import dev.sanmer.mrepo.compat.stub.IFileManager
import java.io.File

internal class FileManagerImpl : IFileManager.Stub() {
override fun deleteOnExit(path: String): Boolean {
val file = File(path)
if (!file.exists()) return false

if (file.isFile) {
return file.delete()
}

if (file.isDirectory) {
return file.deleteRecursively()
override fun deleteOnExit(path: String) = with(File(path)) {
when {
!exists() -> false
isFile -> delete()
isDirectory -> deleteRecursively()
else -> false
}

return false
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.sanmer.mrepo.compat.impl

import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import dev.sanmer.mrepo.compat.stub.IInstallCallback
import dev.sanmer.mrepo.compat.stub.IModuleOpsCallback
Expand Down Expand Up @@ -48,26 +47,10 @@ internal class KernelSUModuleManagerImpl(
}

override fun install(path: String, callback: IInstallCallback) {
val cmd = "ksud module install '${path}'"

val stdout = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStdout)
}
}

val stderr = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStderr)
}
}

val result = shell.newJob().add(cmd).to(stdout, stderr).exec()
if (result.isSuccess) {
val module = getModuleInfo(path)
callback.onSuccess(module)
} else {
callback.onFailure()
}
install(
cmd = "ksud module install '${path}'",
path = path,
callback = callback
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.sanmer.mrepo.compat.impl

import com.topjohnwu.superuser.CallbackList
import com.topjohnwu.superuser.Shell
import dev.sanmer.mrepo.compat.stub.IInstallCallback
import dev.sanmer.mrepo.compat.stub.IModuleOpsCallback
Expand Down Expand Up @@ -51,26 +50,10 @@ internal class MagiskModuleManagerImpl(
}

override fun install(path: String, callback: IInstallCallback) {
val cmd = "magisk --install-module '${path}'"

val stdout = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStdout)
}
}

val stderr = object : CallbackList<String?>() {
override fun onAddElement(msg: String?) {
msg?.let(callback::onStderr)
}
}

val result = shell.newJob().add(cmd).to(stdout, stderr).exec()
if (result.isSuccess) {
val module = getModuleInfo(path)
callback.onSuccess(module)
} else {
callback.onFailure()
}
install(
cmd = "magisk --install-module '${path}'",
path = path,
callback = callback
)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.sanmer.mrepo.compat.impl

enum class Platform {
MAGISK,
KERNELSU,
Magisk,
KernelSU,
APatch
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ internal class ServiceManagerImpl : IServiceManager.Stub() {

private val platform by lazy {
when {
"which magisk".execResult() -> Platform.MAGISK
"which ksud".execResult() -> Platform.KERNELSU
"which magisk".execResult() -> Platform.Magisk
"which ksud".execResult() -> Platform.KernelSU
"which apd".execResult() -> Platform.APatch
else -> throw IllegalArgumentException("unsupported platform: $seLinuxContext")
}
}

private val moduleManager by lazy {
when (platform) {
Platform.MAGISK -> MagiskModuleManagerImpl(main)
Platform.KERNELSU -> KernelSUModuleManagerImpl(main)
Platform.Magisk -> MagiskModuleManagerImpl(main)
Platform.KernelSU -> KernelSUModuleManagerImpl(main)
Platform.APatch -> APatchModuleManagerImpl(main)
}
}
Expand Down

0 comments on commit d904ec9

Please sign in to comment.