This commit is contained in:
DBotThePony 2024-02-04 18:03:18 +07:00
parent 666d746936
commit 4ff09bea2b
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 32 additions and 19 deletions

View File

@ -681,7 +681,7 @@ class StarboundClient private constructor(val clientID: Int) : Closeable {
blendFunc = BlendFunc.MULTIPLY_WITH_ALPHA
}
val spinner = ExecutionSpinner(mailbox, ::renderFrame, Starbound.TICK_TIME_ADVANCE_NANOS, true)
val spinner = ExecutionSpinner(mailbox, ::renderFrame, Starbound.TICK_TIME_ADVANCE_NANOS)
val settings = ClientSettings()
val viewportCells: ICellAccess = object : ICellAccess {

View File

@ -1,11 +1,16 @@
package ru.dbotthepony.kstarbound.util
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.util.MailboxExecutorService
import ru.dbotthepony.kstarbound.Starbound
import java.util.concurrent.locks.LockSupport
import java.util.function.BooleanSupplier
class ExecutionSpinner(private val executor: MailboxExecutorService, private val spinner: BooleanSupplier, private val timeBetweenFrames: Long, val precise: Boolean = false) : Runnable {
class ExecutionSpinner(private val executor: MailboxExecutorService, private val spinner: BooleanSupplier, private val timeBetweenFrames: Long) : Runnable {
init {
Companion
}
private var lastRender = System.nanoTime()
private var frameRenderTime = 0L
private val frameRenderTimes = LongArray(60) { 1L }
@ -46,23 +51,10 @@ class ExecutionSpinner(private val executor: MailboxExecutorService, private val
fun spin(): Boolean {
var diff = timeUntilNextFrame()
if (precise) {
while (diff > 1_500_000L) {
executor.executeQueuedTasks()
diff = timeUntilNextFrame()
if (diff > 1_500_000L) LockSupport.parkNanos(diff - 400_000L)
}
while (diff > 0L) {
executor.executeQueuedTasks()
diff = timeUntilNextFrame()
}
} else {
while (diff > 0L) {
executor.executeQueuedTasks()
diff = timeUntilNextFrame()
if (diff > 400_000L) LockSupport.parkNanos(diff)
}
while (diff > 0L) {
executor.executeQueuedTasks()
diff = timeUntilNextFrame()
if (diff > 400_000L) LockSupport.parkNanos(100_000L)
}
val mark = System.nanoTime()
@ -78,4 +70,25 @@ class ExecutionSpinner(private val executor: MailboxExecutorService, private val
override fun run() {
while (spin()) {}
}
companion object {
private val LOGGER = LogManager.getLogger()
init {
val thread = object : Thread("Fix God Damn Windows Process Scheduler For Once Please") {
override fun run() {
while (true) {
try {
sleep(Int.MAX_VALUE.toLong())
} catch (err: InterruptedException) {
LOGGER.error("Timer hack thread was interrupted, ignoring.")
}
}
}
}
thread.isDaemon = true
thread.start()
}
}
}