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/src/views/debug/view_debug_globals.cpp

257 lines
7.3 KiB
C++
Raw Normal View History

#include "gui/components/components.hpp"
2023-07-13 12:42:21 -04:00
#include "script_global.hpp"
#include "thread_pool.hpp"
#include "view_debug.hpp"
namespace big
{
2023-07-13 12:42:21 -04:00
enum GlobalAppendageType : int
{
2023-07-13 12:42:21 -04:00
GlobalAppendageType_At,
GlobalAppendageType_ReadGlobal,
GlobalAppendageType_PlayerId,
};
2023-07-13 12:42:21 -04:00
struct global_debug_inner
{
GlobalAppendageType type{};
std::ptrdiff_t index{};
std::size_t size{};
std::string global_name{};
2023-07-13 12:42:21 -04:00
NLOHMANN_DEFINE_TYPE_INTRUSIVE(global_debug_inner, type, index, size, global_name)
};
2023-07-13 12:42:21 -04:00
struct global_debug
{
std::size_t global_index{};
std::vector<global_debug_inner> global_appendages{};
2023-07-13 12:42:21 -04:00
NLOHMANN_DEFINE_TYPE_INTRUSIVE(global_debug, global_index, global_appendages)
};
2023-07-13 12:42:21 -04:00
nlohmann::json get_globals_json()
{
nlohmann::json globals{};
2023-07-13 12:42:21 -04:00
auto file = g_file_manager.get_project_file("./globals.json");
if (file.exists())
{
std::ifstream iffstream_file(file.get_path());
iffstream_file >> globals;
}
2023-07-13 12:42:21 -04:00
return globals;
}
2023-07-13 12:42:21 -04:00
void load_global_menu(const std::string& selected_global, global_debug& global_obj)
{
if (!selected_global.empty())
{
auto globals = get_globals_json();
if (globals[selected_global].is_null())
return;
global_obj = globals[selected_global].get<global_debug>();
}
}
2023-07-13 12:42:21 -04:00
int64_t* get_global_ptr(global_debug& global_test)
{
script_global global_to_read = script_global(global_test.global_index);
for (auto item : global_test.global_appendages)
{
if (item.type == GlobalAppendageType_At)
{
if (item.size != 0)
global_to_read = global_to_read.at(item.index, item.size);
else
global_to_read = global_to_read.at(item.index);
}
2023-07-13 12:42:21 -04:00
else if (item.type == GlobalAppendageType_ReadGlobal)
{
2023-07-13 12:42:21 -04:00
global_debug global_read;
load_global_menu(item.global_name, global_read);
if (auto ptr = get_global_ptr(global_read))
if (item.size != 0)
global_to_read = global_to_read.at(*ptr, item.size);
else
global_to_read = global_to_read.at(*ptr);
else
LOG(WARNING) << "Failed to read " << item.global_name << "for get_global_ptr";
}
else if (item.type == GlobalAppendageType_PlayerId)
{
if (item.size != 0)
global_to_read = global_to_read.at(self::id, item.size);
else
global_to_read = global_to_read.at(self::id);
}
}
auto retn_val = global_to_read.as<int64_t*>();
if ((size_t)retn_val < UINT32_MAX)
return nullptr;
return retn_val;
}
2023-07-13 12:42:21 -04:00
std::map<std::string, global_debug> list_globals()
{
auto json = get_globals_json();
std::map<std::string, global_debug> return_value;
for (auto& item : json.items())
return_value[item.key()] = item.value();
return return_value;
}
2023-07-13 12:42:21 -04:00
void save_global(char* global_name, global_debug& global_obj)
{
std::string teleport_name_string = global_name;
if (!teleport_name_string.empty())
{
auto json = get_globals_json();
json[teleport_name_string] = global_obj;
2023-07-13 12:42:21 -04:00
auto file_path = g_file_manager.get_project_file("./globals.json").get_path();
std::ofstream file(file_path, std::ios::out | std::ios::trunc);
file << json.dump(4);
file.close();
ZeroMemory(global_name, sizeof(global_name));
}
}
2023-07-13 12:42:21 -04:00
void delete_global(std::string name)
{
auto locations = get_globals_json();
if (locations[name].is_null())
return;
locations.erase(name);
auto file_path = g_file_manager.get_project_file("./globals.json").get_path();
std::ofstream file(file_path, std::ios::out | std::ios::trunc);
file << locations.dump(4);
file.close();
}
2023-07-13 12:42:21 -04:00
void debug::globals()
{
if (ImGui::BeginTabItem("DEBUG_TAB_GLOBALS"_T.data()))
{
static global_debug global_test{};
static script_global glo_bal_sunday = script_global(global_test.global_index);
ImGui::SetNextItemWidth(200.f);
if (ImGui::InputScalar("Global", ImGuiDataType_U64, &global_test.global_index))
glo_bal_sunday = script_global(global_test.global_index);
2023-07-13 12:42:21 -04:00
for (int i = 0; i < global_test.global_appendages.size(); i++)
{
auto item = global_test.global_appendages[i];
switch (item.type)
{
case GlobalAppendageType_At:
ImGui::SetNextItemWidth(200.f);
ImGui::InputScalar(std::format("At##{}{}", i, (int)item.type).c_str(),
ImGuiDataType_S64,
&global_test.global_appendages[i].index);
ImGui::SameLine();
ImGui::SetNextItemWidth(200.f);
ImGui::InputScalar(std::format("Size##{}{}", i, (int)item.type).c_str(),
ImGuiDataType_S64,
&global_test.global_appendages[i].size);
break;
case GlobalAppendageType_ReadGlobal:
ImGui::Text(std::format("Read Global {}", item.global_name).c_str());
ImGui::SameLine();
ImGui::SetNextItemWidth(200.f);
ImGui::InputScalar(std::format("Size##{}{}", i, (int)item.type).c_str(),
ImGuiDataType_S64,
&global_test.global_appendages[i].size);
break;
case GlobalAppendageType_PlayerId:
ImGui::SetNextItemWidth(200.f);
ImGui::InputScalar(std::format("Read Player ID Size##{}{}", i, (int)item.type).c_str(),
ImGuiDataType_S64,
&global_test.global_appendages[i].size);
break;
}
}
2023-07-13 12:42:21 -04:00
if (ImGui::Button("Add Offset"))
global_test.global_appendages.push_back({GlobalAppendageType_At, 0LL, 0ULL});
ImGui::SameLine();
if (ImGui::Button("Add Read Player Id"))
global_test.global_appendages.push_back({GlobalAppendageType_PlayerId, 0LL, 0ULL});
2023-07-13 12:42:21 -04:00
if (global_test.global_appendages.size() > 0 && ImGui::Button("Remove Offset"))
global_test.global_appendages.pop_back();
2023-07-13 12:42:21 -04:00
if (auto ptr = get_global_ptr(global_test))
{
ImGui::SetNextItemWidth(200.f);
ImGui::InputScalar("Value", ImGuiDataType_S32, ptr);
2023-07-13 12:42:21 -04:00
}
else
ImGui::Text("INVALID_GLOBAL_READ");
2023-07-13 12:42:21 -04:00
auto globals = list_globals();
static std::string selected_global;
ImGui::Text("Saved Globals");
if (ImGui::BeginListBox("##savedglobals", ImVec2(200, 200)))
{
for (auto pair : globals)
{
2023-07-13 12:42:21 -04:00
if (ImGui::Selectable(pair.first.c_str(), selected_global == pair.first))
selected_global = std::string(pair.first);
}
2023-07-13 12:42:21 -04:00
ImGui::EndListBox();
}
ImGui::SameLine();
if (ImGui::BeginListBox("##globalvalues", ImVec2(200, 200)))
{
for (auto pair : globals)
{
if (auto ptr = get_global_ptr(pair.second))
ImGui::Selectable(std::format("{}", (std::int32_t)*ptr).c_str(), false, ImGuiSelectableFlags_Disabled);
2023-07-13 12:42:21 -04:00
else
ImGui::Selectable("INVALID_GLOBAL_READ", false, ImGuiSelectableFlags_Disabled);
}
ImGui::EndListBox();
}
ImGui::SameLine();
ImGui::BeginGroup();
static char global_name[50]{};
ImGui::SetNextItemWidth(200.f);
ImGui::InputText("##GlobalName", global_name, IM_ARRAYSIZE(global_name));
if (ImGui::IsItemActive())
g.self.hud.typing = TYPING_TICKS;
2023-07-13 12:42:21 -04:00
if (ImGui::Button("Save Global"))
{
save_global(global_name, global_test);
}
ImGui::SameLine();
if (ImGui::Button("Load Global"))
{
load_global_menu(selected_global, global_test);
}
2023-07-13 12:42:21 -04:00
if (ImGui::Button("Delete Global"))
{
if (!selected_global.empty())
{
delete_global(selected_global);
selected_global.clear();
}
}
ImGui::SameLine();
if (ImGui::Button("Add Read Global"))
{
global_test.global_appendages.push_back({GlobalAppendageType_ReadGlobal, 0LL, 0ULL, selected_global});
}
ImGui::SameLine();
if (ImGui::Button("Clear"))
{
global_test.global_index = 0;
global_test.global_appendages.clear();
}
ImGui::EndGroup();
ImGui::EndTabItem();
}
}
}