Skip to content

Commit f38ae24

Browse files
committed
More refactoring
1 parent 01752fe commit f38ae24

File tree

1 file changed

+76
-84
lines changed

1 file changed

+76
-84
lines changed

src/main/java/io/github/fabriccompatibiltylayers/modremappingapi/impl/ModDiscoverer.java

Lines changed: 76 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
99
import net.fabricmc.loader.api.FabricLoader;
1010
import net.fabricmc.loader.impl.launch.FabricLauncherBase;
11+
import org.jetbrains.annotations.NotNull;
1112
import org.spongepowered.include.com.google.common.collect.ImmutableList;
1213

1314
import java.io.File;
14-
import java.io.FileInputStream;
1515
import java.io.IOException;
1616
import java.net.URISyntaxException;
1717
import java.nio.file.*;
18+
import java.nio.file.attribute.BasicFileAttributes;
1819
import java.util.*;
1920
import java.util.stream.Collectors;
20-
import java.util.zip.ZipEntry;
21-
import java.util.zip.ZipInputStream;
2221

2322
public class ModDiscoverer {
2423
private static final Map<String, List<String>> EXCLUDED = new HashMap<>();
@@ -44,8 +43,8 @@ protected static void init(List<ModRemapper> modRemappers, boolean remapClassEdi
4443
if (!Files.exists(cacheFolder)) Files.createDirectories(cacheFolder);
4544
else io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils.emptyDir(cacheFolder);
4645

47-
mods.addAll(discoverModsInFolder(mcSubFolder.toFile(), cacheFolder.toFile()));
48-
} catch (IOException e) {
46+
mods.addAll(discoverModsInFolder(mcSubFolder, cacheFolder));
47+
} catch (IOException | URISyntaxException e) {
4948
e.printStackTrace();
5049
}
5150
}
@@ -97,7 +96,7 @@ private static Map<Path, Path> excludeClassEdits(Map<Path, Path> modPaths) {
9796
for (Map.Entry<Path, Path> entry : convertMap.entrySet()) {
9897
try {
9998
if (Files.isDirectory(entry.getKey())) {
100-
io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils.zipFolder(entry.getKey(), entry.getValue());
99+
FileUtils.zipFolder(entry.getKey(), entry.getValue());
101100
} else {
102101
Files.copy(entry.getKey(), entry.getValue(), StandardCopyOption.REPLACE_EXISTING);
103102
}
@@ -114,97 +113,90 @@ private static Map<Path, Path> excludeClassEdits(Map<Path, Path> modPaths) {
114113
return map;
115114
}
116115

117-
private static List<ModEntry> discoverModsInFolder(File folder, File destination) {
118-
List<ModEntry> mods = new ArrayList<>();
116+
private static Optional<ModEntry> discoverFolderMod(Path folder, Path destinationFolder) throws IOException {
117+
String name = folder.getFileName().toString().replace(" ", "_");
118+
Path destination = destinationFolder.resolve(name + ".zip");
119119

120-
File[] folderFiles = folder.listFiles();
121-
if (!folder.isDirectory() || folderFiles == null) return ImmutableList.of();
120+
final boolean[] hasClasses = {false};
122121

123-
for (File file : folderFiles) {
124-
String name = file.getName().replace(" ", "_");
125-
if (file.isDirectory() || (file.isFile() && (name.endsWith(".jar") || name.endsWith(".zip")))) {
126-
File remappedFile = new File(destination, name);
122+
Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
123+
@Override
124+
public @NotNull FileVisitResult visitFile(Path file, @NotNull BasicFileAttributes attrs) throws IOException {
125+
if (file.toString().endsWith(".class")) {
126+
hasClasses[0] = true;
127+
return FileVisitResult.TERMINATE;
128+
}
127129

128-
List<ModEntry> modName = new ArrayList<>();
130+
return super.visitFile(file, attrs);
131+
}
132+
});
133+
134+
if (hasClasses[0]) {
135+
return Optional.of(
136+
new DefaultModEntry(
137+
name,
138+
folder.toFile(),
139+
destination.toFile()
140+
)
141+
);
142+
}
129143

130-
boolean hasClass = false;
131-
boolean fabric = false;
144+
return Optional.empty();
145+
}
132146

133-
File[] fileFiles = file.listFiles();
147+
private static Optional<ModEntry> discoverFileMod(Path file, Path destinationFolder) throws IOException {
148+
String fileName = file.getFileName().toString().replace(" ", "_");
149+
String modName = fileName.replace(".jar", "").replace(".zip", "");
134150

135-
if (file.isDirectory() && fileFiles != null) {
136-
remappedFile = new File(destination, name + ".zip");
137-
for (File subFile : fileFiles) {
138-
String subName = subFile.getName();
139-
if (subFile.isFile()) {
140-
if (subName.endsWith(".class")) {
141-
hasClass = true;
142-
}
143-
}
144-
}
151+
List<String> entries = FileUtils.listZipContent(file);
145152

146-
if (/* modName.isEmpty() && */ hasClass) {
147-
modName.add(new DefaultModEntry(
148-
name.replace(".zip", "").replace(".jar", ""),
149-
remappedFile,
150-
file
151-
));
152-
}
153+
for (String entry : entries) {
154+
if (Objects.equals(entry, "/fabric.mod.json")) break;
153155

154-
if (!modName.isEmpty() && EXCLUDED.containsKey(modName.get(0).modName)) {
155-
for (String excluded :
156-
EXCLUDED.get(modName.get(0).modName)) {
157-
File excludedFile = new File(file, excluded);
158-
if (excludedFile.delete()) {
159-
Constants.MAIN_LOGGER.debug("File deleted: " + excludedFile.getName());
160-
}
161-
}
162-
}
163-
} else {
164-
try {
165-
FileInputStream fileinputstream = new FileInputStream(file);
166-
ZipInputStream zipinputstream = new ZipInputStream(fileinputstream);
167-
while (true) {
168-
ZipEntry zipentry = zipinputstream.getNextEntry();
169-
if (zipentry == null) {
170-
zipinputstream.close();
171-
fileinputstream.close();
172-
break;
173-
}
174-
175-
String s1 = zipentry.getName();
176-
String[] ss = s1.split("/");
177-
String s2 = ss[ss.length - 1];
178-
if (!zipentry.isDirectory()) {
179-
if (s2.equals("fabric.mod.json")) {
180-
// modName.clear();
181-
fabric = true;
182-
break;
183-
} else if (s2.endsWith(".class")) {
184-
hasClass = true;
185-
}
186-
}
187-
}
156+
if (entry.endsWith(".class")) {
157+
return Optional.of(
158+
new DefaultModEntry(
159+
modName,
160+
file.toFile(),
161+
destinationFolder.resolve(fileName).toFile()
162+
)
163+
);
164+
}
165+
}
188166

189-
if (/* modName.isEmpty() && */ hasClass && !fabric) {
190-
modName.add(new DefaultModEntry(
191-
name.replace(".zip", "").replace(".jar", ""),
192-
remappedFile,
193-
file
194-
));
195-
}
167+
return Optional.empty();
168+
}
169+
170+
private static List<ModEntry> discoverModsInFolder(Path folder, Path destination) throws IOException, URISyntaxException {
171+
List<ModEntry> mods = new ArrayList<>();
172+
173+
if (!Files.isDirectory(folder)) return ImmutableList.of();
174+
175+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder)) {
176+
for (Path path : stream) {
177+
String name = path.getFileName().toString();
196178

197-
if (!modName.isEmpty()) {
198-
if (EXCLUDED.containsKey(modName.get(0).modName)) {
199-
FileUtils.removeEntriesFromZip(file.toPath(), EXCLUDED.get(modName.get(0).modName));
200-
}
179+
if (Files.isDirectory(path)) {
180+
discoverFolderMod(path, destination)
181+
.ifPresent(mods::add);
182+
} else if (Files.exists(path) && (name.endsWith(".jar") || name.endsWith(".zip"))) {
183+
discoverFileMod(path, destination)
184+
.ifPresent(mods::add);
185+
}
186+
}
187+
}
188+
189+
for (ModEntry modEntry : mods) {
190+
if (EXCLUDED.containsKey(modEntry.modId)) {
191+
if (Files.isDirectory(modEntry.file.toPath())) {
192+
for (String excluded : EXCLUDED.get(modEntry.modId)) {
193+
if (Files.deleteIfExists(modEntry.file.toPath().resolve(excluded))) {
194+
Constants.MAIN_LOGGER.debug("File deleted: " + modEntry.file.toPath().resolve(excluded));
201195
}
202-
} catch (IOException | URISyntaxException e) {
203-
throw new RuntimeException(e);
204196
}
197+
} else {
198+
FileUtils.removeEntriesFromZip(modEntry.file.toPath(), EXCLUDED.get(modEntry.modId));
205199
}
206-
207-
mods.addAll(modName);
208200
}
209201
}
210202

0 commit comments

Comments
 (0)