LCOV - code coverage report
Current view: top level - source/simulation2/scripting - ScriptComponent.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 40 46 87.0 %
Date: 2026-12-03 01:19:39 Functions: 6 7 85.7 %

          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 "ScriptComponent.h"
      21             : 
      22             : #include "scriptinterface/FunctionWrapper.h"
      23             : #include "scriptinterface/JSON.h"
      24             : #include "scriptinterface/Object.h"
      25             : #include "simulation2/system/Component.h"
      26             : #include "simulation2/system/Message.h"
      27             : 
      28             : #include <string>
      29             : 
      30             : namespace Script { class Interface; }
      31             : 
      32        3962 : CComponentTypeScript::CComponentTypeScript(const Script::Interface& scriptInterface, JS::HandleValue instance) :
      33        3962 :     m_ScriptInterface(scriptInterface), m_Instance(instance)
      34        3962 : {}
      35             : 
      36        3957 : void CComponentTypeScript::Init(CComponentManager& cmpMgr, const CParamNode& paramNode, entity_id_t ent)
      37             : {
      38        3957 :     cmpMgr.RegisterTrace(ent, m_Instance);
      39        3957 :     Script::Request rq(m_ScriptInterface);
      40        7914 :     Script::SetProperty(rq, GetInstance(), "entity", (int)ent, true, false);
      41        3957 :     Script::SetProperty(rq, GetInstance(), "template", paramNode, true, false);
      42        3957 :     Script::Function::CallVoid(rq, GetInstance(), "Init");
      43        3957 : }
      44             : 
      45        3962 : void CComponentTypeScript::Deinit()
      46             : {
      47        3962 :     Script::Request rq(m_ScriptInterface);
      48        3962 :     Script::Function::CallVoid(rq, GetInstance(), "Deinit");
      49        3962 : }
      50             : 
      51           0 : bool CComponentTypeScript::HasMessageHandler(const CMessage& msg, const bool global)
      52             : {
      53           0 :     const Script::Request rq(m_ScriptInterface);
      54           0 :     return Script::HasProperty(rq, GetInstance(), global ? msg.GetScriptGlobalHandlerName() :
      55           0 :         msg.GetScriptHandlerName());
      56           0 : }
      57             : 
      58       20284 : void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
      59             : {
      60       20284 :     Script::Request rq(m_ScriptInterface);
      61             : 
      62       20284 :     const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName();
      63             : 
      64       20284 :     JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(rq));
      65             : 
      66       20284 :     if (!Script::Function::CallVoid(rq, GetInstance(), name, msgVal))
      67           0 :         LOGERROR("Script message handler {} failed", name);
      68       20284 : }
      69             : 
      70       21934 : void CComponentTypeScript::Serialize(ISerializer& serialize)
      71             : {
      72       21934 :     Script::Request rq(m_ScriptInterface);
      73             : 
      74       21934 :     try
      75             :     {
      76       21934 :         serialize.ScriptVal("comp", GetMutInstance());
      77             :     }
      78           1 :     catch(PSERROR_Serialize& err)
      79             :     {
      80           1 :         int ent = INVALID_ENTITY;
      81           1 :         Script::GetProperty(rq, GetInstance(), "entity", ent);
      82           1 :         std::string name = "(error)";
      83           1 :         Script::GetObjectClassName(rq, GetInstance(), name);
      84           4 :         LOGERROR("Script component {} of entity {} failed to serialize: {}\nSerializing:\n{}",
      85             :             name, ent, err.what(), Script::ToString(rq, GetMutInstance()));
      86             :         // Rethrow now that we added more details
      87           1 :         throw;
      88           1 :     }
      89       21934 : }
      90             : 
      91           5 : void CComponentTypeScript::Deserialize(CComponentManager& cmpMgr, const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
      92             : {
      93           5 :     cmpMgr.RegisterTrace(ent, m_Instance);
      94             : 
      95           5 :     Script::Request rq(m_ScriptInterface);
      96             : 
      97           5 :     Script::SetProperty(rq, GetInstance(), "entity", (int)ent, true, false);
      98           5 :     Script::SetProperty(rq, GetInstance(), "template", paramNode, true, false);
      99             : 
     100           5 :     deserialize.ScriptObjectAssign("comp", GetInstance());
     101           5 : }

Generated by: LCOV version 1.16