-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle
More file actions
71 lines (60 loc) · 2.25 KB
/
build.gradle
File metadata and controls
71 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
plugins {
id "java"
id "war"
id "org.teavm" version "0.11.0"
}
repositories {
mavenCentral()
}
dependencies {
implementation teavm.libs.jsoApis
implementation project(':game')
}
teavm {
// Available Java standard library: https://teavm.org/jcl-report/recent/jcl.html
// JavaDocs: https://javadoc.io/doc/org.teavm/
all {
mainClass = "org.ita23.pacman.WebMain"
}
js {
addedToWebApp = true
targetFileName = "pacman.js"
}
}
// Generates a source file with Base64 encoded string constants for all resources of the `game` project.
// This is then used to load the images via the `src`-tag and does not require any network/IO to load images.
task genBase64Resources(dependsOn: ":game:build") {
def outputDir = layout.buildDirectory.dir("generated/sources/webresources/main/java")
def outputFile = outputDir.get().file("org/ita23/pacman/Base64Resource.java").asFile
outputs.dir(outputDir)
def classpath = sourceSets.main.runtimeClasspath
def respath = project(":game").sourceSets.main.output.resourcesDir
doLast {
// TODO only load the one file?
def classLoader = new URLClassLoader(classpath.collect { it.toURI().toURL() } as URL[])
def containerClass = classLoader.loadClass("org.ita23.pacman.res.ImageResource");
def code = StringBuilder.newInstance()
code << "package org.ita23.pacman;\n"
code << "public final class Base64Resource {\n"
code << " private Base64Resource() {};\n"
code << " public static String getResource(String path){\n"
code << " switch (path) {\n"
containerClass.getEnumConstants().each {
code << "case \"" << it.resource_path << "\": "
code << "return \"data:image/png;base64,"
new File(respath, it.resource_path).withInputStream {
code << Base64.encoder.encodeToString(it.readAllBytes())
}
code << "\";\n"
}
code << " default: throw new RuntimeException(\"unknown resource_path: \"+path);\n"
code << " }\n"
code << " }\n"
code << "}\n"
outputFile.parentFile.mkdirs()
outputFile.text = code.toString()
}
}
sourceSets.main.java {
srcDir(genBase64Resources)
}