2026-09-06 18:22:00 +03:00
|
|
|
package online.astradev.packs;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
|
|
|
import net.minecraft.network.Connection;
|
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
|
import net.minecraft.network.protocol.common.ServerboundResourcePackPacket;
|
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
|
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
import online.astradev.packs.mixin.CommonAccessor;
|
|
|
|
|
|
2026-09-06 18:28:23 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
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;
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public final class PackRuntime implements AutoCloseable {
|
|
|
|
|
public static volatile PackRuntime INSTANCE;
|
|
|
|
|
private final MinecraftServer server;
|
|
|
|
|
private final Path root;
|
|
|
|
|
private final Path configPath;
|
|
|
|
|
private volatile PackConfig config;
|
|
|
|
|
private volatile PackBuilder.Pack current;
|
|
|
|
|
private final PackHost host;
|
2026-09-06 18:28:23 +03:00
|
|
|
private final Map<ServerCommonPacketListenerImpl, PackSession> sessions =
|
|
|
|
|
new ConcurrentHashMap<>();
|
2026-09-06 18:22:00 +03:00
|
|
|
private final Map<UUID, String> welcome = new ConcurrentHashMap<>();
|
2026-09-06 18:28:23 +03:00
|
|
|
private final ExecutorService worker =
|
|
|
|
|
Executors.newSingleThreadExecutor(
|
|
|
|
|
Thread.ofPlatform().daemon().name("AstraPacks-build").factory());
|
|
|
|
|
// Read and written on the server thread, including build completion callbacks.
|
|
|
|
|
private boolean busy;
|
2026-09-06 18:22:00 +03:00
|
|
|
private volatile boolean closed;
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public PackRuntime(MinecraftServer server, Path root, Path configPath, PackConfig config) {
|
2026-09-06 18:28:23 +03:00
|
|
|
this.server = server;
|
|
|
|
|
this.root = root;
|
|
|
|
|
this.configPath = configPath;
|
|
|
|
|
this.config = config;
|
2026-09-06 18:22:00 +03:00
|
|
|
this.host = new PackHost(config.hosting);
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void start() throws Exception {
|
|
|
|
|
if (!config.enabled) return;
|
|
|
|
|
if (server.getServerResourcePack().isPresent())
|
2026-09-06 18:28:23 +03:00
|
|
|
throw new IOException(
|
|
|
|
|
"AstraPacks must be the only pack sender: clear resource-pack in"
|
|
|
|
|
+ " server.properties");
|
2026-09-06 18:22:00 +03:00
|
|
|
// Only create the default source automatically. A typo in a custom path must fail visibly.
|
|
|
|
|
Files.createDirectories(root.resolve("astrapacks/contents"));
|
|
|
|
|
PackBuilder.Pack candidate;
|
2026-09-06 18:28:23 +03:00
|
|
|
try {
|
|
|
|
|
candidate =
|
|
|
|
|
config.build.onStartup
|
|
|
|
|
? new PackBuilder(config).build(root, "[88,0]")
|
|
|
|
|
: PackBuilder.previous(root, config);
|
|
|
|
|
} catch (IOException e) {
|
2026-09-06 18:22:00 +03:00
|
|
|
AstraPacks.LOG.error("Resource-pack build failed; trying the last successful build", e);
|
2026-09-06 18:28:23 +03:00
|
|
|
try {
|
|
|
|
|
candidate = PackBuilder.previous(root, config);
|
|
|
|
|
} catch (IOException absent) {
|
|
|
|
|
AstraPacks.LOG.error(
|
|
|
|
|
"No usable pack. Required-pack logins will be refused.", absent);
|
|
|
|
|
candidate = null;
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
if (candidate != null) {
|
2026-09-06 18:28:23 +03:00
|
|
|
try {
|
|
|
|
|
PackBuilder.verifyExternal(config, candidate);
|
|
|
|
|
current = candidate;
|
|
|
|
|
host.publish(candidate);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
AstraPacks.LOG.error(
|
|
|
|
|
"External ZIP is not ready. Required-pack logins will be refused.", e);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
host.start();
|
2026-09-06 18:28:23 +03:00
|
|
|
AstraPacks.LOG.info(
|
|
|
|
|
"AstraPacks: mode={}, required={}, pack={}",
|
|
|
|
|
config.hosting.mode,
|
|
|
|
|
config.delivery.required,
|
|
|
|
|
current == null ? "UNAVAILABLE" : current.path());
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
|
|
|
|
public PackConfig config() {
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PackBuilder.Pack current() {
|
|
|
|
|
return current;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PackHost host() {
|
|
|
|
|
return host;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public PackSession session(ServerCommonPacketListenerImpl listener) {
|
|
|
|
|
PackSession session = new PackSession(this, listener);
|
|
|
|
|
PackSession old = sessions.put(listener, session);
|
|
|
|
|
if (old != null) old.replace();
|
|
|
|
|
return session;
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
|
|
|
|
public boolean intercept(
|
|
|
|
|
ServerCommonPacketListenerImpl listener, ServerboundResourcePackPacket packet) {
|
2026-09-06 18:22:00 +03:00
|
|
|
if (!config.enabled) return false;
|
|
|
|
|
// Vanilla configuration handling finishes its own task for ANY terminal UUID.
|
|
|
|
|
// Consume unrelated/stale replies too, so they cannot finish our gate or another task.
|
2026-09-06 18:28:23 +03:00
|
|
|
server.execute(
|
|
|
|
|
() -> {
|
|
|
|
|
PackSession session = sessions.get(listener);
|
|
|
|
|
if (session != null) session.response(packet);
|
|
|
|
|
});
|
2026-09-06 18:22:00 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void tick() {
|
2026-09-06 18:28:23 +03:00
|
|
|
sessions.entrySet()
|
|
|
|
|
.removeIf(
|
|
|
|
|
entry -> {
|
|
|
|
|
if (!((CommonAccessor) entry.getKey())
|
|
|
|
|
.astrapacks$connection()
|
|
|
|
|
.isConnected()) {
|
|
|
|
|
welcome.remove(entry.getKey().getOwner().id());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
entry.getValue().tick();
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void loaded(ServerCommonPacketListenerImpl listener, String message) {
|
|
|
|
|
if (listener instanceof net.minecraft.server.network.ServerGamePacketListenerImpl game) {
|
2026-09-06 18:28:23 +03:00
|
|
|
if (!message.isBlank())
|
|
|
|
|
game.player.sendSystemMessage(PackText.of(message, listener.getOwner().name()));
|
2026-09-06 18:22:00 +03:00
|
|
|
} else welcome.put(listener.getOwner().id(), message);
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void welcome(net.minecraft.server.level.ServerPlayer player) {
|
|
|
|
|
String text = welcome.remove(player.getUUID());
|
2026-09-06 18:28:23 +03:00
|
|
|
if (text != null && !text.isBlank())
|
|
|
|
|
player.sendSystemMessage(PackText.of(text, player.getGameProfile().name()));
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public String url(ServerCommonPacketListenerImpl listener, PackBuilder.Pack pack) {
|
|
|
|
|
if (config.hosting.mode.equals("no_host")) return config.hosting.externalUrl;
|
2026-09-06 18:28:23 +03:00
|
|
|
Connection connection = ((CommonAccessor) listener).astrapacks$connection();
|
2026-09-06 18:22:00 +03:00
|
|
|
String base = config.hosting.publicAddress;
|
|
|
|
|
if (base.equals("auto")) {
|
2026-09-06 18:28:23 +03:00
|
|
|
String address = ((ConnectionData) connection).astrapacks$host();
|
|
|
|
|
if (address == null || address.isBlank())
|
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"Cannot determine server address; set hosting.publicAddress");
|
|
|
|
|
int port =
|
|
|
|
|
config.hosting.mode.equals("self_host")
|
|
|
|
|
? config.hosting.port
|
|
|
|
|
: server.getPort();
|
|
|
|
|
try {
|
|
|
|
|
base = new URI("http", null, address, port, null, null, null).toASCIIString();
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid server address", e);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
return base.replaceAll("/+$", "") + host.issuePath(pack);
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void rebuild(CommandSourceStack source, boolean reload, boolean publishOnly) {
|
2026-09-06 18:28:23 +03:00
|
|
|
if (closed) {
|
|
|
|
|
source.sendFailure(Component.literal("AstraPacks остановлен."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (busy) {
|
|
|
|
|
source.sendFailure(Component.literal("Сборка AstraPacks уже выполняется."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
busy = true;
|
|
|
|
|
source.sendSuccess(
|
|
|
|
|
() -> Component.literal("AstraPacks: проверка и подготовка ресурспака…"), false);
|
|
|
|
|
worker.execute(() -> preparePack(source, reload, publishOnly));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void preparePack(CommandSourceStack source, boolean reload, boolean publishOnly) {
|
|
|
|
|
try {
|
|
|
|
|
PackConfig next = reload ? PackConfig.load(configPath) : config;
|
|
|
|
|
if (next.enabled != config.enabled || !next.hosting.sameSettings(config.hosting)) {
|
|
|
|
|
throw new IOException("Изменение enabled или hosting требует перезапуска сервера.");
|
|
|
|
|
}
|
|
|
|
|
PackBuilder.Pack pack =
|
|
|
|
|
publishOnly
|
|
|
|
|
? PackBuilder.previous(root, next)
|
|
|
|
|
: new PackBuilder(next).build(root, "[88,0]");
|
|
|
|
|
PackBuilder.verifyExternal(next, pack);
|
|
|
|
|
server.execute(() -> publishPack(source, next, pack));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
AstraPacks.LOG.error("Pack build/publication failed; active pack retained", e);
|
|
|
|
|
server.execute(
|
|
|
|
|
() -> {
|
|
|
|
|
busy = false;
|
|
|
|
|
if (!closed) {
|
|
|
|
|
source.sendFailure(
|
|
|
|
|
Component.literal(
|
|
|
|
|
"AstraPacks: "
|
|
|
|
|
+ e.getMessage()
|
|
|
|
|
+ " Рабочий пак сохранён."));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void publishPack(CommandSourceStack source, PackConfig next, PackBuilder.Pack pack) {
|
|
|
|
|
busy = false;
|
|
|
|
|
if (closed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
config = next;
|
|
|
|
|
current = pack;
|
|
|
|
|
host.publish(pack);
|
|
|
|
|
String message =
|
|
|
|
|
"AstraPacks: готово, "
|
|
|
|
|
+ pack.files()
|
|
|
|
|
+ " файлов, "
|
|
|
|
|
+ pack.bytes()
|
|
|
|
|
+ " байт. SHA-1: "
|
|
|
|
|
+ pack.sha1();
|
|
|
|
|
source.sendSuccess(() -> Component.literal(message), true);
|
|
|
|
|
if (config.enabled && config.build.resendAfterBuild) {
|
|
|
|
|
resendAll();
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void resendAll() {
|
2026-09-06 18:28:23 +03:00
|
|
|
for (var player : server.getPlayerList().getPlayers())
|
|
|
|
|
session(player.connection).start(player.connection::send);
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public String status() {
|
2026-09-06 18:28:23 +03:00
|
|
|
return "AstraPacks "
|
|
|
|
|
+ config.hosting.mode
|
|
|
|
|
+ ", required="
|
|
|
|
|
+ config.delivery.required
|
|
|
|
|
+ ", build="
|
|
|
|
|
+ busy
|
|
|
|
|
+ ", pack="
|
|
|
|
|
+ (current == null
|
|
|
|
|
? "UNAVAILABLE"
|
|
|
|
|
: current.path() + " (" + current.files() + " files)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void close() {
|
|
|
|
|
closed = true;
|
|
|
|
|
worker.shutdownNow();
|
|
|
|
|
host.close();
|
|
|
|
|
sessions.clear();
|
|
|
|
|
welcome.clear();
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|