Fixed Issue #403 (#408)

Fixes #403
This commit is contained in:
aa15032261 2022-08-13 17:19:18 +08:00 committed by GitHub
parent 11073bd80d
commit c0e1ab6a24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 172 additions and 127 deletions

View File

@ -122,6 +122,14 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
g_script_mgr.remove_all_scripts(); g_script_mgr.remove_all_scripts();
LOG(INFO) << "Scripts unregistered."; LOG(INFO) << "Scripts unregistered.";
// Make sure that all threads created don't have any blocking loops
// otherwise make sure that they have stopped executing
thread_pool_instance->destroy();
LOG(INFO) << "Destroyed thread pool.";
thread_pool_instance.reset();
LOG(INFO) << "Thread pool uninitialized.";
gui_service_instance.reset(); gui_service_instance.reset();
LOG(INFO) << "Gui Service reset."; LOG(INFO) << "Gui Service reset.";
gta_data_service_instance.reset(); gta_data_service_instance.reset();
@ -144,14 +152,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
LOG(INFO) << "Context Service reset."; LOG(INFO) << "Context Service reset.";
LOG(INFO) << "Services uninitialized."; LOG(INFO) << "Services uninitialized.";
// Make sure that all threads created don't have any blocking loops
// otherwise make sure that they have stopped executing
thread_pool_instance->destroy();
LOG(INFO) << "Destroyed thread pool.";
thread_pool_instance.reset();
LOG(INFO) << "Thread pool uninitialized.";
hooking_instance.reset(); hooking_instance.reset();
LOG(INFO) << "Hooking uninitialized."; LOG(INFO) << "Hooking uninitialized.";

View File

