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: 37 38 97.4 %
Date: 2023-01-19 00:18:29 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2021 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/ScriptInterface.h"
      25             : #include "scriptinterface/Object.h"
      26             : #include "simulation2/serialization/ISerializer.h"
      27             : #include "simulation2/serialization/IDeserializer.h"
      28             : 
      29          49 : CComponentTypeScript::CComponentTypeScript(const ScriptInterface& scriptInterface, JS::HandleValue instance) :
      30          49 :     m_ScriptInterface(scriptInterface)
      31             : {
      32          49 :     m_Instance.init(ScriptRequest(m_ScriptInterface).cx, instance);
      33          49 : }
      34             : 
      35          44 : void CComponentTypeScript::Init(const CParamNode& paramNode, entity_id_t ent)
      36             : {
      37          88 :     ScriptRequest rq(m_ScriptInterface);
      38          44 :     Script::SetProperty(rq, m_Instance, "entity", (int)ent, true, false);
      39          44 :     Script::SetProperty(rq, m_Instance, "template", paramNode, true, false);
      40          44 :     ScriptFunction::CallVoid(rq, m_Instance, "Init");
      41          44 : }
      42             : 
      43          49 : void CComponentTypeScript::Deinit()
      44             : {
      45          98 :     ScriptRequest rq(m_ScriptInterface);
      46          49 :     ScriptFunction::CallVoid(rq, m_Instance, "Deinit");
      47          49 : }
      48             : 
      49          10 : void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global)
      50             : {
      51          20 :     ScriptRequest rq(m_ScriptInterface);
      52             : 
      53          10 :     const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName();
      54             : 
      55          20 :     JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(m_ScriptInterface));
      56             : 
      57          10 :     if (!ScriptFunction::CallVoid(rq, m_Instance, name, msgVal))
      58           0 :         LOGERROR("Script message handler %s failed", name);
      59          10 : }
      60             : 
      61          10 : void CComponentTypeScript::Serialize(ISerializer& serialize)
      62             : {
      63          20 :     ScriptRequest rq(m_ScriptInterface);
      64             : 
      65             :     try
      66             :     {
      67          11 :         serialize.ScriptVal("comp", &m_Instance);
      68             :     }
      69           2 :     catch(PSERROR_Serialize& err)
      70             :     {
      71           1 :         int ent = INVALID_ENTITY;
      72           1 :         Script::GetProperty(rq, m_Instance, "entity", ent);
      73           2 :         std::string name = "(error)";
      74           1 :         Script::GetObjectClassName(rq, m_Instance, name);
      75           1 :         LOGERROR("Script component %s of entity %i failed to serialize: %s\nSerializing:\n%s", name, ent, err.what(), Script::ToString(rq, &m_Instance));
      76             :         // Rethrow now that we added more details
      77           1 :         throw;
      78             :     }
      79           9 : }
      80             : 
      81           5 : void CComponentTypeScript::Deserialize(const CParamNode& paramNode, IDeserializer& deserialize, entity_id_t ent)
      82             : {
      83          10 :     ScriptRequest rq(m_ScriptInterface);
      84             : 
      85           5 :     Script::SetProperty(rq, m_Instance, "entity", (int)ent, true, false);
      86           5 :     Script::SetProperty(rq, m_Instance, "template", paramNode, true, false);
      87             : 
      88           5 :     deserialize.ScriptObjectAssign("comp", m_Instance);
      89           5 : }

Generated by: LCOV version 1.13