Skip to content

Commit

Permalink
Prepare for dedicated server distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Mar 10, 2024
1 parent df24c6a commit 0dbac1b
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 39 deletions.
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc;
package com.soulfiremc.client;

import com.soulfiremc.builddata.BuildData;
import com.soulfiremc.client.gui.GUIManager;
Expand Down Expand Up @@ -43,7 +43,7 @@
* This class prepares the earliest work possible, such as loading mixins and setting up logging.
*/
@Slf4j
public class SoulFireBootstrap {
public class SoulFireClientBootstrap {
public static final Instant START_TIME = Instant.now();
public static final PluginManager PLUGIN_MANAGER =
new JarPluginManager(SFPathConstants.PLUGINS_FOLDER);
Expand All @@ -64,7 +64,7 @@ public class SoulFireBootstrap {
}
}

private SoulFireBootstrap() {}
private SoulFireClientBootstrap() {}

@SuppressWarnings("unused")
public static void bootstrap(String[] args, List<ClassLoader> classLoaders) {
Expand Down Expand Up @@ -96,7 +96,7 @@ private static void injectMixinsAndRun(boolean runServer, String[] args) {
});

var classLoaders = new ArrayList<ClassLoader>();
classLoaders.add(SoulFireBootstrap.class.getClassLoader());
classLoaders.add(SoulFireClientBootstrap.class.getClassLoader());
PLUGIN_MANAGER
.getPlugins()
.forEach(pluginWrapper -> classLoaders.add(pluginWrapper.getPluginClassLoader()));
Expand All @@ -122,13 +122,13 @@ private static void postMixinMain(boolean runServer, String[] args) {
var port = getRPCPort();
if (runServer) {
log.info("Starting server on {}:{}", host, port);
SoulFireLoader.runHeadless(host, port, args);
SoulFireClientLoader.runHeadless(host, port, args);
} else {
log.info("Starting GUI and server on {}:{}", host, port);
GUIManager.injectTheme();
GUIManager.loadGUIProperties();

SoulFireLoader.runGUI(host, port);
SoulFireClientLoader.runGUI(host, port);
}
}

Expand Down
@@ -0,0 +1,31 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.client;

import com.soulfiremc.launcher.SoulFireAbstractLauncher;

public class SoulFireClientLauncher extends SoulFireAbstractLauncher {
public static void main(String[] args) {
new SoulFireClientLauncher().run(args);
}

@Override
protected String getBootstrapClassName() {
return "com.soulfiremc.client.SoulFireClientBootstrap";
}
}
Expand Up @@ -15,29 +15,29 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc;
package com.soulfiremc.client;

import com.soulfiremc.client.cli.CLIManager;
import com.soulfiremc.client.grpc.RPCClient;
import com.soulfiremc.client.gui.GUIManager;
import com.soulfiremc.server.SoulFireServer;

public class SoulFireLoader {
private SoulFireLoader() {}
public class SoulFireClientLoader {
private SoulFireClientLoader() {}

public static void runHeadless(String host, int port, String[] args) {
var soulFire = new SoulFireServer(host, port, SoulFireBootstrap.PLUGIN_MANAGER, SoulFireBootstrap.START_TIME);
var soulFire = new SoulFireServer(host, port, SoulFireClientBootstrap.PLUGIN_MANAGER, SoulFireClientBootstrap.START_TIME);

var rpcClient = new RPCClient(host, port, soulFire.generateLocalCliJWT());
var cliManager = new CLIManager(rpcClient, SoulFireBootstrap.PLUGIN_MANAGER);
var cliManager = new CLIManager(rpcClient, SoulFireClientBootstrap.PLUGIN_MANAGER);
cliManager.initCLI(args);
}

public static void runGUI(String host, int port) {
var soulFire = new SoulFireServer(host, port, SoulFireBootstrap.PLUGIN_MANAGER, SoulFireBootstrap.START_TIME);
var soulFire = new SoulFireServer(host, port, SoulFireClientBootstrap.PLUGIN_MANAGER, SoulFireClientBootstrap.START_TIME);

var rpcClient = new RPCClient(host, port, soulFire.generateAdminJWT());
var guiManager = new GUIManager(rpcClient, SoulFireBootstrap.PLUGIN_MANAGER);
var guiManager = new GUIManager(rpcClient, SoulFireClientBootstrap.PLUGIN_MANAGER);
guiManager.initGUI();
}
}
Expand Up @@ -15,28 +15,26 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc;
package com.soulfiremc.launcher;

import com.soulfiremc.util.SFContextClassLoader;
import java.util.List;

/**
* This class only changes the classLoader for the rest of the program. This is so we can merge
* plugin and server classes.
*/
public class SoulFireLauncher {
private static final SFContextClassLoader SF_CONTEXT_CLASS_LOADER = new SFContextClassLoader();
public abstract class SoulFireAbstractLauncher {
private final SFContextClassLoader SF_CONTEXT_CLASS_LOADER = new SFContextClassLoader();

public static void main(String[] args) {
public void run(String[] args) {
Thread.currentThread().setContextClassLoader(SF_CONTEXT_CLASS_LOADER);

try {
SF_CONTEXT_CLASS_LOADER
.loadClass("com.soulfiremc.SoulFireBootstrap")
.loadClass(getBootstrapClassName())
.getDeclaredMethod("bootstrap", String[].class, List.class)
.invoke(null, args, SF_CONTEXT_CLASS_LOADER.childClassLoaders());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

protected abstract String getBootstrapClassName();
}
Expand Up @@ -13,10 +13,10 @@

// Too early to init loggers
@SuppressWarnings("CallToPrintStackTrace")
public class SoulFireJava8LauncherWrapper {
public static void mainWrapper(String[] args, String className) {
public abstract class SoulFireAbstractJava8Launcher {
public void run(String[] args) {
try {
Class.forName(className)
Class.forName(getLauncherClassName())
.getMethod("main", String[].class)
.invoke(null, (Object) args);
} catch (UnsupportedClassVersionError e) {
Expand Down Expand Up @@ -82,4 +82,6 @@ public UnsupportedVersionDialog() {
setVisible(true);
}
}

protected abstract String getLauncherClassName();
}
@@ -0,0 +1,12 @@
package com.soulfiremc.launcher;

public class SoulFireClientJava8Launcher extends SoulFireAbstractJava8Launcher {
public static void main(String[] args) {
new SoulFireClientJava8Launcher().run(args);
}

@Override
protected String getLauncherClassName() {
return "com.soulfiremc.client.SoulFireClientLauncher";
}
}

This file was deleted.

@@ -0,0 +1,12 @@
package com.soulfiremc.launcher;

public class SoulFireDedicatedServerJava8Launcher extends SoulFireAbstractJava8Launcher {
public static void main(String[] args) {
new SoulFireDedicatedServerJava8Launcher().run(args);
}

@Override
protected String getLauncherClassName() {
return "com.soulfiremc.client.SoulFireClientLauncher";
}
}

This file was deleted.

0 comments on commit 0dbac1b

Please sign in to comment.