fix: cannot rename game version sometimes

This commit is contained in:
huanghongxun 2020-04-17 23:45:15 +08:00
parent 99f42db370
commit 5893eae87f
8 changed files with 110 additions and 80 deletions

View File

@ -59,6 +59,7 @@ import java.util.Locale;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.config;
@ -170,8 +171,8 @@ public class DecoratorController {
} }
List<Path> candidates; List<Path> candidates;
try { try (Stream<Path> stream = Files.list(imageDir)) {
candidates = Files.list(imageDir) candidates = stream
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.filter(it -> { .filter(it -> {
String filename = it.getFileName().toString(); String filename = it.getFileName().toString();

View File

@ -43,6 +43,7 @@ import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
@ -69,7 +70,7 @@ public class WorldListPage extends ListPageBase<WorldListItem> {
} }
@Override @Override
protected ToolbarListPageSkin createDefaultSkin() { protected ToolbarListPageSkin<WorldListPage> createDefaultSkin() {
return new WorldListPageSkin(); return new WorldListPageSkin();
} }
@ -87,7 +88,11 @@ public class WorldListPage extends ListPageBase<WorldListItem> {
setLoading(true); setLoading(true);
return CompletableFuture return CompletableFuture
.runAsync(() -> gameVersion = GameVersion.minecraftVersion(profile.getRepository().getVersionJar(id)).orElse(null)) .runAsync(() -> gameVersion = GameVersion.minecraftVersion(profile.getRepository().getVersionJar(id)).orElse(null))
.thenApplyAsync(unused -> World.getWorlds(savesDir).parallel().collect(Collectors.toList())) .thenApplyAsync(unused -> {
try (Stream<World> stream = World.getWorlds(savesDir)) {
return stream.parallel().collect(Collectors.toList());
}
})
.whenCompleteAsync((result, exception) -> { .whenCompleteAsync((result, exception) -> {
worlds = result; worlds = result;
setLoading(false); setLoading(false);

View File

@ -102,12 +102,14 @@ public class World {
return; return;
} }
Path root = Files.list(fs.getPath("/")).filter(Files::isDirectory).findAny() try (Stream<Path> stream = Files.list(fs.getPath("/"))) {
Path root = stream.filter(Files::isDirectory).findAny()
.orElseThrow(() -> new IOException("Not a valid world zip file")); .orElseThrow(() -> new IOException("Not a valid world zip file"));
fileName = FileUtils.getName(root); fileName = FileUtils.getName(root);
loadFromZipImpl(root); loadFromZipImpl(root);
} }
} }
}
private void getWorldName(Path levelDat) throws IOException { private void getWorldName(Path levelDat) throws IOException {
CompoundTag nbt = parseLevelDat(levelDat); CompoundTag nbt = parseLevelDat(levelDat);
@ -173,7 +175,8 @@ public class World {
new Unzipper(file, worldDir).unzip(); new Unzipper(file, worldDir).unzip();
} else { } else {
List<Path> subDirs = Files.list(fs.getPath("/")).collect(Collectors.toList()); try (Stream<Path> stream = Files.list(fs.getPath("/"))) {
List<Path> subDirs = stream.collect(Collectors.toList());
if (subDirs.size() != 1) { if (subDirs.size() != 1) {
throw new IOException("World zip malformed"); throw new IOException("World zip malformed");
} }
@ -182,6 +185,7 @@ public class World {
.setSubDirectory("/" + subDirectoryName + "/") .setSubDirectory("/" + subDirectoryName + "/")
.unzip(); .unzip();
} }
}
} }
new World(worldDir).rename(name); new World(worldDir).rename(name);
@ -211,7 +215,7 @@ public class World {
public static Stream<World> getWorlds(Path savesDir) { public static Stream<World> getWorlds(Path savesDir) {
try { try {
if (Files.exists(savesDir)) if (Files.exists(savesDir)) {
return Files.list(savesDir).flatMap(world -> { return Files.list(savesDir).flatMap(world -> {
try { try {
return Stream.of(new World(world)); return Stream.of(new World(world));
@ -220,6 +224,7 @@ public class World {
return Stream.empty(); return Stream.empty();
} }
}); });
}
} catch (IOException e) { } catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Failed to read saves", e); Logging.LOG.log(Level.WARNING, "Failed to read saves", e);
} }

View File

@ -58,13 +58,16 @@ public class Datapack {
Set<String> packs = new HashSet<>(); Set<String> packs = new HashSet<>();
for (Pack pack : info) packs.add(pack.getId()); for (Pack pack : info) packs.add(pack.getId());
if (Files.isDirectory(datapacks)) if (Files.isDirectory(datapacks)) {
for (Path datapack : Files.newDirectoryStream(datapacks)) { try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(datapacks)) {
for (Path datapack : directoryStream) {
if (Files.isDirectory(datapack) && packs.contains(FileUtils.getName(datapack))) if (Files.isDirectory(datapack) && packs.contains(FileUtils.getName(datapack)))
FileUtils.deleteDirectory(datapack.toFile()); FileUtils.deleteDirectory(datapack.toFile());
else if (Files.isRegularFile(datapack) && packs.contains(FileUtils.getNameWithoutExtension(datapack))) else if (Files.isRegularFile(datapack) && packs.contains(FileUtils.getNameWithoutExtension(datapack)))
Files.delete(datapack); Files.delete(datapack);
} }
}
}
if (isMultiple) { if (isMultiple) {
new Unzipper(path, worldPath) new Unzipper(path, worldPath)
@ -148,8 +151,9 @@ public class Datapack {
private void loadFromDir(Path dir) throws IOException { private void loadFromDir(Path dir) throws IOException {
List<Pack> info = new ArrayList<>(); List<Pack> info = new ArrayList<>();
if (Files.isDirectory(dir)) if (Files.isDirectory(dir)) {
for (Path subDir : Files.newDirectoryStream(dir)) { try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir)) {
for (Path subDir : directoryStream) {
if (Files.isDirectory(subDir)) { if (Files.isDirectory(subDir)) {
Path mcmeta = subDir.resolve("pack.mcmeta"); Path mcmeta = subDir.resolve("pack.mcmeta");
Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled"); Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled");
@ -188,6 +192,8 @@ public class Datapack {
} }
} }
} }
}
}
Platform.runLater(() -> this.info.setAll(info)); Platform.runLater(() -> this.info.setAll(info));
} }

View File

@ -24,6 +24,7 @@ import org.jackhuang.hmcl.util.versioning.VersionNumber;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
@ -91,16 +92,21 @@ public final class ModManager {
public void refreshMods() throws IOException { public void refreshMods() throws IOException {
modInfos.clear(); modInfos.clear();
if (Files.isDirectory(getModsDirectory())) { if (Files.isDirectory(getModsDirectory())) {
for (Path subitem : Files.newDirectoryStream(getModsDirectory())) { try (DirectoryStream<Path> modsDirectoryStream = Files.newDirectoryStream(getModsDirectory())) {
for (Path subitem : modsDirectoryStream) {
if (Files.isDirectory(subitem) && VersionNumber.isIntVersionNumber(FileUtils.getName(subitem))) { if (Files.isDirectory(subitem) && VersionNumber.isIntVersionNumber(FileUtils.getName(subitem))) {
// If the folder name is game version, forge will search mod in this subdirectory // If the folder name is game version, forge will search mod in this subdirectory
for (Path subsubitem : Files.newDirectoryStream(subitem)) try (DirectoryStream<Path> subitemDirectoryStream = Files.newDirectoryStream(subitem)) {
for (Path subsubitem : subitemDirectoryStream) {
addModInfo(subsubitem.toFile()); addModInfo(subsubitem.toFile());
}
}
} else { } else {
addModInfo(subitem.toFile()); addModInfo(subitem.toFile());
} }
} }
} }
}
loaded = true; loaded = true;
} }

View File

@ -30,6 +30,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional; import java.util.Optional;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Stream;
/** /**
* *
@ -334,11 +335,13 @@ public final class MultiMCInstanceConfiguration {
public static Path getRootPath(Path root) throws IOException { public static Path getRootPath(Path root) throws IOException {
if (testPath(root)) return root; if (testPath(root)) return root;
Path candidate = Files.list(root).filter(Files::isDirectory).findAny() try (Stream<Path> stream = Files.list(root)) {
Path candidate = stream.filter(Files::isDirectory).findAny()
.orElseThrow(() -> new IOException("Not a valid MultiMC modpack")); .orElseThrow(() -> new IOException("Not a valid MultiMC modpack"));
if (testPath(candidate)) return candidate; if (testPath(candidate)) return candidate;
throw new IOException("Not a valid MultiMC modpack"); throw new IOException("Not a valid MultiMC modpack");
} }
}
public static Modpack readMultiMCModpackManifest(Path modpackFile, Charset encoding) throws IOException { public static Modpack readMultiMCModpackManifest(Path modpackFile, Charset encoding) throws IOException {
try (FileSystem fs = CompressingUtils.readonly(modpackFile).setEncoding(encoding).build()) { try (FileSystem fs = CompressingUtils.readonly(modpackFile).setEncoding(encoding).build()) {

View File

@ -36,6 +36,7 @@ import org.jackhuang.hmcl.util.io.IOUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -148,8 +149,9 @@ public final class MultiMCModpackInstallTask extends Task<Void> {
Path root = MultiMCInstanceConfiguration.getRootPath(fs.getPath("/")); Path root = MultiMCInstanceConfiguration.getRootPath(fs.getPath("/"));
Path patches = root.resolve("patches"); Path patches = root.resolve("patches");
if (Files.exists(patches)) if (Files.exists(patches)) {
for (Path patchJson : Files.newDirectoryStream(patches)) { try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(patches)) {
for (Path patchJson : directoryStream) {
if (patchJson.toString().endsWith(".json")) { if (patchJson.toString().endsWith(".json")) {
// If json is malformed, we should stop installing this modpack instead of skipping it. // If json is malformed, we should stop installing this modpack instead of skipping it.
MultiMCInstancePatch multiMCPatch = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(patchJson)), MultiMCInstancePatch.class); MultiMCInstancePatch multiMCPatch = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(patchJson)), MultiMCInstancePatch.class);
@ -164,6 +166,8 @@ public final class MultiMCModpackInstallTask extends Task<Void> {
version = version.addPatch(patch); version = version.addPatch(patch);
} }
} }
}
}
Path libraries = root.resolve("libraries"); Path libraries = root.resolve("libraries");
if (Files.exists(libraries)) if (Files.exists(libraries))

View File

@ -176,8 +176,8 @@ public final class JavaVersion {
List<JavaVersion> javaVersions; List<JavaVersion> javaVersions;
try { try (Stream<Path> stream = searchPotentialJavaHomes()) {
javaVersions = lookupJavas(searchPotentialJavaHomes()); javaVersions = lookupJavas(stream);
} catch (IOException e) { } catch (IOException e) {
LOG.log(Level.WARNING, "Failed to search Java homes", e); LOG.log(Level.WARNING, "Failed to search Java homes", e);
javaVersions = new ArrayList<>(); javaVersions = new ArrayList<>();