Redesigned Debug Globals. (#1696)

This commit is contained in:
gir489
2023-07-13 12:42:21 -04:00
committed by GitHub
parent 33433ff5b3
commit 06cf2a579e
4 changed files with 233 additions and 323 deletions

View File

@ -1,148 +1,254 @@
#include "gui/components/components.hpp"
#include "services/globals/globals_service.hpp"
#include "script_global.hpp"
#include "thread_pool.hpp"
#include "view_debug.hpp"
namespace big
{
enum GlobalAppendageType : int
{
GlobalAppendageType_At,
GlobalAppendageType_ReadGlobal,
GlobalAppendageType_PlayerId,
};
struct global_debug_inner
{
GlobalAppendageType type{};
std::ptrdiff_t index{};
std::size_t size{};
std::string global_name{};
NLOHMANN_DEFINE_TYPE_INTRUSIVE(global_debug_inner, type, index, size, global_name)
};
struct global_debug
{
std::size_t global_index{};
std::vector<global_debug_inner> global_appendages{};
NLOHMANN_DEFINE_TYPE_INTRUSIVE(global_debug, global_index, global_appendages)
};
nlohmann::json get_globals_json()
{
nlohmann::json globals{};
auto file = g_file_manager.get_project_file("./globals.json");
if (file.exists())
{
std::ifstream iffstream_file(file.get_path());
iffstream_file >> globals;
}
return globals;
}
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>();
}
}
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);
}
else if (item.type == GlobalAppendageType_ReadGlobal)
{
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;
}
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;
}
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[global_name] = global_obj;
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));
}
}
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();
}
void debug::globals()
{
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)
g_thread_pool->push([&]() {
g_globals_service.loop();
});
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);
if (components::button("LOAD"_T))
g_globals_service.load();
ImGui::SameLine();
if (components::button("SAVE"_T))
g_globals_service.save();
ImGui::SameLine();
if (components::button("DEBUG_GLOBALS_ADD"_T))
for (int i = 0; i < global_test.global_appendages.size(); i++)
{
ImGui::OpenPopup("DEBUG_GLOBALS_NEW"_T.data());
}
if (ImGui::BeginPopupModal("DEBUG_GLOBALS_NEW"_T.data()))
{
static int base_address = 0;
static bool freeze = false;
static char name[32] = "";
static int offsets[10][2] = {};
static int offset_count = 0;
static int previous_offset_count = 0;
ImGui::Text("DEBUG_GLOBALS_NAME"_T.data());
components::input_text_with_hint("##global_name", "Name", name, sizeof(name));
ImGui::Text("DEBUG_GLOBALS_BASE_ADDRESS"_T.data());
ImGui::InputInt("##modal_global_base_addr", &base_address);
ImGui::Text("DEBUG_GLOBAL_FREEZE"_T.data());
ImGui::Checkbox("##modal_global_freeze", &freeze);
ImGui::Text("DEBUG_GLOBAL_OFFSET_COUNT"_T.data());
ImGui::InputInt("##modal_offset_count", &offset_count);
offset_count = std::clamp(offset_count, 0, 10);
ImGui::PushItemWidth(320.f);
for (int i = 0; i < offset_count; i++)
auto item = global_test.global_appendages[i];
switch (item.type)
{
ImGui::PushID(i);
ImGui::Separator();
ImGui::Text("DEBUG_GLOBAL_OFFSET"_T.data(), i + 1);
ImGui::InputInt("##offset", &offsets[i][0]);
ImGui::Text("DEBUG_GLOBAL_SIZE"_T.data());
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::InputInt("##size", &offsets[i][1]);
ImGui::PopID();
}
ImGui::PopItemWidth();
if (components::button("CANCEL"_T))
{
strcpy(name, "");
freeze = false;
offset_count = 0;
previous_offset_count = 0;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (components::button("SAVE"_T))
{
auto new_global = global(name, base_address, freeze, offsets, offset_count);
new_global.build_cache();
g_globals_service.m_globals.push_back(new_global);
strcpy(name, "");
freeze = false;
offset_count = 0;
previous_offset_count = 0;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
for (auto& global : g_globals_service.m_globals)
{
char label[64];
ImGui::Separator();
ImGui::PushID(global.get_id());
ImGui::Checkbox("DEBUG_GLOBAL_FREEZE_TOGGLE"_T.data(), &global.m_freeze);
ImGui::BeginGroup();
ImGui::Text("DEBUG_GLOBALS_NAME"_T.data());
ImGui::Text("DEBUG_GLOBALS_VALUE"_T.data());
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
ImGui::Text(global.m_name.c_str());
sprintf(label, "###input_%d", global.get_id());
ImGui::SetNextItemWidth(200.f);
ImGui::InputInt(label, global.get());
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
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);
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;
}
if (components::button("WRITE"_T))
global.write();
ImGui::PopID();
ImGui::EndGroup();
}
ImGui::EndTabItem();
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});
if (global_test.global_appendages.size() > 0 && ImGui::Button("Remove Offset"))
global_test.global_appendages.pop_back();
if (auto ptr = get_global_ptr(global_test))
{
ImGui::SetNextItemWidth(200.f);
ImGui::InputScalar("Value", ImGuiDataType_S64, ptr);
}
else
ImGui::Text("INVALID_GLOBAL_READ");
auto globals = list_globals();
static std::string selected_global;
ImGui::Text("Saved Globals");
if (ImGui::BeginListBox("##savedglobals", ImVec2(200, 200)))
{
for (auto pair : globals)
{
if (ImGui::Selectable(pair.first.c_str(), selected_global == pair.first))
selected_global = std::string(pair.first);
}
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("{}", *ptr).c_str(), false, ImGuiSelectableFlags_Disabled);
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::Button("Save Global"))
{
save_global(global_name, global_test);
}
ImGui::SameLine();
if (ImGui::Button("Load Global"))
{
load_global_menu(selected_global, global_test);
}
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();
}
}
}