Skip to content

Commit cc84968

Browse files
committed
feat: added ResourceExtractor that extracts resource files from deployed jar to local FS
1 parent 81b336e commit cc84968

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB ([email protected]).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.core.util
8+
9+
import com.almasb.fxgl.logging.Logger
10+
import java.net.URL
11+
import java.nio.file.Files
12+
import java.nio.file.Paths
13+
14+
/**
15+
* Extracts resources from the deployed jar to the local file system.
16+
*
17+
* @author Almas Baim (https://github.com/AlmasB)
18+
*/
19+
class ResourceExtractor {
20+
21+
companion object {
22+
23+
private val log = Logger.get(ResourceExtractor::class.java)
24+
25+
/**
26+
* Extracts the file at jar [url] as a [relativeFilePath].
27+
*
28+
* @return the url on the local file system of the extracted file
29+
*/
30+
@JvmStatic fun extract(url: URL, relativeFilePath: String): URL {
31+
log.debug("Extracting $url as $relativeFilePath")
32+
33+
val file = Paths.get(System.getProperty("user.home"))
34+
.resolve(".openjfx")
35+
.resolve("cache")
36+
.resolve("fxgl-21")
37+
.resolve(relativeFilePath)
38+
39+
val fileParentDir = file.parent
40+
41+
if (Files.notExists(fileParentDir)) {
42+
log.debug("Creating directories: $fileParentDir")
43+
44+
Files.createDirectories(fileParentDir)
45+
}
46+
47+
url.openStream().use {
48+
Files.copy(it, file)
49+
}
50+
51+
return file.toUri().toURL()
52+
}
53+
}
54+
}

fxgl-intelligence/src/main/java/com/almasb/fxgl/intelligence/WebAPI.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66

77
package com.almasb.fxgl.intelligence;
88

9+
import com.almasb.fxgl.core.util.ResourceExtractor;
910
import com.almasb.fxgl.logging.Logger;
1011

12+
import java.net.URL;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
1117
/**
1218
* Stores constants related to web-api projects.
1319
* Changes to these values must be synchronized with the web-api project (https://github.com/AlmasB/web-api).
@@ -18,29 +24,49 @@ public final class WebAPI {
1824

1925
private static final Logger log = Logger.get(WebAPI.class);
2026

21-
public static final String TEXT_TO_SPEECH_API = getURL("tts/index.html");
27+
/**
28+
* K - URL relative to resources/com.almasb.fxgl.intelligence in jar.
29+
* V - absolute URL on the file system.
30+
*/
31+
private static final Map<String, URL> URLS = extractURLs();
32+
33+
public static final URL TEXT_TO_SPEECH_API = URLS.get("tts/index.html");
2234
public static final String SPEECH_RECOGNITION_API = "https://almasb.github.io/web-api/speech-recog-v1/";
2335
public static final String GESTURE_RECOGNITION_API = "https://almasb.github.io/web-api/gesture-recog-v1/";
2436

2537
public static final int TEXT_TO_SPEECH_PORT = 55550;
2638
public static final int SPEECH_RECOGNITION_PORT = 55555;
2739
public static final int GESTURE_RECOGNITION_PORT = 55560;
2840

29-
static {
30-
log.debug("TTS API: " + TEXT_TO_SPEECH_API + ":" + TEXT_TO_SPEECH_PORT);
41+
private static Map<String, URL> extractURLs() {
42+
var map = new HashMap<String, URL>();
43+
44+
List.of(
45+
"rpc-common.js",
46+
"tts/index.html",
47+
"tts/script.js"
48+
).forEach(relativeURL -> {
49+
map.put(relativeURL, extractURL(relativeURL, "intelligence/" + relativeURL));
50+
});
51+
52+
return map;
3153
}
3254

33-
private static String getURL(String relativeURL) {
55+
private static URL extractURL(String relativeURL, String relateFilePath) {
3456
try {
35-
var url = WebAPI.class.getResource(relativeURL).toExternalForm();
57+
var url = WebAPI.class.getResource(relativeURL);
58+
59+
if (url == null) {
60+
throw new IllegalArgumentException("URL is null: " + relativeURL);
61+
}
3662

37-
if (url != null)
38-
return url;
63+
return ResourceExtractor.extract(url, relateFilePath);
3964

4065
} catch (Exception e) {
4166
log.warning("Failed to get url: " + relativeURL, e);
4267
}
4368

44-
return "DUMMY_URL";
69+
// TODO: github URLs, e.g. https://raw.githubusercontent.com/AlmasB/FXGL/dev/fxgl-intelligence/src/main/resources/com/almasb/fxgl/intelligence/rpc-common.js
70+
throw new IllegalArgumentException("Failed to extract URL: " + relativeURL);
4571
}
4672
}

0 commit comments

Comments
 (0)