Adds C Interface.

This commit is contained in:
Deukhoofd 2021-04-23 10:09:50 +02:00
parent 5898688a65
commit a5d49d7aa4
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 81 additions and 3 deletions

7
CInterface/Core.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "Core.hpp"
std::string ExceptionHandler::_pkmnLibAILastException = "";
std::string ExceptionHandler::_pkmnLibAILastExceptionStacktrace = "Unset";
export const char* pkmnlibai_C_GetLastException() { return ExceptionHandler::GetLastException(); }
export const char* pkmnlibai_C_GetLastExceptionStacktrace() { return ExceptionHandler::GetLastExceptionStacktrace(); }

53
CInterface/Core.hpp Normal file
View File

@ -0,0 +1,53 @@
#ifndef PKMNLIB_AI_CORE_HPP
#define PKMNLIB_AI_CORE_HPP
#include <cstring>
#include <exception>
#include <string>
#define export extern "C" [[maybe_unused]]
#define PkmnLibAIException 5
class ExceptionHandler {
static std::string _pkmnLibAILastException;
static std::string _pkmnLibAILastExceptionStacktrace;
public:
static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e, size_t depth = 6) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_pkmnLibAILastException = ss.str();
_pkmnLibAILastExceptionStacktrace = e.GetStacktrace(depth, true);
if (_pkmnLibAILastExceptionStacktrace.empty())
_pkmnLibAILastExceptionStacktrace = "err";
}
static void SetLastException(const std::string& function, const std::exception& e) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_pkmnLibAILastException = ss.str();
_pkmnLibAILastExceptionStacktrace = "Exception did not have a stacktrace.";
}
static const char* GetLastException() { return _pkmnLibAILastException.c_str(); }
static const char* GetLastExceptionStacktrace() { return _pkmnLibAILastExceptionStacktrace.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \
return PkmnLibAIException; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(__FUNCTION__, e); \
return PkmnLibAIException; \
}
#define SIMPLE_GET_FUNC(type, name, returnType) \
export returnType pkmnlibai_##type##_##name(const type* p) { return p->name(); }
#define SIMPLE_GET_FUNC_SMART_PTR(type, name, returnType) \
export returnType pkmnlibai_##type##_##name(const type* p) { return p->name().GetRaw(); }
#define DESTRUCTOR(type) \
export void pkmnlibai_##type##_Destruct(const type* p) { delete p; }
#endif // PKMNLIB_AI_CORE_HPP

16
CInterface/pkmnlibai.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "../src/DepthSearchAI.hpp"
#include "../src/NaiveAI.hpp"
#include "../src/PokemonAI.hpp"
#include "../src/RandomAI.hpp"
#include "Core.hpp"
export void pkmnlibai_PokemonAI_Delete(PkmnLibAI::PokemonAI* p) { delete p; }
export u8 pkmnlibai_PokemonAI_GetChoice(CreatureLib::Battling::BaseTurnChoice*& out, PkmnLibAI::PokemonAI* p,
PkmnLib::Battling::Battle* battle, PkmnLib::Battling::Pokemon* user) {
Try(out = p->GetChoice(battle, user);)
}
export PkmnLibAI::RandomAI* pkmnlibai_RandomAI_Create() { return new PkmnLibAI::RandomAI(); }
export PkmnLibAI::NaiveAI* pkmnlibai_NaiveAI_Create() { return new PkmnLibAI::NaiveAI(); }
export PkmnLibAI::DepthSearchAI* pkmnlibai_DepthSearchAI_Create() { return new PkmnLibAI::DepthSearchAI(); }

View File

@ -9,7 +9,7 @@ set(STATICC TRUE)
include(CMakeLists.txt.in) include(CMakeLists.txt.in)
include_pkmnlib() include_pkmnlib()
file(GLOB_RECURSE SRC_FILES src/*.cpp src/*.hpp) file(GLOB_RECURSE SRC_FILES src/*.cpp src/*.hpp CInterface/pkmnlibai.cpp CInterface/Core.cpp)
add_library(pkmnlib_ai SHARED ${SRC_FILES}) add_library(pkmnlib_ai SHARED ${SRC_FILES})
target_precompile_headers(pkmnlib_ai PUBLIC src/Precompiled.hxx) target_precompile_headers(pkmnlib_ai PUBLIC src/Precompiled.hxx)
set_target_properties(pkmnlib_ai PROPERTIES LINKER_LANGUAGE CXX) set_target_properties(pkmnlib_ai PROPERTIES LINKER_LANGUAGE CXX)

View File

@ -5,6 +5,7 @@
#include <CreatureLib/Battling/TurnChoices/SwitchTurnChoice.hpp> #include <CreatureLib/Battling/TurnChoices/SwitchTurnChoice.hpp>
#include <PkmnLib/Battling/Pokemon/LearnedMove.hpp> #include <PkmnLib/Battling/Pokemon/LearnedMove.hpp>
#include <algorithm> #include <algorithm>
#include <angelscript.h>
#include <future> #include <future>
#include <thread> #include <thread>
#include "../cmake-build-release/PkmnLib/src/pkmnlib/src/Battling/Pokemon/PokemonParty.hpp" #include "../cmake-build-release/PkmnLib/src/pkmnlib/src/Battling/Pokemon/PokemonParty.hpp"
@ -92,7 +93,7 @@ namespace PkmnLibAI {
})); }));
} }
std::vector<std::tuple<u8, float>> results(threadPool.size()); std::vector<std::tuple<u8, float>> results(threadPool.size());
for (int i = 0; i < threadPool.size(); ++i) { for (size_t i = 0; i < threadPool.size(); ++i) {
results[i] = threadPool[i].get(); results[i] = threadPool[i].get();
} }
return results; return results;
@ -111,7 +112,7 @@ namespace PkmnLibAI {
scoredMoves.emplace_back(moveIndex, scored); scoredMoves.emplace_back(moveIndex, scored);
} }
auto& party = battle->GetParties()[side->GetSideIndex()]->GetParty()->GetParty(); auto& party = battle->GetParties()[side->GetSideIndex()]->GetParty()->GetParty();
for (int i = 0; i < party.Count(); ++i) { for (size_t i = 0; i < party.Count(); ++i) {
auto mon = party[i]; auto mon = party[i];
if (!mon.HasValue()) { if (!mon.HasValue()) {
continue; continue;

View File

@ -15,6 +15,7 @@ namespace PkmnLibAI {
virtual std::string GetName() const noexcept = 0; virtual std::string GetName() const noexcept = 0;
protected:
CreatureLib::Battling::CreatureIndex GetOppositeIndex(PkmnLib::Battling::Pokemon* user) { CreatureLib::Battling::CreatureIndex GetOppositeIndex(PkmnLib::Battling::Pokemon* user) {
auto side = user->GetBattleSide().GetValue(); auto side = user->GetBattleSide().GetValue();
if (side->GetSideIndex() == 0) { if (side->GetSideIndex() == 0) {