Skip to content

Commit

Permalink
Merge pull request #178 from rheimus/master
Browse files Browse the repository at this point in the history
Fix #175 & #177
  • Loading branch information
rheimus authored Apr 7, 2020
2 parents 637c854 + 56dcd21 commit b4fd594
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 333 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/superzanti/serversync/ServerSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public static void main(String[] args) {
private static void commonInit() {
Locale locale = SyncConfig.getConfig().LOCALE;
try {
System.out.println("Loading language file: " + locale);
strings = ResourceBundle.getBundle("assets.serversync.MessagesBundle", locale);
Logger.log("Loading language file: " + locale);
strings = ResourceBundle.getBundle("assets.serversync.lang.MessagesBundle", locale);
} catch (MissingResourceException e) {
System.out.println("No language file available for: " + locale + ", defaulting to en_US");
Logger.log("No language file available for: " + locale + ", defaulting to en_US");
strings = ResourceBundle.getBundle("assets.serversync.lang.MessagesBundle", new Locale("en", "US"));
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/superzanti/serversync/SyncConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,13 @@ private void writeConfig() {
private void init() {
String couldNotFindString = "Could not find %s config entry";
try {
LOCALE = new Locale(config.getEntryByName("LOCALE").getString());
String localeString = config.getEntryByName("LOCALE").getString();
String[] localeParts = localeString.split("_");
if (localeParts.length != 2) {
Logger.error("Malformed locale string!");
localeParts = new String[]{"en", "US"};
}
LOCALE = new Locale(localeParts[0], localeParts[1]);
} catch (NullPointerException e) {
Logger.debug(String.format(couldNotFindString, "LOCALE"));
isUsingIncompatableConfig = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.superzanti.serversync.filemanager;

import com.superzanti.serversync.config.IgnoredFilesMatcher;
import com.superzanti.serversync.server.Function;
import com.superzanti.serversync.util.FileHash;
import com.superzanti.serversync.util.Logger;
import com.superzanti.serversync.util.PathBuilder;
import com.superzanti.serversync.util.PathUtils;
import com.superzanti.serversync.util.*;

import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FileManager {
public static final String clientOnlyFilesDirectoryName = "clientmods";
Expand Down Expand Up @@ -62,22 +60,34 @@ public Map<String, String> getDiffableFilesFromDirectories(List<String> included
dirs.add(dir);
}

List<Path> allFiles = dirs.stream().flatMap(dir -> {
try {
return Files.walk(dir).filter(dirPath -> !Files.isDirectory(dirPath));
} catch (IOException e) {
Logger.debug(String.format("Failed to access files in the directory: %s", dir));
Logger.debug(e);
}
return null;
}).collect(Collectors.toList());

if (allFiles.stream().anyMatch(Objects::isNull)) {
throw new IOException("Some files could not be accessed");
}

//TODO add file size to this map
return allFiles.stream().collect(Collectors.toMap(Path::toString, FileHash::hashFile));
List<Path> allFiles = dirs
.parallelStream()
.flatMap(dir -> {
try {
return Files.walk(dir).filter(dirPath -> !Files.isDirectory(dirPath));
} catch (IOException e) {
Logger.debug(String.format("Failed to access files in the directory: %s", dir));
Logger.debug(e);
}
return Stream.empty();
}).collect(Collectors.toList());
Logger.debug(String.format("All files: %s", PrettyCollection.get(allFiles)));

List<Path> ignoredFiles = allFiles
.parallelStream()
.filter(IgnoredFilesMatcher::matches)
.collect(Collectors.toList());
Logger.debug(String.format("Ignored files: %s", PrettyCollection.get(ignoredFiles)));

List<Path> filteredFiles = allFiles
.parallelStream()
.filter(f -> !IgnoredFilesMatcher.matches(f))
.collect(Collectors.toList());
Logger.debug(String.format("Filtered files: %s", PrettyCollection.get(filteredFiles)));

return filteredFiles.stream()
.filter(Files::exists)
.collect(Collectors.toConcurrentMap(Path::toString, FileHash::hashFile));
}

public static void removeEmptyDirectories(List<Path> directories, Function<Path> emptyDirectoryCallback) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/superzanti/serversync/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ public Map<String, EFileProccessingStatus> syncFiles(VoidFunction afterEachFile)
// Server: Do you have this file?
String path = ois.readUTF();
String hash = ois.readUTF();


Logger.debug(String.format("Processing file: %s, with hash: %s", path, hash));

if (isClientOnlyFile(path)) {
if (config.REFUSE_CLIENT_MODS) {
Expand Down
68 changes: 33 additions & 35 deletions src/main/java/com/superzanti/serversync/server/ServerSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,51 +55,47 @@ public ServerSetup() {

try {
Logger.log("Starting scan for managed files: " + dateFormatter.format(new Date()));
Logger.debug(String.format("Ignore patterns: %s", String.join(", ", config.FILE_IGNORE_LIST)));
Logger.log(String.format("Ignore patterns: %s", PrettyCollection.get(config.FILE_IGNORE_LIST)));

for (String managedDirectory : managedDirectories) {
Files.createDirectories(Paths.get(managedDirectory));
}

Map<String, String> managedFiles = fileManager
.getDiffableFilesFromDirectories(managedDirectories);

//TODO add file include list for white / black list matching combos
// Glob matching from user configured patterns
Map<String, String> filteredFiles = managedFiles
.entrySet()
.stream()
.filter(entry -> {
Path file = Paths.get(entry.getKey());
return !GlobPathMatcher
.matches(file, config.FILE_IGNORE_LIST);
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Map<String, String> managedFiles = fileManager.getDiffableFilesFromDirectories(managedDirectories);

Logger.log(String.format(
"Found %d files in %d directories <%s>",
managedFiles.size(),
managedDirectories.size(),
String.join(", ", managedDirectories)
));
Logger.debug("unfiltered: " + managedFiles.toString());
Logger.debug("filtered: " + filteredFiles.toString());
serverFiles.putAll(filteredFiles);

// Add config include files
Map<String, String> configIncludeFiles = config.CONFIG_INCLUDE_LIST
.stream()
.parallel()
.map(p -> new PathBuilder("config").add(p).buildPath())
.filter(path -> Files.exists(path) && !IgnoredFilesMatcher.matches(path))
.collect(Collectors.toMap(Path::toString, FileHash::hashFile));
if (managedFiles.size() > 0) {
serverFiles.putAll(managedFiles);
Logger.log(String.format("Managed files: %s", PrettyCollection.get(managedFiles)));
}

Logger.log(String.format(
"Found %d included configs in <config>",
configIncludeFiles.size()
));
Logger.debug("files: " + String.join(",", configIncludeFiles.keySet()));
serverFiles.putAll(configIncludeFiles);
// Only include configs if some are actually listed
// saves wasting time scanning the config directory.
if (config.CONFIG_INCLUDE_LIST.size() > 0) {
Logger.log(String.format("Starting scan for managed configs: %s", dateFormatter.format(new Date())));
Logger.log(String.format("Include patterns: %s", PrettyCollection.get(config.CONFIG_INCLUDE_LIST)));
// Add config include files
Map<String, String> configIncludeFiles = config.CONFIG_INCLUDE_LIST
.stream()
.parallel()
.map(p -> new PathBuilder("config").add(p).buildPath())
.filter(path -> Files.exists(path) && !IgnoredFilesMatcher.matches(path))
.collect(Collectors.toConcurrentMap(Path::toString, FileHash::hashFile));

Logger.log(String.format(
"Found %d included configs in <config>",
configIncludeFiles.size()
));
if (configIncludeFiles.size() > 0) {
Logger.log(String.format("Config files: %s", PrettyCollection.get(configIncludeFiles)));
serverFiles.putAll(configIncludeFiles);
}
}

if (shouldPushClientOnlyFiles()) {
Logger.log("Server configured to push client only mods, clients can still refuse these mods!");
Expand All @@ -118,8 +114,10 @@ public ServerSetup() {
clientOnlyFiles.size(),
FileManager.clientOnlyFilesDirectoryName
));
Logger.debug(clientOnlyFiles.toString());
serverFiles.putAll(clientOnlyFiles);
if (clientOnlyFiles.size() > 0) {
Logger.log(String.format("Client only files: %s", PrettyCollection.get(clientOnlyFiles)));
serverFiles.putAll(clientOnlyFiles);
}
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -164,7 +162,7 @@ public void run() {
clientThread.start();
} catch (IOException e) {
Logger.error(
"Error while accepting client connection, breaking server listener. You will need to restart serversync");
"Error while accepting client connection, breaking server listener. You will need to restart ServerSync");
try {
server.close();
} catch (IOException ex) {
Expand Down
Loading

0 comments on commit b4fd594

Please sign in to comment.