LCOV - code coverage report
Current view: top level - source/ps/scripting - JSInterface_ModIo.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 12 50 24.0 %
Date: 2023-01-19 00:18:29 Functions: 3 7 42.9 %

          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 "JSInterface_ModIo.h"
      21             : 
      22             : #include "ps/CLogger.h"
      23             : #include "ps/ModIo.h"
      24             : #include "scriptinterface/FunctionWrapper.h"
      25             : 
      26             : namespace JSI_ModIo
      27             : {
      28           0 : ModIo* ModIoGetter(const ScriptRequest&, JS::CallArgs&)
      29             : {
      30           0 :     if (!g_ModIo)
      31             :     {
      32           0 :         LOGERROR("Trying to access ModIO when it's not initialized!");
      33           0 :         return nullptr;
      34             :     }
      35           0 :     return g_ModIo;
      36             : }
      37             : 
      38           0 : void StartGetGameId()
      39             : {
      40           0 :     if (!g_ModIo)
      41           0 :         g_ModIo = new ModIo();
      42             : 
      43           0 :     ENSURE(g_ModIo);
      44             : 
      45           0 :     g_ModIo->StartGetGameId();
      46           0 : }
      47             : 
      48             : // TODO: could provide a FromJSVal for ModIoModData
      49           0 : JS::Value GetMods(const ScriptRequest& rq)
      50             : {
      51           0 :     if (!g_ModIo)
      52             :     {
      53           0 :         LOGERROR("ModIoGetMods called before ModIoStartGetGameId");
      54           0 :         return JS::NullValue();
      55             :     }
      56             : 
      57           0 :     const std::vector<ModIoModData>& availableMods = g_ModIo->GetMods();
      58             : 
      59           0 :     JS::RootedValue mods(rq.cx);
      60           0 :     Script::CreateArray(rq, &mods, availableMods.size());
      61             : 
      62           0 :     u32 i = 0;
      63           0 :     for (const ModIoModData& mod : availableMods)
      64             :     {
      65           0 :         JS::RootedValue m(rq.cx);
      66           0 :         Script::CreateObject(rq, &m);
      67             : 
      68           0 :         for (const std::pair<const std::string, std::string>& prop : mod.properties)
      69           0 :             Script::SetProperty(rq, m, prop.first.c_str(), prop.second, true);
      70             : 
      71           0 :         Script::SetProperty(rq, m, "dependencies", mod.dependencies, true);
      72           0 :         Script::SetPropertyInt(rq, mods, i++, m);
      73             :     }
      74             : 
      75           0 :     return mods;
      76             : }
      77             : 
      78          12 : const std::map<DownloadProgressStatus, std::string> statusStrings = {
      79             :     { DownloadProgressStatus::NONE, "none" },
      80             :     { DownloadProgressStatus::GAMEID, "gameid" },
      81             :     { DownloadProgressStatus::READY, "ready" },
      82             :     { DownloadProgressStatus::LISTING, "listing" },
      83             :     { DownloadProgressStatus::LISTED, "listed" },
      84             :     { DownloadProgressStatus::DOWNLOADING, "downloading" },
      85             :     { DownloadProgressStatus::SUCCESS, "success" },
      86             :     { DownloadProgressStatus::FAILED_GAMEID, "failed_gameid" },
      87             :     { DownloadProgressStatus::FAILED_LISTING, "failed_listing" },
      88             :     { DownloadProgressStatus::FAILED_DOWNLOADING, "failed_downloading" },
      89             :     { DownloadProgressStatus::FAILED_FILECHECK, "failed_filecheck" }
      90          11 : };
      91             : 
      92             : // TODO: could provide a FromJSVal for DownloadProgressData
      93           0 : JS::Value GetDownloadProgress(const ScriptRequest& rq)
      94             : {
      95           0 :     if (!g_ModIo)
      96             :     {
      97           0 :         LOGERROR("ModIoGetDownloadProgress called before ModIoGetMods");
      98           0 :         return JS::NullValue();
      99             :     }
     100             : 
     101             : 
     102           0 :     const DownloadProgressData& progress = g_ModIo->GetDownloadProgress();
     103             : 
     104           0 :     JS::RootedValue progressData(rq.cx);
     105           0 :     Script::CreateObject(rq, &progressData);
     106           0 :     Script::SetProperty(rq, progressData, "status", statusStrings.at(progress.status), true);
     107           0 :     Script::SetProperty(rq, progressData, "progress", progress.progress, true);
     108           0 :     Script::SetProperty(rq, progressData, "error", progress.error, true);
     109             : 
     110           0 :     return progressData;
     111             : }
     112             : 
     113          12 : void RegisterScriptFunctions(const ScriptRequest& rq)
     114             : {
     115          12 :     ScriptFunction::Register<&StartGetGameId>(rq, "ModIoStartGetGameId");
     116          12 :     ScriptFunction::Register<&ModIo::StartListMods, &ModIoGetter>(rq, "ModIoStartListMods");
     117          12 :     ScriptFunction::Register<&ModIo::StartDownloadMod, &ModIoGetter>(rq, "ModIoStartDownloadMod");
     118          12 :     ScriptFunction::Register<&ModIo::AdvanceRequest, &ModIoGetter>(rq, "ModIoAdvanceRequest");
     119          12 :     ScriptFunction::Register<&ModIo::CancelRequest, &ModIoGetter>(rq, "ModIoCancelRequest");
     120          12 :     ScriptFunction::Register<&GetMods>(rq, "ModIoGetMods");
     121          12 :     ScriptFunction::Register<&GetDownloadProgress>(rq, "ModIoGetDownloadProgress");
     122          12 : }
     123           3 : }

Generated by: LCOV version 1.13