Skip to content

Commit

Permalink
add js and wasm in the same file. Remove adding resource manually
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jul 30, 2023
1 parent 77a3344 commit 892e3d0
Show file tree
Hide file tree
Showing 17 changed files with 1,464 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Stream;

Expand All @@ -43,7 +45,7 @@ public static void copy(ArrayList<File> assetsPaths, AssetFilter filter, String
copy(null, null, assetsPaths, null, assetsOutputPath, generateTextFile);
}

public static void copy(TeaClassLoader classloader, ArrayList<String> classPathAssetsFiles, ArrayList<File> assetsPaths, AssetFilter filter, String assetsOutputPath, boolean generateTextFile) {
public static void copy(TeaClassLoader classloader, List<String> classPathAssetsFiles, ArrayList<File> assetsPaths, AssetFilter filter, String assetsOutputPath, boolean generateTextFile) {
assetsOutputPath = assetsOutputPath.replace("\\", "/");
FileWrapper target = new FileWrapper(assetsOutputPath);
ArrayList<Asset> assets = new ArrayList<Asset>();
Expand Down Expand Up @@ -133,7 +135,7 @@ public static void copy(TeaClassLoader classloader, ArrayList<String> classPathA
}
}

private static void addDirectoryClassPathFiles(ArrayList<String> classPathFiles) {
private static void addDirectoryClassPathFiles(List<String> classPathFiles) {
ArrayList<String> folderFilePaths = new ArrayList<>();
for(int k = 0; k < classPathFiles.size(); k++) {
String classpathFile = classPathFiles.get(k);
Expand Down Expand Up @@ -219,6 +221,11 @@ private static void addDirectoryClassPathFiles(ArrayList<String> classPathFiles)
}
}
classPathFiles.addAll(folderFilePaths);
//Hack to remove duplicates.
// Fixme fix/improve asset copy
HashSet<String> set = new HashSet<>(folderFilePaths);
classPathFiles.clear();
classPathFiles.addAll(set);
}

private static void copyFile(FileWrapper source, FileWrapper dest, AssetFilter filter, ArrayList<Asset> assets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -121,7 +130,7 @@ public static TeaVMTool config(TeaVMTool tool, TeaBuildConfiguration configurati
TeaClassLoader classLoader = new TeaClassLoader(classPaths, TeaBuilder.class.getClassLoader(), skipClasses);

configTool(tool, classLoader, configuration, webappDirectory, webappName, progressListener);
configAssets(classLoader, configuration, webappDirectory, webappName);
configAssets(classLoader, configuration, webappDirectory, webappName, acceptedURL);

return tool;
}
Expand Down Expand Up @@ -385,25 +394,6 @@ private static void assetsDefaultClasspath(ArrayList<String> filePath) {
filePath.add("net/mgsx/gltf/shaders/pbr/shadows.glsl");
}

private static void scriptsDefault(ArrayList<String> filePath) {
filePath.add("gdx.js");
filePath.add("gdx.wasm.js");
filePath.add("gdx.wasm.wasm");
filePath.add("soundmanager2-jsmin.js");
filePath.add("freetype.js");
filePath.add("bullet.js");
filePath.add("bullet.wasm.js");
filePath.add("bullet.wasm.wasm");
filePath.add("imgui.js");
filePath.add("imgui.wasm.js");
filePath.add("imgui.wasm.wasm");
filePath.add("box2D.js");
filePath.add("box2D.wasm.js");
filePath.add("box2D.wasm.wasm");
filePath.add("exampleLib.js");
filePath.add("exampleLib.wasm.wasm");
}

private static ACCEPT_STATE acceptPath(String path) {
ACCEPT_STATE isValid = ACCEPT_STATE.NO_MATCH;
if(path.contains("junit"))
Expand Down Expand Up @@ -556,7 +546,7 @@ public TeaVMProgressFeedback progressReached(int i) {
preserveClasses(tool, configuration, classLoader);
}

public static void configAssets(TeaClassLoader classLoader, TeaBuildConfiguration configuration, String webappDirectory, String webappName) {
public static void configAssets(TeaClassLoader classLoader, TeaBuildConfiguration configuration, String webappDirectory, String webappName, ArrayList<URL> acceptedURL) {
ArrayList<String> webappAssetsFiles = new ArrayList<>();
webappAssetsFiles.add(webappName);
TeaBuilder.logHeader("COPYING ASSETS");
Expand Down Expand Up @@ -588,7 +578,6 @@ public static void configAssets(TeaClassLoader classLoader, TeaBuildConfiguratio
ArrayList<String> classPathAssetsFiles = new ArrayList<>();
ArrayList<String> classPathScriptFiles = new ArrayList<>();
assetsDefaultClasspath(classPathAssetsFiles);
scriptsDefault(classPathScriptFiles);
if (configuration.isShowLoadingLogo()) {
classPathScriptFiles.add(logo);
}
Expand All @@ -597,8 +586,85 @@ public static void configAssets(TeaClassLoader classLoader, TeaBuildConfiguratio
boolean generateAssetPaths = configuration.assetsPath(assetsPaths);
AssetsCopy.copy(classLoader, classPathAssetsFiles, assetsPaths, filter, assetsOutputPath, generateAssetPaths);

// Copy Scripts
AssetsCopy.copy(classLoader, classPathScriptFiles, null, null, scriptsOutputPath, false);
// Copy assets from resources
List<String> pathsFromResource = getPathsFromResource(acceptedURL);
AssetsCopy.copy(classLoader, pathsFromResource, null, null, assetsOutputPath, false);
TeaBuilder.log("");
}


private static ArrayList<String> getPathsFromResource(String folder) {
ArrayList<String> result = new ArrayList<>();

String jarPath = null;
try {
jarPath = TeaBuilder.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.toURI()
.getPath();
} catch(URISyntaxException e) {
throw new RuntimeException(e);
}
System.out.println("JAR Path :" + jarPath);
// file walks JAR
URI urii = URI.create("jar:file:" + jarPath);

return result;
}

private static ArrayList<String> getPathsFromResource(ArrayList<URL> acceptedURL) {
ArrayList<URI> filteredUrl = new ArrayList<>();
for(URL url : acceptedURL) {
String path = url.getPath();
boolean accept = !(
!(path.endsWith(".jar")) ||
path.contains("org.teavm"
));
if(accept) {
URI uri = URI.create("jar:file:" + path);
filteredUrl.add(uri);
}
}
ArrayList<String> result = new ArrayList<>();
for(URI uri : filteredUrl) {
ArrayList<String> pathsFromResource = getPathsFromResource(uri, ".");
result.addAll(pathsFromResource);
}
return result;
}

private static ArrayList<String> getPathsFromResource(URI uri, String folder) {
ArrayList<String> result = new ArrayList<>();
List<Path> resultPath = null;

try(FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
resultPath = Files.walk(fs.getPath(folder))
.filter(Files::isRegularFile)
.filter(new Predicate<Path>() {
@Override
public boolean test(Path path) {
String pathStr = path.toString();
boolean isValid = !(
pathStr.endsWith(".java") ||
pathStr.endsWith(".class") ||
pathStr.contains("META-INF") ||
pathStr.contains("WEB-INF") ||
pathStr.endsWith(".html")
);
return isValid;
}
})
.collect(Collectors.toList());
} catch(IOException e) {
throw new RuntimeException(e);
}
if(resultPath != null) {
for(Path path : resultPath) {
result.add(path.toString());
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public boolean onSuccess(String urll, Object result) {
}

public void loadScript(boolean async, final String url, final AssetLoaderListener<Object> listener) {
AssetDownloader.getInstance().loadScript(async, baseUrl + url, listener);
AssetDownloader.getInstance().loadScript(async, getAssetUrl() + url, listener);
}

protected void putAssetInMap(AssetType type, String url, Object result) {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object LibExt {
const val gdxVersion = "1.12.0"
const val teaVMVersion = "0.9.0-dev-7"

const val jParserVersion = "1.0.0-SNAPSHOT"
const val jParserVersion = "1.0.0-b3"

const val gdxImGuiVersion = "1.0.0-SNAPSHOT"
const val gdxFrameViewportVersion = "1.0.0-SNAPSHOT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static void main(String[] args) {
TeaApplicationConfiguration config = new TeaApplicationConfiguration("canvas");
config.width = 0;
config.height = 0;
config.showDownloadLogs = true;
new TeaApplication(new PyramidTest(), config);
}
}
1 change: 1 addition & 0 deletions extensions/gdx-box2d/gdx-box2d-build/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(EMCC_ARGS
set(EMCC_WASM_ARGS ${EMCC_ARGS}
-s BINARYEN_IGNORE_IMPLICIT_TRAPS=1
-s WASM=1
-s SINGLE_FILE
)

set(EMCC_GLUE_ARGS
Expand Down
5 changes: 2 additions & 3 deletions extensions/gdx-box2d/gdx-box2d-build/jni/build.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cmake -G "MinGW Makefiles" -B ./build/emscripten/
cmake --build ./build/emscripten/ -- VERBOSE=1
XCOPY build\emscripten\box2d.js ..\..\gdx-box2d-teavm\resources\ /y
XCOPY build\emscripten\box2d.wasm.js ..\..\gdx-box2d-teavm\resources\ /y
XCOPY build\emscripten\box2d.wasm.wasm ..\..\gdx-box2d-teavm\resources\ /y
XCOPY build\emscripten\box2d.js ..\..\gdx-box2d-teavm\src\main\resources\ /y
XCOPY build\emscripten\box2d.wasm.js ..\..\gdx-box2d-teavm\src\main\resources\ /y
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public static void main(String[] args) throws Exception {
IDLReader idlReader = IDLReader.readIDL(path);

String basePath = new File(".").getAbsolutePath();
JParser.generate(new Box2dParser(idlReader), basePath + "./gdx-box2d-base/src", "../gdx-box2d-teavm/src", null);
JParser.generate(new Box2dParser(idlReader), basePath + "./gdx-box2d-base/src/main/java", "../gdx-box2d-teavm/src/main/java", null);
}
}
Loading

0 comments on commit 892e3d0

Please sign in to comment.