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,9 +171,9 @@ 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();
return filename.endsWith(".png") || filename.endsWith(".jpg"); return filename.endsWith(".png") || filename.endsWith(".jpg");

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,10 +102,12 @@ public class World {
return; return;
} }
Path root = Files.list(fs.getPath("/")).filter(Files::isDirectory).findAny() try (Stream<Path> stream = Files.list(fs.getPath("/"))) {
.orElseThrow(() -> new IOException("Not a valid world zip file")); Path root = stream.filter(Files::isDirectory).findAny()
fileName = FileUtils.getName(root); .orElseThrow(() -> new IOException("Not a valid world zip file"));
loadFromZipImpl(root); fileName = FileUtils.getName(root);
loadFromZipImpl(root);
}
} }
} }
@ -173,14 +175,16 @@ 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("/"))) {
if (subDirs.size() != 1) { List<Path> subDirs = stream.collect(Collectors.toList());
throw new IOException("World zip malformed"); if (subDirs.size() != 1) {
throw new IOException("World zip malformed");
}
String subDirectoryName = FileUtils.getName(subDirs.get(0));
new Unzipper(file, worldDir)
.setSubDirectory("/" + subDirectoryName + "/")
.unzip();
} }
String subDirectoryName = FileUtils.getName(subDirs.get(0));
new Unzipper(file, worldDir)
.setSubDirectory("/" + subDirectoryName + "/")
.unzip();
} }
} }
@ -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)) {
if (Files.isDirectory(datapack) && packs.contains(FileUtils.getName(datapack))) for (Path datapack : directoryStream) {
FileUtils.deleteDirectory(datapack.toFile()); if (Files.isDirectory(datapack) && packs.contains(FileUtils.getName(datapack)))
else if (Files.isRegularFile(datapack) && packs.contains(FileUtils.getNameWithoutExtension(datapack))) FileUtils.deleteDirectory(datapack.toFile());
Files.delete(datapack); else if (Files.isRegularFile(datapack) && packs.contains(FileUtils.getNameWithoutExtension(datapack)))
Files.delete(datapack);
}
} }
}
if (isMultiple) { if (isMultiple) {
new Unzipper(path, worldPath) new Unzipper(path, worldPath)
@ -148,46 +151,49 @@ 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)) {
if (Files.isDirectory(subDir)) { for (Path subDir : directoryStream) {
Path mcmeta = subDir.resolve("pack.mcmeta"); if (Files.isDirectory(subDir)) {
Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled"); Path mcmeta = subDir.resolve("pack.mcmeta");
Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled");
if (!Files.exists(mcmeta) && !Files.exists(mcmetaDisabled)) if (!Files.exists(mcmeta) && !Files.exists(mcmetaDisabled))
continue;
boolean enabled = Files.exists(mcmeta);
try {
PackMcMeta pack = enabled ? JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class)
: JsonUtils.fromNonNullJson(FileUtils.readText(mcmetaDisabled), PackMcMeta.class);
info.add(new Pack(enabled ? mcmeta : mcmetaDisabled, FileUtils.getName(subDir), pack.getPackInfo().getDescription(), this));
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
}
} else if (Files.isRegularFile(subDir)) {
try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(subDir)) {
Path mcmeta = fs.getPath("pack.mcmeta");
if (!Files.exists(mcmeta))
continue; continue;
String name = FileUtils.getName(subDir); boolean enabled = Files.exists(mcmeta);
if (name.endsWith(".disabled")) {
name = name.substring(0, name.length() - ".disabled".length()); try {
PackMcMeta pack = enabled ? JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class)
: JsonUtils.fromNonNullJson(FileUtils.readText(mcmetaDisabled), PackMcMeta.class);
info.add(new Pack(enabled ? mcmeta : mcmetaDisabled, FileUtils.getName(subDir), pack.getPackInfo().getDescription(), this));
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
} }
if (!name.endsWith(".zip")) } else if (Files.isRegularFile(subDir)) {
continue; try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(subDir)) {
name = StringUtils.substringBeforeLast(name, ".zip"); Path mcmeta = fs.getPath("pack.mcmeta");
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class); if (!Files.exists(mcmeta))
info.add(new Pack(subDir, name, pack.getPackInfo().getDescription(), this)); continue;
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e); String name = FileUtils.getName(subDir);
if (name.endsWith(".disabled")) {
name = name.substring(0, name.length() - ".disabled".length());
}
if (!name.endsWith(".zip"))
continue;
name = StringUtils.substringBeforeLast(name, ".zip");
PackMcMeta pack = JsonUtils.fromNonNullJson(FileUtils.readText(mcmeta), PackMcMeta.class);
info.add(new Pack(subDir, name, pack.getPackInfo().getDescription(), this));
} catch (IOException | JsonParseException e) {
Logging.LOG.log(Level.WARNING, "Failed to read datapack " + subDir, e);
}
} }
} }
} }
}
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,13 +92,18 @@ 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())) {
if (Files.isDirectory(subitem) && VersionNumber.isIntVersionNumber(FileUtils.getName(subitem))) { for (Path subitem : modsDirectoryStream) {
// If the folder name is game version, forge will search mod in this subdirectory if (Files.isDirectory(subitem) && VersionNumber.isIntVersionNumber(FileUtils.getName(subitem))) {
for (Path subsubitem : Files.newDirectoryStream(subitem)) // If the folder name is game version, forge will search mod in this subdirectory
addModInfo(subsubitem.toFile()); try (DirectoryStream<Path> subitemDirectoryStream = Files.newDirectoryStream(subitem)) {
} else { for (Path subsubitem : subitemDirectoryStream) {
addModInfo(subitem.toFile()); addModInfo(subsubitem.toFile());
}
}
} else {
addModInfo(subitem.toFile());
}
} }
} }
} }

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,10 +335,12 @@ 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)) {
.orElseThrow(() -> new IOException("Not a valid MultiMC modpack")); Path candidate = stream.filter(Files::isDirectory).findAny()
if (testPath(candidate)) return candidate; .orElseThrow(() -> new IOException("Not a valid MultiMC modpack"));
throw new IOException("Not a valid MultiMC modpack"); if (testPath(candidate)) return candidate;
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 {

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,22 +149,25 @@ 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)) {
if (patchJson.toString().endsWith(".json")) { for (Path patchJson : directoryStream) {
// If json is malformed, we should stop installing this modpack instead of skipping it. if (patchJson.toString().endsWith(".json")) {
MultiMCInstancePatch multiMCPatch = JsonUtils.GSON.fromJson(IOUtils.readFullyAsString(Files.newInputStream(patchJson)), MultiMCInstancePatch.class); // 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);
List<String> arguments = new ArrayList<>(); List<String> arguments = new ArrayList<>();
for (String arg : multiMCPatch.getTweakers()) { for (String arg : multiMCPatch.getTweakers()) {
arguments.add("--tweakClass"); arguments.add("--tweakClass");
arguments.add(arg); arguments.add(arg);
}
Version patch = new Version(multiMCPatch.getName(), multiMCPatch.getVersion(), 1, new Arguments().addGameArguments(arguments), multiMCPatch.getMainClass(), multiMCPatch.getLibraries());
version = version.addPatch(patch);
} }
Version patch = new Version(multiMCPatch.getName(), multiMCPatch.getVersion(), 1, new Arguments().addGameArguments(arguments), multiMCPatch.getMainClass(), multiMCPatch.getLibraries());
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<>();