feat: support running the game from jar file

This commit is contained in:
Martin Prokoph
2025-09-06 21:27:15 +02:00
parent f6a94e6cd8
commit 918748b373
6 changed files with 29 additions and 9 deletions

View File

@@ -14,6 +14,11 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.jar {
manifest.attributes["Main-Class"] = "eu.midnightdust.yaytris.Tetris"
}
tasks.test {
useJUnitPlatform()
}

View File

@@ -2,6 +2,8 @@ package eu.midnightdust.yaytris;
import eu.midnightdust.yaytris.util.NightJson;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@@ -15,8 +17,14 @@ public class Translation {
* @see NightJson
*/
public static void load(String locale) {
NightJson json = new NightJson(Translation.class, String.format("translation/%s.json5", locale));
json.readJson();
String jsonString; // This workaround is needed to support reading translations from json files embedded in the .jar file
try (InputStream stream = Translation.class.getResourceAsStream(String.format("/translation/%s.json5", locale))) {
jsonString = stream != null ? new String(stream.readAllBytes()) : "";
} catch (IOException e) {
throw new RuntimeException(e);
}
NightJson json = new NightJson(Translation.class, "translation");
json.readJsonFromString(jsonString);
}
/**

View File

@@ -103,10 +103,6 @@ public class NightJson {
return String.format(type == String.class || type.isEnum() ? "\"%s\"" : "%s", value);
}
/**
* Read the json file from disk and overwrite the json class's field values.
*/
@SuppressWarnings("unchecked")
public void readJson() {
if (fileLocation == null) return;
try {
@@ -115,9 +111,20 @@ public class NightJson {
writeJson();
return;
}
readJsonFromString(Files.readString(file.toPath()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Read the json file from disk and overwrite the json class's field values.
*/
@SuppressWarnings("unchecked")
public void readJsonFromString(String jsonString) {
try {
Map<String, Object> asMap = jsonToMap(
Files.readString(file.toPath()).replaceAll("(//)+.*\n", ""), // Replace comment lines (Json5)
jsonString.replaceAll("(//)+.*\n", ""), // Replace comment lines (Json5)
(key) -> getField(key).isPresent() ? getField(key).get().getType() : String.class); // Determine data type
for (String key : asMap.keySet()) {
@@ -130,7 +137,7 @@ public class NightJson {
((Map<String, Object>)jsonMap.get(null)).put(key, value);
}
}
} catch (IOException | IllegalAccessException | NoSuchElementException | ClassCastException e) {
} catch (IllegalAccessException | NoSuchElementException | ClassCastException e) {
System.out.println("Oh no! An Error occurred whilst reading the JSON file :(");
//noinspection CallToPrintStackTrace
e.printStackTrace();

View File

@@ -41,7 +41,7 @@ public class SoundUtil {
* @param fileLocation the URI of the desired audio file (should be a .wav file)
*/
public static void playSoundClip(String fileLocation) {
try (AudioInputStream stream = AudioSystem.getAudioInputStream(getResource(fileLocation))) { // FIXME: Support audio files from JAR. File streams won't work here for some reason.
try (AudioInputStream stream = AudioSystem.getAudioInputStream(getResource(fileLocation))) {
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);