2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.client;
|
2012-08-15 22:41:41 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
|
2012-11-05 20:29:04 +01:00
|
|
|
import mekanism.common.Mekanism;
|
|
|
|
import net.minecraft.client.Minecraft;
|
2012-08-15 22:41:41 +02:00
|
|
|
/**
|
2012-11-05 20:29:04 +01:00
|
|
|
* Thread that downloads the latest release of Mekanism. The older file is deleted and the newly downloaded file takes it's place.
|
2012-08-15 22:41:41 +02:00
|
|
|
* @author AidanBrady
|
|
|
|
*
|
|
|
|
*/
|
2012-10-03 21:12:17 +02:00
|
|
|
public class ThreadClientUpdate extends Thread
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
|
|
|
private int bytesDownloaded;
|
|
|
|
private int lastBytesDownloaded;
|
|
|
|
private byte[] buffer = new byte[10240];
|
|
|
|
private URL url;
|
|
|
|
|
2012-10-03 21:12:17 +02:00
|
|
|
public ThreadClientUpdate(String location)
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
url = new URL(location);
|
|
|
|
setDaemon(true);
|
|
|
|
start();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run()
|
|
|
|
{
|
2012-11-05 20:29:04 +01:00
|
|
|
File download = new File(new StringBuilder().append(Minecraft.getMinecraftDir()).append("/mods/Mekanism.jar").toString());
|
2012-08-15 22:41:41 +02:00
|
|
|
try {
|
|
|
|
prepareForDownload();
|
|
|
|
download.createNewFile();
|
|
|
|
FileOutputStream outputStream = new FileOutputStream(download.getAbsolutePath());
|
|
|
|
InputStream stream = url.openStream();
|
|
|
|
|
|
|
|
while((lastBytesDownloaded = stream.read(buffer)) > 0)
|
|
|
|
{
|
|
|
|
outputStream.write(buffer, 0, lastBytesDownloaded);
|
|
|
|
buffer = new byte[10240];
|
|
|
|
bytesDownloaded += lastBytesDownloaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputStream.close();
|
|
|
|
stream.close();
|
|
|
|
GuiCredits.onFinishedDownloading();
|
2012-11-05 20:29:04 +01:00
|
|
|
System.out.println("[Mekanism] Successfully updated to latest version (" + Mekanism.latestVersionNumber + ").");
|
2012-08-15 22:41:41 +02:00
|
|
|
finalize();
|
|
|
|
|
|
|
|
} catch(Throwable e)
|
|
|
|
{
|
|
|
|
GuiCredits.onErrorDownloading();
|
2012-11-05 20:29:04 +01:00
|
|
|
System.err.println("[Mekanism] Error while finishing update thread: " + e.getMessage());
|
2012-08-15 22:41:41 +02:00
|
|
|
try {
|
|
|
|
finalize();
|
|
|
|
} catch (Throwable e1) {
|
2012-11-05 20:29:04 +01:00
|
|
|
System.err.println("[Mekanism] Error while finalizing update thread: " + e1.getMessage());
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 18:48:24 +02:00
|
|
|
/**
|
2012-11-05 20:29:04 +01:00
|
|
|
* Prepares to update to the latest version of Mekanism by deleting the files "Mekanism.cfg" and "Mekanism.jar."
|
2012-09-08 18:48:24 +02:00
|
|
|
*/
|
2012-08-15 22:41:41 +02:00
|
|
|
public void prepareForDownload()
|
|
|
|
{
|
2012-11-05 20:29:04 +01:00
|
|
|
File download = new File(new StringBuilder().append(Minecraft.getMinecraftDir()).append("/mods/Mekanism.jar").toString());
|
|
|
|
File config = new File(new StringBuilder().append(Minecraft.getMinecraftDir()).append("/config/Mekanism.cfg").toString());
|
2012-09-08 18:48:24 +02:00
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
if(download.exists())
|
|
|
|
{
|
|
|
|
download.delete();
|
|
|
|
}
|
|
|
|
if(config.exists())
|
|
|
|
{
|
|
|
|
config.delete();
|
|
|
|
}
|
2012-11-05 20:29:04 +01:00
|
|
|
System.out.println("[Mekanism] Preparing to update...");
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
|
|
|
}
|