Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ICmpAIManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 
23 
24 #include "lib/file/vfs/vfs_util.h"
25 #include "ps/Filesystem.h"
26 
27 BEGIN_INTERFACE_WRAPPER(AIManager)
28 DEFINE_INTERFACE_METHOD_3("AddPlayer", void, ICmpAIManager, AddPlayer, std::wstring, player_id_t, uint8_t)
29 DEFINE_INTERFACE_METHOD_0("TryLoadSharedComponent", void, ICmpAIManager, TryLoadSharedComponent)
30 DEFINE_INTERFACE_METHOD_0("RunGamestateInit", void, ICmpAIManager, RunGamestateInit)
31 END_INTERFACE_WRAPPER(AIManager)
32 
33 // Implement the static method that finds all AI scripts
34 // that can be loaded via AddPlayer:
35 
37 {
38  NONCOPYABLE(GetAIsHelper);
39 public:
40  GetAIsHelper(ScriptInterface& scriptInterface) :
41  m_ScriptInterface(scriptInterface)
42  {
43  }
44 
45  void Run()
46  {
47  vfs::ForEachFile(g_VFS, L"simulation/ai/", Callback, (uintptr_t)this, L"*.json", vfs::DIR_RECURSIVE);
48  }
49 
50  static Status Callback(const VfsPath& pathname, const CFileInfo& UNUSED(fileInfo), const uintptr_t cbData)
51  {
52  GetAIsHelper* self = (GetAIsHelper*)cbData;
53 
54  // Extract the 3rd component of the path (i.e. the directory after simulation/ai/)
55  fs::wpath components = pathname.string();
56  fs::wpath::iterator it = components.begin();
57  std::advance(it, 2);
58  std::wstring dirname = GetWstringFromWpath(*it);
59 
61  self->m_ScriptInterface.Eval("({})", ai);
62  self->m_ScriptInterface.SetProperty(ai.get(), "id", dirname, true);
63  self->m_ScriptInterface.SetProperty(ai.get(), "data", self->m_ScriptInterface.ReadJSONFile(pathname), true);
64  self->m_AIs.push_back(ai);
65 
66  return INFO::OK;
67  }
68 
69  std::vector<CScriptValRooted> m_AIs;
71 };
72 
73 std::vector<CScriptValRooted> ICmpAIManager::GetAIs(ScriptInterface& scriptInterface)
74 {
75  GetAIsHelper helper(scriptInterface);
76  helper.Run();
77  return helper.m_AIs;
78 }
#define NONCOPYABLE(className)
GetAIsHelper(ScriptInterface &scriptInterface)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static std::vector< CScriptValRooted > GetAIs(ScriptInterface &scriptInterface)
Returns a vector of {&quot;id&quot;:&quot;value-for-AddPlayer&quot;, &quot;name&quot;:&quot;Human readable name&quot;} objects, based on all the available AI scripts.
#define DEFINE_INTERFACE_METHOD_3(scriptname, rettype, classname, methodname, arg1, arg2, arg3)
const Status OK
Definition: status.h:386
#define END_INTERFACE_WRAPPER(iname)
std::vector< CScriptValRooted > m_AIs
std::wstring GetWstringFromWpath(const fs::wpath &path)
Helper function to handle API differences between Boost Filesystem v2 and v3.
Definition: Filesystem.cpp:98
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
#define DEFINE_INTERFACE_METHOD_0(scriptname, rettype, classname, methodname)
static Status Callback(const VfsPath &pathname, const CFileInfo &fileInfo, const uintptr_t cbData)
ScriptInterface & m_ScriptInterface
Definition: path.h:75
const String & string() const
Definition: path.h:123
unsigned char uint8_t
Definition: wposix_types.h:51
i64 Status
Error handling system.
Definition: status.h:171
Status ForEachFile(const PIVFS &fs, const VfsPath &startPath, FileCallback cb, uintptr_t cbData, const wchar_t *pattern, size_t flags)
call back for each file in a directory tree
Definition: vfs_util.cpp:58
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
Abstraction around a SpiderMonkey JSContext.
#define BEGIN_INTERFACE_WRAPPER(iname)
PIVFS g_VFS
Definition: Filesystem.cpp:30