2026-09-06 18:22:00 +03:00
|
|
|
package online.astradev.packs;
|
|
|
|
|
|
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:36:18 +03:00
|
|
|
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.level.ServerPlayer;
|
|
|
|
|
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
|
|
|
|
|
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
|
|
|
|
import online.astradev.packs.mixin.CommonAccessor;
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public final class PackRuntime implements AutoCloseable {
|
|
|
|
|
public static volatile PackRuntime INSTANCE;
|
2026-09-06 18:36:18 +03:00
|
|
|
|
|
|
|
|
private static final String RESOURCE_VERSION = "[88,0]";
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private final MinecraftServer server;
|
|
|
|
|
private final Path root;
|
|
|
|
|
private final Path configPath;
|
2026-09-06 18:36:18 +03:00
|
|
|
private final PackHost host;
|
|
|
|
|
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());
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private volatile PackConfig config;
|
|
|
|
|
private volatile PackBuilder.Pack current;
|
|
|
|
|
private volatile boolean closed;
|
2026-09-06 18:36:18 +03:00
|
|
|
// Only the server thread changes this flag, including publication and failure callbacks.
|
|
|
|
|
private boolean building;
|
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:36:18 +03:00
|
|
|
public void start() throws IOException, InterruptedException {
|
|
|
|
|
if (!config.enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (server.getServerResourcePack().isPresent()) {
|
|
|
|
|
throw new IOException("AstraPacks must be the only pack sender: clear resource-pack in server.properties");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Custom source paths are not created: a typo in the configuration must be visible.
|
2026-09-06 18:22:00 +03:00
|
|
|
Files.createDirectories(root.resolve("astrapacks/contents"));
|
2026-09-06 18:36:18 +03:00
|
|
|
PackBuilder.Pack pack = loadStartupPack();
|
|
|
|
|
if (pack != null) {
|
2026-09-06 18:28:23 +03:00
|
|
|
try {
|
2026-09-06 18:36:18 +03:00
|
|
|
PackBuilder.verifyExternal(config, pack);
|
|
|
|
|
host.publish(pack);
|
|
|
|
|
current = pack;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
AstraPacks.LOG.error("External ZIP is not ready. Required-pack logins will be refused.", e);
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
|
|
|
|
|
host.start();
|
|
|
|
|
AstraPacks.LOG.info("AstraPacks: mode={}, required={}, pack={}",
|
|
|
|
|
config.hosting.mode, config.delivery.required, current == null ? "UNAVAILABLE" : current.path());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PackBuilder.Pack loadStartupPack() {
|
|
|
|
|
if (config.build.onStartup) {
|
2026-09-06 18:28:23 +03:00
|
|
|
try {
|
2026-09-06 18:36:18 +03:00
|
|
|
return new PackBuilder(config).build(root, RESOURCE_VERSION);
|
2026-09-06 18:28:23 +03:00
|
|
|
} catch (IOException e) {
|
2026-09-06 18:36:18 +03:00
|
|
|
AstraPacks.LOG.error("Resource-pack build failed; trying the last successful build", e);
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
try {
|
|
|
|
|
return PackBuilder.previous(root, config);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
AstraPacks.LOG.error("No usable pack. Required-pack logins will be refused.", e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
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);
|
2026-09-06 18:36:18 +03:00
|
|
|
PackSession previous = sessions.put(listener, session);
|
|
|
|
|
if (previous != null) {
|
|
|
|
|
previous.removeFromClient();
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
return session;
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:36:18 +03:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
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:36:18 +03:00
|
|
|
var iterator = sessions.entrySet().iterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
var entry = iterator.next();
|
|
|
|
|
ServerCommonPacketListenerImpl listener = entry.getKey();
|
|
|
|
|
Connection connection = ((CommonAccessor) listener).astrapacks$connection();
|
|
|
|
|
if (connection.isConnected()) {
|
|
|
|
|
entry.getValue().tick();
|
|
|
|
|
} else {
|
|
|
|
|
pendingWelcome.remove(listener.getOwner().id());
|
|
|
|
|
iterator.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
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) {
|
2026-09-06 18:36:18 +03:00
|
|
|
if (listener instanceof ServerGamePacketListenerImpl game) {
|
|
|
|
|
if (!message.isBlank()) {
|
2026-09-06 18:28:23 +03:00
|
|
|
game.player.sendSystemMessage(PackText.of(message, listener.getOwner().name()));
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
pendingWelcome.put(listener.getOwner().id(), message);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:36:18 +03:00
|
|
|
public void welcome(ServerPlayer player) {
|
|
|
|
|
String message = pendingWelcome.remove(player.getUUID());
|
|
|
|
|
if (message != null && !message.isBlank()) {
|
|
|
|
|
player.sendSystemMessage(PackText.of(message, 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) {
|
2026-09-06 18:36:18 +03:00
|
|
|
if (config.hosting.mode.equals("no_host")) {
|
|
|
|
|
return config.hosting.externalUrl;
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
String base = config.hosting.publicAddress;
|
|
|
|
|
if (base.equals("auto")) {
|
2026-09-06 18:36:18 +03:00
|
|
|
base = automaticAddress(listener);
|
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:36:18 +03:00
|
|
|
private String automaticAddress(ServerCommonPacketListenerImpl listener) {
|
|
|
|
|
Connection connection = ((CommonAccessor) listener).astrapacks$connection();
|
|
|
|
|
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 {
|
|
|
|
|
return new URI("http", null, address, port, null, null, null).toASCIIString();
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid server address", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void build(CommandSourceStack source) {
|
|
|
|
|
requestBuild(source, false, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void reload(CommandSourceStack source) {
|
|
|
|
|
requestBuild(source, true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void publish(CommandSourceStack source) {
|
|
|
|
|
requestBuild(source, false, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void requestBuild(CommandSourceStack source, boolean reload, boolean publishOnly) {
|
2026-09-06 18:28:23 +03:00
|
|
|
if (closed) {
|
|
|
|
|
source.sendFailure(Component.literal("AstraPacks остановлен."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
if (building) {
|
2026-09-06 18:28:23 +03:00
|
|
|
source.sendFailure(Component.literal("Сборка AstraPacks уже выполняется."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
|
|
|
|
|
building = true;
|
|
|
|
|
source.sendSuccess(() -> Component.literal("AstraPacks: проверка и подготовка ресурспака…"), false);
|
2026-09-06 18:28:23 +03:00
|
|
|
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 требует перезапуска сервера.");
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
PackBuilder.Pack pack = publishOnly
|
|
|
|
|
? PackBuilder.previous(root, next)
|
|
|
|
|
: new PackBuilder(next).build(root, RESOURCE_VERSION);
|
2026-09-06 18:28:23 +03:00
|
|
|
PackBuilder.verifyExternal(next, pack);
|
|
|
|
|
server.execute(() -> publishPack(source, next, pack));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
AstraPacks.LOG.error("Pack build/publication failed; active pack retained", e);
|
2026-09-06 18:36:18 +03:00
|
|
|
server.execute(() -> {
|
|
|
|
|
building = false;
|
|
|
|
|
if (!closed) {
|
|
|
|
|
source.sendFailure(Component.literal("AstraPacks: " + e.getMessage() + " Рабочий пак сохранён."));
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void publishPack(CommandSourceStack source, PackConfig next, PackBuilder.Pack pack) {
|
2026-09-06 18:36:18 +03:00
|
|
|
building = false;
|
2026-09-06 18:28:23 +03:00
|
|
|
if (closed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
config = next;
|
|
|
|
|
host.publish(pack);
|
2026-09-06 18:36:18 +03:00
|
|
|
current = pack;
|
|
|
|
|
|
|
|
|
|
String message = "AstraPacks: готово, " + pack.files() + " файлов, "
|
|
|
|
|
+ pack.bytes() + " байт. SHA-1: " + pack.sha1();
|
2026-09-06 18:28:23 +03:00
|
|
|
source.sendSuccess(() -> Component.literal(message), true);
|
|
|
|
|
if (config.enabled && config.build.resendAfterBuild) {
|
2026-09-06 18:36:18 +03:00
|
|
|
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
|
|
|
|
|
session(player.connection).start(player.connection::send);
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
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:36:18 +03:00
|
|
|
String pack = current == null ? "UNAVAILABLE" : current.path() + " (" + current.files() + " files)";
|
|
|
|
|
return "AstraPacks " + config.hosting.mode + ", required=" + config.delivery.required
|
|
|
|
|
+ ", build=" + building + ", pack=" + pack;
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void close() {
|
|
|
|
|
closed = true;
|
|
|
|
|
worker.shutdownNow();
|
|
|
|
|
host.close();
|
|
|
|
|
sessions.clear();
|
2026-09-06 18:36:18 +03:00
|
|
|
pendingWelcome.clear();
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|