@ -128,15 +128,21 @@ namespace big
} }
void gta_data_service::load_from_file(std::string file_path, std::string etag_path, std::string url, void(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)(file), std::string data_name)
{ {
file file_to_load(g_file_manager->get_project_file(file_path)); file file_to_load(g_file_manager->get_project_file(file_path));
if (file_to_load.exists()) if (file_to_load.exists())
{ {
(this->*load_func)(file_to_load); if ((this->*load_func)(file_to_load))
{
LOG(INFO) << "Data loaded: " + data_name; LOG(INFO) << "Data loaded: " + data_name;
} }
else
{
LOG(INFO) << "Data invalid: " + data_name;
}
}
g_thread_pool->push([this, file_path, etag_path, url, load_func, 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_to_load(g_file_manager->get_project_file(file_path));
@ -152,8 +158,24 @@ namespace big
if (ret) if (ret)
{ {
(this->*load_func)(file_to_load); if ((this->*load_func)(file_to_load))
LOG(INFO) << "Data updated: " + data_name; {
LOG(INFO) << "Data loaded: " + data_name;
}
else
{
LOG(INFO) << "Data invalid: " + data_name;
try
{
std::ofstream file_ofstream(file_path, std::ios::binary | std::ios::trunc);
file_ofstream << "";
std::ofstream file_etag_ofstream(etag_path, std::ios::binary | std::ios::trunc);
file_etag_ofstream << "";
}
catch (...) { }
}
break; break;
} }
else if (!file_to_load.exists()) else if (!file_to_load.exists())
@ -164,24 +186,23 @@ namespace big
}); });
} }
void gta_data_service::load_vehicles(file file_to_load) bool gta_data_service::load_vehicles(file file_to_load)
{ {
m_vehicle_class_arr.clear();
m_vehicle_hash_idx_map.clear();
m_vehicle_item_arr.clear();
std::ifstream file(file_to_load.get_path()); std::ifstream file(file_to_load.get_path());
nlohmann::json all_vehicles; nlohmann::json all_vehicles;
try try
{ {
file >> all_vehicles; file >> all_vehicles;
}
catch (const std::exception& ex) if (!all_vehicles.is_array())
{ {
LOG(WARNING) << "Failed to load vehicles.json:\n" << ex.what(); throw "Invalid json format.";
} }
m_vehicle_class_arr.clear();
m_vehicle_hash_idx_map.clear();
m_vehicle_item_arr.clear();
for (auto& item_json : all_vehicles) for (auto& item_json : all_vehicles)
{ {
@ -208,26 +229,34 @@ namespace big
std::sort(m_vehicle_class_arr.begin(), m_vehicle_class_arr.end()); std::sort(m_vehicle_class_arr.begin(), m_vehicle_class_arr.end());
} }
} }
catch (const std::exception& ex)
void gta_data_service::load_peds(file file_to_load)
{ {
m_ped_type_arr.clear(); LOG(WARNING) << "Failed to load vehicles.json:\n" << ex.what();
m_ped_hash_idx_map.clear(); return false;
m_ped_item_arr.clear(); }
return true;
}
bool gta_data_service::load_peds(file file_to_load)
{
std::ifstream file(file_to_load.get_path()); std::ifstream file(file_to_load.get_path());
nlohmann::json all_peds; nlohmann::json all_peds;
try try
{ {
file >> all_peds; file >> all_peds;
}
catch (const std::exception& ex) if (!all_peds.is_array())
{ {
LOG(WARNING) << "Failed to load peds.json:\n" << ex.what(); throw "Invalid json format.";
} }
m_ped_type_arr.clear();
m_ped_hash_idx_map.clear();
m_ped_item_arr.clear();
for (auto& item_json : all_peds) for (auto& item_json : all_peds)
{ {
if ( if (
@ -251,26 +280,34 @@ namespace big
std::sort(m_ped_type_arr.begin(), m_ped_type_arr.end()); std::sort(m_ped_type_arr.begin(), m_ped_type_arr.end());
} }
} }
catch (const std::exception& ex)
void gta_data_service::load_weapons(file file_to_load)
{ {
m_weapon_type_arr.clear(); LOG(WARNING) << "Failed to load peds.json:\n" << ex.what();
m_weapon_hash_idx_map.clear(); return false;
m_weapon_item_arr.clear(); }
return true;
}
bool gta_data_service::load_weapons(file file_to_load)
{
std::ifstream file(file_to_load.get_path()); std::ifstream file(file_to_load.get_path());
nlohmann::json all_weapons; nlohmann::json all_weapons;
try try
{ {
file >> all_weapons; file >> all_weapons;
}
catch (const std::exception& ex) if (!all_weapons.is_array())
{ {
LOG(WARNING) << "Failed to load weapons.json:\n" << ex.what(); throw "Invalid json format.";
} }
m_weapon_type_arr.clear();
m_weapon_hash_idx_map.clear();
m_weapon_item_arr.clear();
constexpr Hash hash_blacklist_arr[] = { constexpr Hash hash_blacklist_arr[] = {
RAGE_JOAAT("WEAPON_BIRD_CRAP"), RAGE_JOAAT("WEAPON_BIRD_CRAP"),
RAGE_JOAAT("WEAPON_DIGISCANNER"), RAGE_JOAAT("WEAPON_DIGISCANNER"),
@ -317,5 +354,14 @@ namespace big
std::sort(m_weapon_type_arr.begin(), m_weapon_type_arr.end()); std::sort(m_weapon_type_arr.begin(), m_weapon_type_arr.end());
} }
}
catch (const std::exception& ex)
{
LOG(WARNING) << "Failed to load weapons.json:\n" << ex.what();
return false;
}
return true;
} }
} }

View File

@ -41,11 +41,11 @@ namespace big
const std::vector<weapon_item>& get_weapon_arr(); const std::vector<weapon_item>& get_weapon_arr();
private: private:
void load_from_file(std::string file_path, std::string etag_path, std::string url, void(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)(file), std::string data_name);
void load_vehicles(file file_to_load); bool load_vehicles(file file_to_load);
void load_peds(file file_to_load); bool load_peds(file file_to_load);
void load_weapons(file file_to_load); bool load_weapons(file file_to_load);
}; };
inline gta_data_service* g_gta_data_service{}; inline gta_data_service* g_gta_data_service{};

View File

@ -28,7 +28,6 @@ namespace big
void thread_pool::destroy() void thread_pool::destroy()
{ {
this->m_managing_thread.join(); this->m_managing_thread.join();
{ {
std::unique_lock lock(m_lock); std::unique_lock lock(m_lock);
this->m_accept_jobs = false; this->m_accept_jobs = false;

2
vendor/json vendored

@ -1 +1 @@
Subproject commit 350ff4f7ced7c4117eae2fb93df02823c8021fcb Subproject commit 5d550374f8ef362cfb4dfb5b689b23218546b1e7