Cogs.Core
ResourceStoreInspector.cpp
1#include "Inspectors.h"
2
3#include "InspectorGuiHelper.h"
4#include "Context.h"
5#include "Resources/ResourceStore.h"
6#include "Resources/ResourceStoreStorage.h"
7
8#include "imgui.h"
9#include <cctype>
10#include <list>
11
12
13void Cogs::Core::resourceStoreInspector(Context * context, bool * show)
14{
15 if (*show) {
16 ImGui::SetNextWindowSize(ImVec2(600, 400), ImGuiCond_Once);
17
18 LockGuard lock(context->resourceStore->data->cache->lock);
19
20 std::list<std::string> keys;
21 for (const auto & it : context->resourceStore->data->cache->resources) {
22 keys.push_back(it.first);
23 }
24 keys.sort();
25
26
27 std::string windowHeader;
28 windowHeader.reserve(50);
29 windowHeader.append("Resources [");
30 windowHeader.append(std::to_string(keys.size()));
31 windowHeader.append("]");
32
33 guiBegin(windowHeader, show);
34
35 for (const auto & key : keys) {
36 if (ImGui::TreeNode(key.c_str())) {
37 const auto & resource = context->resourceStore->data->cache->resources[key];
38
39 ImGui::Text("Name: %s", resource.name.c_str());
40 ImGui::Text("Path: %s", resource.path.c_str());
41 ImGui::Text("Size: %zu (0x%zx) bytes", resource.bytes.size(), resource.bytes.size());
42
43 if (ImGui::TreeNode("Text view")) {
44 ImGui::Separator();
45 const auto size = resource.bytes.size();
46 const auto * data = reinterpret_cast<const char*>(resource.bytes.data());
47
48 uint32_t l = 1;
49 for (size_t a = 0; a < size; ) {
50 // Search for line start
51 for (; a < size; a++) {
52 if (data[a] == '\r');
53 else if (data[a] == '\n') l++;
54 else break;
55 }
56 // Search for end of line
57 auto b = a;
58 for (; b < size; b++) {
59 if ((data[b] == '\r') || (data[b] == '\n')) break;
60 }
61
62 if (a < b) {
63 ImGui::Text("%4d ", l);
64 ImGui::SameLine();
65 ImGui::TextUnformatted(data + a, data + b);
66 a = b;
67 }
68 else break;
69 }
70 ImGui::Separator();
71 ImGui::TreePop();
72 }
73 if (ImGui::TreeNode("Hex dump")) {
74 static constexpr char tab[16] = {
75 '0', '1', '2', '3', '4', '5', '6', '7',
76 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
77 };
78
79 char buf[10 + 3 * 16 + 1 + 16 + 1];
80
81 ImGui::Separator();
82
83 auto data = resource.bytes.toSpan<uint8_t>();
84 const size_t size = data.size();
85
86 for (size_t o = 0; o < size; o += 16) {
87 std::memset(buf, ' ', sizeof(buf));
88
89 for (unsigned i = 0; i < 8; i++) {
90 buf[i] = tab[(o >> (24 - 4 * i)) & 0xf];
91 }
92 buf[8] = ':';
93
94 buf[10 + 3 * 16] = '"';
95 buf[10 + 3 * 16 + 1 + std::min((size - o), (size_t)16)] = '"';
96 for (unsigned i = 0; i < 16; i++) {
97 if (o + i < size) {
98 buf[10 + 3 * i + 0] = tab[((data[o + i] >> 4) & 0xf)];
99 buf[10 + 3 * i + 1] = tab[((data[o + i] >> 0) & 0xf)];
100 if ((33 <= data[o + i]) && (data[o + i] <= 127)) {
101 buf[10 + 3 * 16 + 1 + i] = data[o + i];
102 }
103 }
104 }
105 ImGui::TextUnformatted(buf, buf + sizeof(buf));
106 }
107
108 ImGui::Separator();
109 ImGui::TreePop();
110 }
111 ImGui::TreePop();
112 }
113 }
114
115 guiEnd();
116 }
117}