Fixed CWD issues on Windows

Fixes issues where ViaProxy wouldn't run when double-clicking the jar file
This commit is contained in:
RaphiMC 2024-07-03 11:38:34 +02:00
parent 0e5faf0e64
commit 079e27c840
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
10 changed files with 101 additions and 28 deletions

View File

@ -53,6 +53,7 @@ import net.raphimc.viaproxy.ui.SplashScreen;
import net.raphimc.viaproxy.ui.ViaProxyWindow;
import net.raphimc.viaproxy.util.AddressUtil;
import net.raphimc.viaproxy.util.ClassLoaderPriorityUtil;
import net.raphimc.viaproxy.util.JarUtil;
import net.raphimc.viaproxy.util.logging.Logger;
import javax.swing.*;
@ -61,6 +62,9 @@ import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
@ -70,6 +74,7 @@ public class ViaProxy {
public static final String IMPL_VERSION = "${impl_version}";
public static final LambdaManager EVENT_MANAGER = LambdaManager.threadSafe(new LambdaMetaFactoryGenerator(JavaBypass.TRUSTED_LOOKUP));
private static /*final*/ File CWD;
private static /*final*/ PluginManager PLUGIN_MANAGER;
private static /*final*/ SaveManager SAVE_MANAGER;
private static /*final*/ ViaProxyConfig CONFIG;
@ -107,17 +112,37 @@ public class ViaProxy {
}
public static void injectedMain(final String injectionMethod, final String[] args) throws InterruptedException, IOException, InvocationTargetException {
Logger.setup();
final boolean useUI = args.length == 0 && !GraphicsEnvironment.isHeadless();
final boolean useConfig = args.length == 2 && args[0].equals("config");
final boolean useCLI = args.length > 0 && args[0].equals("cli");
if (!useUI && !useConfig && !useCLI) {
String fileName = "ViaProxy.jar";
try {
fileName = new File(ViaProxy.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getName();
} catch (Throwable ignored) {
final List<File> potentialCwds = new ArrayList<>();
potentialCwds.add(new File(System.getProperty("user.dir")));
potentialCwds.add(new File("."));
JarUtil.getJarFile().map(File::getParentFile).ifPresent(potentialCwds::add);
final List<File> failedCwds = new ArrayList<>();
for (File potentialCwd : potentialCwds) {
if (potentialCwd.isDirectory()) {
if (Files.isWritable(potentialCwd.toPath())) {
CWD = potentialCwd;
break;
}
}
failedCwds.add(potentialCwd);
}
if (CWD != null) {
System.setProperty("user.dir", CWD.getAbsolutePath());
} else if (useUI) {
JOptionPane.showMessageDialog(null, "Could not find a suitable directory to use as working directory. Make sure that the current folder is writeable.", "ViaProxy", JOptionPane.ERROR_MESSAGE);
System.exit(1);
} else {
throw new IllegalStateException("Could not find a suitable directory to use as working directory. Make sure that the current folder is writeable.");
}
Logger.setup();
if (!useUI && !useConfig && !useCLI) {
final String fileName = JarUtil.getJarFile().map(File::getName).orElse("ViaProxy.jar");
Logger.LOGGER.info("Usage: java -jar " + fileName + " | Starts ViaProxy in graphical mode if available");
Logger.LOGGER.info("Usage: java -jar " + fileName + " config <config file> | Starts ViaProxy with the specified config file");
Logger.LOGGER.info("Usage: java -jar " + fileName + " cli --help | Starts ViaProxy in CLI mode");
@ -127,7 +152,13 @@ public class ViaProxy {
Logger.LOGGER.info("Initializing ViaProxy {} v{} ({}) (Injected using {})...", useUI ? "GUI" : "CLI", VERSION, IMPL_VERSION, injectionMethod);
Logger.LOGGER.info("Using java version: " + System.getProperty("java.vm.name") + " " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ") on " + System.getProperty("os.name"));
Logger.LOGGER.info("Available memory (bytes): " + Runtime.getRuntime().maxMemory());
Logger.LOGGER.info("Working directory: " + System.getProperty("user.dir"));
Logger.LOGGER.info("Working directory: " + CWD.getAbsolutePath());
if (!failedCwds.isEmpty()) {
Logger.LOGGER.warn("Failed to use the following directories as working directory:");
for (File failedCwd : failedCwds) {
Logger.LOGGER.warn("\t- " + failedCwd.getAbsolutePath());
}
}
if (System.getProperty("ignoreSystemRequirements") == null) {
if ("32".equals(System.getProperty("sun.arch.data.model")) && Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
Logger.LOGGER.fatal("ViaProxy is not able to run on 32bit Java.");
@ -179,7 +210,7 @@ public class ViaProxy {
if (useConfig) {
viaProxyConfigFile = new File(args[1]);
} else {
viaProxyConfigFile = new File("viaproxy.yml");
viaProxyConfigFile = new File(ViaProxy.getCwd(), "viaproxy.yml");
}
final boolean firstStart = !viaProxyConfigFile.exists();
@ -275,6 +306,10 @@ public class ViaProxy {
CLIENT_CHANNELS = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
}
public static File getCwd() {
return CWD;
}
public static PluginManager getPluginManager() {
return PLUGIN_MANAGER;
}

View File

@ -41,7 +41,7 @@ import java.util.Map;
public class PluginManager {
public static final File PLUGINS_DIR = new File("plugins");
public static final File PLUGINS_DIR = new File(ViaProxy.getCwd(), "plugins");
private final Yaml yaml = new Yaml();
private final IClassProvider rootClassProvider = new GuavaClassPathProvider();

View File

@ -69,7 +69,7 @@ public class ProtocolTranslator {
}
private static void patchConfigs() {
final File configFolder = new File("ViaLoader");
final File configFolder = new File(ViaProxy.getCwd(), "ViaLoader");
configFolder.mkdirs();
try {

View File

@ -29,7 +29,7 @@ import java.util.UUID;
public class ViaProxyViaVersionPlatformImpl extends ViaVersionPlatformImpl {
public ViaProxyViaVersionPlatformImpl() {
super(null);
super(ViaProxy.getCwd());
}
@Override

View File

@ -21,6 +21,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.lenni0451.reflect.stream.RStream;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.saves.impl.AccountsSaveV3;
import net.raphimc.viaproxy.saves.impl.UISave;
import net.raphimc.viaproxy.util.logging.Logger;
@ -31,7 +32,7 @@ import java.io.FileWriter;
public class SaveManager {
private static final File SAVE_FILE = new File("saves.json");
private static final File SAVE_FILE = new File(ViaProxy.getCwd(), "saves.json");
private static final Gson GSON = new Gson();
public final AccountsSaveV3 accountsSave = new AccountsSaveV3();

View File

@ -26,11 +26,11 @@ import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.ui.I18n;
import net.raphimc.viaproxy.ui.ViaProxyWindow;
import net.raphimc.viaproxy.ui.popups.DownloadPopup;
import net.raphimc.viaproxy.util.JarUtil;
import net.raphimc.viaproxy.util.logging.Logger;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@ -77,7 +77,7 @@ public class UpdateCheckTask implements Runnable {
}
if (updateAvailable) {
Logger.LOGGER.warn("You are running an outdated version of ViaProxy! Latest version: " + latestVersion);
if (this.hasUI) {
if (this.hasUI && JarUtil.getJarFile().isPresent()) {
final boolean runsJava8 = System.getProperty("java.version").startsWith("1.8");
JsonArray assets = object.getAsJsonArray("assets");
boolean found = false;
@ -103,13 +103,13 @@ public class UpdateCheckTask implements Runnable {
private void showUpdateQuestion(final String name, final String downloadUrl, final String latestVersion) {
int chosen = JOptionPane.showConfirmDialog(ViaProxy.getForegroundWindow(), I18n.get("popup.update.info", VERSION, latestVersion) + "\n\n" + I18n.get("popup.update.question"), "ViaProxy", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (chosen == JOptionPane.YES_OPTION) {
File f = new File(name);
final File f = new File(JarUtil.getJarFile().map(File::getParentFile).orElseThrow(), name);
new DownloadPopup(ViaProxy.getForegroundWindow(), downloadUrl, f, () -> SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(ViaProxy.getForegroundWindow(), I18n.get("popup.update.success"), "ViaProxy", JOptionPane.INFORMATION_MESSAGE);
try {
Runtime.getRuntime().exec(new String[]{System.getProperty("java.home") + "/bin/java", "-jar", f.getAbsolutePath()});
JarUtil.launch(f);
System.exit(0);
} catch (IOException e) {
} catch (Throwable e) {
Logger.LOGGER.error("Could not start the new ViaProxy jar", e);
ViaProxyWindow.showException(e);
}

View File

@ -18,17 +18,14 @@
package net.raphimc.viaproxy.ui.impl;
import net.lenni0451.commons.swing.GBC;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.ui.I18n;
import net.raphimc.viaproxy.ui.UITab;
import net.raphimc.viaproxy.ui.ViaProxyWindow;
import net.raphimc.viaproxy.util.JarUtil;
import net.raphimc.viaproxy.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import static net.raphimc.viaproxy.ui.ViaProxyWindow.BORDER_PADDING;
@ -65,10 +62,9 @@ public class UISettingsTab extends UITab {
I18n.setLocale(locale);
ViaProxyWindow.showInfo(I18n.get("tab.ui_settings.language.success", I18n.get("language.name"), locale));
try {
final File f = new File(ViaProxy.class.getProtectionDomain().getCodeSource().getLocation().toURI());
Runtime.getRuntime().exec(new String[]{System.getProperty("java.home") + "/bin/java", "-jar", f.getAbsolutePath()});
JarUtil.launch(JarUtil.getJarFile().orElseThrow());
System.exit(0);
} catch (URISyntaxException | IOException e) {
} catch (Throwable e) {
Logger.LOGGER.error("Could not start the ViaProxy jar", e);
ViaProxyWindow.showException(e);
System.exit(1);

View File

@ -18,6 +18,7 @@
package net.raphimc.viaproxy.util;
import net.lenni0451.reflect.ClassLoaders;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.util.logging.Logger;
import java.io.File;
@ -25,7 +26,7 @@ import java.io.File;
public class ClassLoaderPriorityUtil {
public static void loadOverridingJars() {
final File jarsFolder = new File("jars");
final File jarsFolder = new File(ViaProxy.getCwd(), "jars");
if (!jarsFolder.exists()) {
jarsFolder.mkdir();
return;

View File

@ -0,0 +1,40 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
*
* 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/>.
*/
package net.raphimc.viaproxy.util;
import net.raphimc.viaproxy.ViaProxy;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
public class JarUtil {
public static Optional<File> getJarFile() {
try {
return Optional.of(new File(ViaProxy.class.getProtectionDomain().getCodeSource().getLocation().toURI()));
} catch (Throwable ignored) {
return Optional.empty();
}
}
public static void launch(final File jarFile) throws IOException {
new ProcessBuilder(System.getProperty("java.home") + "/bin/java", "-jar", jarFile.getAbsolutePath()).directory(ViaProxy.getCwd()).start();
}
}

View File

@ -7,7 +7,7 @@
</Console>
<!-- latest.log same as vanilla -->
<RollingRandomAccessFile name="LatestFile" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
<RollingRandomAccessFile name="LatestFile" fileName="${sys:user.dir}/logs/latest.log" filePattern="${sys:user.dir}/logs/%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] (%logger{1}) %msg{nolookups}%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
@ -16,7 +16,7 @@
</RollingRandomAccessFile>
<!-- Debug log file -->
<RollingRandomAccessFile name="DebugFile" fileName="logs/debug.log" filePattern="logs/debug-%i.log.gz">
<RollingRandomAccessFile name="DebugFile" fileName="${sys:user.dir}/logs/debug.log" filePattern="${sys:user.dir}/logs/debug-%i.log.gz">
<PatternLayout pattern="[%d{HH:mm:ss}] [%t/%level] (%logger) %msg{nolookups}%n"/>
<!-- Keep 5 files max -->