Adds caching for expensive type resolution.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-09-22 19:02:01 +02:00
parent 7e9e574577
commit e5ea2bbc90
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
22 changed files with 124 additions and 42 deletions

View File

@ -1,4 +1,5 @@
#include "AngelScriptEvolutionScript.hpp" #include "AngelScriptEvolutionScript.hpp"
#include "AngelScriptFunctionCall.hpp"
#include "AngelScriptResolver.hpp" #include "AngelScriptResolver.hpp"
void AngelScriptEvolutionScript::DoesEvolveFromLevelUp( void AngelScriptEvolutionScript::DoesEvolveFromLevelUp(
@ -8,7 +9,7 @@ void AngelScriptEvolutionScript::DoesEvolveFromLevelUp(
return; return;
} }
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__DoesEvolveFromLevelUp.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __DoesEvolveFromLevelUp.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) { [&]([[maybe_unused]] asIScriptContext* ctx) {
ctx->SetArgObject(0, (void*)evolution.GetRaw()); ctx->SetArgObject(0, (void*)evolution.GetRaw());
ctx->SetArgObject(1, (void*)pokemon.GetRaw()); ctx->SetArgObject(1, (void*)pokemon.GetRaw());

View File

@ -1,8 +1,8 @@
#ifndef PKMNLIB_ANGELSCRIPTEVOLUTIONSCRIPT_HPP #ifndef PKMNLIB_ANGELSCRIPTEVOLUTIONSCRIPT_HPP
#define PKMNLIB_ANGELSCRIPTEVOLUTIONSCRIPT_HPP #define PKMNLIB_ANGELSCRIPTEVOLUTIONSCRIPT_HPP
#include <angelscript.h>
#include "../../Battling/Pokemon/Pokemon.hpp" #include "../../Battling/Pokemon/Pokemon.hpp"
#include "../../Library/Evolutions/EvolutionData.hpp" #include "../../Library/Evolutions/EvolutionData.hpp"
#include "AngelScriptFunctionCall.hpp"
class AngelScriptResolver; class AngelScriptResolver;

View File

@ -1,12 +1,13 @@
#ifndef PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP #ifndef PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP
#define PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP #define PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP
#include "AngelScriptResolver.hpp"
#include "ContextPool.hpp" #include "ContextPool.hpp"
class AngelScriptUtils { class AngelScriptUtils {
public: public:
static void AngelscriptFunctionCall(asIScriptFunction* func, ContextPool* ctxPool, asIScriptObject* obj, static void AngelscriptFunctionCall(asIScriptFunction* func, ContextPool* ctxPool, asIScriptObject* obj,
const ArbUt::StringView& scriptName, AngelScriptResolver* resolver, const ArbUt::StringView& scriptName,
const std::function<void(asIScriptContext*)>& setup, const std::function<void(asIScriptContext*)>& setup,
const std::function<void(asIScriptContext*)>& onEnd) { const std::function<void(asIScriptContext*)>& onEnd) {
auto ctx = asGetActiveContext(); auto ctx = asGetActiveContext();
@ -17,6 +18,9 @@ public:
} else { } else {
ctx->PushState(); ctx->PushState();
} }
if (ctx->GetUserData() == nullptr) {
ctx->SetUserData(resolver->GetUserdata());
}
ctx->Prepare(func); ctx->Prepare(func);
ctx->SetObject(obj); ctx->SetObject(obj);
setup(ctx); setup(ctx);

View File

@ -1,10 +1,12 @@
#include "AngelScriptItemUseScript.hpp" #include "AngelScriptItemUseScript.hpp"
#include "AngelScriptFunctionCall.hpp"
#include "AngelScriptResolver.hpp" #include "AngelScriptResolver.hpp"
#include "AngelscriptUserdata.hpp"
CScriptArray* CScriptArray*
AngelScriptItemUseScript::GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls) { AngelScriptItemUseScript::GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls) {
asITypeInfo* t = _resolver->GetBaseType("array<EffectParameter@>"_cnc); auto* ud = _resolver->GetUserdata();
CScriptArray* arr = CScriptArray::Create(t, ls.Count()); auto* arr = ud->CreateArray("array<EffectParameter@>"_cnc, ls.Count());
for (size_t i = 0; i < ls.Count(); i++) { for (size_t i = 0; i < ls.Count(); i++) {
arr->SetValue(i, (void**)&ls[i]); arr->SetValue(i, (void**)&ls[i]);
} }
@ -15,7 +17,7 @@ void AngelScriptItemUseScript::OnInitialize(const ArbUt::List<CreatureLib::Libra
if (__OnInitialize.Exists) { if (__OnInitialize.Exists) {
CScriptArray* arr = nullptr; CScriptArray* arr = nullptr;
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__OnInitialize.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __OnInitialize.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) { [&]([[maybe_unused]] asIScriptContext* ctx) {
arr = GetEffectParameters(parameters); arr = GetEffectParameters(parameters);
ctx->SetArgAddress(0, arr); ctx->SetArgAddress(0, arr);
@ -32,7 +34,7 @@ bool AngelScriptItemUseScript::IsItemUsable() const {
} }
bool res = false; bool res = false;
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__IsItemUsable.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __IsItemUsable.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) {}, [&]([[maybe_unused]] asIScriptContext* ctx) {},
[&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; }); [&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; });
return res; return res;
@ -44,7 +46,7 @@ bool AngelScriptItemUseScript::IsCreatureUseItem() const {
} }
bool res = false; bool res = false;
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__IsPokemonUseItem.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __IsPokemonUseItem.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) {}, [&]([[maybe_unused]] asIScriptContext* ctx) {},
[&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; }); [&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; });
return res; return res;
@ -55,7 +57,7 @@ bool AngelScriptItemUseScript::IsUseValidForCreature(CreatureLib::Battling::Crea
} }
bool res = false; bool res = false;
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__IsUseValidForPokemon.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __IsUseValidForPokemon.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) { ctx->SetArgObject(0, (void*)creature); }, [&]([[maybe_unused]] asIScriptContext* ctx) { ctx->SetArgObject(0, (void*)creature); },
[&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; }); [&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; });
return res; return res;
@ -66,7 +68,7 @@ bool AngelScriptItemUseScript::IsHoldable() const {
} }
bool res = false; bool res = false;
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__IsHoldable.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __IsHoldable.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) {}, [&]([[maybe_unused]] asIScriptContext* ctx) {},
[&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; }); [&]([[maybe_unused]] asIScriptContext* ctx) { res = ctx->GetReturnByte() == 1; });
return res; return res;
@ -76,7 +78,7 @@ void AngelScriptItemUseScript::OnUse() const {
CreatureLib::Battling::ItemUseScript::OnUse(); CreatureLib::Battling::ItemUseScript::OnUse();
} }
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__OnUse.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __OnUse.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) {}, [&]([[maybe_unused]] asIScriptContext* ctx) {}); [&]([[maybe_unused]] asIScriptContext* ctx) {}, [&]([[maybe_unused]] asIScriptContext* ctx) {});
} }
void AngelScriptItemUseScript::OnCreatureUse(CreatureLib::Battling::Creature* creature) const { void AngelScriptItemUseScript::OnCreatureUse(CreatureLib::Battling::Creature* creature) const {
@ -84,7 +86,7 @@ void AngelScriptItemUseScript::OnCreatureUse(CreatureLib::Battling::Creature* cr
CreatureLib::Battling::ItemUseScript::OnUse(); CreatureLib::Battling::ItemUseScript::OnUse();
} }
AngelScriptUtils::AngelscriptFunctionCall( AngelScriptUtils::AngelscriptFunctionCall(
__OnPokemonUse.Function, _resolver->GetContextPool(), _scriptObject, ""_cnc, __OnPokemonUse.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&]([[maybe_unused]] asIScriptContext* ctx) { ctx->SetArgObject(0, (void*)creature); }, [&]([[maybe_unused]] asIScriptContext* ctx) { ctx->SetArgObject(0, (void*)creature); },
[&]([[maybe_unused]] asIScriptContext* ctx) {}); [&]([[maybe_unused]] asIScriptContext* ctx) {});
} }

