From 6e6978fb5776590e0fcdf5b455d103ceaad040ca Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 03:22:25 -0500 Subject: [PATCH 01/10] get in game --- src/Context.cpp | 6 +- src/public/bridge/resourcebridge.cpp | 5 +- src/resource/ResourceManager.cpp | 8 +- src/resource/ResourceManager.h | 3 +- src/resource/archive/Archive.cpp | 12 +- src/resource/archive/Archive.h | 2 + src/resource/archive/ArchiveManager.cpp | 30 +- src/resource/archive/ArchiveManager.h | 6 +- src/resource/archive/OtrArchive.cpp | 7 +- src/window/gui/GameOverlay.cpp | 350 ++++++++++++------------ 10 files changed, 226 insertions(+), 203 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index d8a90f82b..960a92bbf 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -198,9 +198,11 @@ void Context::InitResourceManager(const std::vector& otrFiles, paths.push_back(mMainPath); paths.push_back(mPatchesPath); - mResourceManager = std::make_shared(paths, validHashes, reservedThreadCount); + mResourceManager = std::make_shared(); + GetResourceManager()->Init(paths, validHashes, reservedThreadCount); } else { - mResourceManager = std::make_shared(otrFiles, validHashes, reservedThreadCount); + mResourceManager = std::make_shared(); + GetResourceManager()->Init(otrFiles, validHashes, reservedThreadCount); } if (!GetResourceManager()->DidLoadSuccessfully()) { diff --git a/src/public/bridge/resourcebridge.cpp b/src/public/bridge/resourcebridge.cpp index a54444ef3..594871439 100644 --- a/src/public/bridge/resourcebridge.cpp +++ b/src/public/bridge/resourcebridge.cpp @@ -26,9 +26,8 @@ uint64_t ResourceGetCrcByName(const char* name) { } const char* ResourceGetNameByCrc(uint64_t crc) { - const std::string& hashStr = - LUS::Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(crc); - return hashStr == "" ? hashStr.c_str() : nullptr; + const std::string* hashStr = LUS::Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(crc); + return hashStr != nullptr ? hashStr->c_str() : nullptr; } size_t ResourceGetSizeByName(const char* name) { diff --git a/src/resource/ResourceManager.cpp b/src/resource/ResourceManager.cpp index 7740a49bf..0ae2ce3d0 100644 --- a/src/resource/ResourceManager.cpp +++ b/src/resource/ResourceManager.cpp @@ -15,10 +15,14 @@ extern bool SFileCheckWildCard(const char* szString, const char* szWildCard); namespace LUS { -ResourceManager::ResourceManager(const std::vector& otrFiles, +ResourceManager::ResourceManager() { +} + +void ResourceManager::Init(const std::vector& otrFiles, const std::unordered_set& validHashes, int32_t reservedThreadCount) { mResourceLoader = std::make_shared(); - mArchiveManager = std::make_shared(otrFiles, validHashes); + mArchiveManager = std::make_shared(); + GetArchiveManager()->Init(otrFiles, validHashes); #if defined(__SWITCH__) || defined(__WIIU__) size_t threadCount = 1; #else diff --git a/src/resource/ResourceManager.h b/src/resource/ResourceManager.h index 1eb8076bc..9feb7ff18 100644 --- a/src/resource/ResourceManager.h +++ b/src/resource/ResourceManager.h @@ -21,7 +21,8 @@ class ResourceManager { typedef enum class ResourceLoadError { None, NotCached, NotFound } ResourceLoadError; public: - ResourceManager(const std::vector& otrFiles, const std::unordered_set& validHashes, + ResourceManager(); + void Init(const std::vector& otrFiles, const std::unordered_set& validHashes, int32_t reservedThreadCount = 1); ~ResourceManager(); diff --git a/src/resource/archive/Archive.cpp b/src/resource/archive/Archive.cpp index 7b644d113..9a6fc0f4d 100644 --- a/src/resource/archive/Archive.cpp +++ b/src/resource/archive/Archive.cpp @@ -15,7 +15,7 @@ extern bool SFileCheckWildCard(const char* szString, const char* szWildCard); namespace LUS { -Archive::Archive(const std::string& path) : mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) { +Archive::Archive(const std::string& path) : mHasGameVersion(false), mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) { mHashes = std::make_shared>(); } @@ -29,6 +29,7 @@ void Archive::Load() { auto t = LoadFileRaw("version"); bool isGameVersionValid = false; if (t != nullptr && t->IsLoaded) { + mHasGameVersion = true; auto stream = std::make_shared(t->Buffer->data(), t->Buffer->size()); auto reader = std::make_shared(stream); LUS::Endianness endianness = (Endianness)reader->ReadUByte(); @@ -42,7 +43,7 @@ void Archive::Load() { } } - SetLoaded(opened && isGameVersionValid); + SetLoaded(opened && (!mHasGameVersion || isGameVersionValid)); if (!IsLoaded()) { Unload(); @@ -76,6 +77,10 @@ bool Archive::HasFile(uint64_t hash) { return mHashes->count(hash) > 0; } +bool Archive::HasGameVersion() { + return mHasGameVersion; +} + uint32_t Archive::GetGameVersion() { return mGameVersion; } @@ -156,7 +161,8 @@ std::shared_ptr Archive::LoadFile(const std::string& filePath) { } std::shared_ptr Archive::LoadFile(uint64_t hash) { - return LoadFile(Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash)); + const std::string& filePath = *Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash); + return LoadFile(filePath); } std::shared_ptr Archive::CreateDefaultResourceInitData() { diff --git a/src/resource/archive/Archive.h b/src/resource/archive/Archive.h index 17a53df6a..a0c32d0d4 100644 --- a/src/resource/archive/Archive.h +++ b/src/resource/archive/Archive.h @@ -29,6 +29,7 @@ class Archive { std::shared_ptr> ListFiles(const std::string& filter); bool HasFile(const std::string& filePath); bool HasFile(uint64_t hash); + bool HasGameVersion(); uint32_t GetGameVersion(); const std::string& GetPath(); bool IsLoaded(); @@ -53,6 +54,7 @@ class Archive { static std::shared_ptr CreateDefaultResourceInitData(); bool mIsLoaded; + bool mHasGameVersion; uint32_t mGameVersion; std::string mPath; std::shared_ptr> mHashes; diff --git a/src/resource/archive/ArchiveManager.cpp b/src/resource/archive/ArchiveManager.cpp index 4eedb6800..9307bbfd8 100644 --- a/src/resource/archive/ArchiveManager.cpp +++ b/src/resource/archive/ArchiveManager.cpp @@ -15,15 +15,16 @@ extern bool SFileCheckWildCard(const char* szString, const char* szWildCard); namespace LUS { -ArchiveManager::ArchiveManager() : ArchiveManager(std::vector()) { +ArchiveManager::ArchiveManager() { } -ArchiveManager::ArchiveManager(const std::vector& archivePaths) : ArchiveManager(archivePaths, {}) { +void ArchiveManager::Init(const std::vector& archivePaths) { + Init(archivePaths, {}); } -ArchiveManager::ArchiveManager(const std::vector& archivePaths, - const std::unordered_set& validGameVersions) - : mValidGameVersions(validGameVersions) { +void ArchiveManager::Init(const std::vector& archivePaths, + const std::unordered_set& validGameVersions) { + mValidGameVersions = validGameVersions; auto archives = GetArchiveListInPaths(archivePaths); for (const auto archive : archives) { AddArchive(archive); @@ -39,6 +40,10 @@ bool ArchiveManager::IsArchiveLoaded() { } std::shared_ptr ArchiveManager::LoadFile(const std::string& filePath) { + if (filePath == "") { + return nullptr; + } + const auto archive = mFileToArchive[CRC64(filePath.c_str())]; if (archive == nullptr) { return nullptr; @@ -105,9 +110,9 @@ void ArchiveManager::SetArchives(const std::vector>& ar } } -const std::string& ArchiveManager::HashToString(uint64_t hash) { +const std::string* ArchiveManager::HashToString(uint64_t hash) const { auto it = mHashes.find(hash); - return it != mHashes.end() ? it->second : ""; + return it != mHashes.end() ? &it->second : nullptr; } std::vector ArchiveManager::GetArchiveListInPaths(const std::vector& archivePaths) { @@ -148,6 +153,7 @@ std::shared_ptr ArchiveManager::AddArchive(const std::string& archivePa archive = dynamic_pointer_cast(std::make_shared(archivePath)); } else if (StringHelper::IEquals(extension, ".otr") || StringHelper::IEquals(extension, ".mpq")) { archive = dynamic_pointer_cast(std::make_shared(archivePath)); + archive->Load(); } else { // Not recognized file extension, trying with o2r SPDLOG_WARN("File extension \"{}\" not recognized, trying to create an o2r archive.", extension); @@ -172,12 +178,12 @@ std::shared_ptr ArchiveManager::AddArchive(std::shared_ptr arc SPDLOG_INFO("Adding Archive {} to Archive Manager", archive->GetPath()); mArchives.push_back(archive); - mGameVersions.push_back(archive->GetGameVersion()); + if (archive->HasGameVersion()) { + mGameVersions.push_back(archive->GetGameVersion()); + } const auto fileList = archive->ListFiles(); - for (size_t i = 0; i < fileList->size(); i++) { - const auto file = fileList->operator[](i); - const auto hash = CRC64(file.c_str()); - mHashes[hash] = file; + for (auto& [hash, filename]: *fileList.get()) { + mHashes[hash] = filename; mFileToArchive[hash] = archive; } return archive; diff --git a/src/resource/archive/ArchiveManager.h b/src/resource/archive/ArchiveManager.h index ff02a1177..8b193d7db 100644 --- a/src/resource/archive/ArchiveManager.h +++ b/src/resource/archive/ArchiveManager.h @@ -14,8 +14,8 @@ class Archive; class ArchiveManager { public: ArchiveManager(); - ArchiveManager(const std::vector& archivePaths); - ArchiveManager(const std::vector& archivePaths, const std::unordered_set& validGameVersions); + void Init(const std::vector& archivePaths); + void Init(const std::vector& archivePaths, const std::unordered_set& validGameVersions); ~ArchiveManager(); bool IsArchiveLoaded(); @@ -28,7 +28,7 @@ class ArchiveManager { std::vector GetGameVersions(); std::vector> GetArchives(); void SetArchives(const std::vector>& archives); - const std::string& HashToString(uint64_t hash); + const std::string* HashToString(uint64_t hash) const; bool IsGameVersionValid(uint32_t gameVersion); protected: diff --git a/src/resource/archive/OtrArchive.cpp b/src/resource/archive/OtrArchive.cpp index 1d4dcb1d4..877459327 100644 --- a/src/resource/archive/OtrArchive.cpp +++ b/src/resource/archive/OtrArchive.cpp @@ -24,9 +24,11 @@ std::shared_ptr OtrArchive::LoadFileRaw(const std::string& filePath) { } auto fileToLoad = std::make_shared(); + fileToLoad->InitData = std::make_shared(); fileToLoad->InitData->Path = filePath; DWORD fileSize = SFileGetFileSize(fileHandle, 0); DWORD readBytes; + fileToLoad->Buffer = std::make_shared>(); fileToLoad->Buffer->resize(fileSize); bool readFileSuccess = SFileReadFile(fileHandle, fileToLoad->Buffer->data(), fileSize, &readBytes, NULL); @@ -52,7 +54,8 @@ std::shared_ptr OtrArchive::LoadFileRaw(const std::string& filePath) { } std::shared_ptr OtrArchive::LoadFileRaw(uint64_t hash) { - return LoadFileRaw(Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash)); + const std::string& filePath = *Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash); + return LoadFileRaw(filePath); } std::shared_ptr OtrArchive::LoadFileMeta(const std::string& filePath) { @@ -67,7 +70,7 @@ bool OtrArchive::LoadRaw() { const bool opened = SFileOpenArchive(GetPath().c_str(), 0, MPQ_OPEN_READ_ONLY, &mHandle); if (opened) { SPDLOG_INFO("Opened mpq file \"{}\"", GetPath()); - SFileCloseArchive(mHandle); + // SFileCloseArchive(mHandle); } else { SPDLOG_ERROR("Failed to load mpq file \"{}\"", GetPath()); mHandle = nullptr; diff --git a/src/window/gui/GameOverlay.cpp b/src/window/gui/GameOverlay.cpp index 4349a6dc3..1067cffe4 100644 --- a/src/window/gui/GameOverlay.cpp +++ b/src/window/gui/GameOverlay.cpp @@ -16,213 +16,213 @@ GameOverlay::~GameOverlay() { } void GameOverlay::LoadFont(const std::string& name, const std::string& path, float fontSize) { - ImGuiIO& io = ImGui::GetIO(); - std::shared_ptr font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFile(path); - if (font->IsLoaded) { - // TODO: Nothing is ever unloading the font or this fontData array. - char* fontData = new char[font->Buffer->size()]; - memcpy(fontData, font->Buffer->data(), font->Buffer->size()); - mFonts[name] = io.Fonts->AddFontFromMemoryTTF(fontData, font->Buffer->size(), fontSize); - } + // ImGuiIO& io = ImGui::GetIO(); + // std::shared_ptr font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFile(path); + // if (font->IsLoaded) { + // // TODO: Nothing is ever unloading the font or this fontData array. + // char* fontData = new char[font->Buffer->size()]; + // memcpy(fontData, font->Buffer->data(), font->Buffer->size()); + // mFonts[name] = io.Fonts->AddFontFromMemoryTTF(fontData, font->Buffer->size(), fontSize); + // } } void GameOverlay::TextDraw(float x, float y, bool shadow, ImVec4 color, const char* fmt, ...) { - char buf[1024]; - va_list args; - va_start(args, fmt); - vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); - buf[IM_ARRAYSIZE(buf) - 1] = 0; - va_end(args); - - ImGui::PushStyleColor(ImGuiCol_Text, color); - ImGui::PushFont(mFonts[mCurrentFont]); - if (shadow) { - ImGui::SetCursorPos(ImVec2(x + 1, y + 1)); - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(.0f, .0f, .0f, color.w)); - ImGui::Text(buf, args); - } - ImGui::PopStyleColor(); - ImGui::SetCursorPos(ImVec2(x, y)); - ImGui::Text(buf, args); - ImGui::PopFont(); - ImGui::PopStyleColor(); + // char buf[1024]; + // va_list args; + // va_start(args, fmt); + // vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); + // buf[IM_ARRAYSIZE(buf) - 1] = 0; + // va_end(args); + + // ImGui::PushStyleColor(ImGuiCol_Text, color); + // ImGui::PushFont(mFonts[mCurrentFont]); + // if (shadow) { + // ImGui::SetCursorPos(ImVec2(x + 1, y + 1)); + // ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(.0f, .0f, .0f, color.w)); + // ImGui::Text(buf, args); + // } + // ImGui::PopStyleColor(); + // ImGui::SetCursorPos(ImVec2(x, y)); + // ImGui::Text(buf, args); + // ImGui::PopFont(); + // ImGui::PopStyleColor(); } void GameOverlay::TextDrawNotification(float duration, bool shadow, const char* fmt, ...) { - char buf[1024]; - va_list args; - va_start(args, fmt); - vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); - buf[IM_ARRAYSIZE(buf) - 1] = 0; - va_end(args); - mRegisteredOverlays[StringHelper::Sprintf("NotificationID:%d%d", rand(), mRegisteredOverlays.size())] = - Overlay({ OverlayType::NOTIFICATION, buf, duration, duration }); - mNeedsCleanup = true; + // char buf[1024]; + // va_list args; + // va_start(args, fmt); + // vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); + // buf[IM_ARRAYSIZE(buf) - 1] = 0; + // va_end(args); + // mRegisteredOverlays[StringHelper::Sprintf("NotificationID:%d%d", rand(), mRegisteredOverlays.size())] = + // Overlay({ OverlayType::NOTIFICATION, buf, duration, duration }); + // mNeedsCleanup = true; } void GameOverlay::ClearNotifications() { - for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { - if (it->second.Type == OverlayType::NOTIFICATION) { - it = mRegisteredOverlays.erase(it); - } else { - ++it; - } - } + // for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { + // if (it->second.Type == OverlayType::NOTIFICATION) { + // it = mRegisteredOverlays.erase(it); + // } else { + // ++it; + // } + // } } void GameOverlay::CleanupNotifications() { - if (!mNeedsCleanup) { - return; - } - - for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { - if (it->second.Type == OverlayType::NOTIFICATION && it->second.duration <= 0.0f) { - it = mRegisteredOverlays.erase(it); - } else { - ++it; - } - } - mNeedsCleanup = false; + // if (!mNeedsCleanup) { + // return; + // } + + // for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { + // if (it->second.Type == OverlayType::NOTIFICATION && it->second.duration <= 0.0f) { + // it = mRegisteredOverlays.erase(it); + // } else { + // ++it; + // } + // } + // mNeedsCleanup = false; } float GameOverlay::GetScreenWidth() { - const ImGuiViewport* viewport = ImGui::GetMainViewport(); - return viewport->Size.x; + // const ImGuiViewport* viewport = ImGui::GetMainViewport(); + // return viewport->Size.x; } float GameOverlay::GetScreenHeight() { - const ImGuiViewport* viewport = ImGui::GetMainViewport(); - return viewport->Size.y; + // const ImGuiViewport* viewport = ImGui::GetMainViewport(); + // return viewport->Size.y; } float GameOverlay::GetStringWidth(const char* text) { - return CalculateTextSize(text).x; + // return CalculateTextSize(text).x; } ImVec2 GameOverlay::CalculateTextSize(const char* text, const char* textEnd, bool shortenText, float wrapWidth) { - ImGuiContext& g = *GImGui; - - const char* textDisplayEnd; - if (shortenText) { - textDisplayEnd = ImGui::FindRenderedTextEnd(text, textEnd); // Hide anything after a '##' string - } else { - textDisplayEnd = textEnd; - } - - ImFont* font = mCurrentFont == "Default" ? g.Font : mFonts[mCurrentFont]; - const float fontSize = font->FontSize; - if (text == textDisplayEnd) { - return ImVec2(0.0f, fontSize); - } - ImVec2 textSize = font->CalcTextSizeA(fontSize, FLT_MAX, wrapWidth, text, textDisplayEnd, NULL); - - // Round - // FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out. - // FIXME: Investigate using ceilf or e.g. - // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c - // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html - textSize.x = IM_FLOOR(textSize.x + 0.99999f); - - return textSize; + // ImGuiContext& g = *GImGui; + + // const char* textDisplayEnd; + // if (shortenText) { + // textDisplayEnd = ImGui::FindRenderedTextEnd(text, textEnd); // Hide anything after a '##' string + // } else { + // textDisplayEnd = textEnd; + // } + + // ImFont* font = mCurrentFont == "Default" ? g.Font : mFonts[mCurrentFont]; + // const float fontSize = font->FontSize; + // if (text == textDisplayEnd) { + // return ImVec2(0.0f, fontSize); + // } + // ImVec2 textSize = font->CalcTextSizeA(fontSize, FLT_MAX, wrapWidth, text, textDisplayEnd, NULL); + + // // Round + // // FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out. + // // FIXME: Investigate using ceilf or e.g. + // // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c + // // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html + // textSize.x = IM_FLOOR(textSize.x + 0.99999f); + + // return textSize; } void GameOverlay::Init() { - LoadFont("Press Start 2P", "fonts/PressStart2P-Regular.ttf", 12.0f); - LoadFont("Fipps", "fonts/Fipps-Regular.otf", 32.0f); - const std::string defaultFont = mFonts.begin()->first; - if (!mFonts.empty()) { - const std::string font = CVarGetString("gOverlayFont", defaultFont.c_str()); - for (auto& [name, _] : mFonts) { - if (font.starts_with(name)) { - mCurrentFont = name; - break; - } - mCurrentFont = defaultFont; - } - } + // LoadFont("Press Start 2P", "fonts/PressStart2P-Regular.ttf", 12.0f); + // LoadFont("Fipps", "fonts/Fipps-Regular.otf", 32.0f); + // const std::string defaultFont = mFonts.begin()->first; + // if (!mFonts.empty()) { + // const std::string font = CVarGetString("gOverlayFont", defaultFont.c_str()); + // for (auto& [name, _] : mFonts) { + // if (font.starts_with(name)) { + // mCurrentFont = name; + // break; + // } + // mCurrentFont = defaultFont; + // } + // } } void GameOverlay::DrawSettings() { - ImGui::Text("Overlays Text Font"); - if (ImGui::BeginCombo("##TextFont", mCurrentFont.c_str())) { - for (auto& [name, font] : mFonts) { - if (ImGui::Selectable(name.c_str(), name == mCurrentFont)) { - mCurrentFont = name; - CVarSetString("gOverlayFont", name.c_str()); - LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); - } - } - ImGui::EndCombo(); - } + // ImGui::Text("Overlays Text Font"); + // if (ImGui::BeginCombo("##TextFont", mCurrentFont.c_str())) { + // for (auto& [name, font] : mFonts) { + // if (ImGui::Selectable(name.c_str(), name == mCurrentFont)) { + // mCurrentFont = name; + // CVarSetString("gOverlayFont", name.c_str()); + // LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); + // } + // } + // ImGui::EndCombo(); + // } } void GameOverlay::Draw() { - const ImGuiViewport* viewport = ImGui::GetMainViewport(); - - ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); - ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always); - ImGui::Begin("GameOverlay", nullptr, - ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | - ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | - ImGuiWindowFlags_NoInputs); - - CleanupNotifications(); - - float textY = 50; - float notY = 0; - - for (auto& [key, overlay] : mRegisteredOverlays) { - - if (overlay.Type == OverlayType::TEXT) { - const char* text = overlay.Value.c_str(); - const auto var = CVarGet(text); - ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - - switch (var->Type) { - case ConsoleVariableType::Float: - - TextDraw(30, textY, true, color, "%s %.2f", text, var->Float); - break; - case ConsoleVariableType::Integer: - TextDraw(30, textY, true, color, "%s %d", text, var->Integer); - break; - case ConsoleVariableType::String: - TextDraw(30, textY, true, color, "%s %s", text, var->String.c_str()); - break; - case ConsoleVariableType::Color: - TextDraw(30, textY, true, color, "%s (%u, %u, %u, %u)", text, var->Color.r, var->Color.g, - var->Color.b, var->Color.a); - break; - case ConsoleVariableType::Color24: - TextDraw(30, textY, true, color, "%s (%u, %u, %u)", text, var->Color24.r, var->Color24.g, - var->Color24.b); - break; - } - - free((void*)text); - textY += 30; - } - - if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0) { - const char* text = overlay.Value.c_str(); - const float duration = overlay.duration / overlay.fadeTime; - - const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration); -#if defined(__WIIU__) || defined(__ANDROID__) - const float textWidth = GetStringWidth(overlay.Value.c_str()) * 2.0f; - const float textOffset = 40.0f * 2.0f; -#else - const float textWidth = GetStringWidth(overlay.Value.c_str()); - const float textOffset = 40.0f; -#endif - - TextDraw(GetScreenWidth() - textWidth - textOffset, GetScreenHeight() - textOffset - notY, true, color, - text); - notY += 30; - overlay.duration -= .05f; - } - } - - ImGui::End(); +// const ImGuiViewport* viewport = ImGui::GetMainViewport(); + +// ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); +// ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always); +// ImGui::Begin("GameOverlay", nullptr, +// ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | +// ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | +// ImGuiWindowFlags_NoInputs); + +// CleanupNotifications(); + +// float textY = 50; +// float notY = 0; + +// for (auto& [key, overlay] : mRegisteredOverlays) { + +// if (overlay.Type == OverlayType::TEXT) { +// const char* text = overlay.Value.c_str(); +// const auto var = CVarGet(text); +// ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + +// switch (var->Type) { +// case ConsoleVariableType::Float: + +// TextDraw(30, textY, true, color, "%s %.2f", text, var->Float); +// break; +// case ConsoleVariableType::Integer: +// TextDraw(30, textY, true, color, "%s %d", text, var->Integer); +// break; +// case ConsoleVariableType::String: +// TextDraw(30, textY, true, color, "%s %s", text, var->String.c_str()); +// break; +// case ConsoleVariableType::Color: +// TextDraw(30, textY, true, color, "%s (%u, %u, %u, %u)", text, var->Color.r, var->Color.g, +// var->Color.b, var->Color.a); +// break; +// case ConsoleVariableType::Color24: +// TextDraw(30, textY, true, color, "%s (%u, %u, %u)", text, var->Color24.r, var->Color24.g, +// var->Color24.b); +// break; +// } + +// free((void*)text); +// textY += 30; +// } + +// if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0) { +// const char* text = overlay.Value.c_str(); +// const float duration = overlay.duration / overlay.fadeTime; + +// const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration); +// #if defined(__WIIU__) || defined(__ANDROID__) +// const float textWidth = GetStringWidth(overlay.Value.c_str()) * 2.0f; +// const float textOffset = 40.0f * 2.0f; +// #else +// const float textWidth = GetStringWidth(overlay.Value.c_str()); +// const float textOffset = 40.0f; +// #endif + +// TextDraw(GetScreenWidth() - textWidth - textOffset, GetScreenHeight() - textOffset - notY, true, color, +// text); +// notY += 30; +// overlay.duration -= .05f; +// } +// } + +// ImGui::End(); } } // namespace LUS From 1dd6db9083b998b142a44b70c290815cc8199882 Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 03:27:57 -0500 Subject: [PATCH 02/10] clang format --- src/public/bridge/resourcebridge.cpp | 3 +- src/resource/ResourceManager.cpp | 4 +- src/resource/ResourceManager.h | 2 +- src/resource/archive/Archive.cpp | 6 +- src/resource/archive/ArchiveManager.cpp | 4 +- src/resource/archive/OtrArchive.cpp | 3 +- src/window/gui/GameOverlay.cpp | 135 ++++++++++++------------ 7 files changed, 81 insertions(+), 76 deletions(-) diff --git a/src/public/bridge/resourcebridge.cpp b/src/public/bridge/resourcebridge.cpp index 594871439..09a85f145 100644 --- a/src/public/bridge/resourcebridge.cpp +++ b/src/public/bridge/resourcebridge.cpp @@ -26,7 +26,8 @@ uint64_t ResourceGetCrcByName(const char* name) { } const char* ResourceGetNameByCrc(uint64_t crc) { - const std::string* hashStr = LUS::Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(crc); + const std::string* hashStr = + LUS::Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(crc); return hashStr != nullptr ? hashStr->c_str() : nullptr; } diff --git a/src/resource/ResourceManager.cpp b/src/resource/ResourceManager.cpp index 0ae2ce3d0..e6dd0e11f 100644 --- a/src/resource/ResourceManager.cpp +++ b/src/resource/ResourceManager.cpp @@ -18,8 +18,8 @@ namespace LUS { ResourceManager::ResourceManager() { } -void ResourceManager::Init(const std::vector& otrFiles, - const std::unordered_set& validHashes, int32_t reservedThreadCount) { +void ResourceManager::Init(const std::vector& otrFiles, const std::unordered_set& validHashes, + int32_t reservedThreadCount) { mResourceLoader = std::make_shared(); mArchiveManager = std::make_shared(); GetArchiveManager()->Init(otrFiles, validHashes); diff --git a/src/resource/ResourceManager.h b/src/resource/ResourceManager.h index 9feb7ff18..7b48dd2a9 100644 --- a/src/resource/ResourceManager.h +++ b/src/resource/ResourceManager.h @@ -23,7 +23,7 @@ class ResourceManager { public: ResourceManager(); void Init(const std::vector& otrFiles, const std::unordered_set& validHashes, - int32_t reservedThreadCount = 1); + int32_t reservedThreadCount = 1); ~ResourceManager(); bool DidLoadSuccessfully(); diff --git a/src/resource/archive/Archive.cpp b/src/resource/archive/Archive.cpp index 9a6fc0f4d..433a31b7f 100644 --- a/src/resource/archive/Archive.cpp +++ b/src/resource/archive/Archive.cpp @@ -15,7 +15,8 @@ extern bool SFileCheckWildCard(const char* szString, const char* szWildCard); namespace LUS { -Archive::Archive(const std::string& path) : mHasGameVersion(false), mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) { +Archive::Archive(const std::string& path) + : mHasGameVersion(false), mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) { mHashes = std::make_shared>(); } @@ -161,7 +162,8 @@ std::shared_ptr Archive::LoadFile(const std::string& filePath) { } std::shared_ptr Archive::LoadFile(uint64_t hash) { - const std::string& filePath = *Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash); + const std::string& filePath = + *Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash); return LoadFile(filePath); } diff --git a/src/resource/archive/ArchiveManager.cpp b/src/resource/archive/ArchiveManager.cpp index 9307bbfd8..170aa379f 100644 --- a/src/resource/archive/ArchiveManager.cpp +++ b/src/resource/archive/ArchiveManager.cpp @@ -23,7 +23,7 @@ void ArchiveManager::Init(const std::vector& archivePaths) { } void ArchiveManager::Init(const std::vector& archivePaths, - const std::unordered_set& validGameVersions) { + const std::unordered_set& validGameVersions) { mValidGameVersions = validGameVersions; auto archives = GetArchiveListInPaths(archivePaths); for (const auto archive : archives) { @@ -182,7 +182,7 @@ std::shared_ptr ArchiveManager::AddArchive(std::shared_ptr arc mGameVersions.push_back(archive->GetGameVersion()); } const auto fileList = archive->ListFiles(); - for (auto& [hash, filename]: *fileList.get()) { + for (auto& [hash, filename] : *fileList.get()) { mHashes[hash] = filename; mFileToArchive[hash] = archive; } diff --git a/src/resource/archive/OtrArchive.cpp b/src/resource/archive/OtrArchive.cpp index 877459327..1329cddfc 100644 --- a/src/resource/archive/OtrArchive.cpp +++ b/src/resource/archive/OtrArchive.cpp @@ -54,7 +54,8 @@ std::shared_ptr OtrArchive::LoadFileRaw(const std::string& filePath) { } std::shared_ptr OtrArchive::LoadFileRaw(uint64_t hash) { - const std::string& filePath = *Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash); + const std::string& filePath = + *Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash); return LoadFileRaw(filePath); } diff --git a/src/window/gui/GameOverlay.cpp b/src/window/gui/GameOverlay.cpp index 1067cffe4..0adac2b43 100644 --- a/src/window/gui/GameOverlay.cpp +++ b/src/window/gui/GameOverlay.cpp @@ -157,72 +157,73 @@ void GameOverlay::DrawSettings() { } void GameOverlay::Draw() { -// const ImGuiViewport* viewport = ImGui::GetMainViewport(); - -// ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); -// ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always); -// ImGui::Begin("GameOverlay", nullptr, -// ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | -// ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | -// ImGuiWindowFlags_NoInputs); - -// CleanupNotifications(); - -// float textY = 50; -// float notY = 0; - -// for (auto& [key, overlay] : mRegisteredOverlays) { - -// if (overlay.Type == OverlayType::TEXT) { -// const char* text = overlay.Value.c_str(); -// const auto var = CVarGet(text); -// ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - -// switch (var->Type) { -// case ConsoleVariableType::Float: - -// TextDraw(30, textY, true, color, "%s %.2f", text, var->Float); -// break; -// case ConsoleVariableType::Integer: -// TextDraw(30, textY, true, color, "%s %d", text, var->Integer); -// break; -// case ConsoleVariableType::String: -// TextDraw(30, textY, true, color, "%s %s", text, var->String.c_str()); -// break; -// case ConsoleVariableType::Color: -// TextDraw(30, textY, true, color, "%s (%u, %u, %u, %u)", text, var->Color.r, var->Color.g, -// var->Color.b, var->Color.a); -// break; -// case ConsoleVariableType::Color24: -// TextDraw(30, textY, true, color, "%s (%u, %u, %u)", text, var->Color24.r, var->Color24.g, -// var->Color24.b); -// break; -// } - -// free((void*)text); -// textY += 30; -// } - -// if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0) { -// const char* text = overlay.Value.c_str(); -// const float duration = overlay.duration / overlay.fadeTime; - -// const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration); -// #if defined(__WIIU__) || defined(__ANDROID__) -// const float textWidth = GetStringWidth(overlay.Value.c_str()) * 2.0f; -// const float textOffset = 40.0f * 2.0f; -// #else -// const float textWidth = GetStringWidth(overlay.Value.c_str()); -// const float textOffset = 40.0f; -// #endif - -// TextDraw(GetScreenWidth() - textWidth - textOffset, GetScreenHeight() - textOffset - notY, true, color, -// text); -// notY += 30; -// overlay.duration -= .05f; -// } -// } - -// ImGui::End(); + // const ImGuiViewport* viewport = ImGui::GetMainViewport(); + + // ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); + // ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always); + // ImGui::Begin("GameOverlay", nullptr, + // ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | + // ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar | + // ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs); + + // CleanupNotifications(); + + // float textY = 50; + // float notY = 0; + + // for (auto& [key, overlay] : mRegisteredOverlays) { + + // if (overlay.Type == OverlayType::TEXT) { + // const char* text = overlay.Value.c_str(); + // const auto var = CVarGet(text); + // ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + + // switch (var->Type) { + // case ConsoleVariableType::Float: + + // TextDraw(30, textY, true, color, "%s %.2f", text, var->Float); + // break; + // case ConsoleVariableType::Integer: + // TextDraw(30, textY, true, color, "%s %d", text, var->Integer); + // break; + // case ConsoleVariableType::String: + // TextDraw(30, textY, true, color, "%s %s", text, var->String.c_str()); + // break; + // case ConsoleVariableType::Color: + // TextDraw(30, textY, true, color, "%s (%u, %u, %u, %u)", text, var->Color.r, var->Color.g, + // var->Color.b, var->Color.a); + // break; + // case ConsoleVariableType::Color24: + // TextDraw(30, textY, true, color, "%s (%u, %u, %u)", text, var->Color24.r, var->Color24.g, + // var->Color24.b); + // break; + // } + + // free((void*)text); + // textY += 30; + // } + + // if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0) { + // const char* text = overlay.Value.c_str(); + // const float duration = overlay.duration / overlay.fadeTime; + + // const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration); + // #if defined(__WIIU__) || defined(__ANDROID__) + // const float textWidth = GetStringWidth(overlay.Value.c_str()) * 2.0f; + // const float textOffset = 40.0f * 2.0f; + // #else + // const float textWidth = GetStringWidth(overlay.Value.c_str()); + // const float textOffset = 40.0f; + // #endif + + // TextDraw(GetScreenWidth() - textWidth - textOffset, GetScreenHeight() - textOffset - notY, true, + // color, + // text); + // notY += 30; + // overlay.duration -= .05f; + // } + // } + + // ImGui::End(); } } // namespace LUS From 2b6079052d780cc70ec60706870bdf766a122c9a Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 03:38:56 -0500 Subject: [PATCH 03/10] windows build --- src/window/gui/GameOverlay.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/window/gui/GameOverlay.cpp b/src/window/gui/GameOverlay.cpp index 0adac2b43..67b71a315 100644 --- a/src/window/gui/GameOverlay.cpp +++ b/src/window/gui/GameOverlay.cpp @@ -86,20 +86,24 @@ void GameOverlay::CleanupNotifications() { } float GameOverlay::GetScreenWidth() { + return 0; // const ImGuiViewport* viewport = ImGui::GetMainViewport(); // return viewport->Size.x; } float GameOverlay::GetScreenHeight() { + return 0; // const ImGuiViewport* viewport = ImGui::GetMainViewport(); // return viewport->Size.y; } float GameOverlay::GetStringWidth(const char* text) { + return 0; // return CalculateTextSize(text).x; } ImVec2 GameOverlay::CalculateTextSize(const char* text, const char* textEnd, bool shortenText, float wrapWidth) { + return ImVec2(0, 0); // ImGuiContext& g = *GImGui; // const char* textDisplayEnd; From 070c793ff1bb5b30f3f947b3709bee4b03ca861e Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:02:10 -0500 Subject: [PATCH 04/10] pr comments --- src/resource/archive/ArchiveManager.cpp | 2 +- src/resource/archive/OtrArchive.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/resource/archive/ArchiveManager.cpp b/src/resource/archive/ArchiveManager.cpp index 170aa379f..0605b73b0 100644 --- a/src/resource/archive/ArchiveManager.cpp +++ b/src/resource/archive/ArchiveManager.cpp @@ -153,13 +153,13 @@ std::shared_ptr ArchiveManager::AddArchive(const std::string& archivePa archive = dynamic_pointer_cast(std::make_shared(archivePath)); } else if (StringHelper::IEquals(extension, ".otr") || StringHelper::IEquals(extension, ".mpq")) { archive = dynamic_pointer_cast(std::make_shared(archivePath)); - archive->Load(); } else { // Not recognized file extension, trying with o2r SPDLOG_WARN("File extension \"{}\" not recognized, trying to create an o2r archive.", extension); archive = std::make_shared(archivePath); } + archive->Load(); return AddArchive(archive); } diff --git a/src/resource/archive/OtrArchive.cpp b/src/resource/archive/OtrArchive.cpp index 1329cddfc..7b6bc315a 100644 --- a/src/resource/archive/OtrArchive.cpp +++ b/src/resource/archive/OtrArchive.cpp @@ -71,7 +71,6 @@ bool OtrArchive::LoadRaw() { const bool opened = SFileOpenArchive(GetPath().c_str(), 0, MPQ_OPEN_READ_ONLY, &mHandle); if (opened) { SPDLOG_INFO("Opened mpq file \"{}\"", GetPath()); - // SFileCloseArchive(mHandle); } else { SPDLOG_ERROR("Failed to load mpq file \"{}\"", GetPath()); mHandle = nullptr; From e21a63f1e78d91fb60c9cd80b669dacffc0e126d Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:02:50 -0500 Subject: [PATCH 05/10] return false --- src/resource/archive/OtrArchive.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/resource/archive/OtrArchive.cpp b/src/resource/archive/OtrArchive.cpp index 7b6bc315a..62864246e 100644 --- a/src/resource/archive/OtrArchive.cpp +++ b/src/resource/archive/OtrArchive.cpp @@ -74,6 +74,7 @@ bool OtrArchive::LoadRaw() { } else { SPDLOG_ERROR("Failed to load mpq file \"{}\"", GetPath()); mHandle = nullptr; + return false; } // Generate the file list by reading the list file. From c53e6b1c62d23a2361f1686b28a5d8da2205a60c Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:06:16 -0500 Subject: [PATCH 06/10] uncomment --- src/window/gui/GameOverlay.cpp | 355 ++++++++++++++++----------------- 1 file changed, 175 insertions(+), 180 deletions(-) diff --git a/src/window/gui/GameOverlay.cpp b/src/window/gui/GameOverlay.cpp index 67b71a315..4349a6dc3 100644 --- a/src/window/gui/GameOverlay.cpp +++ b/src/window/gui/GameOverlay.cpp @@ -16,218 +16,213 @@ GameOverlay::~GameOverlay() { } void GameOverlay::LoadFont(const std::string& name, const std::string& path, float fontSize) { - // ImGuiIO& io = ImGui::GetIO(); - // std::shared_ptr font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFile(path); - // if (font->IsLoaded) { - // // TODO: Nothing is ever unloading the font or this fontData array. - // char* fontData = new char[font->Buffer->size()]; - // memcpy(fontData, font->Buffer->data(), font->Buffer->size()); - // mFonts[name] = io.Fonts->AddFontFromMemoryTTF(fontData, font->Buffer->size(), fontSize); - // } + ImGuiIO& io = ImGui::GetIO(); + std::shared_ptr font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFile(path); + if (font->IsLoaded) { + // TODO: Nothing is ever unloading the font or this fontData array. + char* fontData = new char[font->Buffer->size()]; + memcpy(fontData, font->Buffer->data(), font->Buffer->size()); + mFonts[name] = io.Fonts->AddFontFromMemoryTTF(fontData, font->Buffer->size(), fontSize); + } } void GameOverlay::TextDraw(float x, float y, bool shadow, ImVec4 color, const char* fmt, ...) { - // char buf[1024]; - // va_list args; - // va_start(args, fmt); - // vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); - // buf[IM_ARRAYSIZE(buf) - 1] = 0; - // va_end(args); - - // ImGui::PushStyleColor(ImGuiCol_Text, color); - // ImGui::PushFont(mFonts[mCurrentFont]); - // if (shadow) { - // ImGui::SetCursorPos(ImVec2(x + 1, y + 1)); - // ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(.0f, .0f, .0f, color.w)); - // ImGui::Text(buf, args); - // } - // ImGui::PopStyleColor(); - // ImGui::SetCursorPos(ImVec2(x, y)); - // ImGui::Text(buf, args); - // ImGui::PopFont(); - // ImGui::PopStyleColor(); + char buf[1024]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); + buf[IM_ARRAYSIZE(buf) - 1] = 0; + va_end(args); + + ImGui::PushStyleColor(ImGuiCol_Text, color); + ImGui::PushFont(mFonts[mCurrentFont]); + if (shadow) { + ImGui::SetCursorPos(ImVec2(x + 1, y + 1)); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(.0f, .0f, .0f, color.w)); + ImGui::Text(buf, args); + } + ImGui::PopStyleColor(); + ImGui::SetCursorPos(ImVec2(x, y)); + ImGui::Text(buf, args); + ImGui::PopFont(); + ImGui::PopStyleColor(); } void GameOverlay::TextDrawNotification(float duration, bool shadow, const char* fmt, ...) { - // char buf[1024]; - // va_list args; - // va_start(args, fmt); - // vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); - // buf[IM_ARRAYSIZE(buf) - 1] = 0; - // va_end(args); - // mRegisteredOverlays[StringHelper::Sprintf("NotificationID:%d%d", rand(), mRegisteredOverlays.size())] = - // Overlay({ OverlayType::NOTIFICATION, buf, duration, duration }); - // mNeedsCleanup = true; + char buf[1024]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); + buf[IM_ARRAYSIZE(buf) - 1] = 0; + va_end(args); + mRegisteredOverlays[StringHelper::Sprintf("NotificationID:%d%d", rand(), mRegisteredOverlays.size())] = + Overlay({ OverlayType::NOTIFICATION, buf, duration, duration }); + mNeedsCleanup = true; } void GameOverlay::ClearNotifications() { - // for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { - // if (it->second.Type == OverlayType::NOTIFICATION) { - // it = mRegisteredOverlays.erase(it); - // } else { - // ++it; - // } - // } + for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { + if (it->second.Type == OverlayType::NOTIFICATION) { + it = mRegisteredOverlays.erase(it); + } else { + ++it; + } + } } void GameOverlay::CleanupNotifications() { - // if (!mNeedsCleanup) { - // return; - // } - - // for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { - // if (it->second.Type == OverlayType::NOTIFICATION && it->second.duration <= 0.0f) { - // it = mRegisteredOverlays.erase(it); - // } else { - // ++it; - // } - // } - // mNeedsCleanup = false; + if (!mNeedsCleanup) { + return; + } + + for (auto it = mRegisteredOverlays.begin(); it != mRegisteredOverlays.end();) { + if (it->second.Type == OverlayType::NOTIFICATION && it->second.duration <= 0.0f) { + it = mRegisteredOverlays.erase(it); + } else { + ++it; + } + } + mNeedsCleanup = false; } float GameOverlay::GetScreenWidth() { - return 0; - // const ImGuiViewport* viewport = ImGui::GetMainViewport(); - // return viewport->Size.x; + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + return viewport->Size.x; } float GameOverlay::GetScreenHeight() { - return 0; - // const ImGuiViewport* viewport = ImGui::GetMainViewport(); - // return viewport->Size.y; + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + return viewport->Size.y; } float GameOverlay::GetStringWidth(const char* text) { - return 0; - // return CalculateTextSize(text).x; + return CalculateTextSize(text).x; } ImVec2 GameOverlay::CalculateTextSize(const char* text, const char* textEnd, bool shortenText, float wrapWidth) { - return ImVec2(0, 0); - // ImGuiContext& g = *GImGui; - - // const char* textDisplayEnd; - // if (shortenText) { - // textDisplayEnd = ImGui::FindRenderedTextEnd(text, textEnd); // Hide anything after a '##' string - // } else { - // textDisplayEnd = textEnd; - // } - - // ImFont* font = mCurrentFont == "Default" ? g.Font : mFonts[mCurrentFont]; - // const float fontSize = font->FontSize; - // if (text == textDisplayEnd) { - // return ImVec2(0.0f, fontSize); - // } - // ImVec2 textSize = font->CalcTextSizeA(fontSize, FLT_MAX, wrapWidth, text, textDisplayEnd, NULL); - - // // Round - // // FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out. - // // FIXME: Investigate using ceilf or e.g. - // // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c - // // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html - // textSize.x = IM_FLOOR(textSize.x + 0.99999f); - - // return textSize; + ImGuiContext& g = *GImGui; + + const char* textDisplayEnd; + if (shortenText) { + textDisplayEnd = ImGui::FindRenderedTextEnd(text, textEnd); // Hide anything after a '##' string + } else { + textDisplayEnd = textEnd; + } + + ImFont* font = mCurrentFont == "Default" ? g.Font : mFonts[mCurrentFont]; + const float fontSize = font->FontSize; + if (text == textDisplayEnd) { + return ImVec2(0.0f, fontSize); + } + ImVec2 textSize = font->CalcTextSizeA(fontSize, FLT_MAX, wrapWidth, text, textDisplayEnd, NULL); + + // Round + // FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out. + // FIXME: Investigate using ceilf or e.g. + // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c + // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html + textSize.x = IM_FLOOR(textSize.x + 0.99999f); + + return textSize; } void GameOverlay::Init() { - // LoadFont("Press Start 2P", "fonts/PressStart2P-Regular.ttf", 12.0f); - // LoadFont("Fipps", "fonts/Fipps-Regular.otf", 32.0f); - // const std::string defaultFont = mFonts.begin()->first; - // if (!mFonts.empty()) { - // const std::string font = CVarGetString("gOverlayFont", defaultFont.c_str()); - // for (auto& [name, _] : mFonts) { - // if (font.starts_with(name)) { - // mCurrentFont = name; - // break; - // } - // mCurrentFont = defaultFont; - // } - // } + LoadFont("Press Start 2P", "fonts/PressStart2P-Regular.ttf", 12.0f); + LoadFont("Fipps", "fonts/Fipps-Regular.otf", 32.0f); + const std::string defaultFont = mFonts.begin()->first; + if (!mFonts.empty()) { + const std::string font = CVarGetString("gOverlayFont", defaultFont.c_str()); + for (auto& [name, _] : mFonts) { + if (font.starts_with(name)) { + mCurrentFont = name; + break; + } + mCurrentFont = defaultFont; + } + } } void GameOverlay::DrawSettings() { - // ImGui::Text("Overlays Text Font"); - // if (ImGui::BeginCombo("##TextFont", mCurrentFont.c_str())) { - // for (auto& [name, font] : mFonts) { - // if (ImGui::Selectable(name.c_str(), name == mCurrentFont)) { - // mCurrentFont = name; - // CVarSetString("gOverlayFont", name.c_str()); - // LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); - // } - // } - // ImGui::EndCombo(); - // } + ImGui::Text("Overlays Text Font"); + if (ImGui::BeginCombo("##TextFont", mCurrentFont.c_str())) { + for (auto& [name, font] : mFonts) { + if (ImGui::Selectable(name.c_str(), name == mCurrentFont)) { + mCurrentFont = name; + CVarSetString("gOverlayFont", name.c_str()); + LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); + } + } + ImGui::EndCombo(); + } } void GameOverlay::Draw() { - // const ImGuiViewport* viewport = ImGui::GetMainViewport(); - - // ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); - // ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always); - // ImGui::Begin("GameOverlay", nullptr, - // ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | - // ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar | - // ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs); - - // CleanupNotifications(); - - // float textY = 50; - // float notY = 0; - - // for (auto& [key, overlay] : mRegisteredOverlays) { - - // if (overlay.Type == OverlayType::TEXT) { - // const char* text = overlay.Value.c_str(); - // const auto var = CVarGet(text); - // ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - - // switch (var->Type) { - // case ConsoleVariableType::Float: - - // TextDraw(30, textY, true, color, "%s %.2f", text, var->Float); - // break; - // case ConsoleVariableType::Integer: - // TextDraw(30, textY, true, color, "%s %d", text, var->Integer); - // break; - // case ConsoleVariableType::String: - // TextDraw(30, textY, true, color, "%s %s", text, var->String.c_str()); - // break; - // case ConsoleVariableType::Color: - // TextDraw(30, textY, true, color, "%s (%u, %u, %u, %u)", text, var->Color.r, var->Color.g, - // var->Color.b, var->Color.a); - // break; - // case ConsoleVariableType::Color24: - // TextDraw(30, textY, true, color, "%s (%u, %u, %u)", text, var->Color24.r, var->Color24.g, - // var->Color24.b); - // break; - // } - - // free((void*)text); - // textY += 30; - // } - - // if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0) { - // const char* text = overlay.Value.c_str(); - // const float duration = overlay.duration / overlay.fadeTime; - - // const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration); - // #if defined(__WIIU__) || defined(__ANDROID__) - // const float textWidth = GetStringWidth(overlay.Value.c_str()) * 2.0f; - // const float textOffset = 40.0f * 2.0f; - // #else - // const float textWidth = GetStringWidth(overlay.Value.c_str()); - // const float textOffset = 40.0f; - // #endif - - // TextDraw(GetScreenWidth() - textWidth - textOffset, GetScreenHeight() - textOffset - notY, true, - // color, - // text); - // notY += 30; - // overlay.duration -= .05f; - // } - // } - - // ImGui::End(); + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + + ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); + ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always); + ImGui::Begin("GameOverlay", nullptr, + ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoInputs); + + CleanupNotifications(); + + float textY = 50; + float notY = 0; + + for (auto& [key, overlay] : mRegisteredOverlays) { + + if (overlay.Type == OverlayType::TEXT) { + const char* text = overlay.Value.c_str(); + const auto var = CVarGet(text); + ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + + switch (var->Type) { + case ConsoleVariableType::Float: + + TextDraw(30, textY, true, color, "%s %.2f", text, var->Float); + break; + case ConsoleVariableType::Integer: + TextDraw(30, textY, true, color, "%s %d", text, var->Integer); + break; + case ConsoleVariableType::String: + TextDraw(30, textY, true, color, "%s %s", text, var->String.c_str()); + break; + case ConsoleVariableType::Color: + TextDraw(30, textY, true, color, "%s (%u, %u, %u, %u)", text, var->Color.r, var->Color.g, + var->Color.b, var->Color.a); + break; + case ConsoleVariableType::Color24: + TextDraw(30, textY, true, color, "%s (%u, %u, %u)", text, var->Color24.r, var->Color24.g, + var->Color24.b); + break; + } + + free((void*)text); + textY += 30; + } + + if (overlay.Type == OverlayType::NOTIFICATION && overlay.duration > 0) { + const char* text = overlay.Value.c_str(); + const float duration = overlay.duration / overlay.fadeTime; + + const ImVec4 color = ImVec4(1.0f, 1.0f, 1.0f, duration); +#if defined(__WIIU__) || defined(__ANDROID__) + const float textWidth = GetStringWidth(overlay.Value.c_str()) * 2.0f; + const float textOffset = 40.0f * 2.0f; +#else + const float textWidth = GetStringWidth(overlay.Value.c_str()); + const float textOffset = 40.0f; +#endif + + TextDraw(GetScreenWidth() - textWidth - textOffset, GetScreenHeight() - textOffset - notY, true, color, + text); + notY += 30; + overlay.duration -= .05f; + } + } + + ImGui::End(); } } // namespace LUS From 7c02142f9c8e5b4359802b5d3e47e7d244c16c05 Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:10:22 -0500 Subject: [PATCH 07/10] fix fonts --- src/resource/archive/ArchiveManager.cpp | 22 ++++++++++++++++++++++ src/resource/archive/ArchiveManager.h | 2 ++ src/window/gui/GameOverlay.cpp | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/resource/archive/ArchiveManager.cpp b/src/resource/archive/ArchiveManager.cpp index 0605b73b0..9bedc0d3f 100644 --- a/src/resource/archive/ArchiveManager.cpp +++ b/src/resource/archive/ArchiveManager.cpp @@ -61,6 +61,28 @@ std::shared_ptr ArchiveManager::LoadFile(uint64_t hash) { return archive->LoadFile(hash); } +std::shared_ptr ArchiveManager::LoadFileRaw(const std::string& filePath) { + if (filePath == "") { + return nullptr; + } + + const auto archive = mFileToArchive[CRC64(filePath.c_str())]; + if (archive == nullptr) { + return nullptr; + } + + return archive->LoadFileRaw(filePath); +} + +std::shared_ptr ArchiveManager::LoadFileRaw(uint64_t hash) { + const auto archive = mFileToArchive[hash]; + if (archive == nullptr) { + return nullptr; + } + + return archive->LoadFileRaw(hash); +} + bool ArchiveManager::HasFile(const std::string& filePath) { return HasFile(CRC64(filePath.c_str())); } diff --git a/src/resource/archive/ArchiveManager.h b/src/resource/archive/ArchiveManager.h index 8b193d7db..1bcdb4503 100644 --- a/src/resource/archive/ArchiveManager.h +++ b/src/resource/archive/ArchiveManager.h @@ -21,6 +21,8 @@ class ArchiveManager { bool IsArchiveLoaded(); std::shared_ptr LoadFile(const std::string& filePath); std::shared_ptr LoadFile(uint64_t hash); + std::shared_ptr LoadFileRaw(const std::string& filePath); + std::shared_ptr LoadFileRaw(uint64_t hash); bool HasFile(const std::string& filePath); bool HasFile(uint64_t hash); std::shared_ptr> ListFiles(const std::string& filter); diff --git a/src/window/gui/GameOverlay.cpp b/src/window/gui/GameOverlay.cpp index 4349a6dc3..5cbe66d03 100644 --- a/src/window/gui/GameOverlay.cpp +++ b/src/window/gui/GameOverlay.cpp @@ -17,7 +17,7 @@ GameOverlay::~GameOverlay() { void GameOverlay::LoadFont(const std::string& name, const std::string& path, float fontSize) { ImGuiIO& io = ImGui::GetIO(); - std::shared_ptr font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFile(path); + std::shared_ptr font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFileRaw(path); if (font->IsLoaded) { // TODO: Nothing is ever unloading the font or this fontData array. char* fontData = new char[font->Buffer->size()]; From 7eb817d274550e23ac2dc39fe12a06eb6eedb83b Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:12:49 -0500 Subject: [PATCH 08/10] pr comment --- src/resource/archive/OtrArchive.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/resource/archive/OtrArchive.cpp b/src/resource/archive/OtrArchive.cpp index 62864246e..1538a5e71 100644 --- a/src/resource/archive/OtrArchive.cpp +++ b/src/resource/archive/OtrArchive.cpp @@ -28,8 +28,7 @@ std::shared_ptr OtrArchive::LoadFileRaw(const std::string& filePath) { fileToLoad->InitData->Path = filePath; DWORD fileSize = SFileGetFileSize(fileHandle, 0); DWORD readBytes; - fileToLoad->Buffer = std::make_shared>(); - fileToLoad->Buffer->resize(fileSize); + fileToLoad->Buffer = std::make_shared>(fileSize); bool readFileSuccess = SFileReadFile(fileHandle, fileToLoad->Buffer->data(), fileSize, &readBytes, NULL); if (!readFileSuccess) { From 3efa79b3ba9b25475636a11ef61806f5c05bbd42 Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:21:06 -0500 Subject: [PATCH 09/10] init dadon't --- src/resource/archive/OtrArchive.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/resource/archive/OtrArchive.cpp b/src/resource/archive/OtrArchive.cpp index 1538a5e71..8f60ede50 100644 --- a/src/resource/archive/OtrArchive.cpp +++ b/src/resource/archive/OtrArchive.cpp @@ -24,8 +24,6 @@ std::shared_ptr OtrArchive::LoadFileRaw(const std::string& filePath) { } auto fileToLoad = std::make_shared(); - fileToLoad->InitData = std::make_shared(); - fileToLoad->InitData->Path = filePath; DWORD fileSize = SFileGetFileSize(fileHandle, 0); DWORD readBytes; fileToLoad->Buffer = std::make_shared>(fileSize); From 85cba2be7091c372460c3d0961685104095c9806 Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:53:23 -0500 Subject: [PATCH 10/10] raw --- include/libultraship/classes.h | 3 +++ src/resource/archive/Archive.h | 9 +++++---- src/resource/archive/O2rArchive.h | 8 ++++---- src/resource/archive/OtrArchive.h | 8 ++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/libultraship/classes.h b/include/libultraship/classes.h index 51341a055..7d444f05c 100644 --- a/include/libultraship/classes.h +++ b/include/libultraship/classes.h @@ -4,7 +4,10 @@ #ifndef _LIBULTRASHIP_CLASSES_H #define _LIBULTRASHIP_CLASSES_H +#include "resource/archive/ArchiveManager.h" #include "resource/archive/Archive.h" +#include "resource/archive/OtrArchive.h" +#include "resource/archive/O2rArchive.h" #include "resource/ResourceManager.h" #include "Context.h" #include "window/Window.h" diff --git a/src/resource/archive/Archive.h b/src/resource/archive/Archive.h index a0c32d0d4..32991e1bf 100644 --- a/src/resource/archive/Archive.h +++ b/src/resource/archive/Archive.h @@ -34,17 +34,18 @@ class Archive { const std::string& GetPath(); bool IsLoaded(); + virtual bool LoadRaw() = 0; + virtual bool UnloadRaw() = 0; + virtual std::shared_ptr LoadFileRaw(const std::string& filePath) = 0; + virtual std::shared_ptr LoadFileRaw(uint64_t hash) = 0; + protected: static std::shared_ptr ReadResourceInitDataBinary(const std::string& filePath, std::shared_ptr headerReader); static std::shared_ptr ReadResourceInitDataXml(const std::string& filePath, std::shared_ptr document); - virtual std::shared_ptr LoadFileRaw(const std::string& filePath) = 0; - virtual std::shared_ptr LoadFileRaw(uint64_t hash) = 0; virtual std::shared_ptr LoadFileMeta(const std::string& filePath) = 0; virtual std::shared_ptr LoadFileMeta(uint64_t hash) = 0; - virtual bool LoadRaw() = 0; - virtual bool UnloadRaw() = 0; void SetLoaded(bool isLoaded); void SetGameVersion(uint32_t gameVersion); diff --git a/src/resource/archive/O2rArchive.h b/src/resource/archive/O2rArchive.h index e233d4ec7..cc00c34d5 100644 --- a/src/resource/archive/O2rArchive.h +++ b/src/resource/archive/O2rArchive.h @@ -18,15 +18,15 @@ class O2rArchive : virtual public Archive { O2rArchive(const std::string& archivePath); ~O2rArchive(); - protected: + bool LoadRaw(); + bool UnloadRaw(); std::shared_ptr LoadFileRaw(const std::string& filePath); std::shared_ptr LoadFileRaw(uint64_t hash); + + protected: std::shared_ptr LoadFileMeta(const std::string& filePath); std::shared_ptr LoadFileMeta(uint64_t hash); - bool LoadRaw(); - bool UnloadRaw(); - private: }; } // namespace LUS diff --git a/src/resource/archive/OtrArchive.h b/src/resource/archive/OtrArchive.h index a85793636..4fab6e8c9 100644 --- a/src/resource/archive/OtrArchive.h +++ b/src/resource/archive/OtrArchive.h @@ -24,15 +24,15 @@ class OtrArchive : virtual public Archive { OtrArchive(const std::string& archivePath); ~OtrArchive(); - protected: + bool LoadRaw(); + bool UnloadRaw(); std::shared_ptr LoadFileRaw(const std::string& filePath); std::shared_ptr LoadFileRaw(uint64_t hash); + + protected: std::shared_ptr LoadFileMeta(const std::string& filePath); std::shared_ptr LoadFileMeta(uint64_t hash); - bool LoadRaw(); - bool UnloadRaw(); - private: HANDLE mHandle; };