Build shared resource folder with two-version retention and dedicated HTTP hosting

This commit is contained in:
2026-09-06 19:20:55 +03:00
parent 5b51cba772
commit f701699db8
12 changed files with 426 additions and 505 deletions
@@ -1,8 +1,6 @@
package online.astradev.packs;
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;
@@ -19,7 +17,6 @@ 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;
public final class PackRuntime implements AutoCloseable {
public static volatile PackRuntime INSTANCE;
@@ -57,22 +54,18 @@ public final class PackRuntime implements AutoCloseable {
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.
Files.createDirectories(root.resolve("astrapacks/contents"));
Files.createDirectories(config.contents(root));
PackBuilder.Pack pack = loadStartupPack();
if (pack != null) {
try {
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);
}
host.publish(pack);
current = pack;
}
host.start();
AstraPacks.LOG.info("AstraPacks: mode={}, required={}, pack={}",
config.hosting.mode, config.delivery.required, current == null ? "UNAVAILABLE" : current.path());
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.info("AstraPacks: HTTP port={}, required={}, pack={}",
config.hosting.port, config.delivery.required, current == null ? "UNAVAILABLE" : current.path());
}
private PackBuilder.Pack loadStartupPack() {
@@ -84,7 +77,7 @@ public final class PackRuntime implements AutoCloseable {
}
}
try {
return PackBuilder.previous(root, config);
return PackBuilder.latest(root, config);
} catch (IOException e) {
AstraPacks.LOG.error("No usable pack. Required-pack logins will be refused.", e);
return null;
@@ -99,10 +92,6 @@ public final class PackRuntime implements AutoCloseable {
return current;
}
public PackHost host() {
return host;
}
public PackSession session(ServerCommonPacketListenerImpl listener) {
PackSession session = new PackSession(this, listener);
PackSession previous = sessions.put(listener, session);
@@ -131,7 +120,7 @@ public final class PackRuntime implements AutoCloseable {
while (iterator.hasNext()) {
var entry = iterator.next();
ServerCommonPacketListenerImpl listener = entry.getKey();
Connection connection = ((CommonAccessor) listener).astrapacks$connection();
Connection connection = listener.getConnection();
if (connection.isConnected()) {
entry.getValue().tick();
} else {
@@ -158,44 +147,23 @@ public final class PackRuntime implements AutoCloseable {
}
}
public String url(ServerCommonPacketListenerImpl listener, PackBuilder.Pack pack) {
if (config.hosting.mode.equals("no_host")) {
return config.hosting.externalUrl;
}
String base = config.hosting.publicAddress;
if (base.equals("auto")) {
base = automaticAddress(listener);
}
return base.replaceAll("/+$", "") + host.issuePath(pack);
}
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 String url(PackBuilder.Pack pack) {
String address = config.hosting.publicAddress;
if (address.isBlank()) {
throw new IllegalStateException("Set hosting.publicAddress before sending resource packs");
}
return address.replaceAll("/+$", "") + host.issuePath(pack);
}
public void build(CommandSourceStack source) {
requestBuild(source, false, false);
requestBuild(source, false);
}
public void reload(CommandSourceStack source) {
requestBuild(source, true, false);
requestBuild(source, true);
}
public void publish(CommandSourceStack source) {
requestBuild(source, false, true);
}
private void requestBuild(CommandSourceStack source, boolean reload, boolean publishOnly) {
private void requestBuild(CommandSourceStack source, boolean reload) {
if (closed) {
source.sendFailure(Component.literal("AstraPacks остановлен."));
return;
@@ -207,19 +175,17 @@ public final class PackRuntime implements AutoCloseable {
building = true;
source.sendSuccess(() -> Component.literal("AstraPacks: проверка и подготовка ресурспака…"), false);
worker.execute(() -> preparePack(source, reload, publishOnly));
worker.execute(() -> preparePack(source, reload));
}
private void preparePack(CommandSourceStack source, boolean reload, boolean publishOnly) {
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)) {
throw new IOException("Изменение enabled или hosting требует перезапуска сервера.");
if (next.enabled != config.enabled || !next.hosting.sameSettings(config.hosting)
|| !next.build.storageDirectory.equals(config.build.storageDirectory)) {
throw new IOException("Изменение enabled, каталога хранения или настроек HTTP-сервера требует перезапуска.");
}
PackBuilder.Pack pack = publishOnly
? PackBuilder.previous(root, next)
: new PackBuilder(next).build(root, RESOURCE_VERSION);
PackBuilder.verifyExternal(next, pack);
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);
@@ -253,7 +219,8 @@ public final class PackRuntime implements AutoCloseable {
public String status() {
String pack = current == null ? "UNAVAILABLE" : current.path() + " (" + current.files() + " files)";
return "AstraPacks " + config.hosting.mode + ", required=" + config.delivery.required
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;
}