2012-11-05 20:29:04 +01:00
|
|
|
package mekanism.client;
|
2012-08-15 22:41:41 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
2014-01-12 04:19:51 +01:00
|
|
|
import java.io.FileInputStream;
|
2012-08-15 22:41:41 +02:00
|
|
|
import java.io.FileOutputStream;
|
2014-01-12 04:19:51 +01:00
|
|
|
import java.io.IOException;
|
2012-08-15 22:41:41 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.URL;
|
2014-01-12 04:19:51 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipInputStream;
|
2012-08-15 22:41:41 +02:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
import mekanism.api.EnumColor;
|
2013-08-27 00:49:32 +02:00
|
|
|
import mekanism.client.gui.GuiCredits;
|
2014-01-12 04:19:51 +01:00
|
|
|
import mekanism.common.IModule;
|
2012-11-05 20:29:04 +01:00
|
|
|
import mekanism.common.Mekanism;
|
2014-01-12 04:19:51 +01:00
|
|
|
import mekanism.common.Version;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
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];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
private static File modsDir = new File(Mekanism.proxy.getMinecraftDir(), "mods");
|
|
|
|
private static File tempDir = new File(modsDir, "temp");
|
|
|
|
private static URL zipUrl = createURL();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-04-05 22:26:48 +02:00
|
|
|
public static boolean hasUpdated;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
public ThreadClientUpdate()
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2014-01-12 04:19:51 +01:00
|
|
|
setDaemon(false);
|
|
|
|
start();
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-11-06 16:44:14 +01:00
|
|
|
@Override
|
2012-08-15 22:41:41 +02:00
|
|
|
public void run()
|
2014-03-08 02:00:25 +01:00
|
|
|
{
|
2012-08-15 22:41:41 +02:00
|
|
|
try {
|
2014-01-12 04:19:51 +01:00
|
|
|
deleteTemp();
|
|
|
|
createTemp();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
File download = new File(tempDir, "builds.zip");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
prepareForDownload();
|
|
|
|
download.createNewFile();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
GuiCredits.updateInfo("Downloading...");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
FileOutputStream outputStream = new FileOutputStream(download.getAbsolutePath());
|
2014-01-12 04:19:51 +01:00
|
|
|
InputStream stream = zipUrl.openStream();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
while((lastBytesDownloaded = stream.read(buffer)) > 0)
|
|
|
|
{
|
|
|
|
outputStream.write(buffer, 0, lastBytesDownloaded);
|
|
|
|
buffer = new byte[10240];
|
|
|
|
bytesDownloaded += lastBytesDownloaded;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
outputStream.close();
|
|
|
|
stream.close();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:24:15 +01:00
|
|
|
if(Mekanism.versionNumber.comparedState(Version.get(Mekanism.latestVersionNumber)) == -1)
|
2014-01-12 04:19:51 +01:00
|
|
|
{
|
|
|
|
ZipInputStream zip = new ZipInputStream(new FileInputStream(download));
|
|
|
|
deployEntry(zip, "Mekanism-");
|
|
|
|
zip.close();
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
for(IModule module : Mekanism.modulesLoaded)
|
|
|
|
{
|
2014-01-12 04:24:15 +01:00
|
|
|
if(module.getVersion().comparedState(Version.get(Mekanism.latestVersionNumber)) == -1)
|
2014-03-08 02:00:25 +01:00
|
|
|
{
|
2014-01-12 04:19:51 +01:00
|
|
|
ZipInputStream zip = new ZipInputStream(new FileInputStream(download));
|
|
|
|
deployEntry(zip, "Mekanism" + module.getName());
|
|
|
|
zip.close();
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
deleteTemp();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
hasUpdated = true;
|
|
|
|
GuiCredits.updateInfo("Update installed, reboot Minecraft for changes.");
|
|
|
|
System.out.println("[Mekanism] Successfully updated to latest version (" + Mekanism.latestVersionNumber + ").");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-03-11 18:49:01 +01:00
|
|
|
finalize();
|
2013-10-31 19:46:14 +01:00
|
|
|
} catch(Throwable t) {
|
2014-01-12 04:19:51 +01:00
|
|
|
GuiCredits.updateInfo(EnumColor.DARK_RED + "Error updating.");
|
|
|
|
hasUpdated = true;
|
2013-10-31 19:46:14 +01:00
|
|
|
System.err.println("[Mekanism] Error while finishing update thread: " + t.getMessage());
|
2014-01-12 04:19:51 +01:00
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
private void deployEntry(ZipInputStream zip, String filePrefix) throws IOException
|
|
|
|
{
|
|
|
|
byte[] zipBuffer = new byte[1024];
|
|
|
|
ZipEntry entry = zip.getNextEntry();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
while(entry != null)
|
|
|
|
{
|
|
|
|
if(entry.isDirectory())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
if(entry.getName().contains(filePrefix))
|
|
|
|
{
|
|
|
|
File modFile = new File(modsDir, entry.getName().replace("output/", ""));
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
if(modFile.exists())
|
|
|
|
{
|
|
|
|
modFile.delete();
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
modFile.createNewFile();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
FileOutputStream outStream = new FileOutputStream(modFile);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
while((len = zip.read(zipBuffer)) > 0)
|
|
|
|
{
|
|
|
|
outStream.write(zipBuffer, 0, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
zip.closeEntry();
|
2014-01-12 04:19:51 +01:00
|
|
|
outStream.close();
|
|
|
|
break;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
entry = zip.getNextEntry();
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
private void createTemp() throws IOException
|
|
|
|
{
|
|
|
|
if(!tempDir.exists())
|
|
|
|
{
|
|
|
|
tempDir.mkdir();
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
private void deleteTemp() throws IOException
|
|
|
|
{
|
|
|
|
if(tempDir.exists())
|
|
|
|
{
|
|
|
|
clearFiles(tempDir);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
private void clearFiles(File file)
|
|
|
|
{
|
|
|
|
if(file.isDirectory())
|
|
|
|
{
|
|
|
|
for(File sub : file.listFiles())
|
|
|
|
{
|
|
|
|
clearFiles(sub);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
file.delete();
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
private void prepareForDownload()
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2013-07-20 18:10:14 +02:00
|
|
|
File[] modsList = new File(new StringBuilder().append(Mekanism.proxy.getMinecraftDir()).append(File.separator + "mods").toString()).listFiles();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
if(Mekanism.versionNumber.comparedState(Version.get(Mekanism.latestVersionNumber)) == -1)
|
|
|
|
{
|
|
|
|
for(File file : modsList)
|
|
|
|
{
|
|
|
|
if(file.getName().startsWith("Mekanism-") && file.getName().endsWith(".jar") && !file.getName().contains(Mekanism.latestVersionNumber))
|
|
|
|
{
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
for(IModule module : Mekanism.modulesLoaded)
|
2012-08-15 22:41:41 +02:00
|
|
|
{
|
2014-01-12 04:19:51 +01:00
|
|
|
for(File file : modsList)
|
2013-03-11 18:49:01 +01:00
|
|
|
{
|
2014-01-12 04:19:51 +01:00
|
|
|
if(file.getName().startsWith("Mekanism" + module.getName()) && file.getName().endsWith(".jar") && !file.getName().contains(Mekanism.latestVersionNumber))
|
|
|
|
{
|
|
|
|
file.delete();
|
|
|
|
}
|
2013-03-11 18:49:01 +01:00
|
|
|
}
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|
2014-03-08 02:00:25 +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
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
private static URL createURL()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return new URL("http://ci.aidancbrady.com/job/Mekanism/Recommended/artifact/*zip*/archive.zip");
|
|
|
|
} catch(Exception e) {}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-12 04:19:51 +01:00
|
|
|
return null;
|
|
|
|
}
|
2012-08-15 22:41:41 +02:00
|
|
|
}
|