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

Last change on this file since 613 was 605, checked in by alloc, 12 years ago

AEI2:

  • Added mod download prior to installation
  • Dependency checking (needs verification)
  • Conflicts checking basis (not implemented)
  • Run Oni through AEI
File size: 4.1 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 // 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 state = ModDownloadState.RUNNING;
99 downloader.start();
100 }
101
102 /**
103 * Abort this download
104 */
105 public void abort() {
106 switch (state) {
107 case UNPACKED:
108 case INIT:
109 case ERROR:
110 case INTERRUPTED:
111 break;
112 case RUNNING:
113 downloader.stop();
114 break;
115 case DOWNLOADED:
116 unpacker.stop();
117 break;
118 }
119 state = ModDownloadState.INTERRUPTED;
120 }
121
122 /**
123 * @return the mod object handled by this download
124 */
125 public Mod getMod() {
126 return mod;
127 }
128
129 private void writeTimestamp() {
130 File logFile = new File(targetFolder, "aei.cfg");
131 PrintWriter log = null;
132 try {
133 log = new PrintWriter(logFile);
134 log.println("Timestamp -> " + mod.getFile().getTimestamp());
135 } catch (FileNotFoundException e) {
136 e.printStackTrace();
137 }
138 if (log != null)
139 log.close();
140 }
141
142 @Override
143 public void statusUpdate(FileDownloader source, EState state, int done,
144 int total) {
145 switch (state) {
146 case INIT:
147 break;
148 case RUNNING:
149 listener.modDownloadStatusUpdate(this, this.state, done, total);
150 break;
151 case PAUSED:
152 break;
153 case INTERRUPTED:
154 break;
155 case ERROR:
156 this.state = ModDownloadState.ERROR;
157 listener.modDownloadStatusUpdate(this, this.state, done, total);
158 break;
159 case FINISHED:
160 this.state = ModDownloadState.DOWNLOADED;
161 listener.modDownloadStatusUpdate(this, this.state, done, total);
162 this.size = done;
163 unpacker.start();
164 break;
165 }
166 }
167
168 @Override
169 public void statusUpdate(Unpacker source,
170 net.oni2.aeinstaller.backend.unpack.Unpacker.EState state) {
171 switch (state) {
172 case INIT:
173 break;
174 case RUNNING:
175 break;
176 case INTERRUPTED:
177 this.state = ModDownloadState.INTERRUPTED;
178 listener.modDownloadStatusUpdate(this, this.state, size, size);
179 break;
180 case FINISHED:
181 this.state = ModDownloadState.UNPACKED;
182 writeTimestamp();
183 zipFile.delete();
184 listener.modDownloadStatusUpdate(this, this.state, size, size);
185 break;
186 }
187 }
188}
Note: See TracBrowser for help on using the repository browser.