Simplify pack validation and merge response mixins
This commit is contained in:
@@ -1,14 +1,5 @@
|
||||
package online.astradev.packs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.chat.Component;
|
||||
@@ -18,6 +9,15 @@ import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public final class PackRuntime implements AutoCloseable {
|
||||
public static volatile PackRuntime INSTANCE;
|
||||
|
||||
@@ -27,10 +27,12 @@ public final class PackRuntime implements AutoCloseable {
|
||||
private final Path root;
|
||||
private final Path configPath;
|
||||
private final PackHost host;
|
||||
private final Map<ServerCommonPacketListenerImpl, PackSession> sessions = new ConcurrentHashMap<>();
|
||||
private final Map<ServerCommonPacketListenerImpl, PackSession> sessions =
|
||||
new ConcurrentHashMap<>();
|
||||
private final Map<UUID, String> pendingWelcome = new ConcurrentHashMap<>();
|
||||
private final ExecutorService worker = Executors.newSingleThreadExecutor(
|
||||
Thread.ofPlatform().daemon().name("AstraPacks-build").factory());
|
||||
private final ExecutorService worker =
|
||||
Executors.newSingleThreadExecutor(
|
||||
Thread.ofPlatform().daemon().name("AstraPacks-build").factory());
|
||||
|
||||
private volatile PackConfig config;
|
||||
private volatile PackBuilder.Pack current;
|
||||
@@ -51,7 +53,9 @@ public final class PackRuntime implements AutoCloseable {
|
||||
return;
|
||||
}
|
||||
if (server.getServerResourcePack().isPresent()) {
|
||||
throw new IOException("AstraPacks must be the only pack sender: clear resource-pack in server.properties");
|
||||
throw new IOException(
|
||||
"AstraPacks must be the only pack sender: clear resource-pack in"
|
||||
+ " server.properties");
|
||||
}
|
||||
|
||||
Files.createDirectories(config.contents(root));
|
||||
@@ -62,10 +66,16 @@ public final class PackRuntime implements AutoCloseable {
|
||||
}
|
||||
host.start();
|
||||
if (config.hosting.publicAddress.isBlank()) {
|
||||
AstraPacks.LOG.error("Set hosting.publicAddress to the public HTTP address (port {}). Pack delivery is unavailable until configured.", config.hosting.port);
|
||||
AstraPacks.LOG.error(
|
||||
"Set hosting.publicAddress to the public HTTP address (port {}). Pack delivery"
|
||||
+ " is unavailable until configured.",
|
||||
config.hosting.port);
|
||||
}
|
||||
AstraPacks.LOG.info("AstraPacks: HTTP port={}, required={}, pack={}",
|
||||
config.hosting.port, config.delivery.required, current == null ? "UNAVAILABLE" : current.path());
|
||||
AstraPacks.LOG.info(
|
||||
"AstraPacks: HTTP port={}, required={}, pack={}",
|
||||
config.hosting.port,
|
||||
config.delivery.required,
|
||||
current == null ? "UNAVAILABLE" : current.path());
|
||||
}
|
||||
|
||||
private PackBuilder.Pack loadStartupPack() {
|
||||
@@ -73,7 +83,8 @@ public final class PackRuntime implements AutoCloseable {
|
||||
try {
|
||||
return new PackBuilder(config).build(root, RESOURCE_VERSION);
|
||||
} catch (IOException e) {
|
||||
AstraPacks.LOG.error("Resource-pack build failed; trying the last successful build", e);
|
||||
AstraPacks.LOG.error(
|
||||
"Resource-pack build failed; trying the last successful build", e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
@@ -101,17 +112,19 @@ public final class PackRuntime implements AutoCloseable {
|
||||
return session;
|
||||
}
|
||||
|
||||
public boolean intercept(ServerCommonPacketListenerImpl listener, ServerboundResourcePackPacket packet) {
|
||||
public boolean intercept(
|
||||
ServerCommonPacketListenerImpl listener, ServerboundResourcePackPacket packet) {
|
||||
if (!config.enabled) {
|
||||
return false;
|
||||
}
|
||||
// Consume stale replies too: vanilla treats any terminal UUID as completion of its task.
|
||||
server.execute(() -> {
|
||||
PackSession session = sessions.get(listener);
|
||||
if (session != null) {
|
||||
session.response(packet);
|
||||
}
|
||||
});
|
||||
server.execute(
|
||||
() -> {
|
||||
PackSession session = sessions.get(listener);
|
||||
if (session != null) {
|
||||
session.response(packet);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -150,7 +163,8 @@ public final class PackRuntime implements AutoCloseable {
|
||||
public String url(PackBuilder.Pack pack) {
|
||||
String address = config.hosting.publicAddress;
|
||||
if (address.isBlank()) {
|
||||
throw new IllegalStateException("Set hosting.publicAddress before sending resource packs");
|
||||
throw new IllegalStateException(
|
||||
"Set hosting.publicAddress before sending resource packs");
|
||||
}
|
||||
return address.replaceAll("/+$", "") + host.issuePath(pack);
|
||||
}
|
||||
@@ -174,27 +188,36 @@ public final class PackRuntime implements AutoCloseable {
|
||||
}
|
||||
|
||||
building = true;
|
||||
source.sendSuccess(() -> Component.literal("AstraPacks: проверка и подготовка ресурспака…"), false);
|
||||
source.sendSuccess(
|
||||
() -> Component.literal("AstraPacks: проверка и подготовка ресурспака…"), false);
|
||||
worker.execute(() -> preparePack(source, reload));
|
||||
}
|
||||
|
||||
private void preparePack(CommandSourceStack source, boolean reload) {
|
||||
try {
|
||||
PackConfig next = reload ? PackConfig.load(configPath) : config;
|
||||
if (next.enabled != config.enabled || !next.hosting.sameSettings(config.hosting)
|
||||
if (next.enabled != config.enabled
|
||||
|| !next.hosting.sameSettings(config.hosting)
|
||||
|| !next.build.storageDirectory.equals(config.build.storageDirectory)) {
|
||||
throw new IOException("Изменение enabled, каталога хранения или настроек HTTP-сервера требует перезапуска.");
|
||||
throw new IOException(
|
||||
"Изменение enabled, каталога хранения или настроек HTTP-сервера требует"
|
||||
+ " перезапуска.");
|
||||
}
|
||||
PackBuilder.Pack pack = new PackBuilder(next).build(root, RESOURCE_VERSION);
|
||||
server.execute(() -> publishPack(source, next, pack));
|
||||
} catch (Exception e) {
|
||||
AstraPacks.LOG.error("Pack build/publication failed; active pack retained", e);
|
||||
server.execute(() -> {
|
||||
building = false;
|
||||
if (!closed) {
|
||||
source.sendFailure(Component.literal("AstraPacks: " + e.getMessage() + " Рабочий пак сохранён."));
|
||||
}
|
||||
});
|
||||
server.execute(
|
||||
() -> {
|
||||
building = false;
|
||||
if (!closed) {
|
||||
source.sendFailure(
|
||||
Component.literal(
|
||||
"AstraPacks: "
|
||||
+ e.getMessage()
|
||||
+ " Рабочий пак сохранён."));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,8 +230,13 @@ public final class PackRuntime implements AutoCloseable {
|
||||
host.publish(pack);
|
||||
current = pack;
|
||||
|
||||
String message = "AstraPacks: готово, " + pack.files() + " файлов, "
|
||||
+ pack.bytes() + " байт. SHA-1: " + pack.sha1();
|
||||
String message =
|
||||
"AstraPacks: готово, "
|
||||
+ pack.files()
|
||||
+ " файлов, "
|
||||
+ pack.bytes()
|
||||
+ " байт. SHA-1: "
|
||||
+ pack.sha1();
|
||||
source.sendSuccess(() -> Component.literal(message), true);
|
||||
if (config.enabled && config.build.resendAfterBuild) {
|
||||
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
|
||||
@@ -218,10 +246,22 @@ public final class PackRuntime implements AutoCloseable {
|
||||
}
|
||||
|
||||
public String status() {
|
||||
String pack = current == null ? "UNAVAILABLE" : current.path() + " (" + current.files() + " files)";
|
||||
String address = config.hosting.publicAddress.isBlank() ? "NOT SET" : config.hosting.publicAddress;
|
||||
return "AstraPacks HTTP:" + config.hosting.port + ", address=" + address + ", required=" + config.delivery.required
|
||||
+ ", build=" + building + ", pack=" + pack;
|
||||
String pack =
|
||||
current == null
|
||||
? "UNAVAILABLE"
|
||||
: current.path() + " (" + current.files() + " files)";
|
||||
String address =
|
||||
config.hosting.publicAddress.isBlank() ? "NOT SET" : config.hosting.publicAddress;
|
||||
return "AstraPacks HTTP:"
|
||||
+ config.hosting.port
|
||||
+ ", address="
|
||||
+ address
|
||||
+ ", required="
|
||||
+ config.delivery.required
|
||||
+ ", build="
|
||||
+ building
|
||||
+ ", pack="
|
||||
+ pack;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user