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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GlobalVariables {
public static Path MODS_DIR;
public static ModpackExecutor modpackExecutor;
public static NettyServer hostServer;
public static Jsons.ServerConfigFieldsV2 serverConfig;
public static Jsons.ServerConfigFieldsV3 serverConfig;
public static Jsons.ClientConfigFieldsV2 clientConfig;
public static Jsons.KnownHostsFields knownHosts;
public static final Path automodpackDir = Path.of("automodpack");
Expand All @@ -34,8 +34,8 @@ public class GlobalVariables {
// 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 final Path mainHostContentModpackDir = hostModpackDir.resolve("main");
public static final Path hostModpackContentFile = hostModpackDir.resolve("automodpack-content.json");
public static Path serverConfigFile = automodpackDir.resolve("automodpack-server.json");
public static Path serverCoreConfigFile = automodpackDir.resolve("automodpack-core.json");
public static final Path privateDir = automodpackDir.resolve(".private");
Expand Down
23 changes: 10 additions & 13 deletions core/src/main/java/pl/skidam/automodpack_core/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
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.protocol.netty.NettyServer;

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

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

Expand All @@ -31,13 +29,12 @@ public static void main(String[] args) {

modpackDir.toFile().mkdirs();

hostModpackContentFile = modpackDir.resolve("automodpack-content.json");
// hostModpackContentFile = modpackDir.resolve("automodpack-content.json");
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 ArrayList<>();
serverConfig.validateSecrets = false;
ConfigTools.save(serverConfigFile, serverConfig);

Expand All @@ -60,14 +57,14 @@ 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);

if (generated) {
LOGGER.info("Modpack generated!");
} else {
LOGGER.error("Failed to generate modpack!");
}
// ModpackContent modpackContent = new ModpackContent(serverConfig.modpackName, null, mainModpackDir, serverConfig.syncedFiles, serverConfig.allowEditsInFiles, serverConfig.forceCopyFilesToStandardLocation, modpackExecutor.getExecutor());
// boolean generated = modpackExecutor.generateNew(modpackContent);

// if (generated) {
// LOGGER.info("Modpack generated!");
// } else {
// LOGGER.error("Failed to generate modpack!");
// }

modpackExecutor.stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ public static void save(Path configFile, Object configObject) {


// Modpack content stuff
public static Jsons.ModpackContentFields loadModpackContent(Path modpackContentFile) {
public static Jsons.ModpackGroupFields 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.ModpackGroupFields.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.ModpackGroupFields configObject) {
try {
if (!Files.isDirectory(modpackContentFile.getParent())) {
Files.createDirectories(modpackContentFile.getParent());
Expand Down
68 changes: 64 additions & 4 deletions core/src/main/java/pl/skidam/automodpack_core/config/Jsons.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,54 @@ public static class ServerConfigFieldsV2 {
public List<String> acceptedLoaders;
}

public static class GroupDeclaration {
public String groupName = "";
public boolean generateModpackOnStart = true;
public List<String> syncedFiles = List.of();
public List<String> allowEditsInFiles = List.of();
public List<String> forceCopyFilesToStandardLocation = List.of();
public boolean autoExcludeServerSideMods = true;
public boolean autoExcludeUnnecessaryFiles = true;
public boolean required = false;
public boolean checkByDefault = false;
public List<String> breaksWith = List.of();
public List<String> requiredBy = List.of();
public List<String> compatibleOS = List.of();
}

public static GroupDeclaration mainGroupDeclaration() {
GroupDeclaration decl = new GroupDeclaration();
decl.required = true;
decl.checkByDefault = true;
decl.syncedFiles = List.of("/mods/*.jar", "/kubejs/**", "!/kubejs/server_scripts/**", "/emotes/*");
decl.allowEditsInFiles = List.of("/options.txt", "/config/**");
return decl;
}

public static class ServerConfigFieldsV3 {
public int DO_NOT_CHANGE_IT = 3; // file version
public boolean modpackHost = true;
public Map<String, GroupDeclaration> groups = Map.of(
"main", mainGroupDeclaration()
);
public boolean requireAutoModpackOnClient = true;
public boolean nagUnModdedClients = true;
public String nagMessage = "This server provides dedicated modpack through AutoModpack!";
public String nagClickableMessage = "Click here to get the AutoModpack!";
public String nagClickableLink = "https://modrinth.com/project/automodpack";
public String bindAddress = "";
public int bindPort = -1;
public String addressToSend = "";
public int portToSend = -1;
public boolean disableInternalTLS = false;
public boolean updateIpsOnEveryStart = false;
public int bandwidthLimit = 0;
public boolean validateSecrets = true;
public long secretLifetime = 336; // 336 hours = 14 days
public boolean selfUpdater = false;
public List<String> acceptedLoaders;
}

public static class ServerCoreConfigFields {
public String automodpackVersion = "4.0.0-beta37"; // TODO: dont hardcode it
public String loader = "fabric";
Expand All @@ -127,19 +175,31 @@ public static class KnownHostsFields {
public Map<String, String> hosts; // host, fingerprint
}

public static class ModpackContentFields {
public String modpackName = "";
public static class ModpackContentMasterFields {
public String automodpackVersion = "";
public String loader = "";
public String loaderVersion = "";
public String mcVersion = "";
public Set<ModpackGroupFields> groups;

public ModpackContentMasterFields(Set<ModpackGroupFields> groups) {
this.groups = groups;
}

public ModpackContentMasterFields() {
this.groups = Set.of();
}
}

public static class ModpackGroupFields {
public String groupName = "";
public Set<ModpackContentItem> list;

public ModpackContentFields(Set<ModpackContentItem> list) {
public ModpackGroupFields(Set<ModpackContentItem> list) {
this.list = list;
}

public ModpackContentFields() {
public ModpackGroupFields() {
this.list = Set.of();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
import static pl.skidam.automodpack_core.GlobalVariables.LOGGER;

public class ModpackContent {
public final Set<Jsons.ModpackContentFields.ModpackContentItem> list = Collections.synchronizedSet(new HashSet<>());
public final Set<Jsons.ModpackGroupFields.ModpackContentItem> list = Collections.synchronizedSet(new HashSet<>());
public final ObservableMap<String, Path> pathsMap = new ObservableMap<>();
private final String MODPACK_NAME;
private final WildCards SYNCED_FILES_CARDS;
private final WildCards EDITABLE_CARDS;
private final WildCards FORCE_COPY_FILES_TO_STANDARD_LOCATION;
private final boolean AUTO_EXCLUDE_UNNECESSARY_FILES;
private final boolean AUTO_EXCLUDE_SERVER_SIDE_MODS;
private final Path MODPACK_DIR;
private final ThreadPoolExecutor CREATION_EXECUTOR;
private final Map<String, String> sha1MurmurMapPreviousContent = new HashMap<>();

public ModpackContent(String modpackName, Path cwd, Path modpackDir, List<String> syncedFiles, List<String> allowEditsInFiles, List<String> forceCopyFilesToStandardLocation, ThreadPoolExecutor CREATION_EXECUTOR) {
public ModpackContent(String modpackName, Path cwd, Path modpackDir, List<String> syncedFiles, List<String> allowEditsInFiles, List<String> forceCopyFilesToStandardLocation, boolean autoExcludeUnnecessaryFiles, boolean autoExcludeServerSideMods, ThreadPoolExecutor CREATION_EXECUTOR) {
this.MODPACK_NAME = modpackName;
this.MODPACK_DIR = modpackDir;
Set<Path> directoriesToSearch = new HashSet<>(2);
Expand All @@ -38,13 +40,19 @@ public ModpackContent(String modpackName, Path cwd, Path modpackDir, List<String
}
this.EDITABLE_CARDS = new WildCards(allowEditsInFiles, directoriesToSearch);
this.FORCE_COPY_FILES_TO_STANDARD_LOCATION = new WildCards(forceCopyFilesToStandardLocation, directoriesToSearch);
this.AUTO_EXCLUDE_UNNECESSARY_FILES = autoExcludeUnnecessaryFiles;
this.AUTO_EXCLUDE_SERVER_SIDE_MODS = autoExcludeServerSideMods;
this.CREATION_EXECUTOR = CREATION_EXECUTOR;
}

public String getModpackName() {
return MODPACK_NAME;
}

public Path getModpackContentFile() {
return MODPACK_DIR.resolve(hostModpackContentFile.getFileName().toString());
}

public boolean create() {
try {
pathsMap.clear();
Expand Down Expand Up @@ -94,7 +102,7 @@ public boolean create() {
return true;
}

public Optional<Jsons.ModpackContentFields> getPreviousContent() {
public Optional<Jsons.ModpackGroupFields> getPreviousContent() {
var optionalModpackContentFile = ModpackContentTools.getModpackContentFile(MODPACK_DIR);
return optionalModpackContentFile.map(ConfigTools::loadModpackContent);
}
Expand All @@ -103,12 +111,12 @@ public Optional<Jsons.ModpackContentFields> getPreviousContent() {
public boolean loadPreviousContent() {
var optionalModpackContent = getPreviousContent();
if (optionalModpackContent.isEmpty()) return false;
Jsons.ModpackContentFields modpackContent = optionalModpackContent.get();
Jsons.ModpackGroupFields modpackContent = optionalModpackContent.get();

synchronized (list) {
list.addAll(modpackContent.list);

for (Jsons.ModpackContentFields.ModpackContentItem modpackContentItem : list) {
for (Jsons.ModpackGroupFields.ModpackContentItem modpackContentItem : list) {
Path file = CustomFileUtils.getPath(MODPACK_DIR, modpackContentItem.file);
if (!Files.exists(file)) file = CustomFileUtils.getPathFromCWD(modpackContentItem.file);
if (!Files.exists(file)) {
Expand All @@ -133,15 +141,24 @@ public boolean loadPreviousContent() {
// This is important to make it synchronized otherwise it could corrupt the file and crash
public synchronized void saveModpackContent() {
synchronized (list) {
Jsons.ModpackContentFields modpackContent = new Jsons.ModpackContentFields(list);
Jsons.ModpackGroupFields modpackContent = new Jsons.ModpackGroupFields(list);
modpackContent.groupName = MODPACK_NAME;
ConfigTools.saveModpackContent(getModpackContentFile(), modpackContent);

synchronized (hostModpackContentFile) {
Jsons.ModpackContentMasterFields masterContent = ConfigTools.load(hostModpackContentFile, Jsons.ModpackContentMasterFields.class);
if (masterContent == null) {
masterContent = new Jsons.ModpackContentMasterFields();
}

modpackContent.automodpackVersion = AM_VERSION;
modpackContent.mcVersion = MC_VERSION;
modpackContent.loaderVersion = LOADER_VERSION;
modpackContent.loader = LOADER;
modpackContent.modpackName = MODPACK_NAME;
masterContent.automodpackVersion = AM_VERSION;
masterContent.mcVersion = MC_VERSION;
masterContent.loaderVersion = LOADER_VERSION;
masterContent.loader = LOADER;

ConfigTools.saveModpackContent(hostModpackContentFile, modpackContent);
masterContent.groups.removeIf(group -> group.groupName.equals(MODPACK_NAME));
masterContent.groups.add(modpackContent);
}
}
}

Expand All @@ -158,7 +175,7 @@ private List<CompletableFuture<Void>> generateAsync(List<Path> files) {

private void generate(Path file) {
try {
Jsons.ModpackContentFields.ModpackContentItem item = generateContent(file);
Jsons.ModpackGroupFields.ModpackContentItem item = generateContent(file);
if (item != null) {
LOGGER.info("generated content for {}", item.file);
synchronized (list) {
Expand All @@ -185,7 +202,7 @@ public void remove(Path file) {
String modpackFile = CustomFileUtils.formatPath(file, MODPACK_DIR);

synchronized (list) {
for (Jsons.ModpackContentFields.ModpackContentItem item : this.list) {
for (Jsons.ModpackGroupFields.ModpackContentItem item : this.list) {
if (item.file.equals(modpackFile)) {
this.pathsMap.remove(item.sha1);
this.list.remove(item);
Expand All @@ -208,14 +225,9 @@ public static boolean isInnerFile(Path file) {
return isInner;
}

private Jsons.ModpackContentFields.ModpackContentItem generateContent(final Path file) throws Exception {
private Jsons.ModpackGroupFields.ModpackContentItem generateContent(final Path file) throws Exception {
if (!Files.isRegularFile(file)) return null;

if (serverConfig == null) {
LOGGER.error("Server config is null!");
return null;
}

if (isInnerFile(file)) {
return null;
}
Expand All @@ -229,7 +241,7 @@ private Jsons.ModpackContentFields.ModpackContentItem generateContent(final Path

final String size = String.valueOf(Files.size(file));

if (serverConfig.autoExcludeUnnecessaryFiles) {
if (AUTO_EXCLUDE_UNNECESSARY_FILES) {
if (size.equals("0")) {
LOGGER.info("Skipping file {} because it is empty", formattedFile);
return null;
Expand Down Expand Up @@ -260,7 +272,7 @@ private Jsons.ModpackContentFields.ModpackContentItem generateContent(final Path

if (FileInspection.isMod(file)) {
type = "mod";
if (serverConfig.autoExcludeServerSideMods && Objects.equals(FileInspection.getModEnvironment(file), LoaderManagerService.EnvironmentType.SERVER)) {
if (AUTO_EXCLUDE_SERVER_SIDE_MODS && Objects.equals(FileInspection.getModEnvironment(file), LoaderManagerService.EnvironmentType.SERVER)) {
LOGGER.info("File {} is server mod! Skipping...", formattedFile);
return null;
}
Expand Down Expand Up @@ -305,7 +317,7 @@ private Jsons.ModpackContentFields.ModpackContentItem generateContent(final Path
LOGGER.info("File {} is forced to copy to standard location!", formattedFile);
}

return new Jsons.ModpackContentFields.ModpackContentItem(formattedFile, size, type, isEditable, forcedToCopy, sha1, murmur);
return new Jsons.ModpackGroupFields.ModpackContentItem(formattedFile, size, type, isEditable, forcedToCopy, sha1, murmur);

}
}
Loading
Loading