Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve transfer speed & Fix an issue that marks the mod useless #1

Open
wants to merge 2 commits into
base: 1.18
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class PolypackHostConfig {
public static final File CONFIG_FILE = new File(FabricLoader.getInstance().getConfigDir().toFile(), "polypack_host.json");
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create();

public String hostIp = null;
public String externalIp = "127.0.0.1";
public int hostPort = 8001;
public int threadCount = 3;
public boolean randomiseUrl = false;
Expand All @@ -30,6 +31,9 @@ public static PolypackHostConfig loadConfigFile(File file) {
if (config == null) {
config = new PolypackHostConfig();
}
if (Objects.equals(config.externalIp, "127.0.0.1")) {
PolypackHostMod.LOGGER.warn("External ip should be set to your external/public ip.");
}

config.saveConfigFile(file);
return config;
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/github/aws404/polypackhost/PolypackHostMod.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.github.aws404.polypackhost;

import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.file.Path;

public class PolypackHostMod implements DedicatedServerModInitializer {
public static final Logger LOGGER = LogManager.getLogger("Polypack Host");
public static final PolypackHostConfig CONFIG = PolypackHostConfig.loadConfigFile(PolypackHostConfig.CONFIG_FILE);
public static final Path POLYMER_PACK_FILE = Path.of(FabricLoader.getInstance().getGameDir().toFile() + "/polymer-resourcepack.zip");
public static final Logger LOGGER = LogManager.getLogger("Polypack Host");
public static final PolypackHostConfig CONFIG = PolypackHostConfig.loadConfigFile(PolypackHostConfig.CONFIG_FILE);

@Override
public void onInitializeServer() {
LOGGER.info("Starting Polypack Host Mod");
}
@Override
public void onInitializeServer() {
LOGGER.info("Starting Polypack Host Mod");
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,19 @@
package com.github.aws404.polypackhost;

import com.google.common.hash.Hashing;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import eu.pb4.polymer.api.resourcepack.PolymerRPUtils;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.io.*;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.Executors;

public class PolypackHttpHandler implements HttpHandler {
private static final PolypackHttpHandler handler = new PolypackHttpHandler();

private static final Path POLYMER_PACK_FILE = Path.of(FabricLoader.getInstance().getGameDir().toFile() + "/polymer-resourcepack.zip");

public static void start(MinecraftServer minecraftServer) {
try {
String serverIp = PolypackHostMod.CONFIG.hostIp == null || PolypackHostMod.CONFIG.hostIp.isEmpty() ? minecraftServer.getServerIp() : PolypackHostMod.CONFIG.hostIp;
if (serverIp.isEmpty()) {
serverIp = InetAddress.getLocalHost().getHostAddress();
}
PolypackHostMod.LOGGER.info("Building polymer resource pack...");
PolymerRPUtils.build(POLYMER_PACK_FILE);

String subUrl = PolypackHostMod.CONFIG.randomiseUrl ? Integer.toString(new Random().nextInt(Integer.MAX_VALUE)) : "pack";

HttpServer server = HttpServer.create(new InetSocketAddress(serverIp, PolypackHostMod.CONFIG.hostPort), 0);
server.createContext("/" + subUrl, new PolypackHttpHandler());
server.setExecutor(Executors.newFixedThreadPool(PolypackHostMod.CONFIG.threadCount));
server.start();

String packIp = String.format("http://%s:%s/%s", serverIp, PolypackHostMod.CONFIG.hostPort, subUrl);

PolypackHostMod.LOGGER.info("Polymer resource pack host started at {}", packIp);
private PolypackHttpHandler() {
}

String hash = com.google.common.io.Files.asByteSource(PolypackHttpHandler.POLYMER_PACK_FILE.toFile()).hash(Hashing.sha1()).toString();
minecraftServer.setResourcePack(packIp, hash);
} catch (IOException e) {
PolypackHostMod.LOGGER.error("Failed to start the resource pack server!", e);
e.printStackTrace();
}
public static PolypackHttpHandler getHandler() {
return handler;
}

@Override
Expand All @@ -61,16 +26,15 @@ public void handle(HttpExchange exchange) throws IOException {
}

OutputStream outputStream = exchange.getResponseBody();
File pack = POLYMER_PACK_FILE.toFile();
File pack = PolypackHostMod.POLYMER_PACK_FILE.toFile();

exchange.getResponseHeaders().add("User-Agent", "Java/polypack-host");
exchange.sendResponseHeaders(200, pack.length());

FileInputStream fis = new FileInputStream(pack);
int b;
while ((b = fis.read()) != -1) {
outputStream.write(b);
}
BufferedInputStream bis = new BufferedInputStream(fis);
bis.transferTo(outputStream);
bis.close();
fis.close();

outputStream.flush();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.aws404.polypackhost;

import com.google.common.hash.Hashing;
import com.sun.net.httpserver.HttpServer;
import eu.pb4.polymer.api.resourcepack.PolymerRPUtils;
import net.minecraft.server.MinecraftServer;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.google.common.io.Files.asByteSource;

public class PolypackHttpServer {
private static HttpServer server = null;
private static ExecutorService threadPool = null;

private PolypackHttpServer() {
}

public static void stop() {
server.stop(1);
threadPool.shutdownNow();
}

@SuppressWarnings({"UnstableApiUsage", "deprecation"})
public static void init(MinecraftServer minecraftServer) {
try {
int port = PolypackHostMod.CONFIG.hostPort;

String externalIp = PolypackHostMod.CONFIG.externalIp;
assert externalIp != null;

String listening = "0.0.0.0";
PolypackHostMod.LOGGER.info("Building polymer resource pack...");
PolymerRPUtils.build(PolypackHostMod.POLYMER_PACK_FILE);

String subUrl = PolypackHostMod.CONFIG.randomiseUrl ? Integer.toString(new Random().nextInt(Integer.MAX_VALUE)) : "pack";

server = HttpServer.create(new InetSocketAddress(listening, port), 0);
server.createContext("/" + subUrl, PolypackHttpHandler.getHandler());
threadPool = Executors.newFixedThreadPool(PolypackHostMod.CONFIG.threadCount);
server.setExecutor(threadPool);
server.start();

PolypackHostMod.LOGGER.info("Server listening on {}:{}/{}", listening, port, subUrl);

String packIp = String.format("http://%s:%s/%s", externalIp, port, subUrl);

PolypackHostMod.LOGGER.info("Polymer resource pack host started at {}", packIp);

String hash = asByteSource(PolypackHostMod.POLYMER_PACK_FILE.toFile()).hash(Hashing.sha1()).toString();
minecraftServer.setResourcePack(packIp, hash);
} catch (IOException e) {
PolypackHostMod.LOGGER.error("Failed to start the resource pack server!", e);
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.aws404.polypackhost.mixin;

import com.github.aws404.polypackhost.PolypackHttpHandler;
import com.github.aws404.polypackhost.PolypackHttpServer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldGenerationProgressListener;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -13,6 +13,11 @@ public class MinecraftServerMixin {

@Inject(method = "prepareStartRegion", at = @At("HEAD"))
private void initPolypackHost(WorldGenerationProgressListener worldGenerationProgressListener, CallbackInfo ci) {
PolypackHttpHandler.start((MinecraftServer) (Object) this);
PolypackHttpServer.init((MinecraftServer) (Object) this);
}

@Inject(method = "shutdown", at = @At("TAIL"))
private void stopPolypackHost(CallbackInfo ci) {
PolypackHttpServer.stop();
}
}