Add Environment class

This commit is contained in:
JeremyStar™ 2024-12-15 01:12:07 +01:00
parent eea7f5f4af
commit 56152536f9
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
4 changed files with 166 additions and 0 deletions

View file

@ -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
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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,
}
}

View file

@ -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

View file

@ -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