Files
c-evo-evo/ai/include/Cevo/AiModule.h
T

42 lines
1.4 KiB
C++

#pragma once
#include "Cevo/Protocol.h"
#include <string>
namespace Cevo {
// Cross-platform dynamic-library loader for brain modules (AI plugins, and
// in principle any other module speaking the same protocol) exporting the
// "client" entry point defined by Protocol.h (TClientCall). This is the
// host-side counterpart of AIProject.lpr's `library` wrapper: it lets the
// game load precompiled AI .dll/.so files (StdAI, Capital AI, KIAI, ...)
// exactly as the original Pascal game did, with no dependency on Qt or any
// other framework - just the platform's native dynamic-loading API
// (LoadLibrary/GetProcAddress on Windows, dlopen/dlsym on POSIX).
class AiModule {
public:
AiModule() = default;
~AiModule();
AiModule(const AiModule &) = delete;
AiModule &operator=(const AiModule &) = delete;
// fileName: a .dll on Windows, .so on Linux/BSD, .dylib on macOS.
// Returns false (with errorMessage() set) if the file can't be loaded or
// doesn't export a "client" symbol.
bool load(const std::string &fileName);
void unload();
bool isLoaded() const { return m_client != nullptr; }
const std::string &errorMessage() const { return m_error; }
// Invokes the module's Client(Command, Player, Data) entry point.
void call(int command, int player, void *data) const;
private:
void *m_handle = nullptr;
TClientCall m_client = nullptr;
std::string m_error;
};
} // namespace Cevo