mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Can include resources in the generated file
This commit is contained in:
parent
4aec89967b
commit
d9ee68315e
|
@ -29,5 +29,6 @@ public class HTML4JPlugin implements TeaVMPlugin {
|
|||
host.add(new JavaScriptBodyTransformer());
|
||||
host.add(new JCLHacks());
|
||||
host.add(new JavaScriptResourceInterceptor());
|
||||
host.add(new ResourcesInterceptor());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright 2014 Alexey Andreev.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.teavm.html4j;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Base64;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.teavm.codegen.SourceWriter;
|
||||
import org.teavm.javascript.RenderingContext;
|
||||
import org.teavm.vm.BuildTarget;
|
||||
import org.teavm.vm.spi.AbstractRendererListener;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jaroslav Tulach
|
||||
*/
|
||||
public class ResourcesInterceptor extends AbstractRendererListener {
|
||||
private final Set<String> processed = new HashSet<>();
|
||||
@Override
|
||||
public void begin(RenderingContext context, BuildTarget buildTarget) throws IOException {
|
||||
boolean hasOneResource = false;
|
||||
for (String className : context.getClassSource().getClassNames()) {
|
||||
final int lastDot = className.lastIndexOf('.');
|
||||
if (lastDot == -1) {
|
||||
continue;
|
||||
}
|
||||
String packageName = className.substring(0, lastDot);
|
||||
String resourceName = packageName.replace('.', '/') + "/" + "jvm.txt";
|
||||
try (InputStream input = context.getClassLoader().getResourceAsStream(resourceName)) {
|
||||
if (input == null || !processed.add(resourceName)) {
|
||||
continue;
|
||||
}
|
||||
ByteArrayOutputStream arr = new ByteArrayOutputStream();
|
||||
IOUtils.copy(input, arr);
|
||||
String base64 = Base64.getEncoder().encodeToString(arr.toByteArray());
|
||||
input.close();
|
||||
final SourceWriter w = context.getWriter();
|
||||
w.append("// Resource " + resourceName + " included by " + className).newLine();
|
||||
w.append("if (!window.teaVMResources) window.teaVMResources = {};").newLine();
|
||||
w.append("window.teaVMResources['" + resourceName + "'] = '");
|
||||
w.append(base64).append("';").newLine().newLine();
|
||||
}
|
||||
hasOneResource = true;
|
||||
}
|
||||
if (hasOneResource) {
|
||||
context.getWriter().append("// TeaVM generated classes").newLine();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user