source: AE/installer2/src/net/oni2/aeinstaller/backend/mods/download/ModDownload.java@ 623

Last change on this file since 623 was 621, checked in by alloc, 12 years ago

AEI2:

  • Added offline detection (should run without internet connection)
  • Update check (manual/on startup), not actually updating yet
File size: 4.0 KB
Line 
1package net.oni2.aeinstaller.backend.mods.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.mods.Mod;
10import net.oni2.aeinstaller.backend.network.FileDownloadListener;
11import net.oni2.aeinstaller.backend.network.FileDownloader;
12import net.oni2.aeinstaller.backend.network.FileDownloader.EState;
13import net.oni2.aeinstaller.backend.unpack.UnpackListener;
14import net.oni2.aeinstaller.backend.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 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 e.printStackTrace();
83 }
84 }
85
86 /**
87 * @return Size of this download
88 */
89 public int getSize() {
90 return mod.getZipSize();
91 }
92
93 /**
94 * Start this download
95 */
96 public void start() {
97 state = ModDownloadState.RUNNING;
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}
Note: See TracBrowser for help on using the repository browser.