View File

@ -3,7 +3,6 @@
#include <CreatureLib/Battling/ScriptHandling/ItemUseScript.hpp> #include <CreatureLib/Battling/ScriptHandling/ItemUseScript.hpp>
#include "../../../extern/angelscript_addons/scriptarray/scriptarray.h" #include "../../../extern/angelscript_addons/scriptarray/scriptarray.h"
#include "AngelScriptFunctionCall.hpp"
class AngelScriptResolver; class AngelScriptResolver;

View File

@ -9,8 +9,10 @@
#include "../../Battling/PkmnScriptCategory.hpp" #include "../../Battling/PkmnScriptCategory.hpp"
#include "../../Battling/Pokemon/Pokemon.hpp" #include "../../Battling/Pokemon/Pokemon.hpp"
#include "AngelScriptMetadata.hpp" #include "AngelScriptMetadata.hpp"
#include "AngelscriptUserdata.hpp"
#include "ByteCodeHandling/FileByteCodeStream.hpp" #include "ByteCodeHandling/FileByteCodeStream.hpp"
#include "ByteCodeHandling/MemoryByteCodeStream.hpp" #include "ByteCodeHandling/MemoryByteCodeStream.hpp"
#include "ContextPool.hpp"
#include "TypeRegistry/BasicScriptClass.hpp" #include "TypeRegistry/BasicScriptClass.hpp"
#include "TypeRegistry/Battling/RegisterBattleClass.hpp" #include "TypeRegistry/Battling/RegisterBattleClass.hpp"
#include "TypeRegistry/Battling/RegisterBattleLibrary.hpp" #include "TypeRegistry/Battling/RegisterBattleLibrary.hpp"
@ -48,6 +50,22 @@ static void TranslateException(asIScriptContext* ctx, void* /*userParam*/) {
} }
} }
AngelScriptResolver::AngelScriptResolver() : _userData(new AngelscriptUserdata(this)) {}
AngelScriptResolver::~AngelScriptResolver() {
for (const auto& ius : _itemUseScripts) {
delete ius.second;
}
delete _contextPool;
for (const auto& category : _typeDatabase) {
for (const auto& type : category.second) {
delete type.second;
}
}
delete _userData;
_engine->ShutDownAndRelease();
}
void AngelScriptResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg, bool includeStandard) { void AngelScriptResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg, bool includeStandard) {
for (auto scriptCategory : ScriptCategoryHelper::GetValues()) { for (auto scriptCategory : ScriptCategoryHelper::GetValues()) {
_typeDatabase.Insert(scriptCategory, {}); _typeDatabase.Insert(scriptCategory, {});
@ -101,7 +119,7 @@ void AngelScriptResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg,
_engine->RegisterGlobalProperty("const StaticLibrary@ StaticLib", (void*)staticLib.get()); _engine->RegisterGlobalProperty("const StaticLibrary@ StaticLib", (void*)staticLib.get());
} }
_contextPool = new ContextPool(_engine); _contextPool = new ContextPool(_engine, _userData);
asPrepareMultithread(); asPrepareMultithread();
} }
@ -209,6 +227,7 @@ AngelScriptResolver::LoadEvolutionScript(const ArbUt::StringView& view) {
return nullptr; return nullptr;
} }
auto* ctx = _contextPool->RequestContext(); auto* ctx = _contextPool->RequestContext();
auto* factory = typeInfoOption.value().get()->GetFactoryByIndex(0); auto* factory = typeInfoOption.value().get()->GetFactoryByIndex(0);
ctx->Prepare(factory); ctx->Prepare(factory);
auto result = ctx->Execute(); auto result = ctx->Execute();

