Skip to content

Commit a3e6c5d

Browse files
committed
#82 Change file deletion to be based on server state
1 parent 270c896 commit a3e6c5d

File tree

6 files changed

+106
-69
lines changed

6 files changed

+106
-69
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
ssversion = 2.6.16
1+
ssversion = 2.6.16-alpha.2
22
ssname = serversync

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

+28-49
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import java.nio.file.Path;
55
import java.nio.file.Paths;
66
import java.util.ArrayList;
7-
import java.util.HashMap;
87
import java.util.List;
98

109
import com.superzanti.serversync.util.FileIgnoreMatcher;
1110
import com.superzanti.serversync.util.FileIncludeMatcher;
1211
import com.superzanti.serversync.util.Logger;
12+
import com.superzanti.serversync.util.MinecraftModInformation;
1313
import com.superzanti.serversync.util.PathUtils;
1414
import com.superzanti.serversync.util.Server;
1515
import com.superzanti.serversync.util.SyncFile;
@@ -82,6 +82,10 @@ private void closeWorker() {
8282
}
8383

8484
private void populateClientFiles(ArrayList<String> directories) {
85+
populateClientFiles(directories, false);
86+
}
87+
88+
private void populateClientFiles(ArrayList<String> directories, boolean ignoreRules) {
8589
List<Path> clientFilePaths = new ArrayList<>();
8690
List<Path> clientConfigPaths = PathUtils.fileListDeep(Paths.get("config/"));
8791
clientFiles = new ArrayList<SyncFile>(200);
@@ -101,7 +105,7 @@ private void populateClientFiles(ArrayList<String> directories) {
101105
FileIgnoreMatcher ignoredFiles = new FileIgnoreMatcher();
102106

103107
for (Path path : clientFilePaths) {
104-
if (ignoredFiles.matches(path)) {
108+
if (!ignoreRules && ignoredFiles.matches(path)) {
105109
Logger.log(Main.strings.getString("ignoring") + " " + path.toString());
106110
} else {
107111
clientFiles.add(SyncFile.StandardSyncFile(path));
@@ -113,7 +117,7 @@ private void populateClientFiles(ArrayList<String> directories) {
113117
FileIncludeMatcher includedFiles = new FileIncludeMatcher();
114118

115119
for (Path path : clientConfigPaths) {
116-
if (includedFiles.matches(path)) {
120+
if (ignoreRules || includedFiles.matches(path)) {
117121
clientFiles.add(SyncFile.ConfigSyncFile(path));
118122
}
119123
}
@@ -150,19 +154,10 @@ public void run() {
150154

151155
populateClientFiles(syncableDirectories);
152156

153-
Logger.log(Main.strings.getString("config_check"));
154-
155-
// TODO is this needed now? check against last updated perhaps
156-
if (!server.getConfig()) {
157-
Logger.error("Failed to obtain config from server");
158-
errorInUpdates = true;
159-
closeWorker();
160-
return;
161-
}
162-
163157
Logger.debug("Checking Server.isUpdateNeeded()");
164158
Logger.debug(clientFiles.toString());
165159
updateNeeded = server.isUpdateNeeded(clientFiles);
160+
updateNeeded = true; // TEMP
166161

167162
/* MAIN PROCESSING CHUNK */
168163
if (updateNeeded) {
@@ -245,9 +240,8 @@ public void run() {
245240
Logger.debug(e);
246241
}
247242
} else {
248-
// only need to check for ignore here as we are working
249-
// on the servers file tree
250-
if (serverFile.matchesIgnoreListPattern() && !serverFile.isClientSideOnlyFile) {
243+
// Ignore support for client only files, users may wish to not allow some mods out of personal preference
244+
if (serverFile.isClientSideOnlyFile && serverFile.matchesIgnoreListPattern()) {
251245
Logger.log("<>" + Main.strings.getString("ignoring") + " " + serverFile.getFileName());
252246
} else {
253247
Logger.debug(serverFile.getFileName() + " " + Main.strings.getString("does_not_exist"));
@@ -268,31 +262,8 @@ public void run() {
268262
Logger.debug(Main.strings.getString("ignoring") + " " + clientFile.getFileName());
269263
} else {
270264
Logger.debug(Main.strings.getString("client_check") + " " + clientFile.getFileName());
271-
272-
boolean servedByServer = false;
273-
for (SyncFile ignoredClientFile : ignoredClientSideFiles) {
274-
// Client side files provided by the server
275-
try {
276-
if (clientFile.equals(ignoredClientFile)) {
277-
servedByServer = true;
278-
break;
279-
}
280-
} catch (InvalidSyncFileException e) {
281-
//TODO stub invalid sync file handling
282-
e.printStackTrace();
283-
}
284-
}
285-
if (servedByServer) {
286-
Logger.debug(Main.strings.getString("ignoring") + " " + clientFile.getFileName());
287-
continue;
288-
}
289-
290-
boolean exists = server.modExists(clientFile);
291-
292-
if (!exists) {
293-
Logger.debug(clientFile.getFileName() + " " + Main.strings.getString("does_not_match")
294-
+ Main.strings.getString("delete_attempt"));
295-
265+
266+
if (!serverFiles.contains(clientFile)) {
296267
if (clientFile.delete()) {
297268
Logger.log(
298269
"<>" + clientFile.getFileName() + " " + Main.strings.getString("delete_success"));
@@ -301,26 +272,34 @@ public void run() {
301272
}
302273
updateHappened = true;
303274
}
275+
304276
Main.clientGUI.updateProgress((int) (currentPercent / percentScale));
305277
}
306278
}
307279

308280
//TODO complete this with user prompt to pick which duplicate to keep
309281
/* DUPLICATE CHECK */
310-
populateClientFiles(syncableDirectories);
311-
HashMap<String, SyncFile> modList = new HashMap<String, SyncFile>(200);
282+
populateClientFiles(syncableDirectories, true);
283+
ArrayList<String> modNames = new ArrayList<>(200);
284+
ArrayList<String> modHashes = new ArrayList<>(200);
312285
ArrayList<SyncFile> dupes = new ArrayList<SyncFile>(10);
313286
for (SyncFile clientFile : clientFiles) {
314-
if (clientFile.getModInformation() != null) {
315-
System.out.println(clientFile.getFileName());
316-
if (modList.get(clientFile.getModInformation().name) != null) {
317-
Logger.log("<!> Potential duplicate: " + clientFile.getFileName() + " - " + clientFile.getModInformation().name);
287+
MinecraftModInformation modInfo = clientFile.getModInformation();
288+
if (modInfo != null) {
289+
if (modNames.contains(modInfo.name)) {
290+
Logger.log("<!> Potential duplicate: " + clientFile.getFileName() + " - " + modInfo.name);
318291
dupes.add(clientFile);
319292
} else {
320-
modList.put(clientFile.getModInformation().name, clientFile);
293+
modNames.add(modInfo.name);
321294
}
322295
} else {
323-
//TODO what to do when the file has no mod information available
296+
String hash = clientFile.getFileHash();
297+
if (modHashes.contains(hash)) {
298+
Logger.log("<!> Potential duplicate: " + clientFile.getFileName() + " - " + hash);
299+
dupes.add(clientFile);
300+
} else {
301+
modHashes.add(hash);
302+
}
324303
}
325304
}
326305
System.out.println(dupes);

src/main/java/com/superzanti/serversync/util/ProgramArguments.java

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ public class ProgramArguments {
88
public final boolean isServer;
99
public final boolean syncSilent;
1010
public final boolean syncProgressOnly;
11+
public final boolean cleanup;
1112

1213
public ProgramArguments(String[] arguments) {
1314
List<String> args = Arrays.asList(arguments);
1415
this.isServer = args.contains("server");
1516
this.syncSilent = args.contains("silent");
1617
this.syncProgressOnly = args.contains("progress-only");
18+
this.cleanup = args.contains("cleanup");
1719
}
1820
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public boolean updateFile(SyncFile serverFile, SyncFile clientFile) {
446446
return false;
447447
}
448448

449-
Logger.log(Main.strings.getString("update_success") + ": " + clientFile.getFileName());
449+
Logger.log(Main.strings.getString("update_success") + ": " + clientFile.getFileAsPath().toString());
450450
return true;
451451
}
452452
}

src/main/java/com/superzanti/serversync/util/SyncFile.java

+26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public MinecraftModInformation getModInformation() {
3737
}
3838

3939
private final String fileHash;
40+
public String getFileHash() {
41+
return this.fileHash;
42+
}
4043
private final File synchronizableFile;
4144
public String getFileName() {
4245
return this.synchronizableFile.getName();
@@ -206,6 +209,20 @@ private void populateModInformation() {
206209
System.out.println("File: " + this.synchronizableFile.getName() + " not recognized as a minecraft mod (not a problem)");
207210
}
208211
}
212+
213+
@Override
214+
public boolean equals(Object o) {
215+
// Patch for using compare on lists with sync files
216+
if (o instanceof SyncFile) {
217+
try {
218+
return this.equals((SyncFile)o);
219+
} catch (InvalidSyncFileException e) {
220+
e.printStackTrace();
221+
}
222+
}
223+
224+
return super.equals(o);
225+
}
209226

210227
/**
211228
* Compares mod versions from mcmod.info or compares file contents if
@@ -223,12 +240,21 @@ public boolean equals(SyncFile otherSyncFile) throws InvalidSyncFileException {
223240
throw new InvalidSyncFileException();
224241
}
225242

243+
if (this.getFileName() == null || otherSyncFile.getFileName() == null) {
244+
System.out.println("Could not get file names");
245+
throw new InvalidSyncFileException();
246+
}
247+
226248
if (this.fileHash == null || otherSyncFile.fileHash == null) {
227249
System.out.println("File hash comparison impossible");
228250
System.out.println(this.getFileName() + " : " + otherSyncFile.getFileName());
229251
throw new InvalidSyncFileException();
230252
}
231253

254+
if (!this.getFileName().equals(otherSyncFile.getFileName())) {
255+
return false;
256+
}
257+
232258
if (otherSyncFile.minecraftInformation != null && this.minecraftInformation != null) {
233259
return this.minecraftInformation.version.equals(otherSyncFile.minecraftInformation.version)
234260
&& this.minecraftInformation.name.equals(otherSyncFile.minecraftInformation.name);

src/main/java/runme/Main.java

+48-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package runme;
22

33
import java.io.IOException;
4+
import java.nio.file.FileVisitResult;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.nio.file.SimpleFileVisitor;
9+
import java.nio.file.attribute.BasicFileAttributes;
410
import java.util.Locale;
511
import java.util.MissingResourceException;
612
import java.util.ResourceBundle;
@@ -16,69 +22,93 @@
1622
import com.superzanti.serversync.util.ProgramArguments;
1723
import com.superzanti.serversync.util.enums.EConfigType;
1824

19-
2025
public class Main {
21-
26+
2227
/* AWT EVENT DISPATCHER THREAD */
23-
28+
2429
public static final String APPLICATION_TITLE = "Serversync";
2530
public static final String HANDSHAKE = "HANDSHAKE";
26-
31+
2732
public static GUI_Client clientGUI;
2833
public static GUI_Server serverGUI;
29-
34+
3035
public static ResourceBundle strings;
31-
36+
3237
public static SyncConfig CONFIG;
33-
38+
3439
public static ProgramArguments arguments;
35-
36-
40+
3741
public static void main(String[] args) throws InterruptedException, IOException {
3842
arguments = new ProgramArguments(args);
3943
CONFIG = new SyncConfig(EConfigType.COMMON);
40-
44+
4145
try {
4246
// TODO left off here, fix locale use and other main references
4347
System.out.println("Loading language file: " + CONFIG.LOCALE);
4448
strings = ResourceBundle.getBundle("assets.serversync.MessagesBundle", CONFIG.LOCALE);
4549
} catch (MissingResourceException e) {
46-
SwingUtilities.invokeLater(new Runnable() {
50+
SwingUtilities.invokeLater(new Runnable() {
4751
@Override
4852
public void run() {
4953
System.out.println("No language file available for: " + CONFIG.LOCALE + ", defaulting to en_US");
5054
}
5155
});
5256
strings = ResourceBundle.getBundle("assets.serversync.lang.MessagesBundle", new Locale("en", "US"));
5357
}
54-
58+
5559
if (arguments.isServer) {
5660
runInServerMode();
5761
} else {
5862
runInClientMode();
5963
}
64+
65+
// Only for testing, this cleans up old sync files
66+
if (arguments.cleanup) {
67+
System.out.println("cleaning up test files");
68+
System.out.println(Paths.get("").toAbsolutePath().toString());
69+
try {
70+
Path modsDir = Paths.get("mods");
71+
if (Files.exists(modsDir)) {
72+
Files.walkFileTree(modsDir, new SimpleFileVisitor<Path>() {
73+
@Override
74+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
75+
Files.delete(file);
76+
return FileVisitResult.CONTINUE;
77+
}
78+
79+
@Override
80+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
81+
Files.delete(dir);
82+
return FileVisitResult.CONTINUE;
83+
}
84+
});
85+
}
86+
} catch (Exception e) {
87+
e.printStackTrace();
88+
}
89+
}
6090
}
61-
91+
6292
private static void runInServerMode() {
6393
CONFIG = new SyncConfig(EConfigType.SERVER);
6494
ServerSetup setup = new ServerSetup();
6595
Thread serverThread = new Thread(setup);
6696
serverThread.start();
6797
}
68-
98+
6999
private static void runInClientMode() {
70100
CONFIG = new SyncConfig(EConfigType.CLIENT);
71101
Thread clientThread;
72-
if (arguments.syncSilent) {
102+
if (arguments.syncSilent) {
73103
clientGUI = new GUI_Client_Mock();
74104
new Thread(new ClientWorker()).start();
75105
} else if (arguments.syncProgressOnly) {
76-
//TODO setup a progress only version of the GUI
106+
// TODO setup a progress only version of the GUI
77107
clientGUI = new GUI_Client();
78108
clientGUI.setIPAddress(CONFIG.SERVER_IP);
79109
clientGUI.setPort(CONFIG.SERVER_PORT);
80110
clientGUI.build(CONFIG.LOCALE);
81-
111+
82112
clientThread = new Thread(new ClientWorker());
83113
clientThread.start();
84114
try {
@@ -89,7 +119,7 @@ private static void runInClientMode() {
89119
System.exit(1);
90120
}
91121
System.exit(0);
92-
} else {
122+
} else {
93123
clientGUI = new GUI_Client();
94124
clientGUI.setIPAddress(CONFIG.SERVER_IP);
95125
clientGUI.setPort(CONFIG.SERVER_PORT);

0 commit comments

Comments
 (0)