Skip to content

Commit

Permalink
refactor: bootstrap launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
litwak913 committed Feb 18, 2025
1 parent 3fe776c commit 253961f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,67 +1,105 @@
/** Copyright (c) 2022-2025, Harry Huang
* At GPL-3.0 License
*/
package cn.harryh.arkpets;

import cn.harryh.arkpets.utils.ArgPending;
import cn.harryh.arkpets.controllers.Titlebar;
import cn.harryh.arkpets.envchecker.WinGraphicsEnvCheckTask;
import cn.harryh.arkpets.platform.WindowSystem;
import cn.harryh.arkpets.utils.ArgPending;
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 com.sun.jna.Platform;
import javafx.application.Application;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.system.Configuration;
import org.lwjgl.system.MemoryUtil;

import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Objects;

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


/** The bootstrap for ArkPets the libGDX app.
* @see ArkPets
*/
public class EmbeddedLauncher {
// Please note that on macOS your application needs to be started with the -XstartOnFirstThread JVM argument

public static void main (String[] args) {
public class BootstrapLauncher {
public static void main(String[] args) {
// Disable assistive technologies
System.setProperty("javax.accessibility.assistive_technologies", "");
ArgPending.argCache = args;
// Logger
Logger.initialize(LogConfig.logCorePath, LogConfig.logCoreMaxKeep);
ArkConfig appConfig = Objects.requireNonNull(ArkConfig.getConfig(), "ArkConfig returns a null instance, please check the config file.");
// Logger
Logger.initialize(Const.LogConfig.logCorePath, Const.LogConfig.logCoreMaxKeep);
try {
Logger.setLevel(appConfig.logging_level);
} catch (Exception ignored) {
}
new ArgPending(LogConfig.errorArg, args) {
new ArgPending(Const.LogConfig.errorArg, args) {
protected void process(String command, String addition) {
Logger.setLevel(Logger.ERROR);
}
};
new ArgPending(LogConfig.warnArg, args) {
new ArgPending(Const.LogConfig.warnArg, args) {
protected void process(String command, String addition) {
Logger.setLevel(Logger.WARN);
}
};
new ArgPending(LogConfig.infoArg, args) {
new ArgPending(Const.LogConfig.infoArg, args) {
protected void process(String command, String addition) {
Logger.setLevel(Logger.INFO);
}
};
new ArgPending(LogConfig.debugArg, args) {
new ArgPending(Const.LogConfig.debugArg, args) {
protected void process(String command, String addition) {
Logger.setLevel(Logger.DEBUG);
}
};
new ArgPending("--load-lib", args) {
// If requested to start the core app directly
new ArgPending("--direct-start", args) {
protected void process(String command, String addition) {
startCore(appConfig);
System.exit(0);
}
};
startDesktop();
}

/** The entrance of the whole program, also the bootstrap for ArkHomeFX.
* @see ArkHomeFX
*/
private static void startDesktop() {
Logger.info("System", "Entering the app of DesktopLauncher");
Logger.info("System", "ArkPets version is " + appVersion);
Logger.debug("System", "Default charset is " + Charset.defaultCharset());
// Change ui style
new ArgPending("--ui-style", ArgPending.argCache) {
@Override
protected void process(String command, String addition) {
Titlebar.forceUiStyle = addition.toLowerCase();
}
};
// Remove NVIDIA settings when uninstall on windows.
new ArgPending("--remove-nvidia", ArgPending.argCache) {
@Override
protected void process(String command, String addition) {
new WinGraphicsEnvCheckTask().removeNvidiaSettings();
}
};
// Disable libdecor to avoid glfw and javafx problem on linux.
if(Platform.isLinux()) GLFW.glfwInitHint(GLFW.GLFW_WAYLAND_LIBDECOR, GLFW.GLFW_WAYLAND_DISABLE_LIBDECOR);
// Java FX bootstrap
Application.launch(ArkHomeFX.class, ArgPending.argCache);
Logger.info("System", "Exited from DesktopLauncher successfully");
System.exit(0);
}

/** The bootstrap for ArkPets the libGDX app.
* @see ArkPets
*/
private static void startCore(ArkConfig appConfig) {
new ArgPending("--load-lib", ArgPending.argCache) {
@Override
protected void process(String command, String addition) {
Logger.info("System", "Loading the specified library \"" + addition +"\"");
Expand All @@ -72,12 +110,12 @@ protected void process(String command, String addition) {
}
}
};
new ArgPending("--enable-snapshot", args) {
new ArgPending("--enable-snapshot", ArgPending.argCache) {
@Override
protected void process(String command, String addition) {
Logger.info("System", "Enable the snapshot feature");
ArkChar.enableSnapshot = true;
File temp = new File(PathConfig.tempDirPath);
File temp = new File(Const.PathConfig.tempDirPath);
if (!(temp.exists() || temp.mkdir())) {
Logger.error("System", "Failed to create the temporary directory.");
}
Expand Down Expand Up @@ -111,7 +149,7 @@ protected void process(String command, String addition) {
System.setProperty("apple.awt.application.name", TITLE);
SwingUtilities.invokeAndWait(Toolkit::getDefaultToolkit);
Configuration.GLFW_CHECK_THREAD0.set(false);
Configuration.GLFW_LIBRARY_NAME.set("glfw_async");
Configuration.GLFW_LIBRARY_NAME.set("/Users/abc/glfwasync.dylib");
}
// Handle GLFW error
GLFW.glfwSetErrorCallback(new GLFWErrorCallback() {
Expand Down
86 changes: 0 additions & 86 deletions desktop/src/cn/harryh/arkpets/DesktopLauncher.java

This file was deleted.

7 changes: 4 additions & 3 deletions desktop/src/cn/harryh/arkpets/controllers/RootModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import cn.harryh.arkpets.ArkConfig;
import cn.harryh.arkpets.ArkHomeFX;
import cn.harryh.arkpets.EmbeddedLauncher;
import cn.harryh.arkpets.BootstrapLauncher;
import cn.harryh.arkpets.concurrent.ProcessPool;
import cn.harryh.arkpets.guitasks.CheckAppUpdateTask;
import cn.harryh.arkpets.guitasks.CheckEnvironmentTask;
Expand Down Expand Up @@ -135,7 +135,7 @@ public void popLoading(EventHandler<ActionEvent> handler) {

/** Runs the EmbeddedLauncher to launch the ArkPets app.
* It will run in multi-threading mode.
* @see EmbeddedLauncher
* @see BootstrapLauncher
*/
public void startArkPetsCore() {
Task<Boolean> task = new Task<>() {
Expand All @@ -155,10 +155,11 @@ protected Boolean call() throws InterruptedException, ExecutionException {
default -> "";
};
args.add(temp);
args.add("--direct-start");
// Start ArkPets core.
Logger.info("Launcher", "Launching " + app.config.character_asset);
Logger.debug("Launcher", "With args " + args);
Future<ProcessPool.ProcessResult> future = ProcessPool.getInstance().submit(EmbeddedLauncher.class, List.of(), args);
Future<ProcessPool.ProcessResult> future = ProcessPool.getInstance().submit(BootstrapLauncher.class, List.of(), args);
// ArkPets core finalized.
if (!future.get().isSuccess()) {
int exitCode = future.get().exitValue();
Expand Down

0 comments on commit 253961f

Please sign in to comment.