mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-21 20:06:40 +00:00
Adds Load with unique_ptr to VFS
Also replaces some usages of Load with shared_ptr by unique_ptr.
This commit is contained in:
@@ -202,7 +202,7 @@ bool CFont::AddFontFromPath(const OsPath& fontPath)
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<u8> fontData;
|
||||
std::unique_ptr<u8[], AlignedDeleter> 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.
|
||||
|
||||
@@ -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<const std::array<float, 256>> m_GammaCorrectionLUT;
|
||||
|
||||
FT_Library m_FreeType;
|
||||
std::vector<std::shared_ptr<u8>> m_FontsData;
|
||||
std::vector<std::unique_ptr<u8[], AlignedDeleter>> m_FontsData;
|
||||
std::vector<UniqueFTFace> m_Faces;
|
||||
UniqueFTStroker m_Stroker{nullptr, &ftStrokerDeleter};
|
||||
|
||||
|
||||
@@ -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<u8> data; size_t size = 0;
|
||||
std::unique_ptr<u8[], AlignedDeleter> data; size_t size = 0;
|
||||
TS_ASSERT_OK(g_VFS->LoadFile(src, data, size));
|
||||
TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size}));
|
||||
}
|
||||
|
||||
@@ -180,6 +180,29 @@ public:
|
||||
return INFO::OK;
|
||||
}
|
||||
|
||||
Status LoadFile(const VfsPath& pathname, std::unique_ptr<u8[], AlignedDeleter>& fileContents, size_t& size) final
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<u8*>(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<std::mutex> lock(vfs_mutex);
|
||||
|
||||
@@ -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<u8>& 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<u8[], AlignedDeleter>& fileContents, size_t& size) = 0;
|
||||
|
||||
/**
|
||||
* @return a string representation of all files and directories.
|
||||
**/
|
||||
|
||||
@@ -629,7 +629,7 @@ void CConsole::LoadHistory()
|
||||
if (!VfsFileExists(m_HistoryFile))
|
||||
return;
|
||||
|
||||
std::shared_ptr<u8> buf; size_t buflen;
|
||||
std::unique_ptr<u8[], AlignedDeleter> buf; size_t buflen;
|
||||
if (g_VFS->LoadFile(m_HistoryFile, buf, buflen) < 0)
|
||||
return;
|
||||
|
||||
|
||||
@@ -311,7 +311,7 @@ bool CConfigDB::Reload(EConfigNamespace ns)
|
||||
|
||||
std::lock_guard<std::recursive_mutex> s(m_Mutex);
|
||||
|
||||
std::shared_ptr<u8> buffer;
|
||||
std::unique_ptr<u8[], AlignedDeleter> buffer;
|
||||
size_t buflen;
|
||||
{
|
||||
// Handle missing files quietly
|
||||
|
||||
+3
-2
@@ -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<u8> m_buf;
|
||||
std::unique_ptr<u8[], AlignedDeleter> m_buf;
|
||||
size_t m_bufSize;
|
||||
|
||||
size_t m_unpackPos; /// current unpack position in stream
|
||||
|
||||
@@ -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<u8> m_Buffer;
|
||||
std::unique_ptr<u8[], AlignedDeleter> m_Buffer;
|
||||
size_t m_BufferSize;
|
||||
};
|
||||
|
||||
|
||||
@@ -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<u8> buffer;
|
||||
std::unique_ptr<u8[], AlignedDeleter> buffer;
|
||||
size_t size;
|
||||
if (g_VFS->LoadFile(pathHotspotName, buffer, size) != INFO::OK)
|
||||
{
|
||||
|
||||
@@ -39,7 +39,7 @@ class TestSimulation2 : public CxxTest::TestSuite
|
||||
{
|
||||
void copyFile(const VfsPath& src, const VfsPath& dst)
|
||||
{
|
||||
std::shared_ptr<u8> data;
|
||||
std::unique_ptr<u8[], AlignedDeleter> data;
|
||||
size_t size = 0;
|
||||
TS_ASSERT_OK(g_VFS->LoadFile(src, data, size));
|
||||
TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size}));
|
||||
|
||||
Reference in New Issue
Block a user