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.Random;
import java.util.logging.Level;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
@ -170,9 +171,9 @@ public class DecoratorController {
}
List<Path> candidates;
try {
candidates = Files.list(imageDir)
.filter(Files::isRegularFile)
try (Stream<Path> stream = Files.list(imageDir)) {
candidates = stream
.filter(Files::isRegularFile)
.filter(it -> {
String filename = it.getFileName().toString();
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.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
@ -69,7 +70,7 @@ public class WorldListPage extends ListPageBase<WorldListItem> {
}
@Override
protected ToolbarListPageSkin createDefaultSkin() {
protected ToolbarListPageSkin<WorldListPage> createDefaultSkin() {
return new WorldListPageSkin();
}
@ -87,7 +88,11 @@ public class WorldListPage extends ListPageBase<WorldListItem> {
setLoading(true);
return CompletableFuture
.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) -> {
worlds = result;
setLoading(false);

View File

@ -102,10 +102,12 @@ public class World {
return;
}
Path root = Files.list(fs.getPath("/")).filter(Files::isDirectory).findAny()
.orElseThrow(() -> new IOException("Not a valid world zip file"));
fileName = FileUtils.getName(root);
loadFromZipImpl(root);
try (Stream<Path> stream = Files.list(fs.getPath("/"))) {
Path root = stream.filter(Files::isDirectory).findAny()
.orElseThrow(() -> new IOException("Not a valid world zip file"));
fileName = FileUtils.getName(root);
loadFromZipImpl(root);
}
}
}
@ -173,14 +175,16 @@ public class World {
new Unzipper(file, worldDir).unzip();
} else {
List<Path> subDirs = Files.list(fs.getPath("/")).collect(Collectors.toList());
if (subDirs.size() != 1) {
throw new IOException("World zip malformed");
try (Stream<Path> stream = Files.list(fs.getPath("/"))) {
List<Path> subDirs = stream.collect(Collectors.toList());
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) {
try {
if (Files.exists(savesDir))
if (Files.exists(savesDir)) {
return Files.list(savesDir).flatMap(world -> {
try {
return Stream.of(new World(world));
@ -220,6 +224,7 @@ public class World {
return Stream.empty();
}
});
}
} catch (IOException 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<>();
for (Pack pack : info) packs.add(pack.getId());
if (Files.isDirectory(datapacks))
for (Path datapack : Files.newDirectoryStream(datapacks)) {
if (Files.isDirectory(datapack) && packs.contains(FileUtils.getName(datapack)))
FileUtils.deleteDirectory(datapack.toFile());
else if (Files.isRegularFile(datapack) && packs.contains(FileUtils.getNameWithoutExtension(datapack)))
Files.delete(datapack);
if (Files.isDirectory(datapacks)) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(datapacks)) {
for (Path datapack : directoryStream) {
if (Files.isDirectory(datapack) && packs.contains(FileUtils.getName(datapack)))
FileUtils.deleteDirectory(datapack.toFile());
else if (Files.isRegularFile(datapack) && packs.contains(FileUtils.getNameWithoutExtension(datapack)))
Files.delete(datapack);
}
}
}
if (isMultiple) {
new Unzipper(path, worldPath)
@ -148,46 +151,49 @@ public class Datapack {
private void loadFromDir(Path dir) throws IOException {
List<Pack> info = new ArrayList<>();
if (Files.isDirectory(dir))
for (Path subDir : Files.newDirectoryStream(dir)) {
if (Files.isDirectory(subDir)) {
Path mcmeta = subDir.resolve("pack.mcmeta");
Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled");
if (Files.isDirectory(dir)) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir)) {
for (Path subDir : directoryStream) {
if (Files.isDirectory(subDir)) {
Path mcmeta = subDir.resolve("pack.mcmeta");
Path mcmetaDisabled = subDir.resolve("pack.mcmeta.disabled");
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))
if (!Files.exists(mcmeta) && !Files.exists(mcmetaDisabled))
continue;
String name = FileUtils.getName(subDir);
if (name.endsWith(".disabled")) {
name = name.substring(0, name.length() - ".disabled".length());
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);
}
if (!name.endsWith(".zip"))
continue;
name = StringUtils.substringBeforeLast(name, ".zip");
} else if (Files.isRegularFile(subDir)) {
try (FileSystem fs = CompressingUtils.createReadOnlyZipFileSystem(subDir)) {
Path mcmeta = fs.getPath("pack.mcmeta");
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);
if (!Files.exists(mcmeta))
continue;
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));
}

View File

@ -24,6 +24,7 @@ import org.jackhuang.hmcl.util.versioning.VersionNumber;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
@ -91,13 +92,18 @@ public final class ModManager {
public void refreshMods() throws IOException {
modInfos.clear();
if (Files.isDirectory(getModsDirectory())) {
for (Path subitem : Files.newDirectoryStream(getModsDirectory())) {
if (Files.isDirectory(subitem) && VersionNumber.isIntVersionNumber(FileUtils.getName(subitem))) {
// If the folder name is game version, forge will search mod in this subdirectory
for (Path subsubitem : Files.newDirectoryStream(subitem))
addModInfo(subsubitem.toFile());
} else {
addModInfo(subitem.toFile());
try (DirectoryStream<Path> modsDirectoryStream = Files.newDirectoryStream(getModsDirectory())) {
for (Path subitem : modsDirectoryStream) {
if (Files.isDirectory(subitem) && VersionNumber.isIntVersionNumber(FileUtils.getName(subitem))) {
// If the folder name is game version, forge will search mod in this subdirectory
try (DirectoryStream<Path> subitemDirectoryStream = Files.newDirectoryStream(subitem)) {
for (Path subsubitem : subitemDirectoryStream) {
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.util.Optional;
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 {
if (testPath(root)) return root;
Path candidate = Files.list(root).filter(Files::isDirectory).findAny()
.orElseThrow(() -> new IOException("Not a valid MultiMC modpack"));
if (testPath(candidate)) return candidate;
throw new IOException("Not a valid MultiMC modpack");
try (Stream<Path> stream = Files.list(root)) {
Path candidate = stream.filter(Files::isDirectory).findAny()
.orElseThrow(() -> 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 {

View File

@ -36,6 +36,7 @@ import org.jackhuang.hmcl.util.io.IOUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
@ -148,22 +149,25 @@ public final class MultiMCModpackInstallTask extends Task<Void> {
Path root = MultiMCInstanceConfiguration.getRootPath(fs.getPath("/"));
Path patches = root.resolve("patches");
if (Files.exists(patches))
for (Path patchJson : Files.newDirectoryStream(patches)) {
if (patchJson.toString().endsWith(".json")) {
// 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);
if (Files.exists(patches)) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(patches)) {
for (Path patchJson : directoryStream) {
if (patchJson.toString().endsWith(".json")) {
// 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<>();
for (String arg : multiMCPatch.getTweakers()) {
arguments.add("--tweakClass");
arguments.add(arg);
List<String> arguments = new ArrayList<>();
for (String arg : multiMCPatch.getTweakers()) {
arguments.add("--tweakClass");
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");
if (Files.exists(libraries))

View File

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