Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented #69 features #70

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@ jobs:
echo "Current ref: ${{ github.ref }}"

- name: Clone Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup JavaJDK
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'liberica'
cache: 'gradle'

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
gradle-version: 8.1.1

- name: Execute Gradle Tasks
run: gradle clean distAll

- name: Upload Exe
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: ${{ ! startsWith(github.ref, 'refs/tags/') }}
with:
name: ArkPets.exe
path: desktop/build/dist/*.exe

- name: Upload Zip
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: ${{ ! startsWith(github.ref, 'refs/tags/') }}
with:
name: ArkPets.zip
path: desktop/build/dist/*.zip

- name: Upload Jar
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: ${{ ! startsWith(github.ref, 'refs/tags/') }}
with:
name: ArkPets.jar
Expand Down
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ test*/
*.jar

# Exclude runtime files
/ArkPetsConfig.json
/models_data.json
ArkPetsConfig.json
models_data.json
models*/
logs*/
err**
1 change: 1 addition & 0 deletions .idea/Ark-Pets.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/GitCommitMessageStorage.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions core/src/cn/harryh/arkpets/concurrent/ProcessPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public final class ProcessPool implements Executor {
return thread;
});

private static ProcessPool instance = null;
private static volatile ProcessPool instance = null;

public static synchronized ProcessPool getInstance() {
public static ProcessPool getInstance() {
if (instance == null)
instance = new ProcessPool();
synchronized (ProcessPool.class) {
if (instance == null)
instance = new ProcessPool();
}
return instance;
}

Expand Down Expand Up @@ -82,14 +85,20 @@ public boolean isSuccess() {

public static class UnexpectedExitCodeException extends Exception {
private final int exitCode;
private final long processId;

public UnexpectedExitCodeException(int exitCode) {
public UnexpectedExitCodeException(int exitCode, long processId) {
this.exitCode = exitCode;
this.processId = processId;
}

@Override
public String getMessage() {
return "The process exited with a non-zero exit code: " + exitCode;
}

public long getProcessId() {
return processId;
}
}
}
18 changes: 14 additions & 4 deletions core/src/cn/harryh/arkpets/concurrent/SocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;

import static cn.harryh.arkpets.Const.serverPorts;

Expand All @@ -24,12 +25,15 @@ public final class SocketServer {
private ServerSocket serverSocket = null;
private final Set<SocketSession> sessionList = new CopyOnWriteArraySet<>();
private Thread listener;
private final AtomicBoolean running = new AtomicBoolean(false);
private static volatile SocketServer instance = null;

private static SocketServer instance = null;

public static synchronized SocketServer getInstance() {
public static SocketServer getInstance() {
if (instance == null)
instance = new SocketServer();
synchronized (SocketServer.class) {
if (instance == null)
instance = new SocketServer();
}
return instance;
}

Expand All @@ -43,6 +47,8 @@ private SocketServer() {
*/
public synchronized void startServer(HostTray hostTray)
throws PortUtils.NoPortAvailableException, PortUtils.ServerCollisionException {
if (running.get())
return;
Logger.info("SocketServer", "Request to start server");
this.port = PortUtils.getAvailablePort(serverPorts);
listener = new Thread(() -> {
Expand All @@ -65,15 +71,19 @@ public synchronized void startServer(HostTray hostTray)
}
});
ProcessPool.getInstance().execute(listener);
running.set(true);
}

/** Stops the server and close all the sessions.
*/
public synchronized void stopServer() {
if (!running.get())
return;
Logger.info("SocketServer", "Request to stop server");
if (listener != null)
listener.interrupt();
sessionList.forEach(SocketSession::close);
running.set(false);
}

@Override
Expand Down
13 changes: 11 additions & 2 deletions core/src/cn/harryh/arkpets/utils/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.varia.LevelRangeFilter;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Arrays;
import java.util.List;

import static cn.harryh.arkpets.Const.charsetDefault;

Expand Down Expand Up @@ -95,6 +97,13 @@ public String getHeader() {
};
}

/** Get log file path
* @return log file path
*/
public static String getLogFilePath() {
return logFilePath;
}

/** Sets a new log level.
* @param level The new level.
*/
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/cn/harryh/arkpets/controllers/RootModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected Boolean call() throws InterruptedException, ExecutionException {
if (!future.get().isSuccess()) {
int exitCode = future.get().exitValue();
Logger.warn("Launcher", "Detected an abnormal finalization of an ArkPets thread (exit code " + exitCode + "). Please check the log file for details.");
lastLaunchFailed = new ProcessPool.UnexpectedExitCodeException(exitCode);
lastLaunchFailed = new ProcessPool.UnexpectedExitCodeException(exitCode, future.get().processId());
return false;
}
Logger.debug("Launcher", "Detected a successful finalization of an ArkPets thread.");
Expand Down
13 changes: 13 additions & 0 deletions desktop/src/cn/harryh/arkpets/guitasks/ZipTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import javafx.scene.layout.StackPane;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class ZipTask extends GuiTask {
Expand All @@ -23,6 +26,16 @@ public ZipTask(StackPane root, GuiTaskStyle style, String zipPath, Map<String, S
this.contents = contents;
}

public ZipTask(StackPane root, GuiTaskStyle style, String zipPath, List<String> contents) {
super(root, style);
this.zipPath = zipPath;
this.contents = contents.stream()
.collect(Collectors.toMap(
path -> path,
path -> Paths.get(path).getFileName().toString()
));
}

@Override
protected String getHeader() {
return "正在创建压缩文件...";
Expand Down
Loading
Loading