forked from spring/spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaAIImplHandler.cpp
More file actions
91 lines (70 loc) · 2.41 KB
/
LuaAIImplHandler.cpp
File metadata and controls
91 lines (70 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "LuaAIImplHandler.h"
#include "Lua/LuaParser.h"
#include "ExternalAI/Interface/SSkirmishAILibrary.h"
//#include "ExternalAI/SkirmishAIKey.h"
#include "System/Exceptions.h"
#include <cassert>
//CR_BIND(CLuaAIImplHandler,);
//
//CR_REG_METADATA(CLuaAIImplHandler, (
// CR_RESERVED(64)
//));
CLuaAIImplHandler& CLuaAIImplHandler::GetInstance()
{
static CLuaAIImplHandler mySingleton;
return mySingleton;
}
CLuaAIImplHandler::InfoItemVector CLuaAIImplHandler::LoadInfoItems()
{
InfoItemVector luaAIInfos;
LuaParser luaParser("LuaAI.lua", SPRING_VFS_MOD_BASE, SPRING_VFS_MOD_BASE);
// It is not an error if the mod does not come with Lua AIs.
if (!luaParser.Execute())
return luaAIInfos;
const LuaTable root = luaParser.GetRoot();
if (!root.IsValid())
throw content_error("root table invalid");
for (int i = 1; root.KeyExists(i); i++) {
std::string shortName = root.GetString(i, "");
std::string description;
std::string version;
// Lua AIs can be specified in two different formats, string (name) or table (name & desc)
if (!shortName.empty()) {
description = "(please see game description, forum or homepage)";
} else {
const LuaTable& optTbl = root.SubTable(i);
if (!optTbl.IsValid())
continue;
shortName = optTbl.GetString("name", "");
if (shortName.empty())
continue;
description = optTbl.GetString("desc", shortName);
version = optTbl.GetString("version", "<not-versioned>");
}
luaAIInfos.emplace_back();
auto& aiInfo = luaAIInfos.back();
InfoItem ii;
ii.key = SKIRMISH_AI_PROPERTY_SHORT_NAME;
ii.valueType = INFO_VALUE_TYPE_STRING;
ii.valueTypeString = shortName;
ii.desc = "short name of this Lua Skirmish AI";
aiInfo[0] = ii;
ii.key = SKIRMISH_AI_PROPERTY_VERSION;
ii.valueType = INFO_VALUE_TYPE_STRING;
ii.valueTypeString = version;
ii.desc = "version of this Lua Skirmish AI, normally defined by the game's version";
aiInfo[1] = ii;
ii.key = SKIRMISH_AI_PROPERTY_NAME;
ii.valueType = INFO_VALUE_TYPE_STRING;
ii.valueTypeString = shortName + " (game-specific AI)";
ii.desc = "human-readable name of this Lua Skirmish AI";
aiInfo[2] = ii;
ii.key = SKIRMISH_AI_PROPERTY_DESCRIPTION;
ii.valueType = INFO_VALUE_TYPE_STRING;
ii.valueTypeString = description;
ii.desc = "short description of this Lua Skirmish AI";
aiInfo[3] = ii;
}
return luaAIInfos;
}