-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EXPERIMENTAL: Implement cache for profile assets
- Loading branch information
1 parent
8bca9db
commit 3f600c4
Showing
13 changed files
with
562 additions
and
44 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
cirno-serializable/src/main/kotlin/net/cakeyfox/serializable/data/ImagePosition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.cakeyfox.serializable.data | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ImagePosition( | ||
val x: Float, | ||
val y: Float, | ||
val arc: Arc? = null, | ||
) | ||
|
||
@Serializable | ||
data class Arc( | ||
val x: Float, | ||
val y: Float, | ||
val radius: Int, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
foxy/src/main/kotlin/net/cakeyfox/foxy/utils/image/ImageUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package net.cakeyfox.foxy.utils.image | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import mu.KotlinLogging | ||
import net.cakeyfox.foxy.utils.profile.ProfileCacheManager | ||
import net.cakeyfox.serializable.data.ImagePosition | ||
import java.awt.Color | ||
import java.awt.Font | ||
import java.awt.Graphics2D | ||
import java.awt.image.BufferedImage | ||
import java.io.InputStream | ||
import java.net.URL | ||
import javax.imageio.ImageIO | ||
import kotlin.reflect.jvm.jvmName | ||
|
||
object ImageUtils { | ||
private val logger = KotlinLogging.logger(this::class.jvmName) | ||
|
||
fun getFont(fontName: String, fontSize: Int): Font? { | ||
val fontStream: InputStream? = this::class.java.classLoader.getResourceAsStream("fonts/$fontName.ttf") | ||
|
||
return if (fontStream != null) { | ||
try { | ||
Font.createFont(Font.TRUETYPE_FONT, fontStream).deriveFont(Font.PLAIN, fontSize.toFloat()) | ||
} catch (e: Exception) { | ||
logger.error(e) { "Can't load font $fontName " } | ||
null | ||
} | ||
} else { | ||
logger.error { "$fontName font not found on resources path" } | ||
null | ||
} | ||
} | ||
|
||
suspend fun loadImageFromURL(url: String): BufferedImage { | ||
return withContext(Dispatchers.IO) { | ||
try { | ||
ProfileCacheManager.imageCache.get(url) { | ||
downloadImage(url) | ||
} | ||
} catch (e: Exception) { | ||
logger.error(e) { "Error loading image from $url" } | ||
BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB) | ||
} | ||
} | ||
} | ||
|
||
private fun downloadImage(url: String): BufferedImage { | ||
try { | ||
val connection = URL(url).openConnection().apply { | ||
setRequestProperty("User-Agent", "Mozilla/5.0") | ||
connectTimeout = 5000 | ||
readTimeout = 5000 | ||
} | ||
|
||
return ImageIO.read(connection.inputStream) | ||
} catch (e: Exception) { | ||
logger.error(e) { "Error downloading image from $url" } | ||
return BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB) | ||
} | ||
} | ||
|
||
|
||
fun Graphics2D.drawTextWithFont(width: Int, height: Int, textConfig: TextConfig.() -> Unit) { | ||
val config = TextConfig().apply(textConfig) | ||
this.font = getFont(config.fontFamily, config.fontSize) ?: Font("SansSerif", Font.PLAIN, config.fontSize) | ||
this.color = color | ||
this.drawString(config.text, (width / config.textPosition.x), (height / config.textPosition.y)) | ||
} | ||
|
||
data class TextConfig( | ||
var text: String = "", | ||
var fontSize: Int = 16, | ||
var fontFamily: String = "SansSerif", | ||
var fontColor: Color = java.awt.Color.WHITE, | ||
var textPosition: ImagePosition = ImagePosition(0f, 0f, null) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
foxy/src/main/kotlin/net/cakeyfox/foxy/utils/profile/ProfileCacheManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.cakeyfox.foxy.utils.profile | ||
|
||
import com.github.benmanes.caffeine.cache.Cache | ||
import com.github.benmanes.caffeine.cache.Caffeine | ||
import net.cakeyfox.foxy.utils.image.ImageUtils | ||
import net.cakeyfox.serializable.database.data.Background | ||
import net.cakeyfox.serializable.database.data.Badge | ||
import net.cakeyfox.serializable.database.data.Decoration | ||
import net.cakeyfox.serializable.database.data.Layout | ||
import java.awt.image.BufferedImage | ||
|
||
object ProfileCacheManager { | ||
val backgroundCache: Cache<String, Background> = Caffeine.newBuilder().build() | ||
val layoutCache: Cache<String, Layout> = Caffeine.newBuilder().build() | ||
val badgesCache: Cache<String, List<Badge>> = Caffeine.newBuilder().build() | ||
val decorationCache: Cache<String, Decoration> = Caffeine.newBuilder().build() | ||
val imageCache: Cache<String, BufferedImage> = Caffeine.newBuilder() | ||
.maximumSize(100) | ||
.build() | ||
|
||
suspend fun loadImageFromCache(url: String): BufferedImage? { | ||
return imageCache.getIfPresent(url) ?: run { | ||
val image = ImageUtils.loadImageFromURL(url) | ||
imageCache.put(url, image) | ||
image | ||
} | ||
} | ||
} |
Oops, something went wrong.