mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-15 10:18:01 +00:00
Make progress towards being able to automatically download assets files on startup
This commit is contained in:
7
.gitattributes
vendored
7
.gitattributes
vendored
@@ -645,6 +645,9 @@ forge-gui-android/.settings/org.eclipse.core.resources.prefs -text
|
|||||||
forge-gui-android/.settings/org.eclipse.jdt.core.prefs -text
|
forge-gui-android/.settings/org.eclipse.jdt.core.prefs -text
|
||||||
forge-gui-android/.settings/org.eclipse.m2e.core.prefs -text
|
forge-gui-android/.settings/org.eclipse.m2e.core.prefs -text
|
||||||
forge-gui-android/AndroidManifest.xml -text
|
forge-gui-android/AndroidManifest.xml -text
|
||||||
|
forge-gui-android/assets/bg_splash.png -text
|
||||||
|
forge-gui-android/assets/bg_texture.jpg -text
|
||||||
|
forge-gui-android/assets/font1.ttf -text
|
||||||
forge-gui-android/ic_launcher-web.png -text
|
forge-gui-android/ic_launcher-web.png -text
|
||||||
forge-gui-android/libs/android-support-v4.jar -text
|
forge-gui-android/libs/android-support-v4.jar -text
|
||||||
forge-gui-android/libs/armeabi-v7a/libgdx-freetype.so -text
|
forge-gui-android/libs/armeabi-v7a/libgdx-freetype.so -text
|
||||||
@@ -1066,6 +1069,9 @@ forge-gui-mobile-dev/.classpath -text
|
|||||||
forge-gui-mobile-dev/.project -text
|
forge-gui-mobile-dev/.project -text
|
||||||
forge-gui-mobile-dev/.settings/org.eclipse.jdt.core.prefs -text
|
forge-gui-mobile-dev/.settings/org.eclipse.jdt.core.prefs -text
|
||||||
forge-gui-mobile-dev/.settings/org.eclipse.m2e.core.prefs -text
|
forge-gui-mobile-dev/.settings/org.eclipse.m2e.core.prefs -text
|
||||||
|
forge-gui-mobile-dev/bg_splash.png -text
|
||||||
|
forge-gui-mobile-dev/bg_texture.jpg -text
|
||||||
|
forge-gui-mobile-dev/font1.ttf -text
|
||||||
forge-gui-mobile-dev/libs/gdx-backend-lwjgl-natives.jar -text
|
forge-gui-mobile-dev/libs/gdx-backend-lwjgl-natives.jar -text
|
||||||
forge-gui-mobile-dev/libs/gdx-backend-lwjgl-sources.jar -text
|
forge-gui-mobile-dev/libs/gdx-backend-lwjgl-sources.jar -text
|
||||||
forge-gui-mobile-dev/libs/gdx-backend-lwjgl.jar -text
|
forge-gui-mobile-dev/libs/gdx-backend-lwjgl.jar -text
|
||||||
@@ -1083,6 +1089,7 @@ forge-gui-mobile/libs/gdx.jar -text
|
|||||||
forge-gui-mobile/pom.xml -text
|
forge-gui-mobile/pom.xml -text
|
||||||
forge-gui-mobile/src/forge/Forge.java -text
|
forge-gui-mobile/src/forge/Forge.java -text
|
||||||
forge-gui-mobile/src/forge/GuiMobile.java -text
|
forge-gui-mobile/src/forge/GuiMobile.java -text
|
||||||
|
forge-gui-mobile/src/forge/assets/AssetsDownloader.java -text
|
||||||
forge-gui-mobile/src/forge/assets/BitmapFontWriter.java -text
|
forge-gui-mobile/src/forge/assets/BitmapFontWriter.java -text
|
||||||
forge-gui-mobile/src/forge/assets/FImage.java -text
|
forge-gui-mobile/src/forge/assets/FImage.java -text
|
||||||
forge-gui-mobile/src/forge/assets/FSkin.java -text
|
forge-gui-mobile/src/forge/assets/FSkin.java -text
|
||||||
|
|||||||
BIN
forge-gui-android/assets/bg_splash.png
Normal file
BIN
forge-gui-android/assets/bg_splash.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 208 KiB |
BIN
forge-gui-android/assets/bg_texture.jpg
Normal file
BIN
forge-gui-android/assets/bg_texture.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
BIN
forge-gui-android/assets/font1.ttf
Normal file
BIN
forge-gui-android/assets/font1.ttf
Normal file
Binary file not shown.
BIN
forge-gui-mobile-dev/bg_splash.png
Normal file
BIN
forge-gui-mobile-dev/bg_splash.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 208 KiB |
BIN
forge-gui-mobile-dev/bg_texture.jpg
Normal file
BIN
forge-gui-mobile-dev/bg_texture.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
BIN
forge-gui-mobile-dev/font1.ttf
Normal file
BIN
forge-gui-mobile-dev/font1.ttf
Normal file
Binary file not shown.
@@ -23,6 +23,7 @@ import com.badlogic.gdx.math.Vector3;
|
|||||||
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
|
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
|
||||||
import com.badlogic.gdx.utils.Clipboard;
|
import com.badlogic.gdx.utils.Clipboard;
|
||||||
|
|
||||||
|
import forge.assets.AssetsDownloader;
|
||||||
import forge.assets.FSkin;
|
import forge.assets.FSkin;
|
||||||
import forge.assets.FSkinColor;
|
import forge.assets.FSkinColor;
|
||||||
import forge.assets.FSkinFont;
|
import forge.assets.FSkinFont;
|
||||||
@@ -30,6 +31,8 @@ import forge.assets.FImage;
|
|||||||
import forge.error.BugReporter;
|
import forge.error.BugReporter;
|
||||||
import forge.error.ExceptionHandler;
|
import forge.error.ExceptionHandler;
|
||||||
import forge.model.FModel;
|
import forge.model.FModel;
|
||||||
|
import forge.properties.ForgePreferences;
|
||||||
|
import forge.properties.ForgePreferences.FPref;
|
||||||
import forge.screens.FScreen;
|
import forge.screens.FScreen;
|
||||||
import forge.screens.SplashScreen;
|
import forge.screens.SplashScreen;
|
||||||
import forge.screens.home.HomeScreen;
|
import forge.screens.home.HomeScreen;
|
||||||
@@ -73,10 +76,16 @@ public class Forge implements ApplicationListener {
|
|||||||
|
|
||||||
splashScreen = new SplashScreen();
|
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)
|
//load model on background thread (using progress bar to report progress)
|
||||||
FThreads.invokeInBackgroundThread(new Runnable() {
|
FThreads.invokeInBackgroundThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
//see if app or assets need updating
|
||||||
|
AssetsDownloader.checkForUpdates(splashScreen.getProgressBar());
|
||||||
|
|
||||||
FModel.initialize(splashScreen.getProgressBar());
|
FModel.initialize(splashScreen.getProgressBar());
|
||||||
|
|
||||||
splashScreen.getProgressBar().setDescription("Loading fonts");
|
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 preferredFontDir;
|
||||||
private static String preferredName;
|
private static String preferredName;
|
||||||
private static boolean loaded = false;
|
private static boolean loaded = false;
|
||||||
|
private static boolean needReloadAfterAssetsDownloaded = false;
|
||||||
|
|
||||||
public static void changeSkin(final String skinName) {
|
public static void changeSkin(final String skinName) {
|
||||||
final ForgePreferences prefs = FModel.getPreferences();
|
final ForgePreferences prefs = FModel.getPreferences();
|
||||||
@@ -233,6 +234,10 @@ public class FSkin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean assetsDownloadNeeded() {
|
||||||
|
return needReloadAfterAssetsDownloaded;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the name.
|
* Gets the name.
|
||||||
*
|
*
|
||||||
@@ -270,7 +275,14 @@ public class FSkin {
|
|||||||
|
|
||||||
final FileHandle dir = Gdx.files.absolute(ForgeConstants.SKINS_DIR);
|
final FileHandle dir = Gdx.files.absolute(ForgeConstants.SKINS_DIR);
|
||||||
if (!dir.exists() || !dir.isDirectory()) {
|
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 {
|
else {
|
||||||
for (FileHandle skinFile : dir.list()) {
|
for (FileHandle skinFile : dir.list()) {
|
||||||
|
|||||||
@@ -1,25 +1,12 @@
|
|||||||
package forge.screens;
|
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.TextureRegion;
|
||||||
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
|
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
|
||||||
|
|
||||||
import forge.Forge;
|
|
||||||
import forge.Forge.Graphics;
|
import forge.Forge.Graphics;
|
||||||
import forge.assets.FSkin;
|
|
||||||
import forge.assets.FSkinFont;
|
import forge.assets.FSkinFont;
|
||||||
import forge.assets.FSkinTexture;
|
import forge.assets.FSkinTexture;
|
||||||
import forge.properties.ForgeConstants;
|
|
||||||
import forge.properties.ForgePreferences;
|
|
||||||
import forge.properties.ForgePreferences.FPref;
|
|
||||||
import forge.toolbox.FContainer;
|
import forge.toolbox.FContainer;
|
||||||
import forge.toolbox.FProgressBar;
|
import forge.toolbox.FProgressBar;
|
||||||
import forge.util.FileUtil;
|
|
||||||
import forge.util.TextUtil;
|
|
||||||
|
|
||||||
public class SplashScreen extends FContainer {
|
public class SplashScreen extends FContainer {
|
||||||
private TextureRegion background;
|
private TextureRegion background;
|
||||||
@@ -29,11 +16,6 @@ public class SplashScreen extends FContainer {
|
|||||||
public SplashScreen() {
|
public SplashScreen() {
|
||||||
progressBar = new FProgressBar();
|
progressBar = new FProgressBar();
|
||||||
progressBar.setDescription("Welcome to Forge");
|
progressBar.setDescription("Welcome to Forge");
|
||||||
|
|
||||||
checkForAssets();
|
|
||||||
|
|
||||||
final ForgePreferences prefs = new ForgePreferences();
|
|
||||||
FSkin.loadLight(prefs.getPref(FPref.UI_SKIN), this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public FProgressBar getProgressBar() {
|
public FProgressBar getProgressBar() {
|
||||||
@@ -87,27 +69,4 @@ public class SplashScreen extends FContainer {
|
|||||||
progressBar.setBounds(x + padding, y, w - 2 * padding, pbHeight);
|
progressBar.setBounds(x + padding, y, w - 2 * padding, pbHeight);
|
||||||
g.draw(progressBar);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ public final class ForgeConstants {
|
|||||||
public static final String PROFILE_FILE = ASSETS_DIR + "forge.profile.properties";
|
public static final String PROFILE_FILE = ASSETS_DIR + "forge.profile.properties";
|
||||||
public static final String PROFILE_TEMPLATE_FILE = PROFILE_FILE + ".example";
|
public static final String PROFILE_TEMPLATE_FILE = PROFILE_FILE + ".example";
|
||||||
|
|
||||||
private static final String RES_DIR = ASSETS_DIR + "res/";
|
public static final String RES_DIR = ASSETS_DIR + "res/";
|
||||||
private static final String LISTS_DIR = RES_DIR + "lists/";
|
public static final String LISTS_DIR = RES_DIR + "lists/";
|
||||||
public static final String KEYWORD_LIST_FILE = LISTS_DIR + "NonStackingKWList.txt";
|
public static final String KEYWORD_LIST_FILE = LISTS_DIR + "NonStackingKWList.txt";
|
||||||
public static final String TYPE_LIST_FILE = LISTS_DIR + "TypeLists.txt";
|
public static final String TYPE_LIST_FILE = LISTS_DIR + "TypeLists.txt";
|
||||||
public static final String IMAGE_LIST_TOKENS_FILE = LISTS_DIR + "token-images.txt";
|
public static final String IMAGE_LIST_TOKENS_FILE = LISTS_DIR + "token-images.txt";
|
||||||
|
|||||||
Reference in New Issue
Block a user