Skip to content

Commit

Permalink
Refactored resources loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunmag committed Jul 14, 2019
1 parent d2f5884 commit 6154501
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
23 changes: 3 additions & 20 deletions src/main/java/aunmag/shooter/core/gui/font/FontLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String>();

Expand All @@ -103,9 +88,7 @@ private void read(String name) throws IOException {
meta.putAll(data);
}
}
}

reader.close();
});
}

private int toInt(String string) {
Expand Down
32 changes: 4 additions & 28 deletions src/main/java/aunmag/shooter/core/structures/Shader.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
50 changes: 48 additions & 2 deletions src/main/java/aunmag/shooter/core/utilities/UtilsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String> 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<String> 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));
}
Expand Down

0 comments on commit 6154501

Please sign in to comment.