diff --git a/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt b/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt index bca4e69af..b8241b5c3 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt @@ -20,6 +20,7 @@ package de.staropensource.engine.base +import de.staropensource.engine.base.utility.Environment import de.staropensource.engine.logging.Logger /** @@ -118,6 +119,7 @@ class Engine private constructor() { logger.info("Initializing") // Run initialization code + Environment.detect() state = State.INITIALIZED } @@ -139,6 +141,7 @@ class Engine private constructor() { logger.info("Shutting down") // Run shutdown code + Environment.unset() state = State.SHUT_DOWN } diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/Environment.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/Environment.kt new file mode 100644 index 000000000..cef68447a --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/Environment.kt @@ -0,0 +1,158 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU Affero General Public License v3 + * with an exception allowing classpath linking. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.utility + +import de.staropensource.engine.base.Engine.Companion.logger +import de.staropensource.engine.base.utility.Environment.OperatingSystem.FREEBSD +import de.staropensource.engine.base.utility.Environment.OperatingSystem.LINUX +import de.staropensource.engine.base.utility.Environment.OperatingSystem.NETBSD +import de.staropensource.engine.base.utility.Environment.OperatingSystem.OPENBSD +import de.staropensource.engine.base.utility.Environment.OperatingSystem.WINDOWS +import oshi.PlatformEnum +import oshi.SystemInfo + +/** + * Provides information about the + * environment the application runs in. + * + * @since v1-alpha10 + */ +class Environment private constructor() { + /** + * Companion class of [Environment]. + * + * @since v1-alpha10 + */ + companion object { + /** + * The operating system family + * this program is running under. + * + * @since v1-alpha10 + */ + var operatingSystem: OperatingSystem = OperatingSystem.UNKNOWN + internal set + + /** + * Unsets the environment. + * + * @since v1-alpha10 + */ + @JvmStatic + internal fun unset() { + operatingSystem = OperatingSystem.UNKNOWN + } + + /** + * Detects and updates the environment. + * + * @since v1-alpha10 + */ + @JvmStatic + internal fun detect() { + val si: SystemInfo = SystemInfo() + val os: oshi.software.os.OperatingSystem = si.operatingSystem + + logger.diag("Running environment detection") + + when (SystemInfo.getCurrentPlatform()) { + PlatformEnum.LINUX, PlatformEnum.GNU, PlatformEnum.KFREEBSD -> operatingSystem = LINUX + PlatformEnum.FREEBSD -> operatingSystem = FREEBSD + PlatformEnum.NETBSD -> operatingSystem = NETBSD + PlatformEnum.OPENBSD -> operatingSystem = OPENBSD + //PlatformEnum.ANDROID -> Environment.operatingSystem = Environment.OperatingSystem.ANDROID // android is not yet supported by the engine, have to figure that one out sometime + PlatformEnum.WINDOWS, PlatformEnum.WINDOWSCE -> operatingSystem = WINDOWS + else -> logger.crash("Unsupported operating system '" + os.family + "'") + } + } + } + + // -----> Data types + /** + * Represents all operating systems + * detected and supported by the engine. + * + * @since v1-alpha10 + */ + enum class OperatingSystem { + /** + * The operating system could not be determined. + * + * Ideally, this value should be impossible to get + * as the engine crashes during the bootstrapping + * phase if the operating system is not supported + * by the engine. + * + * @since v1-alpha10 + */ + UNKNOWN, + + /** + * Identifies that the application is running + * under the [Linux](https://kernel.org) kernel. + * + * @since v1-alpha10 + */ + LINUX, + + /** + * Identifies that the application is running + * under the [FreeBSD](https://freebsd.org) kernel. + * + * @since v1-alpha10 + */ + FREEBSD, + + /** + * Identifies that the application is running + * under the [NetBSD](https://netbsd.org) kernel. + * + * @since v1-alpha10 + */ + NETBSD, + + /** + * Identifies that the application is running + * under the [OpenBSD](https://openbsd.org) kernel. + * + * @since v1-alpha10 + */ + OPENBSD, + + /** + * Identifies that the application is running + * under [Android](https://android.com), and + * by extension the [Linux kernel](https://kernel.org). + * + * @since v1-alpha10 + */ + ANDROID, + + /** + * Identifies that the application is running + * under [Windows](https://windows.com), and + * by extension the [Windows NT kernel](https://en.wikipedia.org/wiki/Ntoskrnl.exe). + * + * @since v1-alpha10 + */ + WINDOWS, + } +} diff --git a/build.gradle.kts b/build.gradle.kts index 87bf62e55..5259ab334 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -77,6 +77,9 @@ allprojects { kotlin(property("dependencyKotlinStdJavaIdentifier") as String) implementation("${property("dependencyKotlinReflectIdentifier") as String}:${property("dependencyKotlinReflectVersion") as String}") implementation("${property("dependencyKotlinDateTimeIdentifier") as String}:${property("dependencyKotlinDateTimeVersion") as String}") + + // OSHI + implementation("${property("dependencyOshiIdentifier") as String}:${property("dependencyOshiVersion") as String}") } // Java diff --git a/gradle.properties b/gradle.properties index a463f92ab..2a3d85123 100644 --- a/gradle.properties +++ b/gradle.properties @@ -40,5 +40,7 @@ dependencyKotlinReflectIdentifier=org.jetbrains.kotlin:kotlin-reflect dependencyKotlinReflectVersion=2.1.0 dependencyKotlinDateTimeIdentifier=org.jetbrains.kotlinx:kotlinx-datetime dependencyKotlinDateTimeVersion=0.6.1 +dependencyOshiIdentifier=com.github.oshi:oshi-core-java11 +dependencyOshiVersion=6.6.5 dependencyJansiIdentifier=org.fusesource:jansi dependencyJansiVersion=2.4.1