From 62fcdf63a94f3c84d4a83623286efdd9dc9f0124 Mon Sep 17 00:00:00 2001 From: Melledy <121644117+Melledy@users.noreply.github.com> Date: Sat, 30 Dec 2023 17:12:56 -0800 Subject: [PATCH] Auto reload gacha banners on change --- .../lunarcore/game/gacha/GachaService.java | 54 +++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main/java/emu/lunarcore/game/gacha/GachaService.java b/src/main/java/emu/lunarcore/game/gacha/GachaService.java index a61832c67..b4fce25b5 100644 --- a/src/main/java/emu/lunarcore/game/gacha/GachaService.java +++ b/src/main/java/emu/lunarcore/game/gacha/GachaService.java @@ -1,6 +1,7 @@ package emu.lunarcore.game.gacha; import java.io.FileReader; +import java.nio.file.*; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; @@ -33,7 +34,8 @@ @Getter public class GachaService extends BaseGameService { private final Int2ObjectMap gachaBanners; - private GetGachaInfoScRsp cachedProto; + private WatchService watchService; + private Thread watchThread; private int[] yellowAvatars = new int[] {1003, 1004, 1101, 1107, 1104, 1209, 1211}; private int[] yellowWeapons = new int[] {23000, 23002, 23003, 23004, 23005, 23012, 23013}; @@ -48,7 +50,12 @@ public class GachaService extends BaseGameService { public GachaService(GameServer server) { super(server); this.gachaBanners = new Int2ObjectOpenHashMap<>(); - this.load(); + + try { + this.watch(); + } catch (Exception e) { + LunarCore.getLogger().error("Watch service error: ", e); + } } public int randomRange(int min, int max) { @@ -58,9 +65,48 @@ public int randomRange(int min, int max) { public int getRandom(int[] array) { return array[randomRange(0, array.length - 1)]; } + + private String getBannerFileName() { + return LunarCore.getConfig().getDataDir() + "/Banners.json"; + } + + public void watch() throws Exception { + // Load banners first + this.loadBanners(); + + // Create watch service + this.watchService = FileSystems.getDefault().newWatchService(); + Path watchPath = Paths.get(LunarCore.getConfig().getDataDir()); + watchPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); + + // Start watch thread + this.watchThread = new Thread(() -> { + WatchKey key = null; + try { + while ((key = watchService.take()) != null) { + for (var event : key.pollEvents()) { + if (event.context() == null) { + continue; + } + + if (event.context() instanceof Path path && path.toString().equals("Banners.json")) { + loadBanners(); + } + } + + key.reset(); + } + } catch (Exception e) { + LunarCore.getLogger().error("Watch service thread error: ", e); + } + }); + this.watchThread.start(); + } - public synchronized void load() { - try (FileReader fileReader = new FileReader(LunarCore.getConfig().getDataDir() + "/Banners.json")) { + public synchronized void loadBanners() { + this.getGachaBanners().clear(); + + try (FileReader fileReader = new FileReader(getBannerFileName())) { List banners = JsonUtils.loadToList(fileReader, GachaBanner.class); for (GachaBanner banner : banners) { getGachaBanners().put(banner.getId(), banner);