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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
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 "AILibraryManager.h"
#include "Interface/aidefines.h"
#include "Interface/SAIInterfaceLibrary.h"
#include "AIInterfaceLibraryInfo.h"
#include "AIInterfaceLibrary.h"
#include "SkirmishAILibraryInfo.h"
#include "SkirmishAIData.h"
#include "Util.h"
#include "LogOutput.h"
#include "Platform/errorhandler.h"
#include "Platform/SharedLib.h"
#include "FileSystem/FileHandler.h"
#include "FileSystem/FileSystem.h"
#include "Sim/Misc/GlobalConstants.h"
#include "Sim/Misc/Team.h"
#include "Sim/Misc/TeamHandler.h"
#include "Game/GameSetup.h"
#include <string>
#include <set>
#include <sstream>
#include <limits.h>
void CAILibraryManager::reportError(const char* topic, const char* msg) {
handleerror(NULL, msg, topic, MBF_OK | MBF_EXCL);
}
void CAILibraryManager::reportError1(const char* topic, const char* msg, const char* arg0) {
const int MAX_MSG_LENGTH = 511;
char s_msg[MAX_MSG_LENGTH + 1];
SNPRINTF(s_msg, MAX_MSG_LENGTH, msg, arg0);
reportError(topic, s_msg);
}
void CAILibraryManager::reportError2(const char* topic, const char* msg, const char* arg0, const char* arg1) {
const int MAX_MSG_LENGTH = 511;
char s_msg[MAX_MSG_LENGTH + 1];
SNPRINTF(s_msg, MAX_MSG_LENGTH, msg, arg0, arg1);
reportError(topic, s_msg);
}
std::string CAILibraryManager::extractFileName(const std::string& libFile, bool includeExtension) {
std::string::size_type firstChar = libFile.find_last_of("/\\");
std::string::size_type lastChar = std::string::npos;
if (!includeExtension) {
lastChar = libFile.find_last_of('.');
}
return libFile.substr(firstChar+1, lastChar - firstChar -1);
}
static std::string noSlashAtEnd(const std::string& dir) {
std::string resDir = dir;
char lastChar = dir.at(dir.size()-1);
if (lastChar == '/' || lastChar == '\\') {
resDir = resDir.substr(0, dir.size()-1);
}
return resDir;
}
CAILibraryManager::CAILibraryManager() {
GetAllInfosFromCache();
}
void CAILibraryManager::GetAllInfosFromCache() {
ClearAllInfos();
typedef std::vector<std::string> T_dirs;
// cause we use CFileHandler for searching files,
// we are automatically searching in all data-dirs
// Read from AI Interface info files
// we are looking for:
// {AI_INTERFACES_DATA_DIR}/{*}/{*}/InterfaceInfo.lua
T_dirs aiInterfaceDataDirs =
filesystem.FindDirsInDirectSubDirs(AI_INTERFACES_DATA_DIR);
typedef std::map<const AIInterfaceKey, std::set<std::string> > T_dupInt;
T_dupInt duplicateInterfaceInfoCheck;
for (T_dirs::iterator dir = aiInterfaceDataDirs.begin();
dir != aiInterfaceDataDirs.end(); ++dir) {
const std::string& possibleDataDir = *dir;
T_dirs infoFile =
CFileHandler::FindFiles(possibleDataDir, "InterfaceInfo.lua");
if (infoFile.size() > 0) { // interface info is available
// generate and store the interface info
CAIInterfaceLibraryInfo* interfaceInfo =
new CAIInterfaceLibraryInfo(infoFile.at(0));
interfaceInfo->SetDataDir(noSlashAtEnd(possibleDataDir));
interfaceInfo->SetDataDirCommon(
std::string(possibleDataDir) + "common");
AIInterfaceKey interfaceKey = interfaceInfo->GetKey();
interfaceKeys.insert(interfaceKey);
interfaceInfos[interfaceKey] = interfaceInfo;
// so we can check if one interface is specified multiple times
duplicateInterfaceInfoCheck[interfaceKey].insert(infoFile.at(0));
}
}
// filter out interfaces that are specified multiple times
for (T_dupInt::const_iterator info = duplicateInterfaceInfoCheck.begin();
info != duplicateInterfaceInfoCheck.end(); ++info) {
if (info->second.size() >= 2) {
duplicateInterfaceInfos[info->first] = info->second;
logOutput.Print("WARNING: Duplicate AI Interface Info found:");
logOutput.Print("\tfor interface: %s %s", info->first.GetShortName().c_str(),
info->first.GetVersion().c_str());
logOutput.Print("\tin files:");
const std::string* lastDir = NULL;
std::set<std::string>::const_iterator dir;
for (dir = info->second.begin(); dir != info->second.end(); ++dir) {
logOutput.Print("\t%s", dir->c_str());
lastDir = &(*dir);
}
logOutput.Print("\tusing: %s", lastDir->c_str());
}
}
// Read from Skirmish AI info and option files
// we are looking for:
// {SKIRMISH_AI_DATA_DIR}/{*}/{*}/AIInfo.lua
T_dirs skirmishAIDataDirs = filesystem.FindDirsInDirectSubDirs(SKIRMISH_AI_DATA_DIR);
T_dupSkirm duplicateSkirmishAIInfoCheck;
for (T_dirs::iterator dir = skirmishAIDataDirs.begin();
dir != skirmishAIDataDirs.end(); ++dir) {
const std::string& possibleDataDir = *dir;
T_dirs infoFile = CFileHandler::FindFiles(possibleDataDir,
"AIInfo.lua");
if (infoFile.size() > 0) { // skirmish AI info is available
std::string optionFileName = "";
T_dirs optionFile = CFileHandler::FindFiles(possibleDataDir,
"AIOptions.lua");
if (optionFile.size() > 0) {
optionFileName = optionFile.at(0);
}
// generate and store the ai info
CSkirmishAILibraryInfo* skirmishAIInfo =
new CSkirmishAILibraryInfo(infoFile.at(0), optionFileName);
skirmishAIInfo->SetDataDir(noSlashAtEnd(possibleDataDir));
skirmishAIInfo->SetDataDirCommon(
std::string(possibleDataDir) + "common");
skirmishAIInfo->SetLuaAI(false);
SkirmishAIKey aiKey = skirmishAIInfo->GetKey();
AIInterfaceKey interfaceKey =
FindFittingInterfaceSpecifier(
skirmishAIInfo->GetInterfaceShortName(),
skirmishAIInfo->GetInterfaceVersion(),
interfaceKeys);
if (!interfaceKey.IsUnspecified()) {
SkirmishAIKey skirmishAIKey = SkirmishAIKey(aiKey, interfaceKey);
skirmishAIKeys.insert(skirmishAIKey);
skirmishAIInfos[skirmishAIKey] = skirmishAIInfo;
// so we can check if one skirmish AI is specified multiple times
duplicateSkirmishAIInfoCheck[skirmishAIKey].insert(infoFile.at(0));
}
}
}
// filter out skirmish AIs that are specified multiple times
for (T_dupSkirm::const_iterator info = duplicateSkirmishAIInfoCheck.begin();
info != duplicateSkirmishAIInfoCheck.end(); ++info) {
if (info->second.size() >= 2) {
duplicateSkirmishAIInfos[info->first] = info->second;
logOutput.Print("WARNING: Duplicate Skirmish AI Info found:");
logOutput.Print("\tfor Skirmish AI: %s %s", info->first.GetShortName().c_str(),
info->first.GetVersion().c_str());
logOutput.Print("\tin files:");
const std::string* lastDir = NULL;
std::set<std::string>::const_iterator dir;
for (dir = info->second.begin(); dir != info->second.end(); ++dir) {
logOutput.Print("\t%s", dir->c_str());
lastDir = &(*dir);
}
logOutput.Print("\tusing: %s", lastDir->c_str());
}
}
}
void CAILibraryManager::ClearAllInfos() {
IAILibraryManager::T_interfaceInfos::iterator iii;
for (iii=interfaceInfos.begin(); iii!=interfaceInfos.end(); iii++) {
delete iii->second;
iii->second = NULL;
}
IAILibraryManager::T_skirmishAIInfos::iterator sai;
for (sai=skirmishAIInfos.begin(); sai!=skirmishAIInfos.end(); sai++) {
delete sai->second;
sai->second = NULL;
}
interfaceInfos.clear();
skirmishAIInfos.clear();
interfaceKeys.clear();
skirmishAIKeys.clear();
}
CAILibraryManager::~CAILibraryManager() {
ReleaseEverything();
}
const IAILibraryManager::T_interfaceSpecs& CAILibraryManager::GetInterfaceKeys() const {
return interfaceKeys;
}
const IAILibraryManager::T_skirmishAIKeys& CAILibraryManager::GetSkirmishAIKeys() const {
return skirmishAIKeys;
}
const IAILibraryManager::T_interfaceInfos& CAILibraryManager::GetInterfaceInfos() const {
return interfaceInfos;
}
const IAILibraryManager::T_skirmishAIInfos& CAILibraryManager::GetSkirmishAIInfos() const {
return skirmishAIInfos;
}
const IAILibraryManager::T_dupInt& CAILibraryManager::GetDuplicateInterfaceInfos() const {
return duplicateInterfaceInfos;
}
const IAILibraryManager::T_dupSkirm& CAILibraryManager::GetDuplicateSkirmishAIInfos() const {
return duplicateSkirmishAIInfos;
}
std::vector<SkirmishAIKey> CAILibraryManager::FittingSkirmishAIKeys(
const SkirmishAIKey& skirmishAIKey) const {
std::vector<SkirmishAIKey> applyingKeys;
if (skirmishAIKey.IsUnspecified()) {
return applyingKeys;
}
bool checkVersion = false;
if (skirmishAIKey.GetVersion() != "") {
checkVersion = true;
}
std::set<SkirmishAIKey>::const_iterator sasi;
for (sasi=skirmishAIKeys.begin(); sasi!=skirmishAIKeys.end(); sasi++) {
// check if the ai name fits
if (skirmishAIKey.GetShortName() != sasi->GetShortName()) {
continue;
}
// check if the ai version fits (if one is specified)
if (checkVersion && skirmishAIKey.GetVersion() != sasi->GetVersion()) {
continue;
}
// if the programm raches here, we know that this key fits
applyingKeys.push_back(*sasi);
}
return applyingKeys;
}
const CSkirmishAILibrary* CAILibraryManager::FetchSkirmishAILibrary(const SkirmishAIKey& skirmishAIKey) {
T_skirmishAIInfos::const_iterator aiInfo = skirmishAIInfos.find(skirmishAIKey);
if (aiInfo == skirmishAIInfos.end()) {
logOutput.Print(
"Aborting the game, unknown skirmish AI specified: %s %s",
skirmishAIKey.GetShortName().c_str(),
skirmishAIKey.GetVersion().c_str()
);
return NULL;
}
return FetchInterface(skirmishAIKey.GetInterface())->FetchSkirmishAILibrary(*(aiInfo->second));
}
void CAILibraryManager::ReleaseSkirmishAILibrary(const SkirmishAIKey& skirmishAIKey) {
FetchInterface(skirmishAIKey.GetInterface())->ReleaseSkirmishAILibrary(skirmishAIKey);
ReleaseInterface(skirmishAIKey.GetInterface()); // only releases the library if its load count is 0
}
void CAILibraryManager::ReleaseAllSkirmishAILibraries() {
T_loadedInterfaces::const_iterator lil;
for (lil = loadedAIInterfaceLibraries.begin(); lil != loadedAIInterfaceLibraries.end(); lil++) {
FetchInterface(lil->first)->ReleaseAllSkirmishAILibraries();
ReleaseInterface(lil->first); // only releases the library if its load count is 0
}
}
CAIInterfaceLibrary* CAILibraryManager::FetchInterface(const AIInterfaceKey& interfaceKey) {
CAIInterfaceLibrary* interfaceLib = NULL;
T_loadedInterfaces::const_iterator interfacePos = loadedAIInterfaceLibraries.find(interfaceKey);
if (interfacePos == loadedAIInterfaceLibraries.end()) { // interface not yet loaded
T_interfaceInfos::const_iterator interfaceInfo = interfaceInfos.find(interfaceKey);
if (interfaceInfo != interfaceInfos.end()) {
interfaceLib = new CAIInterfaceLibrary(*(interfaceInfo->second));
loadedAIInterfaceLibraries[interfaceKey] = interfaceLib;
} else {
// unavailable interface requested, returning NULL
}
} else {
interfaceLib = interfacePos->second;
}
return interfaceLib;
}
void CAILibraryManager::ReleaseInterface(const AIInterfaceKey& interfaceKey) {
T_loadedInterfaces::iterator interfacePos = loadedAIInterfaceLibraries.find(interfaceKey);
if (interfacePos != loadedAIInterfaceLibraries.end()) {
CAIInterfaceLibrary* interfaceLib = interfacePos->second;
if (interfaceLib->GetLoadCount() == 0) {
loadedAIInterfaceLibraries.erase(interfacePos);
delete interfaceLib;
interfaceLib = NULL;
}
}
}
/** unloads all interfaces and AIs */
void CAILibraryManager::ReleaseEverything() {
ReleaseAllSkirmishAILibraries();
}
AIInterfaceKey CAILibraryManager::FindFittingInterfaceSpecifier(
const std::string& shortName,
const std::string& minVersion,
const IAILibraryManager::T_interfaceSpecs& keys) {
std::set<AIInterfaceKey>::const_iterator key;
int minDiff = INT_MAX;
AIInterfaceKey fittingKey = AIInterfaceKey(); // unspecified key
for (key=keys.begin(); key!=keys.end(); key++) {
if (shortName == key->GetShortName()) {
int diff = IAILibraryManager::VersionCompare(key->GetVersion(), minVersion);
if (diff >= 0 && diff < minDiff) {
fittingKey = *key;
minDiff = diff;
}
}
}
return fittingKey;
}
|