From 6154501b6479a364998b3a59744616ab5fe056de Mon Sep 17 00:00:00 2001 From: Aunmag Date: Sun, 14 Jul 2019 11:06:11 +0500 Subject: [PATCH] Refactored resources loading --- .../shooter/core/gui/font/FontLoader.java | 23 ++------- .../shooter/core/structures/Shader.java | 32 ++---------- .../shooter/core/utilities/UtilsFile.java | 50 ++++++++++++++++++- 3 files changed, 55 insertions(+), 50 deletions(-) diff --git a/src/main/java/aunmag/shooter/core/gui/font/FontLoader.java b/src/main/java/aunmag/shooter/core/gui/font/FontLoader.java index 6d13694..6dba1e5 100644 --- a/src/main/java/aunmag/shooter/core/gui/font/FontLoader.java +++ b/src/main/java/aunmag/shooter/core/gui/font/FontLoader.java @@ -2,11 +2,9 @@ import aunmag.shooter.core.Application; import aunmag.shooter.core.structures.Texture; +import aunmag.shooter.core.utilities.UtilsFile; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -67,20 +65,7 @@ public Font load(String name) throws Exception { } private void read(String name) throws IOException { - var reader = new BufferedReader( - new InputStreamReader( - getClass().getResourceAsStream("/fonts/" + name + ".fnt"), - StandardCharsets.UTF_8 - ) - ); - - while (true) { - var line = reader.readLine(); - - if (line == null) { - break; - } - + UtilsFile.readByLine("/fonts/" + name + ".fnt", line -> { var type = (String) null; var data = new HashMap(); @@ -103,9 +88,7 @@ private void read(String name) throws IOException { meta.putAll(data); } } - } - - reader.close(); + }); } private int toInt(String string) { diff --git a/src/main/java/aunmag/shooter/core/structures/Shader.java b/src/main/java/aunmag/shooter/core/structures/Shader.java index 8e1e709..9fe706b 100644 --- a/src/main/java/aunmag/shooter/core/structures/Shader.java +++ b/src/main/java/aunmag/shooter/core/structures/Shader.java @@ -1,15 +1,12 @@ package aunmag.shooter.core.structures; +import aunmag.shooter.core.utilities.UtilsFile; import org.jetbrains.annotations.Nullable; import org.joml.Matrix4fc; import org.joml.Vector4fc; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL20; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.FloatBuffer; import java.util.ArrayList; import java.util.List; @@ -23,33 +20,12 @@ public class Shader { private final int programFragmentId; @Nullable - private static String readFile(Class resourceClass, String type) { - String filename = resourceClass.getSimpleName() + '.' + type; - StringBuilder stringBuilder = new StringBuilder(); - + private static String readFile(Class cls, String type) { try { - InputStream inputStream = resourceClass.getResourceAsStream(filename); - InputStreamReader inputStreamReader = new InputStreamReader(inputStream); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); - - String line; - while ((line = bufferedReader.readLine()) != null) { - stringBuilder.append(line); - stringBuilder.append("\n"); - } - - bufferedReader.close(); - } catch (IOException | NullPointerException ex) { - String message = String.format( - "Can't find shader \"%s\" near \"%s\" class!", - filename, - resourceClass.getName() - ); - System.err.println(message); + return UtilsFile.read(cls.getSimpleName() + '.' + type, cls); + } catch (Exception e) { return null; } - - return stringBuilder.toString(); } protected Shader(Class resourceClass) { diff --git a/src/main/java/aunmag/shooter/core/utilities/UtilsFile.java b/src/main/java/aunmag/shooter/core/utilities/UtilsFile.java index 6244c4f..b3bc2a9 100644 --- a/src/main/java/aunmag/shooter/core/utilities/UtilsFile.java +++ b/src/main/java/aunmag/shooter/core/utilities/UtilsFile.java @@ -2,25 +2,37 @@ import org.lwjgl.BufferUtils; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URISyntaxException; import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.function.Consumer; public final class UtilsFile { + public static final Charset CHARSET = StandardCharsets.UTF_8; + private UtilsFile() {} - public static Path toPath(String path) throws IOException { + public static Path toPath(String path, Class cls) throws IOException { try { - return Paths.get(UtilsFile.class.getResource(path).toURI()); + return Paths.get(cls.getResource(path).toURI()); } catch (URISyntaxException | NullPointerException e) { throw new IOException(e.getMessage(), e.getCause()); } } + public static Path toPath(String path) throws IOException { + return toPath(path, UtilsFile.class); + } + public static ByteBuffer readByteBuffer(String path) throws IOException { var channel = Files.newByteChannel(toPath(path)); var buffer = BufferUtils.createByteBuffer((int) channel.size() + 1); @@ -37,6 +49,40 @@ public static ByteBuffer readByteBuffer(String path) throws IOException { return buffer; } + public static String read(String path, Class cls) throws IOException { + return new String(Files.readAllBytes(toPath(path, cls)), CHARSET); + } + + public static String read(String path) throws IOException { + return read(path, UtilsFile.class); + } + + public static void readByLine( + InputStream stream, + Consumer consumer + ) throws IOException { + var reader = new BufferedReader(new InputStreamReader(stream, CHARSET)); + + while (true) { + var line = reader.readLine(); + + if (line == null) { + break; + } + + consumer.accept(line); + } + + reader.close(); + } + + public static void readByLine( + String path, + Consumer consumer + ) throws IOException { + readByLine(UtilsFile.class.getResourceAsStream(path), consumer); + } + public static void printReadError(String path) { System.err.println(String.format("Failed to read file from \"%s\"", path)); }