111 lines
4.3 KiB
Java
111 lines
4.3 KiB
Java
package io.gitlab.jfronny.dynres;
|
|
|
|
import com.mojang.brigadier.Command;
|
|
import io.gitlab.jfronny.dynres.web.RequestHandler;
|
|
import io.gitlab.jfronny.dynres.web.bluemapcore.WebServer;
|
|
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
|
|
import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer;
|
|
import net.fabricmc.api.DedicatedServerModInitializer;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
import net.fabricmc.loader.gui.FabricGuiEntry;
|
|
import net.minecraft.server.command.CommandManager;
|
|
import net.minecraft.text.LiteralText;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.util.Arrays;
|
|
|
|
@Environment(EnvType.SERVER)
|
|
public class DynRes implements DedicatedServerModInitializer {
|
|
static WebServer server;
|
|
public static File resFile;
|
|
public static Cfg cfg;
|
|
|
|
static {
|
|
AutoConfig.register(Cfg.class, JanksonConfigSerializer::new);
|
|
cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig();
|
|
resFile = new File(FabricLoader.getInstance().getGameDir().toFile(), cfg.resourcesFile);
|
|
if (!resFile.isFile()) {
|
|
FabricGuiEntry.displayCriticalError(new FileNotFoundException("The file " + resFile + " does not exist in the game directory but is required"), true);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onInitializeServer() {
|
|
ServerLifecycleEvents.SERVER_STOPPED.register(t -> {
|
|
try {
|
|
server.close();
|
|
server.join();
|
|
} catch (Throwable e) {
|
|
Logger.l.error("Failed to stop web server", e);
|
|
}
|
|
});
|
|
|
|
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
|
|
if (dedicated) {
|
|
dispatcher.register(CommandManager.literal("dynres").requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4))
|
|
.executes(context -> {
|
|
context.getSource().sendFeedback(new LiteralText("DynRes is active. Use dynres restart to reload"), false);
|
|
return Command.SINGLE_SUCCESS;
|
|
}));
|
|
dispatcher.register(CommandManager.literal("dynres").requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4))
|
|
.then(CommandManager.literal("restart").executes(context -> {
|
|
try {
|
|
context.getSource().sendFeedback(new LiteralText("Restarting DynRes"), true);
|
|
cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig();
|
|
resFile = new File(FabricLoader.getInstance().getGameDir().toFile(), cfg.resourcesFile);
|
|
restartServer();
|
|
}
|
|
catch (Exception e) {
|
|
Logger.l.error("Failed to run restart command", e);
|
|
context.getSource().sendError(new LiteralText(e.getMessage()));
|
|
}
|
|
return Command.SINGLE_SUCCESS;
|
|
})));
|
|
}
|
|
else {
|
|
Logger.l.error("DYNRES SHOULD NOT BE RUN ON INTERNAL SERVERS!");
|
|
}
|
|
});
|
|
|
|
restartServer();
|
|
}
|
|
|
|
public static void restartServer() {
|
|
int port = cfg.port;
|
|
if (server != null) {
|
|
port = server.getPort();
|
|
server.close();
|
|
try {
|
|
server.join();
|
|
} catch (InterruptedException e) {
|
|
//It is most likely already dead
|
|
}
|
|
}
|
|
server = new WebServer(port, cfg.maxConnections, null, new RequestHandler("resources.zip"));
|
|
server.start();
|
|
}
|
|
|
|
public static String getPort() {
|
|
return Integer.toString(server.getPort());
|
|
}
|
|
|
|
public static String simplifyElement(String s) {
|
|
String path = s.toLowerCase();
|
|
if (path.startsWith("/")) path = path.substring(1);
|
|
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
|
|
return path;
|
|
}
|
|
|
|
public static String removePort(String s) {
|
|
String[] r = s.split(":");
|
|
if (r.length > 2)
|
|
r = Arrays.copyOf(r, 2);
|
|
return String.join(":", r);
|
|
}
|
|
}
|