Skip to content

Commit

Permalink
Small Update
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed May 18, 2023
1 parent 86a5450 commit 352eca6
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 69 deletions.
12 changes: 0 additions & 12 deletions assets/UI/Homepage.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
</rowConstraints>
<JFXButton fx:id="menuBtn1" mnemonicParsing="false" prefHeight="40.0" prefWidth="140.0"
styleClass="shadowed" text="模型" textAlignment="CENTER">
<font>
<Font name="System" size="18.0"/>
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</GridPane.margin>
Expand All @@ -53,9 +50,6 @@
</JFXButton>
<JFXButton fx:id="menuBtn2" mnemonicParsing="false" prefHeight="40.0" prefWidth="140.0"
styleClass="shadowed" text="行为" textAlignment="CENTER" GridPane.rowIndex="1">
<font>
<Font name="System" size="18.0"/>
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</GridPane.margin>
Expand All @@ -71,9 +65,6 @@
</JFXButton>
<JFXButton fx:id="menuBtn3" mnemonicParsing="false" prefHeight="40.0" prefWidth="140.0"
styleClass="shadowed" text="选项" textAlignment="CENTER" GridPane.rowIndex="2">
<font>
<Font name="System" size="18.0"/>
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</GridPane.margin>
Expand All @@ -90,9 +81,6 @@
</GridPane>
<JFXButton id="Start-btn" fx:id="startBtn" layoutX="25.0" layoutY="310.0" mnemonicParsing="false"
prefHeight="36.0" prefWidth="90.0" text="启动" textAlignment="CENTER">
<font>
<Font size="18.0"/>
</font>
<graphic>
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="24.0" prefWidth="2.0"
translateX="-20.0" translateY="1.0">
Expand Down
5 changes: 4 additions & 1 deletion assets/UI/Main.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
******************************************************************************/

* {
/* Font anti-aliasing */
/* Fonts */
-fx-font-smoothing-type: gray;
-fx-font-family: "Microsoft YaHei" "SimHei" "SimSun";
}

