6
6
7
7
package com .almasb .fxgl .intelligence ;
8
8
9
+ import com .almasb .fxgl .core .util .ResourceExtractor ;
9
10
import com .almasb .fxgl .logging .Logger ;
10
11
12
+ import java .net .URL ;
13
+ import java .util .HashMap ;
14
+ import java .util .List ;
15
+ import java .util .Map ;
16
+
11
17
/**
12
18
* Stores constants related to web-api projects.
13
19
* 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 {
18
24
19
25
private static final Logger log = Logger .get (WebAPI .class );
20
26
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" );
22
34
public static final String SPEECH_RECOGNITION_API = "https://almasb.github.io/web-api/speech-recog-v1/" ;
23
35
public static final String GESTURE_RECOGNITION_API = "https://almasb.github.io/web-api/gesture-recog-v1/" ;
24
36
25
37
public static final int TEXT_TO_SPEECH_PORT = 55550 ;
26
38
public static final int SPEECH_RECOGNITION_PORT = 55555 ;
27
39
public static final int GESTURE_RECOGNITION_PORT = 55560 ;
28
40
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 ;
31
53
}
32
54
33
- private static String getURL (String relativeURL ) {
55
+ private static URL extractURL (String relativeURL , String relateFilePath ) {
34
56
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
+ }
36
62
37
- if (url != null )
38
- return url ;
63
+ return ResourceExtractor .extract (url , relateFilePath );
39
64
40
65
} catch (Exception e ) {
41
66
log .warning ("Failed to get url: " + relativeURL , e );
42
67
}
43
68
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 );
45
71
}
46
72
}
0 commit comments