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