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;
|
|
|
|
|
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
|
|
|
{
|
2013-03-11 18:49:01 +01:00
|
|
|
private int downloadType;
|
2012-08-15 22:41:41 +02:00
|
|
|
private int bytesDownloaded;
|
|
|
|
private int lastBytesDownloaded;
|
|
|
|
private byte[] buffer = new byte[10240];
|
|
|
|
private URL url;
|
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
public ThreadClientUpdate(String location, int type)
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2013-03-11 18:49:01 +01:00
|
|
|
downloadType = type;
|
2012-08-15 22:41:41 +02:00
|
|
|
try {
|
|
|
|
url = new URL(location);
|
|
|
|
setDaemon(true);
|
|
|
|
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-03-11 18:49:01 +01:00
|
|
|
String downloadName = downloadType == 0 ? "" : (downloadType == 1 ? "Generators" : "Tools");
|
|
|
|
File download = new File(new StringBuilder().append(Minecraft.getMinecraftDir()).append("/mods/Mekanism" + downloadName + "-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-03-11 18:49:01 +01:00
|
|
|
if(downloadType == 2)
|
|
|
|
{
|
|
|
|
GuiCredits.onFinishedDownloading();
|
|
|
|
System.out.println("[Mekanism] Successfully updated to latest version (" + Mekanism.latestVersionNumber + ").");
|
|
|
|
}
|
|
|
|
|
|
|
|
finalize();
|
2012-08-15 22:41:41 +02:00
|
|
|
} 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()
|
|
|
|
{
|
2013-03-11 18:49:01 +01:00
|
|
|
File[] modsList = new File(new StringBuilder().append(Minecraft.getMinecraftDir()).append("/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-03-11 18:49:01 +01:00
|
|
|
if(file.getName().startsWith("Mekanism") && file.getName().endsWith(".jar") && !file.getName().contains(Mekanism.latestVersionNumber))
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|