Added weapons to gta_data_service. (#365)
This commit is contained in:
@ -3,30 +3,38 @@
|
||||
#include "thread_pool.hpp"
|
||||
#include "gta_data_service.hpp"
|
||||
|
||||
#define EXIST_IN_ARRAY(arr, val) (std::find(std::begin(arr), std::end(arr), val) != std::end(arr))
|
||||
|
||||
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"))
|
||||
gta_data_service::gta_data_service()
|
||||
{
|
||||
const std::string url_prefix = "http://github-proxy.damon.sh/DurtyFree/gta-v-data-dumps/master/";
|
||||
|
||||
load_from_file(
|
||||
m_vehicle_file,
|
||||
m_vehicle_file_etag,
|
||||
"http://github-proxy.damon.sh/DurtyFree/gta-v-data-dumps/master/vehicles.json",
|
||||
"./lib/vehicles.json",
|
||||
"./lib/vehicles_etag.txt",
|
||||
url_prefix + "vehicles.json",
|
||||
>a_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",
|
||||
>a_data_service::load_ped,
|
||||
"./lib/peds.json",
|
||||
"./lib/peds_etag.txt",
|
||||
url_prefix + "peds.json",
|
||||
>a_data_service::load_peds,
|
||||
"Ped"
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -49,18 +57,16 @@ namespace big
|
||||
{
|
||||
return empty_vehicle_item;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_vehicle_item_arr[idx];
|
||||
}
|
||||
|
||||
return m_vehicle_item_arr[idx];
|
||||
}
|
||||
|
||||
std::vector<std::string>& gta_data_service::get_vehicle_class_arr()
|
||||
const 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()
|
||||
const std::vector<vehicle_item>& gta_data_service::get_vehicle_arr()
|
||||
{
|
||||
return m_vehicle_item_arr;
|
||||
}
|
||||
@ -79,33 +85,63 @@ namespace big
|
||||
{
|
||||
return empty_ped_item;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_ped_item_arr[idx];
|
||||
}
|
||||
|
||||
return m_ped_item_arr[idx];
|
||||
}
|
||||
|
||||
std::vector<std::string>& gta_data_service::get_ped_type_arr()
|
||||
const 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()
|
||||
const 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)
|
||||
const weapon_item& gta_data_service::find_weapon_by_hash(Hash hash)
|
||||
{
|
||||
int idx = -1;
|
||||
|
||||
if (m_weapon_hash_idx_map.count(hash))
|
||||
{
|
||||
idx = m_weapon_hash_idx_map[hash];
|
||||
}
|
||||
|
||||
if (idx == -1)
|
||||
{
|
||||
return empty_weapon_item;
|
||||
}
|
||||
|
||||
return m_weapon_item_arr[idx];
|
||||
}
|
||||
|
||||
const std::vector<std::string>& gta_data_service::get_weapon_type_arr()
|
||||
{
|
||||
return m_weapon_type_arr;
|
||||
}
|
||||
|
||||
const std::vector<weapon_item>& gta_data_service::get_weapon_arr()
|
||||
{
|
||||
return m_weapon_item_arr;
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
file file_to_load(g_file_manager->get_project_file(file_path));
|
||||
|
||||
if (file_to_load.exists())
|
||||
{
|
||||
(this->*load_func)();
|
||||
(this->*load_func)(file_to_load);
|
||||
LOG(INFO) << "Data loaded: " + data_name;
|
||||
}
|
||||
|
||||
g_thread_pool->push([this, file_to_load, file_etag, 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_etag(g_file_manager->get_project_file(etag_path));
|
||||
|
||||
for (int retry = 0; retry < 2; retry++)
|
||||
{
|
||||
bool ret = remote::update_binary(
|
||||
@ -116,11 +152,11 @@ namespace big
|
||||
|
||||
if (ret)
|
||||
{
|
||||
(this->*load_func)();
|
||||
(this->*load_func)(file_to_load);
|
||||
LOG(INFO) << "Data updated: " + data_name;
|
||||
break;
|
||||
}
|
||||
else if (!m_vehicle_file.exists())
|
||||
else if (!file_to_load.exists())
|
||||
{
|
||||
LOG(WARNING) << "Failed to download data: " + data_name;
|
||||
}
|
||||
@ -128,13 +164,13 @@ namespace big
|
||||
});
|
||||
}
|
||||
|
||||
void gta_data_service::load_vehicles()
|
||||
void 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(m_vehicle_file.get_path());
|
||||
std::ifstream file(file_to_load.get_path());
|
||||
nlohmann::json all_vehicles;
|
||||
|
||||
try
|
||||
@ -154,8 +190,7 @@ namespace big
|
||||
item_json["Name"].is_null() ||
|
||||
!item_json["Bones"].is_array() ||
|
||||
item_json["Bones"][0] == "stub"
|
||||
)
|
||||
{
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -175,13 +210,13 @@ namespace big
|
||||
}
|
||||
|
||||
|
||||
void gta_data_service::load_ped()
|
||||
void gta_data_service::load_peds(file file_to_load)
|
||||
{
|
||||
m_ped_type_arr.clear();
|
||||
m_ped_hash_idx_map.clear();
|
||||
m_ped_item_arr.clear();
|
||||
|
||||
std::ifstream file(m_ped_file.get_path());
|
||||
std::ifstream file(file_to_load.get_path());
|
||||
nlohmann::json all_peds;
|
||||
|
||||
try
|
||||
@ -198,8 +233,7 @@ namespace big
|
||||
if (
|
||||
item_json["Hash"].is_null() ||
|
||||
item_json["Name"].is_null()
|
||||
)
|
||||
{
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -217,4 +251,71 @@ namespace big
|
||||
std::sort(m_ped_type_arr.begin(), m_ped_type_arr.end());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void gta_data_service::load_weapons(file file_to_load)
|
||||
{
|
||||
m_weapon_type_arr.clear();
|
||||
m_weapon_hash_idx_map.clear();
|
||||
m_weapon_item_arr.clear();
|
||||
|
||||
std::ifstream file(file_to_load.get_path());
|
||||
nlohmann::json all_weapons;
|
||||
|
||||
try
|
||||
{
|
||||
file >> all_weapons;
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
LOG(WARNING) << "Failed to load weapons.json:\n" << ex.what();
|
||||
}
|
||||
|
||||
constexpr Hash hash_blacklist_arr[] = {
|
||||
RAGE_JOAAT("WEAPON_BIRD_CRAP"),
|
||||
RAGE_JOAAT("WEAPON_DIGISCANNER"),
|
||||
RAGE_JOAAT("WEAPON_GARBAGEBAG"),
|
||||
RAGE_JOAAT("WEAPON_GRENADELAUNCHER_SMOKE"),
|
||||
RAGE_JOAAT("WEAPON_HANDCUFFS"),
|
||||
RAGE_JOAAT("WEAPON_METALDETECTOR"),
|
||||
RAGE_JOAAT("GADGET_NIGHTVISION"),
|
||||
RAGE_JOAAT("GADGET_PARACHUTE"),
|
||||
RAGE_JOAAT("WEAPON_TRANQUILIZER"),
|
||||
RAGE_JOAAT("WEAPON_STINGER")
|
||||
};
|
||||
|
||||
for (auto& item_json : all_weapons)
|
||||
{
|
||||
if (
|
||||
item_json["Hash"].is_null() ||
|
||||
item_json["Name"].is_null() ||
|
||||
item_json["IsVehicleWeapon"]
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (EXIST_IN_ARRAY(hash_blacklist_arr, item_json["Hash"]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto item = weapon_item(item_json);
|
||||
|
||||
if (item.name == "Invalid" || item.name == "Unarmed" || item.weapon_type == "NULL")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
m_weapon_hash_idx_map[item_json["Hash"]] = (int)m_weapon_item_arr.size();
|
||||
|
||||
m_weapon_item_arr.push_back(item);
|
||||
|
||||
if (std::find(m_weapon_type_arr.begin(), m_weapon_type_arr.end(), item.weapon_type) == m_weapon_type_arr.end())
|
||||
{
|
||||
m_weapon_type_arr.push_back(item.weapon_type);
|
||||
}
|
||||
|
||||
std::sort(m_weapon_type_arr.begin(), m_weapon_type_arr.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,17 +2,13 @@
|
||||
#include "file_manager/file.hpp"
|
||||
#include "vehicle_item.hpp"
|
||||
#include "ped_item.hpp"
|
||||
#include "weapon_item.hpp"
|
||||
#include "gta/joaat.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;
|
||||
@ -23,23 +19,33 @@ namespace big
|
||||
std::vector<ped_item> m_ped_item_arr;
|
||||
const ped_item empty_ped_item = ped_item();
|
||||
|
||||
std::vector<std::string> m_weapon_type_arr;
|
||||
std::map<Hash, int> m_weapon_hash_idx_map;
|
||||
std::vector<weapon_item> m_weapon_item_arr;
|
||||
const weapon_item empty_weapon_item = weapon_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 std::vector<std::string>& get_vehicle_class_arr();
|
||||
const 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();
|
||||
const std::vector<std::string>& get_ped_type_arr();
|
||||
const std::vector<ped_item>& get_ped_arr();
|
||||
|
||||
const weapon_item& find_weapon_by_hash(Hash hash);
|
||||
const std::vector<std::string>& get_weapon_type_arr();
|
||||
const std::vector<weapon_item>& get_weapon_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_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_vehicles();
|
||||
void load_ped();
|
||||
void load_vehicles(file file_to_load);
|
||||
void load_peds(file file_to_load);
|
||||
void load_weapons(file file_to_load);
|
||||
};
|
||||
|
||||
inline gta_data_service* g_gta_data_service{};
|
||||
|
75
BigBaseV2/src/services/gta_data/weapon_item.cpp
Normal file
75
BigBaseV2/src/services/gta_data/weapon_item.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
#include "weapon_item.hpp"
|
||||
#include "gta/joaat.hpp"
|
||||
|
||||
namespace big
|
||||
{
|
||||
weapon_item::weapon_item()
|
||||
{
|
||||
this->name = "";
|
||||
this->throwable = false;
|
||||
this->weapon_type = "";
|
||||
|
||||
this->hash = 0;
|
||||
this->reward_hash = 0;
|
||||
this->reward_ammo_hash = 0;
|
||||
}
|
||||
weapon_item::weapon_item(nlohmann::json& item_json)
|
||||
{
|
||||
this->name = item_json["Name"];
|
||||
this->throwable = false;
|
||||
this->weapon_type = "NULL";
|
||||
|
||||
if (
|
||||
item_json.contains("TranslatedLabel") &&
|
||||
item_json["TranslatedLabel"].contains("English") &&
|
||||
!item_json["TranslatedLabel"]["English"].is_null()
|
||||
) {
|
||||
this->name = item_json["TranslatedLabel"]["English"];
|
||||
}
|
||||
|
||||
if (!item_json["Category"].is_null())
|
||||
{
|
||||
this->weapon_type = item_json["Category"];
|
||||
}
|
||||
|
||||
this->hash = item_json["Hash"];
|
||||
this->reward_hash = 0;
|
||||
this->reward_ammo_hash = 0;
|
||||
|
||||
const std::string reward_prefix = "REWARD_";
|
||||
bool is_gun = false;
|
||||
bool is_recharge = false;
|
||||
|
||||
if (item_json.contains("Flags"))
|
||||
{
|
||||
auto flags = item_json["Flags"];
|
||||
|
||||
for (auto& flag : flags)
|
||||
{
|
||||
if (flag == "Gun")
|
||||
{
|
||||
is_gun = true;
|
||||
}
|
||||
else if (flag == "DisplayRechargeTimeHUD")
|
||||
{
|
||||
is_recharge = true;
|
||||
}
|
||||
else if (flag == "Thrown")
|
||||
{
|
||||
this->throwable = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this->weapon_type == "GROUP_MELEE" || this->weapon_type == "GROUP_UNARMED" || is_gun)
|
||||
{
|
||||
this->reward_hash = rage::joaat((reward_prefix + std::string(item_json["Name"])).c_str());
|
||||
|
||||
if (is_gun && !is_recharge)
|
||||
{
|
||||
std::string weapon_id = std::string(item_json["Name"]).substr(7);
|
||||
this->reward_ammo_hash = rage::joaat((reward_prefix + "AMMO_" + weapon_id).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
BigBaseV2/src/services/gta_data/weapon_item.hpp
Normal file
19
BigBaseV2/src/services/gta_data/weapon_item.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "file_manager/file.hpp"
|
||||
|
||||
namespace big
|
||||
{
|
||||
class weapon_item {
|
||||
public:
|
||||
weapon_item();
|
||||
weapon_item(nlohmann::json& item_json);
|
||||
|
||||
std::string name;
|
||||
bool throwable;
|
||||
std::string weapon_type;
|
||||
|
||||
Hash hash;
|
||||
Hash reward_hash;
|
||||
Hash reward_ammo_hash;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user