source: AE/installer2/src/net/oni2/aeinstaller/backend/mods/download/ModDownloader.java@ 618

Last change on this file since 618 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: 3.4 KB
Line 
1package net.oni2.aeinstaller.backend.mods.download;
2
3import java.util.Date;
4import java.util.TreeSet;
5import java.util.Vector;
6
7import net.oni2.aeinstaller.backend.mods.Mod;
8import net.oni2.aeinstaller.backend.mods.download.ModDownload.ModDownloadState;
9
10/**
11 * @author Christian Illy
12 */
13public class ModDownloader implements ModDownloadListener {
14 /**
15 * @author Christian Illy
16 */
17 public enum State {
18 /**
19 * Downloads running
20 */
21 RUNNING,
22 /**
23 * Aborted because of an error
24 */
25 ERROR,
26 /**
27 * Downloads interrupted
28 */
29 INTERRUPTED,
30 /**
31 * Everything completed
32 */
33 FINISHED
34 };
35
36 private int currentDownload = -1;
37 private int unpacked = 0;
38 private Vector<ModDownload> downloads = new Vector<ModDownload>();
39 private int totalSize = 0;
40 private int downloadedComplete = 0;
41 private int downloadedCurrent = 0;
42 private long startMS;
43 private State state = State.RUNNING;
44 private ModDownloaderListener listener;
45
46 /**
47 * Create a mods-download-process
48 *
49 * @param mods
50 * Mods to download
51 * @param listener
52 * Listener for status updates
53 */
54 public ModDownloader(TreeSet<Mod> mods, ModDownloaderListener listener) {
55 this.listener = listener;
56 for (Mod m : mods) {
57 downloads.add(new ModDownload(m, this));
58 totalSize += m.getZipSize();
59 }
60 startMS = new Date().getTime();
61 startNextDownload();
62 }
63
64 private void startNextDownload() {
65 if (currentDownload >= 0)
66 downloadedComplete += downloads.get(currentDownload).getSize();
67 currentDownload++;
68 downloadedCurrent = 0;
69 if ((state == State.RUNNING) && (currentDownload < downloads.size())) {
70 downloads.get(currentDownload).start();
71 } else {
72 notifyListener();
73 }
74 }
75
76 private int getTimeElapsed() {
77 int total = (int) (new Date().getTime() - startMS)
78 / 1000;
79 return total;
80 }
81
82 private int getDownloadSpeed() {
83 int elap = getTimeElapsed();
84 int down = downloadedComplete + downloadedCurrent;
85 if (elap > 0)
86 return down / elap;
87 else
88 return 1;
89 }
90
91 private int getTimeRemaining() {
92 int remainingSize = totalSize
93 - (downloadedComplete + downloadedCurrent);
94 return remainingSize / getDownloadSpeed();
95 }
96
97 private void notifyListener() {
98 listener.updateStatus(this, state, unpacked, downloads.size(),
99 downloadedComplete + downloadedCurrent, totalSize,
100 getTimeElapsed(), getTimeRemaining(), getDownloadSpeed());
101 }
102
103 /**
104 * @return total download size
105 */
106 public int getTotalSize() {
107 return totalSize;
108 }
109
110 /**
111 * @return Is this process finished
112 */
113 public boolean isFinished() {
114 return state == State.FINISHED;
115 }
116
117 @Override
118 public void modDownloadStatusUpdate(ModDownload source,
119 ModDownloadState state, int done, int total) {
120 switch (state) {
121 case RUNNING:
122 downloadedCurrent = done;
123 notifyListener();
124 break;
125 case ERROR:
126 this.state = State.ERROR;
127 break;
128 case DOWNLOADED:
129 if (source == downloads.get(currentDownload))
130 startNextDownload();
131 break;
132 case UNPACKED:
133 source.getMod().updateLocalData();
134 unpacked++;
135 if (unpacked >= downloads.size())
136 this.state = State.FINISHED;
137 notifyListener();
138 break;
139 case INIT:
140 break;
141 case INTERRUPTED:
142 break;
143 }
144 }
145
146 /**
147 * Abort download process
148 */
149 public void abort() {
150 if (currentDownload < downloads.size()) {
151 state = State.INTERRUPTED;
152 ModDownload md = downloads.get(currentDownload);
153 md.abort();
154 }
155 }
156
157}
Note: See TracBrowser for help on using the repository browser.