From 191066f65c486bf2a450ae44bd5f573884547785 Mon Sep 17 00:00:00 2001 From: Brendan Robert Date: Tue, 23 Jan 2024 21:44:49 -0600 Subject: [PATCH] Fix loading issues for OGG on windows --- .vscode/settings.json | 3 ++- .../jace/src/main/java/jace/core/Utility.java | 7 ++++-- .../src/main/java/jace/hardware/Joystick.java | 3 +++ .../src/main/java/jace/lawless/Media.java | 23 ++++++++++--------- .../src/test/java/jace/AbstractFXTest.java | 12 ++++++++++ .../jace/src/test/java/jace/SoundTest.java | 4 ++-- 6 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 Platform/Apple/tools/jace/src/test/java/jace/AbstractFXTest.java diff --git a/.vscode/settings.json b/.vscode/settings.json index e0f15db2..9bd06c2e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "java.configuration.updateBuildConfiguration": "automatic" + "java.configuration.updateBuildConfiguration": "automatic", + "java.debug.settings.onBuildFailureProceed": true } \ No newline at end of file diff --git a/Platform/Apple/tools/jace/src/main/java/jace/core/Utility.java b/Platform/Apple/tools/jace/src/main/java/jace/core/Utility.java index 3ad392b0..b6174ab5 100644 --- a/Platform/Apple/tools/jace/src/main/java/jace/core/Utility.java +++ b/Platform/Apple/tools/jace/src/main/java/jace/core/Utility.java @@ -158,7 +158,10 @@ public class Utility { if (isHeadless) { return Optional.empty(); } - Image img = loadIcon(filename).get(); + Optional img = loadIcon(filename); + if (img.isEmpty()) { + return Optional.empty(); + } Label label = new Label() { @Override public boolean equals(Object obj) { @@ -174,7 +177,7 @@ public class Utility { return getText().hashCode(); } }; - label.setGraphic(new ImageView(img)); + label.setGraphic(new ImageView(img.get())); label.setAlignment(Pos.CENTER); label.setContentDisplay(ContentDisplay.TOP); label.setTextFill(Color.WHITE); diff --git a/Platform/Apple/tools/jace/src/main/java/jace/hardware/Joystick.java b/Platform/Apple/tools/jace/src/main/java/jace/hardware/Joystick.java index d9dc60a3..fabaedca 100644 --- a/Platform/Apple/tools/jace/src/main/java/jace/hardware/Joystick.java +++ b/Platform/Apple/tools/jace/src/main/java/jace/hardware/Joystick.java @@ -59,6 +59,9 @@ public class Joystick extends Device { public Joystick(int port, Computer computer) { super(computer); + if (LawlessLegends.getApplication() == null) { + return; + } Stage stage = LawlessLegends.getApplication().primaryStage; // Register a mouse handler on the primary stage that tracks the // mouse x/y position as a percentage of window width and height diff --git a/Platform/Apple/tools/jace/src/main/java/jace/lawless/Media.java b/Platform/Apple/tools/jace/src/main/java/jace/lawless/Media.java index 8b1fcdc2..f136e43d 100644 --- a/Platform/Apple/tools/jace/src/main/java/jace/lawless/Media.java +++ b/Platform/Apple/tools/jace/src/main/java/jace/lawless/Media.java @@ -2,6 +2,8 @@ package jace.lawless; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; @@ -21,21 +23,20 @@ public class Media { File tempFile; public Media(String resourcePath) throws IOException { - // Copy resource to temp file because STBVorbis can't read from jar - tempFile = File.createTempFile("temp", ".ogg"); - tempFile.deleteOnExit(); - getClass().getResource(resourcePath).openStream().transferTo(new java.io.FileOutputStream(tempFile)); - String canonicalPath = tempFile.getAbsolutePath(); - - // Get caononical file path from relative resource path - // String canonicalPath = URLDecoder.decode(getClass().getResource(resourcePath).getPath(), "UTF-8"); - System.out.println("Loading media: " + canonicalPath); + System.out.println("Loading media: " + resourcePath); + byte[] oggFile; + try (InputStream oggStream = getClass().getResourceAsStream(resourcePath)) { + oggFile = oggStream.readAllBytes(); + } try (MemoryStack stack = MemoryStack.stackPush()) { + ByteBuffer oggBuffer = MemoryUtil.memAlloc(oggFile.length); + oggBuffer.put(oggFile); + oggBuffer.flip(); IntBuffer error = stack.callocInt(1); - Long decoder = STBVorbis.stb_vorbis_open_filename(canonicalPath, error, null); + Long decoder = STBVorbis.stb_vorbis_open_memory(oggBuffer, error, null); if (decoder == null || decoder <= 0) { - throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + getError(error.get(0))); + throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + getError(error.get(0)) + " -- file is located at " + resourcePath); } STBVorbisInfo info = STBVorbisInfo.malloc(stack); STBVorbis.stb_vorbis_get_info(decoder, info); diff --git a/Platform/Apple/tools/jace/src/test/java/jace/AbstractFXTest.java b/Platform/Apple/tools/jace/src/test/java/jace/AbstractFXTest.java new file mode 100644 index 00000000..c2cdc226 --- /dev/null +++ b/Platform/Apple/tools/jace/src/test/java/jace/AbstractFXTest.java @@ -0,0 +1,12 @@ +package jace; + +import org.junit.BeforeClass; + +import javafx.application.Platform; + +public abstract class AbstractFXTest { + @BeforeClass + public static void initJfxRuntime() { + Platform.startup(() -> {}); + } +} diff --git a/Platform/Apple/tools/jace/src/test/java/jace/SoundTest.java b/Platform/Apple/tools/jace/src/test/java/jace/SoundTest.java index 3edc3b31..07ccacbb 100644 --- a/Platform/Apple/tools/jace/src/test/java/jace/SoundTest.java +++ b/Platform/Apple/tools/jace/src/test/java/jace/SoundTest.java @@ -9,7 +9,7 @@ import jace.core.SoundMixer.SoundBuffer; import jace.lawless.LawlessHacks; import jace.lawless.Media; -public class SoundTest { +public class SoundTest extends AbstractFXTest { @Test public void musicDecodeTest() { // For every song in the music folder, decode it and print out the duration @@ -25,7 +25,7 @@ public class SoundTest { } } - // @Test + //@Test (Only use this to ensure the sound engine produces audible output, it's otherwise annoying to hear all the time) public void soundGenerationTest() { try { System.out.println("Performing sound test...");