Skip to content

Commit 415d87f

Browse files
committed
Code cleanup
1 parent 821314e commit 415d87f

6 files changed

Lines changed: 256 additions & 241 deletions

File tree

UnityInspector/UnityInspector.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,10 @@
275275
<ClCompile Include="src\game\core\core.cpp" />
276276
<ClCompile Include="src\game\features\inspector\esp.cpp" />
277277
<ClCompile Include="src\game\features\inspector\inspector.cpp" />
278-
<ClCompile Include="src\game\features\inspector\ui.cpp" />
279278
<ClCompile Include="src\game\features\inspector\utils.cpp" />
279+
<ClCompile Include="src\game\features\inspector\hierarchy_window.cpp" />
280+
<ClCompile Include="src\game\features\inspector\inspector_window.cpp" />
281+
<ClCompile Include="src\game\features\inspector\invoke_popup.cpp" />
280282
<ClCompile Include="src\game\features\Tests\Tests.cpp" />
281283
<ClCompile Include="src\game\helper\helper.cpp" />
282284
<ClCompile Include="src\game\hooks\hooks.cpp" />
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "pch.h"
2+
#include "inspector.h"
3+
4+
5+
void Inspector::RenderHierarchyNode(HierarchyNode& node, const int depth)
6+
{
7+
if (!Core::helper->SafeIsAlive(node.gameObject)) return;
8+
9+
bool matchesSearch = true;
10+
if (searchBuffer[0] != '\0')
11+
{
12+
matchesSearch = node.name.find(searchBuffer) != std::string::npos;
13+
14+
if (!matchesSearch)
15+
{
16+
for (auto& child : node.children)
17+
{
18+
std::function<bool(HierarchyNode&)> checkChildren = [&](HierarchyNode& n) -> bool {
19+
if (n.name.find(searchBuffer) != std::string::npos) return true;
20+
for (auto& c : n.children)
21+
{
22+
if (checkChildren(c)) return true;
23+
}
24+
return false;
25+
};
26+
if (checkChildren(child))
27+
{
28+
matchesSearch = true;
29+
break;
30+
}
31+
}
32+
}
33+
}
34+
35+
if (!matchesSearch) return;
36+
37+
ImGui::PushID(node.gameObject);
38+
39+
const bool hasChildren = !node.children.empty();
40+
const bool isSelected = (FindTabForObject(node.gameObject) >= 0);
41+
42+
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanAvailWidth;
43+
if (!hasChildren) flags |= ImGuiTreeNodeFlags_Leaf;
44+
if (isSelected) flags |= ImGuiTreeNodeFlags_Selected;
45+
46+
bool isActive = true;
47+
if (!Core::helper->SafeGetActiveSelf(node.gameObject, isActive))
48+
{
49+
ImGui::PopID();
50+
return;
51+
}
52+
53+
if (!isActive)
54+
{
55+
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
56+
}
57+
58+
const bool nodeOpen = ImGui::TreeNodeEx(node.name.c_str(), flags);
59+
60+
if (!isActive)
61+
{
62+
ImGui::PopStyleColor();
63+
}
64+
65+
if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
66+
{
67+
OpenObjectInNewTab(node.gameObject);
68+
}
69+
70+
if (ImGui::BeginPopupContextItem())
71+
{
72+
if (ImGui::MenuItem("Inspect"))
73+
{
74+
OpenObjectInNewTab(node.gameObject);
75+
}
76+
ImGui::EndPopup();
77+
}
78+
79+
if (nodeOpen)
80+
{
81+
for (auto& child : node.children)
82+
{
83+
RenderHierarchyNode(child, depth + 1);
84+
}
85+
ImGui::TreePop();
86+
}
87+
88+
ImGui::PopID();
89+
}

UnityInspector/src/game/features/inspector/ui.cpp renamed to UnityInspector/src/game/features/inspector/inspector_window.cpp

Lines changed: 4 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,6 @@
1-
#include "pch.h"
1+
#include "pch.h"
22
#include "inspector.h"
33

