2012-08-15 22:41:41 +02:00
|
|
|
package net.uberkat.obsidian.client;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import net.minecraft.src.ModLoader;
|
2012-08-26 15:48:42 +02:00
|
|
|
import net.uberkat.obsidian.common.ObsidianIngots;
|
2012-08-15 22:41:41 +02:00
|
|
|
/**
|
|
|
|
* Thread that downloads the latest release of Obsidian Ingots. The older file is deleted and the newly downloaded file takes it's place.
|
|
|
|
* @author AidanBrady
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ThreadUpdate extends Thread
|
|
|
|
{
|
|
|
|
private int bytesDownloaded;
|
|
|
|
private int lastBytesDownloaded;
|
|
|
|
private byte[] buffer = new byte[10240];
|
|
|
|
private URL url;
|
|
|
|
|
|
|
|
public ThreadUpdate(String location)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
url = new URL(location);
|
|
|
|
setDaemon(true);
|
|
|
|
start();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
File download = new File(new StringBuilder().append(ModLoader.getMinecraftInstance().getMinecraftDir()).append("/mods/ObsidianIngots.jar").toString());
|
|
|
|
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-08-26 15:48:42 +02:00
|
|
|
System.out.println("[ObsidianIngots] Successfully updated to latest version (" + ObsidianIngots.latestVersionNumber + ").");
|
2012-08-15 22:41:41 +02:00
|
|
|
finalize();
|
|
|
|
|
|
|
|
} catch(Throwable e)
|
|
|
|
{
|
|
|
|
GuiCredits.onErrorDownloading();
|
|
|
|
System.err.println("[ObsidianIngots] Error while finishing update thread: " + e.getMessage());
|
|
|
|
try {
|
|
|
|
finalize();
|
|
|
|
} catch (Throwable e1) {
|
|
|
|
System.err.println("[ObsidianIngots] Error while finalizing update thread: " + e1.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 18:48:24 +02:00
|
|
|
/**
|
|
|
|
* Prepares to update to the latest version of Obsidian Ingots by deleting the files "ObsidianIngots.cfg" and "ObsidianIngots.jar."
|
|
|
|
*/
|
2012-08-15 22:41:41 +02:00
|
|
|
public void prepareForDownload()
|
|
|
|
{
|
|
|
|
File download = new File(new StringBuilder().append(ModLoader.getMinecraftInstance().getMinecraftDir()).append("/mods/ObsidianIngots.jar").toString());
|
2012-09-08 18:48:24 +02:00
|
|
|
File config = new File(new StringBuilder().append(ModLoader.getMinecraftInstance().getMinecraftDir()).append("/config/ObsidianIngots.cfg").toString());
|
|
|
|
|
2012-08-15 22:41:41 +02:00
|
|
|
if(download.exists())
|
|
|
|
{
|
|
|
|
download.delete();
|
|
|
|
}
|
|
|
|
if(config.exists())
|
|
|
|
{
|
|
|
|
config.delete();
|
|
|
|
}
|
|
|
|
System.out.println("[ObsidianIngots] Preparing to update...");
|
|
|
|
}
|
|
|
|
}
|