Added File Uploading GUI, not Thread Safe

This commit is contained in:
Marius Metzger
2015-01-31 16:15:31 +01:00
parent f67e681825
commit c8918c933a
11 changed files with 541 additions and 76 deletions

View File

@@ -2,11 +2,19 @@ package eu.crushedpixel.replaymod.gui;
public class GuiConstants {
public static final int CENTER_OVERVIEW_BUTTON = 2000;
public static final int CENTER_MY_REPLAYS_BUTTON = 2001;
public static final int CENTER_UPLOAD_BUTTON = 2002;
public static final int CENTER_SEARCH_BUTTON = 2002;
public static final int CENTER_BACK_BUTTON = 2003;
public static final int CENTER_LOGOUT_BUTTON = 2004;
public static final int CENTER_RECENT_BUTTON = 2005;
public static final int CENTER_BEST_BUTTON = 2006;
public static final int CENTER_MANAGER_BUTTON = 2007;
public static final int UPLOAD_NAME_INPUT = 3001;
public static final int UPLOAD_CATEGORY_BUTTON = 3002;
public static final int UPLOAD_START_BUTTON = 3003;
public static final int UPLOAD_CANCEL_BUTTON = 3004;
public static final int UPLOAD_BACK_BUTTON = 3005;
public static final int REPLAY_OPTIONS_BUTTON_ID = 8000;

View File

@@ -22,6 +22,7 @@ import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.client.ApiException;
import eu.crushedpixel.replaymod.api.client.holders.FileInfo;
import eu.crushedpixel.replaymod.gui.GuiConstants;
import eu.crushedpixel.replaymod.gui.replaymanager.GuiReplayManager;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
@@ -37,14 +38,17 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
//Top Button Bar
List<GuiButton> buttonBar = new ArrayList<GuiButton>();
GuiButton overviewButton = new GuiButton(GuiConstants.CENTER_OVERVIEW_BUTTON, 20, 30, "Overview");
buttonBar.add(overviewButton);
GuiButton recentButton = new GuiButton(GuiConstants.CENTER_RECENT_BUTTON, 20, 30, "Newest Replays");
buttonBar.add(recentButton);
GuiButton bestButton = new GuiButton(GuiConstants.CENTER_BEST_BUTTON, 20, 30, "Best Replays");
buttonBar.add(bestButton);
GuiButton ownReplayButton = new GuiButton(GuiConstants.CENTER_MY_REPLAYS_BUTTON, 20, 30, "My Replays");
buttonBar.add(ownReplayButton);
GuiButton uploadReplayButton = new GuiButton(GuiConstants.CENTER_UPLOAD_BUTTON, 20, 30, "Upload");
buttonBar.add(uploadReplayButton);
GuiButton searchButton = new GuiButton(GuiConstants.CENTER_SEARCH_BUTTON, 20, 30, "Search");
buttonBar.add(searchButton);
int i = 0;
for(GuiButton b : buttonBar) {
@@ -64,8 +68,11 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
//Bottom Button Bar (dat alliteration)
List<GuiButton> bottomBar = new ArrayList<GuiButton>();
GuiButton exitButton = new GuiButton(GuiConstants.CENTER_BACK_BUTTON, 20, 20, "Back");
GuiButton exitButton = new GuiButton(GuiConstants.CENTER_BACK_BUTTON, 20, 20, "Main Menu");
bottomBar.add(exitButton);
GuiButton managerButton = new GuiButton(GuiConstants.CENTER_MANAGER_BUTTON, 20, 20, "Replay Manager");
bottomBar.add(managerButton);
GuiButton logoutButton = new GuiButton(GuiConstants.CENTER_LOGOUT_BUTTON, 20, 20, "Logout");
bottomBar.add(logoutButton);
@@ -85,7 +92,7 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
i++;
}
showOverview();
showOnlineRecent();
}
@Override
@@ -93,9 +100,18 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
if(!button.enabled) return;
if(button.id == GuiConstants.CENTER_BACK_BUTTON) {
mc.displayGuiScreen(new GuiMainMenu());
return;
} else if(button.id == GuiConstants.CENTER_LOGOUT_BUTTON) {
mc.displayGuiScreen(getYesNoGui(this, LOGOUT_CALLBACK_ID));
} else if(button.id == GuiConstants.CENTER_MANAGER_BUTTON) {
mc.displayGuiScreen(new GuiReplayManager());
} else if(button.id == GuiConstants.CENTER_RECENT_BUTTON) {
showOnlineRecent();
} else if(button.id == GuiConstants.CENTER_BEST_BUTTON) {
} else if(button.id == GuiConstants.CENTER_MY_REPLAYS_BUTTON) {
} else if(button.id == GuiConstants.CENTER_SEARCH_BUTTON) {
}
}
@@ -136,7 +152,7 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
Keyboard.enableRepeatEvents(false);
}
public void showOverview() {
public void showOnlineRecent() {
mc.addScheduledTask(new Runnable() {
@Override
public void run() {

View File

@@ -0,0 +1,264 @@
package eu.crushedpixel.replaymod.gui.online;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.util.ResourceLocation;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.FilenameUtils;
import org.lwjgl.input.Keyboard;
import com.google.gson.Gson;
import eu.crushedpixel.replaymod.api.client.ApiException;
import eu.crushedpixel.replaymod.api.client.FileUploader;
import eu.crushedpixel.replaymod.api.client.holders.Category;
import eu.crushedpixel.replaymod.gui.GuiConstants;
import eu.crushedpixel.replaymod.gui.replaymanager.ResourceHelper;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
import eu.crushedpixel.replaymod.recording.ConnectionEventHandler;
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
import eu.crushedpixel.replaymod.reflection.MCPNames;
import eu.crushedpixel.replaymod.utils.ImageUtils;
public class GuiUploadFile extends GuiScreen {
private GuiTextField fileTitleInput;
private GuiButton categoryButton, startUploadButton, cancelUploadButton, backButton;
private Gson gson = new Gson();
private File replayFile;
private ReplayMetaData metaData;
private BufferedImage thumb;
private FileUploader uploader = new FileUploader();
private Category category = Category.MINIGAME;
private final ResourceLocation textureResource;
private DynamicTexture dynTex = null;
private Minecraft mc = Minecraft.getMinecraft();
public GuiUploadFile(File file) {
this.textureResource = new ResourceLocation("upload_thumbs/"+FilenameUtils.getBaseName(file.getAbsolutePath()));
dynTex = null;
boolean correctFile = false;
this.replayFile = file;
if(("."+FilenameUtils.getExtension(file.getAbsolutePath())).equals(ConnectionEventHandler.ZIP_FILE_EXTENSION)) {
ZipFile archive = null;
try {
archive = new ZipFile(file);
ZipArchiveEntry recfile = archive.getEntry("recording"+ConnectionEventHandler.TEMP_FILE_EXTENSION);
ZipArchiveEntry metadata = archive.getEntry("metaData"+ConnectionEventHandler.JSON_FILE_EXTENSION);
ZipArchiveEntry image = archive.getEntry("thumb");
BufferedImage img = null;
if(image != null) {
InputStream is = archive.getInputStream(image);
is.skip(7);
BufferedImage bimg = ImageIO.read(is);
if(bimg != null) {
thumb = ImageUtils.scaleImage(bimg, new Dimension(1280, 720));
}
}
InputStream is = archive.getInputStream(metadata);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String json = br.readLine();
metaData = gson.fromJson(json, ReplayMetaData.class);
archive.close();
correctFile = true;
} catch(Exception e) {
e.printStackTrace();
} finally {
if(archive != null) {
try {
archive.close();
} catch (IOException e) {}
}
}
}
if(!correctFile) {
System.out.println("Invalid file provided to upload");
mc.displayGuiScreen(new GuiMainMenu()); //TODO: Error message
replayFile = null;
return;
}
//If thumb is null, set image to placeholder
if(thumb == null) {
try {
thumb = ImageIO.read(MCPNames.class.getClassLoader().getResourceAsStream("default_thumb.jpg"));
} catch(Exception e) {
e.printStackTrace();
}
}
}
@Override
public void initGui() {
if(replayFile == null) return;
fileTitleInput = new GuiTextField(GuiConstants.UPLOAD_NAME_INPUT, fontRendererObj, 200, 21, 120, 20);
String fname = FilenameUtils.getBaseName(replayFile.getAbsolutePath());
fileTitleInput.setText(fname);
categoryButton = new GuiButton(GuiConstants.UPLOAD_CATEGORY_BUTTON, 200, 80, "Category: "+category.toNiceString());
categoryButton.width = 120;
buttonList.add(categoryButton);
List<GuiButton> bottomBar = new ArrayList<GuiButton>();
startUploadButton = new GuiButton(GuiConstants.UPLOAD_START_BUTTON, 0, 0, "Start Upload");
bottomBar.add(startUploadButton);
cancelUploadButton = new GuiButton(GuiConstants.UPLOAD_CANCEL_BUTTON, 0, 0, "Cancel Upload");
cancelUploadButton.enabled = false;
bottomBar.add(cancelUploadButton);
backButton = new GuiButton(GuiConstants.UPLOAD_BACK_BUTTON, 0, 0, "Back");
bottomBar.add(backButton);
int i = 0;
for(GuiButton b : bottomBar) {
int w = this.width - 30;
int w2 = w/bottomBar.size();
int x = 15+(w2*i);
b.xPosition = x;
b.yPosition = height-30;
b.width = w2-4;
buttonList.add(b);
i++;
}
startUploadButton.enabled = (!(fileTitleInput.getText().trim().length() < 5 || fileTitleInput.getText().trim().length() > 30 ||
p.matcher(fileTitleInput.getText()).find()));
}
@Override
protected void actionPerformed(GuiButton button) throws IOException {
if(!button.enabled) return;
if(button.id == GuiConstants.UPLOAD_CATEGORY_BUTTON) {
category = category.next();
categoryButton.displayString = "Category: "+category.toNiceString();
} else if(button.id == GuiConstants.UPLOAD_BACK_BUTTON) {
mc.displayGuiScreen(new GuiMainMenu());
} else if(button.id == GuiConstants.UPLOAD_START_BUTTON) {
mc.addScheduledTask(new Runnable() {
@Override
public void run() {
try {
uploader.uploadFile(AuthenticationHandler.getKey(), fileTitleInput.getText().trim(), replayFile, category);
} catch (ApiException e) { //TODO: Error handling
e.printStackTrace();
mc.displayGuiScreen(new GuiMainMenu());
} catch (RuntimeException e) {
e.printStackTrace();
mc.displayGuiScreen(new GuiMainMenu());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawDefaultBackground();
drawString(fontRendererObj, metaData.getServerName(), 200, 50, Color.GRAY.getRGB());
drawString(fontRendererObj, "Duration: "+String.format("%02dm%02ds",
TimeUnit.MILLISECONDS.toMinutes(metaData.getDuration()),
TimeUnit.MILLISECONDS.toSeconds(metaData.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(metaData.getDuration()))
), 200, 65, Color.GRAY.getRGB());
drawCenteredString(fontRendererObj, "Upload File", this.width/2, 5, Color.WHITE.getRGB());
//Draw thumbnail
if(thumb != null) {
if(dynTex == null) {
dynTex = new DynamicTexture(thumb);
mc.getTextureManager().loadTexture(textureResource, dynTex);
dynTex.updateDynamicTexture();
ResourceHelper.registerResource(textureResource);
}
mc.getTextureManager().bindTexture(textureResource); //Will be freed by the ResourceHelper
Gui.drawScaledCustomSizeModalRect(20, 20, 0, 0, 1280, 720, 57*3, 32*3, 1280, 720);
}
fileTitleInput.drawTextBox();
super.drawScreen(mouseX, mouseY, partialTicks);
this.drawRect(20, this.height-100, width-20, this.height-80, Color.BLACK.getRGB());
this.drawRect(22, this.height-98, width-22, this.height-82, Color.WHITE.getRGB());
int width = this.width-22 - 22;
float w = width*uploader.getUploadProgress();
this.drawRect(22, this.height-98, Math.round(22+w), this.height-82, Color.RED.getRGB());
}
@Override
public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
super.mouseClicked(mouseX, mouseY, mouseButton);
fileTitleInput.mouseClicked(mouseX, mouseY, mouseButton);
}
@Override
public void updateScreen() {
fileTitleInput.updateCursorCounter();
}
@Override
public void onGuiClosed() {
Keyboard.enableRepeatEvents(false);
}
private static final Pattern p = Pattern.compile("[^a-z0-9 \\-_]", Pattern.CASE_INSENSITIVE);
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
if(fileTitleInput.isFocused()) {
fileTitleInput.textboxKeyTyped(typedChar, keyCode);
}
startUploadButton.enabled = (!(fileTitleInput.getText().trim().length() < 5 || fileTitleInput.getText().trim().length() > 30 ||
p.matcher(fileTitleInput.getText()).find()));
}
}

View File

@@ -38,8 +38,11 @@ import com.google.gson.Gson;
import com.mojang.realmsclient.util.Pair;
import eu.crushedpixel.replaymod.gui.GuiReplaySettings;
import eu.crushedpixel.replaymod.gui.online.GuiUploadFile;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
import eu.crushedpixel.replaymod.recording.ConnectionEventHandler;
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
import eu.crushedpixel.replaymod.reflection.MCPNames;
import eu.crushedpixel.replaymod.replay.ReplayHandler;
import eu.crushedpixel.replaymod.utils.ImageUtils;
@@ -53,20 +56,21 @@ public class GuiReplayManager extends GuiScreen implements GuiYesNoCallback {
private boolean initialized;
private GuiReplayListExtended replayGuiList;
private List<Pair<Pair<File, ReplayMetaData>, BufferedImage>> replayFileList = new ArrayList<Pair<Pair<File, ReplayMetaData>, BufferedImage>>();
private GuiButton loadButton, folderButton, renameButton, deleteButton, cancelButton, settingsButton;
private GuiButton loadButton, uploadButton, folderButton, renameButton, deleteButton, cancelButton, settingsButton;
private static Gson gson = new Gson();
private boolean replaying = false;
private static final int LOAD_BUTTON_ID = 9001;
private static final int FOLDER_BUTTON_ID = 9002;
private static final int RENAME_BUTTON_ID = 9003;
private static final int DELETE_BUTTON_ID = 9004;
private static final int SETTINGS_BUTTON_ID = 9005;
private static final int CANCEL_BUTTON_ID = 9006;
private static final int UPLOAD_BUTTON_ID = 9002;
private static final int FOLDER_BUTTON_ID = 9003;
private static final int RENAME_BUTTON_ID = 9004;
private static final int DELETE_BUTTON_ID = 9005;
private static final int SETTINGS_BUTTON_ID = 9006;
private static final int CANCEL_BUTTON_ID = 9007;
private boolean delete_file = false;
private void reloadFiles() {
replayGuiList.clearEntries();
replayFileList = new ArrayList<Pair<Pair<File, ReplayMetaData>, BufferedImage>>();
@@ -91,6 +95,11 @@ public class GuiReplayManager extends GuiScreen implements GuiYesNoCallback {
img = ImageUtils.scaleImage(bimg, new Dimension(1280, 720));
}
}
//If thumb is null, set image to placeholder
if(img == null) {
img = ImageIO.read(MCPNames.class.getClassLoader().getResourceAsStream("default_thumb.jpg"));
}
InputStream is = archive.getInputStream(metadata);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
@@ -146,7 +155,8 @@ public class GuiReplayManager extends GuiScreen implements GuiYesNoCallback {
}
private void createButtons() {
this.buttonList.add(loadButton = new GuiButton(LOAD_BUTTON_ID, this.width / 2 - 154, this.height - 52, 150, 20, I18n.format("Load Replay", new Object[0])));
this.buttonList.add(loadButton = new GuiButton(LOAD_BUTTON_ID, this.width / 2 - 154, this.height - 52, 73, 20, I18n.format("Load", new Object[0])));
this.buttonList.add(uploadButton = new GuiButton(UPLOAD_BUTTON_ID, this.width / 2 - 154 + 78, this.height - 52, 73, 20, I18n.format("Upload", new Object[0])));
this.buttonList.add(folderButton = new GuiButton(FOLDER_BUTTON_ID, this.width / 2 + 4, this.height - 52, 150, 20, I18n.format("Open Replay Folder...", new Object[0])));
this.buttonList.add(renameButton = new GuiButton(RENAME_BUTTON_ID, this.width / 2 - 154, this.height - 28, 72, 20, I18n.format("Rename", new Object[0])));
this.buttonList.add(deleteButton = new GuiButton(DELETE_BUTTON_ID, this.width / 2 - 76, this.height - 28, 72, 20, I18n.format("Delete", new Object[0])));
@@ -185,20 +195,17 @@ public class GuiReplayManager extends GuiScreen implements GuiYesNoCallback {
@Override
protected void actionPerformed(GuiButton button) throws IOException
{
if (button.enabled) {
if(button.enabled) {
if(button.id == LOAD_BUTTON_ID) {
loadReplay(replayGuiList.selected);
}
else if (button.id == CANCEL_BUTTON_ID)
{
else if(button.id == CANCEL_BUTTON_ID) {
mc.displayGuiScreen(parentScreen);
}
else if (button.id == DELETE_BUTTON_ID)
{
else if(button.id == DELETE_BUTTON_ID) {
String s = replayGuiList.getListEntry(replayGuiList.selected).getFileName();
if (s != null)
{
if (s != null) {
delete_file = true;
GuiYesNo guiyesno = getYesNoGui(this, s, 1);
this.mc.displayGuiScreen(guiyesno);
@@ -207,53 +214,48 @@ public class GuiReplayManager extends GuiScreen implements GuiYesNoCallback {
else if(button.id == SETTINGS_BUTTON_ID) {
this.mc.displayGuiScreen(new GuiReplaySettings(this));
}
else if(button.id == RENAME_BUTTON_ID)
{
else if(button.id == RENAME_BUTTON_ID) {
File file = replayFileList.get(replayGuiList.selected).first().first();
this.mc.displayGuiScreen(new GuiRenameReplay(this, file));
}
else if(button.id == FOLDER_BUTTON_ID)
{
else if(button.id == UPLOAD_BUTTON_ID) {
File file = replayFileList.get(replayGuiList.selected).first().first();
this.mc.displayGuiScreen(new GuiUploadFile(file));
}
else if(button.id == FOLDER_BUTTON_ID) {
File file1 = new File("./replay_recordings/");
file1.mkdirs();
String s = file1.getAbsolutePath();
if (Util.getOSType() == Util.EnumOS.OSX)
{
try
{
if(Util.getOSType() == Util.EnumOS.OSX) {
try {
Runtime.getRuntime().exec(new String[] {"/usr/bin/open", s});
return;
}
catch (IOException ioexception1) {}
catch(IOException ioexception1) {}
}
else if (Util.getOSType() == Util.EnumOS.WINDOWS)
{
else if(Util.getOSType() == Util.EnumOS.WINDOWS) {
String s1 = String.format("cmd.exe /C start \"Open file\" \"%s\"", new Object[] {s});
try
{
try{
Runtime.getRuntime().exec(s1);
return;
}
catch (IOException ioexception) {}
catch(IOException ioexception) {}
}
boolean flag = false;
try
{
try {
Class oclass = Class.forName("java.awt.Desktop");
Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object)null, new Object[0]);
oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {file1.toURI()});
}
catch (Throwable throwable)
{
catch(Throwable throwable) {
flag = true;
}
if (flag)
{
if(flag) {
Sys.openURL("file://" + s);
}
}
@@ -286,6 +288,12 @@ public class GuiReplayManager extends GuiScreen implements GuiYesNoCallback {
public void setButtonsEnabled(boolean b) {
loadButton.enabled = b;
if(!b || !AuthenticationHandler.isAuthenticated()) {
uploadButton.enabled = false;
} else {
uploadButton.enabled = true;
}
renameButton.enabled = b;
deleteButton.enabled = b;
}