LCOV - code coverage report
Current view: top level - source/simulation2/components - ICmpAIManager.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 1 35 2.9 %
Date: 2026-12-02 01:21:50 Functions: 3 8 37.5 %

          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 "ICmpAIManager.h"
      21             : 
      22             : #include "lib/code_annotation.h"
      23             : #include "lib/file/vfs/vfs_util.h"
      24             : #include "lib/path.h"
      25             : #include "lib/status.h"
      26             : #include "ps/Filesystem.h"
      27             : #include "scriptinterface/JSON.h"
      28             : #include "scriptinterface/Object.h"
      29             : #include "scriptinterface/Interface.h"
      30             : #include "scriptinterface/Request.h"
      31             : #include "simulation2/system/InterfaceScripted.h"
      32             : 
      33             : #include <filesystem>
      34             : #include <iterator>
      35             : #include <js/Array.h>
      36             : #include <js/PropertyAndElement.h>
      37             : #include <js/RootingAPI.h>
      38             : #include <js/TypeDecls.h>
      39             : 
      40             : class CFileInfo;
      41             : 
      42             : BEGIN_INTERFACE_WRAPPER(AIManager)
      43           0 : DEFINE_INTERFACE_METHOD("AddPlayer", ICmpAIManager, AddPlayer)
      44             : DEFINE_INTERFACE_METHOD("TryLoadSharedComponent", ICmpAIManager, TryLoadSharedComponent)
      45             : DEFINE_INTERFACE_METHOD("RunGamestateInit", ICmpAIManager, RunGamestateInit)
      46         143 : END_INTERFACE_WRAPPER(AIManager)
      47             : 
      48             : // Implement the static method that finds all AI scripts
      49             : // that can be loaded via AddPlayer:
      50             : 
      51           0 : struct GetAIsHelper
      52             : {
      53             :     NONCOPYABLE(GetAIsHelper);
      54             : public:
      55           0 :     GetAIsHelper(const Script::Interface& scriptInterface) :
      56           0 :         m_ScriptInterface(scriptInterface),
      57           0 :         m_AIs(scriptInterface.GetGeneralJSContext())
      58             :     {
      59           0 :         Script::Request rq(m_ScriptInterface);
      60           0 :         m_AIs = JS::NewArrayObject(rq.cx, 0);
      61           0 :     }
      62             : 
      63           0 :     void Run()
      64             :     {
      65           0 :         vfs::ForEachFile(g_VFS, L"simulation/ai/", Callback, (uintptr_t)this, L"*.json", vfs::DIR_RECURSIVE);
      66           0 :     }
      67             : 
      68           0 :     static Status Callback(const VfsPath& pathname, const CFileInfo&, const uintptr_t cbData)
      69             :     {
      70           0 :         GetAIsHelper* self = (GetAIsHelper*)cbData;
      71           0 :         Script::Request rq(self->m_ScriptInterface);
      72             : 
      73             :         // Extract the 3rd component of the path (i.e. the directory after simulation/ai/)
      74           0 :         std::filesystem::path components = pathname.string();
      75           0 :         std::filesystem::path::iterator it = components.begin();
      76           0 :         std::advance(it, 2);
      77           0 :         std::wstring dirname = it->wstring();
      78             : 
      79           0 :         JS::RootedValue ai(rq.cx);
      80           0 :         Script::CreateObject(rq, &ai);
      81             : 
      82           0 :         JS::RootedValue data(rq.cx);
      83           0 :         Script::ReadJSONFile(rq, pathname, &data);
      84           0 :         Script::SetProperty(rq, ai, "id", dirname, true);
      85           0 :         Script::SetProperty(rq, ai, "data", data, true);
      86           0 :         std::uint32_t length;
      87           0 :         JS::GetArrayLength(rq.cx, self->m_AIs, &length);
      88           0 :         JS_SetElement(rq.cx, self->m_AIs, length, ai);
      89             : 
      90           0 :         return INFO::OK;
      91           0 :     }
      92             : 
      93             :     JS::PersistentRootedObject m_AIs;
      94             :     const Script::Interface& m_ScriptInterface;
      95             : };
      96             : 
      97           0 : JS::Value ICmpAIManager::GetAIs(const Script::Interface& scriptInterface)
      98             : {
      99           0 :     GetAIsHelper helper(scriptInterface);
     100           0 :     helper.Run();
     101           0 :     return JS::ObjectValue(*helper.m_AIs);
     102           0 : }

Generated by: LCOV version 1.16