Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/mod/ModManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jackhuang.hmcl.download.LibraryAnalyzer;
import org.jackhuang.hmcl.game.GameRepository;
import org.jackhuang.hmcl.mod.modinfo.*;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.Pair;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.io.CompressingUtils;
Expand All @@ -31,6 +32,9 @@
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;

import static org.jackhuang.hmcl.util.Pair.pair;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
Expand Down Expand Up @@ -63,8 +67,8 @@ private interface ModMetadataReader {

private final GameRepository repository;
private final String id;
private final TreeSet<LocalModFile> localModFiles = new TreeSet<>();
private final HashMap<Pair<String, ModLoaderType>, LocalMod> localMods = new HashMap<>();
private final Set<LocalModFile> localModFiles = Collections.synchronizedSet(new TreeSet<>());
private final Map<Pair<String, ModLoaderType>, LocalMod> localMods = new ConcurrentHashMap<>();
private LibraryAnalyzer analyzer;

private boolean loaded = false;
Expand Down Expand Up @@ -180,19 +184,43 @@ public void refreshMods() throws IOException {
|| analyzer.has(LibraryAnalyzer.LibraryType.QUILT);

if (Files.isDirectory(getModsDirectory())) {
// collect mod files
var modFiles = new ArrayList<Path>();
try (DirectoryStream<Path> modsDirectoryStream = Files.newDirectoryStream(getModsDirectory())) {
for (Path subitem : modsDirectoryStream) {
if (supportSubfolders && Files.isDirectory(subitem) && !".connector".equalsIgnoreCase(subitem.getFileName().toString())) {
try (DirectoryStream<Path> subitemDirectoryStream = Files.newDirectoryStream(subitem)) {
for (Path subsubitem : subitemDirectoryStream) {
addModInfo(subsubitem);
modFiles.add(subsubitem);
}
}
} else {
addModInfo(subitem);
modFiles.add(subitem);
}
}
}

// load mods
var futures = new ArrayList<CompletableFuture<Void>>(modFiles.size());
var submissionLimit = new Semaphore(10);
for (Path modFile : modFiles) {
try {
submissionLimit.acquire();
} catch (InterruptedException e) {
throw new RuntimeException("Main thread interrupted, should be impossible", e);
}
var future = CompletableFuture.runAsync(() -> {
try {
addModInfo(modFile);
} finally {
submissionLimit.release();
}
}, Schedulers.io());
futures.add(future);
}

// ensure complete
CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)).join();
}
loaded = true;
}
Expand Down