4-
void Inspector::RenderHierarchyNode(HierarchyNode& node, const int depth)
5-
{
6-
if (!Core::helper->SafeIsAlive(node.gameObject)) return;
7-
8-
bool matchesSearch = true;
9-
if (searchBuffer[0] != '\0')
10-
{
11-
matchesSearch = node.name.find(searchBuffer) != std::string::npos;
12-
13-
if (!matchesSearch)
14-
{
15-
for (auto& child : node.children)
16-
{
17-
std::function<bool(HierarchyNode&)> checkChildren = [&](HierarchyNode& n) -> bool {
18-
if (n.name.find(searchBuffer) != std::string::npos) return true;
19-
for (auto& c : n.children)
20-
{
21-
if (checkChildren(c)) return true;
22-
}
23-
return false;
24-
};
25-
if (checkChildren(child))
26-
{
27-
matchesSearch = true;
28-
break;
29-
}
30-
}
31-
}
32-
}
33-
34-
if (!matchesSearch) return;
35-
36-
ImGui::PushID(node.gameObject);
37-
38-
const bool hasChildren = !node.children.empty();
39-
const bool isSelected = (FindTabForObject(node.gameObject) >= 0);
40-
41-
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanAvailWidth;
42-
if (!hasChildren) flags |= ImGuiTreeNodeFlags_Leaf;
43-
if (isSelected) flags |= ImGuiTreeNodeFlags_Selected;
44-
45-
bool isActive = true;
46-
if (!Core::helper->SafeGetActiveSelf(node.gameObject, isActive))
47-
{
48-
ImGui::PopID();
49-
return;
50-
}
51-
52-
if (!isActive)
53-
{
54-
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
55-
}
56-
57-
const bool nodeOpen = ImGui::TreeNodeEx(node.name.c_str(), flags);
58-
59-
if (!isActive)
60-
{
61-
ImGui::PopStyleColor();
62-
}
63-
64-
if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen())
65-
{
66-
OpenObjectInNewTab(node.gameObject);
67-
}
68-
69-
if (ImGui::BeginPopupContextItem())
70-
{
71-
if (ImGui::MenuItem("Inspect"))
72-
{
73-
OpenObjectInNewTab(node.gameObject);
74-
}
75-
ImGui::EndPopup();
76-
}
77-
78-
if (nodeOpen)
79-
{
80-
for (auto& child : node.children)
81-
{
82-
RenderHierarchyNode(child, depth + 1);
83-
}
84-
ImGui::TreePop();
85-
}
86-
87-
ImGui::PopID();
88-
}
894
void Inspector::RenderEditableField(UT::Component* component, const ComponentFieldInfo& field) const
905
{
916
if (!component || field.offset < 0) return;
@@ -692,7 +607,6 @@ void Inspector::RenderTransformSection(UT::Transform* transform, InspectedObject
692607
}
693608
}
694609

