Skip to content

Commit b4fd594

Browse files
authored
Merge pull request #178 from rheimus/master
Fix #175 & #177
2 parents 637c854 + 56dcd21 commit b4fd594

14 files changed

+334
-333
lines changed

src/main/java/com/superzanti/serversync/ServerSync.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public static void main(String[] args) {
3939
private static void commonInit() {
4040
Locale locale = SyncConfig.getConfig().LOCALE;
4141
try {
42-
System.out.println("Loading language file: " + locale);
43-
strings = ResourceBundle.getBundle("assets.serversync.MessagesBundle", locale);
42+
Logger.log("Loading language file: " + locale);
43+
strings = ResourceBundle.getBundle("assets.serversync.lang.MessagesBundle", locale);
4444
} catch (MissingResourceException e) {
45-
System.out.println("No language file available for: " + locale + ", defaulting to en_US");
45+
Logger.log("No language file available for: " + locale + ", defaulting to en_US");
4646
strings = ResourceBundle.getBundle("assets.serversync.lang.MessagesBundle", new Locale("en", "US"));
4747
}
4848
}

src/main/java/com/superzanti/serversync/SyncConfig.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,13 @@ private void writeConfig() {
272272
private void init() {
273273
String couldNotFindString = "Could not find %s config entry";
274274
try {
275-
LOCALE = new Locale(config.getEntryByName("LOCALE").getString());
275+
String localeString = config.getEntryByName("LOCALE").getString();
276+
String[] localeParts = localeString.split("_");
277+
if (localeParts.length != 2) {
278+
Logger.error("Malformed locale string!");
279+
localeParts = new String[]{"en", "US"};
280+
}
281+
LOCALE = new Locale(localeParts[0], localeParts[1]);
276282
} catch (NullPointerException e) {
277283
Logger.debug(String.format(couldNotFindString, "LOCALE"));
278284
isUsingIncompatableConfig = true;

src/main/java/com/superzanti/serversync/filemanager/FileManager.java

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.superzanti.serversync.filemanager;
22

3+
import com.superzanti.serversync.config.IgnoredFilesMatcher;
34
import com.superzanti.serversync.server.Function;
4-
import com.superzanti.serversync.util.FileHash;
5-
import com.superzanti.serversync.util.Logger;
6-
import com.superzanti.serversync.util.PathBuilder;
7-
import com.superzanti.serversync.util.PathUtils;
5+
import com.superzanti.serversync.util.*;
86

97
import java.io.IOException;
108
import java.nio.file.*;
119
import java.util.ArrayList;
1210
import java.util.List;
1311
import java.util.Map;
14-
import java.util.Objects;
1512
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
1614

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

65-
List<Path> allFiles = dirs.stream().flatMap(dir -> {
66-
try {
67-
return Files.walk(dir).filter(dirPath -> !Files.isDirectory(dirPath));
68-
} catch (IOException e) {
69-
Logger.debug(String.format("Failed to access files in the directory: %s", dir));
70-
Logger.debug(e);
71-
}
72-
return null;
73-
}).collect(Collectors.toList());
74-
75-
if (allFiles.stream().anyMatch(Objects::isNull)) {
76-
throw new IOException("Some files could not be accessed");
77-
}
78-
79-
//TODO add file size to this map
80-
return allFiles.stream().collect(Collectors.toMap(Path::toString, FileHash::hashFile));
63+
List<Path> allFiles = dirs
64+
.parallelStream()
65+
.flatMap(dir -> {
66+
try {
67+
return Files.walk(dir).filter(dirPath -> !Files.isDirectory(dirPath));
68+
} catch (IOException e) {
69+
Logger.debug(String.format("Failed to access files in the directory: %s", dir));
70+
Logger.debug(e);
71+
}
72+
return Stream.empty();
73+
}).collect(Collectors.toList());
74+
Logger.debug(String.format("All files: %s", PrettyCollection.get(allFiles)));
75+
76+
List<Path> ignoredFiles = allFiles
77+
.parallelStream()
78+
.filter(IgnoredFilesMatcher::matches)
79+
.collect(Collectors.toList());
80+
Logger.debug(String.format("Ignored files: %s", PrettyCollection.get(ignoredFiles)));
81+
82+
List<Path> filteredFiles = allFiles
83+
.parallelStream()
84+
.filter(f -> !IgnoredFilesMatcher.matches(f))
85+
.collect(Collectors.toList());
86+
Logger.debug(String.format("Filtered files: %s", PrettyCollection.get(filteredFiles)));
87+
88+
return filteredFiles.stream()
89+
.filter(Files::exists)
90+
.collect(Collectors.toConcurrentMap(Path::toString, FileHash::hashFile));
8191
}
8292

8393
public static void removeEmptyDirectories(List<Path> directories, Function<Path> emptyDirectoryCallback) {

src/main/java/com/superzanti/serversync/server/Server.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ public Map<String, EFileProccessingStatus> syncFiles(VoidFunction afterEachFile)
156156
// Server: Do you have this file?
157157
String path = ois.readUTF();
158158
String hash = ois.readUTF();
159-
160-
159+
Logger.debug(String.format("Processing file: %s, with hash: %s", path, hash));
161160

162161
if (isClientOnlyFile(path)) {
163162
if (config.REFUSE_CLIENT_MODS) {

src/main/java/com/superzanti/serversync/server/ServerSetup.java

+33-35
Original file line numberDiff line numberDiff line change
@@ -55,51 +55,47 @@ public ServerSetup() {
5555

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

6060
for (String managedDirectory : managedDirectories) {
6161
Files.createDirectories(Paths.get(managedDirectory));
6262
}
6363

64-
Map<String, String> managedFiles = fileManager
65-
.getDiffableFilesFromDirectories(managedDirectories);
66-
67-
//TODO add file include list for white / black list matching combos
68-
// Glob matching from user configured patterns
69-
Map<String, String> filteredFiles = managedFiles
70-
.entrySet()
71-
.stream()
72-
.filter(entry -> {
73-
Path file = Paths.get(entry.getKey());
74-
return !GlobPathMatcher
75-
.matches(file, config.FILE_IGNORE_LIST);
76-
})
77-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
64+
Map<String, String> managedFiles = fileManager.getDiffableFilesFromDirectories(managedDirectories);
7865

7966
Logger.log(String.format(
8067
"Found %d files in %d directories <%s>",
8168
managedFiles.size(),
8269
managedDirectories.size(),
8370
String.join(", ", managedDirectories)
8471
));
85-
Logger.debug("unfiltered: " + managedFiles.toString());
86-
Logger.debug("filtered: " + filteredFiles.toString());
87-
serverFiles.putAll(filteredFiles);
88-
89-
// Add config include files
90-
Map<String, String> configIncludeFiles = config.CONFIG_INCLUDE_LIST
91-
.stream()
92-
.parallel()
93-
.map(p -> new PathBuilder("config").add(p).buildPath())
94-
.filter(path -> Files.exists(path) && !IgnoredFilesMatcher.matches(path))
95-
.collect(Collectors.toMap(Path::toString, FileHash::hashFile));
72+
if (managedFiles.size() > 0) {
73+
serverFiles.putAll(managedFiles);
74+
Logger.log(String.format("Managed files: %s", PrettyCollection.get(managedFiles)));
75+
}
9676

97-
Logger.log(String.format(
98-
"Found %d included configs in <config>",
99-
configIncludeFiles.size()
100-
));
101-
Logger.debug("files: " + String.join(",", configIncludeFiles.keySet()));
102-
serverFiles.putAll(configIncludeFiles);
77+
// Only include configs if some are actually listed
78+
// saves wasting time scanning the config directory.
79+
if (config.CONFIG_INCLUDE_LIST.size() > 0) {
80+
Logger.log(String.format("Starting scan for managed configs: %s", dateFormatter.format(new Date())));
81+
Logger.log(String.format("Include patterns: %s", PrettyCollection.get(config.CONFIG_INCLUDE_LIST)));
82+
// Add config include files
83+
Map<String, String> configIncludeFiles = config.CONFIG_INCLUDE_LIST
84+
.stream()
85+
.parallel()
86+
.map(p -> new PathBuilder("config").add(p).buildPath())
87+
.filter(path -> Files.exists(path) && !IgnoredFilesMatcher.matches(path))
88+
.collect(Collectors.toConcurrentMap(Path::toString, FileHash::hashFile));
89+
90+
Logger.log(String.format(
91+
"Found %d included configs in <config>",
92+
configIncludeFiles.size()
93+
));
94+
if (configIncludeFiles.size() > 0) {
95+
Logger.log(String.format("Config files: %s", PrettyCollection.get(configIncludeFiles)));
96+
serverFiles.putAll(configIncludeFiles);
97+
}
98+
}
10399

104100
if (shouldPushClientOnlyFiles()) {
105101
Logger.log("Server configured to push client only mods, clients can still refuse these mods!");
@@ -118,8 +114,10 @@ public ServerSetup() {
118114
clientOnlyFiles.size(),
119115
FileManager.clientOnlyFilesDirectoryName
120116
));
121-
Logger.debug(clientOnlyFiles.toString());
122-
serverFiles.putAll(clientOnlyFiles);
117+
if (clientOnlyFiles.size() > 0) {
118+
Logger.log(String.format("Client only files: %s", PrettyCollection.get(clientOnlyFiles)));
119+
serverFiles.putAll(clientOnlyFiles);
120+
}
123121
}
124122
}
125123
} catch (IOException e) {
@@ -164,7 +162,7 @@ public void run() {
164162
clientThread.start();
165163
} catch (IOException e) {
166164
Logger.error(
167-
"Error while accepting client connection, breaking server listener. You will need to restart serversync");
165+
"Error while accepting client connection, breaking server listener. You will need to restart ServerSync");
168166
try {
169167
server.close();
170168
} catch (IOException ex) {

0 commit comments

Comments
 (0)