From 27732bec35c9b51c190a60b84e980b4be6d1569f Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Tue, 27 Dec 2022 06:54:42 +0100 Subject: [PATCH] Fixes for mac --- .../api/connector/XPipeApiConnection.java | 2 +- .../xpipe/beacon/BeaconDaemonController.java | 2 +- .../java/io/xpipe/core/process/OsType.java | 61 +++++-------------- .../java/io/xpipe/core/store/ShellStore.java | 7 +++ .../java/io/xpipe/core/util/Deobfuscator.java | 2 +- .../io/xpipe/core/util/XPipeInstallation.java | 24 +++++--- .../xpipe/core/util/XPipeTempDirectory.java | 2 +- 7 files changed, 42 insertions(+), 58 deletions(-) diff --git a/api/src/main/java/io/xpipe/api/connector/XPipeApiConnection.java b/api/src/main/java/io/xpipe/api/connector/XPipeApiConnection.java index 12b756332..88c7e0659 100644 --- a/api/src/main/java/io/xpipe/api/connector/XPipeApiConnection.java +++ b/api/src/main/java/io/xpipe/api/connector/XPipeApiConnection.java @@ -131,7 +131,7 @@ public final class XPipeApiConnection extends BeaconConnection { } private void start() throws Exception { - var installation = XPipeInstallation.getLocalDefaultInstallationBasePath(); + var installation = XPipeInstallation.getLocalDefaultInstallationBasePath(true); BeaconServer.start(installation); } diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconDaemonController.java b/beacon/src/main/java/io/xpipe/beacon/BeaconDaemonController.java index 0ce23013e..1dae5afb6 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconDaemonController.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconDaemonController.java @@ -19,7 +19,7 @@ public class BeaconDaemonController { if ((process = BeaconServer.tryStartCustom()) != null) { custom = true; } else { - var defaultBase = XPipeInstallation.getLocalDefaultInstallationBasePath(); + var defaultBase = XPipeInstallation.getLocalDefaultInstallationBasePath(true); process = BeaconServer.start(defaultBase); } diff --git a/core/src/main/java/io/xpipe/core/process/OsType.java b/core/src/main/java/io/xpipe/core/process/OsType.java index 1cbf8a9be..68ab2a9dc 100644 --- a/core/src/main/java/io/xpipe/core/process/OsType.java +++ b/core/src/main/java/io/xpipe/core/process/OsType.java @@ -1,9 +1,7 @@ package io.xpipe.core.process; -import java.nio.file.Path; import java.util.Locale; import java.util.Map; -import java.util.UUID; public interface OsType { @@ -34,10 +32,6 @@ public interface OsType { } } - Path getBaseInstallPath(); - - UUID getSystemUUID(ShellProcessControl pc) throws Exception; - static class Windows implements OsType { @Override @@ -70,20 +64,6 @@ public interface OsType { return properties.get("OS Name") + " " + properties.get("OS Version").split(" ")[0]; } - - @Override - public Path getBaseInstallPath() { - return Path.of(System.getenv("LOCALAPPDATA"), "X-Pipe"); - } - - @Override - public UUID getSystemUUID(ShellProcessControl pc) throws Exception { - try (CommandProcessControl c = pc.command( - "reg query \"Computer\\\\HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Microsoft\\\\Cryptography\" /v MachineGuid")) { - var output = c.readOnlyStdout(); - return null; - } - } } static class Linux implements OsType { @@ -110,16 +90,14 @@ public interface OsType { @Override public String determineOperatingSystemName(ShellProcessControl pc) throws Exception { - try (CommandProcessControl c = - pc.command("lsb_release -a").start()) { + try (CommandProcessControl c = pc.command("lsb_release -a").start()) { var text = c.readOnlyStdout(); if (c.getExitCode() == 0) { return PropertiesFormatsParser.parse(text, ":").getOrDefault("Description", null); } } - try (CommandProcessControl c = - pc.command("cat /etc/*release").start()) { + try (CommandProcessControl c = pc.command("cat /etc/*release").start()) { var text = c.readOnlyStdout(); if (c.getExitCode() == 0) { return PropertiesFormatsParser.parse(text, "=").getOrDefault("PRETTY_NAME", null); @@ -144,23 +122,13 @@ public interface OsType { return type + " " + version; } - - @Override - public Path getBaseInstallPath() { - return Path.of("/opt/xpipe"); - } - - @Override - public UUID getSystemUUID(ShellProcessControl pc) throws Exception { - return null; - } } static class Mac implements OsType { @Override public String getTempDirectory(ShellProcessControl pc) throws Exception { - return pc.executeStringSimpleCommand(pc.getShellType().getPrintVariableCommand("TEMP")); + return pc.executeStringSimpleCommand(pc.getShellType().getPrintVariableCommand("TMPDIR")); } @Override @@ -175,22 +143,21 @@ public interface OsType { @Override public Map getProperties(ShellProcessControl pc) throws Exception { - return null; + try (CommandProcessControl c = + pc.subShell(ShellTypes.BASH).command("sw_vers").start()) { + var text = c.readOrThrow(); + return PropertiesFormatsParser.parse(text, ":"); + } } @Override public String determineOperatingSystemName(ShellProcessControl pc) throws Exception { - return null; - } - - @Override - public Path getBaseInstallPath() { - return Path.of(System.getProperty("user.home"), "Application Support", "X-Pipe"); - } - - @Override - public UUID getSystemUUID(ShellProcessControl pc) throws Exception { - return null; + var properties = getProperties(pc); + var name = pc.executeStringSimpleCommand( + "awk '/SOFTWARE LICENSE AGREEMENT FOR macOS/' '/System/Library/CoreServices/Setup " + + "Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | " + + "awk -F 'macOS ' '{print $NF}' | awk '{print substr($0, 0, length($0)-1)}'"); + return properties.get("ProductName") + " " + name + " " + properties.get("ProductVersion"); } } } diff --git a/core/src/main/java/io/xpipe/core/store/ShellStore.java b/core/src/main/java/io/xpipe/core/store/ShellStore.java index 0da5b9d2e..b6022ba8d 100644 --- a/core/src/main/java/io/xpipe/core/store/ShellStore.java +++ b/core/src/main/java/io/xpipe/core/store/ShellStore.java @@ -1,5 +1,6 @@ package io.xpipe.core.store; +import io.xpipe.core.charsetter.Charsetter; import io.xpipe.core.impl.LocalStore; import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.process.ShellType; @@ -10,6 +11,12 @@ public interface ShellStore extends DataStore { return new LocalStore(); } + static void withLocal(Charsetter.FailableConsumer c) throws Exception { + try (var l = local().create().start()) { + c.accept(l); + } + } + static boolean isLocal(ShellStore s) { return s instanceof LocalStore; } diff --git a/core/src/main/java/io/xpipe/core/util/Deobfuscator.java b/core/src/main/java/io/xpipe/core/util/Deobfuscator.java index 465aa0a88..79ac0cb84 100644 --- a/core/src/main/java/io/xpipe/core/util/Deobfuscator.java +++ b/core/src/main/java/io/xpipe/core/util/Deobfuscator.java @@ -70,7 +70,7 @@ public class Deobfuscator { file.toString()) .redirectErrorStream(true); var active = proc.start(); - var out = new String(active.getInputStream().readAllBytes()).replaceAll("\\r\\n", NewLine.LF.getNewLineString()); + var out = new String(active.getInputStream().readAllBytes()).replaceAll("\r\n", NewLine.LF.getNewLineString()); var code = active.waitFor(); if (code == 0) { return out; diff --git a/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java b/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java index 0cde125e4..470849d96 100644 --- a/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java +++ b/core/src/main/java/io/xpipe/core/util/XPipeInstallation.java @@ -64,9 +64,9 @@ public class XPipeInstallation { } } - public static String getLocalDefaultInstallationBasePath() { + public static String getLocalDefaultInstallationBasePath(boolean acceptCustomHome) { var customHome = System.getenv("XPIPE_HOME"); - if (!customHome.isEmpty()) { + if (!customHome.isEmpty() && acceptCustomHome) { return customHome; } @@ -74,8 +74,10 @@ public class XPipeInstallation { if (OsType.getLocal().equals(OsType.WINDOWS)) { var base = System.getenv("LOCALAPPDATA"); path = FileNames.join(base, "X-Pipe"); - } else { + } else if (OsType.getLocal().equals(OsType.LINUX)) { path = "/opt/xpipe"; + } else { + path = "~/Applications/X-Pipe.app"; } return path; @@ -93,8 +95,10 @@ public class XPipeInstallation { if (p.getOsType().equals(OsType.WINDOWS)) { var base = p.executeStringSimpleCommand(p.getShellType().getPrintVariableCommand("LOCALAPPDATA")); path = FileNames.join(base, "X-Pipe"); - } else { + } else if (p.getOsType().equals(OsType.LINUX)) { path = "/opt/xpipe"; + } else { + path = "~/Applications/X-Pipe.app"; } return path; @@ -103,24 +107,30 @@ public class XPipeInstallation { public static String getDaemonDebugScriptPath(OsType type) { if (type.equals(OsType.WINDOWS)) { return FileNames.join("app", "scripts", "xpiped_debug.bat"); - } else { + } else if (type.equals(OsType.LINUX)) { return FileNames.join("app", "scripts", "xpiped_debug.sh"); + } else { + return FileNames.join("Content", "scripts", "xpiped_debug.sh"); } } public static String getDaemonDebugAttachScriptPath(OsType type) { if (type.equals(OsType.WINDOWS)) { return FileNames.join("app", "scripts", "xpiped_debug_attach.bat"); - } else { + } else if (type.equals(OsType.LINUX)) { return FileNames.join("app", "scripts", "xpiped_debug_attach.sh"); + } else { + return FileNames.join("Content", "scripts", "xpiped_debug_attach.sh"); } } public static String getDaemonExecutablePath(OsType type) { if (type.equals(OsType.WINDOWS)) { return FileNames.join("app", "xpiped.exe"); - } else { + } else if (type.equals(OsType.LINUX)) { return FileNames.join("app", "bin", "xpiped"); + } else { + return FileNames.join("Content", "MacOS", "xpiped"); } } } diff --git a/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java b/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java index 5da37d2b1..32cc0d611 100644 --- a/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java +++ b/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java @@ -23,7 +23,7 @@ public class XPipeTempDirectory { throw new IOException("Unable to access or create temporary directory " + dir); } - if (proc.getOsType().equals(OsType.LINUX)) { + if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MAC)) { proc.executeSimpleCommand("(chmod -f 777 \"" + dir + "\" || true)"); }