implemented Engine.watchFile

This commit is contained in:
DaniTheSkunk 2022-11-18 02:55:58 +00:00
parent 63c1def130
commit d77c3eefd4
4 changed files with 93 additions and 0 deletions

View File

@ -43,6 +43,7 @@ public abstract class BaseGame {
currentTime = GLFW.glfwGetTime(); currentTime = GLFW.glfwGetTime();
delta = currentTime - lastTime; delta = currentTime - lastTime;
lastTime = currentTime; lastTime = currentTime;
engine.tick();
window.tick(); window.tick();
//todo: frame rate control //todo: frame rate control
rootNode.tick(); rootNode.tick();

View File

@ -0,0 +1,32 @@
package com.danitheskunk.skunkworks;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
public class Data {
private LinkedHashMap<String, LinkedHashMap<String, String>> data;
private String currentCategory;
public Data(ByteBuffer data) {
this.data = new LinkedHashMap<>();
this.currentCategory = "";
this.data.put("", new LinkedHashMap<>());
StandardCharsets.UTF_8.decode(data).toString().lines().forEach(this::processLine);
}
private void processLine(String line) {
if(line.contains("=")) {
var parts = line.split("=", 2);
data.get(currentCategory).put(parts[0].strip(), parts[1].strip());
} else if(line.startsWith("[") && line.endsWith("]")) {
currentCategory = line.substring(1, line.length()-1);
if(!data.containsKey(currentCategory)) {
data.put(currentCategory, new LinkedHashMap<>());
}
}
}
}

View File

@ -8,11 +8,18 @@ import org.lwjgl.BufferUtils;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import static com.danitheskunk.skunkworks.gfx.GraphicsBackend.OPENGL; import static com.danitheskunk.skunkworks.gfx.GraphicsBackend.OPENGL;
public class Engine { public class Engine {
private final GraphicsBackend graphicsBackend; private final GraphicsBackend graphicsBackend;
private final WatchService watchService;
private final Map<WatchKey, Callable<Boolean>> watchCallbacks;
private final Map<WatchKey, Path> watchPaths;
//Constructors //Constructors
public Engine() { public Engine() {
@ -21,6 +28,13 @@ public class Engine {
public Engine(GraphicsBackend graphicsBackend) { public Engine(GraphicsBackend graphicsBackend) {
this.graphicsBackend = graphicsBackend; this.graphicsBackend = graphicsBackend;
try {
watchService = FileSystems.getDefault().newWatchService();
} catch(IOException e) {
throw new RuntimeException(e);
}
watchCallbacks = new HashMap<>();
watchPaths = new HashMap<>();
} }
//Methods //Methods
@ -42,9 +56,46 @@ public class Engine {
return new Image(loadBytes(path)); return new Image(loadBytes(path));
} }
public Data loadData(String path) {
return new Data(loadBytes(path));
}
//requires engine.tick() to be called regularly
public void watchFile(String path, Callable<Boolean> callback) {
var p = Paths.get(path);
var dir = p.getParent();
try {
var key = dir.register(watchService,
StandardWatchEventKinds.ENTRY_MODIFY
);
watchCallbacks.put(key, callback);
watchPaths.put(key, p.getFileName());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
public IWindow openWindow(Vec2i size, String title) { public IWindow openWindow(Vec2i size, String title) {
return switch(graphicsBackend) { return switch(graphicsBackend) {
case OPENGL -> new Window(size, title, this); case OPENGL -> new Window(size, title, this);
}; };
} }
public void tick() {
WatchKey key;
while((key = watchService.poll()) != null) {
for(var event : key.pollEvents()) {
var path = ((WatchEvent<Path>) event).context();
if(path.equals(watchPaths.get(key))) {
try {
if(watchCallbacks.get(key).call()) {
key.reset();
}
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
}
}
} }

View File

@ -17,6 +17,15 @@ public class Test extends BaseGame {
@Override @Override
protected void init() { protected void init() {
var path = "C:\\Program Files (x86)" +
"\\Steam\\steamapps\\common\\Unreal " +
"Tournament\\System\\UnrealTournament.ini";
var data = engine.loadData(path);
engine.watchFile(path, () -> {
System.out.println("changed");
return true;
});
var fontThin = window.loadFontTileset("fonts\\thin-6x12.png"); var fontThin = window.loadFontTileset("fonts\\thin-6x12.png");
var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png"); var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png");
term = new Terminal(new Vec2i(40, 22), fontThin2, fontThin); term = new Terminal(new Vec2i(40, 22), fontThin2, fontThin);