Skip to content

Commit c091b32

Browse files
committed
refactor!: SPI Refactor
1 parent 05c004a commit c091b32

61 files changed

Lines changed: 346 additions & 182 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,8 @@ allprojects {
3434
javaFXVersion = "17.0.8"
3535
dbusVersion = "5.1.0"
3636
lwjglVersion = "3.3.4"
37-
javaFXPlatform = ""
38-
lwjglPlatform = ""
3937
arkPetsPlatform = ""
4038
lwjglList = ["lwjgl", "lwjgl-opengl","lwjgl-opengles", "lwjgl-stb", "lwjgl-openal", "lwjgl-jemalloc", "lwjgl-glfw"]
41-
def isArm = System.getProperty("os.arch").startsWith("aarch64")
42-
def isX64 = System.getProperty("os.arch").contains("64")
43-
if (forceJavaFXPlatform.empty) {
44-
switch (OperatingSystem.current()) {
45-
case OperatingSystem.LINUX:
46-
javaFXPlatform = "linux"
47-
break
48-
case OperatingSystem.WINDOWS:
49-
javaFXPlatform = isX64 ? "win" : "win-x86"
50-
break
51-
case OperatingSystem.MAC_OS:
52-
javaFXPlatform = isArm ? "mac-aarch64" : "mac"
53-
break
54-
}
55-
} else {
56-
javaFXPlatform = forceJavaFXPlatform
57-
}
58-
if (forceLWJGLPlatform.empty) {
59-
switch (OperatingSystem.current()) {
60-
case OperatingSystem.LINUX:
61-
lwjglPlatform = "natives-linux"
62-
break
63-
case OperatingSystem.WINDOWS:
64-
lwjglPlatform = isX64 ? "natives-windows" : "natives-windows-x86"
65-
break
66-
case OperatingSystem.MAC_OS:
67-
lwjglPlatform = isArm ? "natives-macos-arm64" : "natives-macos"
68-
break
69-
}
70-
} else {
71-
lwjglPlatform = forceLWJGLPlatform
72-
}
7339
if (forceArkPetsPlatform.empty) {
7440
switch (OperatingSystem.current()) {
7541
case OperatingSystem.LINUX:
@@ -99,6 +65,14 @@ println "Building ArkPets for ${arkPetsPlatform}."
9965
project(":desktop") {
10066
dependencies {
10167
implementation project(":core")
68+
compileOnly project(":platform-$arkPetsPlatform")
69+
// JFoenix
70+
api "com.jfoenix:jfoenix:9.0.1"
71+
// CommonMark
72+
api "org.commonmark:commonmark:0.24.0"
73+
api "org.commonmark:commonmark-ext-autolink:0.24.0"
74+
api "org.commonmark:commonmark-ext-gfm-strikethrough:0.24.0"
75+
api "org.commonmark:commonmark-ext-gfm-tables:0.24.0"
10276
// libGDX Desktop
10377
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
10478
api "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
@@ -118,19 +92,13 @@ project(":core") {
11892
api ("com.badlogicgames.gdx:gdx-lwjgl3-angle:$gdxVersion"){
11993
exclude group:"org.lwjgl"
12094
}
121-
// JNA
122-
api "net.java.dev.jna:jna:$jnaVersion"
123-
api "net.java.dev.jna:jna-platform:$jnaVersion"
124-
// JFoenix
125-
api "com.jfoenix:jfoenix:9.0.1"
12695
// FastJson
12796
api "com.alibaba:fastjson:2.0.39"
128-
// CommonMark
129-
api "org.commonmark:commonmark:0.24.0"
130-
api "org.commonmark:commonmark-ext-autolink:0.24.0"
131-
api "org.commonmark:commonmark-ext-gfm-strikethrough:0.24.0"
132-
api "org.commonmark:commonmark-ext-gfm-tables:0.24.0"
133-
api project(":platform-$arkPetsPlatform")
97+
// LWJGL
98+
lwjglList.each {
99+
api "org.lwjgl:${it}:${lwjglVersion}"
100+
}
101+
api "org.lwjgl:lwjgl-egl:${lwjglVersion}"
134102
// reload4j
135103
api "ch.qos.reload4j:reload4j:1.2.26"
136104
// TiniPinyin

core/src/cn/harryh/arkpets/Const.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import cn.harryh.arkpets.platform.HWndCtrl.NumberedTitleManager;
77
import cn.harryh.arkpets.utils.Logger;
88
import cn.harryh.arkpets.utils.Version;
9-
import com.sun.jna.Platform;
10-
import javafx.util.Duration;
119

1210
import javax.swing.*;
1311
import java.awt.*;
@@ -49,11 +47,6 @@ public final class Const {
4947
public static final int behaviorBaseWeight = 320;
5048
public static final float droppedThreshold = 10f;
5149

52-
// Duration presets
53-
public static final Duration durationFast = new Duration(150);
54-
public static final Duration durationNormal = new Duration(300);
55-
public static final Duration durationLong = new Duration(1500);
56-
5750
// Encoding presets
5851
public static final String charsetDefault = "UTF-8";
5952

@@ -78,11 +71,11 @@ public final class Const {
7871
public static final int reconnectDelayMillis = 5 * 1000;
7972

8073
// Platform constants
81-
public static boolean isWindows = Platform.isWindows();
82-
public static boolean isMac = Platform.isMac();
83-
public static boolean isLinux = Platform.isLinux();
84-
public static boolean isIntel = Platform.isIntel();
85-
public static boolean isARM = Platform.isARM();
74+
public static boolean isWindows;
75+
public static boolean isMac;
76+
public static boolean isLinux;
77+
public static boolean isIntel;
78+
public static boolean isARM;
8679

8780
// Misc constants
8881
public static final Pattern ipPortRegex = Pattern.compile(
@@ -101,6 +94,28 @@ public final class Const {
10194
public static final String gnomePluginName = "arkpets-integration@harryh.cn";
10295
public static final String kdePluginName = "ArkPetsIntegration";
10396

97+
static {
98+
String osName = System.getProperty("os.name");
99+
String arch = System.getProperty("os.arch").toLowerCase().trim();
100+
if ("i386".equals(arch) || "i686".equals(arch)) {
101+
arch = "x86";
102+
} else if ("x86_64".equals(arch) || "amd64".equals(arch)) {
103+
arch = "x86-64";
104+
}
105+
if (arch.startsWith("x86")) {
106+
isIntel = true;
107+
} if (arch.startsWith("arm") || arch.startsWith("aarch")) {
108+
isARM = true;
109+
}
110+
if (osName.startsWith("Linux")) {
111+
isLinux = true;
112+
} else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
113+
isMac = true;
114+
} else if (osName.startsWith("Windows")) {
115+
isWindows = true;
116+
}
117+
}
118+
104119
/** Paths presets definition class.
105120
*/
106121
public static class PathConfig {
@@ -142,20 +157,13 @@ public static class LogConfig {
142157
/** Fonts provider class.
143158
*/
144159
public static class FontsConfig {
145-
private static final String fontFileRegular = "/fonts/SourceHanSansCN-Regular.otf";
146-
private static final String fontFileBold = "/fonts/SourceHanSansCN-Bold.otf";
147-
148-
public static void loadFontsToJavafx() {
149-
javafx.scene.text.Font.loadFont(FontsConfig.class.getResourceAsStream(fontFileRegular),
150-
javafx.scene.text.Font.getDefault().getSize());
151-
javafx.scene.text.Font.loadFont(FontsConfig.class.getResourceAsStream(fontFileBold),
152-
javafx.scene.text.Font.getDefault().getSize());
153-
}
160+
public static final String fontFileRegular = "/fonts/SourceHanSansCN-Regular.otf";
161+
public static final String fontFileBold = "/fonts/SourceHanSansCN-Bold.otf";
154162

155163
public static void loadFontsToSwing() {
156164
try {
157165
InputStream in = Objects.requireNonNull(FontsConfig.class.getResourceAsStream(fontFileRegular));
158-
java.awt.Font font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, in);
166+
Font font = Font.createFont(Font.TRUETYPE_FONT, in);
159167
if (font != null) {
160168
UIManager.put("Label.font", font.deriveFont(10f).deriveFont(Font.ITALIC));
161169
UIManager.put("Menu.font", font.deriveFont(11f));

desktop/src/cn/harryh/arkpets/guitasks/envchecker/ConfirmTestCheckTask.java renamed to core/src/cn/harryh/arkpets/envchecker/ConfirmTestCheckTask.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
package cn.harryh.arkpets.guitasks.envchecker;
1+
package cn.harryh.arkpets.envchecker;
22

33

44
public class ConfirmTestCheckTask extends EnvCheckTask {
5+
@Override
6+
public boolean shouldRun() {
7+
return true;
8+
}
9+
10+
@Override
11+
public void init() {
12+
13+
}
14+
515
@Override
616
public String getFailureReason() {
717
return "";

desktop/src/cn/harryh/arkpets/guitasks/envchecker/EnvCheckTask.java renamed to core/src/cn/harryh/arkpets/envchecker/EnvCheckTask.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
package cn.harryh.arkpets.guitasks.envchecker;
1+
package cn.harryh.arkpets.envchecker;
22

3-
import cn.harryh.arkpets.Const;
3+
import cn.harryh.arkpets.utils.Logger;
44

55
import java.util.ArrayList;
66
import java.util.List;
7+
import java.util.ServiceLoader;
78

89

910
public abstract class EnvCheckTask {
11+
public abstract boolean shouldRun();
12+
13+
public abstract void init();
14+
1015
public abstract String getFailureReason();
1116

1217
public abstract String getFailureDetail();
@@ -38,25 +43,15 @@ public String toString() {
3843
public static List<EnvCheckTask> getAvailableTasks() {
3944
ArrayList<EnvCheckTask> list = new ArrayList<>();
4045
list.add(new SleepEnvCheckTask(1000));
41-
if (Const.isWindows) {
42-
list.add(new WinGraphicsEnvCheckTask());
43-
}
44-
if (Const.isLinux) {
45-
String desktop = System.getenv("XDG_CURRENT_DESKTOP");
46-
String type = System.getenv("XDG_SESSION_TYPE");
47-
if (desktop != null && type != null) {
48-
if (type.equals("wayland") && desktop.equals("GNOME")) {
49-
list.add(new GNOMEPluginCheckTask());
50-
} else if (type.equals("wayland") && desktop.equals("KDE")) {
51-
list.add(new KWinPluginCheckTask());
52-
} else {
53-
list.add(new X11CompositorCheckTask());
54-
}
46+
ServiceLoader<EnvCheckTask> loader = ServiceLoader.load(EnvCheckTask.class);
47+
loader.forEach(task -> {
48+
Logger.debug("EnvCheck", "Found EnvCheckTask " + task.getClass().getName());
49+
if(task.shouldRun()) {
50+
Logger.debug("EnvCheck", "Add EnvCheckTask "+task.getClass().getName());
51+
list.add(task);
52+
task.init();
5553
}
56-
}
57-
if (Const.isMac) {
58-
list.add(new AccessibilityCheckTask());
59-
}
54+
});
6055
return list;
6156
}
6257
}

desktop/src/cn/harryh/arkpets/guitasks/envchecker/SleepEnvCheckTask.java renamed to core/src/cn/harryh/arkpets/envchecker/SleepEnvCheckTask.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.harryh.arkpets.guitasks.envchecker;
1+
package cn.harryh.arkpets.envchecker;
22

33

44
public class SleepEnvCheckTask extends EnvCheckTask {
@@ -18,6 +18,16 @@ public boolean run() {
1818
return true;
1919
}
2020

21+
@Override
22+
public boolean shouldRun() {
23+
return true;
24+
}
25+
26+
@Override
27+
public void init() {
28+
29+
}
30+
2131
@Override
2232
public String getFailureReason() {
2333
return "";

core/src/cn/harryh/arkpets/platform/HWndCtrlFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55

66
public abstract class HWndCtrlFactory {
7+
/** Initializes the platform factory.
8+
*/
9+
public abstract void init();
10+
711
/** Finds a window.
812
* @param className The window's class name.
913
* @param windowText The window's title.
@@ -38,4 +42,8 @@ public abstract class HWndCtrlFactory {
3842
/** Return current WindowSystem should enable decoration.
3943
*/
4044
public abstract boolean needDecorated();
45+
46+
/** Return the WindowSystem type of HWndCtrlFactory.
47+
*/
48+
public abstract WindowSystem getType();
4149
}

core/src/cn/harryh/arkpets/platform/NullHWndCtrlFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
public class NullHWndCtrlFactory extends HWndCtrlFactory{
77
private boolean startupFind;
88

9+
@Override
10+
public void init() {
11+
12+
}
13+
914
@Override
1015
public HWndCtrl findWindow(String className, String windowText) {
1116
if (windowText.equals("ArkPets")) {
@@ -46,4 +51,9 @@ public boolean needResize() {
4651
public boolean needDecorated() {
4752
return true;
4853
}
54+
55+
@Override
56+
public WindowSystem getType() {
57+
return WindowSystem.NULL;
58+
}
4959
}

core/src/cn/harryh/arkpets/platform/WindowSystem.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import cn.harryh.arkpets.utils.Logger;
88

99
import java.util.List;
10+
import java.util.ServiceLoader;
1011

1112

1213
public enum WindowSystem {
@@ -53,14 +54,18 @@ public static void init(WindowSystem platform) {
5354
PLATFORM = detectWindowSystem();
5455
}
5556
Logger.info("System", "Using " + PLATFORM.toString() + " Window System");
56-
switch (PLATFORM) {
57-
case USER32 -> factory = new User32HWndCtrlFactory();
58-
case MUTTER -> factory = new MutterHWndCtrlFactory();
59-
case KWIN -> factory = new KWinHWndCtrlFactory();
60-
case X11 -> factory = new X11HWndCtrlFactory();
61-
case QUARTZ -> factory = new QuartzHWndCtrlFactory();
62-
case NULL -> factory = new NullHWndCtrlFactory();
57+
ServiceLoader<HWndCtrlFactory> loader = ServiceLoader.load(HWndCtrlFactory.class);
58+
for (HWndCtrlFactory fac : loader) {
59+
String name = fac.getClass().getName();
60+
Logger.debug("System", "Found HWndCtrlFactory " + name);
61+
if(fac.getType() == PLATFORM) {
62+
Logger.debug("System", "Using factory " + name);
63+
factory = fac;
64+
factory.init();
65+
return;
66+
}
6367
}
68+
throw new RuntimeException("Factory for Window System " + PLATFORM.toString() + " not found");
6469
}
6570

6671
/** Get current WindowSystem.
@@ -104,6 +109,7 @@ public static HWndCtrl.MousePoint getMousePos() {
104109
/** Frees all the resources.
105110
*/
106111
public static void free() {
112+
if(factory == null) return;
107113
factory.free();
108114
factory = null;
109115
}

desktop/src/cn/harryh/arkpets/startup/NullStartupConfig.java renamed to core/src/cn/harryh/arkpets/startup/NullStartupConfig.java

File renamed without changes.

desktop/src/cn/harryh/arkpets/startup/StartupConfig.java renamed to core/src/cn/harryh/arkpets/startup/StartupConfig.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
*/
44
package cn.harryh.arkpets.startup;
55

6-
import cn.harryh.arkpets.Const;
7-
import com.sun.jna.Platform;
6+
import cn.harryh.arkpets.utils.Logger;
7+
8+
import java.util.ServiceLoader;
89

910

1011
public abstract class StartupConfig {
1112
/** Gets the platform StartupConfig.
1213
* @return platform StartupConfig.
1314
*/
1415
public static StartupConfig getInstance() {
15-
if (Const.isWindows) {
16-
return new WindowsStartupConfig();
17-
} else if (Const.isLinux) {
18-
return new XDGStartupConfig();
19-
} else if (Const.isMac) {
20-
return new LaunchdStartupConfig();
16+
ServiceLoader<StartupConfig> loader = ServiceLoader.load(StartupConfig.class);
17+
var startupOptional = loader.findFirst();
18+
if(startupOptional.isPresent()) {
19+
var startup = startupOptional.get();
20+
Logger.debug("System", "Found StartupConfig "+ startup.getClass().getName());
21+
return startup;
2122
}
23+
Logger.warn("System", "No StartupConfig found.");
2224
return new NullStartupConfig();
2325
}
2426

0 commit comments

Comments
 (0)