Skip to content

Commit

Permalink
Add New Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Mar 28, 2023
1 parent a6be480 commit 8860930
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 23 deletions.
3 changes: 2 additions & 1 deletion assets/ArkPetsDefault.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"display_fps":30,
"display_margin_bottom":0,
"display_monitor_info":[1920,1080,60,24],
"display_scale":1.25
"display_scale":1.25,
"logging_level":"INFO"
}
12 changes: 10 additions & 2 deletions assets/UI/Homepage.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0"
minWidth="0.0" prefHeight="335.0" prefWidth="425.0" styleClass="config-field">
<children>
<VBox prefHeight="350.0" prefWidth="440.0">
<VBox>
<children>
<Label styleClass="config-group-title" text="动作"/>
<HBox>
Expand Down Expand Up @@ -274,7 +274,7 @@
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0"
minWidth="0.0" prefHeight="335.0" prefWidth="425.0" styleClass="config-field">
<children>
<VBox prefHeight="350.0" prefWidth="440.0">
<VBox>
<children>
<Label styleClass="config-group-title" text="显示设置"/>
<HBox>
Expand Down Expand Up @@ -354,6 +354,14 @@
</children>
</HBox>
<Separator/>
<Label styleClass="config-group-title" text="高级设置"/>
<HBox>
<children>
<Label text="日志级别"/>
<JFXComboBox fx:id="configLoggingLevel" prefWidth="100.0"/>
</children>
</HBox>
<Separator/>
<Label styleClass="config-group-title" text="关于软件"/>
<HBox spacing="20.0">
<children>
Expand Down
4 changes: 2 additions & 2 deletions core/src/com/isharryh/arkpets/ArkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class ArkConfig {
configCustomPath = "ArkPetsCustom.config";
configDefaultPath = "/ArkPetsDefault.config";
configCustom = new File(configCustomPath);
//configDefault = SrcUtil.getInternalFile(ArkConfig.class, "/ArkPetsDefault.config");
configDefault = new File(Objects.requireNonNull(ArkConfig.class.getResource(configDefaultPath)).toExternalForm());
}

