From 551071c5a44d431771e0a332bdd0685b04db395f Mon Sep 17 00:00:00 2001 From: Vladislav Belov Date: Sat, 5 Sep 2026 12:33:46 +0200 Subject: [PATCH] Replaces shared_ptr by span for LoadFile and Store --- source/graphics/ColladaManager.cpp | 4 ++-- source/graphics/HeightMipmap.cpp | 4 ++-- source/graphics/TextureConverter.cpp | 4 ++-- source/graphics/tests/test_MeshManager.h | 10 +++++----- source/lib/file/common/real_directory.cpp | 6 +++--- source/lib/file/common/real_directory.h | 5 +++-- source/lib/file/vfs/tests/test_vfs_real_path.h | 8 ++++---- source/lib/file/vfs/tests/test_vfs_util.h | 18 +++++++++--------- source/lib/file/vfs/vfs.cpp | 10 +++++----- source/lib/file/vfs/vfs.h | 5 +++-- source/ps/CConsole.cpp | 2 +- source/ps/ConfigDB.cpp | 4 ++-- source/ps/FileIo.cpp | 4 ++-- source/ps/SavedGame.cpp | 2 +- source/ps/Util.cpp | 2 +- source/ps/XML/XMLWriter.cpp | 4 ++-- source/ps/XML/Xeromyces.cpp | 4 ++-- source/ps/scripting/JSInterface_VFS.cpp | 2 +- source/simulation2/tests/test_Simulation2.h | 2 +- 19 files changed, 51 insertions(+), 49 deletions(-) diff --git a/source/graphics/ColladaManager.cpp b/source/graphics/ColladaManager.cpp index 7854e20c27..113355ac83 100644 --- a/source/graphics/ColladaManager.cpp +++ b/source/graphics/ColladaManager.cpp @@ -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); } diff --git a/source/graphics/HeightMipmap.cpp b/source/graphics/HeightMipmap.cpp index 9e4e87f1fd..2d5ccae8f2 100644 --- a/source/graphics/HeightMipmap.cpp +++ b/source/graphics/HeightMipmap.cpp @@ -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); } diff --git a/source/graphics/TextureConverter.cpp b/source/graphics/TextureConverter.cpp index caf3899105..e7d66813e0 100644 --- a/source/graphics/TextureConverter.cpp +++ b/source/graphics/TextureConverter.cpp @@ -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 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; diff --git a/source/graphics/tests/test_MeshManager.h b/source/graphics/tests/test_MeshManager.h index a156c46bde..c831d12e75 100644 --- a/source/graphics/tests/test_MeshManager.h +++ b/source/graphics/tests/test_MeshManager.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 @@ -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 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 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 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 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); diff --git a/source/lib/file/common/real_directory.cpp b/source/lib/file/common/real_directory.cpp index eb3a8c2c21..82015b9f64 100644 --- a/source/lib/file/common/real_directory.cpp +++ b/source/lib/file/common/real_directory.cpp @@ -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& fileContents, size_t size) +Status RealDirectory::Store(const OsPath& name, std::span fileContents) { - return io::Store(m_path / name, fileContents.get(), size); + return io::Store(m_path / name, fileContents.data(), fileContents.size()); } diff --git a/source/lib/file/common/real_directory.h b/source/lib/file/common/real_directory.h index 2e16679730..70cf69fb27 100644 --- a/source/lib/file/common/real_directory.h +++ b/source/lib/file/common/real_directory.h @@ -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 #include +#include class RealDirectory final : public IFileLoader { @@ -58,7 +59,7 @@ public: } Status Load(const OsPath& name, const std::shared_ptr& buf, size_t size) const override; - Status Store(const OsPath& name, const std::shared_ptr& fileContents, size_t size); + Status Store(const OsPath& name, std::span fileContents); void Watch(); diff --git a/source/lib/file/vfs/tests/test_vfs_real_path.h b/source/lib/file/vfs/tests/test_vfs_real_path.h index 614f5f3f67..393e37feed 100644 --- a/source/lib/file/vfs/tests/test_vfs_real_path.h +++ b/source/lib/file/vfs/tests/test_vfs_real_path.h @@ -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"); }; diff --git a/source/lib/file/vfs/tests/test_vfs_util.h b/source/lib/file/vfs/tests/test_vfs_util.h index c88a7f4564..0dc45e2756 100644 --- a/source/lib/file/vfs/tests/test_vfs_util.h +++ b/source/lib/file/vfs/tests/test_vfs_util.h @@ -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 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); diff --git a/source/lib/file/vfs/vfs.cpp b/source/lib/file/vfs/vfs.cpp index 2c2a0b6db9..04bc362c13 100644 --- a/source/lib/file/vfs/vfs.cpp +++ b/source/lib/file/vfs/vfs.cpp @@ -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& fileContents, size_t size) + virtual Status CreateFile(const VfsPath& pathname, std::span fileContents) { std::lock_guard 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; } diff --git a/source/lib/file/vfs/vfs.h b/source/lib/file/vfs/vfs.h index d9923a5556..5563340b70 100644 --- a/source/lib/file/vfs/vfs.h +++ b/source/lib/file/vfs/vfs.h @@ -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 #include #include +#include #include 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& fileContents, size_t size) = 0; + virtual Status CreateFile(const VfsPath& pathname, std::span fileContents) = 0; /** * Read an entire file into memory. diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index d909be8c2a..096b27ce9b 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -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()); diff --git a/source/ps/ConfigDB.cpp b/source/ps/ConfigDB.cpp index 512cf42ed7..f11966598a 100644 --- a/source/ps/ConfigDB.cpp +++ b/source/ps/ConfigDB.cpp @@ -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); diff --git a/source/ps/FileIo.cpp b/source/ps/FileIo.cpp index 0c3562c8bd..dccc579fcf 100644 --- a/source/ps/FileIo.cpp +++ b/source/ps/FileIo.cpp @@ -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); diff --git a/source/ps/SavedGame.cpp b/source/ps/SavedGame.cpp index a0a1aaf16a..2beb46a742 100644 --- a/source/ps/SavedGame.cpp +++ b/source/ps/SavedGame.cpp @@ -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)); diff --git a/source/ps/Util.cpp b/source/ps/Util.cpp index 1ac56c268b..8d1e8616bf 100644 --- a/source/ps/Util.cpp +++ b/source/ps/Util.cpp @@ -76,7 +76,7 @@ Status tex_write(Tex* t, const VfsPath& filename) Status ret = INFO::OK; { std::shared_ptr 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 diff --git a/source/ps/XML/XMLWriter.cpp b/source/ps/XML/XMLWriter.cpp index e0d687d0d5..c7a89e11c5 100644 --- a/source/ps/XML/XMLWriter.cpp +++ b/source/ps/XML/XMLWriter.cpp @@ -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 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()); diff --git a/source/ps/XML/Xeromyces.cpp b/source/ps/XML/Xeromyces.cpp index e298106f42..bf2d1fa256 100644 --- a/source/ps/XML/Xeromyces.cpp +++ b/source/ps/XML/Xeromyces.cpp @@ -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); diff --git a/source/ps/scripting/JSInterface_VFS.cpp b/source/ps/scripting/JSInterface_VFS.cpp index 77cd517a88..26b53bac6a 100644 --- a/source/ps/scripting/JSInterface_VFS.cpp +++ b/source/ps/scripting/JSInterface_VFS.cpp @@ -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); diff --git a/source/simulation2/tests/test_Simulation2.h b/source/simulation2/tests/test_Simulation2.h index d629311589..1f5dfa5ac4 100644 --- a/source/simulation2/tests/test_Simulation2.h +++ b/source/simulation2/tests/test_Simulation2.h @@ -42,7 +42,7 @@ class TestSimulation2 : public CxxTest::TestSuite std::shared_ptr 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;