.scroll-v {
Expand Down Expand Up @@ -113,6 +114,7 @@ Button:focused {
/* Regular */
-fx-cursor: hand;
-fx-padding: 0 0 0 20px;
-fx-font-size: 18px;
-fx-text-fill: #204880;
-fx-text-alignment: left;
-fx-background-color: #FFFFFF;
Expand Down Expand Up @@ -143,6 +145,7 @@ Button:focused {
/* Regular */
-fx-cursor: hand;
-fx-padding: 0 0 0 20px;
-fx-font-size: 18px;
-fx-text-fill: #FFFFFF;
-fx-text-alignment: left;
-fx-background-color: #204880;
Expand Down
6 changes: 3 additions & 3 deletions core/src/cn/harryh/arkpets/ArkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static boolean addStartup() {
IOUtils.FileUtil.writeString(startupFile, charsetVBS, script, false);
Logger.info("Config", "Auto-startup was added: " + startupFile.getAbsolutePath());
return true;
} catch (IOException e) {
} catch (Exception e) {
Logger.error("Config", "Auto-startup adding failed, details see below.", e);
return false;
}
Expand All @@ -120,7 +120,7 @@ public static void removeStartup() {
try {
IOUtils.FileUtil.delete(startupFile.toPath(), false);
Logger.info("Config", "Auto-startup was removed: " + startupFile.getAbsolutePath());
} catch (IOException e) {
} catch (Exception e) {
Logger.error("Config", "Auto-startup removing failed, details see below.", e);
}
}
Expand All @@ -135,7 +135,7 @@ public static boolean isSetStartup() {
String checksum1 = IOUtils.FileUtil.getMD5(Objects.requireNonNull(script).getBytes(charsetVBS));
String checksum2 = IOUtils.FileUtil.getMD5(startupFile);
return checksum1.equals(checksum2);
} catch (IOException e) {
} catch (Exception e) {
return false;
}
}
Expand Down
118 changes: 66 additions & 52 deletions desktop/src/cn/harryh/arkpets/controllers/Homepage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@
import cn.harryh.arkpets.ArkPets;
import cn.harryh.arkpets.utils.*;
import cn.harryh.arkpets.ArkConfig;
import com.alibaba.fastjson.JSONObject;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;

import static cn.harryh.arkpets.Const.*;
import static cn.harryh.arkpets.utils.PopupUtils.*;

import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.*;
import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import org.apache.log4j.Level;

import javafx.animation.FadeTransition;
import javafx.beans.value.ChangeListener;
import javafx.collections.ListChangeListener;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.util.Duration;
import javafx.scene.Group;
Expand All @@ -33,9 +29,9 @@
import javafx.scene.input.KeyCode;
import javafx.scene.layout.*;

import java.awt.Desktop;
import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
Expand All @@ -44,6 +40,9 @@
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException;

import static cn.harryh.arkpets.Const.*;
import static cn.harryh.arkpets.utils.PopupUtils.*;


public class Homepage {
private boolean isHttpsTrustAll = false;
Expand Down Expand Up @@ -152,6 +151,9 @@ public Homepage() {

public void initialize() {
Logger.info("Launcher", "Initializing (JavaFX " + System.getProperty("javafx.version") + ", " + "ArkPets " + appVersionStr + ")");
Logger.debug("Launcher", "Property file.encoding = " + System.getProperty("file.encoding"));
Logger.debug("Launcher", "Property sun.jnu.encoding = " + System.getProperty("sun.jnu.encoding"));
Logger.debug("Launcher", "Default charset = " + Charset.defaultCharset());
wrapper0.setVisible(true);
popLoading(e -> {
config = Objects.requireNonNull(ArkConfig.getConfig(), "ArkConfig returns a null instance, please check the config file.");
Expand Down Expand Up @@ -324,6 +326,7 @@ private void initConfigBehavior() {
config.behavior_allow_sit = configBehaviorAllowSit.isSelected();
config.saveConfig();
});

configBehaviorAiActivation.setMax(8);
configBehaviorAiActivation.setMin(0);
configBehaviorAiActivation.setMajorTickUnit(1);
Expand Down Expand Up @@ -512,16 +515,16 @@ public JFXDialog foregroundTask(Task $task, String $header, String $defaultConte
});
$task.messageProperty().addListener(((observable, oldValue, newValue) -> h3.setText(newValue)));
$task.setOnCancelled(e -> {
Logger.info("Task", "Foreground task was cancelled.");
Logger.info("Task", "Foreground dialog task was cancelled.");
DialogUtil.disposeDialog(dialog, root);
});
$task.setOnFailed(e -> {
Logger.error("Task", "Foreground task failed, details see below.", $task.getException());
Logger.error("Task", "Foreground dialog task failed, details see below.", $task.getException());
popError($task.getException()).show();
DialogUtil.disposeDialog(dialog, root);
});
$task.setOnSucceeded(e -> {
Logger.info("Task", "Foreground task completed.");
Logger.info("Task", "Foreground dialog task completed.");
DialogUtil.disposeDialog(dialog, root);
});
Thread thread = new Thread($task);
Expand All @@ -531,7 +534,11 @@ public JFXDialog foregroundTask(Task $task, String $header, String $defaultConte

public void popLoading(EventHandler<ActionEvent> $onLoading) {
fadeInNode(wrapper0, durationFast, e -> {
$onLoading.handle(e);
try {
$onLoading.handle(e);
} catch (Exception ex) {
Logger.error("Task", "Foreground loading task failed, details see below.", ex);
}
fadeOutNode(wrapper0, durationFast, null);
});
}
Expand Down Expand Up @@ -872,48 +879,55 @@ public Task<Boolean> createDownloadTask(boolean $isArchive, String $remotePathSu
protected Boolean call() throws Exception {
this.updateMessage("正在选择最佳线路");
Logger.info("Network", "Testing real delay");
NetUtils.GitHubSource[] sources = NetUtils.GitHubSource.sortByDelay(NetUtils.ghSources);
NetUtils.GitHubSource[] sources = NetUtils.GitHubSource.sortByOverallAvailability(NetUtils.ghSources);
NetUtils.GitHubSource source = sources[0];
Logger.info("Network", "Selected the shortest delayed source \"" + source.tag + "\" (" + source.delay + "ms)");
String remotePath = ($isArchive ? source.archivePreUrl : source.rawPreUrl) + $remotePathSuffix;
Logger.info("Network", "Downloading " + remotePath + " to " + $localPath);
this.updateMessage("正在尝试与 " + source.tag + " 建立连接");

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File file = new File($localPath);
URL urlFile = new URL(remotePath);
HttpsURLConnection connection = NetUtils.ConnectionUtil.createHttpsConnection(urlFile, httpTimeoutDefault, httpTimeoutDefault, isHttpsTrustAll);

try {
bis = new BufferedInputStream(connection.getInputStream(), httpBufferSizeDefault);
bos = new BufferedOutputStream(Files.newOutputStream(file.toPath()), httpBufferSizeDefault);
int len = httpBufferSizeDefault;
long sum = 0;
long max = connection.getContentLengthLong();
byte[] bytes = new byte[len];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
sum += len;
this.updateMessage("当前已下载:" + NetUtils.getFormattedSizeString(sum));
this.updateProgress(sum, max);
if (this.isCancelled()) {
this.updateMessage("下载进程已被取消");
break;
}
}
this.updateProgress(max, max);
bos.flush();
Logger.info("Network", "Downloaded " + $localPath + " , file size: " + sum);
} finally {
Logger.info("Network", "Selected the most available source \"" + source.tag + "\" (" + source.delay + "ms)");
String remotePath = ($isArchive ? source.archivePreUrl : source.rawPreUrl) + $remotePathSuffix;
Logger.info("Network", "Fetching " + remotePath + " to " + $localPath);
this.updateMessage("正在尝试与 " + source.tag + " 建立连接");

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File file = new File($localPath);
URL urlFile = new URL(remotePath);
HttpsURLConnection connection = NetUtils.ConnectionUtil.createHttpsConnection(urlFile, httpTimeoutDefault, httpTimeoutDefault, isHttpsTrustAll);

try {
connection.getInputStream().close();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception ignored){
bis = new BufferedInputStream(connection.getInputStream(), httpBufferSizeDefault);
bos = new BufferedOutputStream(Files.newOutputStream(file.toPath()), httpBufferSizeDefault);
int len = httpBufferSizeDefault;
long sum = 0;
long max = connection.getContentLengthLong();
byte[] bytes = new byte[len];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
sum += len;
this.updateMessage("当前已下载:" + NetUtils.getFormattedSizeString(sum));
this.updateProgress(sum, max);
if (this.isCancelled()) {
this.updateMessage("下载进程已被取消");
break;
}
}
this.updateProgress(max, max);
bos.flush();
Logger.info("Network", "Fetched " + $localPath + " , size: " + sum);
} finally {
try {
connection.getInputStream().close();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception ignored) {
}
}
} catch (Exception e) {
if (!(e instanceof SSLException))
source.receiveError();
throw e;
}
return this.isDone() && !this.isCancelled();
}
Expand All @@ -925,7 +939,7 @@ public Task<Boolean> createDownloadTask(String $remotePath, String $localPath) {
@Override
protected Boolean call() throws Exception {
this.updateMessage("正在尝试建立连接");
Logger.info("Network", "Downloading " + $remotePath + " to " + $localPath);
Logger.info("Network", "Fetching " + $remotePath + " to " + $localPath);

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
Expand All @@ -952,7 +966,7 @@ protected Boolean call() throws Exception {
}
this.updateProgress(max, max);
bos.flush();
Logger.info("Network", "Downloaded " + $localPath + " , file size: " + sum);
Logger.info("Network", "Fetched " + $localPath + " , size: " + sum);
} finally {
try {
connection.getInputStream().close();
Expand Down
43 changes: 42 additions & 1 deletion desktop/src/cn/harryh/arkpets/utils/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class NetUtils {

public static GitHubSource[] ghSources = new GitHubSource[] {
new GitHubSource("GitHub", "https://raw.githubusercontent.com/", "https://github.com/"),
// new GitHubSource("FastGit", "https://raw.fastgit.org/", "https://download.fastgit.org/"),
new GitHubSource("GHProxy", "https://ghproxy.com/https://raw.githubusercontent.com/", "https://ghproxy.com/https://github.com/")
};
public static final Map<Long, String> sizeMap;
Expand Down Expand Up @@ -218,12 +217,18 @@ public static class Source {
public final String tag;
public final String preUrl;
public int delay = -1;
public long lastErrorTime = -1;

public Source(String $tag, String $preUrl) {
tag= $tag;
preUrl = $preUrl;
}

public void receiveError() {
lastErrorTime = System.currentTimeMillis();
Logger.debug("Network", "Marked source \"" + tag + "\" as historical unavailable with timestamp " + lastErrorTime);
}

public int testDelay() {
return testDelay(delayTestPort, delayUpThreshold);
}
Expand All @@ -249,6 +254,24 @@ public static Source[] sortByDelay(Source[] $sources) {
});
return sources.toArray(new Source[0]);
}

public static Source[] sortByOverallAvailability(Source[] $sources) {
for (Source s : $sources)
s.testDelay();
ArrayList<Source> sources = new ArrayList<>(Arrays.stream($sources).toList());
sources.sort((o1, o2) -> {
if (o1.lastErrorTime != o2.lastErrorTime)
return (o1.lastErrorTime > o2.lastErrorTime) ? 1 : -1;
if (o1.delay == o2.delay)
return 0;
if (o1.delay < 0 && o2.delay >= 0)
return 1;
if (o1.delay >= 0 && o2.delay < 0)
return -1;
return (o1.delay > o2.delay) ? 1 : -1;
});
return sources.toArray(new Source[0]);
}
}


Expand Down Expand Up @@ -283,5 +306,23 @@ public static GitHubSource[] sortByDelay(GitHubSource[] $sources) {
});
return sources.toArray(new GitHubSource[0]);
}

public static GitHubSource[] sortByOverallAvailability(GitHubSource[] $sources) {
for (GitHubSource s : $sources)
s.testDelay();
ArrayList<GitHubSource> sources = new ArrayList<>(Arrays.stream($sources).toList());
sources.sort((o1, o2) -> {
if (o1.lastErrorTime != o2.lastErrorTime)
return (o1.lastErrorTime > o2.lastErrorTime) ? 1 : -1;
if (o1.delay == o2.delay)
return 0;
if (o1.delay < 0 && o2.delay >= 0)
return 1;
if (o1.delay >= 0 && o2.delay < 0)
return -1;
return (o1.delay > o2.delay) ? 1 : -1;
});
return sources.toArray(new GitHubSource[0]);
}
}
}

0 comments on commit 352eca6

Please sign in to comment.