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
|
/*
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 "AIInterfaceLibrary.h"
#include "Interface/aidefines.h"
#include "SkirmishAILibrary.h"
#include "AIInterfaceLibraryInfo.h"
#include "SkirmishAILibraryInfo.h"
#include "SAIInterfaceCallbackImpl.h"
#include "System/Util.h"
#include "System/Platform/errorhandler.h"
#include "System/FileSystem/FileHandler.h"
#include "IAILibraryManager.h"
#include "LogOutput.h"
CAIInterfaceLibrary::CAIInterfaceLibrary(const CAIInterfaceLibraryInfo& _info)
: interfaceId(-1), info(_info) {
libFilePath = FindLibFile();
sharedLib = SharedLib::Instantiate(libFilePath);
if (sharedLib == NULL) {
static const int s_msg_sizeMax = 512;
char s_msg[s_msg_sizeMax];
SNPRINTF(s_msg, s_msg_sizeMax,
"Error while loading AI Interface Library from file \"%s\"",
libFilePath.c_str());
handleerror(NULL, s_msg, "AI Interface Error", MBF_OK | MBF_EXCL);
}
InitializeFromLib(libFilePath);
InitStatic();
}
CAIInterfaceLibrary::~CAIInterfaceLibrary() {
ReleaseStatic();
delete sharedLib;
sharedLib = NULL;
}
void CAIInterfaceLibrary::InitStatic() {
if (sAIInterfaceLibrary.initStatic != NULL) {
if (interfaceId == -1) {
interfaceId = aiInterfaceCallback_getInstanceFor(&info, &callback);
}
int ret = sAIInterfaceLibrary.initStatic(interfaceId, &callback);
if (ret != 0) {
// initializing the library failed!
static const int s_msg_sizeMax = 512;
char s_msg[s_msg_sizeMax];
SNPRINTF(s_msg, s_msg_sizeMax,
"Error initializing AI Interface library from file\n\"%s\".\n"
"The call to initStatic() returned unsuccessfuly.",
libFilePath.c_str());
handleerror(NULL, s_msg, "AI Interface Error", MBF_OK | MBF_EXCL);
}
}
}
void CAIInterfaceLibrary::ReleaseStatic() {
if (sAIInterfaceLibrary.releaseStatic != NULL) {
int ret = sAIInterfaceLibrary.releaseStatic();
if (ret != 0) {
// releasing the library failed!
static const int s_msg_sizeMax = 512;
char s_msg[s_msg_sizeMax];
SNPRINTF(s_msg, s_msg_sizeMax,
"Error releasing AI Interface Library from file\n\"%s\".\n"
"The call to releaseStatic() returned unsuccessfuly.",
libFilePath.c_str());
handleerror(NULL, s_msg, "AI Interface Error", MBF_OK | MBF_EXCL);
}
}
if (interfaceId != -1) {
aiInterfaceCallback_release(interfaceId);
}
}
AIInterfaceKey CAIInterfaceLibrary::GetKey() const {
return info.GetKey();
}
LevelOfSupport CAIInterfaceLibrary::GetLevelOfSupportFor(
const std::string& engineVersionString, int engineVersionNumber) const {
//if (sAIInterfaceLibrary.getLevelOfSupportFor != NULL) {
// return sAIInterfaceLibrary.getLevelOfSupportFor(
// engineVersionString.c_str(), engineVersionNumber);
//} else {
return LOS_Unknown;
//}
}
int CAIInterfaceLibrary::GetLoadCount() const {
int totalSkirmishAILibraryLoadCount = 0;
std::map<const SkirmishAIKey, int>::const_iterator salc;
for (salc=skirmishAILoadCount.begin(); salc != skirmishAILoadCount.end();
salc++) {
totalSkirmishAILibraryLoadCount += salc->second;
}
return totalSkirmishAILibraryLoadCount;
}
// used as fallback, when an AI could not be found
static int CALLING_CONV handleEvent_empty(int teamId, int receiver, const void* data) {
return 0; // signaling: OK
}
// Skirmish AI methods
const CSkirmishAILibrary* CAIInterfaceLibrary::FetchSkirmishAILibrary(const CSkirmishAILibraryInfo& aiInfo) {
CSkirmishAILibrary* ai = NULL;
const SkirmishAIKey& skirmishAIKey = aiInfo.GetKey();
if (skirmishAILoadCount[skirmishAIKey] == 0) {
const SSkirmishAILibrary* sLib = sAIInterfaceLibrary.loadSkirmishAILibrary(aiInfo.GetShortName().c_str(), aiInfo.GetVersion().c_str());
if (sLib == NULL) {
logOutput.Print(
"ERROR: Skirmish AI %s-%s not found!\n"
"The game will go on without it.\n"
"This usually indicates a problem in the used "
"AI Interface library (%s-%s),\n"
"or the Skirmish AI library is not in the same "
"place as its AIInfo.lua.",
skirmishAIKey.GetShortName().c_str(),
skirmishAIKey.GetVersion().c_str(),
skirmishAIKey.GetInterface().GetShortName().c_str(),
skirmishAIKey.GetInterface().GetVersion().c_str()
);
struct SSkirmishAILibrary* sLib_empty = new SSkirmishAILibrary();
sLib_empty->getLevelOfSupportFor = NULL;
sLib_empty->init = NULL;
sLib_empty->release = NULL;
sLib_empty->handleEvent = handleEvent_empty;
// NOTE: this causes a memory leack
// as it is never freed anywhere()
// no problem because it is used till the end of the game anyway.
sLib = sLib_empty;
}
ai = new CSkirmishAILibrary(*sLib, skirmishAIKey);
loadedSkirmishAILibraries[skirmishAIKey] = ai;
} else {
ai = loadedSkirmishAILibraries[skirmishAIKey];
}
skirmishAILoadCount[skirmishAIKey]++;
return ai;
}
int CAIInterfaceLibrary::ReleaseSkirmishAILibrary(const SkirmishAIKey& key) {
const IAILibraryManager* libMan = IAILibraryManager::GetInstance();
const CSkirmishAILibraryInfo* aiInfo = libMan->GetSkirmishAIInfos().find(key)->second;
if (skirmishAILoadCount[key] == 0) {
return 0;
}
skirmishAILoadCount[key]--;
if (skirmishAILoadCount[key] == 0) {
loadedSkirmishAILibraries.erase(key);
sAIInterfaceLibrary.unloadSkirmishAILibrary(aiInfo->GetShortName().c_str(), aiInfo->GetVersion().c_str());
}
return skirmishAILoadCount[key];
}
int CAIInterfaceLibrary::GetSkirmishAILibraryLoadCount(const SkirmishAIKey& key) const {
return *&skirmishAILoadCount.find(key)->second;
}
int CAIInterfaceLibrary::ReleaseAllSkirmishAILibraries() {
int releasedAIs = sAIInterfaceLibrary.unloadAllSkirmishAILibraries();
loadedSkirmishAILibraries.clear();
skirmishAILoadCount.clear();
return releasedAIs;
}
void CAIInterfaceLibrary::reportInterfaceFunctionError(
const std::string* libFileName, const std::string* functionName) {
static const int s_msg_sizeMax = 512;
char s_msg[s_msg_sizeMax];
SNPRINTF(s_msg, s_msg_sizeMax,
"Error loading AI Interface Library from file \"%s\": no \"%s\" function exported",
libFileName->c_str(), functionName->c_str());
handleerror(NULL, s_msg, "AI Interface Error", MBF_OK | MBF_EXCL);
}
int CAIInterfaceLibrary::InitializeFromLib(const std::string& libFilePath) {
// TODO: version checking
std::string funcName;
funcName = "initStatic";
sAIInterfaceLibrary.initStatic
= (int (CALLING_CONV_FUNC_POINTER *)(
int interfaceId, const struct SAIInterfaceCallback* callback))
sharedLib->FindAddress(funcName.c_str());
if (sAIInterfaceLibrary.initStatic == NULL) {
// do nothing: it is permitted that an AI does not export this function
//reportInterfaceFunctionError(&libFilePath, &funcName);
//return -1;
}
funcName = "releaseStatic";
sAIInterfaceLibrary.releaseStatic
= (int (CALLING_CONV_FUNC_POINTER *)())
sharedLib->FindAddress(funcName.c_str());
if (sAIInterfaceLibrary.releaseStatic == NULL) {
// do nothing: it is permitted that an AI does not export this function
//reportInterfaceFunctionError(&libFilePath, &funcName);
//return -1;
}
/*
funcName = "getLevelOfSupportFor";
sAIInterfaceLibrary.getLevelOfSupportFor
= (LevelOfSupport (CALLING_CONV_FUNC_POINTER *)(const char*, int))
sharedLib->FindAddress(funcName.c_str());
if (sAIInterfaceLibrary.getLevelOfSupportFor == NULL) {
// do nothing: it is permitted that an AI does not export this function
//reportInterfaceFunctionError(&libFilePath, &funcName);
//return -2;
}
*/
funcName = "loadSkirmishAILibrary";
sAIInterfaceLibrary.loadSkirmishAILibrary
= (const SSkirmishAILibrary* (CALLING_CONV_FUNC_POINTER *)(
const char* const shortName,
const char* const version))
sharedLib->FindAddress(funcName.c_str());
if (sAIInterfaceLibrary.loadSkirmishAILibrary == NULL) {
reportInterfaceFunctionError(&libFilePath, &funcName);
return -4;
}
funcName = "unloadSkirmishAILibrary";
sAIInterfaceLibrary.unloadSkirmishAILibrary
= (int (CALLING_CONV_FUNC_POINTER *)(
const char* const shortName,
const char* const version))
sharedLib->FindAddress(funcName.c_str());
if (sAIInterfaceLibrary.unloadSkirmishAILibrary == NULL) {
reportInterfaceFunctionError(&libFilePath, &funcName);
return -5;
}
funcName = "unloadAllSkirmishAILibraries";
sAIInterfaceLibrary.unloadAllSkirmishAILibraries
= (int (CALLING_CONV_FUNC_POINTER *)())
sharedLib->FindAddress(funcName.c_str());
if (sAIInterfaceLibrary.unloadAllSkirmishAILibraries == NULL) {
reportInterfaceFunctionError(&libFilePath, &funcName);
return -6;
}
return 0;
}
std::string CAIInterfaceLibrary::FindLibFile() {
const std::string& dataDir = info.GetDataDir();
std::string libFileName("AIInterface"); // "AIInterface"
#ifndef _WIN32
libFileName = "lib" + libFileName; // "libAIInterface"
#endif
// eg. libAIInterface.so
libFileName = libFileName + "." + SharedLib::GetLibExtension();
return dataDir + sPS + libFileName;
}
|