Fixed data downloader not downloading optional packages

This commit is contained in:
pelya
2010-08-27 15:42:44 +03:00
parent 1330aec692
commit 6bd7d4126f
14 changed files with 233 additions and 230 deletions

View File

@@ -1,5 +1,5 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import android.app.Activity;
import android.content.Context;

View File

@@ -1,5 +1,5 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import android.app.Activity;

View File

@@ -1,5 +1,5 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import android.app.Activity;
import android.content.Context;
@@ -130,12 +130,12 @@ class DataDownloader extends Thread
public DataDownloader( MainActivity _Parent, TextView _Status )
{
Parent = _Parent;
DownloadComplete = false;
Status = new StatusWriter( _Status, _Parent );
//Status.setText( "Connecting to " + Globals.DataDownloadUrl );
outFilesDir = Parent.getFilesDir().getAbsolutePath();
if( Globals.DownloadToSdcard )
outFilesDir = "/sdcard/app-data/" + Globals.class.getPackage().getName();
DownloadComplete = false;
this.start();
}
@@ -154,14 +154,18 @@ class DataDownloader extends Thread
for( int i = 0; i < downloadFiles.length; i++ )
{
if( downloadFiles[i].length() > 0 && Globals.OptionalDataDownload.length > i && Globals.OptionalDataDownload[i] )
DownloadDataFile(downloadFiles[i], "libsdl-DownloadFinished-" + String.valueOf(i) + ".flag");
if( ! DownloadDataFile(downloadFiles[i], "libsdl-DownloadFinished-" + String.valueOf(i) + ".flag") )
return;
}
DownloadComplete = true;
initParent();
}
public void DownloadDataFile(final String DataDownloadUrl, final String DownloadFlagFileName)
public boolean DownloadDataFile(final String DataDownloadUrl, final String DownloadFlagFileName)
{
String [] downloadUrls = DataDownloadUrl.split("[|]");
if( downloadUrls.length < 2 )
return;
return false;
String path = getOutFilePath(DownloadFlagFileName);
InputStream checkFile = null;
@@ -187,14 +191,11 @@ class DataDownloader extends Thread
if( ! matched )
throw new IOException();
Status.setText( "No need to download" );
DownloadComplete = true;
initParent();
return;
return true;
} catch ( IOException e ) {};
}
checkFile = null;
int downloadUrlIndex = 1;
// Create output directory (not necessary for phone storage)
if( Globals.DownloadToSdcard )
{
@@ -203,68 +204,140 @@ class DataDownloader extends Thread
} catch( SecurityException e ) { };
}
downloading:
while(true)
HttpResponse response = null;
HttpGet request;
long totalLen;
CountingInputStream stream;
byte[] buf = new byte[16384];
boolean DoNotUnzip = false;
String url = "";
int downloadUrlIndex = 1;
while( downloadUrlIndex < downloadUrls.length )
{
HttpResponse response = null;
HttpGet request;
long totalLen;
CountingInputStream stream;
byte[] buf = new byte[16384];
boolean DoNotUnzip = false;
String url = "";
while( downloadUrlIndex < downloadUrls.length && response == null )
System.out.println("Processing download " + downloadUrls[downloadUrlIndex]);
url = new String(downloadUrls[downloadUrlIndex]);
DoNotUnzip = false;
if(url.indexOf(":") == 0)
{
System.out.println("Connecting to " + downloadUrls[downloadUrlIndex]);
Status.setText( "Connecting to " + downloadUrls[downloadUrlIndex] );
url = new String(downloadUrls[downloadUrlIndex]);
if(url.indexOf(":") == 0)
{
url = url.substring( url.indexOf(":", 1) + 1 );
DoNotUnzip = true;
}
request = new HttpGet(url);
request.addHeader("Accept", "*/*");
try {
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter("http.protocol.handle-redirects", true);
response = client.execute(request);
} catch (IOException e) {
System.out.println("Failed to connect to " + downloadUrls[downloadUrlIndex]);
downloadUrlIndex++;
};
if( response != null )
{
if( response.getStatusLine().getStatusCode() != 200 )
{
response = null;
System.out.println("Failed to connect to " + url);
downloadUrlIndex++;
}
}
url = url.substring( url.indexOf(":", 1) + 1 );
DoNotUnzip = true;
}
if( response == null )
{
System.out.println("Error connecting to " + url);
Status.setText( "Error connecting to " + url );
return;
}
Status.setText( "Downloading data from " + url );
totalLen = response.getEntity().getContentLength();
System.out.println("Connecting to " + url);
Status.setText( "Connecting to " + url);
request = new HttpGet(url);
request.addHeader("Accept", "*/*");
try {
stream = new CountingInputStream(response.getEntity().getContent());
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + Globals.DataDownloadUrl );
return;
}
if(DoNotUnzip)
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter("http.protocol.handle-redirects", true);
response = client.execute(request);
} catch (IOException e) {
System.out.println("Failed to connect to " + downloadUrls[downloadUrlIndex]);
downloadUrlIndex++;
};
if( response != null )
{
path = getOutFilePath(downloadUrls[downloadUrlIndex].substring( 1,
downloadUrls[downloadUrlIndex].indexOf(":", 1) ));
if( response.getStatusLine().getStatusCode() != 200 )
{
response = null;
System.out.println("Failed to connect to " + url);
downloadUrlIndex++;
}
}
}
if( response == null )
{
System.out.println("Error connecting to " + url);
Status.setText( "Error connecting to " + url );
return false;
}
Status.setText( "Downloading data from " + url );
totalLen = response.getEntity().getContentLength();
try {
stream = new CountingInputStream(response.getEntity().getContent());
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + Globals.DataDownloadUrl );
return false;
}
if(DoNotUnzip)
{
path = getOutFilePath(downloadUrls[downloadUrlIndex].substring( 1,
downloadUrls[downloadUrlIndex].indexOf(":", 1) ));
OutputStream out = null;
try {
out = new FileOutputStream( path );
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) { };
if( out == null )
{
Status.setText( "Error writing to " + path );
return false;
}
try {
int len = stream.read(buf);
while (len >= 0)
{
if(len > 0)
out.write(buf, 0, len);
len = stream.read(buf);
String percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
}
out.flush();
out.close();
out = null;
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path + " from URL " + url );
return false;
}
}
else
{
ZipInputStream zip = new ZipInputStream(stream);
while(true)
{
ZipEntry entry = null;
try {
entry = zip.getNextEntry();
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + url );
return false;
}
if( entry == null )
break;
if( entry.isDirectory() )
{
try {
(new File( getOutFilePath(entry.getName()) )).mkdirs();
} catch( SecurityException e ) { };
continue;
}
OutputStream out = null;
path = getOutFilePath(entry.getName());
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
continue;
} catch( Exception e )
{
}
try {
out = new FileOutputStream( path );
} catch( FileNotFoundException e ) {
@@ -272,18 +345,23 @@ class DataDownloader extends Thread
if( out == null )
{
Status.setText( "Error writing to " + path );
return;
return false;
}
String percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
try {
int len = stream.read(buf);
int len = zip.read(buf);
while (len >= 0)
{
if(len > 0)
out.write(buf, 0, len);
len = stream.read(buf);
len = zip.read(buf);
String percent = "";
percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
@@ -293,136 +371,48 @@ class DataDownloader extends Thread
out = null;
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path + " from URL " + url );
return;
return false;
}
}
else
{
ZipInputStream zip = new ZipInputStream(stream);
while(true)
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
} catch( Exception e )
{
ZipEntry entry = null;
try {
entry = zip.getNextEntry();
} catch( java.io.IOException e ) {
Status.setText( "Error downloading data from " + url );
return;
}
if( entry == null )
break;
if( entry.isDirectory() )
{
try {
(new File( getOutFilePath(entry.getName()) )).mkdirs();
} catch( SecurityException e ) { };
continue;
}
OutputStream out = null;
path = getOutFilePath(entry.getName());
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
continue;
} catch( Exception e )
{
}
try {
out = new FileOutputStream( path );
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) { };
if( out == null )
{
Status.setText( "Error writing to " + path + " from URL " + url );
return;
}
String percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
try {
int len = zip.read(buf);
while (len >= 0)
{
if(len > 0)
out.write(buf, 0, len);
len = zip.read(buf);
percent = "";
if( totalLen > 0 )
percent = String.valueOf(stream.getBytesRead() * 100 / totalLen) + "%: ";
Status.setText( percent + "writing file " + path );
}
out.flush();
out.close();
out = null;
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path + " from URL " + url );
return;
}
try {
CheckedInputStream check = new CheckedInputStream( new FileInputStream(path), new CRC32() );
while( check.read(buf, 0, buf.length) > 0 ) {};
check.close();
if( check.getChecksum().getValue() != entry.getCrc() )
{
File ff = new File(path);
ff.delete();
throw new Exception();
}
} catch( Exception e )
{
Status.setText( "CRC32 check failed for file " + path );
continue downloading; // Start download over from the same URL
//return;
}
Status.setText( "CRC32 check failed for file " + path );
return false;
}
}
};
OutputStream out = null;
path = getOutFilePath(DownloadFlagFileName);
try {
out = new FileOutputStream( path );
out.write(downloadUrls[downloadUrlIndex].getBytes("UTF-8"));
out.flush();
out.close();
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) {
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path );
return;
};
if( out == null )
{
Status.setText( "Error writing to " + path );
return;
}
Status.setText( "Finished" );
DownloadComplete = true;
try {
stream.close();
} catch( java.io.IOException e ) {
};
initParent();
break;
}
OutputStream out = null;
path = getOutFilePath(DownloadFlagFileName);
try {
out = new FileOutputStream( path );
out.write(downloadUrls[downloadUrlIndex].getBytes("UTF-8"));
out.flush();
out.close();
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) {
} catch( java.io.IOException e ) {
Status.setText( "Error writing file " + path );
return false;
};
Status.setText( "Finished" );
try {
stream.close();
} catch( java.io.IOException e ) {
};
return true;
};
private void initParent()
@@ -448,8 +438,8 @@ class DataDownloader extends Thread
return outFilesDir + "/" + filename;
};
public boolean DownloadComplete;
public StatusWriter Status;
public boolean DownloadComplete = false;
private MainActivity Parent;
private String outFilesDir = null;
}

