Skip to content

Commit

Permalink
feat: API font extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
opZywl committed Dec 21, 2024
1 parent c2cf651 commit 267626f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
43 changes: 35 additions & 8 deletions src/main/java/net/ccbluex/liquidbounce/ui/font/Fonts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,47 @@ object Fonts : MinecraftInstance {
}

private fun downloadFonts() {
val outputFile = File(fontsDir, "roboto.zip")
if (!outputFile.exists()) {
LOGGER.info("Downloading fonts...")
download("$CLIENT_CLOUD/fonts/Roboto.zip", outputFile)
LOGGER.info("Extract fonts...")
outputFile.extractZipTo(fontsDir)
val robotoZipFile = File(fontsDir, "roboto.zip")
if (!robotoZipFile.exists()) {
LOGGER.info("Downloading roboto fonts...")
download("$CLIENT_CLOUD/fonts/Roboto.zip", robotoZipFile)
LOGGER.info("Extract roboto fonts...")
robotoZipFile.extractZipTo(fontsDir)
}

val fontZipFile = File(fontsDir, "font.zip")
if (!fontZipFile.exists()) {
LOGGER.info("Downloading additional fonts...")
download("${FONTS}/Font.zip", fontZipFile)
LOGGER.info("Extracting additional fonts...")
fontZipFile.extractZipTo(fontsDir)
}

if(fontZipFile.exists()){
LOGGER.info("Font zip file exists, trying to extract...")
if(!fontsDir.exists()){
LOGGER.info("Fonts directory does not exist, trying to create...")
fontsDir.mkdirs()
}
try{
fontZipFile.extractZipTo(fontsDir){file ->
LOGGER.info("Extracted: ${file.absolutePath}")
}
val extractedFiles = fontsDir.listFiles { file -> file.isFile && file.name.endsWith(".ttf") }
if (extractedFiles != null && extractedFiles.isNotEmpty()) {
LOGGER.info("Fonts extracted successfully:")
extractedFiles.forEach{file ->
LOGGER.info(" - ${file.absolutePath}")
}
}else {
LOGGER.warn("No .ttf files extracted")
}
}catch (e:Exception){
LOGGER.error("Error during extraction", e)
}

}else{
LOGGER.warn("font not found")
}

}

fun getFontRenderer(name: String, size: Int): FontRenderer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ import java.util.zip.ZipInputStream

private fun ZipInputStream.entrySequence() = generateSequence { nextEntry }

fun File.extractZipTo(outputFolder: File) {
fun File.extractZipTo(outputFolder: File, fileExtracted: (File) -> Unit = {}) {
require(this.isFile) { "You can only extract from a file." }
require(outputFolder.isDirectory) { "You can only extract zip to a directory." }


outputFolder.apply {
if (!exists()) mkdirs()
}

ZipInputStream(inputStream()).use { zis ->
zis.entrySequence().forEach { entry ->

val newFile = File(outputFolder, entry.name)

if (!newFile.canonicalPath.startsWith(outputFolder.canonicalPath)) {
throw SecurityException("Illegal Zip Entry:${entry.name}")
}

if (entry.isDirectory) {
newFile.mkdirs()
} else {
newFile.parentFile.mkdirs()
newFile.parentFile?.mkdirs()
zis.copyTo(newFile.outputStream())
fileExtracted(newFile)
}
}
}
Expand Down

0 comments on commit 267626f

Please sign in to comment.