From 81c61692f1d899c1da2d3819ce1e6f1d1dba94b4 Mon Sep 17 00:00:00 2001 From: d1str4ught <> Date: Mon, 22 Sep 2025 01:43:01 +0200 Subject: [PATCH] LoadImageFromFile for CImageBox --- src/EterPythonLib/PythonWindow.cpp | 9 ++++++ src/EterPythonLib/PythonWindow.h | 1 + .../PythonWindowManagerModule.cpp | 16 ++++++++++ src/PackLib/PackManager.cpp | 31 ++++++++++++++++--- src/PackLib/PackManager.h | 4 +++ 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/EterPythonLib/PythonWindow.cpp b/src/EterPythonLib/PythonWindow.cpp index 099fe87..ad0d4de 100644 --- a/src/EterPythonLib/PythonWindow.cpp +++ b/src/EterPythonLib/PythonWindow.cpp @@ -1247,6 +1247,15 @@ namespace UI return TRUE; } + BOOL CImageBox::LoadImageFromFile(const char* c_szFileName) + { + CPackManager::instance().SetFileLoadMode(); + BOOL r = LoadImage(c_szFileName); + CPackManager::instance().SetPackLoadMode(); + + return r; + } + void CImageBox::SetDiffuseColor(float fr, float fg, float fb, float fa) { if (!m_pImageInstance) diff --git a/src/EterPythonLib/PythonWindow.h b/src/EterPythonLib/PythonWindow.h index 588a746..c05497b 100644 --- a/src/EterPythonLib/PythonWindow.h +++ b/src/EterPythonLib/PythonWindow.h @@ -335,6 +335,7 @@ namespace UI virtual ~CImageBox(); BOOL LoadImage(const char * c_szFileName); + BOOL LoadImageFromFile(const char * c_szFileName); void SetDiffuseColor(float fr, float fg, float fb, float fa); int GetWidth(); diff --git a/src/EterPythonLib/PythonWindowManagerModule.cpp b/src/EterPythonLib/PythonWindowManagerModule.cpp index d2cc76b..44c8156 100644 --- a/src/EterPythonLib/PythonWindowManagerModule.cpp +++ b/src/EterPythonLib/PythonWindowManagerModule.cpp @@ -1957,6 +1957,21 @@ PyObject * wndImageLoadImage(PyObject * poSelf, PyObject * poArgs) return Py_BuildNone(); } +PyObject* wndImageLoadImageFromFile(PyObject* poSelf, PyObject* poArgs) +{ + UI::CWindow* pWindow; + if (!PyTuple_GetWindow(poArgs, 0, &pWindow)) + return Py_BuildException(); + char* szFileName; + if (!PyTuple_GetString(poArgs, 1, &szFileName)) + return Py_BuildException(); + + if (!((UI::CImageBox*)pWindow)->LoadImageFromFile(szFileName)) + return Py_BuildException("Failed to load image from file (filename: %s)", szFileName); + + return Py_BuildNone(); +} + PyObject * wndImageSetDiffuseColor(PyObject * poSelf, PyObject * poArgs) { UI::CWindow * pWindow; @@ -2451,6 +2466,7 @@ void initwndMgr() // ImageBox { "LoadImage", wndImageLoadImage, METH_VARARGS }, + { "LoadImageFromFile", wndImageLoadImageFromFile, METH_VARARGS }, { "SetDiffuseColor", wndImageSetDiffuseColor, METH_VARARGS }, { "GetWidth", wndImageGetWidth, METH_VARARGS }, { "GetHeight", wndImageGetHeight, METH_VARARGS }, diff --git a/src/PackLib/PackManager.cpp b/src/PackLib/PackManager.cpp index 0fb7e38..9258c78 100644 --- a/src/PackLib/PackManager.cpp +++ b/src/PackLib/PackManager.cpp @@ -1,4 +1,6 @@ #include "PackManager.h" +#include +#include bool CPackManager::AddPack(const std::string& path) { @@ -11,9 +13,23 @@ bool CPackManager::GetFile(std::string_view path, TPackFile& result) thread_local std::string buf; NormalizePath(path, buf); - auto it = m_entries.find(buf); - if (it != m_entries.end()) { - return it->second.first->GetFile(it->second.second, result); + if (m_load_from_pack) { + auto it = m_entries.find(buf); + if (it != m_entries.end()) { + return it->second.first->GetFile(it->second.second, result); + } + } + else { + std::ifstream ifs(buf, std::ios::binary); + if (ifs.is_open()) { + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + ifs.seekg(0, std::ios::beg); + result.resize(size); + if (ifs.read((char*)result.data(), size)) { + return true; + } + } } return false; @@ -24,8 +40,13 @@ bool CPackManager::IsExist(std::string_view path) const thread_local std::string buf; NormalizePath(path, buf); - auto it = m_entries.find(buf); - return it != m_entries.end(); + if (m_load_from_pack) { + auto it = m_entries.find(buf); + return it != m_entries.end(); + } + else { + return std::filesystem::exists(buf); + } } void CPackManager::NormalizePath(std::string_view in, std::string& out) const diff --git a/src/PackLib/PackManager.h b/src/PackLib/PackManager.h index 07b290b..ecf939d 100644 --- a/src/PackLib/PackManager.h +++ b/src/PackLib/PackManager.h @@ -14,9 +14,13 @@ public: bool GetFile(std::string_view path, TPackFile& result); bool IsExist(std::string_view path) const; + void SetPackLoadMode() { m_load_from_pack = true; } + void SetFileLoadMode() { m_load_from_pack = false; } + private: void NormalizePath(std::string_view in, std::string& out) const; private: + bool m_load_from_pack = true; TPackFileMap m_entries; };