This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/BigBaseV2/src/core/globals.hpp

231 lines
4.7 KiB
C++
Raw Normal View History

2021-05-19 14:32:30 +02:00
#pragma once
2021-05-26 13:17:19 +02:00
#include "data/player_struct.hpp"
2021-05-19 16:05:21 +02:00
#include "enums.hpp"
2021-05-19 14:32:30 +02:00
#ifndef GLOBALS_H
#define GLOBALS_H
using namespace big;
2021-05-19 14:32:30 +02:00
struct globals {
nlohmann::json default_options;
nlohmann::json options;
2021-07-24 00:16:04 +02:00
struct tunables {
bool disable_phone = false;
2021-07-24 14:39:13 +02:00
bool no_idle_kick = false;
2021-07-24 00:16:04 +02:00
};
2021-05-26 13:17:19 +02:00
struct player {
int character_slot = 1;
int set_level = 130;
2021-05-26 13:17:19 +02:00
bool spectating = false;
};
2021-05-19 14:32:30 +02:00
struct self {
bool godmode = false;
2021-05-21 01:16:33 +02:00
bool off_radar = false;
2021-05-19 14:32:30 +02:00
bool noclip = false;
2021-05-21 00:52:59 +02:00
bool no_ragdoll = false;
2021-05-19 14:32:30 +02:00
};
struct vehicle {
2021-05-20 23:18:44 +02:00
bool horn_boost = false;
SpeedoMeter speedo_meter = SpeedoMeter::DISABLED;
};
2021-05-19 15:32:51 +02:00
struct weapons {
CustomWeapon custom_weapon = CustomWeapon::NONE;
2021-05-20 22:39:56 +02:00
char vehicle_gun_model[12] = "bus";
2021-05-19 15:32:51 +02:00
};
2021-05-21 02:28:14 +02:00
struct window {
bool main = true;
2021-05-21 12:44:43 +02:00
bool log = false;
bool users = true;
2021-05-26 13:17:19 +02:00
bool player = false;
int x;
int y;
2021-05-21 02:28:14 +02:00
};
2021-07-23 18:06:33 +02:00
int friend_count = 0;
int player_count = 0;
2021-05-26 13:17:19 +02:00
CPlayer players[32];
CPlayer selected_player;
2021-07-24 00:16:04 +02:00
tunables tunables{};
2021-05-26 13:17:19 +02:00
player player{};
2021-05-19 14:32:30 +02:00
self self{};
vehicle vehicle{};
2021-05-19 15:32:51 +02:00
weapons weapons{};
2021-05-21 02:28:14 +02:00
window window{};
void from_json(const nlohmann::json& j)
{
2021-07-24 00:16:04 +02:00
this->tunables.disable_phone = j["tunables"]["disable_phone"];
2021-07-24 14:39:13 +02:00
this->tunables.no_idle_kick = j["tunables"]["no_idle_kick"];
2021-07-24 00:16:04 +02:00
this->self.godmode = j["self"]["godmode"];
2021-05-21 01:16:33 +02:00
this->self.off_radar = j["self"]["off_radar"];
2021-05-21 00:52:59 +02:00
this->self.no_ragdoll = j["self"]["no_ragdoll"];
2021-05-20 23:18:44 +02:00
this->vehicle.horn_boost = j["vehicle"]["horn_boost"];
this->vehicle.speedo_meter = (SpeedoMeter)j["vehicle"]["speedo_meter"];
this->weapons.custom_weapon = (CustomWeapon)j["weapons"]["custom_weapon"];
this->window.log = j["window"]["log"];
this->window.main = j["window"]["main"];
this->window.users = j["window"]["users"];
}
nlohmann::json to_json()
{
return nlohmann::json{
2021-07-24 00:16:04 +02:00
{
"tunables", {
2021-07-24 14:39:13 +02:00
{ "disable_phone", this->tunables.disable_phone },
{ "no_idle_kick", this->tunables.no_idle_kick }
2021-07-24 00:16:04 +02:00
}
},
{
"self", {
2021-05-21 00:52:59 +02:00
{ "godmode", this->self.godmode },
2021-05-21 01:16:33 +02:00
{ "off_radar", this->self.off_radar },
2021-05-21 00:52:59 +02:00
{ "no_ragdoll", this->self.no_ragdoll }
}
},
{
"vehicle", {
2021-05-20 23:18:44 +02:00
{ "horn_boost", this->vehicle.horn_boost },
{ "speedo_meter", (int)this->vehicle.speedo_meter }
}
},
{
"weapons", {
2021-05-20 23:18:44 +02:00
{ "custom_weapon", (int)this->weapons.custom_weapon }
}
},
{
"window", {
{ "log", this->window.log },
{ "main", this->window.main },
{ "users", this->window.users }
}
}
};
}
void attempt_save()
{
nlohmann::json& j = this->to_json();
if (deep_compare(this->options, j, true))
{
LOG(INFO) << "Settings changed, saving...";
this->save();
}
}
bool load()
{
this->default_options = this->to_json();
std::string settings_file = std::getenv("appdata");
settings_file += this->settings_location;
std::ifstream file(settings_file);
if (!file.is_open())
{
this->write_default_config();
file.open(settings_file);
}
try
{
file >> this->options;
}
catch (const std::exception&)
{
LOG(WARNING) << "Detected corrupt settings, writing default config...";
this->write_default_config();
return this->load();
}
bool should_save = this->deep_compare(this->options, this->default_options);
this->from_json(this->options);
if (should_save)
{
LOG(INFO) << "Updating settings.";
save();
}
return true;
}
2021-05-21 00:52:59 +02:00
private:
const char* settings_location = "\\BigBaseV2\\settings.json";
bool deep_compare(nlohmann::json& current_settings, const nlohmann::json& default_settings, bool compare_value = false)
{
bool should_save = false;
for (auto& e : default_settings.items())
{
const std::string &key = e.key();
if (current_settings.count(key) == 0 || (compare_value && current_settings[key] != e.value()))
{
current_settings[key] = e.value();
should_save = true;
}
else if (current_settings[key].is_structured() && e.value().is_structured())
{
if (deep_compare(current_settings[key], e.value(), compare_value))
should_save = true;
}
else if (!current_settings[key].is_structured() && e.value().is_structured()) {
current_settings[key] = e.value();
should_save = true;
}
}
return should_save;
}
bool save()
{
std::string settings_file = std::getenv("appdata");
settings_file += this->settings_location;
std::ofstream file(settings_file, std::ios::out | std::ios::trunc);
file << this->to_json().dump(4);
file.close();
return true;
}
bool write_default_config()
{
std::string settings_file = std::getenv("appdata");
settings_file += this->settings_location;
std::ofstream file(settings_file, std::ios::out);
file << this->to_json().dump(4);
file.close();
return true;
}
2021-05-19 14:32:30 +02:00
};
inline struct globals g;
#endif // !GLOBALS_H