Expand All @@ -33,13 +32,14 @@ public class ArkConfig {
public int display_fps;
public int display_margin_bottom;
public int[] display_monitor_info;
public String character_recent;
public String character_recent;
public int behavior_ai_activation;
public boolean behavior_allow_sleep;
public boolean behavior_allow_walk;
public boolean behavior_allow_sit;
public boolean behavior_allow_interact;
public boolean behavior_do_peer_repulsion;
public String logging_level;

private ArkConfig() {
}
Expand Down
27 changes: 23 additions & 4 deletions core/src/com/isharryh/arkpets/utils/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Logger {
private static boolean isFileLoggerAvailable = false;
private static boolean isInitialized = false;
private static int maxFileCount = 256;
private static Level level = Level.INFO;

public static final int ERROR = 40000;
public static final int WARN = 30000;
Expand All @@ -41,7 +42,7 @@ public static void initialize(String logPrefix, int maxFileCount) {
public String getHeader() {
return "# *** " + header + " ***" + Layout.LINE_SEP +
"# Created: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS")) + Layout.LINE_SEP +
"# OS name: " + System.getProperty("os.name") + Layout.LINE_SEP +
"# OS: " + System.getProperty("os.name") + " (" + System.getProperty("os.arch") + ")" + Layout.LINE_SEP +
"# Java version: " + System.getProperty("java.version") + Layout.LINE_SEP +
"# Working directory: " + System.getProperty("user.dir") + Layout.LINE_SEP +
Layout.LINE_SEP;
Expand All @@ -63,15 +64,33 @@ public String getHeader() {
ConsoleAppender consoleAppender = new ConsoleAppender(consoleLayout);
rootLogger.addAppender(consoleAppender);

setLevel(INFO);
setLevel(level);
isInitialized = true;
}

/** Set a new log level.
* @param level The new level.
*/
public static void setLevel(Level level) {
Logger.level = level;
rootLogger.setLevel(level);
currentLogger.setLevel(level);
}

/** Set a new log level.
* @param level The new level in int format.
*/
public static void setLevel(int level) {
rootLogger.setLevel(Level.toLevel(level));
currentLogger.setLevel(Level.toLevel(level));
Logger.level = Level.toLevel(level);
rootLogger.setLevel(Logger.level);
currentLogger.setLevel(Logger.level);
}

/** Get the level of root logger.
* @return The level object.
*/
public static Level getLevel() {
return rootLogger.getLevel();
}

/** Log a message that has the level {@code DEBUG}.
Expand Down
20 changes: 18 additions & 2 deletions desktop/src/com/isharryh/arkpets/ArkHomeFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void start(Stage stage) throws Exception {
// Set handler for internal start button.
Button startBtn = (Button)root.lookup("#Start-btn");
startBtn.setOnAction(e -> {
// When request to launch ArkPets
// When request to launch ArkPets:
startArkPets();
try {
Thread.sleep(1000);
Expand Down Expand Up @@ -68,10 +68,26 @@ public void startArkPets() {
Task<Boolean> task = new Task<>() {
@Override
protected Boolean call() throws IOException, InterruptedException {
// Renew the logging level arg to match the custom value of the Launcher.
List<String> args = Arrays.asList(ArgPending.argCache.clone());
args.remove("--quiet");
args.remove("--warn");
args.remove("--info");
args.remove("--debug");
String temp = null;
switch (ctrl.config.logging_level) {
case "ERROR": temp = "--quiet"; break;
case "WARN": temp = "--warn"; break;
case "INFO": temp = "--info"; break;
case "DEBUG": temp = "--debug"; break;
default: temp = "";
}
args.add(temp);
// Start ArkPets core.
int code = JavaProcess.exec(
EmbeddedLauncher.class, true,
List.of(),
Arrays.asList(ArgPending.argCache)
args
);
if (code != 0) {
// TODO pop error
Expand Down
28 changes: 22 additions & 6 deletions desktop/src/com/isharryh/arkpets/controllers/Homepage.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.*;
import org.apache.log4j.Level;

import javafx.application.Platform;
import javafx.collections.ListChangeListener;
Expand Down Expand Up @@ -127,6 +128,8 @@ public class Homepage {
@FXML
private JFXButton manageModelVerify;
@FXML
private JFXComboBox<String> configLoggingLevel;
@FXML
private Label aboutQueryUpdate;
@FXML
private Label aboutVisitWebsite;
Expand All @@ -151,6 +154,7 @@ public void initialize() {
initWrapper(1);
initModelSearch();
initModelManage();
initConfigAdvanced();
initAbout();
menuBtn1.getStyleClass().add("menu-btn-active");
Platform.runLater(() -> {
Expand Down Expand Up @@ -342,6 +346,18 @@ private void initModelManage() {
manageModelVerify.setOnAction(e -> foregroundVerifyModels());
}

private void initConfigAdvanced() {
configLoggingLevel.getItems().setAll("DEBUG", "INFO", "WARN", "ERROR");
configLoggingLevel.getSelectionModel().select(config.logging_level);
configLoggingLevel.valueProperty().addListener(observable -> {
if (configLoggingLevel.getValue() != null) {
Logger.setLevel(Level.toLevel(configLoggingLevel.getValue(), Level.INFO));
config.logging_level = Logger.getLevel().toString();
config.saveConfig();
}
});
}

private void initAbout() {
aboutQueryUpdate.setOnMouseClicked(e -> foregroundCheckUpdate(true, "manual"));
aboutVisitWebsite.setOnMouseClicked(e -> {
Expand Down Expand Up @@ -703,12 +719,12 @@ public Task<Boolean> createDownloadTask(boolean $isArchive, String $remotePathSu
@Override
protected Boolean call() throws Exception {
this.updateMessage("正在选择最佳线路");
Logger.info("Download", "Testing real delay");
Logger.info("Downloader", "Testing real delay");
Downloader.GitHubSource[] sources = Downloader.GitHubSource.sortByDelay(Downloader.ghSources);
Downloader.GitHubSource source = sources[0];
Logger.info("Download", "Selected the shortest delayed source \"" + source.tag + "\" (" + source.delay + "ms)");
Logger.info("Downloader", "Selected the shortest delayed source \"" + source.tag + "\" (" + source.delay + "ms)");
String remotePath = ($isArchive ? source.archivePreUrl : source.rawPreUrl) + $remotePathSuffix;
Logger.info("Download", "Downloading " + remotePath + " to " + $localPath);
Logger.info("Downloader", "Downloading " + remotePath + " to " + $localPath);
this.updateMessage("正在尝试与 " + source.tag + " 建立连接");

URL urlFile;
Expand Down Expand Up @@ -765,7 +781,7 @@ protected Boolean call() throws Exception {
} catch (Exception ignored){
}
}
Logger.info("Download", "Downloaded " + $localPath + " , file size: " + sum);
Logger.info("Downloader", "Downloaded " + $localPath + " , file size: " + sum);
return this.isDone() && !this.isCancelled();
}
};
Expand All @@ -776,7 +792,7 @@ public Task<Boolean> createDownloadTask(String $remotePath, String $localPath) {
@Override
protected Boolean call() throws Exception {
this.updateMessage("正在尝试建立连接");
Logger.info("Download", "Downloading " + $remotePath + " to " + $localPath);
Logger.info("Downloader", "Downloading " + $remotePath + " to " + $localPath);

URL urlFile;
HttpsURLConnection connection = null;
Expand Down Expand Up @@ -832,7 +848,7 @@ protected Boolean call() throws Exception {
} catch (Exception ignored){
}
}
Logger.info("Download", "Downloaded " + $localPath + " , file size: " + sum);
Logger.info("Downloader", "Downloaded " + $localPath + " , file size: " + sum);
return this.isDone() && !this.isCancelled();
}
};
Expand Down
16 changes: 10 additions & 6 deletions desktop/src/com/isharryh/arkpets/utils/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ protected void onFail(Exception e) {
protected void onSucceed(long sum) {
}


/** Test the real connection delay of the given URL (with a specified port).
* @param $url The URL to be tested.
* @param $port The port to connect.
* @param $timeoutMillis Timeout (ms).
* @return The delay (ms). {@code -1} when connection failed or timeout.
*/
public static int testDelay(String $url, int $port, int $timeoutMillis) {
Socket socket = new Socket();
int delayMillis = -1;
Expand Down Expand Up @@ -122,6 +127,7 @@ public int testDelay() {

public int testDelay(int $port, int $timeoutMillis) {
delay = Downloader.testDelay(preUrl, $port, $timeoutMillis);
Logger.debug("Downloader", "Real delay for \"" + tag + "\" is " + delay + "ms");
return delay;
}

Expand All @@ -130,11 +136,9 @@ public static Source[] sortByDelay(Source[] $sources) {
s.testDelay();
ArrayList<Source> sources = new ArrayList<>(Arrays.stream($sources).toList());
sources.sort((o1, o2) -> {
if (o1.delay < 0)
return 1;
if (o1.delay != o2.delay)
return o1.delay > o2.delay ? 1 : -1;
return 0;
if (o1.delay == o2.delay)
return 0;
return (o1.delay > o2.delay || o1.delay < 0) ? 1 : -1;
});
return sources.toArray(new Source[0]);
}
Expand Down

0 comments on commit 8860930

Please sign in to comment.