|
| 1 | +/* |
| 2 | + * Ultralight Java - Java wrapper for the Ultralight web engine |
| 3 | + * Copyright (C) 2020 - 2021 LabyMedia and contributors |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.labymedia.ultralight.example.base; |
| 21 | + |
| 22 | +import com.labymedia.ultralight.UltralightJava; |
| 23 | +import com.labymedia.ultralight.UltralightLoadException; |
| 24 | +import com.labymedia.ultralight.gpu.UltralightGPUDriverNativeUtil; |
| 25 | +import org.lwjgl.glfw.GLFW; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.nio.file.Paths; |
| 32 | +import java.nio.file.StandardCopyOption; |
| 33 | + |
| 34 | +public abstract class UltralightExampleBase { |
| 35 | + |
| 36 | + private final UltralightExampleConfiguration ultralightExampleConfiguration; |
| 37 | + |
| 38 | + public UltralightExampleBase() { |
| 39 | + try { |
| 40 | + |
| 41 | + this.ultralightExampleConfiguration = new UltralightExampleConfiguration(); |
| 42 | + this.setupNatives(); |
| 43 | + |
| 44 | + this.configure(this.ultralightExampleConfiguration); |
| 45 | + |
| 46 | + // Example resources |
| 47 | + extractResources(); |
| 48 | + this.begin(); |
| 49 | + } catch (UltralightLoadException exception) { |
| 50 | + //Process will end here. Just wrapped in a RuntimeException so no example has to create a constructor |
| 51 | + throw new RuntimeException(exception); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void setupNatives() throws UltralightLoadException { |
| 56 | + // Get a directory to put natives into |
| 57 | + Path nativesDir = Paths.get("."); |
| 58 | + |
| 59 | + // Get the existing native library path |
| 60 | + String libraryPath = System.getProperty("java.library.path"); |
| 61 | + if (libraryPath != null) { |
| 62 | + // There is a path set already, append our natives dir |
| 63 | + libraryPath += File.pathSeparator + nativesDir.toAbsolutePath().toString(); |
| 64 | + } else { |
| 65 | + // There is no path set, make our natives dir the current path |
| 66 | + libraryPath = nativesDir.toAbsolutePath().toString(); |
| 67 | + } |
| 68 | + |
| 69 | + // Set the path back |
| 70 | + System.setProperty("java.library.path", libraryPath); |
| 71 | + |
| 72 | + // Extract the natives |
| 73 | + // |
| 74 | + // This only extracts the native library for ultralight-java-base, but not the other Ultralight libraries. |
| 75 | + // It is your task to get them into the run directory, possibly by extracting them on your own. |
| 76 | + UltralightJava.extractNativeLibrary(nativesDir); |
| 77 | + |
| 78 | + // Load the native libraries from the given directory. This method makes sure everything is loaded in the |
| 79 | + // correct order. If you want to manually load all natives, either don't use this function or pass 'false' as |
| 80 | + // the second parameter. |
| 81 | + UltralightJava.load(nativesDir); |
| 82 | + UltralightGPUDriverNativeUtil.extractAndLoadNativeLibraries(nativesDir); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + public void configure(UltralightExampleConfiguration ultralightExampleConfiguration) { |
| 87 | + } |
| 88 | + |
| 89 | + public void begin() { |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Helper function to set up the run directory with jar resources. |
| 94 | + */ |
| 95 | + private void extractResources() { |
| 96 | + try { |
| 97 | + for (String path : this.ultralightExampleConfiguration.getResourcesToExtract()) { |
| 98 | + Files.copy( |
| 99 | + UltralightExampleBase.class.getResourceAsStream("/" + path), |
| 100 | + Paths.get("./" + path), |
| 101 | + StandardCopyOption.REPLACE_EXISTING |
| 102 | + ); |
| 103 | + } |
| 104 | + } catch (IOException e) { |
| 105 | + throw new RuntimeException(e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments