mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-15 18:28:00 +00:00
Make progress towards being able to automatically download assets files on startup
This commit is contained in:
@@ -23,6 +23,7 @@ import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
|
||||
import com.badlogic.gdx.utils.Clipboard;
|
||||
|
||||
import forge.assets.AssetsDownloader;
|
||||
import forge.assets.FSkin;
|
||||
import forge.assets.FSkinColor;
|
||||
import forge.assets.FSkinFont;
|
||||
@@ -30,6 +31,8 @@ import forge.assets.FImage;
|
||||
import forge.error.BugReporter;
|
||||
import forge.error.ExceptionHandler;
|
||||
import forge.model.FModel;
|
||||
import forge.properties.ForgePreferences;
|
||||
import forge.properties.ForgePreferences.FPref;
|
||||
import forge.screens.FScreen;
|
||||
import forge.screens.SplashScreen;
|
||||
import forge.screens.home.HomeScreen;
|
||||
@@ -73,10 +76,16 @@ public class Forge implements ApplicationListener {
|
||||
|
||||
splashScreen = new SplashScreen();
|
||||
|
||||
final ForgePreferences prefs = new ForgePreferences();
|
||||
FSkin.loadLight(prefs.getPref(FPref.UI_SKIN), splashScreen);
|
||||
|
||||
//load model on background thread (using progress bar to report progress)
|
||||
FThreads.invokeInBackgroundThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
//see if app or assets need updating
|
||||
AssetsDownloader.checkForUpdates(splashScreen.getProgressBar());
|
||||
|
||||
FModel.initialize(splashScreen.getProgressBar());
|
||||
|
||||
splashScreen.getProgressBar().setDescription("Loading fonts");
|
||||
|
||||
155
forge-gui-mobile/src/forge/assets/AssetsDownloader.java
Normal file
155
forge-gui-mobile/src/forge/assets/AssetsDownloader.java
Normal file
@@ -0,0 +1,155 @@
|
||||
package forge.assets;
|
||||
|
||||
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.util.Enumeration;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.esotericsoftware.minlog.Log;
|
||||
|
||||
import forge.Forge;
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.toolbox.FProgressBar;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.TextUtil;
|
||||
|
||||
public class AssetsDownloader {
|
||||
//if not forge-gui-mobile-dev, check whether assets are up to date
|
||||
public static void checkForUpdates(final FProgressBar progressBar) {
|
||||
//if (Gdx.app.getType() == ApplicationType.Desktop) { return; }
|
||||
|
||||
//TODO see if app needs updating
|
||||
//progressBar.setDescription("Checking for updates");
|
||||
|
||||
//set if assets need updating
|
||||
File versionFile = new File(ForgeConstants.ASSETS_DIR + "version.txt");
|
||||
if (!versionFile.exists()) {
|
||||
try {
|
||||
versionFile.createNewFile();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Gdx.app.exit(); //can't continue if this fails
|
||||
}
|
||||
}
|
||||
else if (Forge.CURRENT_VERSION.equals(TextUtil.join(FileUtil.readFile(versionFile), "\n")) && !FSkin.assetsDownloadNeeded()) {
|
||||
return; //if version matches what had been previously saved and FSkin isn't requesting assets download, no need to download assets
|
||||
}
|
||||
|
||||
downloadAssets(progressBar);
|
||||
|
||||
//save version string to file once assets finish downloading
|
||||
//so they don't need to be re-downloaded until you upgrade again
|
||||
FileUtil.writeFile(versionFile, Forge.CURRENT_VERSION);
|
||||
}
|
||||
|
||||
private static void downloadAssets(final FProgressBar progressBar) {
|
||||
progressBar.setDescription("Downloading resource files");
|
||||
|
||||
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);
|
||||
try {
|
||||
if (resDir.exists()) {
|
||||
resDir.delete(); //attempt to delete previous res directory if to be rebuilt
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
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);
|
||||
conn.connect();
|
||||
|
||||
if (conn.getResponseCode() != 200) {
|
||||
conn.disconnect();
|
||||
System.out.println("Could not download assets.zip");
|
||||
return;
|
||||
}
|
||||
|
||||
rbc = Channels.newChannel(conn.getInputStream());
|
||||
fos = new FileOutputStream(fileDest);
|
||||
fos.getChannel().transferFrom(rbc, 0, 1 << 27);
|
||||
}
|
||||
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 download successfully, unzip into destination folder
|
||||
progressBar.setDescription("Unzipping resource files");
|
||||
|
||||
try {
|
||||
ZipFile zipFile = new ZipFile(fileDest);
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = (ZipEntry)entries.nextElement();
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
(new File(entry.getName())).mkdir();
|
||||
continue;
|
||||
}
|
||||
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(entry.getName())));
|
||||
}
|
||||
|
||||
zipFile.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static final void copyInputStream(InputStream in, OutputStream out) throws IOException{
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
|
||||
while((len = in.read(buffer)) >= 0) {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
|
||||
in.close();
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ public class FSkin {
|
||||
private static String preferredFontDir;
|
||||
private static String preferredName;
|
||||
private static boolean loaded = false;
|
||||
private static boolean needReloadAfterAssetsDownloaded = false;
|
||||
|
||||
public static void changeSkin(final String skinName) {
|
||||
final ForgePreferences prefs = FModel.getPreferences();
|
||||
@@ -233,6 +234,10 @@ public class FSkin {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean assetsDownloadNeeded() {
|
||||
return needReloadAfterAssetsDownloaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name.
|
||||
*
|
||||
@@ -270,7 +275,14 @@ public class FSkin {
|
||||
|
||||
final FileHandle dir = Gdx.files.absolute(ForgeConstants.SKINS_DIR);
|
||||
if (!dir.exists() || !dir.isDirectory()) {
|
||||
System.err.println("FSkin > can't find skins directory!");
|
||||
//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"));
|
||||
mySkins.add("default");
|
||||
needReloadAfterAssetsDownloaded = true; //flag that skins need to be reloaded after assets downloaded
|
||||
}
|
||||
else {
|
||||
for (FileHandle skinFile : dir.list()) {
|
||||
|
||||
@@ -1,25 +1,12 @@
|
||||
package forge.screens;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.badlogic.gdx.Application.ApplicationType;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
|
||||
|
||||
import forge.Forge;
|
||||
import forge.Forge.Graphics;
|
||||
import forge.assets.FSkin;
|
||||
import forge.assets.FSkinFont;
|
||||
import forge.assets.FSkinTexture;
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.properties.ForgePreferences;
|
||||
import forge.properties.ForgePreferences.FPref;
|
||||
import forge.toolbox.FContainer;
|
||||
import forge.toolbox.FProgressBar;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.TextUtil;
|
||||
|
||||
public class SplashScreen extends FContainer {
|
||||
private TextureRegion background;
|
||||
@@ -29,11 +16,6 @@ public class SplashScreen extends FContainer {
|
||||
public SplashScreen() {
|
||||
progressBar = new FProgressBar();
|
||||
progressBar.setDescription("Welcome to Forge");
|
||||
|
||||
checkForAssets();
|
||||
|
||||
final ForgePreferences prefs = new ForgePreferences();
|
||||
FSkin.loadLight(prefs.getPref(FPref.UI_SKIN), this);
|
||||
}
|
||||
|
||||
public FProgressBar getProgressBar() {
|
||||
@@ -87,27 +69,4 @@ public class SplashScreen extends FContainer {
|
||||
progressBar.setBounds(x + padding, y, w - 2 * padding, pbHeight);
|
||||
g.draw(progressBar);
|
||||
}
|
||||
|
||||
//if not forge-gui-mobile-dev, check whether assets are up to date
|
||||
private void checkForAssets() {
|
||||
if (Gdx.app.getType() == ApplicationType.Desktop) { return; }
|
||||
|
||||
File versionFile = new File(ForgeConstants.ASSETS_DIR + "version.txt");
|
||||
if (!versionFile.exists()) {
|
||||
try {
|
||||
versionFile.createNewFile();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Gdx.app.exit(); //can't continue if this fails
|
||||
}
|
||||
}
|
||||
else if (Forge.CURRENT_VERSION.equals(TextUtil.join(FileUtil.readFile(versionFile), "\n"))) {
|
||||
return; //if version matches what had been previously saved, no need to download assets
|
||||
}
|
||||
|
||||
//save version string to file once assets finish downloading
|
||||
//so they don't need to be re-downloaded until you upgrade again
|
||||
FileUtil.writeFile(versionFile, Forge.CURRENT_VERSION);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user