Improved error handling of gta_data_service. (#412)
* Bug fix for #403 * Fixed Issue #403 * Minor fixes. * Improved error handling of gta_data_service. Fixed an issue where the game crashes when throwing invalid json format exception. * Minor fixes. * Minor fixes. * Improved error handling of gta_data_service. Also extended the timeout to 30s. * Improved error handling of gta_data_service. - Fixed an issue where http_request doesn't handle HEAD requests and socket.recv error correctly. - Limited gta_data_service to have only 1 active http request. * Minor fixes. * Minor fixes.
This commit is contained in:
@ -9,31 +9,40 @@ namespace big
|
||||
{
|
||||
gta_data_service::gta_data_service()
|
||||
{
|
||||
const std::string url_prefix = "http://github-proxy.damon.sh/DurtyFree/gta-v-data-dumps/master/";
|
||||
g_thread_pool->push([this] {
|
||||
while (!g_running)
|
||||
std::this_thread::sleep_for(1s);
|
||||
|
||||
load_from_file(
|
||||
"./lib/vehicles.json",
|
||||
"./lib/vehicles_etag.txt",
|
||||
url_prefix + "vehicles.json",
|
||||
>a_data_service::load_vehicles,
|
||||
"Vehicle"
|
||||
);
|
||||
const std::string url_prefix = "http://github-proxy.damon.sh/DurtyFree/gta-v-data-dumps/master/";
|
||||
|
||||
load_from_file(
|
||||
"./lib/peds.json",
|
||||
"./lib/peds_etag.txt",
|
||||
url_prefix + "peds.json",
|
||||
>a_data_service::load_peds,
|
||||
"Ped"
|
||||
);
|
||||
this->load_from_file(
|
||||
"./lib/vehicles.json",
|
||||
"./lib/vehicles_etag.txt",
|
||||
url_prefix + "vehicles.json",
|
||||
>a_data_service::load_vehicles,
|
||||
"Vehicle"
|
||||
);
|
||||
|
||||
load_from_file(
|
||||
"./lib/weapons.json",
|
||||
"./lib/weapons_etag.txt",
|
||||
url_prefix + "weapons.json",
|
||||
>a_data_service::load_weapons,
|
||||
"Weapon"
|
||||
);
|
||||
std::this_thread::sleep_for(1s);
|
||||
|
||||
this->load_from_file(
|
||||
"./lib/peds.json",
|
||||
"./lib/peds_etag.txt",
|
||||
url_prefix + "peds.json",
|
||||
>a_data_service::load_peds,
|
||||
"Ped"
|
||||
);
|
||||
|
||||
std::this_thread::sleep_for(1s);
|
||||
|
||||
this->load_from_file(
|
||||
"./lib/weapons.json",
|
||||
"./lib/weapons_etag.txt",
|
||||
url_prefix + "weapons.json",
|
||||
>a_data_service::load_weapons,
|
||||
"Weapon"
|
||||
);
|
||||
});
|
||||
|
||||
g_gta_data_service = this;
|
||||
}
|
||||
@ -127,64 +136,85 @@ namespace big
|
||||
}
|
||||
|
||||
|
||||
void gta_data_service::load_from_file(std::string file_path, std::string etag_path, std::string url, bool(gta_data_service::* load_func)(file), std::string data_name)
|
||||
{
|
||||
void gta_data_service::load_from_file(
|
||||
std::string file_path, std::string etag_path, std::string url,
|
||||
bool(gta_data_service::* load_func)(std::filesystem::path), std::string data_name
|
||||
) {
|
||||
file file_to_load(g_file_manager->get_project_file(file_path));
|
||||
file file_etag(g_file_manager->get_project_file(etag_path));
|
||||
auto file_to_load_path = file_to_load.get_path();
|
||||
auto file_etag_path = file_etag.get_path();
|
||||
|
||||
if (file_to_load.exists())
|
||||
bool up_to_date = false;
|
||||
|
||||
for (int retry = 0; retry < 3 && g_running; retry++)
|
||||
{
|
||||
if ((this->*load_func)(file_to_load))
|
||||
{
|
||||
LOG(INFO) << "Data loaded: " + data_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(INFO) << "Data invalid: " + data_name;
|
||||
}
|
||||
}
|
||||
LOG(INFO) << "Checking update (attempt: " << (retry + 1) << "/3): " << data_name;
|
||||
|
||||
g_thread_pool->push([this, file_path, etag_path, url, load_func, data_name]() {
|
||||
file file_to_load(g_file_manager->get_project_file(file_path));
|
||||
file file_etag(g_file_manager->get_project_file(etag_path));
|
||||
|
||||
for (int retry = 0; retry < 2; retry++)
|
||||
try
|
||||
{
|
||||
bool ret = remote::update_binary(
|
||||
url,
|
||||
file_to_load.get_path(),
|
||||
file_etag.get_path()
|
||||
file_to_load_path,
|
||||
file_etag_path
|
||||
);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
if ((this->*load_func)(file_to_load))
|
||||
up_to_date = true;
|
||||
|
||||
LOG(INFO) << "Data updated: " << data_name;
|
||||
|
||||
if ((this->*load_func)(file_to_load_path))
|
||||
{
|
||||
LOG(INFO) << "Data loaded: " + data_name;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(INFO) << "Data invalid: " + data_name;
|
||||
|
||||
try
|
||||
{
|
||||
std::filesystem::remove(file_to_load.get_path());
|
||||
std::filesystem::remove(file_etag.get_path());
|
||||
}
|
||||
catch (...) { }
|
||||
std::filesystem::remove(file_to_load_path);
|
||||
std::filesystem::remove(file_etag_path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (!file_to_load.exists())
|
||||
{
|
||||
LOG(WARNING) << "Failed to download data: " + data_name;
|
||||
}
|
||||
}
|
||||
});
|
||||
catch (...)
|
||||
{
|
||||
LOG(WARNING) << "Data invalid: " + data_name;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(2s);
|
||||
}
|
||||
|
||||
if (!up_to_date)
|
||||
{
|
||||
LOG(WARNING) << "Data not updated: " + data_name;
|
||||
|
||||
try
|
||||
{
|
||||
if (file_to_load.exists())
|
||||
{
|
||||
if ((this->*load_func)(file_to_load_path))
|
||||
{
|
||||
LOG(INFO) << "Cache loaded: " + data_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::filesystem::remove(file_to_load_path);
|
||||
std::filesystem::remove(file_etag_path);
|
||||
throw std::exception("");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOG(WARNING) << "Cache invalid: " + data_name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool gta_data_service::load_vehicles(file file_to_load)
|
||||
bool gta_data_service::load_vehicles(std::filesystem::path path)
|
||||
{
|
||||
std::ifstream file(file_to_load.get_path());
|
||||
std::ifstream file(path);
|
||||
nlohmann::json all_vehicles;
|
||||
|
||||
try
|
||||
@ -235,9 +265,9 @@ namespace big
|
||||
}
|
||||
|
||||
|
||||
bool gta_data_service::load_peds(file file_to_load)
|
||||
bool gta_data_service::load_peds(std::filesystem::path path)
|
||||
{
|
||||
std::ifstream file(file_to_load.get_path());
|
||||
std::ifstream file(path);
|
||||
nlohmann::json all_peds;
|
||||
|
||||
try
|
||||
@ -286,9 +316,9 @@ namespace big
|
||||
}
|
||||
|
||||
|
||||
bool gta_data_service::load_weapons(file file_to_load)
|
||||
bool gta_data_service::load_weapons(std::filesystem::path path)
|
||||
{
|
||||
std::ifstream file(file_to_load.get_path());
|
||||
std::ifstream file(path);
|
||||
nlohmann::json all_weapons;
|
||||
|
||||
try
|
||||
|
@ -41,11 +41,14 @@ namespace big
|
||||
const std::vector<weapon_item>& get_weapon_arr();
|
||||
|
||||
private:
|
||||
void load_from_file(std::string file_path, std::string etag_path, std::string url, bool(gta_data_service::* load_func)(file), std::string data_name);
|
||||
void load_from_file(
|
||||
std::string file_path, std::string etag_path, std::string url,
|
||||
bool(gta_data_service::* load_func)(std::filesystem::path), std::string data_name
|
||||
);
|
||||
|
||||
bool load_vehicles(file file_to_load);
|
||||
bool load_peds(file file_to_load);
|
||||
bool load_weapons(file file_to_load);
|
||||
bool load_vehicles(std::filesystem::path path);
|
||||
bool load_peds(std::filesystem::path path);
|
||||
bool load_weapons(std::filesystem::path path);
|
||||
};
|
||||
|
||||
inline gta_data_service* g_gta_data_service{};
|
||||
|
Reference in New Issue
Block a user