Move UriScheme to extras module

This commit is contained in:
johni0702
2015-11-13 16:00:27 +01:00
parent 868d873fe8
commit 97d375dfce
7 changed files with 99 additions and 81 deletions

View File

@@ -3,17 +3,14 @@ package com.replaymod.core;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.replaymod.replay.ReplaySender;
import eu.crushedpixel.replaymod.api.ApiClient;
import eu.crushedpixel.replaymod.api.replay.holders.FileInfo;
import eu.crushedpixel.replaymod.chat.ChatMessageHandler;
import eu.crushedpixel.replaymod.events.handlers.CrosshairRenderHandler;
import eu.crushedpixel.replaymod.events.handlers.GuiEventHandler;
import eu.crushedpixel.replaymod.events.handlers.MouseInputHandler;
import eu.crushedpixel.replaymod.events.handlers.TickAndRenderListener;
import eu.crushedpixel.replaymod.events.handlers.keyboard.KeyInputHandler;
import eu.crushedpixel.replaymod.gui.online.GuiReplayDownloading;
import eu.crushedpixel.replaymod.localization.LocalizedResourcePack;
import eu.crushedpixel.replaymod.online.authentication.ConfigurationAuthData;
import eu.crushedpixel.replaymod.online.urischeme.UriScheme;
import eu.crushedpixel.replaymod.registry.*;
import eu.crushedpixel.replaymod.renderer.CustomObjectRenderer;
import eu.crushedpixel.replaymod.renderer.InvisibilityRender;
@@ -47,13 +44,9 @@ import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.Queue;
@@ -156,17 +149,6 @@ public class ReplayMod {
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
try {
UriScheme uriScheme = UriScheme.create();
if (uriScheme == null) {
throw new UnsupportedOperationException("OS not supported.");
}
uriScheme.install();
} catch (Exception e) {
System.err.println("Failed to install UriScheme handler:");
e.printStackTrace();
}
// Initialize the static OpenGL info field from the minecraft main thread
// Unfortunately lwjgl uses static methods so we have to make use of magic init calls as well
OpenGLUtils.init();
@@ -398,67 +380,13 @@ public class ReplayMod {
testIfMoeshAndExitMinecraft();
}
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);
mc.addScheduledTask(new Runnable() {
@Override
public void run() {
loadOnlineReplay(id);
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(clientSocket);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(serverSocket);
}
}
}, "UriSchemeHandler").start();
String replayId = System.getenv("replaymod.uri.replayid");
if (replayId != null) {
final int id = Integer.parseInt(replayId);
@SuppressWarnings("unchecked")
Queue<ListenableFutureTask> tasks = mc.scheduledTasks;
synchronized (mc.scheduledTasks) {
tasks.add(ListenableFutureTask.create(new Runnable() {
@Override
public void run() {
loadOnlineReplay(id);
}
}, null));
}
}
}
private void loadOnlineReplay(int id) {
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 {
// TODO
// ReplayHandler.startReplay(file);
} catch (Exception e) {
e.printStackTrace();
}
public void runLater(Runnable runnable) {
@SuppressWarnings("unchecked")
Queue<ListenableFutureTask> tasks = mc.scheduledTasks;
synchronized (mc.scheduledTasks) {
tasks.add(ListenableFutureTask.create(runnable, null));
}
}

View File

@@ -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
);

View File

@@ -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);
}
}

View 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.");
}
}

View 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;
}

View File

@@ -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();
// }
// }
}
}

View File

@@ -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());
}
}
}