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

48 lines
2.0 KiB
Java
Raw Normal View History

2020-09-07 13:29:44 +02:00
package io.gitlab.jfronny.dynres.mixin;
2021-06-02 13:31:04 +02:00
import io.gitlab.jfronny.dynres.Cfg;
2020-09-07 13:29:44 +02:00
import io.gitlab.jfronny.dynres.DynRes;
import io.gitlab.jfronny.libjf.web.api.WebServer;
import io.gitlab.jfronny.libjf.web.impl.util.WebPaths;
2020-09-07 13:29:44 +02:00
import net.minecraft.server.dedicated.ServerPropertiesHandler;
2021-06-02 13:31:04 +02:00
import org.spongepowered.asm.mixin.Final;
2020-09-07 13:29:44 +02:00
import org.spongepowered.asm.mixin.Mixin;
2021-06-02 13:31:04 +02:00
import org.spongepowered.asm.mixin.Mutable;
2020-09-07 13:29:44 +02:00
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;
2020-09-07 15:03:15 +02:00
import java.io.IOException;
2021-11-30 20:53:13 +01:00
import java.nio.file.Files;
2021-12-30 13:36:10 +01:00
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
2020-09-07 13:29:44 +02:00
@Mixin(ServerPropertiesHandler.class)
2021-11-30 20:53:13 +01:00
public class ServerPropertiesHandlerMixin {
2021-06-02 13:31:04 +02:00
@Mutable @Final @Shadow public String resourcePackHash;
@Mutable @Final @Shadow public String resourcePackSha1;
@Mutable @Final @Shadow public String resourcePack;
2020-09-07 13:29:44 +02:00
2021-06-08 20:47:48 +02:00
@Inject(at = @At("RETURN"), method = "<init>(Ljava/util/Properties;)V")
public void init(CallbackInfo info) {
2021-11-30 20:53:13 +01:00
if (DynRes.resFile != null) {
resourcePack = WebPaths.concat(WebServer.getInstance().getServerRoot(), "resources.zip");
DynRes.LOGGER.info("Pack link: " + resourcePack);
2021-11-30 20:53:13 +01:00
resourcePackSha1 = "";
if (Cfg.hashResources) {
2021-12-30 13:36:10 +01:00
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);
2021-12-30 13:36:10 +01:00
} catch (IOException | NoSuchAlgorithmException e) {
2021-11-30 20:53:13 +01:00
DynRes.LOGGER.error("Failed to get hash, continuing with empty", e);
}
2020-09-07 15:03:15 +02:00
}
2021-11-30 20:53:13 +01:00
resourcePackHash = resourcePackSha1;
2020-09-07 15:03:15 +02:00
}
2020-09-07 13:29:44 +02:00
}
}