Skip to content

Commit

Permalink
Prepare for text dialog and work on logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Oct 2, 2023
1 parent b80aa36 commit 1844758
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public CompletableFuture<Void> start(SettingsHolder settingsHolder) {
var accountSettings = settingsHolder.get(AccountSettings.class);
var proxySettings = settingsHolder.get(ProxySettings.class);

serverWrecker.setupLogging(settingsHolder.get(DevSettings.class));
serverWrecker.setupLoggingAndVia(settingsHolder.get(DevSettings.class));

this.attackState = AttackState.RUNNING;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ private void initPlugins(Path pluginDir) {
}

@SuppressWarnings("UnstableApiUsage")
public void setupLogging(DevSettings devSettings) {
public void setupLoggingAndVia(DevSettings devSettings) {
Via.getManager().debugHandler().setEnabled(devSettings.viaDebug());
setupLogging(devSettings);
}

public static void setupLogging(DevSettings devSettings) {
var level = devSettings.coreDebug() ? Level.DEBUG : Level.INFO;
var nettyLevel = devSettings.nettyDebug() ? Level.DEBUG : Level.INFO;
var grpcLevel = devSettings.grpcDebug() ? Level.DEBUG : Level.INFO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.pistonmaster.serverwrecker.grpc.RPCClient;
import net.pistonmaster.serverwrecker.gui.GUIManager;
import net.pistonmaster.serverwrecker.gui.theme.ThemeUtil;
import net.pistonmaster.serverwrecker.settings.DevSettings;
import org.fusesource.jansi.AnsiConsole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,14 +44,14 @@ private ServerWreckerLoader() {
}

public static void injectJvm() {
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) ->
LOGGER.error("Exception in thread {}", thread.getName(), throwable));

if (System.console() != null) {
AnsiConsole.systemInstall();
}

Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
LOGGER.error("Exception in thread {}", thread.getName(), throwable);
throwable.printStackTrace();
});
ServerWrecker.setupLogging(DevSettings.DEFAULT);
}

public static void injectTheme() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import javax.swing.*;
import java.awt.*;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -77,7 +76,7 @@ public void setAppTitle() {

CLASS_NAME_VARIABLE.set("ServerWrecker");
} catch (Exception e) {
e.printStackTrace();
logger.error("Failed to set app title!", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import javafx.stage.FileChooser;
import net.pistonmaster.serverwrecker.ServerWrecker;
import net.pistonmaster.serverwrecker.auth.AccountRegistry;
import net.pistonmaster.serverwrecker.auth.AccountSettings;
import net.pistonmaster.serverwrecker.auth.AuthType;
import net.pistonmaster.serverwrecker.auth.MinecraftAccount;
Expand All @@ -46,7 +45,7 @@

public class AccountPanel extends NavigationItem implements SettingsDuplex<AccountSettings> {
private static final Logger LOGGER = LoggerFactory.getLogger(AccountPanel.class);
private final JTextField nameFormat;
private final JTextField nameFormat = new JTextField(AccountSettings.DEFAULT_NAME_FORMAT);
private final JCheckBox shuffleAccounts = new PresetJCheckBox(AccountSettings.DEFAULT_SHUFFLE_ACCOUNTS);

@Inject
Expand Down Expand Up @@ -76,7 +75,6 @@ public AccountPanel(ServerWrecker serverWrecker, GUIFrame parent) {
accountSettingsPanel.add(shuffleAccounts);

accountSettingsPanel.add(new JLabel("Name Format: "));
nameFormat = new JTextField(AccountSettings.DEFAULT_NAME_FORMAT);
accountSettingsPanel.add(nameFormat);

accountOptionsPanel.add(accountSettingsPanel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* ServerWrecker
*
* Copyright (C) 2023 ServerWrecker
*
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.gui.popups;

import javafx.stage.FileChooser;
import net.pistonmaster.serverwrecker.ServerWrecker;
import net.pistonmaster.serverwrecker.gui.GUIFrame;
import net.pistonmaster.serverwrecker.gui.libs.JFXFileHelper;
import net.pistonmaster.serverwrecker.gui.libs.SwingTextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Consumer;

public class ImportTextDialog extends JPopupMenu {
private static final Logger LOGGER = LoggerFactory.getLogger(ImportTextDialog.class);

public ImportTextDialog(String loadText, String typeText, ServerWrecker serverWrecker, GUIFrame frame, Consumer<String> consumer) {
setBorder(new EmptyBorder(10, 10, 10, 10));

var button = new JButton(SwingTextUtils.htmlCenterText(loadText));

var chooser = new FileChooser();
chooser.setInitialDirectory(Path.of(System.getProperty("user.dir")).toFile());
chooser.setTitle(loadText);
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(typeText, "*.txt"));

button.addActionListener(new ImportFileListener(serverWrecker, frame, chooser, consumer));
}

private record ImportFileListener(ServerWrecker serverWrecker, GUIFrame frame,
FileChooser chooser, Consumer<String> consumer) implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
var accountFile = JFXFileHelper.showOpenDialog(chooser);
if (accountFile == null) {
return;
}

LOGGER.info("Opening: {}", accountFile.getFileName());
serverWrecker.getThreadPool().submit(() -> {
try {
consumer.accept(Files.readString(accountFile));
} catch (Throwable e) {
LOGGER.error("Failed to load accounts!", e);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public record DevSettings(boolean viaDebug, boolean nettyDebug, boolean grpcDebu
public static final boolean DEFAULT_NETTY_DEBUG = false;
public static final boolean DEFAULT_GRPC_DEBUG = false;
public static final boolean DEFAULT_CORE_DEBUG = false;
public static DevSettings DEFAULT = new DevSettings(DEFAULT_VIA_DEBUG, DEFAULT_NETTY_DEBUG, DEFAULT_GRPC_DEBUG, DEFAULT_CORE_DEBUG);
}

0 comments on commit 1844758

Please sign in to comment.