2019-12-01 21:16:52 +00:00
|
|
|
/*
|
2020-04-15 19:39:21 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-12-01 21:16:52 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.platform.standalone;
|
|
|
|
|
2020-07-03 00:10:43 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import net.minecrell.terminalconsole.TerminalConsoleAppender;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.core.Appender;
|
|
|
|
import org.apache.logging.log4j.core.Logger;
|
|
|
|
import org.apache.logging.log4j.core.appender.ConsoleAppender;
|
2019-12-01 21:16:52 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-06-10 22:58:29 +00:00
|
|
|
import org.geysermc.connector.bootstrap.GeyserBootstrap;
|
2020-07-03 00:10:43 +00:00
|
|
|
import org.geysermc.connector.common.PlatformType;
|
2020-04-19 23:08:14 +00:00
|
|
|
import org.geysermc.connector.command.CommandManager;
|
2020-07-03 00:10:43 +00:00
|
|
|
import org.geysermc.connector.configuration.GeyserConfiguration;
|
2020-06-27 15:36:48 +00:00
|
|
|
import org.geysermc.connector.dump.BootstrapDumpInfo;
|
2020-06-10 22:58:29 +00:00
|
|
|
import org.geysermc.connector.ping.GeyserLegacyPingPassthrough;
|
2020-07-03 00:10:43 +00:00
|
|
|
import org.geysermc.connector.ping.IGeyserPingPassthrough;
|
2019-12-01 21:16:52 +00:00
|
|
|
import org.geysermc.connector.utils.FileUtils;
|
2020-07-05 23:35:51 +00:00
|
|
|
import org.geysermc.connector.utils.LanguageUtils;
|
2020-04-19 23:08:14 +00:00
|
|
|
import org.geysermc.platform.standalone.command.GeyserCommandManager;
|
2020-07-03 00:10:43 +00:00
|
|
|
import org.geysermc.platform.standalone.gui.GeyserStandaloneGUI;
|
2019-12-01 21:16:52 +00:00
|
|
|
|
2020-04-20 01:56:03 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-07-13 23:20:56 +00:00
|
|
|
import java.lang.reflect.Method;
|
2020-06-20 17:54:40 +00:00
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2020-04-20 01:56:03 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
public class GeyserStandaloneBootstrap implements GeyserBootstrap {
|
2020-04-19 23:08:14 +00:00
|
|
|
|
|
|
|
private GeyserCommandManager geyserCommandManager;
|
2020-05-23 21:50:04 +00:00
|
|
|
private GeyserStandaloneConfiguration geyserConfig;
|
2020-04-29 20:01:53 +00:00
|
|
|
private GeyserStandaloneLogger geyserLogger;
|
2020-05-23 21:50:04 +00:00
|
|
|
private IGeyserPingPassthrough geyserPingPassthrough;
|
2019-12-01 21:16:52 +00:00
|
|
|
|
2020-07-03 00:10:43 +00:00
|
|
|
private GeyserStandaloneGUI gui;
|
|
|
|
|
|
|
|
@Getter
|
2020-07-13 23:20:56 +00:00
|
|
|
private boolean useGui = System.console() == null && !isHeadless();
|
2020-07-03 00:10:43 +00:00
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
private GeyserConnector connector;
|
|
|
|
|
2019-12-01 21:16:52 +00:00
|
|
|
public static void main(String[] args) {
|
2020-07-03 00:10:43 +00:00
|
|
|
for (String arg : args) {
|
|
|
|
// By default, standalone Geyser will check if it should open the GUI based on if the GUI is null
|
|
|
|
// Optionally, you can force the use of a GUI or no GUI by specifying args
|
|
|
|
if (arg.equals("gui")) {
|
|
|
|
new GeyserStandaloneBootstrap().onEnable(true);
|
|
|
|
return;
|
|
|
|
} else if (arg.equals("nogui")) {
|
|
|
|
new GeyserStandaloneBootstrap().onEnable(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 20:01:53 +00:00
|
|
|
new GeyserStandaloneBootstrap().onEnable();
|
2019-12-01 21:16:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 00:10:43 +00:00
|
|
|
public void onEnable(boolean useGui) {
|
|
|
|
this.useGui = useGui;
|
|
|
|
this.onEnable();
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:16:52 +00:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
2020-07-03 00:10:43 +00:00
|
|
|
Logger logger = (Logger) LogManager.getRootLogger();
|
|
|
|
for (Appender appender : logger.getAppenders().values()) {
|
|
|
|
// Remove the appender that is not in use
|
|
|
|
// Prevents multiple appenders/double logging and removes harmless errors
|
|
|
|
if ((useGui && appender instanceof TerminalConsoleAppender) || (!useGui && appender instanceof ConsoleAppender)) {
|
|
|
|
logger.removeAppender(appender);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (useGui && gui == null) {
|
|
|
|
gui = new GeyserStandaloneGUI();
|
|
|
|
gui.redirectSystemStreams();
|
|
|
|
gui.startUpdateThread();
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
geyserLogger = new GeyserStandaloneLogger();
|
2020-04-19 23:08:14 +00:00
|
|
|
|
2020-03-25 05:55:09 +00:00
|
|
|
LoopbackUtil.checkLoopback(geyserLogger);
|
2020-03-24 07:56:04 +00:00
|
|
|
|
2019-12-01 21:16:52 +00:00
|
|
|
try {
|
|
|
|
File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
|
2020-04-29 20:01:53 +00:00
|
|
|
geyserConfig = FileUtils.loadConfig(configFile, GeyserStandaloneConfiguration.class);
|
2020-08-16 17:45:52 +00:00
|
|
|
if (this.geyserConfig.getRemote().getAddress().equalsIgnoreCase("auto")) {
|
|
|
|
geyserConfig.getRemote().setAddress("127.0.0.1");
|
|
|
|
}
|
2019-12-01 21:16:52 +00:00
|
|
|
} catch (IOException ex) {
|
2020-07-05 23:35:51 +00:00
|
|
|
geyserLogger.severe(LanguageUtils.getLocaleStringLog("geyser.config.failed"), ex);
|
2019-12-01 21:16:52 +00:00
|
|
|
System.exit(0);
|
|
|
|
}
|
2020-05-21 03:43:22 +00:00
|
|
|
GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger);
|
2019-12-01 21:16:52 +00:00
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
connector = GeyserConnector.start(PlatformType.STANDALONE, this);
|
2020-04-19 23:08:14 +00:00
|
|
|
geyserCommandManager = new GeyserCommandManager(connector);
|
2020-05-23 21:50:04 +00:00
|
|
|
|
2020-07-03 00:10:43 +00:00
|
|
|
if (gui != null) {
|
|
|
|
gui.setupInterface(geyserLogger, geyserCommandManager);
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:50:04 +00:00
|
|
|
geyserPingPassthrough = GeyserLegacyPingPassthrough.init(connector);
|
|
|
|
|
2020-07-03 00:10:43 +00:00
|
|
|
if (!useGui) {
|
|
|
|
geyserLogger.start(); // Throws an error otherwise
|
|
|
|
}
|
2019-12-01 21:16:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 23:20:56 +00:00
|
|
|
/**
|
|
|
|
* Check using {@link java.awt.GraphicsEnvironment} that we are a headless client
|
|
|
|
*
|
|
|
|
* @return If the current environment is headless
|
|
|
|
*/
|
|
|
|
private boolean isHeadless() {
|
|
|
|
try {
|
|
|
|
Class<?> graphicsEnv = Class.forName("java.awt.GraphicsEnvironment");
|
|
|
|
Method isHeadless = graphicsEnv.getDeclaredMethod("isHeadless");
|
|
|
|
return (boolean) isHeadless.invoke(null);
|
|
|
|
} catch (Exception ignore) { }
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:16:52 +00:00
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2020-03-17 16:43:09 +00:00
|
|
|
connector.shutdown();
|
2019-12-01 21:16:52 +00:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserConfiguration getGeyserConfig() {
|
|
|
|
return geyserConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-29 20:01:53 +00:00
|
|
|
public GeyserStandaloneLogger getGeyserLogger() {
|
2019-12-01 21:16:52 +00:00
|
|
|
return geyserLogger;
|
|
|
|
}
|
2020-04-19 23:08:14 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public CommandManager getGeyserCommandManager() {
|
|
|
|
return geyserCommandManager;
|
|
|
|
}
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGeyserPingPassthrough getGeyserPingPassthrough() {
|
|
|
|
return geyserPingPassthrough;
|
|
|
|
}
|
2020-06-20 17:54:40 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Path getConfigFolder() {
|
|
|
|
// Return the current working directory
|
|
|
|
return Paths.get(System.getProperty("user.dir"));
|
|
|
|
}
|
2020-06-27 15:36:48 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public BootstrapDumpInfo getDumpInfo() {
|
2020-07-30 15:59:42 +00:00
|
|
|
return new GeyserStandaloneDumpInfo(this);
|
2020-06-27 15:36:48 +00:00
|
|
|
}
|
2019-12-01 21:16:52 +00:00
|
|
|
}
|