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
|
#include "StdInc.h"
#include "CGameInterface.h"
#include "BattleState.h"
#include "VCMIDirs.h"
#ifdef VCMI_WINDOWS
#include <windows.h> //for .dll libs
#else
#include <dlfcn.h>
#endif
#include "serializer/BinaryDeserializer.h"
#include "serializer/BinarySerializer.h"
/*
* CGameInterface.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#ifdef VCMI_ANDROID
// we can't use shared libraries on Android so here's a hack
extern "C" DLL_EXPORT void VCAI_GetAiName(char* name);
extern "C" DLL_EXPORT void VCAI_GetNewAI(std::shared_ptr<CGlobalAI> &out);
extern "C" DLL_EXPORT void StupidAI_GetAiName(char* name);
extern "C" DLL_EXPORT void StupidAI_GetNewBattleAI(std::shared_ptr<CGlobalAI> &out);
extern "C" DLL_EXPORT void BattleAI_GetAiName(char* name);
extern "C" DLL_EXPORT void BattleAI_GetNewBattleAI(std::shared_ptr<CBattleGameInterface> &out);
#endif
template<typename rett>
std::shared_ptr<rett> createAny(const boost::filesystem::path& libpath, const std::string& methodName)
{
typedef void(*TGetAIFun)(std::shared_ptr<rett>&);
typedef void(*TGetNameFun)(char*);
char temp[150];
TGetAIFun getAI = nullptr;
TGetNameFun getName = nullptr;
#ifdef VCMI_ANDROID
// this is awful but it seems using shared libraries on some devices is even worse
const std::string filename = libpath.filename().string();
if (filename == "libVCAI.so")
{
getName = (TGetNameFun)VCAI_GetAiName;
getAI = (TGetAIFun)VCAI_GetNewAI;
}
else if (filename == "libStupidAI.so")
{
getName = (TGetNameFun)StupidAI_GetAiName;
getAI = (TGetAIFun)StupidAI_GetNewBattleAI;
}
else if (filename == "libBattleAI.so")
{
getName = (TGetNameFun)BattleAI_GetAiName;
getAI = (TGetAIFun)BattleAI_GetNewBattleAI;
}
else
throw std::runtime_error("Don't know what to do with " + libpath.string() + " and method " + methodName);
#else // !VCMI_ANDROID
#ifdef VCMI_WINDOWS
HMODULE dll = LoadLibraryW(libpath.c_str());
if (dll)
{
getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
}
#else // !VCMI_WINDOWS
void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
if (dll)
{
getName = (TGetNameFun)dlsym(dll, "GetAiName");
getAI = (TGetAIFun)dlsym(dll, methodName.c_str());
}
else
logGlobal->errorStream() << "Error: " << dlerror();
#endif // VCMI_WINDOWS
if (!dll)
{
logGlobal->errorStream() << "Cannot open dynamic library ("<<libpath<<"). Throwing...";
throw std::runtime_error("Cannot open dynamic library");
}
else if(!getName || !getAI)
{
logGlobal->errorStream() << libpath << " does not export method " << methodName;
#ifdef VCMI_WINDOWS
FreeLibrary(dll);
#else
dlclose(dll);
#endif
throw std::runtime_error("Cannot find method " + methodName);
}
#endif // VCMI_ANDROID
getName(temp);
logGlobal->infoStream() << "Loaded " << temp;
std::shared_ptr<rett> ret;
getAI(ret);
if(!ret)
logGlobal->error("Cannot get AI!");
return ret;
}
template<typename rett>
std::shared_ptr<rett> createAnyAI(std::string dllname, const std::string& methodName)
{
logGlobal->infoStream() << "Opening " << dllname;
const boost::filesystem::path filePath =
VCMIDirs::get().libraryPath() / "AI" / VCMIDirs::get().libraryName(dllname);
auto ret = createAny<rett>(filePath, methodName);
ret->dllName = std::move(dllname);
return ret;
}
std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
{
return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
}
std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname )
{
return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
}
std::shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
{
return createAny<CScriptingModule>(dllname, "GetNewModule");
}
BattleAction CGlobalAI::activeStack( const CStack * stack )
{
BattleAction ba; ba.actionType = Battle::DEFEND;
ba.stackNumber = stack->ID;
return ba;
}
CGlobalAI::CGlobalAI()
{
human = false;
}
void CAdventureAI::battleNewRound(int round)
{
battleAI->battleNewRound(round);
}
void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
{
battleAI->battleCatapultAttacked(ca);
}
void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
{
assert(!battleAI);
assert(cbc);
battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
battleAI->init(cbc);
battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
}
void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
{
battleAI->battleStacksAttacked(bsa);
}
void CAdventureAI::actionStarted(const BattleAction &action)
{
battleAI->actionStarted(action);
}
void CAdventureAI::battleNewRoundFirst(int round)
{
battleAI->battleNewRoundFirst(round);
}
void CAdventureAI::actionFinished(const BattleAction &action)
{
battleAI->actionFinished(action);
}
void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
{
battleAI->battleStacksEffectsSet(sse);
}
void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
{
battleAI->battleStacksRemoved(bsr);
}
void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
{
battleAI->battleObstaclesRemoved(removedObstacles);
}
void CAdventureAI::battleNewStackAppeared(const CStack * stack)
{
battleAI->battleNewStackAppeared(stack);
}
void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
{
battleAI->battleStackMoved(stack, dest, distance);
}
void CAdventureAI::battleAttack(const BattleAttack *ba)
{
battleAI->battleAttack(ba);
}
void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
{
battleAI->battleSpellCast(sc);
}
void CAdventureAI::battleEnd(const BattleResult *br)
{
battleAI->battleEnd(br);
battleAI.reset();
}
void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
{
battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
}
BattleAction CAdventureAI::activeStack(const CStack * stack)
{
return battleAI->activeStack(stack);
}
void CAdventureAI::yourTacticPhase(int distance)
{
battleAI->yourTacticPhase(distance);
}
void CAdventureAI::saveGame(BinarySerializer & h, const int version) /*saving */
{
LOG_TRACE_PARAMS(logAi, "version '%i'", version);
CGlobalAI::saveGame(h, version);
bool hasBattleAI = static_cast<bool>(battleAI);
h & hasBattleAI;
if(hasBattleAI)
{
h & std::string(battleAI->dllName);
battleAI->saveGame(h, version);
}
}
void CAdventureAI::loadGame(BinaryDeserializer & h, const int version) /*loading */
{
LOG_TRACE_PARAMS(logAi, "version '%i'", version);
CGlobalAI::loadGame(h, version);
bool hasBattleAI = false;
h & hasBattleAI;
if(hasBattleAI)
{
std::string dllName;
h & dllName;
battleAI = CDynLibHandler::getNewBattleAI(dllName);
assert(cbc); //it should have been set by the one who new'ed us
battleAI->init(cbc);
//battleAI->loadGame(h, version);
}
}
void CBattleGameInterface::saveGame(BinarySerializer & h, const int version)
{
}
void CBattleGameInterface::loadGame(BinaryDeserializer & h, const int version)
{
}
|