695-
696610
void Inspector::RenderMethodsSection(UT::Component* component, const std::vector<ComponentMethodInfo>& methods, InspectedObjectTab& tab, const size_t componentIndex)
697611
{
698612
if (methods.empty())
@@ -1364,7 +1278,7 @@ void Inspector::RenderTabContent(InspectedObjectTab& tab)
13641278
ImGui::Text("Tag: %s", tag->ToString().c_str());
13651279
}
13661280
}
1367-
1281+
13681282
if (bool isStatic = false; Helper::SafeGetIsStatic(tab.gameObject, isStatic))
13691283
{
13701284
ImGui::SameLine();
@@ -1392,12 +1306,12 @@ void Inspector::RenderTabContent(InspectedObjectTab& tab)
13921306
if (ImGui::BeginChild("Content", ImVec2(0, 0), false))
13931307
{
13941308
UT::Transform* transform = nullptr;
1395-
1309+
13961310
if (tab.gameObject && Core::helper->SafeIsAlive(tab.gameObject))
13971311
{
13981312
Core::helper->SafeGetTransform(tab.gameObject, transform);
13991313
}
1400-
1314+
14011315

14021316
if (transform)
14031317
{
@@ -1413,153 +1327,4 @@ void Inspector::RenderTabContent(InspectedObjectTab& tab)
14131327
RenderComponentsSection(tab);
14141328
}
14151329
ImGui::EndChild();
1416-
}
1417-
1418-
void Inspector::RenderMethodInvokePopup()
1419-
{
1420-
if (!invokeState.showPopup) return;
1421-
1422-
ImGui::SetNextWindowSize(ImVec2(400, 300), ImGuiCond_FirstUseEver);
1423-
1424-
std::string title = "Invoke: " + invokeState.method.name + "###MethodInvoke";
1425-
if (ImGui::Begin(title.c_str(), &invokeState.showPopup))
1426-
{
1427-
ImGui::Text("Method: %s", invokeState.method.name.c_str());
1428-
ImGui::Text("Return Type: %s", invokeState.method.returnTypeName.c_str());
1429-
if (invokeState.method.isStatic)
1430-
{
1431-
ImGui::SameLine();
1432-
ImGui::TextColored(ImVec4(0.5f, 0.8f, 1.0f, 1.0f), "[Static]");
1433-
}
1434-
1435-
ImGui::Separator();
1436-
1437-
if (invokeState.method.parameters.empty())
1438-
{
1439-
ImGui::TextDisabled("No parameters");
1440-
}
1441-
else
1442-
{
1443-
ImGui::Text("Parameters:");
1444-
for (size_t i = 0; i < invokeState.method.parameters.size(); i++)
1445-
{
1446-
const auto& [name, typeName] = invokeState.method.parameters[i];
1447-
const auto paramType = invokeState.method.parameterEditableTypes[i];
1448-
1449-
ImGui::PushID(static_cast<int>(i));
1450-
1451-
std::string label = name + " (" + typeName + ")";
1452-
ImGui::Text("%s", label.c_str());
1453-
ImGui::SameLine();
1454-
1455-
char buf[256] = {};
1456-
if (i < invokeState.parameterValues.size() && !invokeState.parameterValues[i].empty())
1457-
{
1458-
strncpy_s(buf, invokeState.parameterValues[i].c_str(), sizeof(buf) - 1);
1459-
}
1460-
1461-
ImGui::SetNextItemWidth(-1);
1462-
switch (paramType)
1463-
{
1464-
case EditableType::Int:
1465-
case EditableType::Float:
1466-
case EditableType::Double:
1467-
if (ImGui::InputText("##param", buf, sizeof(buf), ImGuiInputTextFlags_CharsDecimal))
1468-
{
1469-
invokeState.parameterValues[i] = buf;
1470-
}
1471-
break;
1472-
case EditableType::Bool:
1473-
{
1474-
bool val = (invokeState.parameterValues[i] == "true" || invokeState.parameterValues[i] == "1");
1475-
if (ImGui::Checkbox("##param", &val))
1476-
{
1477-
invokeState.parameterValues[i] = val ? "true" : "false";
1478-
}
1479-
break;
1480-
}
1481-
default:
1482-
if (ImGui::InputText("##param", buf, sizeof(buf)))
1483-
{
1484-
invokeState.parameterValues[i] = buf;
1485-
}
1486-
break;
1487-
}
1488-
1489-
ImGui::PopID();
1490-
}
1491-
}
1492-
1493-
ImGui::Separator();
1494-
1495-
if (ImGui::Button("Invoke", ImVec2(100, 0)))
1496-
{
1497-
void* result = InvokeMethod(invokeState.targetComponent, invokeState.method, invokeState.parameterValues);
1498-
1499-
invokeState.hasResult = true;
1500-
if (result)
1501-
{
1502-
if (invokeState.method.returnTypeName == "void" || invokeState.method.returnTypeName == "System.Void")
1503-
{
1504-
invokeState.resultText = "(void)";
1505-
}
1506-
else
1507-
{
1508-
const EditableType retType = DetermineEditableType(invokeState.method.returnTypeName);
1509-
void* unboxed = UR::Invoke<void*, void*>(
1510-
Core::config->gameMode == UnityResolve::Mode::Mono ? "mono_object_unbox" : "il2cpp_object_unbox", result);
1511-
1512-
if (unboxed)
1513-
{
1514-
switch (retType)
1515-
{
1516-
case EditableType::Int:
1517-
invokeState.resultText = std::to_string(*static_cast<int*>(unboxed));
1518-
break;
1519-
case EditableType::Float:
1520-
invokeState.resultText = std::to_string(*static_cast<float*>(unboxed));
1521-
break;
1522-
case EditableType::Double:
1523-
invokeState.resultText = std::to_string(*static_cast<double*>(unboxed));
1524-
break;
1525-
case EditableType::Bool:
1526-
invokeState.resultText = *static_cast<bool*>(unboxed) ? "true" : "false";
1527-
break;
1528-
default:
1529-
invokeState.resultText = std::format("(object: 0x{:X})", reinterpret_cast<uintptr_t>(result));
1530-
break;
1531-
}
1532-
}
1533-
else
1534-
{
1535-
invokeState.resultText = std::format("(object: 0x{:X})", reinterpret_cast<uintptr_t>(result));
1536-
}
1537-
}
1538-
}
1539-
else
1540-
{
1541-
if (invokeState.method.returnTypeName == "void" || invokeState.method.returnTypeName == "System.Void")
1542-
{
1543-
invokeState.resultText = "(completed)";
1544-
}
1545-
else
1546-
{
1547-
invokeState.resultText = "(null or error)";
1548-
}
1549-
}
1550-
}
1551-
1552-
ImGui::SameLine();
1553-
if (ImGui::Button("Close", ImVec2(100, 0)))
1554-
{
1555-
invokeState.showPopup = false;
1556-
}
1557-
1558-
if (invokeState.hasResult)
1559-
{
1560-
ImGui::Separator();
1561-
ImGui::Text("Result: %s", invokeState.resultText.c_str());
1562-
}
1563-
}
1564-
ImGui::End();
15651330
}

0 commit comments

Comments
 (0)