Refactor file downloading code

This commit is contained in:
drdev
2014-08-23 15:58:05 +00:00
parent 34e2daaeb0
commit 961cedc549
4 changed files with 29 additions and 15 deletions

View File

@@ -4,16 +4,15 @@ import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglClipboard; import com.badlogic.gdx.backends.lwjgl.LwjglClipboard;
import forge.Forge; import forge.Forge;
import forge.assets.AssetsDownloader;
import forge.interfaces.INetworkConnection; import forge.interfaces.INetworkConnection;
import forge.util.FileUtil; import forge.util.FileUtil;
import forge.util.Utils; import forge.util.Utils;
public class Main { public class Main {
private static final boolean SHARE_DESKTOP_ASSETS = true;
public static void main(String[] args) { public static void main(String[] args) {
String assetsDir = SHARE_DESKTOP_ASSETS ? "../forge-gui/" : "testAssets/"; String assetsDir = AssetsDownloader.SHARE_DESKTOP_ASSETS ? "../forge-gui/" : "testAssets/";
if (!SHARE_DESKTOP_ASSETS) { if (!AssetsDownloader.SHARE_DESKTOP_ASSETS) {
FileUtil.ensureDirectoryExists(assetsDir); FileUtil.ensureDirectoryExists(assetsDir);
} }

View File

@@ -37,7 +37,7 @@ import forge.util.FileUtil;
import forge.util.Utils; import forge.util.Utils;
public class Forge implements ApplicationListener { public class Forge implements ApplicationListener {
public static final String CURRENT_VERSION = "1.5.25.004"; public static final String CURRENT_VERSION = "1.5.25.003";
private static final ApplicationListener app = new Forge(); private static final ApplicationListener app = new Forge();
private static Clipboard clipboard; private static Clipboard clipboard;

View File

@@ -28,9 +28,11 @@ import forge.util.FileUtil;
import forge.util.gui.SOptionPane; import forge.util.gui.SOptionPane;
public class AssetsDownloader { public class AssetsDownloader {
//if not forge-gui-mobile-dev, check whether assets are up to date public static final boolean SHARE_DESKTOP_ASSETS = true; //change to false to test downloading separate assets for desktop version
//if not sharing desktop assets, check whether assets are up to date
public static boolean checkForUpdates(final SplashScreen splashScreen) { public static boolean checkForUpdates(final SplashScreen splashScreen) {
if (Gdx.app.getType() == ApplicationType.Desktop) { return true; } if (Gdx.app.getType() == ApplicationType.Desktop && SHARE_DESKTOP_ASSETS) { return true; }
splashScreen.getProgressBar().setDescription("Checking for updates..."); splashScreen.getProgressBar().setDescription("Checking for updates...");
@@ -50,6 +52,7 @@ public class AssetsDownloader {
message += " If so, you may want to connect to wifi first. The installer download is 6.5MB."; message += " If so, you may want to connect to wifi first. The installer download is 6.5MB.";
} }
if (SOptionPane.showConfirmDialog(message, "New Version Available", "Update Now", "Update Later")) { if (SOptionPane.showConfirmDialog(message, "New Version Available", "Update Now", "Update Later")) {
return false; return false;
} }
} }
@@ -136,15 +139,13 @@ public class AssetsDownloader {
return true; return true;
} }
private static void downloadAssets(final FProgressBar progressBar) { private static String downloadFile(final String desc, final String filename, final String sourceFolder, final String destFolder, final FProgressBar progressBar) {
final String destFile = ForgeConstants.ASSETS_DIR + "assets.zip";
progressBar.reset(); progressBar.reset();
progressBar.setPercentMode(true); progressBar.setPercentMode(true);
progressBar.setDescription("Downloading resource files"); progressBar.setDescription("Downloading " + desc);
try { try {
URL url = new URL("http://cardforge.org/android/releases/forge/forge-gui-android/" + Forge.CURRENT_VERSION + "/assets.zip"); URL url = new URL(sourceFolder + filename);
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.connect(); conn.connect();
@@ -155,6 +156,7 @@ public class AssetsDownloader {
InputStream input = new BufferedInputStream(url.openStream(), 8192); InputStream input = new BufferedInputStream(url.openStream(), 8192);
// output stream to write file // output stream to write file
String destFile = destFolder + filename;
OutputStream output = new FileOutputStream(destFile); OutputStream output = new FileOutputStream(destFile);
int count; int count;
@@ -170,10 +172,19 @@ public class AssetsDownloader {
output.flush(); output.flush();
output.close(); output.close();
input.close(); input.close();
return destFile;
} }
catch (final Exception ex) { catch (final Exception ex) {
Log.error("Assets", "Error downloading assets", ex); Log.error("Downloading " + desc, "Error downloading " + desc, ex);
} }
return null;
}
private static void downloadAssets(final FProgressBar progressBar) {
String assetsFile = downloadFile("resource files", "assets.zip",
"http://cardforge.org/android/releases/forge/forge-gui-android/" + Forge.CURRENT_VERSION + "/",
ForgeConstants.ASSETS_DIR, progressBar);
if (assetsFile == null) { return; }
//if assets.zip downloaded successfully, unzip into destination folder //if assets.zip downloaded successfully, unzip into destination folder
try { try {
@@ -185,7 +196,7 @@ public class AssetsDownloader {
FileUtil.deleteDirectory(resDir); FileUtil.deleteDirectory(resDir);
} }
ZipFile zipFile = new ZipFile(destFile); ZipFile zipFile = new ZipFile(assetsFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries(); Enumeration<? extends ZipEntry> entries = zipFile.entries();
progressBar.reset(); progressBar.reset();
@@ -208,7 +219,7 @@ public class AssetsDownloader {
} }
zipFile.close(); zipFile.close();
new File(destFile).delete(); new File(assetsFile).delete();
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -43,6 +43,10 @@ public enum FSkinTexture implements FImage {
return; return;
} }
} }
else {
System.err.println("Failed to load skin file: " + defaultFile);
return;
}
} }
if (repeat) { if (repeat) {
texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat); texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);