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

49 lines
2.1 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;
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) {
2022-03-01 15:02:48 +01:00
if (DynRes.packFile != null) {
2021-11-30 20:53:13 +01:00
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 {
2021-12-31 11:28:19 +01:00
StringBuilder result = new StringBuilder();
2022-03-01 15:02:48 +01:00
for (byte b : MessageDigest.getInstance("SHA-1").digest(Files.readAllBytes(DynRes.packFile))) {
2021-12-31 11:28:19 +01:00
result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
resourcePackSha1 = result.toString();
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
}
}