refactor: cleaned up service directory structure (#300)

This commit is contained in:
Yimura
2022-06-30 00:11:54 +02:00
committed by GitHub
parent 39056979fa
commit dba617ae9f
54 changed files with 57 additions and 71 deletions

View File

@ -0,0 +1,121 @@
#include "api/remote.hpp"
#include "fiber_pool.hpp"
#include "file_manager.hpp"
#include "gui.hpp"
#include "thread_pool.hpp"
#include "util/entity.hpp"
#include "util/vehicle.hpp"
#include "vehicle_preview_service.hpp"
namespace big
{
vehicle_preview_service::vehicle_preview_service() :
m_vehicle_file(g_file_manager->get_project_file("./lib/vehicles.json"))
{
if (m_vehicle_file.exists())
this->load();
else
{
g_thread_pool->push([this]()
{
if (remote::download_binary("http://github-proxy.damon.sh/DurtyFree/gta-v-data-dumps/master/vehicles.json", m_vehicle_file.get_path()))
this->load();
else
LOG(WARNING) << "Failed to download vehicles.json data...";
});
}
g_vehicle_preview_service = this;
}
vehicle_preview_service::~vehicle_preview_service()
{
g_vehicle_preview_service = nullptr;
}
nlohmann::json& vehicle_preview_service::get_vehicle_list()
{
return m_all_vehicles;
}
void vehicle_preview_service::preview_loop()
{
if (m_running)
return;
m_running = true;
g_fiber_pool->queue_job([this]
{
while (g_running && m_running && g->spawn.preview_vehicle && g_gui.m_opened)
{
auto location = ENTITY::GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(self::ped, 2.5f, 2.5f, .5f);
if (m_current_veh == -1)
{
m_new_model = false;
location.z = -10.f;
m_current_veh = vehicle::spawn(m_model, location, 0.f, false);
ENTITY::FREEZE_ENTITY_POSITION(m_current_veh, true);
ENTITY::SET_ENTITY_ALPHA(m_current_veh, 0, 0);
ENTITY::SET_ENTITY_COLLISION(m_current_veh, false, false);
ENTITY::SET_CAN_CLIMB_ON_ENTITY(m_current_veh, false);
OBJECT::SET_OBJECT_ALLOW_LOW_LOD_BUOYANCY(m_current_veh, false);
}
else if (m_new_model)
{
entity::delete_entity(m_current_veh);
m_current_veh = -1;
}
else
{
if (const int alpha = ENTITY::GET_ENTITY_ALPHA(m_current_veh); alpha < 250)
ENTITY::SET_ENTITY_ALPHA(m_current_veh, std::min<int>(255, alpha + 10), 0);
ENTITY::SET_ENTITY_HEADING(m_current_veh, m_heading);
ENTITY::SET_ENTITY_COORDS(m_current_veh, location.x, location.y, location.z, 0, 0, 0, 0);
}
if (m_heading += 0.5f; m_heading > 359) m_heading = 0;
script::get_current()->yield();
}
entity::delete_entity(m_current_veh);
m_current_veh = -1;
m_running = false;
});
}
void vehicle_preview_service::set_preview_vehicle(const nlohmann::json& item)
{
if (m_model != item["Name"])
{
m_model = item["Name"];
m_new_model = true;
}
if (!m_running)
g_thread_pool->push([this] { preview_loop(); });
}
void vehicle_preview_service::stop_preview()
{
m_running = false;
}
void vehicle_preview_service::load()
{
std::ifstream file(m_vehicle_file.get_path());
try
{
file >> m_all_vehicles;
}
catch (const std::exception& ex)
{
LOG(WARNING) << "Failed to load vehicles.json:\n" << ex.what();
}
}
}

View File

@ -0,0 +1,37 @@
#pragma once
#include "file_manager/file.hpp"
namespace big
{
class vehicle_preview_service
{
file m_vehicle_file;
std::condition_variable m_cond;
std::mutex m_mutex;
nlohmann::json m_all_vehicles;
Vehicle m_current_veh = -1;
std::string m_model;
bool m_new_model = false;
float m_heading = 0.f;
bool m_running = false;
public:
vehicle_preview_service();
~vehicle_preview_service();
nlohmann::json& get_vehicle_list();
void preview_loop();
void set_preview_vehicle(const nlohmann::json& item);
void stop_preview();
private:
void load();
};
inline vehicle_preview_service* g_vehicle_preview_service{};
}