-
-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ResourceExtractor that extracts resource files from deplo…
…yed jar to local FS
- Loading branch information
Showing
2 changed files
with
88 additions
and
8 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
fxgl-core/src/main/kotlin/com/almasb/fxgl/core/util/ResourceExtractor.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,54 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB ([email protected]). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.core.util | ||
|
||
import com.almasb.fxgl.logging.Logger | ||
import java.net.URL | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
/** | ||
* Extracts resources from the deployed jar to the local file system. | ||
* | ||
* @author Almas Baim (https://github.com/AlmasB) | ||
*/ | ||
class ResourceExtractor { | ||
|
||
companion object { | ||
|
||
private val log = Logger.get(ResourceExtractor::class.java) | ||
|
||
/** | ||
* Extracts the file at jar [url] as a [relativeFilePath]. | ||
* | ||
* @return the url on the local file system of the extracted file | ||
*/ | ||
@JvmStatic fun extract(url: URL, relativeFilePath: String): URL { | ||
log.debug("Extracting $url as $relativeFilePath") | ||
|
||
val file = Paths.get(System.getProperty("user.home")) | ||
.resolve(".openjfx") | ||
.resolve("cache") | ||
.resolve("fxgl-21") | ||
.resolve(relativeFilePath) | ||
|
||
val fileParentDir = file.parent | ||
|
||
if (Files.notExists(fileParentDir)) { | ||
log.debug("Creating directories: $fileParentDir") | ||
|
||
Files.createDirectories(fileParentDir) | ||
} | ||
|
||
url.openStream().use { | ||
Files.copy(it, file) | ||
} | ||
|
||
return file.toUri().toURL() | ||
} | ||
} | ||
} |
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