Skip to content

Commit 9edfe29

Browse files
实现 #2969: 启动前日志列出mods目录的jar文件 (#3125)
* Support #2969: 启动前日志列出mods目录的jar文件 * Support #2969: 启动前日志列出mods目录jar文件 * Support #2969: 启动前日志中使用字符画列出mods目录模组文件 * Support #2969: 启动前日志中使用字符画列出mods目录模组文件 * Support #2969: 启动前日志中使用字符画列出mods目录模组文件 * Support #2969: 日志列出mods目录时不再过滤文件后缀 * Support #2969: 启动时日志列出mods目录(代码优化) * Support #2969: 启动时日志列出mods目录(不再列出Zip内容) * Support #2969 * Fix: checkstyle --------- Co-authored-by: YELANDAOKONG <yelandaokong@yldk.xyz>
1 parent 5591d92 commit 9edfe29

File tree

3 files changed

+117
-10
lines changed

3 files changed

+117
-10
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@
3636
import org.jackhuang.hmcl.setting.*;
3737
import org.jackhuang.hmcl.task.*;
3838
import org.jackhuang.hmcl.ui.*;
39-
import org.jackhuang.hmcl.ui.construct.*;
39+
import org.jackhuang.hmcl.ui.construct.DialogCloseEvent;
40+
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
4041
import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
42+
import org.jackhuang.hmcl.ui.construct.PromptDialogPane;
43+
import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane;
4144
import org.jackhuang.hmcl.util.*;
4245
import org.jackhuang.hmcl.util.i18n.I18n;
46+
import org.jackhuang.hmcl.util.io.FileUtils;
4347
import org.jackhuang.hmcl.util.io.ResponseCodeException;
4448
import org.jackhuang.hmcl.util.platform.*;
4549
import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
@@ -60,9 +64,10 @@
6064
import static javafx.application.Platform.setImplicitExit;
6165
import static org.jackhuang.hmcl.ui.FXUtils.runInFX;
6266
import static org.jackhuang.hmcl.util.Lang.resolveException;
63-
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
6467
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
65-
import static org.jackhuang.hmcl.util.platform.Platform.*;
68+
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
69+
import static org.jackhuang.hmcl.util.platform.Platform.SYSTEM_PLATFORM;
70+
import static org.jackhuang.hmcl.util.platform.Platform.isCompatibleWithX86Java;
6671

6772
public final class LauncherHelper {
6873

@@ -178,6 +183,9 @@ private void launch0() {
178183
.thenComposeAsync(() -> logIn(account).withStage("launch.state.logging_in"))
179184
.thenComposeAsync(authInfo -> Task.supplyAsync(() -> {
180185
LaunchOptions launchOptions = repository.getLaunchOptions(selectedVersion, javaVersionRef.get(), profile.getGameDir(), javaAgents, scriptFile != null);
186+
187+
LOG.info("Here's the structure of game mod directory:\n" + FileUtils.printFileStructure(repository.getModManager(selectedVersion).getModsDirectory(), 10));
188+
181189
return new HMCLGameLauncher(
182190
repository,
183191
version.get(),
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.jackhuang.hmcl.util.io;
2+
3+
import java.io.IOException;
4+
import java.nio.file.FileVisitResult;
5+
import java.nio.file.FileVisitor;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.attribute.BasicFileAttributes;
9+
10+
final class DirectoryStructurePrinter {
11+
private DirectoryStructurePrinter() {
12+
}
13+
14+
public static String list(Path path, int maxDepth) throws IOException {
15+
StringBuilder output = new StringBuilder(128);
16+
list(path, maxDepth, output);
17+
output.setLength(output.length() - 1);
18+
return output.toString();
19+
}
20+
21+
private static void list(Path path, int maxDepth, StringBuilder output) throws IOException {
22+
output.append("Filesystem structure of: ").append(path).append('\n');
23+
24+
if (!Files.exists(path)) {
25+
pushMessage(output, "nonexistent path", 1);
26+
return;
27+
}
28+
if (Files.isRegularFile(path)) {
29+
pushMessage(output, "regular file path", 1);
30+
return;
31+
}
32+
if (Files.isDirectory(path)) {
33+
Files.walkFileTree(path, new FileVisitor<Path>() {
34+
private boolean isFolderEmpty;
35+
36+
private int depth = 1;
37+
38+
@Override
39+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
40+
isFolderEmpty = true;
41+
42+
pushFile(output, dir, depth);
43+
if (depth == maxDepth) {
44+
pushMessage(output, "too deep", depth);
45+
return FileVisitResult.SKIP_SUBTREE;
46+
}
47+
48+
depth++;
49+
return FileVisitResult.CONTINUE;
50+
}
51+
52+
@Override
53+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
54+
isFolderEmpty = false;
55+
56+
pushFile(output, file, depth);
57+
return FileVisitResult.CONTINUE;
58+
}
59+
60+
@Override
61+
public FileVisitResult visitFileFailed(Path file, IOException exc) {
62+
visitFile(file, null);
63+
64+
pushMessage(output, exc.toString(), depth);
65+
return FileVisitResult.CONTINUE;
66+
}
67+
68+
@Override
69+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
70+
if (isFolderEmpty) {
71+
pushMessage(output, "empty directory", depth);
72+
}
73+
74+
depth--;
75+
return FileVisitResult.CONTINUE;
76+
}
77+
});
78+
return;
79+
}
80+
81+
pushMessage(output, "unknown file type", 1);
82+
}
83+
84+
private static void pushFile(StringBuilder output, Path file, int depth) {
85+
output.append("|");
86+
for (int i = 1; i < depth; i++) {
87+
output.append(" |");
88+
}
89+
output.append("-> ").append(FileUtils.getName(file)).append('\n');
90+
}
91+
92+
private static void pushMessage(StringBuilder output, String message, int depth) {
93+
output.append("| ");
94+
for (int i = 1; i < depth; i++) {
95+
output.append(" | ");
96+
}
97+
output.append('<').append(message).append(">\n");
98+
}
99+
}

HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
import java.time.ZonedDateTime;
3131
import java.time.format.DateTimeFormatter;
3232
import java.time.temporal.ChronoUnit;
33-
import java.util.ArrayList;
34-
import java.util.List;
35-
import java.util.Locale;
36-
import java.util.Objects;
37-
import java.util.Optional;
33+
import java.util.*;
3834
import java.util.function.Predicate;
3935

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

592588
Files.move(tmpFile, file, StandardCopyOption.REPLACE_EXISTING);
593589
}
590+
591+
public static String printFileStructure(Path path, int maxDepth) throws IOException {
592+
return DirectoryStructurePrinter.list(path, maxDepth);
593+
}
594594
}

0 commit comments

Comments
 (0)