Make progress towards having download support work

This commit is contained in:
drdev
2014-05-30 04:59:32 +00:00
parent df8d38475a
commit fcbe79ee3f
7 changed files with 74 additions and 67 deletions

View File

@@ -31,6 +31,7 @@ import forge.assets.FImage;
import forge.error.BugReporter;
import forge.error.ExceptionHandler;
import forge.model.FModel;
import forge.properties.ForgeConstants;
import forge.properties.ForgePreferences;
import forge.properties.ForgePreferences.FPref;
import forge.screens.FScreen;
@@ -41,6 +42,7 @@ import forge.toolbox.FContainer;
import forge.toolbox.FDisplayObject;
import forge.toolbox.FGestureAdapter;
import forge.toolbox.FOverlay;
import forge.util.FileUtil;
import forge.util.Utils;
public class Forge implements ApplicationListener {
@@ -76,8 +78,16 @@ public class Forge implements ApplicationListener {
splashScreen = new SplashScreen();
final ForgePreferences prefs = new ForgePreferences();
FSkin.loadLight(prefs.getPref(FPref.UI_SKIN), splashScreen);
System.err.println("testing2323");
String skinName;
if (FileUtil.doesFileExist(ForgeConstants.MAIN_PREFS_FILE)) {
skinName = new ForgePreferences().getPref(FPref.UI_SKIN);
}
else {
skinName = "default"; //use default skin if preferences file doesn't exist yet
}
FSkin.loadLight(skinName, splashScreen);
//load model on background thread (using progress bar to report progress)
FThreads.invokeInBackgroundThread(new Runnable() {
@@ -170,7 +180,7 @@ public class Forge implements ApplicationListener {
Animation.advanceAll();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear the screen.
FContainer screen = currentScreen;
if (screen == null) {
screen = splashScreen;

View File

@@ -1,16 +1,14 @@
package forge.assets;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -56,7 +54,6 @@ public class AssetsDownloader {
FThreads.invokeInEdtAndWait(new Runnable() {
@Override
public void run() {
splashScreen.getProgressBar().setShowProgressTrail(false);
FSkin.reloadAfterAssetsDownload(splashScreen);
}
});
@@ -67,94 +64,82 @@ public class AssetsDownloader {
}
private static void downloadAssets(final FProgressBar progressBar) {
FThreads.invokeInEdtLater(new Runnable() {
@Override
public void run() {
progressBar.setShowProgressTrail(true);
progressBar.setDescription("Updating resource files...\n(This may take several minutes)");
}
});
String url = "http://cardforge.org/android/releases/forge/forge-gui-android/" + Forge.CURRENT_VERSION + "/assets.zip";
final File destDir = new File(ForgeConstants.ASSETS_DIR);
final File fileDest = new File(destDir.getAbsolutePath() + "/assets.zip");
final File resDir = new File(ForgeConstants.RES_DIR);
final String destFile = ForgeConstants.ASSETS_DIR + "assets.zip";
try {
File resDir = new File(ForgeConstants.RES_DIR);
if (resDir.exists()) {
resDir.delete(); //attempt to delete previous res directory if to be rebuilt
//attempt to delete previous res directory if to be rebuilt
FileUtil.deleteDirectory(resDir);
}
}
catch (Exception e) {
e.printStackTrace();
}
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
// test for folder existence
if (!destDir.exists() && !destDir.mkdir()) { // create folder if not found
System.out.println("Can't create folder" + destDir.getAbsolutePath());
}
progressBar.reset();
progressBar.setPercentMode(true);
progressBar.setDescription("Downloading resource files");
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(Proxy.NO_PROXY);
// don't allow redirections here -- they indicate 'file not found' on the server
conn.setInstanceFollowRedirects(false);
try {
URL url = new URL("http://cardforge.org/android/releases/forge/forge-gui-android/" + Forge.CURRENT_VERSION + "/assets.zip");
URLConnection conn = url.openConnection();
conn.connect();
if (conn.getResponseCode() != 200) {
conn.disconnect();
System.out.println("Could not download assets.zip");
return;
}
long contentLength = conn.getContentLength();
progressBar.setMaximum(100);
rbc = Channels.newChannel(conn.getInputStream());
fos = new FileOutputStream(fileDest);
fos.getChannel().transferFrom(rbc, 0, 1 << 27);
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// output stream to write file
OutputStream output = new FileOutputStream(destFile);
int count;
long total = 0;
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
total += count;
progressBar.setValue((int)(total / contentLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
catch (final Exception ex) {
Log.error("Assets", "Error downloading assets", ex);
}
finally {
if (rbc != null) {
try {
rbc.close();
}
catch (IOException e) {
System.out.println("error closing input stream");
}
}
if (fos != null) {
try {
fos.close();
}
catch (IOException e) {
System.out.println("error closing output stream");
}
}
}
//if assets.zip downloaded successfully, unzip into destination folder
try {
ZipFile zipFile = new ZipFile(fileDest);
ZipFile zipFile = new ZipFile(destFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
progressBar.reset();
progressBar.setPercentMode(true);
progressBar.setDescription("Unzipping resource files");
progressBar.setMaximum(zipFile.size());
int count = 0;
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
String path = ForgeConstants.ASSETS_DIR + entry.getName();
if (entry.isDirectory()) {
new File(path).mkdir();
progressBar.setValue(++count);
continue;
}
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(path)));
progressBar.setValue(++count);
}
zipFile.close();
fileDest.delete();
new File(destFile).delete();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

View File

@@ -284,9 +284,9 @@ public class FSkin {
//if skins directory doesn't exists, create a minimum directory containing skin files for the splash screen
FileUtil.ensureDirectoryExists(ForgeConstants.DEFAULT_SKINS_DIR);
final FileHandle defaultDir = Gdx.files.absolute(ForgeConstants.DEFAULT_SKINS_DIR);
Gdx.files.internal("bg_splash.png").copyTo(defaultDir.child("bg_splash.png"));
Gdx.files.internal("bg_texture.jpg").copyTo(defaultDir.child("bg_texture.jpg"));
Gdx.files.internal("font1.ttf").copyTo(defaultDir.child("font1.ttf"));
Gdx.files.internal(ForgeConstants.SPLASH_BG_FILE).copyTo(defaultDir.child(ForgeConstants.SPLASH_BG_FILE));
Gdx.files.internal(ForgeConstants.TEXTURE_BG_FILE).copyTo(defaultDir.child(ForgeConstants.TEXTURE_BG_FILE));
Gdx.files.internal(ForgeConstants.FONT_FILE).copyTo(defaultDir.child(ForgeConstants.FONT_FILE));
mySkins.add("default");
needReloadAfterAssetsDownloaded = true; //flag that skins need to be reloaded after assets downloaded
}

View File

@@ -17,7 +17,7 @@ public class BugReportDialog extends FOptionPane {
private static boolean dialogShown;
public static void show(String title, String text, boolean showExitAppBtn) {
if (dialogShown) { return; }
if (dialogShown || Forge.getCurrentScreen() == null) { return; } //don't allow showing if Forge not finished initializing yet
dialogShown = true;
BugReportDialog dialog = new BugReportDialog(title, text, showExitAppBtn);