-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseModDiscoverer.java
More file actions
99 lines (82 loc) · 4.08 KB
/
BaseModDiscoverer.java
File metadata and controls
99 lines (82 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package io.github.fabriccompatibiltylayers.modremappingapi.impl.discover;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.ModCandidate;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingsRegistry;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
import org.spongepowered.include.com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
public abstract class BaseModDiscoverer {
public abstract boolean isValidFileName(String fileName);
public abstract boolean allowDirectoryMods();
public abstract boolean searchRecursively();
public abstract boolean isValidDirectoryName(String directoryName);
public abstract Optional<ModCandidate> discoverFolderMod(Path folder, Path destinationFolder) throws IOException;
public abstract Optional<ModCandidate> discoverFileMod(Path file, Path destinationFolder) throws IOException;
public List<ModCandidate> discoverMods(Path folder, Path destination) throws IOException {
List<ModCandidate> mods = new ArrayList<>();
if (!Files.isDirectory(folder)) return ImmutableList.of();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder)) {
for (Path path : stream) {
String name = path.getFileName().toString();
if (Files.isDirectory(path)) {
if (searchRecursively() && isValidDirectoryName(name)) {
mods.addAll(discoverMods(folder.resolve(name), destination.resolve(name)));
} else if (allowDirectoryMods()) {
discoverFolderMod(path, destination)
.ifPresent(mods::add);
}
} else if (Files.exists(path) && isValidFileName(name)) {
discoverFileMod(path, destination)
.ifPresent(mods::add);
}
}
}
return mods;
}
public Map<ModCandidate, Path> excludeClassEdits(Map<ModCandidate, Path> modPaths, Path tempFolder, MappingsRegistry mappingsRegistry) {
Map<ModCandidate, Path> map = new HashMap<>();
Map<ModCandidate, Path> convertMap = new HashMap<>();
for (Map.Entry<ModCandidate, Path> entry : modPaths.entrySet()) {
ModCandidate modCandidate = entry.getKey();
Path tempDir = tempFolder.resolve(entry.getValue().getParent().getFileName().toString());
if (!Files.exists(tempDir)) {
try {
Files.createDirectory(tempDir);
} catch (IOException e) {
e.printStackTrace();
continue;
}
}
Path tempFile = tempDir.resolve(entry.getValue().getFileName().toString());
map.put(new ModCandidate(
modCandidate.modName,
modCandidate.accessWidenerName,
modCandidate.file,
tempFile
), entry.getValue());
convertMap.put(entry.getKey(), tempFile);
}
List<ModCandidate> errored = new ArrayList<>();
for (Map.Entry<ModCandidate, Path> entry : convertMap.entrySet()) {
ModCandidate modCandidate = entry.getKey();
try {
if (Files.isDirectory(modCandidate.original)) {
FileUtils.zipFolder(modCandidate.original, entry.getValue());
} else {
Files.copy(modCandidate.original, entry.getValue(), StandardCopyOption.REPLACE_EXISTING);
}
FileUtils.removeEntriesFromZip(entry.getValue(), mappingsRegistry.getVanillaClassNames());
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
errored.add(modCandidate);
}
}
errored.forEach(map::remove);
return map;
}
}