LCOV - code coverage report
Current view: top level - source/ps - ModInstaller.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 52 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 4 0.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 "ModInstaller.h"
      21             : 
      22             : #include "lib/file/file_system.h"
      23             : #include "lib/status.h"
      24             : #include "lib/sysdep/os.h"
      25             : #include "ps/CLogger.h"
      26             : #include "ps/Errors.h"
      27             : #include "ps/Filesystem.h"
      28             : #include "scriptinterface/JSON.h"
      29             : #include "scriptinterface/Conversions.h"
      30             : #include "scriptinterface/Interface.h"
      31             : #include "scriptinterface/Request.h"
      32             : 
      33             : #include <js/PropertyAndElement.h>
      34             : #include <js/RootingAPI.h>
      35             : #include <js/TypeDecls.h>
      36             : #include <js/Value.h>
      37             : #include <string>
      38             : 
      39             : #if !OS_WIN
      40             : #include "lib/os_path.h"
      41             : #endif
      42             : 
      43             : #include <fstream>
      44             : #if OS_WIN
      45             : #include <filesystem>
      46             : #endif
      47             : 
      48           0 : CModInstaller::CModInstaller(const OsPath& modsdir, const OsPath& tempdir) :
      49           0 :     m_ModsDir(modsdir), m_TempDir(tempdir / "_modscache"), m_CacheDir("cache/")
      50             : {
      51           0 :     m_VFS = CreateVfs();
      52           0 :     CreateDirectories(m_TempDir, 0700);
      53           0 : }
      54             : 
      55           0 : CModInstaller::~CModInstaller()
      56             : {
      57           0 :     m_VFS.reset();
      58           0 :     DeleteDirectory(m_TempDir);
      59           0 : }
      60             : 
      61           0 : CModInstaller::ModInstallationResult CModInstaller::Install(
      62             :     const OsPath& mod,
      63             :     const std::shared_ptr<Script::Context>& scriptContext,
      64             :     bool keepFile)
      65             : {
      66           0 :     const OsPath modTemp = m_TempDir / mod.Basename() / mod.Filename().ChangeExtension(L".zip");
      67           0 :     CreateDirectories(modTemp.Parent(), 0700);
      68             : 
      69           0 :     if (keepFile)
      70             :     {
      71           0 :         if (CopyFile(mod, modTemp, true) != INFO::OK)
      72             :         {
      73           0 :             LOGERROR("Failed to copy '{}' to '{}'", mod.string8(), modTemp.string8());
      74           0 :             return FAIL_ON_MOD_COPY;
      75             :         }
      76             :     }
      77           0 :     else if (RenameFile(mod, modTemp) != INFO::OK)
      78             :     {
      79           0 :         LOGERROR("Failed to rename '{}' into '{}'", mod.string8(), modTemp.string8());
      80           0 :         return FAIL_ON_MOD_MOVE;
      81             :     }
      82             : 
      83             :     // Load the mod to VFS
      84           0 :     if (m_VFS->Mount(m_CacheDir, m_TempDir / "") != INFO::OK)
      85             :         return FAIL_ON_VFS_MOUNT;
      86           0 :     CVFSFile modinfo;
      87           0 :     PSRETURN modinfo_status = modinfo.Load(m_VFS, m_CacheDir / modTemp.Basename() / "mod.json", false);
      88           0 :     m_VFS->Clear();
      89           0 :     if (modinfo_status != PSRETURN_OK)
      90             :         return FAIL_ON_MOD_LOAD;
      91             : 
      92             :     // Extract the name of the mod
      93           0 :     CStr modName;
      94           0 :     {
      95           0 :         Script::Interface scriptInterface("Engine", "ModInstaller", scriptContext);
      96           0 :         Script::Request rq(scriptInterface);
      97             : 
      98           0 :         JS::RootedValue json_val(rq.cx);
      99           0 :         if (!Script::ParseJSON(rq, modinfo.GetAsString(), &json_val))
     100             :             return FAIL_ON_PARSE_JSON;
     101           0 :         JS::RootedObject json_obj(rq.cx, json_val.toObjectOrNull());
     102           0 :         JS::RootedValue name_val(rq.cx);
     103           0 :         if (!JS_GetProperty(rq.cx, json_obj, "name", &name_val))
     104             :             return FAIL_ON_EXTRACT_NAME;
     105           0 :         Script::FromJSVal(rq, name_val, modName);
     106           0 :         if (modName.empty())
     107             :             return FAIL_ON_EXTRACT_NAME;
     108           0 :     }
     109             : 
     110           0 :     const OsPath modDir = m_ModsDir / modName;
     111           0 :     const OsPath modPath = modDir / (modName + ".zip");
     112             : 
     113             :     // Create a directory with the following structure:
     114             :     //   mod-name/
     115             :     //     mod-name.zip
     116             :     //     mod.json
     117           0 :     CreateDirectories(modDir, 0700);
     118           0 :     if (RenameFile(modTemp, modPath) != INFO::OK)
     119             :     {
     120           0 :         LOGERROR("Failed to rename '{}' into '{}'", modTemp.string8(), modPath.string8());
     121           0 :         return FAIL_ON_MOD_MOVE;
     122             :     }
     123             : 
     124           0 :     DeleteDirectory(modTemp.Parent());
     125             : 
     126             : #if OS_WIN
     127             :     const std::filesystem::path modJsonPath = (modDir / L"mod.json").fileSystemPath();
     128             : #else
     129           0 :     const std::string modJsonPath = OsString(modDir / L"mod.json");
     130             : #endif
     131           0 :     std::ofstream mod_json(modJsonPath);
     132           0 :     if (mod_json.good())
     133             :     {
     134           0 :         mod_json << modinfo.GetAsString();
     135           0 :         mod_json.close();
     136             :     }
     137             :     else
     138             :         return FAIL_ON_JSON_WRITE;
     139             : 
     140           0 :     m_InstalledMods.emplace_back(modName);
     141             : 
     142             :     return SUCCESS;
     143           0 : }
     144             : 
     145           0 : const std::vector<CStr>& CModInstaller::GetInstalledMods() const
     146             : {
     147           0 :     return m_InstalledMods;
     148             : }

Generated by: LCOV version 1.16