Skip to content

Commit

Permalink
Added ignore files support, added local file support
Browse files Browse the repository at this point in the history
  • Loading branch information
BloodWorkXGaming committed May 20, 2018
1 parent c348bd3 commit b987a87
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
15 changes: 8 additions & 7 deletions server-setup-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,25 @@ install:
baseInstallPath: setup/

# a list of files which are supposed to be ignored when installing it from the client files
# this is supposed to support regex
# this can either use regex or glob {default glob: https://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-}
# specify with regex:.... or glob:.... if you want to force a matching type
ignoreFiles:
- ./mods/optifine.*.jar
- ./resources/**
- mods/optifine*.jar
- resources/**

# often a server needs more files, which are nearly useless on the client, such as tickprofiler
# This is a list of files, each ' - ' is a new file:
# url is the directlink to the file, destination is the path to where the file should be copied to
additionalFiles:
- url: https://minecraft.curseforge.com/projects/tickprofiler/files/2536494/download
destination: ./mods/TickProfiler-1.12-0.0.4.jar
destination: mods/TickProfiler-1.12-0.0.4.jar
- url: https://minecraft.curseforge.com/projects/blooddebug/files/2514858/download
destination: ./mods/BloodDebug-1.12.2-0.0.1-9.jar
destination: mods/BloodDebug-1.12.2-0.0.1-9.jar

# For often there are config which the user wants to change, here is the place to put the local path to configs, jars or whatever
localFiles:
- from: ../custommods/discord-chat.jar
to: ./mods/discord-chat.jar
- from: test/All+the+Mods+3-v5.9.5.zip
to: setup/test/All+the+Mods+3-v5.9.5.zip

# This makes the program check the folder for whether it is supposed to use the
checkFolder: yes
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/atm/bloodworkxgaming/serverstarter/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import atm.bloodworkxgaming.serverstarter.config.AddionalFile;
import atm.bloodworkxgaming.serverstarter.config.ConfigFile;
import atm.bloodworkxgaming.serverstarter.config.LocalFile;
import org.apache.commons.io.FileUtils;

import java.io.File;
Expand Down Expand Up @@ -48,6 +49,18 @@ private void handleAdditionalFile(AddionalFile file, List<AddionalFile> fallback
}


public void installLocalFiles() {
LOGGER.info("Starting to copy local files.");
for (LocalFile localFile : configFile.install.localFiles) {
LOGGER.info("Copying localfile: " + localFile);
try {
FileUtils.copyFile(new File(localFile.from), new File(localFile.to));
} catch (IOException e) {
LOGGER.error("Error while copying local file", e);
}
}
}

public static URL cleanUrl(String url) throws URISyntaxException, MalformedURLException {
int sOrNotIndex = url.indexOf("/");
String sOrNot = url.substring(0, sOrNotIndex - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static void main(String[] args) {

FileManager filemanger = new FileManager(config);
filemanger.installAdditionalFiles();
filemanger.installLocalFiles();


} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public void error(String message, Throwable throwable) {
try {
FileUtils.write(outputFile, message, "utf-8", true);
} catch (IOException e) {
error("Error while logging!", e);
System.err.println("Error while logging!");
e.printStackTrace();
}

System.out.print(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
import org.apache.commons.io.FilenameUtils;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand All @@ -37,12 +41,14 @@ public class CursePackType implements IPackType {
private String basePath;
private String forgeVersion;
private String mcVersion;
private File oldFiles;

public CursePackType(ConfigFile configFile) {
this.configFile = configFile;
basePath = configFile.install.baseInstallPath;
forgeVersion = configFile.install.forgeVersion;
mcVersion = configFile.install.mcVersion;
oldFiles = new File(basePath + "OLD_TO_DELETE/");
}

@Override
Expand All @@ -53,7 +59,18 @@ public void installPack() {
url += "/download";

try {
unzipFile(downloadPack(url));
List<PathMatcher> patterns = configFile.install.ignoreFiles
.stream()
.map(s -> {
if (s.startsWith("glob:") || s.startsWith("regex:"))
return s;
else
return "glob:" + s;
})
.map(FileSystems.getDefault()::getPathMatcher)
.collect(Collectors.toList());

unzipFile(downloadPack(url), patterns);
// unzipFile(new File(basePath + "modpack-download.zip"));
handleManifest();

Expand Down Expand Up @@ -116,10 +133,15 @@ private File downloadPack(String url) throws IOException {
}
}

private void unzipFile(File downloadedPack) throws IOException {
private void unzipFile(File downloadedPack, List<PathMatcher> patterns) throws IOException {
// delete old installer folder
FileUtils.deleteDirectory(oldFiles);

// start with deleting the mods folder as it is not garanteed to have override mods
FileUtils.deleteDirectory(new File(basePath + "mods/"));
LOGGER.info("Deleted the mods folder");
File modsFolder = new File(basePath + "mods/");
if (modsFolder.exists())
FileUtils.moveDirectory(modsFolder, new File(oldFiles, "mods"));
LOGGER.info("Moved the mods folder");

LOGGER.info("Starting to unzip files.");
// unzip start
Expand Down Expand Up @@ -148,8 +170,18 @@ private void unzipFile(File downloadedPack) throws IOException {

// overrides
if (name.startsWith("overrides/")) {

String path = entry.getName().substring(10);
Path p = Paths.get(path);
if (patterns.stream().anyMatch(pattern -> pattern.matches(p))) {
LOGGER.info("Skipping " + path + " as it is on the ignore List.", true);

entry = zis.getNextEntry();
continue;
}

if (!name.endsWith("/")) {
File outfile = new File(basePath + entry.getName().substring(10));
File outfile = new File(basePath + path);
LOGGER.info("Copying zip entry to = " + outfile, true);
//noinspection ResultOfMethodCallIgnored
new File(outfile.getParent()).mkdirs();
Expand All @@ -162,10 +194,11 @@ private void unzipFile(File downloadedPack) throws IOException {
}

} else if (!name.equals("overrides/")) {
File newFolder = new File(basePath + entry.getName().substring(10));
FileUtils.deleteDirectory(newFolder);
File newFolder = new File(basePath + path);
if (newFolder.exists())
FileUtils.moveDirectory(newFolder, new File(oldFiles, path));

LOGGER.info("Folder deleted: " + newFolder.getAbsolutePath(), true);
LOGGER.info("Folder moved: " + newFolder.getAbsolutePath(), true);
}
}

Expand Down

0 comments on commit b987a87

Please sign in to comment.