DynRes/src/main/java/io/gitlab/jfronny/dynres/mixin/ServerPropertiesHandlerMixi...

48 lines
2.0 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.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.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
@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 {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(Files.readAllBytes(DynRes.resFile));
resourcePackSha1 = Base64.getEncoder().encodeToString(hash);
DynRes.LOGGER.info("Set hash to " + resourcePackSha1);
} catch (IOException | NoSuchAlgorithmException e) {
DynRes.LOGGER.error("Failed to get hash, continuing with empty", e);
}
}
resourcePackHash = resourcePackSha1;
}
}
}