Skip to content

Flatpak support #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions src/main/java/com/jagrosh/discordipc/entities/pipe/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public abstract class Pipe {

Expand Down Expand Up @@ -58,13 +60,13 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,C

// store some files so we can get the preferred client
Pipe[] open = new Pipe[DiscordBuild.values().length];
for(int i = 0; i < 10; i++)
String osName = System.getProperty("os.name").toLowerCase();
for(String location : (Iterable<String>) getPipeLocations(osName)::iterator)
{
try
{
String location = getPipeLocation(i);
LOGGER.debug(String.format("Searching for IPC: %s", location));
pipe = createPipe(ipcClient, callbacks, location);
pipe = createPipe(ipcClient, callbacks, osName, location);

pipe.send(Packet.OpCode.HANDSHAKE, new JSONObject().put("v", VERSION).put("client_id", Long.toString(clientId)), null);

Expand Down Expand Up @@ -150,22 +152,14 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,C
return pipe;
}

private static Pipe createPipe(IPCClient ipcClient, HashMap<String, Callback> callbacks, String location) {
String osName = System.getProperty("os.name").toLowerCase();

private static Pipe createPipe(IPCClient ipcClient, HashMap<String, Callback> callbacks, String osName, String location) throws IOException {
if (osName.contains("win"))
{
return new WindowsPipe(ipcClient, callbacks, location);
}
else if (osName.contains("linux") || osName.contains("mac"))
{
try {
return new UnixPipe(ipcClient, callbacks, location);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
return new UnixPipe(ipcClient, callbacks, location);
}
else
{
Expand Down Expand Up @@ -251,25 +245,30 @@ public DiscordBuild getDiscordBuild()
private final static String[] unixPaths = {"XDG_RUNTIME_DIR","TMPDIR","TMP","TEMP"};

/**
* Finds the IPC location in the current system.
* Creates an IntStream with the range 0-10.
*
* @param i Index to try getting the IPC at.
* @return The stream.
*/
private static IntStream range() {
return IntStream.range(0, 10);
}

/**
* Finds the IPC location in the current system.
*
* @return The IPC location.
*/
private static String getPipeLocation(int i)
{
if(System.getProperty("os.name").contains("Win"))
return "\\\\?\\pipe\\discord-ipc-"+i;
String tmppath = null;
for(String str : unixPaths)
{
tmppath = System.getenv(str);
if(tmppath != null)
break;
}
if(tmppath == null)
tmppath = "/tmp";
return tmppath+"/discord-ipc-"+i;
private static Stream<String> getPipeLocations(String osName) {
if(osName.contains("win"))
return range().mapToObj(i -> "\\\\?\\pipe\\discord-ipc-" + i);

String tmppath = Stream.of(unixPaths).map(System::getenv).filter(path -> path != null).findFirst()
.orElse("/tmp");

Stream<String> unix = range().mapToObj(i -> tmppath + "/discord-ipc-" + i).limit(10);
if(osName.contains("linux"))
unix = Stream.concat(unix, range().mapToObj(i -> tmppath + "/app/com.discordapp.Discord/discord-ipc-" + i));

return unix;
}
}