Skip to content

Commit

Permalink
refactor: #78 and #79
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Oct 4, 2024
1 parent f16ccc1 commit a63734b
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test*/

# Exclude runtime files
/ArkPetsConfig.json
/models_data.json
/assets/models
/assets/logs
/assets/ArkPetsConfig.json
Expand Down
2 changes: 1 addition & 1 deletion assets/ArkPetsConfigDefault.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"background_color":"#00000000",
"behavior_ai_activation":8,
"behavior_allow_interact":true,
"behavior_allow_sit":true,
"behavior_allow_walk":true,
"behavior_do_peer_repulsion":true,
"canvas_color":"#00000000",
"canvas_fitting_samples":16,
"character_asset":"",
"character_files":{},
Expand Down
2 changes: 1 addition & 1 deletion assets/UI/SettingsModule.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</HBox>
<HBox>
<Label text="背景颜色"/>
<JFXComboBox fx:id="configBackgroundColor" prefWidth="120.0"/>
<JFXComboBox fx:id="configCanvasColor" prefWidth="120.0"/>
</HBox>
<Separator/>
<Label styleClass="config-group-title" text="高级设置"/>
Expand Down
3 changes: 1 addition & 2 deletions core/src/cn/harryh/arkpets/ArkChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public ArkChar(ArkConfig config, float scale) {
camera.setMinInsert(canvasReserveLength - canvasMaxSize);
batch = new TwoColorPolygonBatch();
renderer = new SkeletonRenderer();
Color backgroundColor = config.getBackgroundColor();
/* Pre-multiplied alpha shouldn't be applied to models released in Arknights 2.1.41 or later,
otherwise you may get a corrupted rendering result. */
renderer.setPremultipliedAlpha(false);
Expand Down Expand Up @@ -123,7 +122,7 @@ protected void onApply(AnimData playing) {
}
};
// 6.Canvas setup
setCanvas(backgroundColor);
setCanvas(config.getCanvasColor());
stageInsertMap = new HashMap<>();
for (AnimStage stage : animList.clusterByStage().keySet()) {
// Figure out the suitable canvas size
Expand Down
21 changes: 11 additions & 10 deletions core/src/cn/harryh/arkpets/ArkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public class ArkConfig implements Serializable {


// Config items and default values:
/** @since ArkPets 3.3 */ @JSONField(defaultValue = "#00000000")
public String background_color;
/** @since ArkPets 1.0 */ @JSONField(defaultValue = "8")
public int behavior_ai_activation;
/** @since ArkPets 1.0 */ @JSONField(defaultValue = "true")
Expand All @@ -51,6 +49,8 @@ public class ArkConfig implements Serializable {
public boolean behavior_allow_walk;
/** @since ArkPets 1.6 */ @JSONField(defaultValue = "true")
public boolean behavior_do_peer_repulsion;
/** @since ArkPets 3.3 */ @JSONField(defaultValue = "#00000000")
public String canvas_color;
/** @since ArkPets 3.1 */ @JSONField(defaultValue = "16")
public int canvas_fitting_samples;
/** @since ArkPets 2.0 */ @JSONField()
Expand Down Expand Up @@ -114,18 +114,19 @@ public boolean isNewcomer() {
return isNewcomer;
}

/** Returns converted background color.
/** Returns the background color of the canvas.
* @see com.badlogic.gdx.graphics.Color
*/
@JSONField(serialize = false)
public Color getBackgroundColor() {
Color backgroundColor;
if (background_color.matches("^#[0-9a-fA-F]{8}$")) {
backgroundColor = Color.valueOf(background_color);
public Color getCanvasColor() {
Color color;
if (hexColorRegex.matcher(canvas_color).matches()) {
color = Color.valueOf(canvas_color);
} else {
Logger.warn("System", "Invalid background color,using transparent");
backgroundColor = Color.CLEAR;
Logger.warn("Config", "Invalid color config, using transparent");
color = Color.CLEAR;
}
return backgroundColor;
return color;
}

/** Gets the custom ArkConfig object by reading the external config file.
Expand Down
4 changes: 2 additions & 2 deletions core/src/cn/harryh/arkpets/ArkPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void create() {
config = Objects.requireNonNull(ArkConfig.getConfig(), "ArkConfig returns a null instance, please check the config file.");
Gdx.input.setInputProcessor(this);
Gdx.graphics.setForegroundFPS(config.display_fps);
Logger.info("System", "OpenGL Version " + Gdx.gl.glGetString(GL20.GL_VERSION));
Logger.info("System", "OpenGL Vendor " + Gdx.gl.glGetString(GL20.GL_VENDOR));
Logger.debug("App", "OpenGL version is " + Gdx.gl.glGetString(GL20.GL_VERSION));
Logger.debug("App", "OpenGL vendor is " + Gdx.gl.glGetString(GL20.GL_VENDOR));

// 2.Character setup
Logger.info("App", "Using model asset \"" + config.character_asset + "\"");
Expand Down
7 changes: 5 additions & 2 deletions core/src/cn/harryh/arkpets/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.regex.Pattern;


/** Constants definition class.
Expand Down Expand Up @@ -78,8 +79,10 @@ public final class Const {
public static final int reconnectDelayMillis = 5 * 1000;

// Misc constants
public static String ipPortRegex = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?):\\d{1,5}$";

public static final Pattern ipPortRegex = Pattern.compile(
"^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?):\\d{1,5}$");
public static final Pattern hexColorRegex = Pattern.compile(
"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$");

/** Paths presets definition class.
*/
Expand Down
9 changes: 8 additions & 1 deletion core/src/cn/harryh/arkpets/concurrent/PortUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public static int getServerPort(int[] expectedPorts)
if (socketData.operation == SocketData.Operation.HANDSHAKE_RESPONSE)
return serverPort;
} catch (JSONException ignored) {
Logger.error("SocketServer", String.format("Port %d is already occupied and is not occupied by ArkPets", serverPort));
Logger.warn("SocketServer", "Port " + serverPort + " responded with an invalid content");
} catch (IOException ignored) {
Logger.warn("SocketServer", "Port " + serverPort + " is inaccessible");
}
}
throw new NoServerRunningException();
Expand Down Expand Up @@ -67,20 +68,26 @@ public static int getAvailablePort(int[] expectedPorts)
}


/** This exception indicates that there is no port in idle.
*/
public static class NoPortAvailableException extends IllegalStateException {
public NoPortAvailableException() {
super("No port is available.");
}
}


/** This exception indicates that a server is already running.
*/
public static class ServerCollisionException extends IllegalStateException {
public ServerCollisionException() {
super("A server is already running.");
}
}


/** This exception indicates that no running server is found.
*/
public static class NoServerRunningException extends IllegalStateException {
public NoServerRunningException() {
super("No running server is found.");
Expand Down
3 changes: 2 additions & 1 deletion desktop/src/cn/harryh/arkpets/EmbeddedLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.harryh.arkpets.utils.Logger;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.system.MemoryUtil;
Expand Down Expand Up @@ -74,7 +75,7 @@ protected void process(String command, String addition) {
// Configure window display
config.setInitialVisible(true);
config.setTransparentFramebuffer(true);
config.setInitialBackgroundColor(appConfig.getBackgroundColor());
config.setInitialBackgroundColor(Color.CLEAR);
// Handle GLFW error
GLFW.glfwSetErrorCallback(new GLFWErrorCallback() {
@Override
Expand Down
16 changes: 8 additions & 8 deletions desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class SettingsModule implements Controller<ArkHomeFX> {
@FXML
private JFXComboBox<NamedItem<Integer>> configRenderOutline;
@FXML
private JFXComboBox<NamedItem<Integer>> configBackgroundColor;
private JFXComboBox<NamedItem<Integer>> configCanvasColor;
@FXML
private JFXCheckBox configWindowTopmost;
@FXML
Expand Down Expand Up @@ -143,13 +143,13 @@ public String getContent() {
app.config.display_render_outline = newValue.value();
app.config.save();
});
new ComboBoxSetup<>(configBackgroundColor).setItems(new NamedItem<>("透明", 0x00000000),
new NamedItem<>("绿色", 0x00ff00ff),
new NamedItem<>("蓝色", 0x0000ffff),
new NamedItem<>("品红色", 0xff00ffff))
.selectValue(Color.rgba8888(app.config.getBackgroundColor()), app.config.background_color + "(自定义)")
new ComboBoxSetup<>(configCanvasColor).setItems(new NamedItem<>("透明", 0x00000000),
new NamedItem<>("绿色", 0x00FF00FF),
new NamedItem<>("蓝色", 0x0000FFFF),
new NamedItem<>("品红色", 0xFF00FFFF))
.selectValue(Color.rgba8888(app.config.getCanvasColor()), app.config.canvas_color + "(自定义)")
.setOnNonNullValueUpdated((observable, oldValue, newValue) -> {
app.config.background_color = String.format("#%08X", newValue.value());
app.config.canvas_color = String.format("#%08X", newValue.value());
app.config.save();
});
configWindowTopmost.setSelected(app.config.window_style_topmost);
Expand Down Expand Up @@ -201,7 +201,7 @@ else if (args.contains(Const.LogConfig.debugArg))
System.setProperty("https.proxyHost", "");
System.setProperty("https.proxyPort", "");
} else {
if (newValue.matches(ipPortRegex)) {
if (ipPortRegex.matcher(newValue).matches()) {
String[] ipPort = newValue.split(":");
System.setProperty("http.proxyHost", ipPort[0]);
System.setProperty("http.proxyPort", ipPort[1]);
Expand Down

0 comments on commit a63734b

Please sign in to comment.