refactor!: File Manager (#1633)

* feat(vscode): added launch.json to dbg
* feat(file_manager): Make use of a global instance
* feat(file_manager): Allow for file and folder instances to be empty
* refactor(GlobalsService): Update code of global service (untested)
This commit is contained in:
Andreas Maerten 2023-07-08 17:54:59 +02:00 committed by GitHub
parent 6f40a38045
commit f13eab76a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 185 additions and 208 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
# IDE
.vs/
.vscode/
.vscode/*
!.vscode/launch.json
.cache/
# output directory

14
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "GTA5.exe Attach",
"type": "cppvsdbg",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}

View File

@ -12,7 +12,7 @@ namespace big
void menu_settings::init(const file& save_file)
{
m_running = true;
m_save_file = std::make_unique<file>(save_file.get_path());
m_save_file = save_file;
load();
g_thread_pool->push([this] {
@ -36,13 +36,13 @@ namespace big
{
m_default_options = *this;
std::ifstream file(m_save_file->get_path());
std::ifstream file(m_save_file.get_path());
if (!m_save_file->exists())
if (!m_save_file.exists())
{
write_default_config();
file.open(m_save_file->get_path());
file.open(m_save_file.get_path());
}
try
@ -85,7 +85,7 @@ namespace big
bool menu_settings::write_default_config()
{
std::ofstream file(m_save_file->get_path(), std::ios::out | std::ios::trunc);
std::ofstream file(m_save_file.get_path(), std::ios::out | std::ios::trunc);
file << m_default_options.dump(4);
file.close();
@ -124,7 +124,7 @@ namespace big
bool menu_settings::save()
{
std::ofstream file(m_save_file->get_path(), std::ios::out | std::ios::trunc);
std::ofstream file(m_save_file.get_path(), std::ios::out | std::ios::trunc);
nlohmann::json j = *this;
file << j.dump(4);
file.close();

View File

@ -58,7 +58,7 @@ namespace big
private:
bool m_running;
std::unique_ptr<file> m_save_file;
file m_save_file;
nlohmann::json m_default_options;
nlohmann::json m_options;

55
src/file_manager.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "file_manager.hpp"
namespace big
{
bool file_manager::init(const std::filesystem::path& base_dir)
{
m_base_dir = base_dir;
file_manager::ensure_folder_exists(m_base_dir);
return true;
}
const std::filesystem::path& file_manager::get_base_dir()
{
return m_base_dir;
}
file file_manager::get_project_file(std::filesystem::path file_path)
{
if (file_path.is_absolute())
throw std::exception("Project files are relative to the BaseDir, don't use absolute paths!");
return file_manager::ensure_file_can_be_created(m_base_dir / file_path);
}
folder file_manager::get_project_folder(std::filesystem::path folder_path)
{
if (folder_path.is_absolute())
throw std::exception("Project folders are relative to the BaseDir, don't use absolute paths!");
return file_manager::ensure_folder_exists(m_base_dir / folder_path);
}
std::filesystem::path file_manager::ensure_file_can_be_created(const std::filesystem::path file_path)
{
file_manager::ensure_folder_exists(file_path.parent_path());
return file_path;
}
std::filesystem::path file_manager::ensure_folder_exists(const std::filesystem::path folder_path)
{
bool create_path = !std::filesystem::exists(folder_path);
if (!create_path && !std::filesystem::is_directory(folder_path))
{
std::filesystem::remove(folder_path);
create_path = true;
}
if (create_path)
std::filesystem::create_directory(folder_path);
return folder_path;
}
}

View File

@ -5,66 +5,30 @@
namespace big
{
class file_manager;
inline file_manager* g_file_manager{};
class file_manager final
{
public:
file_manager(std::filesystem::path base_dir) :
m_base_dir(base_dir)
{
file_manager::ensure_folder_exists(m_base_dir);
file_manager() = default;
virtual ~file_manager() = default;
file_manager(const file_manager&) = delete;
file_manager(file_manager&&) noexcept = delete;
file_manager& operator=(const file_manager&) = delete;
file_manager& operator=(file_manager&&) noexcept = delete;
g_file_manager = this;
}
~file_manager()
{
g_file_manager = nullptr;
}
bool init(const std::filesystem::path& base_dir);
std::filesystem::path get_base_dir()
{
return m_base_dir;
}
const std::filesystem::path& get_base_dir();
file get_project_file(std::filesystem::path file_path)
{
if (file_path.is_absolute())
throw std::exception("Project files are relative to the BaseDir, don't use absolute paths!");
file get_project_file(std::filesystem::path file_path);
return file(this, file_path);
}
folder get_project_folder(std::filesystem::path folder_path)
{
if (folder_path.is_absolute())
throw std::exception("Project folders are relative to the BaseDir, don't use absolute paths!");
folder get_project_folder(std::filesystem::path folder_path);
return folder(this, folder_path);
}
static std::filesystem::path ensure_file_can_be_created(const std::filesystem::path file_path)
{
file_manager::ensure_folder_exists(file_path.parent_path());
return file_path;
}
static std::filesystem::path ensure_folder_exists(const std::filesystem::path folder_path)
{
bool create_path = !std::filesystem::exists(folder_path);
if (!create_path && !std::filesystem::is_directory(folder_path))
{
std::filesystem::remove(folder_path);
create_path = true;
}
if (create_path)
std::filesystem::create_directory(folder_path);
return folder_path;
}
static std::filesystem::path ensure_file_can_be_created(const std::filesystem::path file_path);
static std::filesystem::path ensure_folder_exists(const std::filesystem::path folder_path);
private:
std::filesystem::path m_base_dir;
};
inline auto g_file_manager = file_manager();
}

View File

@ -5,16 +5,24 @@
namespace big
{
file::file(file_manager* file_manager, std::filesystem::path file_path) :
file(file_manager->get_base_dir() / file_path)
file::file(const std::filesystem::path& file_path) :
m_file_path(file_path)
{
m_is_project_file = true;
}
file::file(std::filesystem::path file_path) :
m_file_path(file_manager::ensure_file_can_be_created(file_path)),
m_is_project_file(false)
void file::operator=(const file& other)
{
m_file_path = other.m_file_path;
}
file::operator std::filesystem::path()
{
return m_file_path;
}
file::operator std::filesystem::path&()
{
return m_file_path;
}
bool file::exists() const

View File

@ -7,16 +7,16 @@ namespace big
class file
{
public:
file(std::filesystem::path file_path);
file(const std::filesystem::path& file_path = "");
void operator=(const file& other);
operator std::filesystem::path();
operator std::filesystem::path&();
file copy(std::filesystem::path new_path);
file copy(const std::filesystem::path& new_path);
bool exists() const;
const std::filesystem::path get_path() const;
file move(std::filesystem::path new_path);
protected:
file(file_manager* file_manager, std::filesystem::path file_path);
private:
friend class file_manager;

View File

@ -4,15 +4,8 @@
namespace big
{
folder::folder(file_manager* file_manager, std::filesystem::path file_path) :
folder(file_manager->get_base_dir() / file_path)
{
m_file_manager = file_manager;
m_is_project_file = true;
}
folder::folder(std::filesystem::path folder_path) :
m_folder_path(file_manager::ensure_folder_exists(folder_path))
folder::folder(const std::filesystem::path& folder_path) :
m_folder_path(folder_path)
{
}

View File

@ -8,14 +8,11 @@ namespace big
class folder
{
public:
folder(std::filesystem::path folder_path);
folder(const std::filesystem::path& folder_path = "");
file get_file(std::filesystem::path file_path) const;
const std::filesystem::path get_path() const;
protected:
folder(file_manager* file_manager, std::filesystem::path file_path);
private:
friend class file_manager;
file_manager* m_file_manager;

View File

@ -91,9 +91,9 @@ class IDirectSoundCaptureBuffer
virtual HRESULT Start(int flags)
{
if (big::g_file_manager->get_project_file("./audio.wav").exists())
if (big::g_file_manager.get_project_file("./audio.wav").exists())
{
std::ifstream wave_stream(big::g_file_manager->get_project_file("./audio.wav").get_path(), std::ios::in | std::ios::binary);
std::ifstream wave_stream(big::g_file_manager.get_project_file("./audio.wav").get_path(), std::ios::in | std::ios::binary);
// https://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
int header_size = 0;

View File

@ -55,9 +55,9 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
std::filesystem::path base_dir = std::getenv("appdata");
base_dir /= "YimMenu";
do_migration(base_dir);
auto file_manager_instance = std::make_unique<file_manager>(base_dir);
g_file_manager.init(base_dir);
auto logger_instance = std::make_unique<logger>("YimMenu", file_manager_instance->get_project_file("./cout.log"));
auto logger_instance = std::make_unique<logger>("YimMenu", g_file_manager.get_project_file("./cout.log"));
EnableMenuItem(GetSystemMenu(GetConsoleWindow(), 0), SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
@ -69,7 +69,7 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
auto thread_pool_instance = std::make_unique<thread_pool>();
LOG(INFO) << "Thread pool initialized.";
g.init(file_manager_instance->get_project_file("./settings.json"));
g.init(g_file_manager.get_project_file("./settings.json"));
LOG(INFO) << "Settings Loaded.";
auto pointers_instance = std::make_unique<pointers>();
@ -138,7 +138,7 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
auto native_hooks_instance = std::make_unique<native_hooks>();
LOG(INFO) << "Dynamic native hooker initialized.";
auto lua_manager_instance = std::make_unique<lua_manager>(g_file_manager->get_project_folder("scripts"));
auto lua_manager_instance = std::make_unique<lua_manager>(g_file_manager.get_project_folder("scripts"));
LOG(INFO) << "Lua manager initialized.";
g_running = true;
@ -229,8 +229,6 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID)
logger_instance->destroy();
logger_instance.reset();
file_manager_instance.reset();
CloseHandle(g_main_thread);
FreeLibraryAndExitThread(g_hmodule, 0);
},

View File

@ -1419,8 +1419,8 @@ namespace big
}
pointers::pointers() :
m_gta_pointers_cache(g_file_manager->get_project_file("./cache/gta_pointers.bin")),
m_sc_pointers_cache(g_file_manager->get_project_file("./cache/sc_pointers.bin"))
m_gta_pointers_cache(g_file_manager.get_project_file("./cache/gta_pointers.bin")),
m_sc_pointers_cache(g_file_manager.get_project_file("./cache/sc_pointers.bin"))
{
g_pointers = this;

View File

@ -24,7 +24,7 @@ namespace big
}
m_d3d_device->GetImmediateContext(&m_d3d_device_context);
auto file_path = g_file_manager->get_project_file("./imgui.ini").get_path();
auto file_path = g_file_manager.get_project_file("./imgui.ini").get_path();
ImGuiContext* ctx = ImGui::CreateContext();

View File

@ -86,7 +86,7 @@ namespace big
big::folder creator_storage_service::check_jobs_folder()
{
const auto folder = g_file_manager->get_project_folder("./jobs");
const auto folder = g_file_manager.get_project_folder("./jobs");
return folder;
}

View File

@ -4,37 +4,30 @@
namespace big
{
globals_service::globals_service()
{
g_globals_service = this;
}
globals_service::globals_service() :
m_globals_file(g_file_manager.get_project_file("./globals.json"))
{ }
globals_service::~globals_service()
{
g_globals_service = nullptr;
m_running = false;
this->save();
save();
}
void globals_service::build(nlohmann::json& data)
void globals_service::build()
{
m_globals.clear();
for (auto& offset : data)
m_globals.push_back(global(offset));
for (auto& global : m_globals)
global.build_cache();
}
bool globals_service::load()
{
std::string path = std::getenv("appdata");
path += this->file_location;
m_globals_file;
std::ifstream file(path);
std::ifstream file(m_globals_file.get_path(), std::ios::binary);
if (!file.is_open())
return false;
@ -42,9 +35,10 @@ namespace big
try
{
nlohmann::json j;
j << file;
file >> j;
m_globals = j.get<global_vec>();
this->build(j);
build();
}
catch (const std::exception&)
{
@ -66,13 +60,9 @@ namespace big
void globals_service::save()
{
nlohmann::json j = nlohmann::json::array();
for (auto& global : m_globals)
j.push_back(global.to_json());
nlohmann::json j = m_globals;
std::string path = std::getenv("appdata");
path += this->file_location;
std::ofstream file(path, std::ios::out | std::ios::trunc);
std::ofstream file(m_globals_file.get_path(), std::ios::binary | std::ios::out | std::ios::trunc);
try
{

View File

@ -5,14 +5,10 @@ namespace big
{
struct global_offset
{
global_offset(nlohmann::json data)
{
m_offset = data["offset"];
if (data.contains("size"))
m_size = data["size"];
}
int m_offset = 0;
int m_size = 0;
global_offset() = default;
global_offset(int offset, int size = 0)
{
m_offset = offset;
@ -25,22 +21,8 @@ namespace big
{
return m_size ? internal_cache.at(m_offset, m_size) : internal_cache.at(m_offset);
}
nlohmann::json to_json()
{
nlohmann::json j;
j["offset"] = m_offset;
if (m_size)
j["size"] = m_size;
return j;
}
private:
int m_offset = 0;
int m_size = 0;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(global_offset, m_offset, m_size)
struct global
{
@ -50,30 +32,19 @@ namespace big
std::vector<global_offset> m_offsets;
int m_value;
global(nlohmann::json data)
global() = default;
global(const char* name, const int base_address, const bool freeze, const int (*offsets)[2], int offset_count) :
m_name(name),
m_base_address(base_address),
m_freeze(freeze)
{
m_internal_id = ++m_instance_count;
m_base_address = data["base_address"];
m_freeze = data["freeze"];
m_name = data["name"];
m_value = data["value"];
for (auto& offset : data["offsets"])
m_offsets.push_back(global_offset(offset));
}
global(const char* name, const int base_address, const bool freeze, const int (*offsets)[2], int offset_count)
{
m_internal_id = ++m_instance_count;
m_base_address = base_address;
m_freeze = freeze;
m_name = std::string(name);
m_value = 0;
for (int i = 0; i < offset_count; i++)
m_offsets.push_back(global_offset(offsets[i][0], offsets[i][1]));
for (int i = 0; i < offset_count; ++i)
{
const auto offset = offsets[i];
m_offsets.push_back({ offset[0], offset[1] });
}
}
void build_cache()
@ -105,22 +76,6 @@ namespace big
this->write();
}
nlohmann::json to_json()
{
nlohmann::json j;
j["base_address"] = m_base_address;
j["freeze"] = m_freeze;
j["name"] = m_name;
j["value"] = m_value;
j["offsets"] = nlohmann::json::array();
for (auto& offset : m_offsets)
j["offsets"].push_back(offset.to_json());
return j;
}
void write()
{
*m_internal_addr = m_value;
@ -132,10 +87,12 @@ namespace big
int m_internal_id;
int* m_internal_addr;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(global, m_base_address, m_freeze, m_offsets, m_name, m_value)
using global_vec = std::vector<global>;
class globals_service
{
const char* file_location = "\\BigBaseV2\\globals.json";
file m_globals_file;
public:
globals_service();
@ -145,13 +102,13 @@ namespace big
void loop();
void save();
std::vector<global> m_globals;
global_vec m_globals;
bool m_running = false;
;
private:
void build(nlohmann::json& data);
void build();
};
inline globals_service* g_globals_service{};
inline auto g_globals_service = globals_service();
}

View File

@ -30,8 +30,8 @@ namespace big
}
gta_data_service::gta_data_service() :
m_peds_cache(g_file_manager->get_project_file("./cache/peds.bin"), 5),
m_vehicles_cache(g_file_manager->get_project_file("./cache/vehicles.bin"), 4),
m_peds_cache(g_file_manager.get_project_file("./cache/peds.bin"), 5),
m_vehicles_cache(g_file_manager.get_project_file("./cache/vehicles.bin"), 4),
m_update_state(eGtaDataUpdateState::IDLE)
{
if (!is_cache_up_to_date())
@ -161,7 +161,7 @@ namespace big
m_peds_cache.load();
m_vehicles_cache.load();
auto weapons_file = g_file_manager->get_project_file("./cache/weapons.json");
auto weapons_file = g_file_manager.get_project_file("./cache/weapons.json");
if (weapons_file.exists())
{
std::ifstream file(weapons_file.get_path());
@ -593,7 +593,7 @@ namespace big
m_weapons_cache.weapon_components.insert({weapon_component.m_name, weapon_component});
}
auto weapons_file = big::g_file_manager->get_project_file("./cache/weapons.json");
auto weapons_file = g_file_manager.get_project_file("./cache/weapons.json");
std::ofstream file(weapons_file.get_path());
try
{

View File

@ -24,7 +24,7 @@ namespace big
std::filesystem::path locals_service::get_path()
{
return g_file_manager->get_project_file("locals.json").get_path();
return g_file_manager.get_project_file("locals.json").get_path();
}
bool locals_service::load()

View File

@ -11,7 +11,7 @@ namespace big
g.player_db.update_player_online_states);
player_database_service::player_database_service() :
m_file_path(g_file_manager->get_project_file("./players.json").get_path())
m_file_path(g_file_manager.get_project_file("./players.json").get_path())
{
load();
g_player_database_service = this;

View File

@ -5,7 +5,7 @@ namespace big
std::filesystem::path squad_spawner::get_file_path()
{
return g_file_manager->get_project_folder("squad_spawner").get_path();
return g_file_manager.get_project_folder("squad_spawner").get_path();
}
bool squad_spawner::fetch_squads()

View File

@ -17,7 +17,7 @@ namespace big
void translation_service::init()
{
m_translation_directory = std::make_unique<folder>(g_file_manager->get_project_folder("./translations").get_path());
m_translation_directory = std::make_unique<folder>(g_file_manager.get_project_folder("./translations").get_path());
bool loaded_remote_index = false;
for (size_t i = 0; i < 5 && !loaded_remote_index; i++)

View File

@ -12,7 +12,7 @@
namespace big
{
tunables_service::tunables_service() :
m_cache_file(g_file_manager->get_project_file("./cache/tunables.bin"), 1)
m_cache_file(g_file_manager.get_project_file("./cache/tunables.bin"), 1)
{
m_cache_file.load();

View File

@ -5,7 +5,7 @@
namespace big
{
handling_service::handling_service() :
m_profiles_folder(g_file_manager->get_project_folder("./handling_profiles"))
m_profiles_folder(g_file_manager.get_project_folder("./handling_profiles"))
{
g_handling_service = this;

View File

@ -514,7 +514,7 @@ namespace big
big::folder persist_car_service::check_vehicle_folder()
{
const auto folder = g_file_manager->get_project_folder("./saved_json_vehicles");
const auto folder = g_file_manager.get_project_folder("./saved_json_vehicles");
return folder;
}

View File

@ -21,7 +21,7 @@ namespace big
void xml_vehicles_service::fetch_xml_files()
{
auto folder_path = g_file_manager->get_project_folder("xml_vehicles").get_path();
auto folder_path = g_file_manager.get_project_folder("xml_vehicles").get_path();
try
{

View File

@ -55,14 +55,14 @@ namespace big::animations
all_anims.clear();
all_dicts.clear();
if (!std::filesystem::exists(g_file_manager->get_project_file("animDictsCompact.json").get_path()))
if (!std::filesystem::exists(g_file_manager.get_project_file("animDictsCompact.json").get_path()))
{
LOG(INFO) << "Animations file is not in directory. https://raw.githubusercontent.com/DurtyFree/gta-v-data-dumps/master/animDictsCompact.json";
g_notification_service->push_warning("Animations", "Please download the appropriate animations json and put it in the mod directory.");
return;
}
auto path = g_file_manager->get_project_file("animDictsCompact.json").get_path();
auto path = g_file_manager.get_project_file("animDictsCompact.json").get_path();
try
{

View File

@ -70,7 +70,7 @@ namespace big::spam
inline void log_chat(char* msg, player_ptr player, bool is_spam)
{
std::ofstream spam_log(g_file_manager->get_project_file(is_spam ? "./spam.log" : "./chat.log").get_path(), std::ios::app);
std::ofstream spam_log(g_file_manager.get_project_file(is_spam ? "./spam.log" : "./chat.log").get_path(), std::ios::app);
auto& plData = *player->get_net_data();
auto ip = player->get_ip_address();

View File

@ -10,7 +10,7 @@ namespace big::system
{
DWORD64 base_address = memory::module("GTA5.exe").begin().as<DWORD64>();
const auto file_path = g_file_manager->get_project_file("./entrypoints.txt");
const auto file_path = g_file_manager.get_project_file("./entrypoints.txt");
auto file = std::ofstream(file_path.get_path(), std::ios::out | std::ios::trunc);
for (auto& map : g_crossmap)

View File

@ -20,7 +20,7 @@ namespace big::teleport
inline std::filesystem::path get_telelocations_file_path()
{
return g_file_manager->get_project_file("telelocations.json").get_path();
return g_file_manager.get_project_file("telelocations.json").get_path();
}
inline bool fetch_saved_locations()

View File

@ -9,17 +9,17 @@ namespace big
{
if (ImGui::BeginTabItem("DEBUG_TAB_GLOBALS"_T.data()))
{
if (ImGui::Checkbox("DEBUG_GLOBALS_ENABLE_FREEZING"_T.data(), &g_globals_service->m_running)
&& g_globals_service->m_running)
if (ImGui::Checkbox("DEBUG_GLOBALS_ENABLE_FREEZING"_T.data(), &g_globals_service.m_running)
&& g_globals_service.m_running)
g_thread_pool->push([&]() {
g_globals_service->loop();
g_globals_service.loop();
});
if (components::button("LOAD"_T))
g_globals_service->load();
g_globals_service.load();
ImGui::SameLine();
if (components::button("SAVE"_T))
g_globals_service->save();
g_globals_service.save();
ImGui::SameLine();
@ -81,7 +81,7 @@ namespace big
auto new_global = global(name, base_address, freeze, offsets, offset_count);
new_global.build_cache();
g_globals_service->m_globals.push_back(new_global);
g_globals_service.m_globals.push_back(new_global);
strcpy(name, "");
freeze = false;
@ -94,7 +94,7 @@ namespace big
ImGui::EndPopup();
}
for (auto& global : g_globals_service->m_globals)
for (auto& global : g_globals_service.m_globals)
{
char label[64];
@ -128,9 +128,9 @@ namespace big
if (components::button("DELETE"_T))
{
for (int i = 0; i < g_globals_service->m_globals.size(); i++)
if (auto& it = g_globals_service->m_globals.at(i); it.get_id() == global.get_id())
g_globals_service->m_globals.erase(g_globals_service->m_globals.begin() + i);
for (int i = 0; i < g_globals_service.m_globals.size(); i++)
if (auto& it = g_globals_service.m_globals.at(i); it.get_id() == global.get_id())
g_globals_service.m_globals.erase(g_globals_service.m_globals.begin() + i);
break;
}

View File

@ -161,7 +161,7 @@ namespace big
ImGui::Separator();
static char outfit_name[MAX_PATH] = {};
static folder saved_outfit_path = g_file_manager->get_project_folder("saved_outfits");
static folder saved_outfit_path = g_file_manager.get_project_folder("saved_outfits");
std::vector<std::string> saved_outfits;
for (const auto& directory_entry : std::filesystem::directory_iterator(saved_outfit_path.get_path()))
saved_outfits.push_back(directory_entry.path().filename().generic_string());