@@ -116,63 +116,7 @@ class ModDownload::Impl final {
116
116
117
117
m_downloadListener.bind ([this , hash = std::move (version.hash ), version = std::move (version)](web::WebTask::Event* event) {
118
118
if (auto value = event->getValue ()) {
119
- if (value->ok ()) {
120
- if (auto actualHash = ::calculateHash (value->data ()); actualHash != hash) {
121
- log::error (" Failed to download {}, hash mismatch ({} != {})" , m_id, actualHash, hash);
122
- m_status = DownloadStatusError {
123
- .details = " Hash mismatch, downloaded file did not match what was expected" ,
124
- };
125
- ModDownloadEvent (m_id).post ();
126
- return ;
127
- }
128
-
129
- bool removingInstalledWasError = false ;
130
- std::string id = m_replacesMod.has_value () ? m_replacesMod.value () : m_id;
131
- if (auto mod = Loader::get ()->getInstalledMod (id)) {
132
- std::error_code ec;
133
- std::filesystem::remove (mod->getPackagePath (), ec);
134
- if (ec) {
135
- removingInstalledWasError = true ;
136
- m_status = DownloadStatusError {
137
- .details = fmt::format (" Unable to delete existing .geode package (code {})" , ec),
138
- };
139
- }
140
- // Mark mod as updated
141
- ModImpl::getImpl (mod)->m_requestedAction = ModRequestedAction::Update;
142
- }
143
- // If this was an update, delete the old file first
144
- if (!removingInstalledWasError) {
145
- auto geodePath = dirs::getModsDir () / (m_id + " .geode" );
146
- auto ok = file::writeBinary (geodePath, value->data ());
147
- if (!ok) {
148
- m_status = DownloadStatusError {
149
- .details = ok.unwrapErr (),
150
- };
151
- }
152
- else {
153
- #ifdef GEODE_IS_IOS
154
- auto metadata = ModMetadata::createFromGeodeZip (geodePath);
155
- if (metadata.isErr ()) {
156
- m_status = DownloadStatusError {
157
- .details = m_metadata.unwrapErr (),
158
- };
159
- return ;
160
- }
161
- auto okUnzip = LoaderImpl::get ()->unzipGeodeFile (metadata.unwrap ());
162
- if (!okUnzip) {
163
- m_status = DownloadStatusError {
164
- .details = okUnzip.unwrapErr (),
165
- };
166
- return ;
167
- }
168
- #endif
169
- m_status = DownloadStatusDone {
170
- .version = version
171
- };
172
- }
173
- }
174
- }
175
- else {
119
+ if (!value->ok ()) {
176
120
auto resp = event->getValue ();
177
121
178
122
if (resp->code () == -1 ) {
@@ -199,7 +143,60 @@ class ModDownload::Impl final {
199
143
if (!extErr.empty ()) {
200
144
log::error (" Extended error info: {}" , extErr);
201
145
}
146
+ goto postdownloadedevent;
202
147
}
148
+
149
+ if (auto actualHash = ::calculateHash (value->data ()); actualHash != hash) {
150
+ log::error (" Failed to download {}, hash mismatch ({} != {})" , m_id, actualHash, hash);
151
+ m_status = DownloadStatusError {
152
+ .details = " Hash mismatch, downloaded file did not match what was expected" ,
153
+ };
154
+ goto postdownloadedevent;
155
+ }
156
+
157
+ std::string id = m_replacesMod.has_value () ? m_replacesMod.value () : m_id;
158
+ if (auto mod = Loader::get ()->getInstalledMod (id)) {
159
+ std::error_code ec;
160
+ std::filesystem::remove (mod->getPackagePath (), ec);
161
+ if (ec) {
162
+ m_status = DownloadStatusError {
163
+ .details = fmt::format (" Unable to delete existing .geode package (code {})" , ec),
164
+ };
165
+ goto postdownloadedevent;
166
+ }
167
+ // Mark mod as updated
168
+ ModImpl::getImpl (mod)->m_requestedAction = ModRequestedAction::Update;
169
+ }
170
+
171
+ // If this was an update, delete the old file first
172
+ auto geodePath = dirs::getModsDir () / (m_id + " .geode" );
173
+ auto ok = file::writeBinary (geodePath, value->data ());
174
+ if (!ok) {
175
+ m_status = DownloadStatusError {
176
+ .details = ok.unwrapErr (),
177
+ };
178
+ goto postdownloadedevent;
179
+ }
180
+
181
+ auto metadata = ModMetadata::createFromGeodeFile (geodePath);
182
+ if (metadata.isErr ()) {
183
+ m_status = DownloadStatusError {
184
+ .details = metadata.unwrapErr (),
185
+ };
186
+ goto postdownloadedevent;
187
+ }
188
+
189
+ auto okBinary = LoaderImpl::get ()->extractBinary (metadata.unwrap ());
190
+ if (!okBinary) {
191
+ m_status = DownloadStatusError {
192
+ .details = okBinary.unwrapErr (),
193
+ };
194
+ goto postdownloadedevent;
195
+ }
196
+
197
+ m_status = DownloadStatusDone {
198
+ .version = version
199
+ };
203
200
}
204
201
else if (auto progress = event->getProgress ()) {
205
202
m_status = DownloadStatusDownloading {
@@ -211,6 +208,8 @@ class ModDownload::Impl final {
211
208
}
212
209
// Throttle events to only once per frame to not cause a
213
210
// billion UI updates at once
211
+ postdownloadedevent:
212
+
214
213
if (m_scheduledEventForFrame != CCDirector::get ()->getTotalFrames ()) {
215
214
m_scheduledEventForFrame = CCDirector::get ()->getTotalFrames ();
216
215
Loader::get ()->queueInMainThread ([id = m_id]() {
0 commit comments