View File

@@ -18,7 +18,7 @@
fixed with a hammer and rasp to work with libSDL port */
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import java.io.Writer;
import java.util.ArrayList;

View File

@@ -1,14 +1,14 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount anywhere
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import android.app.Activity;
import android.content.Context;
class Globals {
public static String ApplicationName = "OpenTyrian";
public static String ApplicationName = "Ur-QuanMasters";
// Should be zip file
public static String DataDownloadUrl = "Data files size is 11 Mb|http://sites.google.com/site/xpelyax/Home/tyrian21-data.zip?attredirects=0%26d=1|http://sitesproxy.goapk.com/site/xpelyax/Home/tyrian21-data.zip";
public static String DataDownloadUrl = "Game data is 14 Mb|https://sites.google.com/site/xpelyax/Home/sc2-data.zip?attredirects=0%26d=1|http://sitesproxy.goapk.com/site/xpelyax/Home/sc2-data.zip^3DO remixed music (19 Mb) - enable it in Setup->Sound Options->3DO Remixes|:addons/3domusic/3domusic.zip:https://sites.google.com/site/xpelyax/Home/3domusic.zip?attredirects=0%26d=1|:addons/3domusic/3domusic.zip:http://sitesproxy.goapk.com/site/xpelyax/Home/3domusic.zip^UQM music remix pack 1 (50 Mb) - enable it in Setup->Sound Options->UQM Remixes|:addons/remix/uqm-remix-pack1.zip:http://sourceforge.net/projects/sc2/files/UQM%20Remix%20Packs/UQM%20Remix%20Pack%201/uqm-remix-pack1.zip/download^UQM music remix pack 2 (60 Mb)|:addons/remix/uqm-remix-pack2.zip:http://sourceforge.net/projects/sc2/files/UQM%20Remix%20Packs/UQM%20Remix%20Pack%202/uqm-remix-pack2.zip/download^UQM music remix pack 3 (40 Mb)|:addons/remix/uqm-remix-pack3.zip:http://sourceforge.net/projects/sc2/files/UQM%20Remix%20Packs/UQM%20Remix%20Pack%203/uqm-remix-pack3.zip/download";
// Set this value to true if you're planning to render 3D using OpenGL - it eats some GFX resources, so disabled for 2D
public static boolean NeedDepthBuffer = false;
@@ -27,9 +27,9 @@ class Globals {
public static boolean AppUsesMultitouch = false;
public static int AppTouchscreenKeyboardKeysAmount = 4;
public static int AppTouchscreenKeyboardKeysAmount = 2;
public static int AppTouchscreenKeyboardKeysAmountAutoFire = 1;
public static int AppTouchscreenKeyboardKeysAmountAutoFire = 2;
// Phone-specific config
// It will download app data to /sdcard/alienblaster if set to true,
@@ -48,5 +48,5 @@ class Globals {
}
class LoadLibrary {
public LoadLibrary() { System.loadLibrary("sdl"); System.loadLibrary("sdl_net"); };
public LoadLibrary() { System.loadLibrary("sdl"); System.loadLibrary("sdl_image"); };
}

View File

@@ -1,5 +1,5 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import android.app.Activity;
import android.content.Context;
@@ -26,9 +26,12 @@ public class MainActivity extends Activity {
_tv = new TextView(this);
_tv.setText("Initializing");
setContentView(_tv);
mLoadLibraryStub = new LoadLibrary();
mAudioThread = new AudioThread(this);
Settings.Load(this);
if(mAudioThread == null) // Starting from background (should not happen)
{
mLoadLibraryStub = new LoadLibrary();
mAudioThread = new AudioThread(this);
Settings.Load(this);
}
}
public void startDownloader()
@@ -57,7 +60,7 @@ public class MainActivity extends Activity {
protected void onPause() {
if( downloader != null ) {
synchronized( downloader ) {
downloader.setParent(null, null);
downloader.setParent(null, null);
}
}
// TODO: if application pauses it's screen is messed up
@@ -75,6 +78,7 @@ public class MainActivity extends Activity {
super.onResume();
if( mGLView != null )
mGLView.onResume();
else
if( downloader != null ) {
synchronized( downloader ) {
downloader.setParent(this, _tv);
@@ -89,7 +93,7 @@ public class MainActivity extends Activity {
{
if( downloader != null ) {
synchronized( downloader ) {
downloader.setParent(null, null);
downloader.setParent(null, null);
}
}
if( wakeLock != null )
@@ -154,8 +158,8 @@ public class MainActivity extends Activity {
}
private DemoGLSurfaceView mGLView = null;
private LoadLibrary mLoadLibraryStub = null;
private AudioThread mAudioThread = null;
private static LoadLibrary mLoadLibraryStub = null;
private static AudioThread mAudioThread = null;
private PowerManager.WakeLock wakeLock = null;
private static DataDownloader downloader = null;
private TextView _tv = null;

View File

@@ -1,5 +1,5 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import android.app.Activity;
import android.content.Context;
@@ -26,6 +26,7 @@ class Settings
static AlertDialog changeConfigAlert = null;
static Thread changeConfigAlertThread = null;
static boolean settingsLoaded = false;
static void Save(final MainActivity p)
{
@@ -45,6 +46,8 @@ class Settings
out.writeBoolean(Globals.OptionalDataDownload[i]);
out.writeInt(Globals.TouchscreenKeyboardTheme);
out.close();
settingsLoaded = true;
} catch( FileNotFoundException e ) {
} catch( SecurityException e ) {
} catch ( IOException e ) {};
@@ -52,6 +55,11 @@ class Settings
static void Load( final MainActivity p )
{
if(settingsLoaded) // Prevent starting twice
{
startDownloader(p);
return;
}
try {
ObjectInputStream settingsFile = new ObjectInputStream(new FileInputStream( p.getFilesDir().getAbsolutePath() + "/" + SettingsFileName ));
Globals.DownloadToSdcard = settingsFile.readBoolean();
@@ -68,6 +76,8 @@ class Settings
Globals.OptionalDataDownload[i] = settingsFile.readBoolean();
Globals.TouchscreenKeyboardTheme = settingsFile.readInt();
settingsLoaded = true;
AlertDialog.Builder builder = new AlertDialog.Builder(p);
builder.setTitle("Phone configuration");
builder.setPositiveButton("Change phone configuration", new DialogInterface.OnClickListener()
@@ -113,8 +123,7 @@ class Settings
changeConfigAlertThread.start();
alert.show();
return;
} catch( FileNotFoundException e ) {

View File

@@ -1,5 +1,5 @@
// This string is autogenerated by ChangeAppSettings.sh, do not change spaces amount
package com.googlecode.opentyrian;
package com.sourceforge.sc2;
import javax.microedition.khronos.opengles.GL10;