Skip to content
Draft
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
12 changes: 5 additions & 7 deletions core/src/main/java/pl/skidam/automodpack_core/Constants.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package pl.skidam.automodpack_core;

import java.nio.file.Path;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pl.skidam.automodpack_core.config.Jsons;
import pl.skidam.automodpack_core.loader.*;
import pl.skidam.automodpack_core.modpack.ModpackExecutor;
import pl.skidam.automodpack_core.protocol.netty.NettyServer;

import java.nio.file.Path;

// More or less constants
// TODO cleanup
public class Constants {

public static final Logger LOGGER = LogManager.getLogger("AutoModpack");
public static final String MOD_ID = "automodpack"; // For real its "automodpack_mod" but we use this for resource locations etc.
public static Boolean DEBUG = false;
Expand All @@ -28,8 +28,8 @@ public class Constants {
public static Path MODS_DIR;
public static ModpackExecutor modpackExecutor;
public static NettyServer hostServer;
public static Jsons.ServerConfigFieldsV2 serverConfig;
public static Jsons.ClientConfigFieldsV2 clientConfig;
public static Jsons.ServerConfigFieldsV3 serverConfig;
public static Jsons.ClientConfigFieldsV3 clientConfig;
public static Jsons.KnownHostsFields knownHosts;
public static final Path automodpackDir = Path.of("automodpack");
public static final Path storeDir = automodpackDir.resolve("store");
Expand All @@ -38,10 +38,8 @@ public class Constants {
// Main - required
// Addons - optional addon packs
// Switches - optional or required packs, chosen by the player, only one can be installed at a time
public static final Path hostContentModpackDir = hostModpackDir.resolve("main");
public static Path hostModpackContentFile = hostModpackDir.resolve("automodpack-content.json");
public static Path serverConfigFile = automodpackDir.resolve("automodpack-server.json");
public static Path clientLocalMetadataFile = automodpackDir.resolve("automodpack-client-metadata.json");
public static Path cacheDir = automodpackDir.resolve("cache");
public static Path hashCacheDBFile = cacheDir.resolve("hash-cache.db");
public static Path modCacheDBFile = cacheDir.resolve("mod-cache.db");
Expand All @@ -54,10 +52,10 @@ public class Constants {
public static final Path serverCertFile = privateDir.resolve("cert.crt");
public static final Path serverPrivateKeyFile = privateDir.resolve("key.pem");


// Client
public static final Path modpackContentTempFile = automodpackDir.resolve("automodpack-content.json.temp");
public static final Path clientConfigFile = automodpackDir.resolve("automodpack-client.json");
public static final Path clientSelectionFile = automodpackDir.resolve("automodpack-client-selection.json");
public static final Path clientSecretsFile = privateDir.resolve("automodpack-client-secrets.json");
public static final Path modpacksDir = automodpackDir.resolve("modpacks");

Expand Down
20 changes: 9 additions & 11 deletions core/src/main/java/pl/skidam/automodpack_core/Server.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package pl.skidam.automodpack_core;

import static pl.skidam.automodpack_core.Constants.*;

import java.nio.file.Path;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;
import pl.skidam.automodpack_core.config.ConfigTools;
import pl.skidam.automodpack_core.config.Jsons;
import pl.skidam.automodpack_core.modpack.ModpackExecutor;
import pl.skidam.automodpack_core.modpack.ModpackContent;
import pl.skidam.automodpack_core.modpack.ModpackExecutor;
import pl.skidam.automodpack_core.protocol.netty.NettyServer;

import java.nio.file.Path;
import java.util.HashSet;

import static pl.skidam.automodpack_core.Constants.*;

public class Server {

// TODO Finish this class that it will be able to host the server without mod
public static void main(String[] args) {

if (args.length < 1) {
LOGGER.error("Modpack id not provided!");
return;
Expand All @@ -35,9 +35,8 @@ public static void main(String[] args) {
serverConfigFile = modpackDir.resolve("automodpack-server.json");
serverCoreConfigFile = modpackDir.resolve("automodpack-core.json");

serverConfig = ConfigTools.load(serverConfigFile, Jsons.ServerConfigFieldsV2.class);
serverConfig = ConfigTools.load(serverConfigFile, Jsons.ServerConfigFieldsV3.class);
if (serverConfig != null) {
serverConfig.syncedFiles = new HashSet<>();
serverConfig.validateSecrets = false;
ConfigTools.save(serverConfigFile, serverConfig);

Expand All @@ -60,8 +59,7 @@ public static void main(String[] args) {
mainModpackDir.toFile().mkdirs();

ModpackExecutor modpackExecutor = new ModpackExecutor();
ModpackContent modpackContent = new ModpackContent(serverConfig.modpackName, null, mainModpackDir, serverConfig.syncedFiles, serverConfig.allowEditsInFiles, serverConfig.forceCopyFilesToStandardLocation, modpackExecutor.getExecutor());
boolean generated = modpackExecutor.generateNew(modpackContent);
boolean generated = modpackExecutor.generateNew();

if (generated) {
LOGGER.info("Modpack generated!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@

package pl.skidam.automodpack_core.config;

import com.google.gson.*;
import pl.skidam.automodpack_core.utils.AddressHelpers;
import static pl.skidam.automodpack_core.Constants.*;

import com.google.gson.*;
import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import static pl.skidam.automodpack_core.Constants.*;
import pl.skidam.automodpack_core.utils.AddressHelpers;

public class ConfigTools {

public static Gson GSON = new GsonBuilder()
.disableHtmlEscaping()
.setPrettyPrinting()
.registerTypeAdapter(InetSocketAddress.class, new InetSocketAddressTypeAdapter())
.create();
public static Gson GSON = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().registerTypeAdapter(InetSocketAddress.class, new InetSocketAddressTypeAdapter()).create();

private static class InetSocketAddressTypeAdapter implements JsonSerializer<InetSocketAddress>, JsonDeserializer<InetSocketAddress> {

private static class InetSocketAddressTypeAdapter implements JsonSerializer<InetSocketAddress>,JsonDeserializer<InetSocketAddress> {
@Override
public JsonElement serialize(InetSocketAddress src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getHostString() + ":" + src.getPort());
Expand Down Expand Up @@ -50,14 +45,14 @@ public static <T> T softLoad(Path configFile, Class<T> configClass) {
String json = Files.readString(configFile);
return GSON.fromJson(json, configClass);
}
} catch (Exception ignored) { }
} catch (Exception ignored) {}
return null;
}

public static <T> T load(Path configFile, Class<T> configClass) {
try {
if (!Files.isDirectory(configFile.getParent())) {
Files.createDirectories(configFile.getParent());
Files.createDirectories(configFile.getParent());
}

if (Files.isRegularFile(configFile)) {
Expand All @@ -80,7 +75,8 @@ public static <T> T load(Path configFile, Class<T> configClass) {
e.printStackTrace();
}

try { // create new config
try {
// create new config
T obj = getConfigObject(configClass);
save(configFile, obj);
return obj;
Expand All @@ -94,7 +90,7 @@ public static <T> T load(Path configFile, Class<T> configClass) {
public static <T> T load(String json, Class<T> configClass) {
try {
if (json != null) {
return GSON.fromJson(json, configClass);
return GSON.fromJson(json, configClass);
}
} catch (Exception e) {
LOGGER.error("Couldn't load config! " + configClass);
Expand All @@ -121,21 +117,20 @@ public static void save(Path configFile, Object configObject) {
}
}


// Modpack content stuff
public static Jsons.ModpackContentFields loadModpackContent(Path modpackContentFile) {
public static Jsons.ModpackContent loadModpackContent(Path modpackContentFile) {
try {
if (Files.isRegularFile(modpackContentFile)) {
String json = Files.readString(modpackContentFile);
return GSON.fromJson(json, Jsons.ModpackContentFields.class);
return GSON.fromJson(json, Jsons.ModpackContent.class);
}
} catch (Exception e) {
LOGGER.error("Couldn't load modpack content! {}", modpackContentFile.toAbsolutePath().normalize(), e);
}
return null;
}

public static void saveModpackContent(Path modpackContentFile, Jsons.ModpackContentFields configObject) {
public static void saveModpackContent(Path modpackContentFile, Jsons.ModpackContent configObject) {
try {
if (!Files.isDirectory(modpackContentFile.getParent())) {
Files.createDirectories(modpackContentFile.getParent());
Expand All @@ -147,4 +142,33 @@ public static void saveModpackContent(Path modpackContentFile, Jsons.ModpackCont
e.printStackTrace();
}
}

public static Jsons.ClientSelectionManagerFields loadClientSelectionManager(Path selectionFile) {
try {
if (Files.isRegularFile(selectionFile)) {
String json = Files.readString(selectionFile);
Jsons.ClientSelectionManagerFields obj = GSON.fromJson(json, Jsons.ClientSelectionManagerFields.class);
if (obj == null) {
return new Jsons.ClientSelectionManagerFields();
}
return obj;
}
} catch (Exception e) {
LOGGER.debug("Couldn't load client selection manager file (this is normal on first startup): {}", e.getMessage());
}
return new Jsons.ClientSelectionManagerFields();
}

public static void saveClientSelectionManager(Path selectionFile, Jsons.ClientSelectionManagerFields configObject) {
try {
if (!Files.isDirectory(selectionFile.getParent())) {
Files.createDirectories(selectionFile.getParent());
}

Files.writeString(selectionFile, GSON.toJson(configObject), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (Exception e) {
LOGGER.error("Couldn't save client selection manager!");
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void normalizeServerConfig(Jsons.ServerConfigFieldsV2 config, bool
}
}

// TODO adapt yourself
public static void normalizeServerConfig(Jsons.ServerConfigFieldsV2 config) {
Set<String> fixedSyncedFiles = new HashSet<>(config.syncedFiles.size());
Set<String> fixedAllowEditsInFiles = new HashSet<>(config.allowEditsInFiles.size());
Expand Down
Loading