Skip to content

Commit

Permalink
Live Reload Now Watches Subdirectories
Browse files Browse the repository at this point in the history
* Live Reload now watches recursively

* Fixing issue with Prefab force refresh
  • Loading branch information
joshuaskelly authored Oct 31, 2020
1 parent a05489d commit 0e8eb49
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 66 deletions.
27 changes: 17 additions & 10 deletions DelvEdit/src/com/interrupt/dungeoneer/editor/EditorApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,31 +395,38 @@ public void init(){
StringManager.init();
Game.init();

// load the entity templates
loadEntities();
loadMonsters();

Gdx.input.setCursorCatched(false);
initTextures();

pickedWallTextureAtlas = pickedWallBottomTextureAtlas = pickedFloorTextureAtlas = pickedCeilingTextureAtlas =
TextureAtlas.cachedRepeatingAtlases.firstKey();

createEmptyLevel(17, 17);
}

/** Load entity templates */
public void loadEntities() {
try {
entityManager = Game.getModManager().loadEntityManager(Game.gameData.entityDataFiles);
EntityManager.setSingleton(entityManager);
} catch (Exception ex) {
// whoops
Gdx.app.log("Editor", "Error loading entities.dat: " + ex.getMessage());
}
}

// load the monster templates
/** Load monster templates. */
public void loadMonsters() {
try {
monsterManager = Game.getModManager().loadMonsterManager(Game.gameData.monsterDataFiles);
MonsterManager.setSingleton(monsterManager);
} catch (Exception ex) {
// whoops
Gdx.app.log("Editor", "Error loading monsters.dat: " + ex.getMessage());
}

Gdx.input.setCursorCatched(false);
initTextures();

pickedWallTextureAtlas = pickedWallBottomTextureAtlas = pickedFloorTextureAtlas = pickedCeilingTextureAtlas =
TextureAtlas.cachedRepeatingAtlases.firstKey();

createEmptyLevel(17, 17);
}

public void createEmptyLevel(int width, int height) {
Expand Down
121 changes: 65 additions & 56 deletions DelvEdit/src/com/interrupt/dungeoneer/editor/utils/LiveReload.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.interrupt.dungeoneer.editor.Editor;
import com.interrupt.dungeoneer.editor.EditorArt;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -25,74 +27,81 @@ public class LiveReload {
private final List<String> watchedExtensions = Arrays.asList("dat", "obj", "png", "frag", "vert");

public LiveReload() {
if (LiveReload.created) {
throw new RuntimeException("FileWatcher instance already exists.");
}

watcher = new Thread() {
public void run() {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
java.nio.file.Path path = Paths.get(Gdx.files.getLocalStoragePath());
path.register(watchService, ENTRY_CREATE, ENTRY_MODIFY);

boolean running = true;
while(running) {
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
@SuppressWarnings("unchecked")
WatchEvent<Path> watchEvent = (WatchEvent<Path>)event;
String filename = watchEvent.context().toString();
String extension = getFileExtension(filename).toLowerCase();

long now = System.currentTimeMillis();

if (watchedExtensions.contains(extension) && now - lastTimeCalled > MIN_TIME_BETWEEN_CALLS) {
lastTimeCalled = now;

// Reload must happen on main render thread.
needToReloadAssets.set(true);
break;
}
}

running = key.reset();
}
}
catch (Exception ignore) {}
}
};
if (LiveReload.created) {
throw new RuntimeException("FileWatcher instance already exists.");
}

watcher = new Thread(() -> {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path root = Paths.get(Gdx.files.getLocalStoragePath());
root.register(watchService, ENTRY_CREATE, ENTRY_MODIFY);

Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
dir.register(watchService, ENTRY_CREATE, ENTRY_MODIFY);
return FileVisitResult.CONTINUE;
}
});

boolean running = true;
while (running) {
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
@SuppressWarnings("unchecked")
WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
String filename = watchEvent.context().toString();
String extension = getFileExtension(filename).toLowerCase();

long now = System.currentTimeMillis();

if (watchedExtensions.contains(extension) && now - lastTimeCalled > MIN_TIME_BETWEEN_CALLS) {
lastTimeCalled = now;

// Reload must happen on main render thread.
needToReloadAssets.set(true);
break;
}
}

running = key.reset();
}
} catch (Exception ignore) {}
});
}

public void init() {
Gdx.app.log("LiveReload", "Initializing");
if (watcher.isAlive()) {
return;
}
Gdx.app.log("LiveReload", "Initializing");
if (watcher.isAlive()) {
return;
}

watcher.setPriority(Thread.MIN_PRIORITY);
watcher.start();
}
watcher.setPriority(Thread.MIN_PRIORITY);
watcher.start();
}

public void dispose() {
Gdx.app.log("LiveReload", "Disposing");
watcher.interrupt();
}
Gdx.app.log("LiveReload", "Disposing");
watcher.interrupt();
}

public void tick() {
public void tick() {
if (needToReloadAssets.compareAndSet(true, false)) {
Gdx.app.log("LiveReload", "Reloading assets");
Gdx.app.log("LiveReload", "Reloading assets");
EditorArt.refresh();
Editor.app.initTextures();
Editor.app.loadEntities();
Editor.app.loadMonsters();
}
}

private String getFileExtension(String filename) {
int index = filename.lastIndexOf(".");
if (index != -1 && index != 0) {
return filename.substring(index + 1);
}
private String getFileExtension(String filename) {
int index = filename.lastIndexOf(".");
if (index != -1 && index != 0) {
return filename.substring(index + 1);
}

return "";
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void init(Level level, Source source) {
// Force a refresh
lastRot.set(0, 0, 0);
loadedCategory = "NONE";
loadedName = "NONE";

updateDrawable();
spawnPrefab(level, source);
Expand Down

0 comments on commit 0e8eb49

Please sign in to comment.