improve screenshot taking

This commit is contained in:
Bixilon 2021-10-01 19:54:27 +02:00
parent a78f24c15c
commit 79eb7afa49
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -35,21 +35,20 @@ class ScreenshotTaker(
) { ) {
fun takeScreenshot() { fun takeScreenshot() {
try { try {
val width = renderWindow.window.size.x
val height = renderWindow.window.size.y
val buffer = renderWindow.renderSystem.readPixels(Vec2i(0, 0), Vec2i(width, height), PixelTypes.RGBA)
val basePath = "${RunConfiguration.HOME_DIRECTORY}/screenshots/${renderWindow.connection.address.hostname}/${DATE_FORMATTER.format(System.currentTimeMillis())}" val basePath = "${RunConfiguration.HOME_DIRECTORY}/screenshots/${renderWindow.connection.address.hostname}/${DATE_FORMATTER.format(System.currentTimeMillis())}"
var path = "$basePath.png" var path = "$basePath.png"
var i = 1 var i = 1
while (File(path).exists()) { while (File(path).exists()) {
path = "${basePath}_${i++}.png" path = "${basePath}_${i++}.png"
if (i > 10) { if (i > MAX_FILES_CHECK) {
screenshotFail(StackOverflowError()) throw StackOverflowError("There are already > $MAX_FILES_CHECK screenshots with this date! Please try again!")
return
} }
} }
val width = renderWindow.window.size.x
val height = renderWindow.window.size.y
val buffer = renderWindow.renderSystem.readPixels(Vec2i(0, 0), Vec2i(width, height), PixelTypes.RGBA)
DefaultThreadPool += { DefaultThreadPool += {
try { try {
val bufferedImage = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) val bufferedImage = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
@ -88,20 +87,21 @@ class ScreenshotTaker(
// }, // },
)) ))
} catch (exception: Exception) { } catch (exception: Exception) {
screenshotFail(exception) exception.fail()
} }
} }
} catch (exception: Exception) { } catch (exception: Exception) {
screenshotFail(exception) exception.fail()
} }
} }
private fun screenshotFail(exception: Throwable?) { private fun Throwable?.fail() {
exception?.printStackTrace() this?.printStackTrace()
renderWindow.sendDebugMessage("§cFailed to make a screenshot!") renderWindow.sendDebugMessage("§cFailed to make a screenshot!")
} }
companion object { companion object {
private val DATE_FORMATTER = SimpleDateFormat("yyyy-MM-dd_HH.mm.ss") private val DATE_FORMATTER = SimpleDateFormat("yyyy-MM-dd_HH.mm.ss")
private const val MAX_FILES_CHECK = 10
} }
} }