diff --git a/src/api/api.cpp b/src/api/api.cpp index ffde700c..307e7782 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -365,13 +365,26 @@ LOOT_API unsigned int loot_eval_lists(loot_db db, const unsigned int language) { LOOT_API unsigned int loot_sort_plugins(loot_db db, char *** const sortedPlugins, - size_t * numPlugins) { + size_t * const numPlugins) { + if (db == nullptr || sortedPlugins == nullptr || numPlugins == nullptr) + return c_error(loot_error_invalid_args, "Null pointer passed."); + return loot_ok; } LOOT_API unsigned int loot_apply_load_order(loot_db db, - const char ** const loadOrder, - size_t numPlugins) { + const char * const * const loadOrder, + const size_t numPlugins) { + if (db == nullptr || loadOrder == nullptr) + return c_error(loot_error_invalid_args, "Null pointer passed."); + + try { + db->SetLoadOrder(loadOrder, numPlugins); + } + catch (loot::error &e) { + return c_error(e); + } + return loot_ok; } @@ -380,6 +393,9 @@ LOOT_API unsigned int loot_update_masterlist(loot_db db, const char * const remoteURL, const char * const remoteBranch, bool * const updated) { + if (db == nullptr || masterlistPath == nullptr || remoteURL == nullptr || remoteBranch == nullptr || updated == nullptr) + return c_error(loot_error_invalid_args, "Null pointer passed."); + return loot_ok; } @@ -388,6 +404,9 @@ LOOT_API unsigned int loot_get_masterlist_revision(const char * const masterlist char ** const revisionID, char ** const revisionDate, bool * const isModified) { + if (masterlistPath == nullptr || revisionID == nullptr || revisionDate == nullptr || isModified == nullptr) + return c_error(loot_error_invalid_args, "Null pointer passed."); + return loot_ok; } diff --git a/src/api/api.h b/src/api/api.h index 40f1167d..50694ec7 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -404,7 +404,7 @@ extern "C" */ LOOT_API unsigned int loot_sort_plugins(loot_db db, char *** const sortedPlugins, - size_t * numPlugins); + size_t * const numPlugins); /** * @brief Applies the given load order. @@ -417,8 +417,8 @@ extern "C" * @returns A return code. */ LOOT_API unsigned int loot_apply_load_order(loot_db db, - const char ** const loadOrder, - size_t numPlugins); + const char * const * const loadOrder, + const size_t numPlugins); /** * @brief Update the given masterlist. diff --git a/src/backend/game.cpp b/src/backend/game.cpp index d66ad6d7..fe6ddff6 100644 --- a/src/backend/game.cpp +++ b/src/backend/game.cpp @@ -620,12 +620,10 @@ namespace loot { lo_destroy_handle(gh); } - void Game::SetLoadOrder(const std::list& loadOrder) const { + void Game::SetLoadOrder(const char * const * const loadOrder, const size_t numPlugins) const { BOOST_LOG_TRIVIAL(debug) << "Setting load order for game: " << _name; lo_game_handle gh = nullptr; - char ** pluginArr = nullptr; - size_t pluginArrSize = 0; int ret; if (Id() == Game::tes4) ret = lo_create_handle(&gh, LIBLO_GAME_TES4, gamePath.string().c_str()); @@ -670,19 +668,7 @@ namespace loot { throw error(error::liblo_error, err); } - pluginArrSize = loadOrder.size(); - pluginArr = new char*[pluginArrSize]; - int i = 0; - for (const auto &plugin : loadOrder) { - pluginArr[i] = new char[plugin.length() + 1]; - strcpy(pluginArr[i], plugin.c_str()); - ++i; - } - - if (lo_set_load_order(gh, pluginArr, pluginArrSize) != LIBLO_OK) { - for (size_t i = 0; i < pluginArrSize; i++) - delete[] pluginArr[i]; - delete[] pluginArr; + if (lo_set_load_order(gh, loadOrder, numPlugins) != LIBLO_OK) { const char * e = nullptr; string err; lo_get_error_message(&e); @@ -699,11 +685,33 @@ namespace loot { throw error(error::liblo_error, err); } + lo_destroy_handle(gh); + } + + void Game::SetLoadOrder(const std::list& loadOrder) const { + + size_t pluginArrSize = loadOrder.size(); + char ** pluginArr = new char*[pluginArrSize]; + int i = 0; + for (const auto &plugin : loadOrder) { + pluginArr[i] = new char[plugin.length() + 1]; + strcpy(pluginArr[i], plugin.c_str()); + ++i; + } + + try { + SetLoadOrder(pluginArr, pluginArrSize); + } + catch (error &e) { + for (size_t i = 0; i < pluginArrSize; i++) + delete[] pluginArr[i]; + delete[] pluginArr; + throw e; + } + for (size_t i = 0; i < pluginArrSize; i++) delete[] pluginArr[i]; delete[] pluginArr; - - lo_destroy_handle(gh); } void Game::RedatePlugins() { diff --git a/src/backend/game.h b/src/backend/game.h index 13cec264..30329d34 100644 --- a/src/backend/game.h +++ b/src/backend/game.h @@ -131,6 +131,7 @@ namespace loot { void GetLoadOrder(std::list& loadOrder) const; void SetLoadOrder(const std::list& loadOrder) const; //Modifies game load order, even though const. + void SetLoadOrder(const char * const * const loadOrder, const size_t numPlugins) const; // For API. void RefreshActivePluginsList(); void RedatePlugins(); //Change timestamps to match load order (Skyrim only).