Add much more getters to the Environment class

This commit is contained in:
JeremyStar™ 2024-12-17 19:42:04 +01:00
parent dd920dffaa
commit b7772f5fb8
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 347 additions and 69 deletions

View file

@ -25,6 +25,7 @@ import de.staropensource.engine.base.utility.Environment.OperatingSystem.*
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import oshi.PlatformEnum import oshi.PlatformEnum
import oshi.SystemInfo import oshi.SystemInfo
import oshi.hardware.HardwareAbstractionLayer
/** /**
* Provides information about the * Provides information about the
@ -32,6 +33,7 @@ import oshi.SystemInfo
* *
* @since v1-alpha10 * @since v1-alpha10
*/ */
@Suppress("unused")
class Environment private constructor() { class Environment private constructor() {
/** /**
* Companion class of [Environment]. * Companion class of [Environment].
@ -39,43 +41,28 @@ class Environment private constructor() {
* @since v1-alpha10 * @since v1-alpha10
*/ */
companion object { companion object {
// -----> Properties // -----> Internal properties
/** /**
* The operating system family * Contains the [oshi.software.os.OperatingSystem]
* this program is running under. * implementation used for accessing
* information about the operating
* system the engine is running on.
* *
* @return [oshi.software.os.OperatingSystem] instance
* @since v1-alpha10 * @since v1-alpha10
*/ */
var operatingSystem: OperatingSystem = UNKNOWN private var os: oshi.software.os.OperatingSystem? = null
private set
/** /**
* Returns the maximum amount of bits this operating system supports. * Contains the [HardwareAbstractionLayer]
* * implementation used for accessing
* This might be 32, 64 or even higher. * hardware information of the system the
* engine is running on.
* *
* @return [HardwareAbstractionLayer] instance
* @since v1-alpha10 * @since v1-alpha10
*/ */
var bitAmount: Int = 0 private var hw: HardwareAbstractionLayer? = null
private set
/**
* The point in time where the
* operating system was started at.
*
* @since v1-alpha10
*/
var startTime: Instant = Instant.fromEpochMilliseconds(0)
private set
/**
* Contains if this process is elevated ie.
* has superuser/administrator permissions.
*
* @since v1-alpha10
*/
var elevated: Boolean = false
private set
// -----> Update methods // -----> Update methods
@ -86,10 +73,8 @@ class Environment private constructor() {
*/ */
@JvmStatic @JvmStatic
internal fun unset() { internal fun unset() {
operatingSystem = UNKNOWN os = null
bitAmount = 0 hw = null
startTime = Instant.fromEpochMilliseconds(0)
elevated = false
} }
/** /**
@ -100,27 +85,281 @@ class Environment private constructor() {
@JvmStatic @JvmStatic
internal fun detect() { internal fun detect() {
val si: SystemInfo = SystemInfo() val si: SystemInfo = SystemInfo()
val os: oshi.software.os.OperatingSystem = si.operatingSystem os = si.operatingSystem
hw = si.hardware
}
logger.diag("Running environment detection")
// Detect operating system // -----> Getters
// -------> General
/**
* Returns the amount of
* bits this platform targets.
*
* This may be 32, 64 or an
* even higher value.
*
* @return maximum of supported bits
* @since v1-alpha10
*/
fun getOperatingSystem(): OperatingSystem? {
when (SystemInfo.getCurrentPlatform()) { when (SystemInfo.getCurrentPlatform()) {
PlatformEnum.LINUX, PlatformEnum.GNU, PlatformEnum.KFREEBSD -> operatingSystem = LINUX PlatformEnum.LINUX, PlatformEnum.GNU, PlatformEnum.KFREEBSD -> return LINUX
PlatformEnum.FREEBSD -> operatingSystem = FREEBSD PlatformEnum.FREEBSD -> return FREEBSD
PlatformEnum.NETBSD -> operatingSystem = NETBSD PlatformEnum.NETBSD -> return NETBSD
PlatformEnum.OPENBSD -> operatingSystem = OPENBSD PlatformEnum.OPENBSD -> return OPENBSD
//PlatformEnum.ANDROID -> Environment.operatingSystem = Environment.OperatingSystem.ANDROID // android is not yet supported by the engine, have to figure that one out sometime //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 -> operatingSystem = WINDOWS PlatformEnum.WINDOWS, PlatformEnum.WINDOWSCE -> return WINDOWS
PlatformEnum.MACOS -> operatingSystem = MACOS PlatformEnum.MACOS -> return MACOS
else -> logger.crash("Unsupported operating system '" + os.family + "'") else -> {
logger.crash("Unsupported operating system '" + SystemInfo.getCurrentPlatform().name + "' reported by OSHI")
return null
}
}
} }
// Detect other metadata /**
bitAmount = os.bitness * Returns the point in time where
startTime = Instant.fromEpochSeconds(epochSeconds = os.systemBootTime) * the system the engine is
elevated = os.isElevated * running on has started.
*
* @return time of system startup
* @since v1-alpha10
*/
fun getTimeOfStartup(): Instant? {
if (os == null)
return null
return Instant.fromEpochSeconds(epochSeconds = os!!.systemBootTime)
} }
/**
* Returns whether the current process
* is elevated and is able to execute
* superuser/administrator actions.
*
* @return elevated?
* @since v1-alpha10
*/
fun isElevated(): Boolean? = os?.isElevated
/**
* Returns the amount of bits
* this platform targets.
*
* This may be 32, 64 or an
* even higher value.
*
* @return maximum of supported bits
* @since v1-alpha10
*/
fun getBitness(): Int? = os?.bitness
// -------> Computer
/**
* Returns the name of the
* manufacturer of this computer.
*
* @return computer manufacturer name
* @since v1-alpha10
*/
fun getComputerManufacturer(): String? = hw?.computerSystem?.manufacturer
/**
* Returns the name of the
* model of this computer.
*
* @return computer model name
* @since v1-alpha10
*/
fun getComputerModel(): String? = hw?.computerSystem?.model
// -------> Firmware (BIOS, UEFI, etc.)
/**
* Returns the name of the firmware (BIOS,
* UEFI, etc.) installed on this computer.
*
* @return firmware name
* @since v1-alpha10
*/
fun getFirmwareName(): String? = hw?.computerSystem?.firmware?.name
/**
* Returns the description of the firmware (BIOS,
* UEFI, etc.) installed on this computer.
*
* @return firmware description
* @since v1-alpha10
*/
fun getFirmwareDescription(): String? = hw?.computerSystem?.firmware?.description
/**
* Returns the name of the manufacturer of this
* computer's firmware (BIOS, UEFI, etc.).
*
* @return firmware manufacturer name
* @since v1-alpha10
*/
fun getFirmwareManufacturer(): String? = hw?.computerSystem?.firmware?.manufacturer
/**
* Returns the version of the firmware (BIOS,
* UEFI, etc.) installed on this computer.
*
* @return firmware version
* @since v1-alpha10
*/
fun getFirmwareVersion(): String? = hw?.computerSystem?.firmware?.version
/**
* Returns the release date of the firmware (BIOS,
* UEFI, etc.) installed on this computer.
*
* @return firmware release date
* @since v1-alpha10
*/
fun getFirmwareReleaseDate(): String? = hw?.computerSystem?.firmware?.releaseDate
// -------> Motherboard
/**
* Returns the name of the model of
* this computer's motherboard.
*
* @return motherboard model name
* @since v1-alpha10
*/
fun getMotherboardModel(): String? = hw?.computerSystem?.baseboard?.model
/**
* Returns the version of this
* computer's motherboard.
*
* @return motherboard version
* @since v1-alpha10
*/
fun getMotherboardVersion(): String? = hw?.computerSystem?.baseboard?.version
/**
* Returns the serial number of
* this computer's motherboard.
*
* @return motherboard serial number
* @since v1-alpha10
*/
fun getMotherboardSerialNumber(): String? = hw?.computerSystem?.baseboard?.serialNumber
/**
* Returns the name of the manufacturer
* of this computer's motherboard.
*
* @return motherboard manufacturer name
* @since v1-alpha10
*/
fun getMotherboardManufacturer(): String? = hw?.computerSystem?.baseboard?.manufacturer
// -------> Memory
/**
* Returns the total amount of physical
* memory this system has in bytes.
*
* @return amount of physical memory in bytes
* @since v1-alpha10
*/
fun getMemoryTotal(): Long? = hw?.memory?.total
/**
* Returns the amount of available
* physical memory in bytes.
*
* @return amount of physical memory in bytes
* @since v1-alpha10
*/
fun getMemoryAvailable(): Long? = hw?.memory?.available
/**
* Returns the amount of available
* physical memory in bytes.
*
* @return amount of physical memory in bytes
* @since v1-alpha10
*/
fun getMemoryUsed(): Long? = hw?.memory?.total?.minus(hw!!.memory!!.available)
/**
* Returns the size of a
* memory page in bytes.
*
* @return size of a memory page in bytes
* @since v1-alpha10
*/
fun getMemoryPageSize(): Long? = hw?.memory?.pageSize
// -------> CPU
/**
* Returns the maximum clock speed of
* the logical processors of the CPU
* installed in this computer in Hz.
*
* @return maximum clock speed of logical processors in Hz or `-1` if it couldn't be determined
* @since v1-alpha10
*/
fun getCPUMaxFrequency(): Long? = hw?.processor?.maxFreq
/**
* Returns the estimated current clock
* speed of the logical processors of the
* CPU installed in this computer in Hz.
*
* Each item in the returned array
* stands for one logical processor.
*
* Depending on the platform the engine
* runs on, this may or may not be
* implemented. The algorithm used for
* calculating this estimate depends on
* the platform targeted. Some special
* restrictions apply to certain platforms.
* In short: Do not rely on the values
* in the array returned by this method.
*
* @return current clock rate of logical processors in Hz or an empty array
* @since v1-alpha10
*/
fun getCPUCurrentFrequency(): LongArray? = hw?.processor?.currentFreq
/**
* Returns the current amount of
* logical processors of the CPU
* installed in this computer.
*
* This value may change over time
* on some platforms. Be sure to
* query this number frequently to
* avoid out of date information.
*
* @return amount of logical processors
* @since v1-alpha10
*/
fun getCPULogicalCount(): Int? = hw?.processor?.logicalProcessorCount
/**
* Returns the amount of physical
* processors installed in this computer.
*
* @return amount of physical processors
* @since v1-alpha10
*/
fun getCPUPhysicalCount(): Int? = hw?.processor?.physicalProcessorCount
// -------> CPU
/**
* Returns an array of graphics cards
* installed in this computer.
*
* @return array of [GraphicsCard] instances
* @since v1-alpha10
*/
fun getGPUs(): Array<GraphicsCard>? = hw?.graphicsCards?.map { graphicsCard -> GraphicsCard(graphicsCard) }?.toTypedArray()
} }
// -----> Data types // -----> Data types
@ -131,18 +370,6 @@ class Environment private constructor() {
* @since v1-alpha10 * @since v1-alpha10
*/ */
enum class OperatingSystem { 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 * Identifies that the application is running
* under the [Linux](https://kernel.org) kernel * under the [Linux](https://kernel.org) kernel
@ -206,4 +433,55 @@ class Environment private constructor() {
*/ */
MACOS, MACOS,
} }
/**
* Represents a graphics card.
*
* @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
}
} }

View file

@ -152,14 +152,14 @@ class FileAccess {
.createDirectory() .createDirectory()
.verifyIsDirectory() .verifyIsDirectory()
configDirectory = FileAccess(when (Environment.operatingSystem) { configDirectory = FileAccess(when (Environment.getOperatingSystem()) {
LINUX, FREEBSD, NETBSD, OPENBSD -> "${homeDirectory}/.config" LINUX, FREEBSD, NETBSD, OPENBSD -> "${homeDirectory}/.config"
WINDOWS -> "${homeDirectory}/AppData/Roaming/sosengine/config" WINDOWS -> "${homeDirectory}/AppData/Roaming/sosengine/config"
MACOS -> "${homeDirectory}/Library/Application Support/sosengine/config" MACOS -> "${homeDirectory}/Library/Application Support/sosengine/config"
else -> "${homeDirectory}/.sosengine/config" else -> "${homeDirectory}/.sosengine/config"
}).createDirectory().verifyIsDirectory() }).createDirectory().verifyIsDirectory()
dataDirectory = FileAccess(when (Environment.operatingSystem) { dataDirectory = FileAccess(when (Environment.getOperatingSystem()) {
LINUX, FREEBSD, NETBSD, OPENBSD -> "${homeDirectory}/.local/share" LINUX, FREEBSD, NETBSD, OPENBSD -> "${homeDirectory}/.local/share"
WINDOWS -> "${homeDirectory}/AppData/Roaming/sosengine/data" WINDOWS -> "${homeDirectory}/AppData/Roaming/sosengine/data"
MACOS -> "${homeDirectory}/Library/Application Support/sosengine/data" MACOS -> "${homeDirectory}/Library/Application Support/sosengine/data"
@ -174,7 +174,7 @@ class FileAccess {
+ ProcessHandle.current().pid() + ProcessHandle.current().pid()
).createDirectory().verifyIsDirectory().deleteOnShutdown() ).createDirectory().verifyIsDirectory().deleteOnShutdown()
persistentCacheDirectory = FileAccess(when (Environment.operatingSystem) { persistentCacheDirectory = FileAccess(when (Environment.getOperatingSystem()) {
LINUX, FREEBSD, NETBSD, OPENBSD -> "${homeDirectory}/.cache" LINUX, FREEBSD, NETBSD, OPENBSD -> "${homeDirectory}/.cache"
WINDOWS -> "${homeDirectory}/AppData/Local/Temp" WINDOWS -> "${homeDirectory}/AppData/Local/Temp"
MACOS -> "${homeDirectory}/Library/Caches" MACOS -> "${homeDirectory}/Library/Caches"
@ -549,7 +549,7 @@ class FileAccess {
* @since v1-alpha10 * @since v1-alpha10
*/ */
fun getFilesystemRestrictedNames(): String { fun getFilesystemRestrictedNames(): String {
return when (Environment.operatingSystem) { return when (Environment.getOperatingSystem()) {
WINDOWS -> "(?i)\\|/|:|[*]|[?]|\"|<|>|[|]|CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9]" WINDOWS -> "(?i)\\|/|:|[*]|[?]|\"|<|>|[|]|CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9]"
else -> "/" else -> "/"
} }

View file

@ -62,7 +62,7 @@ class FileAccessTest : TestBase() {
.replace("%", File.separator) .replace("%", File.separator)
// Apply operating system-specific updates // Apply operating system-specific updates
when (Environment.operatingSystem) { when (Environment.getOperatingSystem()) {
Environment.OperatingSystem.WINDOWS -> { Environment.OperatingSystem.WINDOWS -> {
if (string.startsWith(File.separator) || string.startsWith("/")) if (string.startsWith(File.separator) || string.startsWith("/"))
string = "C:${string}" string = "C:${string}"