From bf992bf6c56f4180f74c15c9a74398face8684e7 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 29 Dec 2024 04:15:41 +0100 Subject: [PATCH] Migrate Environment, SOSLSv2FormatBuilder & KotlinShutdownHandler --- .../base/implementable/stream/Stream.kt | 16 +- .../logging/KotlinShutdownHandler.kt | 9 +- .../formatbuilder/SOSLSv2FormatBuilder.kt | 12 +- .../engine/base/platform/Platform.kt | 22 ++ .../engine/base/platform/StreamPlatform.kt | 8 +- .../base/type/environment/GraphicsCard.kt | 35 +++ .../base/type/environment/OperatingSystem.kt | 112 +++++++ .../base/type/environment/package-info.kt | 27 ++ .../engine/base/utility/Environment.kt | 282 ++++-------------- .../engine/base/platform/Platform.kt | 22 ++ .../base/platform/StreamPlatformImpl.kt | 8 +- .../engine/base/utility/Environment.kt | 166 +++++++++++ 12 files changed, 465 insertions(+), 254 deletions(-) create mode 100644 base/src/commonMain/kotlin/de/staropensource/engine/base/platform/Platform.kt create mode 100644 base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/GraphicsCard.kt create mode 100644 base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/OperatingSystem.kt create mode 100644 base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/package-info.kt create mode 100644 base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/Platform.kt create mode 100644 base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/Environment.kt diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt index fe27045..6cb0909 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt @@ -22,10 +22,10 @@ package de.staropensource.engine.base.implementable.stream import de.staropensource.engine.base.exception.io.IOAccessException import de.staropensource.engine.base.implementable.PlatformData import de.staropensource.engine.base.platform.StreamPlatform -import de.staropensource.engine.base.platform.streamPlatform -import de.staropensource.engine.base.platform.streamStandardError -import de.staropensource.engine.base.platform.streamStandardInput -import de.staropensource.engine.base.platform.streamStandardOutput +import de.staropensource.engine.base.platform.platformStreamPlatform +import de.staropensource.engine.base.platform.platformStreamStandardError +import de.staropensource.engine.base.platform.platformStreamStandardInput +import de.staropensource.engine.base.platform.platformStreamStandardOutput /** * Makes streaming data easy. @@ -53,7 +53,7 @@ abstract class Stream( * @see stdin(3) * @since v1-alpha10 */ - val standardInput: ReadStream = streamStandardInput() + val standardInput: ReadStream = platformStreamStandardInput() /** * Contains the standard output as a [Stream]. @@ -62,7 +62,7 @@ abstract class Stream( * @see stdout(3) * @since v1-alpha10 */ - val standardOutput: WriteStream = streamStandardOutput() + val standardOutput: WriteStream = platformStreamStandardOutput() /** * Contains the standard error as a [Stream]. @@ -70,13 +70,13 @@ abstract class Stream( * @see stderr(3) * @since v1-alpha10 */ - val standardError: WriteStream = streamStandardError() + val standardError: WriteStream = platformStreamStandardError() } // -----> Properties override val platformData: MutableMap = mutableMapOf() - private val platform: StreamPlatform = streamPlatform(this) + private val platform: StreamPlatform = platformStreamPlatform(this) /** * Indicates whether this stream is closed. diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/KotlinShutdownHandler.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/KotlinShutdownHandler.kt index e8b8bf6..6c7b79f 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/KotlinShutdownHandler.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/KotlinShutdownHandler.kt @@ -20,16 +20,17 @@ package de.staropensource.engine.base.implementation.logging import de.staropensource.engine.base.implementable.ShutdownHandler +import de.staropensource.engine.base.platform.platformTerminate import kotlin.jvm.JvmStatic -import kotlin.system.exitProcess /** - * [ShutdownHandler] implementation using - * Kotlin's [exitProcess] method. + * [ShutdownHandler] implementation which + * simply terminates the application. * * @constructor Initializes this shutdown handler * @since v1-alpha10 */ +@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") class KotlinShutdownHandler private constructor() : ShutdownHandler { /** * Companion object of [KotlinShutdownHandler]. @@ -47,6 +48,6 @@ class KotlinShutdownHandler private constructor() : ShutdownHandler { } override fun exit(exitcode: UByte) { - exitProcess(exitcode.toInt()) + platformTerminate(exitcode) } } diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt index 0ee5768..417171d 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/implementation/logging/formatbuilder/SOSLSv2FormatBuilder.kt @@ -143,22 +143,22 @@ open class SOSLSv2FormatBuilder(call: Call, channelSettings: ChannelSettings?) : if (enabledFeatures.contains(Feature.DATE)) format - .append("%02d".format(datetime.dayOfMonth)) + .append(datetime.dayOfMonth.toString().padStart(2, padChar = '0')) .append(".") - .append("%02d".format(datetime.monthNumber)) + .append(datetime.monthNumber.toString().padStart(2, padChar = '0')) .append(".") - .append("%04d".format(datetime.year)) + .append(datetime.year.toString().padStart(4, padChar = '0')) if (enabledFeatures.contains(Feature.DATE) && enabledFeatures.contains(Feature.TIME)) format.append(" ") if (enabledFeatures.contains(Feature.TIME)) format - .append("%02d".format(datetime.hour)) + .append(datetime.hour.toString().padStart(2, padChar = '0')) .append(":") - .append("%02d".format(datetime.minute)) + .append(datetime.minute.toString().padStart(2, padChar = '0')) .append(":") - .append("%02d".format(datetime.second)) + .append(datetime.second.toString().padStart(2, padChar = '0')) if (enabledFeatures.contains(Feature.DATE) || enabledFeatures.contains(Feature.TIME)) { if (enabledFeatures.contains(Feature.FORMATTING)) diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/Platform.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/Platform.kt new file mode 100644 index 0000000..b8afc09 --- /dev/null +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/Platform.kt @@ -0,0 +1,22 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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.platform + +expect fun platformTerminate(exitcode: UByte) diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/StreamPlatform.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/StreamPlatform.kt index bf14e81..52c012f 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/StreamPlatform.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/platform/StreamPlatform.kt @@ -50,7 +50,7 @@ abstract class StreamPlatform( * @return matching [StreamPlatform] instance * @since v1-alpha10 */ -internal expect fun streamPlatform(stream: Stream): StreamPlatform +internal expect fun platformStreamPlatform(stream: Stream): StreamPlatform /** * Returns a [ReadStream] representing @@ -60,7 +60,7 @@ internal expect fun streamPlatform(stream: Stream): StreamPlatform * @return [ReadStream] instance representing the standard input * @since v1-alpha10 */ -internal expect fun streamStandardInput(): ReadStream +internal expect fun platformStreamStandardInput(): ReadStream /** * Returns a [WriteStream] representing @@ -70,7 +70,7 @@ internal expect fun streamStandardInput(): ReadStream * @return [WriteStream] instance representing the standard output * @since v1-alpha10 */ -internal expect fun streamStandardOutput(): WriteStream +internal expect fun platformStreamStandardOutput(): WriteStream /** * Returns a [WriteStream] representing @@ -80,4 +80,4 @@ internal expect fun streamStandardOutput(): WriteStream * @return [WriteStream] instance representing the standard error * @since v1-alpha10 */ -internal expect fun streamStandardError(): WriteStream +internal expect fun platformStreamStandardError(): WriteStream diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/GraphicsCard.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/GraphicsCard.kt new file mode 100644 index 0000000..e80d19d --- /dev/null +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/GraphicsCard.kt @@ -0,0 +1,35 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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.type.environment + +/** + * Represents a graphics card. + * + * @constructor Initializes this class + * @since v1-alpha10 + */ +@Suppress("Unused") +class GraphicsCard internal constructor( + val name: String, + val deviceIdentifier: String, + val manufacturer: String, + val version: String, + val videoMemory: ULong, +) diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/OperatingSystem.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/OperatingSystem.kt new file mode 100644 index 0000000..960d2fd --- /dev/null +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/OperatingSystem.kt @@ -0,0 +1,112 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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.type.environment + +import de.staropensource.engine.base.EngineConfiguration + +/** + * Represents all operating systems + * detected and supported by the engine. + * + * @since v1-alpha10 + */ +enum class OperatingSystem { + /** + * Identifies that the application is running + * under the [Linux](https://kernel.org) kernel + * on some kind of Linux distribution. + * + * @since v1-alpha10 + */ + LINUX, + + /** + * Identifies that the application is running + * under the [FreeBSD](https://freebsd.org) kernel + * and likely the FreeBSD operating system. + * + * @since v1-alpha10 + */ + FREEBSD, + + /** + * Identifies that the application is running + * under the [NetBSD](https://netbsd.org) kernel + * and likely the NetBSD operating system. + * + * @since v1-alpha10 + */ + NETBSD, + + /** + * Identifies that the application is running + * under the [OpenBSD](https://openbsd.org) kernel + * and likely the OpenBSD operating system. + * + * @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, + + /** + * Identifies that the application is running + * under [macOS](https://apple.com/macos), and + * by extension under the [XNU kernel](https://github.com/apple-oss-distributions/xnu). + * + * @since v1-alpha10 + */ + MACOS; + + override fun toString(): String = "${if (EngineConfiguration.fullTypePath) "de.staropensource.engine.base.utility." else ""}Environment\$OperatingSystem.${name}" + + /** + * Returns the human-friendly name + * of this operating system. + * + * @return human-friendly name + * @since v1-alpha10 + */ + fun humanFriendly(): String = when (this) { + LINUX -> "Linux" + FREEBSD -> "FreeBSD" + NETBSD -> "NetBSD" + OPENBSD -> "OpenBSD" + ANDROID -> "Android" + WINDOWS -> "Windows" + MACOS -> "macOS" + } +} diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/package-info.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/package-info.kt new file mode 100644 index 0000000..fb51af3 --- /dev/null +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/type/environment/package-info.kt @@ -0,0 +1,27 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 . + */ + +/** + * Data types returned by [Environment]. + * + * @since v1-alpha10 + */ +package de.staropensource.engine.base.type.environment + +import de.staropensource.engine.base.utility.Environment diff --git a/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/Environment.kt b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/Environment.kt index c848387..1914a72 100644 --- a/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/Environment.kt +++ b/base/src/commonMain/kotlin/de/staropensource/engine/base/utility/Environment.kt @@ -19,14 +19,9 @@ package de.staropensource.engine.base.utility -import de.staropensource.engine.base.Engine.Companion.logger -import de.staropensource.engine.base.EngineConfiguration -import de.staropensource.engine.base.utility.Environment.OperatingSystem.* +import de.staropensource.engine.base.type.environment.GraphicsCard +import de.staropensource.engine.base.type.environment.OperatingSystem import kotlinx.datetime.Instant -import oshi.PlatformEnum -import oshi.SystemInfo -import oshi.hardware.HardwareAbstractionLayer -import kotlin.jvm.JvmStatic /** * Provides information about the @@ -35,38 +30,14 @@ import kotlin.jvm.JvmStatic * @constructor Initializes this class * @since v1-alpha10 */ -@Suppress("Unused") -class Environment private constructor() { +@Suppress("Unused", "EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") +expect class Environment private constructor() { /** * Companion class of [Environment]. * * @since v1-alpha10 */ companion object { - // -----> Internal properties - /** - * Contains the [oshi.software.os.OperatingSystem] - * implementation used for accessing - * information about the operating - * system the engine is running on. - * - * @return [oshi.software.os.OperatingSystem] instance - * @since v1-alpha10 - */ - private var os: oshi.software.os.OperatingSystem? = null - - /** - * Contains the [HardwareAbstractionLayer] - * implementation used for accessing - * hardware information of the system the - * engine is running on. - * - * @return [HardwareAbstractionLayer] instance - * @since v1-alpha10 - */ - private var hw: HardwareAbstractionLayer? = null - - // -----> Update methods /** * Unsets the environment. @@ -74,10 +45,7 @@ class Environment private constructor() { * @since v1-alpha10 */ @JvmStatic - internal fun unset() { - os = null - hw = null - } + internal fun unset() /** * Detects and updates the environment. @@ -85,11 +53,7 @@ class Environment private constructor() { * @since v1-alpha10 */ @JvmStatic - internal fun detect() { - val si: SystemInfo = SystemInfo() - os = si.operatingSystem - hw = si.hardware - } + internal fun detect() // -----> Getters @@ -104,21 +68,8 @@ class Environment private constructor() { * @return maximum of supported bits * @since v1-alpha10 */ - fun getOperatingSystem(): OperatingSystem? { - when (SystemInfo.getCurrentPlatform()) { - PlatformEnum.LINUX, PlatformEnum.GNU, PlatformEnum.KFREEBSD -> return LINUX - PlatformEnum.FREEBSD -> return FREEBSD - PlatformEnum.NETBSD -> return NETBSD - PlatformEnum.OPENBSD -> return OPENBSD - //PlatformEnum.ANDROID -> Environment.getOperatingSystem() = Environment.OperatingSystem.ANDROID // android is not yet supported by the engine, have to figure that one out sometime - PlatformEnum.WINDOWS, PlatformEnum.WINDOWSCE -> return WINDOWS - PlatformEnum.MACOS -> return MACOS - else -> { - logger.crash("Unsupported operating system '" + SystemInfo.getCurrentPlatform().name + "' reported by OSHI") - return null - } - } - } + @JvmStatic + fun getOperatingSystem(): OperatingSystem? /** * Returns the point in time where @@ -128,12 +79,8 @@ class Environment private constructor() { * @return time of system startup * @since v1-alpha10 */ - fun getTimeOfStartup(): Instant? { - if (os == null) - return null - - return Instant.fromEpochSeconds(epochSeconds = os!!.systemBootTime) - } + @JvmStatic + fun getTimeOfStartup(): Instant? /** * Returns whether the current process @@ -143,7 +90,8 @@ class Environment private constructor() { * @return elevated? * @since v1-alpha10 */ - fun isElevated(): Boolean? = os?.isElevated + @JvmStatic + fun isElevated(): Boolean? /** * Returns the amount of bits @@ -155,7 +103,8 @@ class Environment private constructor() { * @return maximum of supported bits * @since v1-alpha10 */ - fun getBitness(): Int? = os?.bitness + @JvmStatic + fun getBitness(): Int? // -------> Computer /** @@ -165,7 +114,8 @@ class Environment private constructor() { * @return computer manufacturer name * @since v1-alpha10 */ - fun getComputerManufacturer(): String? = hw?.computerSystem?.manufacturer + @JvmStatic + fun getComputerManufacturer(): String? /** * Returns the name of the @@ -174,7 +124,8 @@ class Environment private constructor() { * @return computer model name * @since v1-alpha10 */ - fun getComputerModel(): String? = hw?.computerSystem?.model + @JvmStatic + fun getComputerModel(): String? // -------> Firmware (BIOS, UEFI, etc.) /** @@ -184,7 +135,8 @@ class Environment private constructor() { * @return firmware name * @since v1-alpha10 */ - fun getFirmwareName(): String? = hw?.computerSystem?.firmware?.name + @JvmStatic + fun getFirmwareName(): String? /** * Returns the description of the firmware (BIOS, @@ -193,7 +145,8 @@ class Environment private constructor() { * @return firmware description * @since v1-alpha10 */ - fun getFirmwareDescription(): String? = hw?.computerSystem?.firmware?.description + @JvmStatic + fun getFirmwareDescription(): String? /** * Returns the name of the manufacturer of this @@ -202,7 +155,8 @@ class Environment private constructor() { * @return firmware manufacturer name * @since v1-alpha10 */ - fun getFirmwareManufacturer(): String? = hw?.computerSystem?.firmware?.manufacturer + @JvmStatic + fun getFirmwareManufacturer(): String? /** * Returns the version of the firmware (BIOS, @@ -211,7 +165,8 @@ class Environment private constructor() { * @return firmware version * @since v1-alpha10 */ - fun getFirmwareVersion(): String? = hw?.computerSystem?.firmware?.version + @JvmStatic + fun getFirmwareVersion(): String? /** * Returns the release date of the firmware (BIOS, @@ -220,7 +175,8 @@ class Environment private constructor() { * @return firmware release date * @since v1-alpha10 */ - fun getFirmwareReleaseDate(): String? = hw?.computerSystem?.firmware?.releaseDate + @JvmStatic + fun getFirmwareReleaseDate(): String? // -------> Motherboard /** @@ -230,7 +186,8 @@ class Environment private constructor() { * @return motherboard model name * @since v1-alpha10 */ - fun getMotherboardModel(): String? = hw?.computerSystem?.baseboard?.model + @JvmStatic + fun getMotherboardModel(): String? /** * Returns the version of this @@ -239,7 +196,8 @@ class Environment private constructor() { * @return motherboard version * @since v1-alpha10 */ - fun getMotherboardVersion(): String? = hw?.computerSystem?.baseboard?.version + @JvmStatic + fun getMotherboardVersion(): String? /** * Returns the serial number of @@ -248,7 +206,8 @@ class Environment private constructor() { * @return motherboard serial number * @since v1-alpha10 */ - fun getMotherboardSerialNumber(): String? = hw?.computerSystem?.baseboard?.serialNumber + @JvmStatic + fun getMotherboardSerialNumber(): String? /** * Returns the name of the manufacturer @@ -257,7 +216,8 @@ class Environment private constructor() { * @return motherboard manufacturer name * @since v1-alpha10 */ - fun getMotherboardManufacturer(): String? = hw?.computerSystem?.baseboard?.manufacturer + @JvmStatic + fun getMotherboardManufacturer(): String? // -------> Memory /** @@ -267,7 +227,8 @@ class Environment private constructor() { * @return amount of physical memory in bytes * @since v1-alpha10 */ - fun getMemoryTotal(): Long? = hw?.memory?.total + @JvmStatic + fun getMemoryTotal(): Long? /** * Returns the amount of available @@ -276,7 +237,8 @@ class Environment private constructor() { * @return amount of physical memory in bytes * @since v1-alpha10 */ - fun getMemoryAvailable(): Long? = hw?.memory?.available + @JvmStatic + fun getMemoryAvailable(): Long? /** * Returns the amount of available @@ -285,7 +247,8 @@ class Environment private constructor() { * @return amount of physical memory in bytes * @since v1-alpha10 */ - fun getMemoryUsed(): Long? = hw?.memory?.total?.minus(hw!!.memory!!.available) + @JvmStatic + fun getMemoryUsed(): Long? /** * Returns the size of a @@ -294,7 +257,8 @@ class Environment private constructor() { * @return size of a memory page in bytes * @since v1-alpha10 */ - fun getMemoryPageSize(): Long? = hw?.memory?.pageSize + @JvmStatic + fun getMemoryPageSize(): ULong? // -------> CPU /** @@ -305,7 +269,8 @@ class Environment private constructor() { * @return maximum clock speed of logical processors in Hz or `-1` if it couldn't be determined * @since v1-alpha10 */ - fun getCPUMaximumFrequency(): Long? = hw?.processor?.maxFreq + @JvmStatic + fun getCPUMaximumFrequency(): Long? /** * Returns the estimated current clock @@ -327,7 +292,8 @@ class Environment private constructor() { * @return current clock rate of logical processors in Hz or an empty array * @since v1-alpha10 */ - fun getCPUCurrentFrequency(): LongArray? = hw?.processor?.currentFreq + @JvmStatic + fun getCPUCurrentFrequency(): LongArray? /** * Returns the current amount of @@ -342,7 +308,8 @@ class Environment private constructor() { * @return amount of logical processors * @since v1-alpha10 */ - fun getCPULogicalCount(): Int? = hw?.processor?.logicalProcessorCount + @JvmStatic + fun getCPULogicalCount(): Int? /** * Returns the amount of physical @@ -351,7 +318,8 @@ class Environment private constructor() { * @return amount of physical processors * @since v1-alpha10 */ - fun getCPUPhysicalCount(): Int? = hw?.processor?.physicalProcessorCount + @JvmStatic + fun getCPUPhysicalCount(): Int? // -------> CPU /** @@ -361,149 +329,7 @@ class Environment private constructor() { * @return array of [GraphicsCard] instances * @since v1-alpha10 */ - fun getGPUs(): Array? = hw?.graphicsCards?.map { graphicsCard -> GraphicsCard(graphicsCard) }?.toTypedArray() - } - - // -----> Data types - /** - * Represents all operating systems - * detected and supported by the engine. - * - * @since v1-alpha10 - */ - enum class OperatingSystem { - /** - * Identifies that the application is running - * under the [Linux](https://kernel.org) kernel - * on some kind of Linux distribution. - * - * @since v1-alpha10 - */ - LINUX, - - /** - * Identifies that the application is running - * under the [FreeBSD](https://freebsd.org) kernel - * and likely the FreeBSD operating system. - * - * @since v1-alpha10 - */ - FREEBSD, - - /** - * Identifies that the application is running - * under the [NetBSD](https://netbsd.org) kernel - * and likely the NetBSD operating system. - * - * @since v1-alpha10 - */ - NETBSD, - - /** - * Identifies that the application is running - * under the [OpenBSD](https://openbsd.org) kernel - * and likely the OpenBSD operating system. - * - * @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, - - /** - * Identifies that the application is running - * under [macOS](https://apple.com/macos), and - * by extension under the [XNU kernel](https://github.com/apple-oss-distributions/xnu). - * - * @since v1-alpha10 - */ - MACOS; - - override fun toString(): String = "${if (EngineConfiguration.fullTypePath) "de.staropensource.engine.base.utility." else ""}Environment\$OperatingSystem.${name}" - - /** - * Returns the human-friendly name - * of this operating system. - * - * @return human-friendly name - * @since v1-alpha10 - */ - fun humanFriendly(): String = when (this) { - LINUX -> "Linux" - FREEBSD -> "FreeBSD" - NETBSD -> "NetBSD" - OPENBSD -> "OpenBSD" - ANDROID -> "Android" - WINDOWS -> "Windows" - MACOS -> "macOS" - } - } - - /** - * Represents a graphics card. - * - * @constructor Initializes this class - * @since v1-alpha10 - */ - class GraphicsCard internal constructor(val graphicsCard: oshi.hardware.GraphicsCard) { - /** - * Returns the name of this graphics card. - * - * @return name - * @since v1-alpha10 - */ - fun getName(): String = graphicsCard.name - - /** - * Returns the device identifier - * of this graphics card. - * - * @return device identifier - * @since v1-alpha10 - */ - fun getDeviceIdentifier(): String = graphicsCard.deviceId - - /** - * Returns the name of the manufacturer - * of this graphics card. - * - * @return manufacturer name - * @since v1-alpha10 - */ - fun getManufacturer(): String = graphicsCard.vendor - - /** - * Returns the version of - * this graphics card. - * - * @return version - * @since v1-alpha10 - */ - fun getVersion(): String = graphicsCard.versionInfo - - /** - * Returns the amount of VRAM provided - * by this graphics card in bytes. - * - * @return amount of total VRAM - * @since v1-alpha10 - */ - fun getVideoMemory(): Long = graphicsCard.vRam + @JvmStatic + fun getGPUs(): Array? } } diff --git a/base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/Platform.kt b/base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/Platform.kt new file mode 100644 index 0000000..1bce42e --- /dev/null +++ b/base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/Platform.kt @@ -0,0 +1,22 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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.platform + +actual fun platformTerminate(exitcode: UByte) = Runtime.getRuntime().exit(exitcode.toInt()) diff --git a/base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/StreamPlatformImpl.kt b/base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/StreamPlatformImpl.kt index cbeb93d..ce8a8a8 100644 --- a/base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/StreamPlatformImpl.kt +++ b/base/src/jvmMain/kotlin/de/staropensource/engine/base/platform/StreamPlatformImpl.kt @@ -53,16 +53,16 @@ class StreamPlatformImpl(stream: Stream) : StreamPlatform(stream) { // -----> Actual methods -actual fun streamPlatform(stream: Stream): StreamPlatform = stream.platformData.getOrPut("platform") { StreamPlatformImpl(stream) } as StreamPlatform -actual fun streamStandardInput(): ReadStream = object : JavaReadStream(System.`in`) { +actual fun platformStreamPlatform(stream: Stream): StreamPlatform = stream.platformData.getOrPut("platform") { StreamPlatformImpl(stream) } as StreamPlatform +actual fun platformStreamStandardInput(): ReadStream = object : JavaReadStream(System.`in`) { override fun close() = Unit override fun closeStream() = Unit } -actual fun streamStandardOutput(): WriteStream = object : JavaWriteStream(System.out) { +actual fun platformStreamStandardOutput(): WriteStream = object : JavaWriteStream(System.out) { override fun close() = Unit override fun closeStream() = Unit } -actual fun streamStandardError(): WriteStream = object : JavaWriteStream(System.err) { +actual fun platformStreamStandardError(): WriteStream = object : JavaWriteStream(System.err) { override fun close() = Unit override fun closeStream() = Unit } diff --git a/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/Environment.kt b/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/Environment.kt new file mode 100644 index 0000000..3d45eaa --- /dev/null +++ b/base/src/jvmMain/kotlin/de/staropensource/engine/base/utility/Environment.kt @@ -0,0 +1,166 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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.type.environment.GraphicsCard +import de.staropensource.engine.base.type.environment.OperatingSystem +import kotlinx.datetime.Instant +import oshi.PlatformEnum +import oshi.SystemInfo +import oshi.hardware.HardwareAbstractionLayer + +@Suppress("Unused", "EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") +actual class Environment private actual constructor() { + actual companion object { + // -----> Internal properties + /** + * Contains the [oshi.software.os.OperatingSystem] + * implementation used for accessing + * information about the operating + * system the engine is running on. + * + * @return [oshi.software.os.OperatingSystem] instance + * @since v1-alpha10 + */ + private var os: oshi.software.os.OperatingSystem? = null + + /** + * Contains the [HardwareAbstractionLayer] + * implementation used for accessing + * hardware information of the system the + * engine is running on. + * + * @return [HardwareAbstractionLayer] instance + * @since v1-alpha10 + */ + private var hw: HardwareAbstractionLayer? = null + + @JvmStatic + actual fun unset() { + os = null + hw = null + } + + @JvmStatic + actual fun detect() { + val si: SystemInfo = SystemInfo() + os = si.operatingSystem + hw = si.hardware + } + + @JvmStatic + actual fun getOperatingSystem(): OperatingSystem? { + when (SystemInfo.getCurrentPlatform()) { + PlatformEnum.LINUX, PlatformEnum.GNU, PlatformEnum.KFREEBSD -> return OperatingSystem.LINUX + PlatformEnum.FREEBSD -> return OperatingSystem.FREEBSD + PlatformEnum.NETBSD -> return OperatingSystem.NETBSD + PlatformEnum.OPENBSD -> return OperatingSystem.OPENBSD + //PlatformEnum.ANDROID -> return OperatingSystem.ANDROID // android is not yet supported by the engine, have to figure that one out sometime + PlatformEnum.WINDOWS, PlatformEnum.WINDOWSCE -> return OperatingSystem.WINDOWS + PlatformEnum.MACOS -> return OperatingSystem.MACOS + else -> { + logger.crash("Unsupported operating system '" + SystemInfo.getCurrentPlatform().name + "' reported by OSHI") + return null + } + } + } + + @JvmStatic + actual fun getTimeOfStartup(): Instant? { + if (os == null) + return null + + return Instant.fromEpochSeconds(epochSeconds = os!!.systemBootTime) + } + + @JvmStatic + actual fun isElevated(): Boolean? = os?.isElevated + + @JvmStatic + actual fun getBitness(): Int? = os?.bitness + + @JvmStatic + actual fun getComputerManufacturer(): String? = hw?.computerSystem?.manufacturer + + @JvmStatic + actual fun getComputerModel(): String? = hw?.computerSystem?.model + + @JvmStatic + actual fun getFirmwareName(): String? = hw?.computerSystem?.firmware?.name + + @JvmStatic + actual fun getFirmwareDescription(): String? = hw?.computerSystem?.firmware?.description + + @JvmStatic + actual fun getFirmwareManufacturer(): String? = hw?.computerSystem?.firmware?.manufacturer + + @JvmStatic + actual fun getFirmwareVersion(): String? = hw?.computerSystem?.firmware?.version + + @JvmStatic + actual fun getFirmwareReleaseDate(): String? = hw?.computerSystem?.firmware?.releaseDate + + @JvmStatic + actual fun getMotherboardModel(): String? = hw?.computerSystem?.baseboard?.model + + @JvmStatic + actual fun getMotherboardVersion(): String? = hw?.computerSystem?.baseboard?.version + + @JvmStatic + actual fun getMotherboardSerialNumber(): String? = hw?.computerSystem?.baseboard?.serialNumber + + @JvmStatic + actual fun getMotherboardManufacturer(): String? = hw?.computerSystem?.baseboard?.manufacturer + + @JvmStatic + actual fun getMemoryTotal(): Long? = hw?.memory?.total + + @JvmStatic + actual fun getMemoryAvailable(): Long? = hw?.memory?.available + + @JvmStatic + actual fun getMemoryUsed(): Long? = hw?.memory?.total?.minus(hw!!.memory!!.available) + + @JvmStatic + actual fun getMemoryPageSize(): ULong? = hw?.memory?.pageSize?.toULong() + + @JvmStatic + actual fun getCPUMaximumFrequency(): Long? = hw?.processor?.maxFreq + + @JvmStatic + actual fun getCPUCurrentFrequency(): LongArray? = hw?.processor?.currentFreq + + @JvmStatic + actual fun getCPULogicalCount(): Int? = hw?.processor?.logicalProcessorCount + + @JvmStatic + actual fun getCPUPhysicalCount(): Int? = hw?.processor?.physicalProcessorCount + + @JvmStatic + actual fun getGPUs(): Array? = hw?.graphicsCards?.map { graphicsCard -> GraphicsCard( + graphicsCard.name, + graphicsCard.deviceId, + graphicsCard.vendor, + graphicsCard.versionInfo, + graphicsCard.vRam.toULong(), + ) }?.toTypedArray() + } +}