Fix animated cape recovery and add reload command
This commit is contained in:
1 parent
5f02d9cbc5
commit
2c8bb8493e
11 files changed
+396
-78
No files matched your search
@@ -57,6 +57,10 @@ subprojects {
|
|||||||
options.encoding = "UTF-8"
|
options.encoding = "UTF-8"
|
||||||
options.release = javaVer
|
options.release = javaVer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.withType(Test).configureEach {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def runtimePlatforms = rootProject.enabled_platforms.split(",").collect { it.trim() }.findAll { !it.isEmpty() }
|
def runtimePlatforms = rootProject.enabled_platforms.split(",").collect { it.trim() }.findAll { !it.isEmpty() }
|
||||||
|
|||||||
@@ -7,4 +7,5 @@ dependencies {
|
|||||||
// which Architectury remaps to the right annotation on each platform.
|
// which Architectury remaps to the right annotation on each platform.
|
||||||
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
|
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
|
||||||
modApi "dev.architectury:architectury:${rootProject.architectury_api_version}"
|
modApi "dev.architectury:architectury:${rootProject.architectury_api_version}"
|
||||||
|
testImplementation "org.junit.jupiter:junit-jupiter:5.11.4"
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package xyz.nightsync.skinsg;
|
package xyz.nightsync.skinsg;
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.multiplayer.ClientPacketListener;
|
||||||
import xyz.nightsync.skinsg.client.CapeManager;
|
import xyz.nightsync.skinsg.client.CapeManager;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -13,6 +14,7 @@ import java.nio.file.Path;
|
|||||||
public final class SkinsgClient {
|
public final class SkinsgClient {
|
||||||
|
|
||||||
private static SkinsgConfig config;
|
private static SkinsgConfig config;
|
||||||
|
private static ClientPacketListener lastConnection;
|
||||||
|
|
||||||
private SkinsgClient() {}
|
private SkinsgClient() {}
|
||||||
|
|
||||||
@@ -30,6 +32,11 @@ public final class SkinsgClient {
|
|||||||
|
|
||||||
/** Call every client tick from the loader event. */
|
/** Call every client tick from the loader event. */
|
||||||
public static void tick() {
|
public static void tick() {
|
||||||
|
ClientPacketListener connection = Minecraft.getInstance().getConnection();
|
||||||
|
if (connection != lastConnection) {
|
||||||
|
CapeManager.releaseAll();
|
||||||
|
lastConnection = connection;
|
||||||
|
}
|
||||||
CapeManager.clientTick();
|
CapeManager.clientTick();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,6 +46,25 @@ public final class SkinsgConfig {
|
|||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized boolean reload() {
|
||||||
|
if (file == null || !Files.isRegularFile(file)) return false;
|
||||||
|
try {
|
||||||
|
SkinsgConfig loaded = GSON.fromJson(Files.readString(file, StandardCharsets.UTF_8), SkinsgConfig.class);
|
||||||
|
if (loaded == null) return false;
|
||||||
|
loaded.normalize();
|
||||||
|
baseUrl = loaded.baseUrl;
|
||||||
|
mode = loaded.mode;
|
||||||
|
enabled = new LinkedHashSet<>(loaded.enabled);
|
||||||
|
blocked = new LinkedHashSet<>(loaded.blocked);
|
||||||
|
globalSpeed = loaded.globalSpeed;
|
||||||
|
speed = new HashMap<>(loaded.speed);
|
||||||
|
save();
|
||||||
|
return true;
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void normalize() {
|
private void normalize() {
|
||||||
if (baseUrl == null || baseUrl.isBlank()) baseUrl = DEFAULT_BASE_URL;
|
if (baseUrl == null || baseUrl.isBlank()) baseUrl = DEFAULT_BASE_URL;
|
||||||
baseUrl = baseUrl.trim();
|
baseUrl = baseUrl.trim();
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ public final class CapeManager {
|
|||||||
private static final String MODID = "skinsg";
|
private static final String MODID = "skinsg";
|
||||||
private static final int MAX_CACHE = 96;
|
private static final int MAX_CACHE = 96;
|
||||||
private static final int MAX_BYTES = 12 * 1024 * 1024;
|
private static final int MAX_BYTES = 12 * 1024 * 1024;
|
||||||
|
private static final long RETRY_BASE_MS = 5_000L;
|
||||||
|
private static final long RETRY_MAX_MS = 5 * 60_000L;
|
||||||
|
private static final long STATIC_CAPE_RECHECK_MS = 15 * 60_000L;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger("skinsg");
|
private static final Logger LOGGER = LoggerFactory.getLogger("skinsg");
|
||||||
|
|
||||||
private static SkinsgConfig config;
|
private static SkinsgConfig config;
|
||||||
@@ -75,7 +78,9 @@ public final class CapeManager {
|
|||||||
releaseIfPresent(uuid);
|
releaseIfPresent(uuid);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return CACHE.computeIfAbsent(uuid, CapeManager::startLoad).texture;
|
Entry entry = CACHE.computeIfAbsent(uuid, Entry::new);
|
||||||
|
scheduleLoad(entry);
|
||||||
|
return entry.texture();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clientTick() {
|
public static void clientTick() {
|
||||||
@@ -103,37 +108,94 @@ public final class CapeManager {
|
|||||||
if (e != null) e.release();
|
if (e != null) e.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Entry startLoad(UUID uuid) {
|
private static void scheduleLoad(Entry entry) {
|
||||||
Entry entry = new Entry(uuid);
|
if (!entry.beginLoad(System.currentTimeMillis())) return;
|
||||||
IO.submit(() -> {
|
SkinsgConfig currentConfig = config;
|
||||||
try {
|
String baseUrl = currentConfig == null ? "" : currentConfig.baseUrl;
|
||||||
byte[] bytes = httpGet(config.baseUrl + "/api/minecraft/cape/" + uuid + "?gif");
|
if (baseUrl == null || baseUrl.isBlank()) {
|
||||||
if (bytes == null || bytes.length < 6 || !isGif(bytes)) {
|
entry.defer(STATIC_CAPE_RECHECK_MS);
|
||||||
entry.noAnimation = true;
|
return;
|
||||||
return;
|
}
|
||||||
}
|
try {
|
||||||
buildTexture(entry, GifDecoder.decode(bytes, LIMITS));
|
IO.submit(() -> load(entry, baseUrl));
|
||||||
} catch (Exception | LinkageError t) {
|
} catch (RuntimeException error) {
|
||||||
entry.noAnimation = true;
|
retry(entry, "schedule", error);
|
||||||
LOGGER.error("[SkinsG/Cape] Failed to load animated cape for uuid={}", uuid, t);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void load(Entry entry, String baseUrl) {
|
||||||
|
try {
|
||||||
|
byte[] bytes = httpGet(baseUrl + "/api/minecraft/cape/" + entry.uuid + "?gif");
|
||||||
|
if (bytes.length < 6 || !isGif(bytes)) {
|
||||||
|
entry.defer(STATIC_CAPE_RECHECK_MS);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
buildTexture(entry, GifDecoder.decode(bytes, LIMITS));
|
||||||
return entry;
|
} catch (Exception | LinkageError error) {
|
||||||
|
retry(entry, "load", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void retry(Entry entry, String stage, Throwable error) {
|
||||||
|
long delay = entry.retry(System.currentTimeMillis());
|
||||||
|
if (delay >= 0) {
|
||||||
|
LOGGER.warn("[NightSkins/Cape] Animated cape {} failed for uuid={}; retrying in {} ms",
|
||||||
|
stage, entry.uuid, delay, error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void buildTexture(Entry entry, GifImage gif) {
|
private static void buildTexture(Entry entry, GifImage gif) {
|
||||||
RenderSystem.recordRenderCall(() -> {
|
try {
|
||||||
try {
|
RenderSystem.recordRenderCall(() -> {
|
||||||
DynamicTexture tex = new DynamicTexture(gif.width, gif.height, true);
|
if (entry.isReleased()) return;
|
||||||
((AbstractTexture) tex).setFilter(false, false);
|
DynamicTexture tex = null;
|
||||||
ResourceLocation id = new ResourceLocation(MODID, "cape/" + entry.uuid + "/anim");
|
ResourceLocation id = new ResourceLocation(MODID, "cape/" + entry.uuid + "/anim");
|
||||||
Minecraft.getInstance().getTextureManager().register(id, tex);
|
boolean registered = false;
|
||||||
entry.attach(gif, tex, id);
|
try {
|
||||||
} catch (Exception | LinkageError t) {
|
tex = new DynamicTexture(gif.width, gif.height, true);
|
||||||
entry.noAnimation = true;
|
((AbstractTexture) tex).setFilter(false, false);
|
||||||
LOGGER.error("[SkinsG/Cape] Failed to create animated cape texture for uuid={}", entry.uuid, t);
|
Minecraft.getInstance().getTextureManager().register(id, tex);
|
||||||
|
registered = true;
|
||||||
|
if (!entry.attach(gif, tex, id)) {
|
||||||
|
releaseTexture(id, tex);
|
||||||
|
}
|
||||||
|
} catch (Exception | LinkageError error) {
|
||||||
|
releaseTexture(registered ? id : null, tex);
|
||||||
|
retry(entry, "texture creation", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception | LinkageError error) {
|
||||||
|
retry(entry, "render scheduling", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void releaseTexture(ResourceLocation id, DynamicTexture tex) {
|
||||||
|
Runnable release = () -> {
|
||||||
|
if (id != null) {
|
||||||
|
try {
|
||||||
|
Minecraft.getInstance().getTextureManager().release(id);
|
||||||
|
return;
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
if (tex != null) {
|
||||||
|
try {
|
||||||
|
tex.close();
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
if (RenderSystem.isOnRenderThread()) release.run();
|
||||||
|
else RenderSystem.recordRenderCall(release::run);
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
if (tex != null) {
|
||||||
|
try {
|
||||||
|
tex.close();
|
||||||
|
} catch (Throwable ignoredClose) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isGif(byte[] b) {
|
private static boolean isGif(byte[] b) {
|
||||||
@@ -141,82 +203,171 @@ public final class CapeManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] httpGet(String url) throws IOException {
|
private static byte[] httpGet(String url) throws IOException {
|
||||||
HttpURLConnection c = (HttpURLConnection) URI.create(url).toURL().openConnection();
|
HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
|
||||||
c.setConnectTimeout(7000);
|
connection.setConnectTimeout(7000);
|
||||||
c.setReadTimeout(10000);
|
connection.setReadTimeout(10000);
|
||||||
c.setRequestProperty("User-Agent", MODID + "/1.0");
|
connection.setRequestProperty("User-Agent", MODID + "/1.0");
|
||||||
int status = c.getResponseCode();
|
try {
|
||||||
if (status != 200) throw new IOException("Cape endpoint returned HTTP " + status);
|
int status = connection.getResponseCode();
|
||||||
int len = c.getContentLength();
|
if (status != 200) throw new IOException("Cape endpoint returned HTTP " + status);
|
||||||
if (len > MAX_BYTES) return null;
|
int length = connection.getContentLength();
|
||||||
try (InputStream in = c.getInputStream()) {
|
if (length > MAX_BYTES) throw new IOException("Cape response exceeds " + MAX_BYTES + " bytes");
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(1024, Math.min(Math.max(len, 0), MAX_BYTES)));
|
try (InputStream in = connection.getInputStream()) {
|
||||||
byte[] buf = new byte[8192];
|
ByteArrayOutputStream out = new ByteArrayOutputStream(
|
||||||
int r, total = 0;
|
Math.max(1024, Math.min(Math.max(length, 0), MAX_BYTES)));
|
||||||
while ((r = in.read(buf)) != -1) {
|
byte[] buffer = new byte[8192];
|
||||||
total += r;
|
int read;
|
||||||
if (total > MAX_BYTES) return null;
|
int total = 0;
|
||||||
out.write(buf, 0, r);
|
while ((read = in.read(buffer)) != -1) {
|
||||||
|
total += read;
|
||||||
|
if (total > MAX_BYTES) throw new IOException("Cape response exceeds " + MAX_BYTES + " bytes");
|
||||||
|
out.write(buffer, 0, read);
|
||||||
|
}
|
||||||
|
return out.toByteArray();
|
||||||
}
|
}
|
||||||
return out.toByteArray();
|
} finally {
|
||||||
|
connection.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class Entry {
|
private static final class Entry {
|
||||||
final UUID uuid;
|
final UUID uuid;
|
||||||
volatile boolean noAnimation;
|
|
||||||
|
|
||||||
private GifImage gif;
|
private GifImage gif;
|
||||||
private DynamicTexture tex;
|
private DynamicTexture tex;
|
||||||
private volatile ResourceLocation texture;
|
private ResourceLocation texture;
|
||||||
private int frame = -1;
|
private int frame = -1;
|
||||||
private long nextAt;
|
private long nextAt;
|
||||||
|
private long nextLoadAt;
|
||||||
|
private int failures;
|
||||||
|
private boolean loading;
|
||||||
|
private boolean released;
|
||||||
|
|
||||||
Entry(UUID uuid) { this.uuid = uuid; }
|
Entry(UUID uuid) { this.uuid = uuid; }
|
||||||
|
|
||||||
void attach(GifImage gif, DynamicTexture tex, ResourceLocation id) {
|
synchronized ResourceLocation texture() {
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized boolean beginLoad(long now) {
|
||||||
|
if (released || loading || texture != null || now < nextLoadAt) return false;
|
||||||
|
loading = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized void defer(long delay) {
|
||||||
|
if (released) return;
|
||||||
|
loading = false;
|
||||||
|
failures = 0;
|
||||||
|
nextLoadAt = System.currentTimeMillis() + delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized long retry(long now) {
|
||||||
|
if (released) return -1L;
|
||||||
|
loading = false;
|
||||||
|
failures++;
|
||||||
|
long delay = RetryBackoff.delayMillis(failures, RETRY_BASE_MS, RETRY_MAX_MS);
|
||||||
|
nextLoadAt = now + delay;
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized boolean attach(GifImage gif, DynamicTexture tex, ResourceLocation id) {
|
||||||
|
if (released) return false;
|
||||||
this.gif = gif;
|
this.gif = gif;
|
||||||
this.tex = tex;
|
this.tex = tex;
|
||||||
this.texture = id;
|
this.texture = id;
|
||||||
this.frame = -1;
|
this.frame = -1;
|
||||||
this.nextAt = 0L;
|
this.nextAt = 0L;
|
||||||
|
this.nextLoadAt = 0L;
|
||||||
|
this.failures = 0;
|
||||||
|
this.loading = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized boolean isReleased() {
|
||||||
|
return released;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tick(long now, int speedPercent) {
|
void tick(long now, int speedPercent) {
|
||||||
if (gif == null || tex == null) return;
|
GifImage currentGif;
|
||||||
if (frame != -1 && now < nextAt) return;
|
DynamicTexture currentTex;
|
||||||
frame = (frame + 1) % gif.frameCount();
|
int currentFrame;
|
||||||
|
synchronized (this) {
|
||||||
int[] src = gif.framesAbgr[frame];
|
if (released || gif == null || tex == null) return;
|
||||||
NativeImage px = tex.getPixels();
|
if (frame != -1 && now < nextAt) return;
|
||||||
if (px != null && px.getWidth() == gif.width && px.getHeight() == gif.height) {
|
currentGif = gif;
|
||||||
int i = 0;
|
currentTex = tex;
|
||||||
for (int y = 0; y < gif.height; y++) {
|
frame = (frame + 1) % currentGif.frameCount();
|
||||||
for (int x = 0; x < gif.width; x++) {
|
currentFrame = frame;
|
||||||
px.setPixelRGBA(x, y, src[i++]);
|
int base = currentGif.delaysMs[currentFrame];
|
||||||
}
|
long duration = speedPercent > 0 ? (long) base * 100L / speedPercent : base;
|
||||||
}
|
nextAt = now + Math.max(20L, duration);
|
||||||
RenderSystem.recordRenderCall(tex::upload);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int base = gif.delaysMs[frame];
|
try {
|
||||||
long dur = speedPercent > 0 ? (long) base * 100L / speedPercent : base;
|
int[] src = currentGif.framesAbgr[currentFrame];
|
||||||
nextAt = now + Math.max(20L, dur);
|
NativeImage pixels = currentTex.getPixels();
|
||||||
|
if (pixels == null || pixels.getWidth() != currentGif.width || pixels.getHeight() != currentGif.height) {
|
||||||
|
throw new IllegalStateException("Animated cape texture is no longer allocated");
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
for (int y = 0; y < currentGif.height; y++) {
|
||||||
|
for (int x = 0; x < currentGif.width; x++) {
|
||||||
|
pixels.setPixelRGBA(x, y, src[i++]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (RenderSystem.isOnRenderThread()) {
|
||||||
|
currentTex.upload();
|
||||||
|
} else {
|
||||||
|
RenderSystem.recordRenderCall(() -> upload(currentTex));
|
||||||
|
}
|
||||||
|
} catch (Exception | LinkageError error) {
|
||||||
|
playbackFailed(currentTex, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void upload(DynamicTexture expectedTexture) {
|
||||||
|
try {
|
||||||
|
expectedTexture.upload();
|
||||||
|
} catch (Exception | LinkageError error) {
|
||||||
|
playbackFailed(expectedTexture, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void playbackFailed(DynamicTexture expectedTexture, Throwable error) {
|
||||||
|
ResourceLocation staleId;
|
||||||
|
synchronized (this) {
|
||||||
|
if (released || tex != expectedTexture) return;
|
||||||
|
staleId = texture;
|
||||||
|
texture = null;
|
||||||
|
tex = null;
|
||||||
|
gif = null;
|
||||||
|
frame = -1;
|
||||||
|
loading = false;
|
||||||
|
failures++;
|
||||||
|
long delay = RetryBackoff.delayMillis(failures, RETRY_BASE_MS, RETRY_MAX_MS);
|
||||||
|
nextLoadAt = System.currentTimeMillis() + delay;
|
||||||
|
}
|
||||||
|
releaseTexture(staleId, expectedTexture);
|
||||||
|
LOGGER.warn("[NightSkins/Cape] Animated cape playback failed for uuid={}; texture will be rebuilt",
|
||||||
|
uuid, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void release() {
|
void release() {
|
||||||
ResourceLocation id = texture;
|
ResourceLocation staleId;
|
||||||
if (id != null) {
|
DynamicTexture staleTexture;
|
||||||
RenderSystem.recordRenderCall(() -> {
|
synchronized (this) {
|
||||||
try {
|
if (released) return;
|
||||||
Minecraft.getInstance().getTextureManager().release(id);
|
released = true;
|
||||||
} catch (Throwable ignored) {
|
loading = false;
|
||||||
}
|
staleId = texture;
|
||||||
});
|
staleTexture = tex;
|
||||||
|
texture = null;
|
||||||
|
tex = null;
|
||||||
|
gif = null;
|
||||||
|
frame = -1;
|
||||||
}
|
}
|
||||||
texture = null;
|
releaseTexture(staleId, staleTexture);
|
||||||
tex = null;
|
|
||||||
gif = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package xyz.nightsync.skinsg.client;
|
||||||
|
|
||||||
|
final class RetryBackoff {
|
||||||
|
|
||||||
|
private RetryBackoff() {}
|
||||||
|
|
||||||
|
static long delayMillis(int failureCount, long baseDelay, long maxDelay) {
|
||||||
|
if (baseDelay <= 0 || maxDelay < baseDelay) {
|
||||||
|
throw new IllegalArgumentException("Invalid retry delay bounds");
|
||||||
|
}
|
||||||
|
int exponent = Math.max(0, Math.min(30, failureCount - 1));
|
||||||
|
if (baseDelay > (maxDelay >> exponent)) return maxDelay;
|
||||||
|
return Math.min(maxDelay, baseDelay << exponent);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,7 +45,9 @@ public final class SkinsgCommands {
|
|||||||
return b.buildFuture();
|
return b.buildFuture();
|
||||||
};
|
};
|
||||||
|
|
||||||
return SkinsgCommands.<S>literal("skinsg")
|
return SkinsgCommands.<S>literal("nightskins")
|
||||||
|
.then(SkinsgCommands.<S>literal("reload")
|
||||||
|
.executes(c -> reload(fb, cfg, c)))
|
||||||
.then(SkinsgCommands.<S>literal("playgif")
|
.then(SkinsgCommands.<S>literal("playgif")
|
||||||
.executes(c -> { cfg.setNone(); CapeManager.releaseAll(); return ok(fb, c, "GIF capes: off"); })
|
.executes(c -> { cfg.setNone(); CapeManager.releaseAll(); return ok(fb, c, "GIF capes: off"); })
|
||||||
.then(SkinsgCommands.<S, String>argument("target", StringArgumentType.word()).suggests(targets)
|
.then(SkinsgCommands.<S, String>argument("target", StringArgumentType.word()).suggests(targets)
|
||||||
@@ -79,6 +81,12 @@ public final class SkinsgCommands {
|
|||||||
return ok(fb, c, "GIF capes: on for " + target);
|
return ok(fb, c, "GIF capes: on for " + target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static <S> int reload(Feedback<S> fb, SkinsgConfig cfg, CommandContext<S> c) {
|
||||||
|
if (!cfg.reload()) return err(fb, c, "could not reload config/skinsg.json");
|
||||||
|
CapeManager.releaseAll();
|
||||||
|
return ok(fb, c, "configuration and animated cape cache reloaded");
|
||||||
|
}
|
||||||
|
|
||||||
private static <S> int stopgif(Feedback<S> fb, SkinsgConfig cfg, CommandContext<S> c) {
|
private static <S> int stopgif(Feedback<S> fb, SkinsgConfig cfg, CommandContext<S> c) {
|
||||||
String target = StringArgumentType.getString(c, "target");
|
String target = StringArgumentType.getString(c, "target");
|
||||||
if (ALL.equals(target)) { cfg.setNone(); CapeManager.releaseAll(); return ok(fb, c, "GIF capes: off"); }
|
if (ALL.equals(target)) { cfg.setNone(); CapeManager.releaseAll(); return ok(fb, c, "GIF capes: off"); }
|
||||||
@@ -111,12 +119,12 @@ public final class SkinsgCommands {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static <S> int ok(Feedback<S> fb, CommandContext<S> c, String text) {
|
private static <S> int ok(Feedback<S> fb, CommandContext<S> c, String text) {
|
||||||
fb.success(c.getSource(), "[skinsg] " + text);
|
fb.success(c.getSource(), "[NightSkins] " + text);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <S> int err(Feedback<S> fb, CommandContext<S> c, String text) {
|
private static <S> int err(Feedback<S> fb, CommandContext<S> c, String text) {
|
||||||
fb.failure(c.getSource(), "[skinsg] " + text);
|
fb.failure(c.getSource(), "[NightSkins] " + text);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package xyz.nightsync.skinsg;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class SkinsgConfigTest {
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path configDir;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void reloadNormalizesBlankBaseUrl() throws Exception {
|
||||||
|
SkinsgConfig config = SkinsgConfig.load(configDir);
|
||||||
|
Files.writeString(configDir.resolve("skinsg.json"), """
|
||||||
|
{
|
||||||
|
"baseUrl": "",
|
||||||
|
"mode": "ALL",
|
||||||
|
"globalSpeed": 100
|
||||||
|
}
|
||||||
|
""", StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
assertTrue(config.reload());
|
||||||
|
assertEquals(SkinsgConfig.DEFAULT_BASE_URL, config.baseUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void malformedReloadKeepsLastKnownGoodConfiguration() throws Exception {
|
||||||
|
SkinsgConfig config = SkinsgConfig.load(configDir);
|
||||||
|
config.baseUrl = "https://example.invalid";
|
||||||
|
Files.writeString(configDir.resolve("skinsg.json"), "{broken", StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
assertFalse(config.reload());
|
||||||
|
assertEquals("https://example.invalid", config.baseUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package xyz.nightsync.skinsg.client;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
class RetryBackoffTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void growsExponentiallyAndCapsAtMaximum() {
|
||||||
|
assertEquals(5_000L, RetryBackoff.delayMillis(1, 5_000L, 300_000L));
|
||||||
|
assertEquals(10_000L, RetryBackoff.delayMillis(2, 5_000L, 300_000L));
|
||||||
|
assertEquals(160_000L, RetryBackoff.delayMillis(6, 5_000L, 300_000L));
|
||||||
|
assertEquals(300_000L, RetryBackoff.delayMillis(7, 5_000L, 300_000L));
|
||||||
|
assertEquals(300_000L, RetryBackoff.delayMillis(Integer.MAX_VALUE, 5_000L, 300_000L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void treatsNonPositiveFailureCountAsFirstFailure() {
|
||||||
|
assertEquals(5_000L, RetryBackoff.delayMillis(0, 5_000L, 300_000L));
|
||||||
|
assertEquals(5_000L, RetryBackoff.delayMillis(-10, 5_000L, 300_000L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsInvalidBounds() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> RetryBackoff.delayMillis(1, 0L, 300_000L));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> RetryBackoff.delayMillis(1, 5_000L, 4_999L));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package xyz.nightsync.skinsg.commands;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
import xyz.nightsync.skinsg.SkinsgConfig;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
class SkinsgCommandsTest {
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path configDir;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void exposesNightSkinsRootAndReloadCommand() {
|
||||||
|
SkinsgConfig config = SkinsgConfig.load(configDir);
|
||||||
|
SkinsgCommands.Feedback<Object> feedback = new SkinsgCommands.Feedback<>() {
|
||||||
|
@Override
|
||||||
|
public void success(Object source, String message) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failure(Object source, String message) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
LiteralCommandNode<Object> root = SkinsgCommands.build(config, feedback).build();
|
||||||
|
|
||||||
|
assertEquals("nightskins", root.getLiteral());
|
||||||
|
assertNotNull(root.getChild("reload"));
|
||||||
|
assertNotNull(root.getChild("playgif"));
|
||||||
|
assertNotNull(root.getChild("stopgif"));
|
||||||
|
assertNotNull(root.getChild("self"));
|
||||||
|
assertNotNull(root.getChild("speed"));
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -6,7 +6,7 @@ org.gradle.parallel=true
|
|||||||
# --- Mod ---
|
# --- Mod ---
|
||||||
mod_id=skinsg
|
mod_id=skinsg
|
||||||
mod_name=HD Skin Support (Gravit)
|
mod_name=HD Skin Support (Gravit)
|
||||||
mod_version=0.1.3
|
mod_version=0.1.4
|
||||||
mod_group_id=xyz.nightsync.skinsg
|
mod_group_id=xyz.nightsync.skinsg
|
||||||
mod_license=All Rights Reserved
|
mod_license=All Rights Reserved
|
||||||
mod_author=Nghtly
|
mod_author=Nghtly
|
||||||
|
|||||||
Reference in new issue
Block a user