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