Skip to content

Commit

Permalink
Cancels the download async for a more responsive experience.
Browse files Browse the repository at this point in the history
And other fixes and bugs.
  • Loading branch information
fd committed Feb 25, 2015
1 parent 121ac5b commit 211d135
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 66 deletions.
32 changes: 24 additions & 8 deletions src/fluddokt/opsu/fake/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,30 @@ public File(String name) {
else
fh = Gdx.files.external(name);
System.out.println(fh+" "+fh.exists()+" "+fh.type());
if (fh.exists())
if (fh.exists()) {
System.out.println("new file:"+info());
return;

fh = Gdx.files.internal(name);
if (fh.exists())
return;
fh = Gdx.files.local(name);
if (fh.exists())
}
if (Gdx.app.getType() == ApplicationType.Desktop)
fh = Gdx.files.external(name);
else
fh = Gdx.files.local(name);
if (fh.exists()) {
System.out.println("new file:"+info());
return;
}
fh = Gdx.files.absolute(name);
if (fh.exists())
if (fh.exists()) {
System.out.println("new file:"+info());
return;
}
fh = Gdx.files.internal(name);
if (fh.exists()) {
System.out.println("new file:"+info());
return;
}
fh = Gdx.files.external(name);
System.out.println("new nonexist file:"+info());


}
Expand All @@ -57,9 +68,14 @@ private File(FileHandle nfh) {
}

public File(File parent, String child) {
System.out.println("new child file:"+parent.info()+" "+child);
fh = parent.fh.child(child);
}

private String info() {
return fh.path()+"["+fh.type();
}

public File(String parent, String child) {
this(new File(parent), child);
}
Expand Down
5 changes: 3 additions & 2 deletions src/fluddokt/opsu/fake/GameContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public int getHeight() {

public void exit() {
close_sub();
if(music != null)
music.stop();
if(music != null){
music.dispose();
}
Gdx.app.exit();
}

Expand Down
2 changes: 1 addition & 1 deletion src/fluddokt/opsu/fake/Music.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public float getPosition() {
}

public void dispose() {
System.out.println("Music pause " + music.getName());
System.out.println("Music dispose " + music.getName());
music.dispose();
}

Expand Down
28 changes: 0 additions & 28 deletions src/fluddokt/opsu/fake/MusicGdx.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,6 @@ public void run() {
music.stop();
}

/*
* public float getPosition2() { float thisPosition = music.getPosition();
* // 1/8 //System.out.println("getPosition: mpos:"+thisPosition); long
* thisTime = TimeUtils.millis(); //float dxPosition = thisPosition -
* lastPosition; float dxPosition2 = thisPosition - lastUpdatePosition;
*
* float syncPosition = (lastPosition);//; long dxTime = thisTime -
* lastTime; //if(dxTime>1) // lastTime = thisTime; //Whenever the time
* changes check the difference between that and our current time //sync our
* time to song time if((int)(dxPosition2*1000)!=0 && Math.abs(syncPosition
* - dxTime/1000f)>0){
* //System.out.println("Time Reset"+" "+syncPosition+" "+(dxTime/1000f)
* +" " +(syncPosition-(dxTime/1000f)) ); //dxTime = (pos*1000+dxtime)/2
* //dxTime = (pos*1000+dxtime)/2 //lastTime = thisTime -
* (pos*1000+dxtime)/2 lastTime = thisTime -
* ((long)(syncPosition*1000)+dxTime)/2; //System.out.println(
* "ASDF:"+(syncPosition*1000)+" "+(syncPosition-lastTime)+" "
* //+((long)(syncPosition*1000)-(syncPosition-lastTime))); //lastTime =
* thisTime; lastPosition = thisPosition; //setPosition(thisPosition);
* //dxTime = 0; } lastUpdatePosition = thisPosition; //if(Gdx.) return
* dxTime/1000f;
*
* //long thisTime = TimeUtils.millis(); //long dxTime = thisTime -
* lastTime; //return dxTime/1000f; //Gdx.graphics.getDeltaTime(); //return
* music.getPosition();
*
* }
*/
@Override
public float getPosition() {
return music.getPosition();
Expand Down
60 changes: 33 additions & 27 deletions src/itdelatrisu/opsu/downloads/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import itdelatrisu.opsu.ErrorHandler;
import itdelatrisu.opsu.Utils;


import java.io.FileInputStream;
//import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -178,12 +177,20 @@ public void run() {
updateReadSoFar();
int total = 0;

while(status == Status.DOWNLOADING && total < contentLength){
long readed = foschannel.transferFrom(rbc, total, Math.min(0x10000, contentLength-total));
total += readed;
try {
while(status == Status.DOWNLOADING && total < contentLength){
long readed = foschannel.transferFrom(rbc, total, Math.min(8192, contentLength-total));
total += readed;
}
} catch (Exception e) {
//e.printStackTrace();
if(status != Status.CANCELLED){
status = Status.ERROR;
ErrorHandler.error("Error while downloading.", e, true);
}
}
//*/
if (status == Status.DOWNLOADING) { // not interrupted

if (status == Status.DOWNLOADING) {
status = Status.COMPLETE;
rbc.close();
fos.close();
Expand Down Expand Up @@ -356,27 +363,26 @@ else if (lastReadSoFarTime <= 0) {
* Cancels the download, if running.
*/
public void cancel() {
try {
this.status = Status.CANCELLED;
boolean transferring = isTransferring();
dlThread.interrupt();
try {
dlThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (rbc != null && rbc.isOpen())
rbc.close();
if (fos != null && fos.getChannel().isOpen())
fos.close();
if (transferring) {
File f = localFile;
if (f.isFile())
f.delete();
//dlThread.interrupt();
new Thread(){
public void run(){
try {
status = Status.CANCELLED;
boolean transferring = isTransferring();
if (rbc != null && rbc.isOpen())
rbc.close();
if (fos != null && fos.getChannel().isOpen())
fos.close();
if (transferring) {
File f = localFile;
if (f.isFile())
f.delete();
}
} catch (Exception e) {
status = Status.ERROR;
ErrorHandler.error("Failed to cancel download.", e, true);
}
}
} catch (IOException e) {
this.status = Status.ERROR;
ErrorHandler.error("Failed to cancel download.", e, true);
}
}.start();
}
}

0 comments on commit 211d135

Please sign in to comment.