View File

@ -14,6 +14,8 @@
#include "AngelScriptScript.hpp" #include "AngelScriptScript.hpp"
#include "AngelScriptTypeInfo.hpp" #include "AngelScriptTypeInfo.hpp"
class AngelscriptUserdata;
class AngelScriptResolver final : public PkmnLib::Battling::ScriptResolver { class AngelScriptResolver final : public PkmnLib::Battling::ScriptResolver {
private: private:
asIScriptEngine* _engine = nullptr; asIScriptEngine* _engine = nullptr;
@ -21,6 +23,7 @@ private:
ContextPool* _contextPool = nullptr; ContextPool* _contextPool = nullptr;
CScriptBuilder _builder = {}; CScriptBuilder _builder = {};
std::string _sourceDirectory = {}; std::string _sourceDirectory = {};
AngelscriptUserdata* _userData;
ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, AngelScriptTypeInfo*>> _typeDatabase; ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, AngelScriptTypeInfo*>> _typeDatabase;
ArbUt::Dictionary<ArbUt::StringView, asITypeInfo*> _baseTypes; ArbUt::Dictionary<ArbUt::StringView, asITypeInfo*> _baseTypes;
@ -42,18 +45,8 @@ private:
const ArbUt::StringView& effectName); const ArbUt::StringView& effectName);
public: public:
~AngelScriptResolver() override { AngelScriptResolver();
for (const auto& ius : _itemUseScripts) { ~AngelScriptResolver() override;
delete ius.second;
}
delete _contextPool;
for (const auto& category : _typeDatabase) {
for (const auto& type : category.second) {
delete type.second;
}
}
_engine->ShutDownAndRelease();
}
void Initialize(CreatureLib::Battling::BattleLibrary* library) override { Initialize(library, true); } void Initialize(CreatureLib::Battling::BattleLibrary* library) override { Initialize(library, true); }
void Initialize(CreatureLib::Battling::BattleLibrary* library, bool includeStandard); void Initialize(CreatureLib::Battling::BattleLibrary* library, bool includeStandard);
@ -112,5 +105,7 @@ public:
} }
inline ContextPool* GetContextPool() const noexcept { return _contextPool; } inline ContextPool* GetContextPool() const noexcept { return _contextPool; }
inline AngelscriptUserdata* GetUserdata() const noexcept { return _userData; }
}; };
#endif // PKMNLIB_ANGELSCRIPRESOLVER_HPP #endif // PKMNLIB_ANGELSCRIPRESOLVER_HPP

