File: SkirmishAILibraryInfo.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (232 lines) | stat: -rw-r--r-- 7,058 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
	Copyright (c) 2008 Robin Vobruba <hoijui.quaero@gmail.com>

	This program is free software {} you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation {} either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY {} without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "SkirmishAILibraryInfo.h"

#include "Interface/aidefines.h"
#include "Interface/SSkirmishAILibrary.h"
#include "SkirmishAIKey.h"
#include "Util.h"
#include "Info.h"
#include "Option.h"

#include "Platform/errorhandler.h"
#include "FileSystem/VFSModes.h"


static const char* BAD_CHARS = "\t _#";
static const std::string DEFAULT_VALUE = "";

CSkirmishAILibraryInfo::CSkirmishAILibraryInfo(
		const CSkirmishAILibraryInfo& aiInfo) :
		info_keyLower_key(aiInfo.info_keyLower_key),
		info_key_value(aiInfo.info_key_value),
		info_key_description(aiInfo.info_key_description),
		options(aiInfo.options)
		{}

CSkirmishAILibraryInfo::CSkirmishAILibraryInfo(
		const std::string& aiInfoFile,
		const std::string& aiOptionFile) {

	std::vector<InfoItem> tmpInfo;
	parseInfo(tmpInfo, aiInfoFile);
	std::vector<InfoItem>::const_iterator ii;
	for (ii = tmpInfo.begin(); ii != tmpInfo.end(); ++ii) {
		SetInfo(ii->key, ii->value, ii->desc);
	}

	if (!aiOptionFile.empty()) {
		parseOptions(options, aiOptionFile);
	}
}

CSkirmishAILibraryInfo::~CSkirmishAILibraryInfo() {}


size_t CSkirmishAILibraryInfo::size() const {
	return info_keys.size();
}
const std::string& CSkirmishAILibraryInfo::GetKeyAt(size_t index) const {

	if (index < info_keys.size()) {
		return *(info_keys.begin() + index);
	} else {
		return DEFAULT_VALUE;
	}
}
const std::string& CSkirmishAILibraryInfo::GetValueAt(size_t index) const {

	if (index < info_keys.size()) {
		return info_key_value.find(GetKeyAt(index))->second;
	} else {
		return DEFAULT_VALUE;
	}
}
const std::string& CSkirmishAILibraryInfo::GetDescriptionAt(size_t index) const {

	if (index < info_keys.size()) {
		return info_key_description.find(GetKeyAt(index))->second;
	} else {
		return DEFAULT_VALUE;
	}
}

SkirmishAIKey CSkirmishAILibraryInfo::GetKey() const {

	const std::string& sn = GetShortName();
	const std::string& v = GetVersion();
	SkirmishAIKey key = SkirmishAIKey(sn, v);

	return key;
}

const std::string& CSkirmishAILibraryInfo::GetDataDir() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_DATA_DIR);
}
const std::string& CSkirmishAILibraryInfo::GetDataDirCommon() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_DATA_DIR_COMMON);
}
const std::string& CSkirmishAILibraryInfo::GetShortName() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_SHORT_NAME);
}
const std::string& CSkirmishAILibraryInfo::GetVersion() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_VERSION);
}
const std::string& CSkirmishAILibraryInfo::GetName() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_NAME);
}
const std::string& CSkirmishAILibraryInfo::GetDescription() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_DESCRIPTION);
}
const std::string& CSkirmishAILibraryInfo::GetURL() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_URL);
}
bool CSkirmishAILibraryInfo::IsLuaAI() const {

	bool isLua = false;

	const std::string& isLuaStr = GetInfo("isLuaAI");
	if ((isLuaStr == "yes") ||
		(isLuaStr == "Yes") ||
		(isLuaStr == "YES") ||
		(isLuaStr == "1") ||
		(isLuaStr == "true") ||
		(isLuaStr == "True") ||
		(isLuaStr == "TRUE")) {
		isLua = true;
	}

	return isLua;
}
const std::string& CSkirmishAILibraryInfo::GetInterfaceShortName() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_INTERFACE_SHORT_NAME);
}
const std::string& CSkirmishAILibraryInfo::GetInterfaceVersion() const {
	return GetInfo(SKIRMISH_AI_PROPERTY_INTERFACE_VERSION);
}
const std::string& CSkirmishAILibraryInfo::GetInfo(const std::string& key) const {

	bool found = false;
	std::map<std::string, std::string>::const_iterator strPair;

	// get real key through lower case key
	strPair = info_keyLower_key.find(StringToLower(key));
	found = (strPair != info_keyLower_key.end());

	// get value
	if (found) {
		strPair = info_key_value.find(strPair->second);
		found = (strPair != info_key_value.end());
	}

	if (!found) {
		logOutput.Print("Skirmish AI property '%s' could not be found.", key.c_str());
		return DEFAULT_VALUE;
	} else {
		return strPair->second;
	}
}


