Skip to content

Commit

Permalink
Merge remote-tracking branch 'official/main' into prs
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Nov 16, 2024
2 parents 6388d06 + 9edfe29 commit 383aea0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 10 deletions.
14 changes: 11 additions & 3 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@
import org.jackhuang.hmcl.setting.*;
import org.jackhuang.hmcl.task.*;
import org.jackhuang.hmcl.ui.*;
import org.jackhuang.hmcl.ui.construct.*;
import org.jackhuang.hmcl.ui.construct.DialogCloseEvent;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
import org.jackhuang.hmcl.ui.construct.PromptDialogPane;
import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane;
import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.ResponseCodeException;
import org.jackhuang.hmcl.util.platform.*;
import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
Expand All @@ -60,9 +64,10 @@
import static javafx.application.Platform.setImplicitExit;
import static org.jackhuang.hmcl.ui.FXUtils.runInFX;
import static org.jackhuang.hmcl.util.Lang.resolveException;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
import static org.jackhuang.hmcl.util.platform.Platform.*;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
import static org.jackhuang.hmcl.util.platform.Platform.SYSTEM_PLATFORM;
import static org.jackhuang.hmcl.util.platform.Platform.isCompatibleWithX86Java;

public final class LauncherHelper {

Expand Down Expand Up @@ -178,6 +183,9 @@ private void launch0() {
.thenComposeAsync(() -> logIn(account).withStage("launch.state.logging_in"))
.thenComposeAsync(authInfo -> Task.supplyAsync(() -> {
LaunchOptions launchOptions = repository.getLaunchOptions(selectedVersion, javaVersionRef.get(), profile.getGameDir(), javaAgents, scriptFile != null);

LOG.info("Here's the structure of game mod directory:\n" + FileUtils.printFileStructure(repository.getModManager(selectedVersion).getModsDirectory(), 10));

return new HMCLGameLauncher(
repository,
version.get(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.jackhuang.hmcl.util.io;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

final class DirectoryStructurePrinter {
private DirectoryStructurePrinter() {
}

public static String list(Path path, int maxDepth) throws IOException {
StringBuilder output = new StringBuilder(128);
list(path, maxDepth, output);
output.setLength(output.length() - 1);
return output.toString();
}

private static void list(Path path, int maxDepth, StringBuilder output) throws IOException {
output.append("Filesystem structure of: ").append(path).append('\n');

if (!Files.exists(path)) {
pushMessage(output, "nonexistent path", 1);
return;
}
if (Files.isRegularFile(path)) {
pushMessage(output, "regular file path", 1);
return;
}
if (Files.isDirectory(path)) {
Files.walkFileTree(path, new FileVisitor<Path>() {
private boolean isFolderEmpty;

private int depth = 1;

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
isFolderEmpty = true;

pushFile(output, dir, depth);
if (depth == maxDepth) {
pushMessage(output, "too deep", depth);
return FileVisitResult.SKIP_SUBTREE;
}

depth++;
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
isFolderEmpty = false;

pushFile(output, file, depth);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
visitFile(file, null);

pushMessage(output, exc.toString(), depth);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
if (isFolderEmpty) {
pushMessage(output, "empty directory", depth);
}

depth--;
return FileVisitResult.CONTINUE;
}
});
return;
}

pushMessage(output, "unknown file type", 1);
}

private static void pushFile(StringBuilder output, Path file, int depth) {
output.append("|");
for (int i = 1; i < depth; i++) {
output.append(" |");
}
output.append("-> ").append(FileUtils.getName(file)).append('\n');
}

private static void pushMessage(StringBuilder output, String message, int depth) {
output.append("| ");
for (int i = 1; i < depth; i++) {
output.append(" | ");
}
output.append('<').append(message).append(">\n");
}
}
14 changes: 7 additions & 7 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Predicate;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -199,7 +195,7 @@ public static void writeText(Path file, String text, Charset charset) throws IOE
* It will create the file if it does not exist, or truncate the existing file to empty for rewriting.
* All bytes in byte array will be written into the file in binary format. Existing data will be erased.
*
* @param file the path to the file
* @param file the path to the file
* @param data the data being written to file
* @throws IOException if an I/O error occurs
*/
Expand All @@ -212,7 +208,7 @@ public static void writeBytes(File file, byte[] data) throws IOException {
* It will create the file if it does not exist, or truncate the existing file to empty for rewriting.
* All bytes in byte array will be written into the file in binary format. Existing data will be erased.
*
* @param file the path to the file
* @param file the path to the file
* @param data the data being written to file
* @throws IOException if an I/O error occurs
*/
Expand Down Expand Up @@ -591,4 +587,8 @@ public static void saveSafely(Path file, ExceptionalConsumer<? super OutputStrea

Files.move(tmpFile, file, StandardCopyOption.REPLACE_EXISTING);
}

public static String printFileStructure(Path path, int maxDepth) throws IOException {
return DirectoryStructurePrinter.list(path, maxDepth);
}
}

0 comments on commit 383aea0

Please sign in to comment.