diff --git a/source/graphics/Font.cpp b/source/graphics/Font.cpp index a217a0a896..3c6edb2280 100644 --- a/source/graphics/Font.cpp +++ b/source/graphics/Font.cpp @@ -202,7 +202,7 @@ bool CFont::AddFontFromPath(const OsPath& fontPath) return false; } - std::shared_ptr fontData; + std::unique_ptr fontData; size_t fontDataSize; if (g_VFS->LoadFile(fontPath, fontData, fontDataSize) != 0) { @@ -223,7 +223,7 @@ bool CFont::AddFontFromPath(const OsPath& fontPath) } // Keep the font data alive. - m_FontsData.push_back(fontData); + m_FontsData.push_back(std::move(fontData)); // Set the font size. if (FT_Error error{FT_Set_Char_Size(face, 0, FloatToF26Dot6(m_FontSize), 0 , 0)}) @@ -233,7 +233,7 @@ bool CFont::AddFontFromPath(const OsPath& fontPath) } // Get the height of the font. - if(m_Faces.empty()) + if (m_Faces.empty()) m_Height = FPosF26Dot6ToFloat(face->size->metrics.height); // Add the fallback font to the list. diff --git a/source/graphics/Font.h b/source/graphics/Font.h index c31227f613..984ce49b75 100644 --- a/source/graphics/Font.h +++ b/source/graphics/Font.h @@ -1,4 +1,4 @@ - /* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,6 +19,7 @@ #define INCLUDED_FONT #include "graphics/Texture.h" +#include "lib/allocators/shared_ptr.h" #include "lib/code_annotation.h" #include "lib/os_path.h" #include "lib/types.h" @@ -190,7 +191,7 @@ private: std::reference_wrapper> m_GammaCorrectionLUT; FT_Library m_FreeType; - std::vector> m_FontsData; + std::vector> m_FontsData; std::vector m_Faces; UniqueFTStroker m_Stroker{nullptr, &ftStrokerDeleter}; diff --git a/source/graphics/tests/test_MeshManager.h b/source/graphics/tests/test_MeshManager.h index c831d12e75..eca06721df 100644 --- a/source/graphics/tests/test_MeshManager.h +++ b/source/graphics/tests/test_MeshManager.h @@ -82,7 +82,7 @@ class TestMeshManager : public CxxTest::TestSuite void copyFile(const VfsPath& src, const VfsPath& dst) { // Copy a file into the mod directory, so we can work on it: - std::shared_ptr data; size_t size = 0; + std::unique_ptr data; size_t size = 0; TS_ASSERT_OK(g_VFS->LoadFile(src, data, size)); TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size})); } diff --git a/source/lib/file/vfs/vfs.cpp b/source/lib/file/vfs/vfs.cpp index 23acd75f17..c77e527204 100644 --- a/source/lib/file/vfs/vfs.cpp +++ b/source/lib/file/vfs/vfs.cpp @@ -180,6 +180,29 @@ public: return INFO::OK; } + Status LoadFile(const VfsPath& pathname, std::unique_ptr& fileContents, size_t& size) final + { + std::lock_guard lock(vfs_mutex); + + VfsDirectory* directory; VfsFile* file; + // per 2010-05-01 meeting, this shouldn't raise 'scary error + // dialogs', which might fail to display the culprit pathname + // instead, callers should log the error, including pathname. + RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file)); + + size = file->Size(); + fileContents.reset(reinterpret_cast(rtl_AllocateAligned(size, maxSectorSize))); + if (!fileContents) + WARN_RETURN(ERR::NO_MEM); + + RETURN_STATUS_IF_ERR(file->Loader()->Load(file->Name(), {fileContents.get(), file->Size()})); + + stats_io_user_request(size); + m_trace->NotifyLoad(pathname, size); + + return INFO::OK; + } + std::wstring TextRepresentation() const final { std::lock_guard lock(vfs_mutex); diff --git a/source/lib/file/vfs/vfs.h b/source/lib/file/vfs/vfs.h index 6684c927b4..923cbcbf48 100644 --- a/source/lib/file/vfs/vfs.h +++ b/source/lib/file/vfs/vfs.h @@ -28,6 +28,7 @@ #ifndef INCLUDED_VFS #define INCLUDED_VFS +#include "lib/allocators/shared_ptr.h" #include "lib/file/file_system.h" // CFileInfo #include "lib/file/vfs/vfs_path.h" #include "lib/os_path.h" @@ -160,6 +161,16 @@ struct IVFS **/ virtual Status LoadFile(const VfsPath& pathname, std::shared_ptr& fileContents, size_t& size) = 0; + /** + * Read an entire file into memory. + * + * @param pathname + * @param fileContents receives a unique pointer to the contents. + * @param size receives the size [bytes] of the file contents. + * @return Status. + **/ + virtual Status LoadFile(const VfsPath& pathname, std::unique_ptr& fileContents, size_t& size) = 0; + /** * @return a string representation of all files and directories. **/ diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index 096b27ce9b..d2277d234b 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -629,7 +629,7 @@ void CConsole::LoadHistory() if (!VfsFileExists(m_HistoryFile)) return; - std::shared_ptr buf; size_t buflen; + std::unique_ptr buf; size_t buflen; if (g_VFS->LoadFile(m_HistoryFile, buf, buflen) < 0) return; diff --git a/source/ps/ConfigDB.cpp b/source/ps/ConfigDB.cpp index f11966598a..6849052e96 100644 --- a/source/ps/ConfigDB.cpp +++ b/source/ps/ConfigDB.cpp @@ -311,7 +311,7 @@ bool CConfigDB::Reload(EConfigNamespace ns) std::lock_guard s(m_Mutex); - std::shared_ptr buffer; + std::unique_ptr buffer; size_t buflen; { // Handle missing files quietly diff --git a/source/ps/FileIo.h b/source/ps/FileIo.h index 0ed15a2357..ab4e038bc0 100644 --- a/source/ps/FileIo.h +++ b/source/ps/FileIo.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -30,6 +30,7 @@ #ifndef INCLUDED_FILEPACKER #define INCLUDED_FILEPACKER +#include "lib/allocators/shared_ptr.h" #include "lib/file/io/write_buffer.h" #include "lib/file/vfs/vfs_path.h" #include "lib/types.h" @@ -144,7 +145,7 @@ public: private: // the data read from file and used during unpack operations - std::shared_ptr m_buf; + std::unique_ptr m_buf; size_t m_bufSize; size_t m_unpackPos; /// current unpack position in stream diff --git a/source/ps/Filesystem.h b/source/ps/Filesystem.h index b0d809d5a5..769422538a 100644 --- a/source/ps/Filesystem.h +++ b/source/ps/Filesystem.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -18,6 +18,7 @@ #ifndef INCLUDED_PS_FILESYSTEM #define INCLUDED_PS_FILESYSTEM +#include "lib/allocators/shared_ptr.h" #include "lib/file/vfs/vfs.h" #include "lib/file/vfs/vfs_path.h" #include "lib/status.h" @@ -99,7 +100,7 @@ public: CStr8 DecodeUTF8() const; private: - std::shared_ptr m_Buffer; + std::unique_ptr m_Buffer; size_t m_BufferSize; }; diff --git a/source/ps/VideoMode.cpp b/source/ps/VideoMode.cpp index c3b45d84ce..a2207d620d 100644 --- a/source/ps/VideoMode.cpp +++ b/source/ps/VideoMode.cpp @@ -198,7 +198,7 @@ void CVideoMode::CCursor::SetCursor(const CStrW& name) int hotspotX = 0, hotspotY = 0; { const VfsPath pathHotspotName = pathBaseName.ChangeExtension(L".txt"); - std::shared_ptr buffer; + std::unique_ptr buffer; size_t size; if (g_VFS->LoadFile(pathHotspotName, buffer, size) != INFO::OK) { diff --git a/source/simulation2/tests/test_Simulation2.h b/source/simulation2/tests/test_Simulation2.h index 1f5dfa5ac4..d721e6425e 100644 --- a/source/simulation2/tests/test_Simulation2.h +++ b/source/simulation2/tests/test_Simulation2.h @@ -39,7 +39,7 @@ class TestSimulation2 : public CxxTest::TestSuite { void copyFile(const VfsPath& src, const VfsPath& dst) { - std::shared_ptr data; + std::unique_ptr data; size_t size = 0; TS_ASSERT_OK(g_VFS->LoadFile(src, data, size)); TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size}));