Skip to content

Code refractor #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: enhanced
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 45 additions & 50 deletions src/game/backend/SavedVehicles.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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))
{
Notifications::Show("Persist Car", "File does not exist.", NotificationType::Error);
return;
}
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;
std::ifstream file_stream(file);
nlohmann::json vehicle_json;

try
{
file_stream >> vehicle_json;
auto veh = SpawnFromJson(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)
if (veh)
{
LOG(WARNING) << e.what();
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);
}
Expand Down
51 changes: 28 additions & 23 deletions src/game/frontend/submenus/Vehicle/SavedVehicles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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([] {
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/gta/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion src/game/gta/Entity.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "types/rage/vector.hpp"
#include "types/script/types.hpp"

#define ENTITY_DEBUG

Expand Down Expand Up @@ -92,7 +93,7 @@ namespace YimMenu
bool IsMissionEntity();

// info
int GetModel();
Hash GetModel();

// position
rage::fvector3 GetPosition();
Expand Down