LCOV - code coverage report
Current view: top level - source/ps - CacheLoader.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 41 53 77.4 %
Date: 2026-12-03 01:19:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 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 "CacheLoader.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : #include "lib/file/file_system.h"
      24             : #include "lib/os_path.h"
      25             : #include "lib/path.h"
      26             : #include "lib/utf8.h"
      27             : #include "maths/MD5.h"
      28             : #include "ps/Util.h"
      29             : 
      30             : #include <ctime>
      31             : 
      32        9024 : CCacheLoader::CCacheLoader(PIVFS vfs, const std::wstring& fileExtension) :
      33        9024 :     m_VFS(vfs), m_FileExtension(fileExtension)
      34             : {
      35        9024 : }
      36             : 
      37        9017 : Status CCacheLoader::TryLoadingCached(const VfsPath& sourcePath, const MD5& initialHash,
      38             :     std::uint32_t version, VfsPath& loadPath)
      39             : {
      40        9017 :     VfsPath archiveCachePath = ArchiveCachePath(sourcePath);
      41             : 
      42             :     // Try the archive cache file first
      43        9017 :     if (CanUseArchiveCache(sourcePath, archiveCachePath))
      44             :     {
      45           0 :         loadPath = archiveCachePath;
      46             :         return INFO::OK;
      47             :     }
      48             : 
      49             :     // Fail if no source or archive cache
      50             :     // Note: this is not always an error case, because for instance there
      51             :     //  are some uncached .pmd/psa files in the game with no source .dae.
      52             :     //  This test fails (correctly) in that valid situation, so it seems
      53             :     //  best to leave the error handling to the caller.
      54        9017 :     Status err = m_VFS->GetFileInfo(sourcePath, NULL);
      55        9017 :     if (err < 0)
      56             :     {
      57             :         return err;
      58             :     }
      59             : 
      60             :     // Look for loose cache of source file
      61             : 
      62        8872 :     VfsPath looseCachePath = LooseCachePath(sourcePath, initialHash, version);
      63             : 
      64             :     // If the loose cache file exists, use it
      65        8872 :     if (m_VFS->GetFileInfo(looseCachePath, NULL) >= 0)
      66             :     {
      67        6831 :         loadPath = looseCachePath;
      68             :         return INFO::OK;
      69             :     }
      70             : 
      71             :     // No cache - we'll need to regenerate it
      72             : 
      73       10913 :     loadPath = looseCachePath;
      74             :     return INFO::SKIPPED;
      75       18034 : }
      76             : 
      77        9017 : bool CCacheLoader::CanUseArchiveCache(const VfsPath& sourcePath, const VfsPath& archiveCachePath)
      78             : {
      79             :     // We want to use the archive cache whenever possible,
      80             :     // unless it's superseded by a source file that the user has edited
      81             : 
      82        9017 :     size_t archiveCachePriority = 0;
      83        9017 :     size_t sourcePriority = 0;
      84             : 
      85        9017 :     bool archiveCacheExists = (m_VFS->GetFilePriority(archiveCachePath, &archiveCachePriority) >= 0);
      86             : 
      87             :     // Can't use it if there's no cache
      88        9017 :     if (!archiveCacheExists)
      89             :         return false;
      90             : 
      91           0 :     bool sourceExists = (m_VFS->GetFilePriority(sourcePath, &sourcePriority) >= 0);
      92             : 
      93             :     // Must use the cache if there's no source
      94           0 :     if (!sourceExists)
      95             :         return true;
      96             : 
      97             :     // If source file is from a higher-priority mod than archive cache,
      98             :     // don't use the old cache
      99           0 :     if (archiveCachePriority < sourcePriority)
     100             :         return false;
     101             : 
     102             :     // If source file is more recent than the archive cache (i.e. the user has edited it),
     103             :     // don't use the old cache
     104           0 :     CFileInfo sourceInfo, archiveCacheInfo;
     105           0 :     if (m_VFS->GetFileInfo(sourcePath, &sourceInfo) >= 0 &&
     106           0 :         m_VFS->GetFileInfo(archiveCachePath, &archiveCacheInfo) >= 0)
     107             :     {
     108           0 :         const double howMuchNewer = difftime(sourceInfo.MTime(), archiveCacheInfo.MTime());
     109           0 :         const double threshold = 2.0;   // FAT timestamp resolution [seconds]
     110           0 :         if (howMuchNewer > threshold)
     111             :             return false;
     112             :     }
     113             : 
     114             :     // Otherwise we can use the cache
     115             :     return true;
     116        9017 : }
     117             : 
     118        9017 : VfsPath CCacheLoader::ArchiveCachePath(const VfsPath& sourcePath) const
     119             : {
     120       36068 :     return sourcePath.ChangeExtension(std::wstring{sourcePath.Extension()} + L".cached" + m_FileExtension);
     121             : }
     122             : 
     123        8877 : VfsPath CCacheLoader::LooseCachePath(const VfsPath& sourcePath, const MD5& initialHash, std::uint32_t version)
     124             : {
     125        8877 :     CFileInfo fileInfo;
     126        8877 :     if (m_VFS->GetFileInfo(sourcePath, &fileInfo) < 0)
     127             :     {
     128           0 :         debug_warn(L"source file disappeared"); // this should never happen
     129           0 :         return VfsPath();
     130             :     }
     131             : 
     132        8877 :     std::uint64_t mtime = static_cast<std::uint64_t>(fileInfo.MTime()) & ~1; // skip lowest bit, since zip and FAT don't preserve it
     133        8877 :     std::uint64_t size = static_cast<std::uint64_t>(fileInfo.Size());
     134             : 
     135             :     // Construct a hash of the file data and settings.
     136             : 
     137        8877 :     MD5 hash = initialHash;
     138        8877 :     hash.Update(reinterpret_cast<const std::uint8_t*>(&mtime), sizeof(mtime));
     139        8877 :     hash.Update(reinterpret_cast<const std::uint8_t*>(&size), sizeof(size));
     140        8877 :     hash.Update(reinterpret_cast<const std::uint8_t*>(&version), sizeof(version));
     141             :     // these are local cached files, so we don't care about endianness etc
     142             : 
     143        8877 :     std::uint8_t digest[MD5::DIGESTSIZE];
     144        8877 :     hash.Final(digest);
     145             : 
     146             :     // Get the mod path
     147       17754 :     OsPath path;
     148        8877 :     m_VFS->GetOriginalPath(sourcePath, path);
     149             : 
     150       35496 :     return VfsPath("cache") /
     151       35508 :         path_name_only(path.BeforeCommon(sourcePath).Parent().string().c_str()) /
     152       53262 :             sourcePath.ChangeExtension(std::wstring{sourcePath.Extension()} +
     153       17754 :             L"." +
     154             :             // Use a short prefix of the full hash (we don't need high collision-resistance)
     155       44385 :             wstring_from_utf8(Hexify(digest, 8)) +
     156       17754 :             m_FileExtension);
     157        8877 : }

Generated by: LCOV version 1.16