source: java/installer2/src/net/oni2/aeinstaller/backend/packages/download/ModDownload.java

Last change on this file was 857, checked in by alloc, 12 years ago

AEI2.10:

  • Switched caching from Raw Depot cache to AEI packages cache (more compact, faster startup)
File size: 4.0 KB
Line 
1package net.oni2.aeinstaller.backend.packages.download;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.io.PrintWriter;
7
8import net.oni2.aeinstaller.backend.Paths;
9import net.oni2.aeinstaller.backend.packages.Package;
10import net.oni2.httpfiledownloader.FileDownloadListener;
11import net.oni2.httpfiledownloader.FileDownloader;
12import net.oni2.httpfiledownloader.FileDownloader.EState;
13import net.oni2.aeinstaller.backend.packages.unpack.UnpackListener;
14import net.oni2.aeinstaller.backend.packages.unpack.Unpacker;
15
16/**
17 * @author Christian Illy
18 */
19public 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 Package 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(Package mod, ModDownloadListener listener) {
69 this.mod = mod;
70 this.listener = listener;
71
72 zipFile = new File(Paths.getDownloadPath(),
73 mod.getPackageNumberString() + ".zip");
74 targetFolder = mod.getLocalPath();
75 try {
76 downloader = new FileDownloader(mod.getFileUrl(), zipFile);
77 downloader.addListener(this);
78 unpacker = new Unpacker(zipFile, targetFolder, this);
79 } catch (IOException e) {
80 e.printStackTrace();
81 }
82 }
83
84 /**
85 * @return Size of this download
86 */
87 public int getSize() {
88 return mod.getZipSize();
89 }
90
91 /**
92 * Start this download
93 */
94 public void start() {
95 state = ModDownloadState.RUNNING;
96 downloader.start();
97 }
98
99 /**
100 * Abort this download
101 */
102 public void abort() {
103 switch (state) {
104 case UNPACKED:
105 case INIT:
106 case ERROR:
107 case INTERRUPTED:
108 break;
109 case RUNNING:
110 downloader.stop();
111 break;
112 case DOWNLOADED:
113 unpacker.stop();
114 break;
115 }
116 state = ModDownloadState.INTERRUPTED;
117 }
118
119 /**
120 * @return the mod object handled by this download
121 */
122 public Package getMod() {
123 return mod;
124 }
125
126 private void writeTimestamp() {
127 File logFile = new File(targetFolder, "aei.cfg");
128 PrintWriter log = null;
129 try {
130 log = new PrintWriter(logFile);
131 log.println("Timestamp -> " + mod.getUpdatedMillis());
132 } catch (FileNotFoundException e) {
133 e.printStackTrace();
134 }
135 if (log != null)
136 log.close();
137 }
138
139 @Override
140 public void statusUpdate(FileDownloader source, EState state, int done,
141 int total) {
142 switch (state) {
143 case INIT:
144 break;
145 case RUNNING:
146 listener.modDownloadStatusUpdate(this, this.state, done, total);
147 break;
148 case PAUSED:
149 break;
150 case INTERRUPTED:
151 break;
152 case ERROR:
153 this.state = ModDownloadState.ERROR;
154 listener.modDownloadStatusUpdate(this, this.state, done, total);
155 break;
156 case FINISHED:
157 this.state = ModDownloadState.DOWNLOADED;
158 listener.modDownloadStatusUpdate(this, this.state, done, total);
159 this.size = done;
160 unpacker.start();
161 break;
162 }
163 }
164
165 @Override
166 public void statusUpdate(Unpacker source,
167 net.oni2.aeinstaller.backend.packages.unpack.Unpacker.EState state) {
168 switch (state) {
169 case INIT:
170 break;
171 case RUNNING:
172 break;
173 case INTERRUPTED:
174 this.state = ModDownloadState.INTERRUPTED;
175 listener.modDownloadStatusUpdate(this, this.state, size, size);
176 break;
177 case FINISHED:
178 this.state = ModDownloadState.UNPACKED;
179 writeTimestamp();
180 zipFile.delete();
181 listener.modDownloadStatusUpdate(this, this.state, size, size);
182 break;
183 }
184 }
185}
Note: See TracBrowser for help on using the repository browser.