Code refractor (#622)

* refractor fiberpool code

* fix model saved is not correct

- the model value returned by native is 2627665880 (unsigned)
  same as Joaat("mp_f_freemode_01")
  but when converted to int it is -1667301416 (signed).
  this value will get saved in jsons instead of correct
  one and will also cause issue when applying old jsons.

* Update VehicleEditor.cpp

* display current plate text

* fix teleport in water surrounded land
This commit is contained in:
lonelybud
2025-12-17 20:12:46 +05:30
committed by GitHub
parent f372a42a32
commit 75d6a798e0
6 changed files with 102 additions and 95 deletions
+48 -53
View File
@@ -1,6 +1,5 @@
#include "SavedVehicles.hpp"
#include "core/backend/FiberPool.hpp"
#include "core/backend/ScriptMgr.hpp"
#include "core/frontend/Notifications.hpp"
#include "game/gta/VehicleModel.hpp"
@@ -18,21 +17,19 @@ namespace YimMenu
void SavedVehicles::RefreshList(std::string folderName, std::vector<std::string>& folders, std::vector<std::string>& files)
{
FiberPool::Push([folderName, &folders, &files] {
folders.clear();
folders.clear();
const auto file_path = CheckFolder();
for (const auto& directory_entry : std::filesystem::directory_iterator(file_path.Path()))
if (directory_entry.is_directory())
folders.push_back(directory_entry.path().filename().generic_string());
const auto file_path = CheckFolder();
for (const auto& directory_entry : std::filesystem::directory_iterator(file_path.Path()))
if (directory_entry.is_directory())
folders.push_back(directory_entry.path().filename().generic_string());
files.clear();
files.clear();
const auto file_path2 = CheckFolder(folderName);
for (const auto& directory_entry : std::filesystem::directory_iterator(file_path2.Path()))
if (directory_entry.path().extension() == ".json")
files.push_back(directory_entry.path().filename().generic_string());
});
const auto file_path2 = CheckFolder(folderName);
for (const auto& directory_entry : std::filesystem::directory_iterator(file_path2.Path()))
if (directory_entry.path().extension() == ".json")
files.push_back(directory_entry.path().filename().generic_string());
}
nlohmann::json SavedVehicles::GetJson(Vehicle veh)
@@ -132,55 +129,53 @@ namespace YimMenu
void SavedVehicles::Save(std::string folderName, std::string fileName)
{
FiberPool::Push([folderName, fileName] {
if (auto veh = Self::GetVehicle(); veh && veh.IsValid())
{
const auto file = SavedVehicles::CheckFolder(folderName).GetFile(fileName);
std::ofstream file_stream(file.Path(), std::ios::out | std::ios::trunc);
file_stream << SavedVehicles::GetJson(veh).dump(4);
file_stream.close();
}
else
Notifications::Show("Persist Car", "Tried to save a vehicle which does not exist", NotificationType::Warning);
});
if (auto veh = Self::GetVehicle(); veh && veh.IsValid())
{
const auto file = SavedVehicles::CheckFolder(folderName).GetFile(fileName);
std::ofstream file_stream(file.Path(), std::ios::out | std::ios::trunc);
file_stream << SavedVehicles::GetJson(veh).dump(4);
file_stream.close();
}
else
Notifications::Show("Persist Car", "Tried to save a vehicle which does not exist", NotificationType::Warning);
}
void SavedVehicles::Load(std::string folderName, std::string fileName, bool spawnInside)
{
if (!fileName.empty())
FiberPool::Push([folderName, fileName, spawnInside] {
const auto file = CheckFolder(folderName).GetFile(fileName).Path();
{
const auto file = CheckFolder(folderName).GetFile(fileName).Path();
if (!std::filesystem::exists(file))
if (!std::filesystem::exists(file))
{
Notifications::Show("Persist Car", "File does not exist.", NotificationType::Error);
return;
}
std::ifstream file_stream(file);
nlohmann::json vehicle_json;
try
{
file_stream >> vehicle_json;
auto veh = SpawnFromJson(vehicle_json);
if (veh)
{
Notifications::Show("Persist Car", "File does not exist.", NotificationType::Error);
return;
if (spawnInside)
Self::GetPed().SetInVehicle(veh.GetHandle());
Notifications::Show("Persist Car", std::format("Spawned {}", fileName), NotificationType::Success);
}
else
Notifications::Show("Persist Car", std::format("Unable to spawn {}", fileName), NotificationType::Error);
}
catch (std::exception& e)
{
LOG(WARNING) << e.what();
}
std::ifstream file_stream(file);
nlohmann::json vehicle_json;
try
{
file_stream >> vehicle_json;
auto veh = SpawnFromJson(vehicle_json);
if (veh)
{
if (spawnInside)
Self::GetPed().SetInVehicle(veh.GetHandle());
Notifications::Show("Persist Car", std::format("Spawned {}", fileName), NotificationType::Success);
}
else
Notifications::Show("Persist Car", std::format("Unable to spawn {}", fileName), NotificationType::Error);
}
catch (std::exception& e)
{
LOG(WARNING) << e.what();
}
file_stream.close();
});
file_stream.close();
}
else
Notifications::Show("Persist Car", "Select a file first", NotificationType::Warning);
}
+15 -9
View File
@@ -14,22 +14,18 @@ namespace YimMenu::Features
constexpr int max_attempts = 20;
float ground_z = vec.z;
int current_attempts = 0;
bool found_ground = false;
do
{
STREAMING::REQUEST_COLLISION_AT_COORD(vec.x, vec.y, vec.z);
float water_height;
if (WATER::GET_WATER_HEIGHT(vec.x, vec.y, vec.z, &water_height))
{
vec.z = water_height;
return;
}
// find ground first and if found set z to new value and break
if (MISC::GET_GROUND_Z_FOR_3D_COORD(vec.x, vec.y, max_ground_check, &ground_z, false, false))
{
vec.z = ground_z + 1.0f;
return;
found_ground = true;
break;
}
if (current_attempts % 3 == 0)
@@ -40,8 +36,18 @@ namespace YimMenu::Features
++current_attempts;
ScriptMgr::Yield();
} while (current_attempts < max_attempts);
// whether ground was found or not check for water height
float water_height;
if (WATER::GET_WATER_HEIGHT(vec.x, vec.y, vec.z, &water_height))
{
vec.z = water_height;
return;
}
vec.z = PATHFIND::GET_APPROX_HEIGHT_FOR_POINT(vec.x, vec.y); // fallback value
// fallback value
if (!found_ground)
vec.z = PATHFIND::GET_APPROX_HEIGHT_FOR_POINT(vec.x, vec.y);
}
class TpToWaypoint : public Command
@@ -30,32 +30,29 @@ namespace YimMenu::Submenus
return;
if (ImGui::Button("Save"))
{
std::string fileName = vehicle_file_name_input;
strcpy(vehicle_file_name_input, "");
FiberPool::Push([saveToNewFolder] {
std::string fileName = vehicle_file_name_input;
strcpy(vehicle_file_name_input, "");
if (!TrimString(fileName).size())
{
Notifications::Show("Saved Vehicles", "Filename empty!", NotificationType::Warning);
return;
}
if (!TrimString(fileName).size())
{
Notifications::Show("Saved Vehicles", "Filename empty!", NotificationType::Warning);
return;
}
ReplaceString(fileName, ".", ""); // filename say "bob.." will throw relative path error from Folder::GetFile
fileName += ".json";
ReplaceString(fileName, ".", ""); // filename say "bob.." will throw relative path error from Folder::GetFile
fileName += ".json";
SavedVehicles::Save(saveToNewFolder ? newFolder : folder, fileName);
SavedVehicles::Save(saveToNewFolder ? newFolder : folder, fileName);
if (saveToNewFolder)
{
folder = newFolder; // set current folder to newly created folder
strcpy(newFolder, "");
}
if (saveToNewFolder)
{
folder = newFolder; // set current folder to newly created folder
strcpy(newFolder, "");
}
FiberPool::Push([] {
ScriptMgr::Yield(1000ms); // wait for files to save and then refresh
SavedVehicles::RefreshList(folder, folders, files);
});
}
ImGui::SameLine();
if (ImGui::Button("Populate Name"))
FiberPool::Push([] {
@@ -65,7 +62,9 @@ namespace YimMenu::Submenus
};
if (ImGui::Button("Refresh List"))
SavedVehicles::RefreshList(folder, folders, files);
FiberPool::Push([] {
SavedVehicles::RefreshList(folder, folders, files);
});
ImGui::SetNextItemWidth(300.f);
auto folder_display = folder.empty() ? "Root" : folder.c_str();
@@ -74,14 +73,18 @@ namespace YimMenu::Submenus
if (ImGui::Selectable("Root", folder == ""))
{
folder.clear();
SavedVehicles::RefreshList(folder, folders, files);
FiberPool::Push([] {
SavedVehicles::RefreshList(folder, folders, files);
});
}
for (std::string folder_name : folders)
if (ImGui::Selectable(folder_name.c_str(), folder == folder_name))
{
folder = folder_name;
SavedVehicles::RefreshList(folder, folders, files);
FiberPool::Push([] {
SavedVehicles::RefreshList(folder, folders, files);
});
}
ImGui::EndCombo();
@@ -144,7 +147,9 @@ namespace YimMenu::Submenus
ImGui::Spacing();
if (ImGui::Button("Yes"))
{
SavedVehicles::Load(folder, file, spawnInsideSavedVehicle.GetState());
FiberPool::Push([] {
SavedVehicles::Load(folder, file, spawnInsideSavedVehicle.GetState());
});
open_modal = false;
ImGui::CloseCurrentPopup();
}
@@ -17,6 +17,7 @@ namespace YimMenu::Submenus
static bool preparingVehicle = false;
static bool isBennys = false;
static int selected_slot = -1;
static char plate[9] = "";
static std::map<int, int32_t> owned_mods{};
static std::map<int, std::string> slot_display_names{};
@@ -38,6 +39,10 @@ namespace YimMenu::Submenus
auto model = Self::GetVehicle().GetModel();
front_wheel_stock_mod = -1;
rear_wheel_stock_mod = -1;
vehName = Self::GetVehicle().GetFullName();
owned_mods = Self::GetVehicle().GetOwnedMods();
isBennys = VehicleModel::IsBennys(currentVeh);
strcpy(plate, Self::GetVehicle().GetPlateText().c_str());
if (!HUD::HAS_THIS_ADDITIONAL_TEXT_LOADED("MOD_MNU", 10))
{
@@ -46,10 +51,6 @@ namespace YimMenu::Submenus
ScriptMgr::Yield();
}
vehName = Self::GetVehicle().GetFullName();
owned_mods = Self::GetVehicle().GetOwnedMods();
isBennys = VehicleModel::IsBennys(currentVeh);
VEHICLE::SET_VEHICLE_MOD_KIT(currentVeh, 0);
std::map<int, std::string> tmp_slot_display_names;
@@ -162,15 +163,15 @@ namespace YimMenu::Submenus
return;
}
if (preparingVehicle)
return;
if (currentVeh != Self::GetVehicle().GetHandle())
{
prepareVehicle();
return;
}
if (preparingVehicle)
return;
ImGui::BeginGroup();
{
ImGui::Text("%s", vehName.c_str());
@@ -182,7 +183,6 @@ namespace YimMenu::Submenus
ImGui::Spacing();
{
static char plate[9];
ImGui::SetNextItemWidth(150);
ImGui::InputTextWithHint("##plate", "Plate Number", plate, sizeof(plate), ImGuiInputTextFlags_None);
ImGui::SameLine();
+1 -1
View File
@@ -122,7 +122,7 @@ namespace YimMenu
return ENTITY::IS_ENTITY_A_MISSION_ENTITY(GetHandle()); // TODO: detect more mission states
}
int Entity::GetModel()
Hash Entity::GetModel()
{
ENTITY_ASSERT_VALID();
return ENTITY::GET_ENTITY_MODEL(GetHandle());
+2 -1
View File
@@ -1,5 +1,6 @@
#pragma once
#include "types/rage/vector.hpp"
#include "types/script/types.hpp"
#define ENTITY_DEBUG
@@ -92,7 +93,7 @@ namespace YimMenu
bool IsMissionEntity();
// info
int GetModel();
Hash GetModel();
// position
rage::fvector3 GetPosition();