void CSkirmishAILibraryInfo::SetDataDir(const std::string& dataDir) {
	SetInfo(SKIRMISH_AI_PROPERTY_DATA_DIR, dataDir);
}
void CSkirmishAILibraryInfo::SetDataDirCommon(const std::string& dataDirCommon) {
	SetInfo(SKIRMISH_AI_PROPERTY_DATA_DIR_COMMON, dataDirCommon);
}
void CSkirmishAILibraryInfo::SetShortName(const std::string& shortName) {
	SetInfo(SKIRMISH_AI_PROPERTY_SHORT_NAME, shortName);
}
void CSkirmishAILibraryInfo::SetVersion(const std::string& version) {
	SetInfo(SKIRMISH_AI_PROPERTY_VERSION, version);
}
void CSkirmishAILibraryInfo::SetName(const std::string& name) {
	SetInfo(SKIRMISH_AI_PROPERTY_NAME, name);
}
void CSkirmishAILibraryInfo::SetDescription(const std::string& description) {
	SetInfo(SKIRMISH_AI_PROPERTY_DESCRIPTION, description);
}
void CSkirmishAILibraryInfo::SetURL(const std::string& url) {
	SetInfo(SKIRMISH_AI_PROPERTY_URL, url);
}
void CSkirmishAILibraryInfo::SetLuaAI(const std::string& isLua) {
	SetInfo("isLuaAI", isLua);
}
void CSkirmishAILibraryInfo::SetLuaAI(const bool isLua) {
	SetLuaAI(std::string(isLua ? "yes" : "no"));
}
void CSkirmishAILibraryInfo::SetInterfaceShortName(
		const std::string& interfaceShortName) {
	SetInfo(SKIRMISH_AI_PROPERTY_INTERFACE_SHORT_NAME, interfaceShortName);
}
void CSkirmishAILibraryInfo::SetInterfaceVersion(
		const std::string& interfaceVersion) {
	SetInfo(SKIRMISH_AI_PROPERTY_INTERFACE_VERSION, interfaceVersion);
}
bool CSkirmishAILibraryInfo::SetInfo(const std::string& key,
		const std::string& value, const std::string& description) {

	static const std::string snKey = StringToLower(SKIRMISH_AI_PROPERTY_SHORT_NAME);
	static const std::string vKey = StringToLower(SKIRMISH_AI_PROPERTY_VERSION);

	std::string keyLower = StringToLower(key);
	if (keyLower == snKey || keyLower == vKey) {
		if (value.find_first_of(BAD_CHARS) != std::string::npos) {
		logOutput.Print("Error, Skirmish AI property (%s or %s)\n"
				"contains illegal characters (%s).",
				SKIRMISH_AI_PROPERTY_SHORT_NAME, SKIRMISH_AI_PROPERTY_VERSION,
				BAD_CHARS);
			return false;
		}
	}

	// only add the key if it is not yet present
	if (info_key_value.find(key) == info_key_value.end()) {
		info_keys.push_back(key);
	}
	info_keyLower_key[keyLower] = key;
	info_key_value[key] = value;
	info_key_description[key] = description;

	return true;
}


const std::vector<Option>& CSkirmishAILibraryInfo::GetOptions() const {
	return options;
}