server almost finished

This commit is contained in:
2024-09-28 01:45:15 +02:00
parent 5e9a266c7b
commit af826ae665
4 changed files with 154 additions and 176 deletions

View File

@@ -1,6 +1,7 @@
.gradle .gradle
build/ build/
run/ run/*
!run/.gitkeep
!gradle/wrapper/gradle-wrapper.jar !gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/ !**/src/main/**/build/
!**/src/test/**/build/ !**/src/test/**/build/

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component> </component>
</project> </project>

View File

View File

@@ -4,15 +4,11 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Path; import java.util.*;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
@@ -23,46 +19,77 @@ import com.google.gson.JsonParser;
public class BoundlessServer { public class BoundlessServer {
private static final Logger logger = LogManager.getLogger(BoundlessServer.class); private static final Logger logger = LogManager.getLogger(BoundlessServer.class);
private static final String currentDir = System.getProperty("user.dir"); private static final String currentDir = System.getProperty("user.dir");
private static final String installerFileName = "installer.jar";
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
logger.info("Downloading and installing NeoForge Server");
String neoForgeVersion = "21.1.62"; String neoForgeVersion = "21.1.62";
downloadAndExecuteJar( String jarUrl = String.format("https://maven.neoforged.net/releases/net/neoforged/neoforge/%s/neoforge-%s-installer.jar", neoForgeVersion, neoForgeVersion);
"https://maven.neoforged.net/releases/net/neoforged/neoforge/" + neoForgeVersion + "/neoforge-" + neoForgeVersion + "-installer.jar",
"installer.jar", new String[]{"--install-server"}); logger.info("Downloading and installing NeoForge Server...");
logger.info("↑ Yeah no I'll do that for you automatically..."); downloadAndExecuteJar(jarUrl, new String[]{"--install-server"});
Files.deleteIfExists(Path.of(currentDir,"installer.jar")); deleteFileIfExists(Path.of(currentDir, installerFileName));
downloadMrPack("https://cdn.modrinth.com/data/ScTwzzH2/versions/IImtmTWG/Boundless%20Horizons%200.2.0.mrpack");
String mrPackUrl = "https://cdn.modrinth.com/data/ScTwzzH2/versions/IImtmTWG/Boundless%20Horizons%200.2.0.mrpack";
String[] excludedPaths = {
"mods/Sounds",
"mods/sodium-extra",
"mods/watermedia",
"mods/world-host"
};
downloadMrPack(mrPackUrl, excludedPaths);
launchServer();
} }
public static void downloadAndExecuteJar(String fileURL, String fileName, String[] jarArgs) { private static boolean shouldProcessFile(JsonObject fileObject, String[] excludedPaths) {
String currentDir = System.getProperty("user.dir"); String path = fileObject.get("path").getAsString();
String destinationPath = currentDir + File.separator + fileName; if (path.contains("mods/") && "required".equals(fileObject.getAsJsonObject("env").get("server").getAsString())) {
for (String exclude : excludedPaths) {
if (path.startsWith(exclude)) {
return false;
}
}
return true;
}
return false;
}
private static void launchServer() {
String neoForgeVersion = "21.1.62";
String osSpecificArgs = System.getProperty("os.name").startsWith("Windows")
? "@libraries/net/neoforged/neoforge/" + neoForgeVersion + "/win_args.txt"
: "@libraries/net/neoforged/neoforge/" + neoForgeVersion + "/unix_args.txt";
logger.info("Starting Boundless Horizons Server...");
executeJarFile(new String[]{"@user_jvm_args.txt", osSpecificArgs});
}
private static void downloadAndExecuteJar(String fileURL, String[] jarArgs) {
String destinationPath = currentDir + File.separator + installerFileName;
try { try {
logger.info("Downloading the JAR file from: {}", fileURL); logger.info("Downloading file from: {}", fileURL);
URI uri = new URI(fileURL); URL url = new URI(fileURL).toURL();
URL url = uri.toURL(); try (InputStream inputStream = url.openStream()) {
InputStream inputStream = url.openStream(); Files.copy(inputStream, Paths.get(destinationPath), StandardCopyOption.REPLACE_EXISTING);
Path targetPath = Paths.get(destinationPath); }
Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING); logger.info("Download completed: {}", destinationPath);
inputStream.close();
logger.info("Download completed successfully: {}", destinationPath);
} catch (IOException | URISyntaxException e) { } catch (IOException | URISyntaxException e) {
logger.error("Error downloading the file from {}", fileURL, e); logger.error("Error downloading file from {}: {}", fileURL, e.getMessage());
return; return;
} }
logger.info("Executing the downloaded JAR file...");
executeJarFile(new String[]{"-jar",installerFileName, "--install-server"});
}
private static void executeJarFile(String[] javaArgs) {
String[] command = new String[1 + javaArgs.length];
command[0] = "java";
System.arraycopy(javaArgs, 0, command, 1, javaArgs.length);
try { try {
logger.info("Executing the downloaded JAR file..."); ProcessBuilder processBuilder = new ProcessBuilder(command).directory(new File(currentDir));
String[] command = new String[3 + jarArgs.length];
command[0] = "java";
command[1] = "-jar";
command[2] = fileName;
System.arraycopy(jarArgs, 0, command, 3, jarArgs.length);
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.directory(new File(currentDir));
Process process = processBuilder.start(); Process process = processBuilder.start();
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT"); StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");
@@ -74,202 +101,152 @@ public class BoundlessServer {
process.waitFor(); process.waitFor();
process.getOutputStream().close(); process.getOutputStream().close();
outputGobbler.join(); outputGobbler.join();
errorGobbler.join(); errorGobbler.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
logger.error("Error executing the JAR file: {}", fileName, e); logger.error("Error executing command: {}", Arrays.toString(command), e);
} }
} }
public static void downloadMrPack(String fileURL) { private static void downloadMrPack(String fileURL, String[] excludedPaths) {
String outputFile = "modrinth.index.json"; Path tempMrPackPath = Path.of(currentDir, "temp.mrpack");
try { try {
logger.info("Downloading .mrpack file from: {}", fileURL); logger.info("Downloading .mrpack file from: {}", fileURL);
URI uri = new URI(fileURL); URL url = new URI(fileURL).toURL();
URL url = uri.toURL(); try (InputStream inputStream = url.openStream()) {
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); Files.copy(inputStream, tempMrPackPath, StandardCopyOption.REPLACE_EXISTING);
httpConn.setRequestMethod("GET");
httpConn.setConnectTimeout(5000);
httpConn.setReadTimeout(5000);
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpConn.getInputStream();
Path outputPath = Path.of(currentDir, "temp.mrpack");
Files.copy(inputStream, outputPath, StandardCopyOption.REPLACE_EXISTING);
inputStream.close();
logger.info("Download completed successfully: {}", outputPath);
unzipMrPack(outputPath.toString());
if (!Files.exists(Path.of(currentDir, outputFile))) {
logger.error("An error occurred: temp.mrpack does not contain a valid modrinth.index.json file.");
return;
}
logger.info("Extraction completed. Processing {}...", outputFile);
processModrinthIndex(outputFile);
copyOverrides();
Files.deleteIfExists(outputPath);
Files.deleteIfExists(Path.of(currentDir,"modrinth.index.json"));
logger.info("Temporary file temp.mrpack deleted.");
} else {
logger.error("Failed to download the file: Server returned response code {}", responseCode);
} }
httpConn.disconnect(); logger.info("Download completed: {}", tempMrPackPath);
} catch (IOException | URISyntaxException e) {
logger.error("Error downloading or extracting the .mrpack file: {}", e.getMessage());
}
}
private static void processModrinthIndex(String jsonFile) { unzipMrPack(tempMrPackPath.toString());
logger.info("Processing {}...", jsonFile);
try (Reader reader = Files.newBufferedReader(Path.of(jsonFile))) { Path modrinthIndexFile = Path.of(currentDir, "modrinth.index.json");
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject(); if (!Files.exists(modrinthIndexFile)) {
String name = jsonObject.get("name").getAsString(); logger.error("Error: temp.mrpack does not contain a valid modrinth.index.json file.");
String versionId = jsonObject.get("versionId").getAsString(); return;
}
logger.info("Installing modpack {} version {}...", name, versionId); logger.info("Processing modpack...");
try (Reader reader = Files.newBufferedReader(modrinthIndexFile)) {
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
JsonArray files = jsonObject.getAsJsonArray("files");
JsonArray files = jsonObject.getAsJsonArray("files"); for (var fileElement : files) {
JsonObject fileObject = fileElement.getAsJsonObject();
String path = fileObject.get("path").getAsString();
String downloadUrl = fileObject.getAsJsonArray("downloads").get(0).getAsString();
for (int i = 0; i < files.size(); i++) { if (shouldProcessFile(fileObject, excludedPaths)) {
JsonObject fileObject = files.get(i).getAsJsonObject(); Path outputPath = Path.of(currentDir, path);
String downloadUrl = fileObject.getAsJsonArray("downloads").get(0).getAsString(); Files.createDirectories(outputPath.getParent());
String path = fileObject.get("path").getAsString(); logger.info("Downloading mod: {}", path);
JsonObject env = fileObject.getAsJsonObject("env"); try (InputStream in = new URL(downloadUrl).openStream()) {
Files.copy(in, outputPath, StandardCopyOption.REPLACE_EXISTING);
if (path.contains("mods/") && "required".equals(env.get("server").getAsString())) { logger.info("Downloaded {} to {}", path, outputPath);
Path outputPath = Path.of(System.getProperty("user.dir"), path); } catch (IOException e) {
Files.createDirectories(outputPath.getParent()); logger.error("Failed to download mod {}: {}", path, e.getMessage());
}
logger.info("Downloading {}...", path); } else {
try (InputStream in = new URL(downloadUrl).openStream()) { logger.info("Skipping: {}", path);
Files.copy(in, outputPath, StandardCopyOption.REPLACE_EXISTING);
logger.info("Downloaded {} to {}", path, outputPath);
} catch (IOException e) {
logger.error("Failed to download {}: {}", path, e.getMessage());
} }
} else {
logger.info("Skipping {}: not a required mod or does not belong to mods directory.", path);
} }
} catch (IOException e) {
logger.error("Error processing modrinth.index.json: {}", e.getMessage());
} }
} catch (IOException e) {
logger.error("Error processing {}: {}", jsonFile, e.getMessage()); copyOverrides();
Files.deleteIfExists(tempMrPackPath);
Files.deleteIfExists(modrinthIndexFile);
} catch (IOException | URISyntaxException e) {
logger.error("Error downloading or processing .mrpack file: {}", e.getMessage());
} }
} }
private static void copyOverrides() { private static void copyOverrides() {
Path modsSource = Path.of(currentDir, "overrides", "mods"); Path modsSource = Path.of(currentDir, "overrides", "mods");
Path configSource = Path.of(currentDir, "overrides", "config"); Path configSource = Path.of(currentDir, "overrides", "config");
Path modsDestination = Path.of(currentDir, "mods");
Path configDestination = Path.of(currentDir, "config");
Path overridesSource = Path.of(currentDir, "overrides");
try { if (Files.exists(modsSource)) {
if (Files.exists(modsSource)) { try {
if (!Files.exists(modsDestination)) {
Files.createDirectories(modsDestination);
}
Files.walk(modsSource).forEach(source -> { Files.walk(modsSource).forEach(source -> {
Path dest = modsDestination.resolve(modsSource.relativize(source)); Path destination = Path.of(currentDir, "mods").resolve(modsSource.relativize(source));
try { try {
if (Files.isDirectory(source)) { if (Files.isDirectory(source)) {
Files.createDirectories(dest); Files.createDirectories(destination);
} else { } else {
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING); Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
logger.info("Copied mod: {}", destination);
} }
logger.info("Copied mod: {}", dest);
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to copy mod {}: {}", source, e.getMessage()); logger.error("Error copying mod: {}", source, e.getMessage());
} }
}); });
} else { } catch (IOException e) {
logger.warn("Mods source directory does not exist: {}", modsSource); logger.error("Error copying mods: {}", e.getMessage());
} }
} else {
logger.warn("Mods source directory does not exist: {}", modsSource);
}
if (Files.exists(configSource)) { if (Files.exists(configSource)) {
if (!Files.exists(configDestination)) { try {
Files.createDirectories(configDestination);
}
Files.walk(configSource).forEach(source -> { Files.walk(configSource).forEach(source -> {
Path dest = configDestination.resolve(configSource.relativize(source)); Path destination = Path.of(currentDir, "config").resolve(configSource.relativize(source));
try { try {
if (Files.isDirectory(source)) { if (Files.isDirectory(source)) {
Files.createDirectories(dest); Files.createDirectories(destination);
} else { } else {
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING); Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
logger.info("Copied config: {}", destination);
} }
logger.info("Copied config: {}", dest);
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to copy config {}: {}", source, e.getMessage()); logger.error("Error copying config: {}", source, e.getMessage());
} }
}); });
} else { } catch (IOException e) {
logger.warn("Config source directory does not exist: {}", configSource); logger.error("Error copying config: {}", e.getMessage());
} }
deleteDirectoryRecursively(overridesSource);
logger.info("Deleted the overrides directory successfully.");
} catch (IOException e) {
logger.error("Error while copying / deleting overrides: {}", e.getMessage());
} }
} }
private static void deleteDirectoryRecursively(Path dirPath) throws IOException { private static void unzipMrPack(String zipFilePath) throws IOException {
Files.walk(dirPath) Path destDir = Path.of(currentDir);
.sorted(Comparator.reverseOrder()) try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
.forEach(path -> { ZipEntry entry = zipIn.getNextEntry();
try { while (entry != null) {
Files.delete(path); Path filePath = destDir.resolve(entry.getName());
} catch (IOException e) { if (!entry.isDirectory()) {
logger.error("Failed to delete {}: {}", path, e.getMessage()); Files.createDirectories(filePath.getParent());
} try (OutputStream out = Files.newOutputStream(filePath)) {
}); zipIn.transferTo(out);
}
private static void unzipMrPack(String mrPackFilePath) {
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(mrPackFilePath))) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
File file = new File(currentDir, entry.getName());
if (entry.isDirectory()) {
if (!file.isDirectory() && !file.mkdirs()) {
logger.warn("Failed to create directory: {}", file);
} }
} else { } else {
file.getParentFile().mkdirs(); Files.createDirectories(filePath);
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = zipInputStream.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
}
logger.info("Extracted file: {}", file);
} }
zipInputStream.closeEntry(); zipIn.closeEntry();
entry = zipIn.getNextEntry();
} }
}
}
private static void deleteFileIfExists(Path path) {
try {
Files.deleteIfExists(path);
} catch (IOException e) { } catch (IOException e) {
logger.error("Error while extracting .mrpack file: {}", e.getMessage()); logger.error("Error deleting file: {}", path, e.getMessage());
} }
} }
private static class StreamGobbler extends Thread { private static class StreamGobbler extends Thread {
private final InputStream inputStream; private final InputStream inputStream;
private final String streamType; private final String type;
public StreamGobbler(InputStream inputStream, String streamType) { public StreamGobbler(InputStream inputStream, String type) {
this.inputStream = inputStream; this.inputStream = inputStream;
this.streamType = streamType; this.type = type;
} }
@Override @Override
@@ -277,10 +254,10 @@ public class BoundlessServer {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
logger.info(line); logger.info("[{}] {}", type, line);
} }
} catch (IOException e) { } catch (IOException e) {
logger.error("Error reading {} stream: {}", streamType, e.getMessage()); logger.error("Error reading output stream: {}", e.getMessage());
} }
} }
} }