Custom cache downloader
This can be added to every server, no matter the revision or which base etc.
The features:
- Removes junk files
- Shows download progress
- Easy to implement
To see the look of this feel free to watch the video:
How to add?
Add the class '
CacheDownloader':
[SPOIL]
Code:
package rs;
import java.io.File;
import javax.swing.JFrame;
import rs.sign.signlink;
public class CacheDownloader {
private static JFrame frame;
private File cache = new File(signlink.findcachedir());
private File dlVerification = new File(signlink.findcachedir() + "doneDownload");
public static void closeFrame() {
frame.dispose();
}
public boolean dlRequired() {
if (cache.exists() && dlVerification.exists() && !dlVerification.isDirectory()) {
return false;
} else {
return true;
}
}
public CacheDownloader(String[] args) {
if (dlRequired()) {
downloadCache(args);
} else {
client.runGame(args);
}
}
private void downloadCache(String[] args) {
removeOtherCache();
createFrame(args);
}
/**
* Removes previous caches
*/
private void removeOtherCache() {
if (cache.exists()) {
String[] files = cache.list();
for (String s: files) {
File currentFile = new File(cache.getPath(),s);
currentFile.delete();
System.out.println("Delted file: " + currentFile.getPath());
}
}
cache.delete();
}
private void createFrame(String[] args) {
frame = new JFrame("Game Initializer");
frame.add(new CacheFrame(cache.getPath(), args));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
[/SPOIL]
Add the class '
CacheFrame':
[SPOIL]
Code:
package rs;
import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CacheFrame extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel label;
private File dlLocation;
public CacheFrame(String locationPath, String[] args) {
if (label == null) {
label = new JLabel("Retrieving cache size...");
label.setHorizontalAlignment(JLabel.CENTER);
label.setPreferredSize(new Dimension(400, 50));
label.setForeground(Color.WHITE);
}
add(label);
setPreferredSize(new Dimension(400, 60));
setBackground(Color.BLACK);
Thread t = new Thread(new Runnable() {
public void run() {
redirectory(locationPath);
download("https://dl.dropboxusercontent.com/u/22332789/TempCache5944.zip");
unzipCache();
finishAndClean(locationPath, args);
}
});
t.start();
}
private void finishAndClean(String locationPath, String[] args) {
File confirmFile = new File(locationPath + File.separator
+ "doneDownload");
try {
confirmFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
dlLocation.delete();
client.runGame(args);
CacheDownloader.closeFrame();
}
private void redirectory(String locationPath) {
File f = new File(locationPath);
dlLocation = new File(f.getParent().toString() + "/TempCache5944.zip");
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public void download(String downloadPath) {
BufferedInputStream in = null;
FileOutputStream out = null;
try {
URL url = new URL(downloadPath);
URLConnection conn = url.openConnection();
int size = conn.getContentLength();
if (size < 0) {
System.out.println("Check your connection or contact us");
label.setText("Check your connection or contact us");
} else {
System.out.println("File size: " + size);
double mb = (size);
double fileSize = mb / 1048576;
label.setText("File size: " + round(fileSize, 2) + "MB");
Thread.sleep(2000);
}
in = new BufferedInputStream(url.openStream());
out = new FileOutputStream(dlLocation);
byte data[] = new byte[1024];
int count;
double sumCount = 0.0;
while ((count = in.read(data, 0, 1024)) != -1) {
out.write(data, 0, count);
sumCount += count;
int progress = (int) (sumCount / size * 100.0);
if (size > 0) {
label.setText("Downloading cache: " + progress + "%");
}
}
label.setText("Cache has been downloaded!");
in.close();
out.close();
Thread.sleep(2000);
} catch (Exception e) {
label.setText("Check your connection or contact us");
e.printStackTrace();
}
}
private void unzipCache() {
byte[] buffer = new byte[1024];
try {
ZipInputStream zis = new ZipInputStream(new FileInputStream(dlLocation));
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(dlLocation.getParent() + File.separator
+ "cache" + File.separator + fileName);
System.out.println("File Unzipping: " + newFile);
System.out.println("error: " + newFile.getParent());
new File(newFile.getParent()).mkdirs();
FileOutputStream zos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
label.setText("Extracted: " + fileName + "...");
}
zos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Done with extracting!");
label.setText("Done with extracting!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
[/SPOIL]
In client.java add this method:
Code:
public static void runGame(String args[]) {
}
Copy all content from the main method between the brackets of the runGame method you just made.
Now in client.java replace your main method with this one:
Code:
public static void main(String args[]) {
new CacheDownloader(args);
}
This is for the code part.
Now you have to zip your cache it's content (
yes only the content of your cache!) You may only use zip so not gzip or rar etc.
You have to call the cache 'TempCache5944.zip'. If you don't like this name, eventhough the file is only a temporary file that will be deleted after the process, you can change it, but you have to change the desired code too if you decide to change the name.