added hot reloading to Data

This commit is contained in:
DaniTheSkunk 2022-11-18 06:24:31 +00:00
parent d77c3eefd4
commit fe6625aa12
3 changed files with 29 additions and 5 deletions

View File

@ -10,6 +10,7 @@ import java.util.LinkedHashMap;
public class Data { public class Data {
private LinkedHashMap<String, LinkedHashMap<String, String>> data; private LinkedHashMap<String, LinkedHashMap<String, String>> data;
private String currentCategory; private String currentCategory;
private Runnable callback;
public Data(ByteBuffer data) { public Data(ByteBuffer data) {
this.data = new LinkedHashMap<>(); this.data = new LinkedHashMap<>();
@ -18,6 +19,19 @@ public class Data {
StandardCharsets.UTF_8.decode(data).toString().lines().forEach(this::processLine); StandardCharsets.UTF_8.decode(data).toString().lines().forEach(this::processLine);
} }
public void reload(ByteBuffer data) {
this.data.clear();
this.data.put("", new LinkedHashMap<>());
StandardCharsets.UTF_8.decode(data).toString().lines().forEach(this::processLine);
if(callback != null) {
callback.run();
}
}
public void onReload(Runnable callback) {
this.callback = callback;
}
private void processLine(String line) { private void processLine(String line) {
if(line.contains("=")) { if(line.contains("=")) {
var parts = line.split("=", 2); var parts = line.split("=", 2);
@ -29,4 +43,8 @@ public class Data {
} }
} }
} }
public String get(String category, String key) {
return data.get(category).get(key);
}
} }

View File

@ -60,6 +60,15 @@ public class Engine {
return new Data(loadBytes(path)); return new Data(loadBytes(path));
} }
public Data loadDataWatched(String path) {
var data = loadData(path);
watchFile(path, () -> {
data.reload(loadBytes(path));
return true;
});
return data;
}
//requires engine.tick() to be called regularly //requires engine.tick() to be called regularly
public void watchFile(String path, Callable<Boolean> callback) { public void watchFile(String path, Callable<Boolean> callback) {
var p = Paths.get(path); var p = Paths.get(path);

View File

@ -20,11 +20,8 @@ public class Test extends BaseGame {
var path = "C:\\Program Files (x86)" + var path = "C:\\Program Files (x86)" +
"\\Steam\\steamapps\\common\\Unreal " + "\\Steam\\steamapps\\common\\Unreal " +
"Tournament\\System\\UnrealTournament.ini"; "Tournament\\System\\UnrealTournament.ini";
var data = engine.loadData(path); var data = engine.loadDataWatched(path);
engine.watchFile(path, () -> { data.onReload(() -> System.out.println(data.get("URL", "Host")));
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");