1 |
package net.oni2.aeinstaller.backend.mods.download; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileNotFoundException; |
5 |
import java.io.IOException; |
6 |
import java.io.PrintWriter; |
7 |
|
8 |
import net.oni2.aeinstaller.backend.Paths; |
9 |
import net.oni2.aeinstaller.backend.mods.Mod; |
10 |
import net.oni2.aeinstaller.backend.network.FileDownloadListener; |
11 |
import net.oni2.aeinstaller.backend.network.FileDownloader; |
12 |
import net.oni2.aeinstaller.backend.network.FileDownloader.EState; |
13 |
import net.oni2.aeinstaller.backend.unpack.UnpackListener; |
14 |
import net.oni2.aeinstaller.backend.unpack.Unpacker; |
15 |
|
16 |
/** |
17 |
* @author Christian Illy |
18 |
*/ |
19 |
public class ModDownload implements FileDownloadListener, UnpackListener { |
20 |
/** |
21 |
* @author Christian Illy |
22 |
*/ |
23 |
public enum ModDownloadState { |
24 |
/** |
25 |
* Downloader initialized but not started |
26 |
*/ |
27 |
INIT, |
28 |
/** |
29 |
* Download running |
30 |
*/ |
31 |
RUNNING, |
32 |
/** |
33 |
* Aborted because of an error |
34 |
*/ |
35 |
ERROR, |
36 |
/** |
37 |
* Download interrupted |
38 |
*/ |
39 |
INTERRUPTED, |
40 |
/** |
41 |
* Download finished successfully |
42 |
*/ |
43 |
DOWNLOADED, |
44 |
/** |
45 |
* Package unzipped successfully |
46 |
*/ |
47 |
UNPACKED |
48 |
}; |
49 |
|
50 |
private Mod mod; |
51 |
private FileDownloader downloader; |
52 |
private Unpacker unpacker; |
53 |
private File zipFile; |
54 |
private File targetFolder; |
55 |
private ModDownloadListener listener; |
56 |
private int size; |
57 |
|
58 |
private ModDownloadState state = ModDownloadState.INIT; |
59 |
|
60 |
/** |
61 |
* Create a mod download |
62 |
* |
63 |
* @param mod |
64 |
* Mod to download |
65 |
* @param listener |
66 |
* Listener for progress |
67 |
*/ |
68 |
public ModDownload(Mod mod, ModDownloadListener listener) { |
69 |
this.mod = mod; |
70 |
this.listener = listener; |
71 |
|
72 |
zipFile = new File(Paths.getDownloadPath(), |
73 |
mod.getPackageNumberString() + ".zip"); |
74 |
targetFolder = new File(Paths.getModsPath(), |
75 |
mod.getPackageNumberString()); |
76 |
try { |
77 |
downloader = new FileDownloader(mod.getFile().getUri_full(), |
78 |
zipFile.getPath()); |
79 |
downloader.addListener(this); |
80 |
unpacker = new Unpacker(zipFile, targetFolder, this); |
81 |
} catch (IOException e) { |
82 |
// TODO Auto-generated catch block |
83 |
e.printStackTrace(); |
84 |
} |
85 |
} |
86 |
|
87 |
/** |
88 |
* @return Size of this download |
89 |
*/ |
90 |
public int getSize() { |
91 |
return mod.getZipSize(); |
92 |
} |
93 |
|
94 |
/** |
95 |
* Start this download |
96 |
*/ |
97 |
public void start() { |
98 |
downloader.start(); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Abort this download |
103 |
*/ |
104 |
public void abort() { |
105 |
switch (state) { |
106 |
case UNPACKED: |
107 |
case INIT: |
108 |
case ERROR: |
109 |
case INTERRUPTED: |
110 |
break; |
111 |
case RUNNING: |
112 |
downloader.stop(); |
113 |
break; |
114 |
case DOWNLOADED: |
115 |
unpacker.stop(); |
116 |
break; |
117 |
} |
118 |
state = ModDownloadState.INTERRUPTED; |
119 |
} |
120 |
|
121 |
/** |
122 |
* @return the mod object handled by this download |
123 |
*/ |
124 |
public Mod getMod() { |
125 |
return mod; |
126 |
} |
127 |
|
128 |
private void writeTimestamp() { |
129 |
File logFile = new File(targetFolder, "aei.cfg"); |
130 |
PrintWriter log = null; |
131 |
try { |
132 |
log = new PrintWriter(logFile); |
133 |
log.println("Timestamp -> " + mod.getFile().getTimestamp()); |
134 |
} catch (FileNotFoundException e) { |
135 |
e.printStackTrace(); |
136 |
} |
137 |
if (log != null) |
138 |
log.close(); |
139 |
} |
140 |
|
141 |
@Override |
142 |
public void statusUpdate(FileDownloader source, EState state, int done, |
143 |
int total) { |
144 |
switch (state) { |
145 |
case INIT: |
146 |
break; |
147 |
case RUNNING: |
148 |
listener.modDownloadStatusUpdate(this, this.state, done, total); |
149 |
break; |
150 |
case PAUSED: |
151 |
break; |
152 |
case INTERRUPTED: |
153 |
break; |
154 |
case ERROR: |
155 |
this.state = ModDownloadState.ERROR; |
156 |
listener.modDownloadStatusUpdate(this, this.state, done, total); |
157 |
break; |
158 |
case FINISHED: |
159 |
this.state = ModDownloadState.DOWNLOADED; |
160 |
listener.modDownloadStatusUpdate(this, this.state, done, total); |
161 |
this.size = done; |
162 |
unpacker.start(); |
163 |
break; |
164 |
} |
165 |
} |
166 |
|
167 |
@Override |
168 |
public void statusUpdate(Unpacker source, |
169 |
net.oni2.aeinstaller.backend.unpack.Unpacker.EState state) { |
170 |
switch (state) { |
171 |
case INIT: |
172 |
break; |
173 |
case RUNNING: |
174 |
break; |
175 |
case INTERRUPTED: |
176 |
this.state = ModDownloadState.INTERRUPTED; |
177 |
listener.modDownloadStatusUpdate(this, this.state, size, size); |
178 |
break; |
179 |
case FINISHED: |
180 |
this.state = ModDownloadState.UNPACKED; |
181 |
writeTimestamp(); |
182 |
zipFile.delete(); |
183 |
listener.modDownloadStatusUpdate(this, this.state, size, size); |
184 |
break; |
185 |
} |
186 |
} |
187 |
} |