diff --git a/include/c++/schemas/Error.h b/include/c++/schemas/Error.h index 69eb4057..b12862a0 100644 --- a/include/c++/schemas/Error.h +++ b/include/c++/schemas/Error.h @@ -12,6 +12,7 @@ class Error { public: u32 code; + u32 error_ref; std::string message; std::vector errors; diff --git a/include/c/ModioC.h b/include/c/ModioC.h index c3d51345..21db107a 100644 --- a/include/c/ModioC.h +++ b/include/c/ModioC.h @@ -52,6 +52,7 @@ typedef int i32; #define MODIO_EVENT_USER_TEAM_LEAVE 6 #define MODIO_EVENT_USER_SUBSCRIBE 7 #define MODIO_EVENT_USER_UNSUBSCRIBE 8 +#define MODIO_EVENT_MOD_DELETED 9 // Presentation Option Constants #define MODIO_GRID_VIEW 0 @@ -266,7 +267,7 @@ extern "C" char* thumb_640x360; char* thumb_1280x720; }; - + struct ModioIcon { char* filename; @@ -304,6 +305,7 @@ extern "C" struct ModioError { u32 code; + u32 error_ref; char* message; char** errors_array; u32 errors_array_size; @@ -469,7 +471,7 @@ extern "C" char* path; ModioMod mod; }; - + struct ModioQueuedModDownload { u32 state; @@ -689,8 +691,8 @@ extern "C" void MODIO_DLL modioCancelModDownload(u32 mod_id); void MODIO_DLL modioResumeDownloads(void); void MODIO_DLL modioPrioritizeModDownload(u32 mod_id); - void MODIO_DLL modioSetDownloadListener(void (*callback)(u32 response_code, u32 mod_id)); - void MODIO_DLL modioSetUploadListener(void (*callback)(u32 response_code, u32 mod_id)); + void MODIO_DLL modioSetDownloadListener(void (*callback)(u32 response_code, u32 mod_id)); + void MODIO_DLL modioSetUploadListener(void (*callback)(u32 response_code, u32 mod_id)); u32 MODIO_DLL modioGetModDownloadQueueCount(void); void MODIO_DLL modioGetModDownloadQueue(ModioQueuedModDownload* download_queue); u32 MODIO_DLL modioGetModfileUploadQueueCount(void); @@ -712,7 +714,7 @@ extern "C" //Comment Methods void MODIO_DLL modioGetAllModComments(void* object, u32 mod_id, ModioFilterCreator filter, void (*callback)(void* object, ModioResponse response, ModioComment comments[], u32 comments_size)); void MODIO_DLL modioGetAllModCommentsFilterString(void* object, u32 mod_id, char const* filter_string, u32 cache_max_age_seconds, void (*callback)(void* object, ModioResponse response, ModioComment comments[], u32 comments_size)); - void MODIO_DLL modioGetModComment(void* object, u32 mod_id, u32 comment_id, void (*callback)(void* object, ModioResponse response, ModioComment comment)); + void MODIO_DLL modioGetModComment(void* object, u32 mod_id, u32 comment_id, void (*callback)(void* object, ModioResponse response, ModioComment comment)); void MODIO_DLL modioDeleteModComment(void* object, u32 mod_id, u32 comment_id, void(*callback)(void* object, ModioResponse response)); //Reports Methods @@ -728,7 +730,7 @@ extern "C" void MODIO_DLL modioFreeQueuedModDownload(ModioQueuedModDownload* queued_mod_download); void MODIO_DLL modioFreeQueuedModfileUpload(ModioQueuedModfileUpload* queued_modfile_upload); - // General Utility Methods + // General Utility Methods void MODIO_DLL compressFiles(char const* root_directory, char const* const filenames[], u32 filenames_size, char const* zip_path); void MODIO_DLL extractFiles(char const* zip_path, char const* directory_path); void MODIO_DLL windowsUTF8ToAnsi(const char* UTF8_string, char* ansi_string); diff --git a/include/c/methods/callbacks/MeCallbacks.h b/include/c/methods/callbacks/MeCallbacks.h index 2eb80b44..631f7e5f 100644 --- a/include/c/methods/callbacks/MeCallbacks.h +++ b/include/c/methods/callbacks/MeCallbacks.h @@ -18,10 +18,10 @@ struct GetAuthenticatedUserParams struct GetUserSubscriptionsParams { - void* object; std::string url; bool is_cache; - void (*callback)(void* object, ModioResponse response, ModioMod mods[], u32 mods_size); + std::vector objects; + std::vector callbacks; }; struct GetUserEventsParams diff --git a/include/wrappers/CurlWrapper.h b/include/wrappers/CurlWrapper.h index 48c543b3..ce987201 100644 --- a/include/wrappers/CurlWrapper.h +++ b/include/wrappers/CurlWrapper.h @@ -41,7 +41,7 @@ void downloadMod(QueuedModDownload *queued_mod_download); void queueModDownload(ModioMod& modio_mod); void uploadModfile(QueuedModfileUpload *queued_modfile_upload); void queueModfileUpload(u32 mod_id, ModioModfileCreator *modio_modfile_creator); -void removeDownloadedModfile(u32 mod_id); +void removeDownloadedMod(u32 mod_id); } } diff --git a/src/ModioUtility.cpp b/src/ModioUtility.cpp index c21e2d87..768454f4 100644 --- a/src/ModioUtility.cpp +++ b/src/ModioUtility.cpp @@ -67,7 +67,7 @@ void onUpdateCurrentUserSubscriptions(void* object, ModioResponse response, Modi { if(response.result_offset == 0) /* Clear only if is first result page */ modio::current_user_subscriptions.clear(); - + if (response.code >= 200 && response.code < 300) { modio::writeLogLine("Current user subscriptions retrieved sucessfully.", MODIO_DEBUGLEVEL_LOG); @@ -176,7 +176,7 @@ void handleUnsubscription(u32 mod_id) { modioUninstallMod(mod_id); modioCancelModDownload(mod_id); - modio::curlwrapper::removeDownloadedModfile(mod_id); + modio::curlwrapper::removeDownloadedMod(mod_id); } void updateModsCache(std::vector mod_ids) @@ -185,7 +185,7 @@ void updateModsCache(std::vector mod_ids) modioInitFilter(&filter); std::vector* ptr_mod_ids = new std::vector(); - + for (auto &mod_id : mod_ids) { modioAddFilterInField(&filter, "id", modio::toString(mod_id).c_str()); @@ -205,9 +205,9 @@ void addModsToDownloadQueue(std::vector mod_ids) } ModioFilterCreator filter; modioInitFilter(&filter); - + std::vector* ptr_mod_ids = new std::vector(); - + for (auto &mod_id : mod_ids) { modioAddFilterInField(&filter, "id", modio::toString(mod_id).c_str()); @@ -227,6 +227,7 @@ static void onGetAllEventsPoll(void *object, ModioResponse response, ModioModEve std::vector mod_edited_ids; std::vector mod_to_download_queue_ids; + std::vector mod_deleted_ids; if(events_array_size > 0) modio::clearCache(); for (size_t i = 0; i < events_array_size; i++) @@ -236,7 +237,7 @@ static void onGetAllEventsPoll(void *object, ModioResponse response, ModioModEve modio::writeLogLine("Mod event id polled: " + modio::toString(events_array[i].id), MODIO_DEBUGLEVEL_LOG); modio::LAST_MOD_EVENT_POLL_ID = events_array[i].id; } - + switch (events_array[i].event_type) { case MODIO_EVENT_UNDEFINED: @@ -244,62 +245,11 @@ static void onGetAllEventsPoll(void *object, ModioResponse response, ModioModEve break; case MODIO_EVENT_MODFILE_CHANGED: { - bool reinstall = true; - for (auto installed_mod : modio::installed_mods) - { - if (modio::hasKey(installed_mod, "mod_id") && - modio::hasKey(installed_mod, "date_updated") && - installed_mod["mod_id"] == events_array[i].mod_id && - installed_mod["date_updated"] >= events_array[i].date_added) - { - reinstall = false; - modio::writeLogLine("Modfile changed event detected but you already have a newer version installed, the modfile will not be downloaded. Mod id: " + modio::toString(events_array[i].mod_id), MODIO_DEBUGLEVEL_LOG); - } - } - - for (auto &downloaded_mod_json : modio::g_downloaded_mods) - { - if (modio::hasKey(downloaded_mod_json, "mod") && - modio::hasKey(downloaded_mod_json["mod"], "id") && - downloaded_mod_json["mod"]["id"] == events_array[i].mod_id) - { - if (modio::hasKey(downloaded_mod_json["mod"], "date_updated") && - downloaded_mod_json["mod"]["date_updated"] >= events_array[i].date_added) - { - reinstall = false; - modio::writeLogLine("Modfile changed event detected but you already have a newer version downloaded, the modfile will not be downloaded. Mod id: " + modio::toString(events_array[i].mod_id), MODIO_DEBUGLEVEL_LOG); - }else - { - nlohmann::json downloaded_mods_json_temp; - for (auto &downloaded_mod_temp_json : modio::g_downloaded_mods) - { - if(modio::hasKey(downloaded_mod_temp_json, "mod") && - modio::hasKey(downloaded_mod_temp_json["mod"], "id") && - downloaded_mod_temp_json["mod"]["id"] != events_array[i].mod_id) - { - downloaded_mods_json_temp.push_back(downloaded_mod_temp_json); - } - } - modio::g_downloaded_mods = downloaded_mods_json_temp; - - if(modio::hasKey(downloaded_mod_json, "downloaded_zip_path")) - { - std::string downloaded_zip_path = downloaded_mod_json["downloaded_zip_path"]; - modio::removeFile(downloaded_zip_path); - } - modio::writeLogLine("Modfile changed event detected and you have an older version downloaded, old modfile will be deleted. Mod id: " + modio::toString(events_array[i].mod_id), MODIO_DEBUGLEVEL_LOG); - break; - } - } - } - - if (reinstall) - { - modio::writeLogLine("Modfile changed. Mod id: " + modio::toString(events_array[i].mod_id) + " Reisntalling...", MODIO_DEBUGLEVEL_LOG); - mod_to_download_queue_ids.push_back(events_array[i].mod_id); - } + modio::writeLogLine("Modfile changed. Mod id: " + modio::toString(events_array[i].mod_id), MODIO_DEBUGLEVEL_LOG); + modio::writeLogLine(" Adding to the download queue...", MODIO_DEBUGLEVEL_LOG); + mod_to_download_queue_ids.push_back(events_array[i].mod_id); + break; } - break; case MODIO_EVENT_MOD_AVAILABLE: { // N/A @@ -312,15 +262,28 @@ static void onGetAllEventsPoll(void *object, ModioResponse response, ModioModEve } case MODIO_EVENT_MOD_EDITED: { - modio::writeLogLine("Mod updated. Mod id: " + modio::toString(events_array[i].mod_id) + " Updating cache...", MODIO_DEBUGLEVEL_LOG); - mod_edited_ids.push_back(events_array[i].mod_id); + modio::writeLogLine("Mod edited. Mod id: " + modio::toString(events_array[i].mod_id), MODIO_DEBUGLEVEL_LOG); + /* TODO: Re-enable local mod profile update? */ + //modio::writeLogLine("Mod updated. Mod id: " + modio::toString(events_array[i].mod_id) + " Updating cache...", MODIO_DEBUGLEVEL_LOG); + //mod_edited_ids.push_back(events_array[i].mod_id); break; } + case MODIO_EVENT_MOD_DELETED: + { + modio::writeLogLine("Mod deleted. Mod id: " + modio::toString(events_array[i].mod_id) + " Uninstalling...", MODIO_DEBUGLEVEL_LOG); + mod_deleted_ids.push_back(events_array[i].mod_id); + } } } /* TODO: Re-enable mod profile update? */ //if (mod_edited_ids.size() > 0) // updateModsCache(mod_edited_ids); + size_t deleted_mod_count = mod_deleted_ids.size(); + for (size_t i = 0; i < deleted_mod_count; ++i) + { + modioUninstallMod(mod_deleted_ids[i]); + } + if (mod_to_download_queue_ids.size() > 0) addModsToDownloadQueue(mod_to_download_queue_ids); @@ -550,159 +513,121 @@ void handleDownloadImageError(void *object, void (*callback)(void *object, Modio modioFreeResponse(&response); } -void processGenericLocalUnauthorizedRequest(void* object, void(*callback)(void* object, ModioResponse response)) +ModioResponse createUnauthorizedResponse() { modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); - + ModioResponse response; nlohmann::json empty_json; + modioInitResponse(&response, empty_json); response.code = 401; + + return response; +} + +void processGenericLocalUnauthorizedRequest(void* object, void(*callback)(void* object, ModioResponse response)) +{ + ModioResponse response = createUnauthorizedResponse(); + callback(object, response); + modioFreeResponse(&response); } void processLocalUnauthorizedRequestModParam(void* object, void (*callback)(void *object, ModioResponse response, ModioMod mod)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); + ModioResponse response = createUnauthorizedResponse(); - ModioResponse response; ModioMod mod; nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); modioInitMod(&mod, empty_json); callback(object, response, mod); - + modioFreeResponse(&response); modioFreeMod(&mod); } void processLocalUnauthorizedRequestModfileParam(void* object, void (*callback)(void *object, ModioResponse response, ModioModfile modfile)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); + ModioResponse response = createUnauthorizedResponse(); - ModioResponse response; ModioModfile modfile; nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); modioInitModfile(&modfile, empty_json); callback(object, response, modfile); - + modioFreeResponse(&response); modioFreeModfile(&modfile); } void processLocalUnauthorizedRequestBoolParam(void* object, void (*callback)(void *object, ModioResponse response, bool)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); - - ModioResponse response; - nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); + ModioResponse response = createUnauthorizedResponse(); callback(object, response, false); - + modioFreeResponse(&response); } void processLocalUnauthorizedRequestUserParam(void* object, void (*callback)(void *object, ModioResponse response, ModioUser user)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); + ModioResponse response = createUnauthorizedResponse(); - ModioResponse response; ModioUser user; nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); modioInitUser(&user, empty_json); callback(object, response, user); - + modioFreeResponse(&response); modioFreeUser(&user); } void processLocalUnauthorizedRequestModsParam(void* object, void (*callback)(void *object, ModioResponse response, ModioMod mods[], u32 mods_size)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); - - ModioResponse response; - nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); + ModioResponse response = createUnauthorizedResponse(); callback(object, response, NULL, 0); - + modioFreeResponse(&response); } void processLocalUnauthorizedRequestUserEventsParam(void* object, void (*callback)(void *object, ModioResponse response, ModioUserEvent* events_array, u32 events_array_size)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); - - ModioResponse response; - nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); + ModioResponse response = createUnauthorizedResponse(); callback(object, response, NULL, 0); - + modioFreeResponse(&response); } void processLocalUnauthorizedRequestGamesParam(void* object, void (*callback)(void *object, ModioResponse response, ModioGame games[], u32 games_size)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); - - ModioResponse response; - nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); + ModioResponse response = createUnauthorizedResponse(); callback(object, response, NULL, 0); - + modioFreeResponse(&response); } void processLocalUnauthorizedRequestModfilesParam(void* object, void (*callback)(void *object, ModioResponse response, ModioModfile modfiles[], u32 modfiles_size)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); - - ModioResponse response; - nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); + ModioResponse response = createUnauthorizedResponse(); callback(object, response, NULL, 0); - + modioFreeResponse(&response); } void processLocalUnauthorizedRequestRatingsParam(void* object, void (*callback)(void *object, ModioResponse response, ModioRating ratings[], u32 ratings_size)) { - modio::writeLogLine("Unauthorized local request found. 401 will be returned", MODIO_DEBUGLEVEL_LOG); - - ModioResponse response; - nlohmann::json empty_json; - response.code = 401; - - modioInitResponse(&response, empty_json); + ModioResponse response = createUnauthorizedResponse(); callback(object, response, NULL, 0); - + modioFreeResponse(&response); } diff --git a/src/c++/schemas/Error.cpp b/src/c++/schemas/Error.cpp index 9148a575..513e3215 100644 --- a/src/c++/schemas/Error.cpp +++ b/src/c++/schemas/Error.cpp @@ -7,6 +7,7 @@ namespace modio void Error::initialize(ModioError modio_error) { code = modio_error.code; + error_ref = modio_error.error_ref; if (modio_error.message) message = modio_error.message; errors.resize(modio_error.errors_array_size); @@ -21,15 +22,16 @@ nlohmann::json toJson(Error &error) nlohmann::json error_json; error_json["code"] = error.code; + error_json["error_ref"] = error.error_ref; error_json["message"] = error.message; nlohmann::json errors_json; - + for (u32 i = 0; icallbacks.push_back(callback); + get_user_subscriptions_params->objects.push_back(object); + return; + } + } + u32 call_number = modio::curlwrapper::getCallNumber(); - get_user_subscriptions_callbacks[call_number] = new GetUserSubscriptionsParams; - get_user_subscriptions_callbacks[call_number]->callback = callback; - get_user_subscriptions_callbacks[call_number]->object = object; - get_user_subscriptions_callbacks[call_number]->url = url_without_api_key; - get_user_subscriptions_callbacks[call_number]->is_cache = false; + GetUserSubscriptionsParams* new_get_user_subscriptions_params = new GetUserSubscriptionsParams; + get_user_subscriptions_callbacks[call_number] = new_get_user_subscriptions_params; + new_get_user_subscriptions_params->url = url_without_api_key; + new_get_user_subscriptions_params->is_cache = false; + new_get_user_subscriptions_params->callbacks.push_back(callback); + new_get_user_subscriptions_params->objects.push_back(object); std::string cache_filename = modio::getCallFileFromCache(url_without_api_key, cache_max_age_seconds); if(cache_filename != "") @@ -51,13 +70,10 @@ extern "C" nlohmann::json cache_file_json = modio::openJson(modio::getModIODirectory() + "cache/" + cache_filename); if(!cache_file_json.empty()) { - get_user_subscriptions_callbacks[call_number]->is_cache = true; + new_get_user_subscriptions_params->is_cache = true; modioOnGetUserSubscriptions(call_number, 200, cache_file_json); return; } - }else - { - modio::writeLogLine("No cache found", MODIO_DEBUGLEVEL_LOG); } modio::curlwrapper::get(call_number, url, modio::getHeaders(), &modioOnGetUserSubscriptions); diff --git a/src/c/methods/ModMethods.cpp b/src/c/methods/ModMethods.cpp index 38e57c21..a0bdac77 100644 --- a/src/c/methods/ModMethods.cpp +++ b/src/c/methods/ModMethods.cpp @@ -39,8 +39,6 @@ extern "C" std::string url_without_api_key = modio::MODIO_URL + modio::MODIO_VERSION_PATH + "games/" + modio::toString(modio::GAME_ID) + "/mods?" + (filter_string ? filter_string : ""); std::string url = url_without_api_key + "&api_key=" + modio::API_KEY; - u32 call_number = modio::curlwrapper::getCallNumber(); - for(auto get_all_mods_callbacks_iterator : get_all_mods_callbacks) { if(get_all_mods_callbacks_iterator.second->url == url_without_api_key) @@ -53,6 +51,8 @@ extern "C" } } + u32 call_number = modio::curlwrapper::getCallNumber(); + GetAllModsParams* new_get_all_mods_params = new GetAllModsParams; get_all_mods_callbacks[call_number] = new_get_all_mods_params; new_get_all_mods_params->url = url_without_api_key; @@ -63,8 +63,8 @@ extern "C" std::string cache_filename = modio::getCallFileFromCache(url_without_api_key, cache_max_age_seconds); if (cache_filename != "") { + modio::writeLogLine("Cache file found: " + cache_filename, MODIO_DEBUGLEVEL_LOG); nlohmann::json cache_file_json = modio::openJson(modio::getModIODirectory() + "cache/" + cache_filename); - nlohmann::json empty_json = {}; if (!cache_file_json.empty()) { new_get_all_mods_params->is_cache = true; diff --git a/src/c/methods/callbacks/MeCallbacks.cpp b/src/c/methods/callbacks/MeCallbacks.cpp index 87c1d37b..74226868 100644 --- a/src/c/methods/callbacks/MeCallbacks.cpp +++ b/src/c/methods/callbacks/MeCallbacks.cpp @@ -42,8 +42,8 @@ void modioOnGetUserSubscriptions(u32 call_number, u32 response_code, nlohmann::j ModioResponse response; modioInitResponse(&response, response_json); response.code = response_code; - ModioMod *mods = NULL; u32 mods_size = 0; + ModioMod *mods = NULL; if (response.code == 200) { @@ -64,7 +64,10 @@ void modioOnGetUserSubscriptions(u32 call_number, u32 response_code, nlohmann::j } } - get_user_subscriptions_callbacks[call_number]->callback(get_user_subscriptions_callbacks[call_number]->object, response, mods, mods_size); + for(int i=0; icallbacks.size(); i++) + { + get_user_subscriptions_callbacks[call_number]->callbacks[i](get_user_subscriptions_callbacks[call_number]->objects[i], response, mods, mods_size); + } delete get_user_subscriptions_callbacks[call_number]; get_user_subscriptions_callbacks.erase(call_number); diff --git a/src/c/schemas/ModioError.cpp b/src/c/schemas/ModioError.cpp index c46014bd..7235e7d7 100644 --- a/src/c/schemas/ModioError.cpp +++ b/src/c/schemas/ModioError.cpp @@ -10,6 +10,10 @@ extern "C" if(modio::hasKey(error_json, "code")) error->code = error_json["code"]; + error->error_ref = 0; + if(modio::hasKey(error_json, "error_ref")) + error->error_ref = error_json["error_ref"]; + error->message = NULL; if(modio::hasKey(error_json, "message")) { @@ -44,6 +48,7 @@ extern "C" void modioInitErrorCpp(ModioError* modio_error, modio::Error* error) { modio_error->code = error->code; + modio_error->error_ref = error->error_ref; modio_error->message = new char[error->message.size() + 1]; strcpy(modio_error->message, error->message.c_str()); diff --git a/src/c/schemas/ModioModEvent.cpp b/src/c/schemas/ModioModEvent.cpp index 704e9b82..4f063ddf 100644 --- a/src/c/schemas/ModioModEvent.cpp +++ b/src/c/schemas/ModioModEvent.cpp @@ -29,6 +29,8 @@ extern "C" event->event_type = MODIO_EVENT_MOD_UNAVAILABLE; else if(event_json["event_type"] == "MOD_EDITED") event->event_type = MODIO_EVENT_MOD_EDITED; + else if(event_json["event_type"] == "MOD_DELETED") + event->event_type = MODIO_EVENT_MOD_DELETED; else if(event_json["event_type"] == "USER_TEAM_JOIN") event->event_type = MODIO_EVENT_USER_TEAM_JOIN; else if(event_json["event_type"] == "USER_TEAM_LEAVE") diff --git a/src/c/schemas/ModioUserEvent.cpp b/src/c/schemas/ModioUserEvent.cpp index 3e48c219..48e4c606 100644 --- a/src/c/schemas/ModioUserEvent.cpp +++ b/src/c/schemas/ModioUserEvent.cpp @@ -33,6 +33,8 @@ extern "C" event->event_type = MODIO_EVENT_MOD_UNAVAILABLE; else if(event_json["event_type"] == "MOD_EDITED") event->event_type = MODIO_EVENT_MOD_EDITED; + else if(event_json["event_type"] == "MOD_DELETED") + event->event_type = MODIO_EVENT_MOD_DELETED; else if(event_json["event_type"] == "USER_TEAM_JOIN") event->event_type = MODIO_EVENT_USER_TEAM_JOIN; else if(event_json["event_type"] == "USER_TEAM_LEAVE") diff --git a/src/wrappers/CurlWrapper.cpp b/src/wrappers/CurlWrapper.cpp index 7f34b593..2dcab32d 100644 --- a/src/wrappers/CurlWrapper.cpp +++ b/src/wrappers/CurlWrapper.cpp @@ -533,8 +533,31 @@ void downloadMod(QueuedModDownload *queued_mod_download) get(call_number, url, modio::getHeaders(), &onGetDownloadMod); } -void removeDownloadedModfile(nlohmann::json downloaded_mod) +nlohmann::json getDownloadedModJson(u32 mod_id) +{ + for (auto &downloaded_mod_json : modio::g_downloaded_mods) + { + if (modio::hasKey(downloaded_mod_json, "mod") && + modio::hasKey(downloaded_mod_json["mod"], "id") && + downloaded_mod_json["mod"]["id"] == mod_id) + { + return downloaded_mod_json; + } + } + nlohmann::json empty_json; + return empty_json; +} + +void removeDownloadedMod(u32 mod_id) { + modio::writeLogLine("Removing mod: " + mod_id, MODIO_DEBUGLEVEL_ERROR); + nlohmann::json downloaded_mod = getDownloadedModJson(mod_id); + if(!downloaded_mod.empty()) + { + modio::writeLogLine("Could not remove modfile. Empty downloaded mod json object.", MODIO_DEBUGLEVEL_ERROR); + return; + } + if(!modio::hasKey(downloaded_mod, "downloaded_zip_path")) { modio::writeLogLine("Could not find the downloaded_zip_path attribute on to the mod download json.", MODIO_DEBUGLEVEL_ERROR); @@ -593,21 +616,6 @@ void cancelModDownload(QueuedModDownload* queued_mod_download) writeLogLine("Finished cancel preprocess.", MODIO_DEBUGLEVEL_WARNING); } -nlohmann::json getDownloadedModJson(u32 mod_id) -{ - for (auto &downloaded_mod_json : modio::g_downloaded_mods) - { - if (modio::hasKey(downloaded_mod_json, "mod") && - modio::hasKey(downloaded_mod_json["mod"], "id") && - downloaded_mod_json["mod"]["id"] == mod_id) - { - return downloaded_mod_json; - } - } - nlohmann::json empty_json; - return empty_json; -} - QueuedModDownload* getQueuedModJson(u32 mod_id) { for (auto &queued_mod_download : g_mod_download_queue) @@ -620,86 +628,73 @@ QueuedModDownload* getQueuedModJson(u32 mod_id) return NULL; } -bool isDownloadedModfileUpdated(nlohmann::json downloaded_mod, u32 new_modfile_date) +bool isModDownloaded(u32 mod_id, u32 new_modfile_id) { - if (modio::hasKey(downloaded_mod, "mod") && - modio::hasKey(downloaded_mod["mod"], "modfile") && - modio::hasKey(downloaded_mod["mod"]["modfile"], "date_added") && - downloaded_mod["mod"]["date_added"] >= new_modfile_date) + modio::writeLogLine("Checking if mod is downloaded...", MODIO_DEBUGLEVEL_LOG); + nlohmann::json downloaded_mod = getDownloadedModJson(mod_id); + if(!downloaded_mod.empty()) { - modio::writeLogLine("Downloaded mod is updated " + modio::toString(new_modfile_date), MODIO_DEBUGLEVEL_LOG); - return true; + if (modio::hasKey(downloaded_mod, "mod") && + modio::hasKey(downloaded_mod["mod"], "modfile") && + modio::hasKey(downloaded_mod["mod"]["modfile"], "id") && + downloaded_mod["mod"]["modfile"]["id"] == new_modfile_id) + { + modio::writeLogLine("Mod is already downloaded.", MODIO_DEBUGLEVEL_LOG); + return true; + } + removeDownloadedMod(downloaded_mod); } - modio::writeLogLine("Downloaded mod is not updated " + modio::toString(new_modfile_date), MODIO_DEBUGLEVEL_LOG); + modio::writeLogLine("Mod is not downloaded.", MODIO_DEBUGLEVEL_LOG); return false; } -bool isInstalledModfileUpdated(u32 mod_id, u32 date_updated) +bool isModInstalled(u32 mod_id, u32 modfile_id) { + modio::writeLogLine("Checking if mod is installed...", MODIO_DEBUGLEVEL_LOG); for (auto installed_mod : modio::installed_mods) { if (modio::hasKey(installed_mod, "mod_id") && - modio::hasKey(installed_mod, "date_updated") && + modio::hasKey(installed_mod, "modfile_id") && installed_mod["mod_id"] == mod_id && - installed_mod["date_updated"] >= date_updated) + installed_mod["modfile_id"] == modfile_id) { - u32 installed_mod_updated_at = installed_mod["date_updated"]; - writeLogLine(std::string("Installed mod found: " + modio::toString(installed_mod_updated_at)), MODIO_DEBUGLEVEL_LOG); - modio::writeLogLine("Modfile changed event detected but you already have a newer version installed, the modfile will not be downloaded. Mod id: " + modio::toString(mod_id), MODIO_DEBUGLEVEL_LOG); + u32 modfile_id = installed_mod["modfile_id"]; + modio::writeLogLine("Mod is already installed.", MODIO_DEBUGLEVEL_LOG); return true; } } + modio::writeLogLine("Mod is not installed.", MODIO_DEBUGLEVEL_LOG); return false; } -void removeDownloadedModfile(u32 mod_id) -{ - nlohmann::json downloaded_mod = getDownloadedModJson(mod_id); - if(!downloaded_mod.empty()) - { - removeDownloadedModfile(downloaded_mod); - } -} - -bool modEnqueuePreprocess(u32 mod_id, u32 modfile_date_added) +bool isModQueued(u32 mod_id, u32 modfile_id) { - nlohmann::json downloaded_mod = getDownloadedModJson(mod_id); - if(!downloaded_mod.empty()) - { - writeLogLine(std::string("Downloaded mod found: "), MODIO_DEBUGLEVEL_LOG); - writeLogLine(downloaded_mod, MODIO_DEBUGLEVEL_LOG); - if(isDownloadedModfileUpdated(downloaded_mod, modfile_date_added)) - return false; - else - removeDownloadedModfile(downloaded_mod); - } - - if(isInstalledModfileUpdated(mod_id, modfile_date_added)) - { - writeLogLine("Did not add the mod: " + toString(mod_id) + " to the mod download queue. It's already installed in a more recent version.", MODIO_DEBUGLEVEL_WARNING); - return false; - } - + modio::writeLogLine("Checking if mod is on the download queue...", MODIO_DEBUGLEVEL_LOG); QueuedModDownload* queued_mod_download = getQueuedModJson(mod_id); if(queued_mod_download) { - if(queued_mod_download->mod.modfile.date_added >= modfile_date_added) + if(queued_mod_download->mod.modfile.id == modfile_id) { - writeLogLine("Did not add the mod: " + toString(mod_id) + " to the mod download queue. It's already on the download queue in a more recent version.", MODIO_DEBUGLEVEL_WARNING); - return false; + modio::writeLogLine("Mod is already on the download queue.", MODIO_DEBUGLEVEL_LOG); + return true; }else { cancelModDownload(queued_mod_download); } } - return true; + modio::writeLogLine("Mod is not on the download queue.", MODIO_DEBUGLEVEL_LOG); + return false; } void queueModDownload(ModioMod &modio_mod) { writeLogLine("Adding to download queue mod: " + toString(modio_mod.id), MODIO_DEBUGLEVEL_LOG); - if(!modEnqueuePreprocess(modio_mod.id, modio_mod.modfile.date_added)) + + if(isModDownloaded(modio_mod.id, modio_mod.modfile.id) + || isModInstalled(modio_mod.id, modio_mod.modfile.id) + || isModQueued(modio_mod.id, modio_mod.modfile.id)) { + writeLogLine("Mod is up to date, the mod won't be queued.", MODIO_DEBUGLEVEL_LOG); return; } @@ -717,7 +712,7 @@ void queueModDownload(ModioMod &modio_mod) updateModDownloadQueueFile(); - writeLogLine("Download queued. Mod id: " + toString(modio_mod.id), MODIO_DEBUGLEVEL_LOG); + writeLogLine("Download queued.", MODIO_DEBUGLEVEL_LOG); if (g_mod_download_queue.size() == 1) {