mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-08-27 12:41:43 +00:00
object cleanup
This commit is contained in:
+9
-85
@@ -18,121 +18,45 @@
|
||||
|
||||
package art.arcane.iris.modded.service;
|
||||
|
||||
import art.arcane.iris.core.IrisSettings;
|
||||
import art.arcane.iris.core.localization.IrisLanguage;
|
||||
import art.arcane.iris.core.SettingsHotloadWatch;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.volmlib.util.hotload.ConfigHotloadEngine;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public final class ModdedSettingsHotloadService implements ModdedTickableService {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger("Iris");
|
||||
private static final long POLL_PERIOD_MILLIS = 500L;
|
||||
private static final long HOTLOAD_COOLDOWN_MILLIS = 3_000L;
|
||||
private static final int MAX_SETTINGS_BYTES = 2 * 1024 * 1024;
|
||||
|
||||
private ConfigHotloadEngine hotloadEngine;
|
||||
private SettingsHotloadWatch hotloadWatch;
|
||||
private long lastPollAtNanos;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
File settingsFile = settingsFile();
|
||||
hotloadEngine = new ConfigHotloadEngine(
|
||||
this::isSettingsFile,
|
||||
() -> List.of(settingsFile),
|
||||
this::readSettings,
|
||||
this::normalizeSettings
|
||||
);
|
||||
hotloadEngine.configure(
|
||||
POLL_PERIOD_MILLIS,
|
||||
HOTLOAD_COOLDOWN_MILLIS,
|
||||
List.of(settingsFile),
|
||||
List.of()
|
||||
);
|
||||
hotloadWatch = new SettingsHotloadWatch(settingsFile());
|
||||
lastPollAtNanos = 0L;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
ConfigHotloadEngine active = hotloadEngine;
|
||||
hotloadEngine = null;
|
||||
SettingsHotloadWatch active = hotloadWatch;
|
||||
hotloadWatch = null;
|
||||
if (active != null) {
|
||||
active.clear();
|
||||
active.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServerTick(MinecraftServer server) {
|
||||
ConfigHotloadEngine active = hotloadEngine;
|
||||
SettingsHotloadWatch active = hotloadWatch;
|
||||
if (active == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
long now = System.nanoTime();
|
||||
if (now - lastPollAtNanos < TimeUnit.MILLISECONDS.toNanos(POLL_PERIOD_MILLIS)) {
|
||||
if (now - lastPollAtNanos < TimeUnit.MILLISECONDS.toNanos(SettingsHotloadWatch.POLL_PERIOD_MILLIS)) {
|
||||
return;
|
||||
}
|
||||
lastPollAtNanos = now;
|
||||
|
||||
try {
|
||||
for (ConfigHotloadEngine.StableContentSnapshot snapshot : active.pollTouchedSnapshots()) {
|
||||
if ("missing".equals(snapshot.signature())) {
|
||||
active.processSnapshotChange(snapshot, ignored -> true, null);
|
||||
LOGGER.warn("settings.json was removed; retaining the last valid runtime settings");
|
||||
continue;
|
||||
}
|
||||
active.processSnapshotChange(snapshot, this::reloadSettings, ignored -> LOGGER.info("Hotloaded settings.json"));
|
||||
}
|
||||
IrisLanguage.update();
|
||||
} catch (RuntimeException failure) {
|
||||
LOGGER.error("Iris settings hotload watcher failed", failure);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean reloadSettings(ConfigHotloadEngine.StableContentSnapshot snapshot) {
|
||||
try {
|
||||
String content = snapshot.normalizedContent();
|
||||
if (content == null) {
|
||||
return false;
|
||||
}
|
||||
return IrisSettings.applyHotloadSnapshot(content, IrisLanguage::reload);
|
||||
} catch (RuntimeException failure) {
|
||||
LOGGER.error("Iris settings hotload failed; keeping the previous runtime settings", failure);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSettingsFile(File file) {
|
||||
return file != null && settingsFile().getAbsoluteFile().equals(file.getAbsoluteFile());
|
||||
}
|
||||
|
||||
private String readSettings(File file) {
|
||||
if (file == null || !file.isFile()) {
|
||||
return null;
|
||||
}
|
||||
try (InputStream input = Files.newInputStream(file.toPath())) {
|
||||
byte[] content = input.readNBytes(MAX_SETTINGS_BYTES + 1);
|
||||
if (content.length > MAX_SETTINGS_BYTES) {
|
||||
throw new IOException("Settings exceed " + MAX_SETTINGS_BYTES + " bytes: " + file);
|
||||
}
|
||||
return new String(content, StandardCharsets.UTF_8);
|
||||
} catch (IOException failure) {
|
||||
throw new UncheckedIOException("Failed to read Iris settings from " + file, failure);
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeSettings(String content) {
|
||||
return content == null ? null : content.replace("\r\n", "\n").trim();
|
||||
active.checkConfigHotload();
|
||||
}
|
||||
|
||||
private static File settingsFile() {
|
||||
|
||||
Reference in New Issue
Block a user