Replaces shared_ptr by span for LoadFile and Store

This commit is contained in:
Vladislav Belov
2026-09-05 12:33:46 +02:00
parent 2e13192479
commit 551071c5a4
19 changed files with 51 additions and 49 deletions
+2 -2
View File
@@ -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
@@ -187,7 +187,7 @@ public:
// logic warns when asked to load such.
if (writeBuffer.Size())
{
Status ret = m_VFS->CreateFile(pmdFilename, writeBuffer.Data(), writeBuffer.Size());
Status ret = m_VFS->CreateFile(pmdFilename, {writeBuffer.Data().get(), writeBuffer.Size()});
ENSURE(ret == INFO::OK);
}
+2 -2
View File
@@ -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
@@ -263,6 +263,6 @@ void CHeightMipmap::DumpToDisk(const VfsPath& filename) const
DynArray da;
WARN_IF_ERR(t.encode(filename.Extension(), &da));
g_VFS->CreateFile(filename, DummySharedPtr(da.base), da.pos);
g_VFS->CreateFile(filename, {da.base, da.pos});
std::ignore = da_free(&da);
}
+2 -2
View File
@@ -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
@@ -521,7 +521,7 @@ bool CTextureConverter::Poll(CTexturePtr& texture, VfsPath& dest, bool& ok)
std::shared_ptr<u8> file;
AllocateAligned(file, size, maxSectorSize);
memcpy(file.get(), &result->output.buffer[0], size);
if (m_VFS->CreateFile(result->dest, file, size) < 0)
if (m_VFS->CreateFile(result->dest, {file.get(), size}) < 0)
{
// error writing file
ok = false;
+5 -5
View File
@@ -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
@@ -84,7 +84,7 @@ class TestMeshManager : public CxxTest::TestSuite
// Copy a file into the mod directory, so we can work on it:
std::shared_ptr<u8> data; size_t size = 0;
TS_ASSERT_OK(g_VFS->LoadFile(src, data, size));
TS_ASSERT_OK(g_VFS->CreateFile(dst, data, size));
TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size}));
}
void buildArchive()
@@ -123,7 +123,7 @@ public:
std::shared_ptr<u8> buf;
AllocateAligned(buf, 100, maxSectorSize);
strcpy_s((char*)buf.get(), 5, "Test");
g_VFS->CreateFile(testDAE, buf, 4);
g_VFS->CreateFile(testDAE, {buf.get(), 4});
}
void test_load_pmd_with_extension()
@@ -189,7 +189,7 @@ public:
std::shared_ptr<u8> buf;
AllocateAligned(buf, 100, maxSectorSize);
strcpy_s((char*)buf.get(), 100, "Not valid XML");
g_VFS->CreateFile(testSkeletonDefs, buf, 13);
g_VFS->CreateFile(testSkeletonDefs, {buf.get(), 13});
CModelDefPtr modeldef = meshManager->GetMesh(testDAE);
TS_ASSERT(! modeldef);
@@ -204,7 +204,7 @@ public:
std::shared_ptr<u8> buf;
AllocateAligned(buf, 100, maxSectorSize);
strcpy_s((char*)buf.get(), 100, "Not valid XML");
g_VFS->CreateFile(testDAE, buf, 13);
g_VFS->CreateFile(testDAE, {buf.get(), 13});
CModelDefPtr modeldef = meshManager->GetMesh(testDAE);
TS_ASSERT(! modeldef);
+3 -3
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -54,9 +54,9 @@ RealDirectory::RealDirectory(const OsPath& path, size_t priority, size_t flags)
}
Status RealDirectory::Store(const OsPath& name, const std::shared_ptr<u8>& fileContents, size_t size)
Status RealDirectory::Store(const OsPath& name, std::span<const u8> fileContents)
{
return io::Store(m_path / name, fileContents.get(), size);
return io::Store(m_path / name, fileContents.data(), fileContents.size());
}
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -32,6 +32,7 @@
#include <cstddef>
#include <memory>
#include <span>
class RealDirectory final : public IFileLoader
{
@@ -58,7 +59,7 @@ public:
}
Status Load(const OsPath& name, const std::shared_ptr<u8>& buf, size_t size) const override;
Status Store(const OsPath& name, const std::shared_ptr<u8>& fileContents, size_t size);
Status Store(const OsPath& name, std::span<const u8> fileContents);
void Watch();
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -85,20 +85,20 @@ public:
g_VFS->Mount(L"", TEST_FOLDER / "some_mod" / "", 0, 0);
// Access the subfolder, creating subdirectories in the VFS.
g_VFS->CreateFile(L"cache/some_mod/peek.txt", buf, 0);
g_VFS->CreateFile(L"cache/some_mod/peek.txt", {buf.get(), 0});
g_VFS->Mount(L"cache/", TEST_FOLDER / "cache" / "", 0, 1);
OsPath realPath;
g_VFS->GetDirectoryRealPath(L"cache/", realPath);
TS_ASSERT_EQUALS(realPath, TEST_FOLDER / "cache" / "");
g_VFS->CreateFile(L"cache/test.txt", buf, 0);
g_VFS->CreateFile(L"cache/test.txt", {buf.get(), 0});
g_VFS->GetRealPath(L"cache/test.txt", realPath);
TS_ASSERT_EQUALS(realPath, TEST_FOLDER / "cache" / "test.txt");
g_VFS->GetDirectoryRealPath(L"cache/some_mod/", realPath);
TS_ASSERT_EQUALS(realPath, TEST_FOLDER / "cache" / "some_mod" / "");
g_VFS->CreateFile(L"cache/some_mod/test.txt", buf, 0);
g_VFS->CreateFile(L"cache/some_mod/test.txt", {buf.get(), 0});
g_VFS->GetRealPath(L"cache/some_mod/test.txt", realPath);
TS_ASSERT_EQUALS(realPath, TEST_FOLDER / "cache" / "some_mod" / "test.txt");
};
+9 -9
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -77,14 +77,14 @@ public:
void test_getPathnames()
{
std::shared_ptr<u8> nodata(new u8);
g_VFS->CreateFile("test_file.txt", nodata, 0);
g_VFS->CreateFile("test_file2.txt", nodata, 0);
g_VFS->CreateFile("test_file3.txt", nodata, 0);
g_VFS->CreateFile("test_file2.not_txt", nodata, 0);
g_VFS->CreateFile("sub_folder_a/sub_test_file1.txt", nodata, 0);
g_VFS->CreateFile("sub_folder_a/sub_test_file2.txt", nodata, 0);
g_VFS->CreateFile("sub_folder_b/sub_test_file1.txt", nodata, 0);
g_VFS->CreateFile("sub_folder_b/another_file.not_txt", nodata, 0);
g_VFS->CreateFile("test_file.txt", {nodata.get(), 0});
g_VFS->CreateFile("test_file2.txt", {nodata.get(), 0});
g_VFS->CreateFile("test_file3.txt", {nodata.get(), 0});
g_VFS->CreateFile("test_file2.not_txt", {nodata.get(), 0});
g_VFS->CreateFile("sub_folder_a/sub_test_file1.txt", {nodata.get(), 0});
g_VFS->CreateFile("sub_folder_a/sub_test_file2.txt", {nodata.get(), 0});
g_VFS->CreateFile("sub_folder_b/sub_test_file1.txt", {nodata.get(), 0});
g_VFS->CreateFile("sub_folder_b/another_file.not_txt", {nodata.get(), 0});
VfsPaths pathNames;
vfs::GetPathnames(g_VFS, "", L"*.txt", pathNames);
+5 -5
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -134,7 +134,7 @@ public:
return INFO::OK;
}
virtual Status CreateFile(const VfsPath& pathname, const std::shared_ptr<u8>& fileContents, size_t size)
virtual Status CreateFile(const VfsPath& pathname, std::span<const u8> fileContents)
{
std::lock_guard<std::mutex> lock(vfs_mutex);
VfsDirectory* directory;
@@ -147,12 +147,12 @@ public:
const PRealDirectory& realDirectory = directory->AssociatedDirectory();
const OsPath name = pathname.Filename();
RETURN_STATUS_IF_ERR(realDirectory->Store(name, fileContents, size));
RETURN_STATUS_IF_ERR(realDirectory->Store(name, fileContents));
const VfsFile file(name, size, time(0), realDirectory->Priority(), realDirectory);
const VfsFile file(name, fileContents.size(), time(0), realDirectory->Priority(), realDirectory);
directory->AddFile(file);
m_trace->NotifyStore(pathname, size);
m_trace->NotifyStore(pathname, fileContents.size());
return INFO::OK;
}
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -37,6 +37,7 @@
#include <cstddef>
#include <limits>
#include <memory>
#include <span>
#include <string>
constexpr size_t VFS_MIN_PRIORITY = 0;
@@ -148,7 +149,7 @@ struct IVFS
* @param size [bytes] of the contents, will match that of the file.
* @return Status.
**/
virtual Status CreateFile(const VfsPath& pathname, const std::shared_ptr<u8>& fileContents, size_t size) = 0;
virtual Status CreateFile(const VfsPath& pathname, std::span<const u8> fileContents) = 0;
/**
* Read an entire file into memory.
+1 -1
View File
@@ -666,7 +666,7 @@ void CConsole::SaveHistory()
buffer.Append(&newline, 1);
}
if (g_VFS->CreateFile(m_HistoryFile, buffer.Data(), buffer.Size()) == INFO::OK)
if (g_VFS->CreateFile(m_HistoryFile, {buffer.Data().get(), buffer.Size()}) == INFO::OK)
ONCE(debug_printf("FILES| Console command history written to '%s'\n", m_HistoryFile.string8().c_str()));
else
debug_printf("FILES| Failed to write console command history to '%s'\n", m_HistoryFile.string8().c_str());
+2 -2
View File
@@ -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
@@ -506,7 +506,7 @@ bool CConfigDB::WriteFile(EConfigNamespace ns, const VfsPath& path) const
len = pos - (char*)buf.get();
}
Status ret = g_VFS->CreateFile(path, buf, len);
Status ret = g_VFS->CreateFile(path, {buf.get(), len});
if (ret < 0)
{
LOGERROR("CConfigDB::WriteFile(): CreateFile \"%s\" failed (error: %d)", path.string8(), (int)ret);
+2 -2
View File
@@ -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
@@ -76,7 +76,7 @@ void CFilePacker::Write(const VfsPath& filename)
m_writeBuffer.Overwrite(&payloadSize_le, sizeof(payloadSize_le), 0+offsetof(FileHeader, payloadSize_le));
// write out all data (including header)
const Status st = g_VFS->CreateFile(filename, m_writeBuffer.Data(), m_writeBuffer.Size());
const Status st = g_VFS->CreateFile(filename, {m_writeBuffer.Data().get(), m_writeBuffer.Size()});
if (st < 0)
{
LOGERROR("Failed to write file '%s' with status '%lld'", filename.string8(), (long long)st);
+1 -1
View File
@@ -161,7 +161,7 @@ Status SavedGames::Save(const CStrW& name, const CStrW& description, CSimulation
WARN_RETURN_STATUS_IF_ERR(GetFileInfo(tempSaveFileRealPath, &tempSaveFile));
buffer.Reserve(tempSaveFile.Size());
WARN_RETURN_STATUS_IF_ERR(io::Load(tempSaveFileRealPath, buffer.Data().get(), buffer.Size()));
WARN_RETURN_STATUS_IF_ERR(g_VFS->CreateFile(filename, buffer.Data(), buffer.Size()));
WARN_RETURN_STATUS_IF_ERR(g_VFS->CreateFile(filename, {buffer.Data().get(), buffer.Size()}));
OsPath realPath;
WARN_RETURN_STATUS_IF_ERR(g_VFS->GetRealPath(filename, realPath));
+1 -1
View File
@@ -76,7 +76,7 @@ Status tex_write(Tex* t, const VfsPath& filename)
Status ret = INFO::OK;
{
std::shared_ptr<u8> file = DummySharedPtr(da.base);
const ssize_t bytes_written = g_VFS->CreateFile(filename, file, da.pos);
const ssize_t bytes_written = g_VFS->CreateFile(filename, {file.get(), da.pos});
if(bytes_written > 0)
ENSURE(bytes_written == (ssize_t)da.pos);
else
+2 -2
View File
@@ -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
@@ -108,7 +108,7 @@ bool XMLWriter_File::StoreVFS(const PIVFS& vfs, const VfsPath& pathname)
std::shared_ptr<u8> data;
AllocateAligned(data, size, maxSectorSize);
memcpy(data.get(), m_Data.data(), size);
Status ret = vfs->CreateFile(pathname, data, size);
Status ret = vfs->CreateFile(pathname, {data.get(), size});
if (ret < 0)
{
LOGERROR("Error saving XML data through VFS: %lld '%s'", (long long)ret, pathname.string8());
+2 -2
View File
@@ -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
@@ -189,7 +189,7 @@ PSRETURN CXeromyces::ConvertFile(const PIVFS& vfs, const VfsPath& filename, cons
// Save the file to disk, so it can be loaded quickly next time.
// Don't save if invalid, because we want the syntax error every program start.
vfs->CreateFile(xmbPath, m_Data.m_Buffer, m_Data.m_Size);
vfs->CreateFile(xmbPath, {m_Data.m_Buffer.get(), m_Data.m_Size});
// Set up the XMBData
const bool ok = Initialise(m_Data);
+1 -1
View File
@@ -275,7 +275,7 @@ void WriteJSONFile(const Script::Interface& scriptInterface, const std::wstring&
VfsPath path(filePath);
WriteBuffer buf;
buf.Append(str.c_str(), str.length());
if (g_VFS->CreateFile(path, buf.Data(), buf.Size()) == INFO::OK)
if (g_VFS->CreateFile(path, {buf.Data().get(), buf.Size()}) == INFO::OK)
{
OsPath realPath;
g_VFS->GetRealPath(path, realPath, false);
+1 -1
View File
@@ -42,7 +42,7 @@ class TestSimulation2 : public CxxTest::TestSuite
std::shared_ptr<u8> data;
size_t size = 0;
TS_ASSERT_OK(g_VFS->LoadFile(src, data, size));
TS_ASSERT_OK(g_VFS->CreateFile(dst, data, size));
TS_ASSERT_OK(g_VFS->CreateFile(dst, {data.get(), size}));
}
CTerrain m_Terrain;