DynRes/src/main/java/io/gitlab/jfronny/dynres/mixin/ServerPropertiesHandlerMixin.java
2021-11-30 20:53:13 +01:00

45 lines
1.8 KiB
Java

package io.gitlab.jfronny.dynres.mixin;
import io.gitlab.jfronny.dynres.Cfg;
import io.gitlab.jfronny.dynres.DynRes;
import io.gitlab.jfronny.libjf.web.api.WebServer;
import io.gitlab.jfronny.libjf.web.impl.util.WebPaths;
import net.minecraft.server.dedicated.ServerPropertiesHandler;
import org.apache.commons.codec.digest.DigestUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
@Mixin(ServerPropertiesHandler.class)
public class ServerPropertiesHandlerMixin {
@Mutable @Final @Shadow public String resourcePackHash;
@Mutable @Final @Shadow public String resourcePackSha1;
@Mutable @Final @Shadow public String resourcePack;
@Inject(at = @At("RETURN"), method = "<init>(Ljava/util/Properties;)V")
public void init(CallbackInfo info) {
if (DynRes.resFile != null) {
resourcePack = WebPaths.concat(WebServer.getInstance().getServerRoot(), "resources.zip");
DynRes.LOGGER.info("Pack link: " + resourcePack);
resourcePackSha1 = "";
if (Cfg.hashResources) {
try (InputStream fs = Files.newInputStream(DynRes.resFile)) {
resourcePackSha1 = DigestUtils.sha1Hex(fs);
DynRes.LOGGER.info("Set hash to " + resourcePackSha1);
} catch (IOException e) {
DynRes.LOGGER.error("Failed to get hash, continuing with empty", e);
}
}
resourcePackHash = resourcePackSha1;
}
}
}