LCOV - code coverage report
Current view: top level - source/ps - Filesystem.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 58 61 95.1 %
Date: 2026-12-03 01:19:39 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "Filesystem.h"
      21             : 
      22             : #include "lib/code_annotation.h"
      23             : #include "lib/debug.h"
      24             : #include "lib/os_path.h"
      25             : #include "lib/path.h"
      26             : #include "lib/sysdep/dir_watch.h"
      27             : #include "ps/CLogger.h"
      28             : #include "ps/CStr.h"
      29             : #include "ps/Profile.h"
      30             : 
      31             : #include <algorithm>
      32             : #include <string>
      33             : #include <utility>
      34             : #include <vector>
      35             : 
      36             : PIVFS g_VFS;
      37             : 
      38             : static std::vector<std::pair<FileReloadFunc, void*> > g_ReloadFuncs;
      39             : 
      40       21317 : bool VfsFileExists(const VfsPath& pathname)
      41             : {
      42       21317 :     return g_VFS->GetFileInfo(pathname, 0) == INFO::OK;
      43             : }
      44             : 
      45          24 : bool VfsDirectoryExists(const VfsPath& pathname)
      46             : {
      47          24 :     ENSURE(pathname.IsDirectory());
      48          24 :     return g_VFS->GetDirectoryEntries(pathname, NULL, NULL) == INFO::OK;
      49             : }
      50             : 
      51         451 : void RegisterFileReloadFunc(FileReloadFunc func, void* obj)
      52             : {
      53         451 :     g_ReloadFuncs.emplace_back(func, obj);
      54         451 : }
      55             : 
      56         451 : void UnregisterFileReloadFunc(FileReloadFunc func, void* obj)
      57             : {
      58         451 :     g_ReloadFuncs.erase(std::remove(g_ReloadFuncs.begin(), g_ReloadFuncs.end(), std::make_pair(func, obj)));
      59         451 : }
      60             : 
      61             : // try to skip unnecessary work by ignoring uninteresting notifications.
      62           9 : static bool CanIgnore(const DirWatchNotification& notification)
      63             : {
      64             :     // ignore directories
      65           9 :     const OsPath& pathname = notification.Pathname();
      66           9 :     if(pathname.IsDirectory())
      67             :         return true;
      68             : 
      69             :     // ignore uninteresting file types (e.g. temp files, or the
      70             :     // hundreds of XMB files that are generated from XML)
      71           9 :     const OsPath extension = pathname.Extension();
      72           9 :     const wchar_t* extensionsToIgnore[] = { L".xmb", L".tmp" };
      73          27 :     for(size_t i = 0; i < ARRAY_SIZE(extensionsToIgnore); i++)
      74             :     {
      75          36 :         if(extension == extensionsToIgnore[i])
      76             :             return true;
      77             :     }
      78             : 
      79             :     return false;
      80           9 : }
      81             : 
      82           9 : Status ReloadChangedFiles()
      83             : {
      84           9 :     PROFILE3("hotload");
      85             : 
      86           9 :     std::vector<DirWatchNotification> notifications;
      87           9 :     RETURN_STATUS_IF_ERR(dir_watch_Poll(notifications));
      88             : 
      89           9 :     if (notifications.empty())
      90             :         return INFO::SKIPPED;
      91             : 
      92          18 :     for(size_t i = 0; i < notifications.size(); i++)
      93             :     {
      94           9 :         if(!CanIgnore(notifications[i]))
      95             :         {
      96           9 :             VfsPath pathname;
      97           9 :             RETURN_STATUS_IF_ERR(g_VFS->GetVirtualPath(notifications[i].Pathname(), pathname));
      98           9 :             RETURN_STATUS_IF_ERR(g_VFS->RemoveFile(pathname));
      99           9 :             RETURN_STATUS_IF_ERR(g_VFS->RepopulateDirectory(pathname.Parent()/""));
     100             : 
     101             :             // Tell each hotloadable system about this file change:
     102             : 
     103          18 :             for (size_t j = 0; j < g_ReloadFuncs.size(); ++j)
     104           9 :                 g_ReloadFuncs[j].first(g_ReloadFuncs[j].second, pathname);
     105           9 :         }
     106             :     }
     107             :     return INFO::OK;
     108           9 : }
     109             : 
     110             : 
     111        7186 : CVFSFile::CVFSFile()
     112        7186 :     : m_BufferSize(0)
     113             : {
     114        7186 : }
     115             : 
     116        7186 : CVFSFile::~CVFSFile()
     117             : {
     118        7186 : }
     119             : 
     120        7186 : PSRETURN CVFSFile::Load(const PIVFS& vfs, const VfsPath& filename, bool log /* = true */)
     121             : {
     122             :     // Load should never be called more than once, so complain
     123        7186 :     if (m_Buffer)
     124             :     {
     125           0 :         DEBUG_WARN_ERR(ERR::LOGIC);
     126           0 :         return PSRETURN_CVFSFile_AlreadyLoaded;
     127             :     }
     128             : 
     129        7186 :     Status ret = vfs->LoadFile(filename, m_Buffer, m_BufferSize);
     130        7186 :     if (ret != INFO::OK)
     131             :     {
     132          11 :         if (log)
     133          44 :             LOGERROR("CVFSFile: file {} couldn't be opened (vfs_load: {})", filename.string8(), ret);
     134          11 :         m_Buffer.reset();
     135          11 :         m_BufferSize = 0;
     136          11 :         return PSRETURN_CVFSFile_LoadFailed;
     137             :     }
     138             : 
     139             :     return PSRETURN_OK;
     140             : }
     141             : 
     142        7175 : const std::uint8_t* CVFSFile::GetBuffer() const
     143             : {
     144        7175 :     return m_Buffer.get();
     145             : }
     146             : 
     147        7175 : size_t CVFSFile::GetBufferSize() const
     148             : {
     149           6 :     return m_BufferSize;
     150             : }
     151             : 
     152           6 : CStr CVFSFile::GetAsString() const
     153             : {
     154          17 :     return std::string((char*)GetBuffer(), GetBufferSize());
     155             : }
     156             : 
     157        5133 : CStr CVFSFile::DecodeUTF8() const
     158             : {
     159        5133 :     const std::uint8_t* buffer = GetBuffer();
     160             : 
     161             :     // Detect if there's a UTF-8 BOM and strip it
     162        5133 :     if (GetBufferSize() >= 3 && buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF)
     163             :     {
     164           0 :         return std::string(&buffer[3], buffer + GetBufferSize());
     165             :     }
     166             :     else
     167             :     {
     168       15372 :         return std::string(buffer, buffer + GetBufferSize());
     169             :     }
     170             : }

Generated by: LCOV version 1.16