Allow replays to be started by "replay://" URIs (e.g. from browser)
This commit is contained in:
@@ -2,12 +2,15 @@ package eu.crushedpixel.replaymod;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFutureTask;
|
||||
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.*;
|
||||
import eu.crushedpixel.replaymod.gui.online.GuiReplayDownloading;
|
||||
import eu.crushedpixel.replaymod.gui.overlay.GuiReplayOverlay;
|
||||
import eu.crushedpixel.replaymod.holders.KeyframeSet;
|
||||
import eu.crushedpixel.replaymod.localization.LocalizedResourcePack;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
import eu.crushedpixel.replaymod.online.urischeme.UriScheme;
|
||||
import eu.crushedpixel.replaymod.recording.ConnectionEventHandler;
|
||||
import eu.crushedpixel.replaymod.registry.*;
|
||||
import eu.crushedpixel.replaymod.renderer.*;
|
||||
@@ -38,9 +41,13 @@ import net.minecraftforge.fml.common.ModContainer;
|
||||
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.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;
|
||||
@@ -87,6 +94,17 @@ 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();
|
||||
}
|
||||
|
||||
config = new Configuration(event.getSuggestedConfigurationFile());
|
||||
config.load();
|
||||
AuthenticationHandler.loadAuthkeyFromConfig();
|
||||
@@ -320,6 +338,67 @@ 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);
|
||||
mc.displayGuiScreen(new GuiReplayDownloading(info));
|
||||
} else {
|
||||
try {
|
||||
ReplayHandler.startReplay(file);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void testIfMoeshAndExitMinecraft() {
|
||||
|
||||
Reference in New Issue
Block a user