Skip to content

Commit a0e97fe

Browse files
committed
修复实际的重试次数和配置文件中不一致的问题
1 parent 1fb1c0e commit a0e97fe

13 files changed

Lines changed: 157 additions & 116 deletions

File tree

src/main/java/com/github/balloonupdate/mcpatch/client/Main.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static boolean AppMain(boolean graphicsMode, StartMethod startMethod, boolean en
103103

104104
// 非独立进程启动时,使用标签标明日志所属模块
105105
if (startMethod == StartMethod.ModLoader || startMethod == StartMethod.JavaAgent)
106-
Log.openTag("Mcpatch");
106+
Log.setAppIdentifier(true);
107107

108108
// 打印调试信息
109109
PrintEnvironmentInfo(graphicsMode, startMethod, baseDir, workDir);
@@ -179,10 +179,12 @@ static boolean AppMain(boolean graphicsMode, StartMethod startMethod, boolean en
179179
if (!a && !b) {
180180
// 打印异常日志
181181
try {
182-
Log.error(new McpatchBusinessException((Exception) ex1).toString());
182+
Log.openIndent("Crash");
183+
Log.error(ex1.toString());
184+
Log.closeIndent();
183185
} catch (Exception e) {
184186
System.out.println("------------------------");
185-
System.out.println(e);
187+
System.out.println(ex1);
186188
}
187189

188190
// 图形模式下弹框显示错误
@@ -404,9 +406,9 @@ static void PrintEnvironmentInfo(boolean graphicsMode, StartMethod startMethod,
404406
Log.info("启动方式: " + startMethod);
405407
Log.info("基本目录: " + baseDir);
406408
Log.info("工作目录: " + workDir);
407-
Log.info("可执行文件所在目录: " + (Env.isDevelopment() ? "Dev" : Env.getJarPath()));
408-
Log.info("应用版本: " + Env.getVersion() + " (" + Env.getGitCommit() + ")");
409-
Log.info("Java虚拟机: " + jvmVendor + " (" + jvmVersion + ")");
410-
Log.info("操纵系统: " + osName + "/" + osVersion + "/" + osArch);
409+
Log.info("二进制文件目录: " + (Env.isDevelopment() ? "Dev" : Env.getJarPath()));
410+
Log.info("软件版本: " + Env.getVersion() + " (" + Env.getGitCommit() + ")");
411+
Log.info("虚拟机版本: " + jvmVendor + " (" + jvmVersion + ")");
412+
Log.info("操作系统: " + osName + ", " + osVersion + ", " + osArch);
411413
}
412414
}

src/main/java/com/github/balloonupdate/mcpatch/client/Work.java

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,6 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
425425
Path tempDirectory = f.tempPath.getParent();
426426
Files.createDirectories(tempDirectory);
427427

428-
McpatchBusinessException ex = null;
429-
430-
for (int i = 0; i < config.reties + 1; i++) {
431428
// 空文件不需要下载
432429
if (f.length == 0) {
433430
Files.createFile(f.tempPath);
@@ -443,52 +440,40 @@ boolean run2(Servers server) throws IOException, McpatchBusinessException {
443440

444441
AtomicLong bytesCounter = new AtomicLong();
445442

446-
try {
447-
Range range = new Range(f.offset, f.offset + f.length);
448-
String desc = f.path + " in " + f.label;
443+
Range range = new Range(f.offset, f.offset + f.length);
444+
String desc = f.path + " in " + f.label;
449445

450-
server.downloadFile(f.containerName, range, desc, f.tempPath, (packageLength, bytesReceived, lengthExpected) -> {
451-
// 计数
452-
bytesCounter.addAndGet(packageLength);
453-
totalDownloaded.addAndGet(packageLength);
454-
speed.feed(packageLength);
446+
server.downloadFile(f.containerName, range, desc, f.tempPath, (packageLength, bytesReceived, lengthExpected) -> {
447+
// 计数
448+
bytesCounter.addAndGet(packageLength);
449+
totalDownloaded.addAndGet(packageLength);
450+
speed.feed(packageLength);
455451

456-
// 更新UI
457-
if (window != null) {
458-
long now2 = System.currentTimeMillis();
452+
// 更新UI
453+
if (window != null) {
454+
long now2 = System.currentTimeMillis();
459455

460-
if (now2 - uiTimer.get() > 300) {
461-
uiTimer.set(now2);
456+
if (now2 - uiTimer.get() > 300) {
457+
uiTimer.set(now2);
462458

463-
window.setProgressBarText(String.format("%s/%s - %s/s", BytesUtils.convertBytes(totalDownloaded.get()), BytesUtils.convertBytes(totalBytes), speed.sampleSpeed2()));
464-
window.setProgressBarValue((int) (totalDownloaded.get() / (float) totalBytes * 1000));
465-
}
459+
window.setProgressBarText(String.format("%s/%s - %s/s", BytesUtils.convertBytes(totalDownloaded.get()), BytesUtils.convertBytes(totalBytes), speed.sampleSpeed2()));
460+
window.setProgressBarValue((int) (totalDownloaded.get() / (float) totalBytes * 1000));
466461
}
467-
});
468-
469-
// 修复文件 mtime
470-
Files.setLastModifiedTime(f.tempPath, FileTime.from(f.modified, TimeUnit.SECONDS));
471-
472-
break;
473-
} catch (McpatchBusinessException e) {
474-
ex = e;
475-
462+
}
463+
}, (fallback) -> {
476464
// 进度回退
477465
totalDownloaded.addAndGet(-bytesCounter.get());
478466

479467
if (window != null) {
480468
window.setProgressBarText(String.format("%s/%s - %s/s", BytesUtils.convertBytes(totalDownloaded.get()), BytesUtils.convertBytes(totalBytes), speed.sampleSpeed2()));
481469
window.setProgressBarValue((int) (totalDownloaded.get() / (float) totalBytes * 1000));
482470
}
471+
});
472+
473+
// 修复文件 mtime
474+
Files.setLastModifiedTime(f.tempPath, FileTime.from(f.modified, TimeUnit.SECONDS));
483475

484-
if (i != config.reties) {
485-
Log.error("retrying");
486-
}
487-
}
488-
}
489476

490-
if (ex != null)
491-
throw ex;
492477

493478
// 校验文件
494479
String hash = HashUtility.calculateHash(f.tempPath);

src/main/java/com/github/balloonupdate/mcpatch/client/exceptions/McpatchBusinessException.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@ public String toString() {
2222

2323
Throwable cause = getCause();
2424

25+
sb.append(getMessage());
26+
sb.append("\n");
27+
2528
if (cause != null) {
26-
sb.append(getMessage());
27-
sb.append("\n");
2829
sb.append(stackTraceToString(cause));
2930
} else {
30-
sb.append(getMessage());
31-
sb.append("\n");
3231
sb.append(stackTraceToString(this));
3332
}
3433

3534
return sb.toString();
3635
}
3736

38-
public static String stackTraceToString(Throwable e) {
37+
/**
38+
* 获取错误的调用堆栈并做成字符串返回
39+
*/
40+
static String stackTraceToString(Throwable e) {
3941
StringBuilder sb = new StringBuilder();
4042

4143
StackTraceElement[] frames = e.getStackTrace();
4244

4345
for (int i = 0; i < frames.length; i++) {
44-
// sb.append(" ");
46+
// sb.append(" ");
47+
// sb.append(i);
48+
// sb.append(": ");
4549
sb.append(frames[i].toString());
4650
sb.append("\n");
4751
}

src/main/java/com/github/balloonupdate/mcpatch/client/logging/ConsoleHandler.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,20 @@ public void onStop() {
2727

2828
@Override
2929
public void onMessage(Message message) {
30-
if (!message.newLine) {
31-
System.out.print(message.content);
32-
System.out.flush();
33-
return;
34-
}
30+
String indentText = "";
3531

36-
String prefix;
37-
String tags = "";
32+
if (!message.indents.isEmpty())
33+
indentText = String.join(" ", message.indents) + " ";
3834

39-
if (!message.tags.isEmpty())
40-
tags = String.join("/", message.tags);
35+
String appId = message.appIdentifier ? "Mcpatch" : "";
36+
String level = message.level.name().toUpperCase();
4137

42-
prefix = String.format("[ %-5s ] %s ", level.toString().toUpperCase(), tags);
38+
String prefix = String.format("%s[ %-5s ] %s ", appId, level, indentText);
4339

44-
String text = prefix + message.content.replaceAll("\n", "\n" + prefix);
40+
String text = prefix + message.content;
4541

46-
System.err.println(text);
42+
text = text.replaceAll("\n", "\n" + prefix);
43+
44+
System.out.println(text);
4745
}
4846
}

src/main/java/com/github/balloonupdate/mcpatch/client/logging/FileHandler.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,17 @@ public void onMessage(Message message) {
5353
if (writer == null)
5454
return;
5555

56-
if (!message.newLine)
57-
{
58-
writer.print(message.content);
59-
writer.flush();
60-
return;
61-
}
6256

63-
String ts = fmt.format(System.currentTimeMillis());
64-
String level = message.level.name().toUpperCase();
57+
String indentText = "";
6558

66-
String tags = "";
59+
if (!message.indents.isEmpty())
60+
indentText = String.join(" ", message.indents) + " ";
6761

68-
if (!message.tags.isEmpty())
69-
tags = String.join("/", message.tags);
62+
String appId = message.appIdentifier ? "Mcpatch" : "";
63+
String ts = fmt.format(System.currentTimeMillis());
64+
String level = message.level.name().toUpperCase();
7065

71-
String prefix = String.format("[ %s %-5s ] %s ", ts, level, tags);
66+
String prefix = String.format("%s[ %s %-5s ] %s", appId, ts, level, indentText);
7267

7368
String text = prefix + message.content;
7469

src/main/java/com/github/balloonupdate/mcpatch/client/logging/Log.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,58 @@ public class Log {
1212
static ArrayList<LogHandler> handlers = new ArrayList<>();
1313

1414
/**
15-
* 日志的 tags 目前至少预留并没有使用
15+
* 日志的 缩进文字 会显示在日志头的后面,日志文本的前面
1616
*/
17-
static ArrayList<String> tags = new ArrayList<>();
17+
static ArrayList<String> indents = new ArrayList<>();
18+
19+
/**
20+
* 应用程序标识,当开启时,在每条日志的最前面应该增加 Mcpatch 的字样,用来区分这是 mcpatch 输出的日志
21+
*/
22+
static boolean appIdentifierEnabled = false;
1823

1924
/**
2025
* 记录一条 debug 日志
2126
*/
2227
public static void debug(String message) {
23-
message(LogLevel.Debug, message, true);
28+
message(LogLevel.Debug, message);
2429
}
2530

2631
/**
2732
* 记录一条 info 日志
2833
*/
2934
public static void info(String message) {
30-
message(LogLevel.Info, message, true);
35+
message(LogLevel.Info, message);
3136
}
3237

3338
/**
3439
* 记录一条 warn 日志
3540
*/
3641
public static void warn(String message) {
37-
message(LogLevel.Warn, message, true);
42+
message(LogLevel.Warn, message);
3843
}
3944

4045
/**
4146
* 记录一条 error 日志
4247
*/
4348
public static void error(String message) {
44-
message(LogLevel.Error, message, true);
49+
message(LogLevel.Error, message);
4550
}
4651

4752
/**
4853
* 记录一条日志
4954
* @param level 日志的等级
5055
* @param content 日志的内容
51-
* @param newLine 日志内容后面是否跟上一个换行符
5256
*/
53-
public static void message(LogLevel level, String content, Boolean newLine) {
57+
public static void message(LogLevel level, String content) {
5458
for (LogHandler handler : handlers) {
5559
if(level.ordinal() >= handler.getFilterLevel().ordinal()) {
5660
Message msg = new Message();
5761

5862
msg.time = System.currentTimeMillis();
5963
msg.level = level;
6064
msg.content = content;
61-
msg.tags = tags;
62-
msg.newLine = newLine;
65+
msg.indents = indents;
66+
msg.appIdentifier = appIdentifierEnabled;
6367

6468
handler.onMessage(msg);
6569
}
@@ -69,16 +73,23 @@ public static void message(LogLevel level, String content, Boolean newLine) {
6973
/**
7074
* 开启一个 tag
7175
*/
72-
public static void openTag(String tag) {
73-
tags.add(tag);
76+
public static void openIndent(String prefix) {
77+
indents.add(prefix);
78+
}
79+
80+
/**
81+
* 关闭上一个 tag
82+
*/
83+
public static void closeIndent() {
84+
if (!indents.isEmpty())
85+
indents.remove(indents.size() - 1);
7486
}
7587

7688
/**
77-
* 开启上一个 tag
89+
* 设置应用程序标识是否打开
7890
*/
79-
public static void closeTag() {
80-
if (!tags.isEmpty())
81-
tags.remove(tags.size() - 1);
91+
public static void setAppIdentifier(boolean enabled) {
92+
appIdentifierEnabled = enabled;
8293
}
8394

8495
/**

src/main/java/com/github/balloonupdate/mcpatch/client/logging/Message.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public class Message {
2222
public String content;
2323

2424
/**
25-
* 日志的标签
25+
* 缩进文字,支持多层
2626
*/
27-
public List<String> tags;
27+
public List<String> indents;
2828

2929
/**
30-
* 日志后面是否要换行
30+
* 应用程序标识
31+
* @see "Log.appIdentifierEnabled"
3132
*/
32-
public Boolean newLine;
33+
public boolean appIdentifier;
3334
}

src/main/java/com/github/balloonupdate/mcpatch/client/network/Servers.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public String requestText(String path, Range range, String desc) throws McpatchB
6565
}
6666

6767
@Override
68-
public void downloadFile(String path, Range range, String desc, Path writeTo, OnDownload callback) throws McpatchBusinessException {
68+
public void downloadFile(String path, Range range, String desc, Path writeTo, OnDownload callback, OnFail fallback) throws McpatchBusinessException {
6969
multipleAvailableServers(e -> {
70-
e.downloadFile(path, range, desc, writeTo, callback);
70+
e.downloadFile(path, range, desc, writeTo, callback, fallback);
7171

7272
// 没办法这里必须要返回一个东西,不然编译不通过
7373
return 114514;
@@ -84,16 +84,25 @@ private <T> T multipleAvailableServers(Retry<T, UpdatingServer> task) throws Mcp
8484
{
8585
McpatchBusinessException ex = null;
8686

87+
// 记录第几次出错
88+
int errorTimes = 0;
89+
8790
// 每个服务器挨个试
8891
while (current < servers.size()) {
89-
UpdatingServer s = servers.get(current);
92+
UpdatingServer server = servers.get(current);
9093

9194
int times = config.reties;
9295

9396
while (--times >= 0) {
9497
try {
95-
return task.runTask(s);
98+
return task.runTask(server);
9699
} catch (McpatchBusinessException e) {
100+
try {
101+
server.close();
102+
} catch (Exception exc) {
103+
throw new McpatchBusinessException(exc);
104+
}
105+
97106
// 用户打断了更新
98107
if (e.getCause() instanceof ClosedByInterruptException) {
99108
throw new McpatchBusinessException("用户打断了更新", (Exception) e.getCause());
@@ -102,14 +111,19 @@ private <T> T multipleAvailableServers(Retry<T, UpdatingServer> task) throws Mcp
102111
// 记录一次错误
103112
ex = e;
104113

114+
errorTimes += 1;
115+
Log.openIndent("第" + errorTimes + "次出错:");
116+
105117
Log.warn("");
106118
Log.warn(ex.toString());
107119
Log.warn("retry " + times + "...");
108120

121+
Log.closeIndent();
122+
109123
// 不是最后一次机会的话就等待一下下
110-
if (times > 1) {
124+
if (times > 0) {
111125
try {
112-
Thread.sleep(3);
126+
Thread.sleep(3000);
113127
} catch (InterruptedException ignored) {}
114128
}
115129
}

0 commit comments

Comments
 (0)