View File

@ -1,18 +1,18 @@
#include "AngelScriptScript.hpp" #include "AngelScriptScript.hpp"
#include "AngelScriptFunctionCall.hpp" #include "AngelScriptFunctionCall.hpp"
#include "AngelScriptResolver.hpp" #include "AngelScriptResolver.hpp"
#include "AngelscriptUserdata.hpp"
#define CALL_HOOK(name, setup) \ #define CALL_HOOK(name, setup) \
auto s = _type->Get##name(); \ auto s = _type->Get##name(); \
if (!s.Exists) \ if (!s.Exists) \
return; \ return; \
AngelScriptUtils::AngelscriptFunctionCall( \ AngelScriptUtils::AngelscriptFunctionCall( \
s.Function, _ctxPool, _obj, GetName(), [&]([[maybe_unused]] asIScriptContext* ctx) { setup }, \ s.Function, _ctxPool, _obj, _resolver, GetName(), [&]([[maybe_unused]] asIScriptContext* ctx) { setup }, \
[&]([[maybe_unused]] asIScriptContext* ctx) {}); [&]([[maybe_unused]] asIScriptContext* ctx) {});
CScriptArray* AngelScriptScript::GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls) { CScriptArray* AngelScriptScript::GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls) {
asITypeInfo* t = _resolver->GetBaseType("array<EffectParameter@>"_cnc); auto arr = _resolver->GetUserdata()->CreateArray("array<EffectParameter@>"_cnc, ls.Count());
CScriptArray* arr = CScriptArray::Create(t, ls.Count());
for (size_t i = 0; i < ls.Count(); i++) { for (size_t i = 0; i < ls.Count(); i++) {
arr->SetValue(i, (void**)&ls[i]); arr->SetValue(i, (void**)&ls[i]);
} }

View File

@ -6,9 +6,10 @@
#include "../../../extern/angelscript_addons/scriptarray/scriptarray.h" #include "../../../extern/angelscript_addons/scriptarray/scriptarray.h"
#include "../../Battling/PkmnScript.hpp" #include "../../Battling/PkmnScript.hpp"
#include "AngelScriptTypeInfo.hpp" #include "AngelScriptTypeInfo.hpp"
#include "ContextPool.hpp"
class AngelScriptResolver; class AngelScriptResolver;
class ContextPool;
class AngelScriptScript final : public PkmnLib::Battling::PkmnScript { class AngelScriptScript final : public PkmnLib::Battling::PkmnScript {
private: private:
AngelScriptResolver* _resolver = nullptr; AngelScriptResolver* _resolver = nullptr;

View File

@ -0,0 +1,36 @@
#ifndef PKMNLIB_ANGELSCRIPTUSERDATA_HPP
#define PKMNLIB_ANGELSCRIPTUSERDATA_HPP
#include <Arbutils/Collections/Dictionary.hpp>
#include "AngelScriptResolver.hpp"
class AngelscriptUserdata {
AngelScriptResolver* _resolver;
ArbUt::Dictionary<ArbUt::StringView, asITypeInfo*> _cachedTypes;
public:
AngelscriptUserdata(AngelScriptResolver* r) : _resolver(r) {}
~AngelscriptUserdata() {
for (auto& t : _cachedTypes) {
t.second->Release();
}
}
asITypeInfo* GetType(const ArbUt::StringView& name) {
auto v = _cachedTypes.TryGet(name);
if (v.has_value()) {
return v.value();
}
auto t = _resolver->GetBaseType(name);
t->AddRef();
_cachedTypes.Set(name, t);
return t;
}
CScriptArray* CreateArray(const ArbUt::StringView& name, size_t length) {
auto t = GetType(name);
return CScriptArray::Create(t, length);
}
};
#endif // PKMNLIB_ANGELSCRIPTUSERDATA_HPP

View File

@ -4,6 +4,7 @@
#include <angelscript.h> #include <angelscript.h>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include "AngelscriptUserdata.hpp"
class ContextPool { class ContextPool {
struct CtxThreadedPoolStorage { struct CtxThreadedPoolStorage {
@ -19,6 +20,7 @@ class ContextPool {
ArbUt::Dictionary<std::thread::id, CtxThreadedPoolStorage*> _pool; ArbUt::Dictionary<std::thread::id, CtxThreadedPoolStorage*> _pool;
asIScriptEngine* _engine; asIScriptEngine* _engine;
AngelscriptUserdata* _userData;
CtxThreadedPoolStorage* GetThreadPool() { CtxThreadedPoolStorage* GetThreadPool() {
auto id = std::this_thread::get_id(); auto id = std::this_thread::get_id();
@ -30,7 +32,7 @@ class ContextPool {
} }
public: public:
ContextPool(asIScriptEngine* engine) : _engine(engine) {} ContextPool(asIScriptEngine* engine, AngelscriptUserdata* userData) : _engine(engine), _userData(userData) {}
~ContextPool() { ~ContextPool() {
for (const auto& kv : _pool) { for (const auto& kv : _pool) {
@ -48,6 +50,7 @@ public:
pool->Pool.pop_back(); pool->Pool.pop_back();
} else { } else {
ctx = _engine->CreateContext(); ctx = _engine->CreateContext();
ctx->SetUserData(_userData);
} }
return ctx; return ctx;
} }

View File

@ -6,6 +6,7 @@
#include "../../../../Battling/Pokemon/Pokemon.hpp" #include "../../../../Battling/Pokemon/Pokemon.hpp"
#include "../../AngelScriptResolver.hpp" #include "../../AngelScriptResolver.hpp"
#include "../../AngelScriptScript.hpp" #include "../../AngelScriptScript.hpp"
#include "../../AngelscriptUserdata.hpp"
#include "../HelperFile.hpp" #include "../HelperFile.hpp"
void RegisterBattleClass::Register(asIScriptEngine* engine) { void RegisterBattleClass::Register(asIScriptEngine* engine) {
@ -99,14 +100,26 @@ void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) {
void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) { void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) {
REGISTER_GETTER("Battle", "const BattleLibrary@ get_Library() const property", CreatureLib::Battling::Battle, REGISTER_GETTER("Battle", "const BattleLibrary@ get_Library() const property", CreatureLib::Battling::Battle,
GetLibrary); GetLibrary);
REGISTER_GETTER("Battle", "bool get_CanFlee() const property", CreatureLib::Battling::Battle, REGISTER_GETTER("Battle", "bool get_CanFlee() const property", CreatureLib::Battling::Battle, CanFlee);
CanFlee); REGISTER_GETTER("Battle", "uint get_CurrentTurn() const property", CreatureLib::Battling::Battle, GetCurrentTurn);
REGISTER_GETTER("Battle", "uint get_CurrentTurn() const property", CreatureLib::Battling::Battle, REGISTER_GETTER("Battle", "BattleRandom@ get_Random() const property", CreatureLib::Battling::Battle, GetRandom);
GetCurrentTurn);
REGISTER_GETTER("Battle", "BattleRandom@ get_Random() const property", CreatureLib::Battling::Battle,
GetRandom);
REGISTER_GETTER("Battle", "ChoiceQueue@ get_TurnQueue() const property", CreatureLib::Battling::Battle, REGISTER_GETTER("Battle", "ChoiceQueue@ get_TurnQueue() const property", CreatureLib::Battling::Battle,
GetCurrentTurnQueue); GetCurrentTurnQueue);
{
auto l = [](const CreatureLib::Battling::Battle* b) {
const auto& ls = b->GetSides();
auto* ctx = asGetActiveContext();
auto* ud = (AngelscriptUserdata*)ctx->GetUserData();
auto* arr = ud->CreateArray("array<BattleSide@>"_cnc, ls.Count());
for (size_t i = 0; i < ls.Count(); i++) {
arr->SetValue(i, ls[i].GetRaw());
}
return arr;
};
Ensure(engine->RegisterObjectMethod("Battle", "const array<BattleSide@>& get_Sides() const property",
asFUNCTIONPR(l, (const CreatureLib::Battling::Battle*), CScriptArray*),
asCALL_CDECL_OBJFIRST) >= 0);
}
auto r = engine->RegisterObjectMethod("Battle", "bool CanUse(BaseTurnChoice@ choice)", auto r = engine->RegisterObjectMethod("Battle", "bool CanUse(BaseTurnChoice@ choice)",
asMETHOD(PkmnLib::Battling::Battle, CanUse), asCALL_THISCALL); asMETHOD(PkmnLib::Battling::Battle, CanUse), asCALL_THISCALL);

View File

@ -6,6 +6,7 @@
#include "../../../../Battling/Pokemon/Pokemon.hpp" #include "../../../../Battling/Pokemon/Pokemon.hpp"
#include "../../AngelScriptResolver.hpp" #include "../../AngelScriptResolver.hpp"
#include "../../AngelScriptScript.hpp" #include "../../AngelScriptScript.hpp"
#include "../../AngelscriptUserdata.hpp"
#include "../HelperFile.hpp" #include "../HelperFile.hpp"
void RegisterPokemonClass::Register(asIScriptEngine* engine) { void RegisterPokemonClass::Register(asIScriptEngine* engine) {
@ -63,10 +64,9 @@ ENUM__SIZE_WRAPPER(Pkmn_GenderWrapper, PkmnLib::Battling::Pokemon, GetGender)
CScriptArray* GetMoves(const PkmnLib::Battling::Pokemon* obj) { CScriptArray* GetMoves(const PkmnLib::Battling::Pokemon* obj) {
asIScriptContext* ctx = asGetActiveContext(); asIScriptContext* ctx = asGetActiveContext();
if (ctx) { if (ctx) {
asIScriptEngine* engine = ctx->GetEngine();
asITypeInfo* t = engine->GetTypeInfoByDecl("array<LearnedMove@>");
auto a = obj->GetMoves(); auto a = obj->GetMoves();
CScriptArray* arr = CScriptArray::Create(t, a.Count()); auto ud = (AngelscriptUserdata*)ctx->GetUserData();
CScriptArray* arr = ud->CreateArray("array<LearnedMove@>"_cnc, a.Count());
for (size_t i = 0; i < a.Count(); i++) { for (size_t i = 0; i < a.Count(); i++) {
arr->SetValue(i, &a[i]); arr->SetValue(i, &a[i]);
} }

View File

@ -2,6 +2,7 @@
#include "../../extern/doctest.hpp" #include "../../extern/doctest.hpp"
#include "../../src/Battling/Pokemon/CreatePokemon.hpp" #include "../../src/Battling/Pokemon/CreatePokemon.hpp"
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../TestLibrary/TestLibrary.hpp" #include "../TestLibrary/TestLibrary.hpp"
#define AS_CLASS(name, contents) \ #define AS_CLASS(name, contents) \

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../extern/doctest.hpp" #include "../../extern/doctest.hpp"
#include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../TestLibrary/TestLibrary.hpp" #include "../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =

View File

@ -3,6 +3,7 @@
#include "../../../../src/Battling/Battle/Battle.hpp" #include "../../../../src/Battling/Battle/Battle.hpp"
#include "../../../../src/Battling/Pokemon/CreatePokemon.hpp" #include "../../../../src/Battling/Pokemon/CreatePokemon.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../../../TestLibrary/TestLibrary.hpp" #include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =

View File

@ -2,6 +2,7 @@
#include "../../../../extern/doctest.hpp" #include "../../../../extern/doctest.hpp"
#include "../../../../src/Battling/Pokemon/CreatePokemon.hpp" #include "../../../../src/Battling/Pokemon/CreatePokemon.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../../../TestLibrary/TestLibrary.hpp" #include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../../../extern/doctest.hpp" #include "../../../../extern/doctest.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../../../TestLibrary/TestLibrary.hpp" #include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../../../extern/doctest.hpp" #include "../../../../extern/doctest.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../../../TestLibrary/TestLibrary.hpp" #include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../../../extern/doctest.hpp" #include "../../../../extern/doctest.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../../../TestLibrary/TestLibrary.hpp" #include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../../../extern/doctest.hpp" #include "../../../../extern/doctest.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../../../TestLibrary/TestLibrary.hpp" #include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =

View File

@ -1,6 +1,7 @@
#ifdef TESTS_BUILD #ifdef TESTS_BUILD
#include "../../../../extern/doctest.hpp" #include "../../../../extern/doctest.hpp"
#include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp" #include "../../../../src/ScriptResolving/AngelScript/AngelScriptResolver.hpp"
#include "../../../../src/ScriptResolving/AngelScript/ContextPool.hpp"
#include "../../../TestLibrary/TestLibrary.hpp" #include "../../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts = static std::unordered_map<const char*, const char*> _scripts =