Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.3 kB
2
Indexable
Never
package acidicoala.koalageddon

import androidx.compose.runtime.rememberCoroutineScope
import acidicoala.koalageddon.core.di.coreModule
import acidicoala.koalageddon.core.model.Settings
import acidicoala.koalageddon.core.ui.composition.LocalSettings
import acidicoala.koalageddon.core.ui.composition.LocalStrings
import acidicoala.koalageddon.core.ui.theme.AppRippleTheme
import acidicoala.koalageddon.core.ui.theme.AppTheme
import acidicoala.koalageddon.core.values.Bitmaps
import acidicoala.koalageddon.home.ui.HomeScreen
import acidicoala.koalageddon.settings.di.settingsModule
import acidicoala.koalageddon.steam.di.steamModule
import androidx.compose.material.MaterialTheme
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import kotlinx.coroutines.flow.MutableStateFlow
import org.kodein.di.bindInstance
import org.kodein.di.compose.localDI
import org.kodein.di.compose.withDI
import org.kodein.di.instance
import java.awt.Dimension
import java.io.File
import java.io.IOException
import javax.swing.UIManager

fun main() = application {
    try {
        // Run the second executable with elevated privileges
        runSecondExecutableWithAdminPermissions()
    } catch (e: IOException) {
        e.printStackTrace()
    }

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())

    Window(
        onCloseRequest = ::exitApplication,
        icon = painterResource(Bitmaps.Icon),
        title = "Koalageddon"
    ) {
        with(LocalDensity.current) {
            window.minimumSize = Dimension(720.dp.toPx().toInt(), 480.dp.toPx().toInt())
        }

        val appScope = rememberCoroutineScope()

        withDI({
            bindInstance { appScope }
            importAll(coreModule, settingsModule, steamModule)
        }) {
            val settingsFlow: MutableStateFlow<Settings> by localDI().instance()
            val settings by settingsFlow.collectAsState()

            val colors = when (settings.theme) {
                Settings.Theme.Dark -> AppTheme.Material.darkColors
                Settings.Theme.Light -> AppTheme.Material.lightColors
            }

            // TODO: Prompt to add Windows Defender exclusion folder
            MaterialTheme(colors = colors, shapes = AppTheme.shapes) {
                CompositionLocalProvider(LocalSettings provides settings) {
                    CompositionLocalProvider(LocalStrings provides settings.strings) {
                        CompositionLocalProvider(LocalRippleTheme provides AppRippleTheme) {
                            HomeScreen()
                        }
                    }
                }
            }
        }
    }
}

@Throws(IOException::class)
fun runSecondExecutableWithAdminPermissions() {
    val currentDirectory = System.getProperty("user.dir")
    val appFolder = "app"
    val secondExecutableName = "Koalageddon2.exe"

    val appFolderPath = File(currentDirectory, appFolder).absolutePath
    val secondExecutablePath = File(appFolderPath, secondExecutableName).absolutePath

    println("Current Directory: $currentDirectory")
    println("App Folder Path: $appFolderPath")
    println("Second Executable Path: $secondExecutablePath")

    val processBuilder = ProcessBuilder(secondExecutablePath)
    processBuilder.redirectErrorStream(true) // Redirect error stream to input stream

    try {
        // Start the process
        val process = processBuilder.start()

        // Read the output and error streams
        val output = process.inputStream.bufferedReader().readText()

        // Optional: You can wait for the process to finish
        val exitCode = process.waitFor()

        println("Output from the second executable:\n$output")
        println("Second executable exited with code: $exitCode")
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
Leave a Comment