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.InputStream;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
|
2013-04-13 16:33:37 +02:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.client.gui.GuiCredits;
|
2012-11-05 20:29:04 +01:00
|
|
|
import mekanism.common.Mekanism;
|
2013-04-05 22:26:48 +02:00
|
|
|
|
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
|
|
|
|
*
|
|
|
|
*/
|
2013-04-13 16:33:37 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
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;
|
2013-04-06 19:28:59 +02:00
|
|
|
public String moduleName;
|
2012-08-15 22:41:41 +02:00
|
|
|
|
2013-04-05 22:26:48 +02:00
|
|
|
public static int modulesBeingDownloaded;
|
|
|
|
public static boolean hasUpdated;
|
|
|
|
|
2013-04-06 19:28:59 +02:00
|
|
|
public ThreadClientUpdate(String location, String name)
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2013-04-06 19:28:59 +02:00
|
|
|
moduleName = name;
|
2013-04-05 22:26:48 +02:00
|
|
|
modulesBeingDownloaded++;
|
2012-08-15 22:41:41 +02:00
|
|
|
try {
|
|
|
|
url = new URL(location);
|
2013-04-01 01:12:10 +02:00
|
|
|
setDaemon(false);
|
2012-08-15 22:41:41 +02:00
|
|
|
start();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-08-15 22:41:41 +02:00
|
|
|
public void run()
|
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
File download = new File(new StringBuilder().append(Mekanism.proxy.getMinecraftDir()).append(File.separator + "mods" + File.separator + "Mekanism" + moduleName + "-v" + Mekanism.latestVersionNumber + ".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();
|
|
|
|
|
2013-04-05 22:26:48 +02:00
|
|
|
modulesBeingDownloaded--;
|
2013-03-11 18:49:01 +01:00
|
|
|
finalize();
|
2013-10-31 19:46:14 +01:00
|
|
|
} catch(Throwable t) {
|
2012-08-15 22:41:41 +02:00
|
|
|
GuiCredits.onErrorDownloading();
|
2013-10-31 19:46:14 +01:00
|
|
|
System.err.println("[Mekanism] Error while finishing update thread: " + t.getMessage());
|
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
try {
|
2013-04-05 22:26:48 +02:00
|
|
|
modulesBeingDownloaded--;
|
2012-08-15 22:41:41 +02:00
|
|
|
finalize();
|
2013-10-31 19:46:14 +01:00
|
|
|
} catch (Throwable t1) {}
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 18:48:24 +02:00
|
|
|
/**
|
2013-04-05 22:26:48 +02:00
|
|
|
* Prepares to update to the latest version of Mekanism by removing the old files.
|
2012-09-08 18:48:24 +02:00
|
|
|
*/
|
2012-08-15 22:41:41 +02:00
|
|
|
public void prepareForDownload()
|
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
File[] modsList = new File(new StringBuilder().append(Mekanism.proxy.getMinecraftDir()).append(File.separator + "mods").toString()).listFiles();
|
2012-09-08 18:48:24 +02:00
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
for(File file : modsList)
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2013-04-06 19:28:59 +02:00
|
|
|
if(file.getName().startsWith("Mekanism" + moduleName) && file.getName().endsWith(".jar") && !file.getName().contains(Mekanism.latestVersionNumber))
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
|
|
|
file.delete();
|
|
|
|
}
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
|
2012-11-05 20:29:04 +01:00
|
|
|
System.out.println("[Mekanism] Preparing to update...");
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
|
|
|
}
|