Added ped model data. (#349)

This commit is contained in:
aa15032261
2022-07-19 18:19:19 +08:00
committed by GitHub
parent c9e6ed5db2
commit 18ab39394f
13 changed files with 492 additions and 199 deletions

View File

@ -0,0 +1,220 @@
#include "api/remote.hpp"
#include "file_manager.hpp"
#include "thread_pool.hpp"
#include "gta_data_service.hpp"
namespace big
{
gta_data_service::gta_data_service() :
m_vehicle_file(g_file_manager->get_project_file("./lib/vehicles.json")),
m_vehicle_file_etag(g_file_manager->get_project_file("./lib/vehicles_etag.txt")),
m_ped_file(g_file_manager->get_project_file("./lib/peds.json")),
m_ped_file_etag(g_file_manager->get_project_file("./lib/peds_etag.txt"))
{
load_from_file(
m_vehicle_file,
m_vehicle_file_etag,
"http://github-proxy.damon.sh/DurtyFree/gta-v-data-dumps/master/vehicles.json",
&gta_data_service::load_vehicles,
"Vehicle"
);
load_from_file(
m_ped_file,
m_ped_file_etag,
"http://github-proxy.damon.sh/DurtyFree/gta-v-data-dumps/master/peds.json",
&gta_data_service::load_ped,
"Ped"
);
g_gta_data_service = this;
}
gta_data_service::~gta_data_service()
{
g_gta_data_service = nullptr;
}
const vehicle_item& gta_data_service::find_vehicle_by_hash(Hash hash)
{
int idx = -1;
if (m_vehicle_hash_idx_map.count(hash))
{
idx = m_vehicle_hash_idx_map[hash];
}
if (idx == -1)
{
return empty_vehicle_item;
}
else
{
return m_vehicle_item_arr[idx];
}
}
std::vector<std::string>& gta_data_service::get_vehicle_class_arr()
{
return m_vehicle_class_arr;
}
std::vector<vehicle_item>& gta_data_service::get_vehicle_arr()
{
return m_vehicle_item_arr;
}
const ped_item& gta_data_service::find_ped_by_hash(Hash hash)
{
int idx = -1;
if (m_ped_hash_idx_map.count(hash))
{
idx = m_ped_hash_idx_map[hash];
}
if (idx == -1)
{
return empty_ped_item;
}
else
{
return m_ped_item_arr[idx];
}
}
std::vector<std::string>& gta_data_service::get_ped_type_arr()
{
return m_ped_type_arr;
}
std::vector<ped_item>& gta_data_service::get_ped_arr()
{
return m_ped_item_arr;
}
void gta_data_service::load_from_file(file file_to_load, file file_etag, std::string url, void(gta_data_service::* load_func)(), std::string data_name)
{
if (file_to_load.exists())
{
(this->*load_func)();
LOG(INFO) << "Data loaded: " + data_name;
}
g_thread_pool->push([this, file_to_load, file_etag, url, load_func, data_name]() {
for (int retry = 0; retry < 2; retry++)
{
bool ret = remote::update_binary(
url,
file_to_load.get_path(),
file_etag.get_path()
);
if (ret)
{
(this->*load_func)();
LOG(INFO) << "Data updated: " + data_name;
break;
}
else if (!m_vehicle_file.exists())
{
LOG(WARNING) << "Failed to download data: " + data_name;
}
}
});
}
void gta_data_service::load_vehicles()
{
m_vehicle_class_arr.clear();
m_vehicle_hash_idx_map.clear();
m_vehicle_item_arr.clear();
std::ifstream file(m_vehicle_file.get_path());
nlohmann::json all_vehicles;
try
{
file >> all_vehicles;
}
catch (const std::exception& ex)
{
LOG(WARNING) << "Failed to load vehicles.json:\n" << ex.what();
}
for (auto& item_json : all_vehicles)
{
if (
item_json["Hash"].is_null() ||
item_json["Name"].is_null() ||
!item_json["Bones"].is_array() ||
item_json["Bones"][0] == "stub"
)
{
continue;
}
auto item = vehicle_item(item_json);
m_vehicle_hash_idx_map[item_json["Hash"]] = (int)m_vehicle_item_arr.size();
m_vehicle_item_arr.push_back(item);
if (std::find(m_vehicle_class_arr.begin(), m_vehicle_class_arr.end(), item.clazz) == m_vehicle_class_arr.end())
{
m_vehicle_class_arr.push_back(item.clazz);
}
std::sort(m_vehicle_class_arr.begin(), m_vehicle_class_arr.end());
}
}
void gta_data_service::load_ped()
{
m_ped_type_arr.clear();
m_ped_hash_idx_map.clear();
m_ped_item_arr.clear();
std::ifstream file(m_ped_file.get_path());
nlohmann::json all_peds;
try
{
file >> all_peds;
}
catch (const std::exception& ex)
{
LOG(WARNING) << "Failed to load peds.json:\n" << ex.what();
}
for (auto& item_json : all_peds)
{
if (
item_json["Hash"].is_null() ||
item_json["Name"].is_null()
)
{
continue;
}
auto item = ped_item(item_json);
m_ped_hash_idx_map[item_json["Hash"]] = (int)m_ped_item_arr.size();
m_ped_item_arr.push_back(item);
if (std::find(m_ped_type_arr.begin(), m_ped_type_arr.end(), item.ped_type) == m_ped_type_arr.end())
{
m_ped_type_arr.push_back(item.ped_type);
}
std::sort(m_ped_type_arr.begin(), m_ped_type_arr.end());
}
}
}

View File

@ -0,0 +1,46 @@
#pragma once
#include "file_manager/file.hpp"
#include "vehicle_item.hpp"
#include "ped_item.hpp"
namespace big
{
class gta_data_service
{
file m_vehicle_file;
file m_vehicle_file_etag;
file m_ped_file;
file m_ped_file_etag;
std::vector<std::string> m_vehicle_class_arr;
std::map<Hash, int> m_vehicle_hash_idx_map;
std::vector<vehicle_item> m_vehicle_item_arr;
const vehicle_item empty_vehicle_item = vehicle_item();
std::vector<std::string> m_ped_type_arr;
std::map<Hash, int> m_ped_hash_idx_map;
std::vector<ped_item> m_ped_item_arr;
const ped_item empty_ped_item = ped_item();
public:
gta_data_service();
~gta_data_service();
const vehicle_item& find_vehicle_by_hash(Hash hash);
std::vector<std::string>& get_vehicle_class_arr();
std::vector<vehicle_item>& get_vehicle_arr();
const ped_item& find_ped_by_hash(Hash hash);
std::vector<std::string>& get_ped_type_arr();
std::vector<ped_item>& get_ped_arr();
private:
void load_from_file(file file_to_load, file file_etag, std::string url, void(gta_data_service::* load_func)(), std::string data_name);
void load_vehicles();
void load_ped();
};
inline gta_data_service* g_gta_data_service{};
}

View File

@ -0,0 +1,20 @@
#include "ped_item.hpp"
namespace big
{
ped_item::ped_item()
{
this->name = "";
this->ped_type = "";
this->hash = 0;
}
ped_item::ped_item(nlohmann::json& item_json)
{
this->name = item_json["Name"];
this->ped_type = item_json["Pedtype"];
std::transform(this->ped_type.begin(), this->ped_type.end(), this->ped_type.begin(), ::toupper);
this->hash = item_json["Hash"];
}
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "file_manager/file.hpp"
namespace big
{
class ped_item {
public:
ped_item();
ped_item(nlohmann::json& item_json);
std::string name;
std::string ped_type;
Hash hash;
};
}

View File

@ -0,0 +1,46 @@
#include "vehicle_item.hpp"
namespace big
{
vehicle_item::vehicle_item()
{
this->name = "";
this->display_name = "";
this->display_manufacturer = "";
this->clazz = "";
this->hash = 0;
}
vehicle_item::vehicle_item(nlohmann::json& item_json)
{
this->name = item_json["Name"];
this->display_name = item_json["Name"];
this->display_manufacturer = "";
this->clazz = "";
this->hash = item_json["Hash"];
if (!item_json["DisplayName"].is_null())
{
this->display_name = item_json["DisplayName"];
}
if (!item_json["ManufacturerDisplayName"].is_null())
{
this->display_manufacturer = item_json["ManufacturerDisplayName"];
}
else if (!item_json["Manufacturer"].is_null())
{
this->display_manufacturer = item_json["Manufacturer"];
}
if (!item_json["Class"].is_null())
{
this->clazz = item_json["Class"];
std::transform(this->clazz.begin(), this->clazz.end(), this->clazz.begin(), ::toupper);
if (this->clazz == "COMPACTS")
{
this->clazz = "COMPACT";
}
}
}
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "file_manager/file.hpp"
namespace big
{
class vehicle_item {
public:
vehicle_item();
vehicle_item(nlohmann::json& item_json);
std::string name;
std::string display_name;
std::string display_manufacturer;
std::string clazz;
Hash hash;
};
}