#include "imgui_snapshot.h" #include #include #include #include #include #include template void ImFontAtlasSnapshot::SnapPointer(size_t offset, const T1& value, const T2& ptr, size_t count) { if (ptr != nullptr && count != 0) { if (!objects.contains(ptr)) { constexpr size_t ALIGN = alignof(std::remove_pointer_t); constexpr size_t SIZE = sizeof(std::remove_pointer_t); size_t ptrOffset = (data.size() + ALIGN - 1) & ~(ALIGN - 1); data.resize(ptrOffset + SIZE * count); memcpy(&data[ptrOffset], ptr, SIZE * count); for (size_t i = 0; i < count; i++) { size_t curPtrOffset = ptrOffset + SIZE * i; objects[&ptr[i]] = curPtrOffset; Traverse(curPtrOffset, ptr[i]); } } size_t fieldOffset = offset + (reinterpret_cast(&ptr) - reinterpret_cast(&value)); *reinterpret_cast(&data[fieldOffset]) = objects[ptr]; offsets.push_back(fieldOffset); } } template void ImFontAtlasSnapshot::Traverse(size_t offset, const T& value) { if constexpr (std::is_pointer_v) { SnapPointer(offset, value, value, 1); } else if constexpr (std::is_same_v) { SnapPointer(offset, value, value.ConfigData.Data, value.ConfigData.Size); SnapPointer(offset, value, value.CustomRects.Data, value.CustomRects.Size); SnapPointer(offset, value, value.Fonts.Data, value.Fonts.Size); } else if constexpr (std::is_same_v) { SnapPointer(offset, value, value.IndexAdvanceX.Data, value.IndexAdvanceX.Size); SnapPointer(offset, value, value.IndexLookup.Data, value.IndexLookup.Size); SnapPointer(offset, value, value.Glyphs.Data, value.Glyphs.Size); SnapPointer(offset, value, value.FallbackGlyph, 1); SnapPointer(offset, value, value.ContainerAtlas, 1); SnapPointer(offset, value, value.ConfigData, value.ConfigDataCount); } else if constexpr (std::is_same_v) { SnapPointer(offset, value, value.Font, 1); } else if constexpr (std::is_same_v) { SnapPointer(offset, value, value.GlyphRanges, value.GlyphRanges != nullptr ? wcslen(reinterpret_cast(value.GlyphRanges)) + 1 : 0); SnapPointer(offset, value, value.DstFont, 1); } } template size_t ImFontAtlasSnapshot::Snap(const T& value) { size_t offset = (data.size() + alignof(T) - 1) & ~(alignof(T) - 1); data.resize(offset + sizeof(T)); memcpy(&data[offset], &value, sizeof(T)); objects[&value] = offset; Traverse(offset, value); return offset; } struct ImFontAtlasSnapshotHeader { uint32_t imguiVersion; uint32_t dataOffset; uint32_t offsetCount; uint32_t offsetsOffset; }; void ImFontAtlasSnapshot::Snap() { data.resize(sizeof(ImFontAtlasSnapshotHeader)); size_t dataOffset = Snap(*ImGui::GetIO().Fonts); size_t offsetsOffset = data.size(); std::sort(offsets.begin(), offsets.end()); data.insert(data.end(), reinterpret_cast(offsets.data()), reinterpret_cast(offsets.data() + offsets.size())); auto header = reinterpret_cast(data.data()); header->imguiVersion = IMGUI_VERSION_NUM; header->dataOffset = dataOffset; header->offsetCount = offsets.size(); header->offsetsOffset = offsetsOffset; } static std::unique_ptr g_imFontAtlas; ImFontAtlas* ImFontAtlasSnapshot::Load() { g_imFontAtlas = decompressZstd(g_im_font_atlas, g_im_font_atlas_uncompressed_size); auto header = reinterpret_cast(g_imFontAtlas.get()); assert(header->imguiVersion == IMGUI_VERSION_NUM && "ImGui version mismatch, the font atlas needs to be regenerated!"); auto offsetTable = reinterpret_cast(g_imFontAtlas.get() + header->offsetsOffset); for (size_t i = 0; i < header->offsetCount; i++) { *reinterpret_cast(g_imFontAtlas.get() + (*offsetTable)) += reinterpret_cast(g_imFontAtlas.get()); ++offsetTable; } return reinterpret_cast(g_imFontAtlas.get() + header->dataOffset); } static void GetGlyphs(std::set& glyphs, const std::string_view& value) { const char* cur = value.data(); while (cur < value.data() + value.size()) { unsigned int c; cur += ImTextCharFromUtf8(&c, cur, value.data() + value.size()); glyphs.emplace(c); } } static std::vector g_glyphRanges; void ImFontAtlasSnapshot::GenerateGlyphRanges() { std::vector localeStrings; for (auto& config : g_configDefinitions) config->GetLocaleStrings(localeStrings); std::set glyphs; for (size_t i = 0x20; i <= 0xFF; i++) glyphs.emplace(i); for (auto& localeString : localeStrings) GetGlyphs(glyphs, localeString); for (auto& [name, locale] : g_locale) { for (auto& [language, value] : locale) GetGlyphs(glyphs, value); } for (size_t i = XDBF_LANGUAGE_ENGLISH; i <= XDBF_LANGUAGE_ITALIAN; i++) { auto achievements = g_xdbfWrapper.GetAchievements(static_cast(i)); for (auto& achievement : achievements) { GetGlyphs(glyphs, achievement.Name); GetGlyphs(glyphs, achievement.UnlockedDesc); GetGlyphs(glyphs, achievement.LockedDesc); } } for (auto glyph : glyphs) { if (g_glyphRanges.empty() || (g_glyphRanges.back() + 1) != glyph) { g_glyphRanges.push_back(glyph); g_glyphRanges.push_back(glyph); } else { g_glyphRanges.back() = glyph; } } g_glyphRanges.push_back(0); } ImFont* ImFontAtlasSnapshot::GetFont(const char* name) { auto fontAtlas = ImGui::GetIO().Fonts; for (auto& configData : fontAtlas->ConfigData) { if (strstr(configData.Name, name) != nullptr) { assert(configData.DstFont != nullptr); return configData.DstFont; } } #ifdef ENABLE_IM_FONT_ATLAS_SNAPSHOT assert(false && "Unable to locate equivalent font in the atlas file."); #endif return fontAtlas->AddFontFromFileTTF(name, 24.0f, nullptr, g_glyphRanges.data()); }