Move UriScheme to extras module
This commit is contained in:
@@ -2,6 +2,7 @@ package com.replaymod.extras;
|
||||
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import com.replaymod.extras.playeroverview.PlayerOverview;
|
||||
import com.replaymod.extras.urischeme.UriSchemeExtra;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
@@ -22,6 +23,7 @@ public class ReplayModExtras {
|
||||
|
||||
private static final List<Class<? extends Extra>> builtin = Arrays.asList(
|
||||
PlayerOverview.class,
|
||||
UriSchemeExtra.class,
|
||||
FullBrightness.class,
|
||||
HotkeyButtons.class
|
||||
);
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.replaymod.extras.urischeme;
|
||||
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.io.output.StringBuilderWriter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class LinuxUriScheme extends UriScheme {
|
||||
|
||||
@Override
|
||||
public void install() throws URISyntaxException, IOException {
|
||||
File file = new File("replaymod.desktop");
|
||||
File iconFile = new File("replaymod-icon.jpg");
|
||||
String path = ReplayMod.getContainer().getSource().getAbsolutePath().replace("\\", "\\\\").replace("\"", "\\\"");
|
||||
String content =
|
||||
"[Desktop Entry]\n" +
|
||||
"Name=ReplayMod\n" +
|
||||
"Exec=java -cp \"" + path + "\" " + UriScheme.class.getName() + " %u\n" +
|
||||
"Icon=" + iconFile.getAbsolutePath().replace("\\", "\\\\").replace("\"", "\\\"") + "\n" +
|
||||
"Type=Application\n" +
|
||||
"Terminal=false\n" +
|
||||
"MimeType=x-scheme-handler/replaymod;";
|
||||
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
try {
|
||||
IOUtils.write(content, out);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
|
||||
InputStream in = LinuxUriScheme.class.getResourceAsStream("/assets/replaymod/logo.jpg");
|
||||
try {
|
||||
out = new FileOutputStream(iconFile);
|
||||
try {
|
||||
IOUtils.copy(in, out);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
|
||||
String[] command = {"xdg-desktop-menu", "install", "--novendor", "replaymod.desktop"};
|
||||
Process process = new ProcessBuilder().command(command).start();
|
||||
try {
|
||||
if (process.waitFor() != 0) {
|
||||
StringBuilderWriter writer = new StringBuilderWriter();
|
||||
IOUtils.copy(process.getInputStream(), writer);
|
||||
IOUtils.copy(process.getErrorStream(), writer);
|
||||
throw new IOException(writer.toString());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
FileUtils.deleteQuietly(file);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.replaymod.extras.urischeme;
|
||||
|
||||
public class OSXUriScheme extends UriScheme {
|
||||
@Override
|
||||
public void install() throws Exception {
|
||||
throw new UnsupportedOperationException("OSX URI scheme not yet implemented.");
|
||||
}
|
||||
}
|
||||
85
src/main/java/com/replaymod/extras/urischeme/UriScheme.java
Normal file
85
src/main/java/com/replaymod/extras/urischeme/UriScheme.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package com.replaymod.extras.urischeme;
|
||||
|
||||
import net.minecraft.util.Util;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.*;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
public abstract class UriScheme {
|
||||
public static final String PROTOCOL = "replaymod://";
|
||||
public static final int PROCESS_PORT = 11754;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.out.println("Args: " + Arrays.toString(args));
|
||||
|
||||
if (args.length == 1 && args[0].startsWith(PROTOCOL)) {
|
||||
int id = Integer.parseInt(args[0].substring(PROTOCOL.length()));
|
||||
try {
|
||||
Socket socket = new Socket(InetAddress.getLocalHost(), PROCESS_PORT);
|
||||
socket.getOutputStream().write(String.valueOf(id).getBytes());
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
launchNewInstance(id);
|
||||
}
|
||||
} else {
|
||||
launchNewInstance(null);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
JOptionPane.showMessageDialog(null, "Failed to start Minecraft launcher. Saving exception to replaymod-crash.txt");
|
||||
try {
|
||||
FileOutputStream out = new FileOutputStream("replaymod-crash.txt");
|
||||
t.printStackTrace(new PrintStream(out));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void launchNewInstance(Integer replayId) throws IOException {
|
||||
// Determine launcher.jar location
|
||||
String osName = System.getProperty("os.name").toLowerCase();
|
||||
String userHome = System.getProperty("user.home", ".");
|
||||
File dir;
|
||||
if (osName.contains("win")) {
|
||||
String appData = System.getenv("APPDATA");
|
||||
dir = new File(appData != null ? appData : userHome, ".minecraft/");
|
||||
} else if (osName.contains("mac")) {
|
||||
dir = new File(userHome, "Library/Application Support/minecraft");
|
||||
} else if (osName.contains("linux") || osName.contains("unix")) {
|
||||
dir = new File(userHome, ".minecraft/");
|
||||
} else {
|
||||
dir = new File(userHome, "minecraft/");
|
||||
}
|
||||
|
||||
// Launch process
|
||||
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "launcher.jar").directory(dir);
|
||||
if (replayId != null) {
|
||||
processBuilder.environment().put("replaymod.uri.replayid", String.valueOf(replayId));
|
||||
}
|
||||
processBuilder.start();
|
||||
}
|
||||
|
||||
public static UriScheme create() {
|
||||
switch (Util.getOSType()) {
|
||||
case LINUX:
|
||||
return new LinuxUriScheme();
|
||||
case WINDOWS:
|
||||
return new WindowsUriScheme();
|
||||
case OSX:
|
||||
return new OSXUriScheme();
|
||||
case SOLARIS:
|
||||
case UNKNOWN:
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void install() throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.replaymod.extras.urischeme;
|
||||
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import com.replaymod.extras.Extra;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class UriSchemeExtra implements Extra {
|
||||
private ReplayMod mod;
|
||||
|
||||
@Override
|
||||
public void register(final ReplayMod mod) throws Exception {
|
||||
this.mod = mod;
|
||||
|
||||
UriScheme uriScheme = UriScheme.create();
|
||||
if (uriScheme == null) {
|
||||
throw new UnsupportedOperationException("OS not supported.");
|
||||
}
|
||||
uriScheme.install();
|
||||
|
||||
mod.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Start listening for future requests
|
||||
startListener();
|
||||
|
||||
// Handle initial request
|
||||
String replayId = System.getenv("replaymod.uri.replayid");
|
||||
if (replayId != null) {
|
||||
loadReplay(Integer.parseInt(replayId));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startListener() {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ServerSocket serverSocket = null;
|
||||
try {
|
||||
serverSocket = new ServerSocket(UriScheme.PROCESS_PORT);
|
||||
while (!Thread.interrupted()) {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
try {
|
||||
InputStream inputStream = clientSocket.getInputStream();
|
||||
String replayId = IOUtils.toString(inputStream);
|
||||
final int id = Integer.parseInt(replayId);
|
||||
mod.runLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
loadReplay(id);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(clientSocket);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(serverSocket);
|
||||
}
|
||||
}
|
||||
}, "UriSchemeHandler").start();
|
||||
}
|
||||
|
||||
private void loadReplay(int id) {
|
||||
// TODO
|
||||
// File file = ReplayMod.downloadedFileHandler.getFileForID(id);
|
||||
// if (file == null) {
|
||||
// FileInfo info = new FileInfo(id, null, null, null, 0, 0, 0, String.valueOf(id), false, 0);
|
||||
// new GuiReplayDownloading(info).display();
|
||||
// } else {
|
||||
// try {
|
||||
// ReplayHandler.startReplay(file);
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.replaymod.extras.urischeme;
|
||||
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.io.output.StringBuilderWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class WindowsUriScheme extends UriScheme {
|
||||
@Override
|
||||
public void install() throws IOException, InterruptedException {
|
||||
String path = ReplayMod.getContainer().getSource().getAbsolutePath().replace("\\", "\\\\").replace("\"", "\\\"");
|
||||
regAdd("\\replaymod /f /ve /d \"URL:replaymod Protocol\"");
|
||||
regAdd("\\replaymod /f /v \"URL Protocol\" /d \"\"");
|
||||
regAdd("\\replaymod\\shell\\open\\command /f /ve /d \"java -cp \\\"" + path + "\\\" " + UriScheme.class.getName() + " \\\"%1\\\"\"");
|
||||
}
|
||||
|
||||
private void regAdd(String args) throws IOException, InterruptedException {
|
||||
Process process = Runtime.getRuntime().exec("REG ADD HKCU\\Software\\Classes" + args);
|
||||
if (process.waitFor() != 0) {
|
||||
StringBuilderWriter writer = new StringBuilderWriter();
|
||||
IOUtils.copy(process.getInputStream(), writer);
|
||||
IOUtils.copy(process.getErrorStream(), writer);
|
||||
throw new IOException(writer.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user