Fixed different sized enums being a problem for AngelScript, minor fixes for Item class
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-01-22 10:33:10 +01:00
parent 9372ba4b27
commit 22648e8617
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
7 changed files with 117 additions and 42 deletions

View File

@ -40,7 +40,7 @@ class PkmnLibConan(ConanFile):
self.options["AngelScript"].link_std_statically = True
def requirements(self):
self.requires("CreatureLib/cc46cd46a78d998d6cd6ebb2f76b5b54aa240893@creaturelib/master")
self.requires("CreatureLib/15523a18a556dedf5b27212264a871260608abb6@creaturelib/master")
if self.options.script_handler == "angelscript":
self.requires("AngelScript/2.34@AngelScript/Deukhoofd")
else:

View File

@ -39,6 +39,13 @@ void RegisterMoveTypes::RegisterMoveTarget(asIScriptEngine* engine) {
REGISTER_ENUM_VALUE(MoveTarget, CreatureLib::Library::AttackTarget, Self)
}
// Hack to handle AngelScript not recognizing different sized enums on fields, and returning invalid values due to it.
#define ENUM__SIZE_WRAPPER(name, type, func) \
int32_t name(type* obj) { return static_cast<int32_t>(obj->func()); }
ENUM__SIZE_WRAPPER(Move_CategoryWrapper, PkmnLib::Library::MoveData, GetCategory)
ENUM__SIZE_WRAPPER(Move_TargetWrapper, PkmnLib::Library::MoveData, GetTarget)
void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) {
[[maybe_unused]] int r = engine->RegisterObjectType("MoveData", 0, asOBJ_REF | asOBJ_NOCOUNT);
assert(r >= 0);
@ -49,7 +56,7 @@ void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) {
asMETHOD(PkmnLib::Library::MoveData, GetType), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "MoveCategory get_Category() const property",
asMETHOD(PkmnLib::Library::MoveData, GetCategory), asCALL_THISCALL);
asFUNCTION(Move_CategoryWrapper), asCALL_CDECL_OBJLAST);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "uint8 get_BasePower() const property",
asMETHOD(PkmnLib::Library::MoveData, GetBasePower), asCALL_THISCALL);
@ -61,10 +68,10 @@ void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) {
asMETHOD(PkmnLib::Library::MoveData, GetBaseUsages), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "MoveTarget get_Target() const property",
asMETHOD(PkmnLib::Library::MoveData, GetTarget), asCALL_THISCALL);
asFUNCTION(Move_TargetWrapper), asCALL_CDECL_OBJLAST);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "int8 get_Priority() const property",
asMETHOD(PkmnLib::Library::MoveData, GetTarget), asCALL_THISCALL);
asMETHOD(PkmnLib::Library::MoveData, GetPriority), asCALL_THISCALL);
assert(r >= 0);
r = engine->RegisterObjectMethod("MoveData", "bool HasFlag(const string &in flag) const",
asMETHOD(PkmnLib::Library::MoveData, HasFlag), asCALL_THISCALL);
@ -76,7 +83,6 @@ void RegisterMoveTypes::RegisterMoveLibrary(asIScriptEngine* engine) {
r = engine->RegisterObjectMethod("MoveLibrary", "const MoveData@ GetMove(const string &in name) const",
asMETHOD(PkmnLib::Library::MoveLibrary, GetMove), asCALL_THISCALL);
assert(r >= 0);
}
#undef REGISTER_ENUM_VALUE

View File

@ -3,7 +3,13 @@
#include <Library/Items/Item.hpp>
namespace PkmnLib::Library {
class Item : public CreatureLib::Library::Item {};
class Item : public CreatureLib::Library::Item {
public:
Item(std::string name, CreatureLib::Library::ItemCategory category,
CreatureLib::Library::BattleItemCategory battleCategory, int32_t price,
std::unordered_set<std::string> flags)
: CreatureLib::Library::Item(name, category, battleCategory, price, flags) {}
};
}
#endif // PKMNLIB_ITEM_HPP

View File

@ -0,0 +1,58 @@
#ifdef TESTS_BUILD
#include "../../../extern/catch.hpp"
#include "../../../src/AngelScript/AngelScripResolver.hpp"
#include "../../TestLibrary/TestLibrary.hpp"
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
class testScript1 {
bool testName(const Item@ i, const string &in name){ return i.Name == name; }
}
)"}};
static const char* _testLoadFunc(const char* name) { return _scripts[name]; }
struct ScriptData {
AngelScriptScript* Script;
AngelScripResolver* Resolver;
asIScriptFunction* Func;
asIScriptContext* Context;
~ScriptData() {
Script->GetContextPool()->ReturnContextToPool(Context);
delete Script;
delete Resolver;
}
};
static ScriptData GetScript(PkmnLib::Battling::BattleLibrary* mainLib, const char* funcName) {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(mainLib);
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript("testScript1");
lib->FinalizeModule();
auto s = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1");
auto script = dynamic_cast<AngelScriptScript*>(s);
auto ctxPool = script->GetContextPool();
auto ctx = ctxPool->RequestContext();
auto func = script->PrepareMethod(funcName, ctx);
REQUIRE(func != nullptr);
return {.Script = script, .Resolver = lib, .Func = func, .Context = ctx};
}
TEST_CASE("Validate Item Name in Script") {
auto mainLib = TestLibrary::GetLibrary();
auto data = GetScript(mainLib, "testName");
auto item = mainLib->GetItemLibrary()->GetItem("testItem");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::Item*>(item));
auto name = item->GetName();
data.Context->SetArgAddress(1, &name);
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
}
#endif

View File

@ -79,11 +79,14 @@ TEST_CASE("Validate Move Category in Script") {
auto data = GetScript(mainLib, "testCategory");
auto move = mainLib->GetMoveLibrary()->GetAttack("testMove");
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgDWord(1, (uint32_t)move->GetCategory());
REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnWord());
data.Context->SetArgObject(0, const_cast<PkmnLib::Library::MoveData*>(move));
data.Context->SetArgDWord(1, (asDWORD)move->GetCategory());
auto result = data.Context->Execute();
INFO("exception: " << data.Context->GetExceptionString());
REQUIRE(result == asEXECUTION_FINISHED);
REQUIRE((bool)data.Context->GetReturnByte());
}
TEST_CASE("Validate Move BasePower in Script") {

View File

@ -2,38 +2,43 @@
PkmnLib::Battling::BattleLibrary* TestLibrary::_library = nullptr;
PkmnLib::Library::SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
auto lib = new PkmnLib::Library::SpeciesLibrary();
lib->LoadSpecies("testSpecies", new PkmnLib::Library::PokemonSpecies(
1, "testSpecies",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0},
CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100),
{"testAbility"}, {"testHiddenAbility"},
new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100));
lib->LoadSpecies(
"testSpecies2",
new PkmnLib::Library::PokemonSpecies(
2, "testSpecies2",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0},
CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"},
{"testHiddenAbility"}, new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100));
lib->LoadSpecies(
"statTestSpecies1",
new PkmnLib::Library::PokemonSpecies(
3, "statTestSpecies1",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0},
CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"},
{"testHiddenAbility"}, new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100));
lib->LoadSpecies("testSpecies",
new PkmnLib::Library::PokemonSpecies(
1, "testSpecies",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0},
CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"},
{"testHiddenAbility"}, new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100));
lib->LoadSpecies("testSpecies2",
new PkmnLib::Library::PokemonSpecies(
2, "testSpecies2",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0},
CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"},
{"testHiddenAbility"}, new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100));
lib->LoadSpecies("statTestSpecies1",
new PkmnLib::Library::PokemonSpecies(
3, "statTestSpecies1",
new PkmnLib::Library::PokemonForme(
"default", 1.0f, 1.0f, 100, {0},
CreatureLib::Core::StatisticSet<uint16_t>(100, 100, 100, 100, 100, 100), {"testAbility"},
{"testHiddenAbility"}, new CreatureLib::Library::LearnableAttacks(100)),
0.5f, "testGrowthRate", 100, 100));
return lib;
}
PkmnLib::Library::MoveLibrary* TestLibrary::BuildMoveLibrary() {
auto lib = new PkmnLib::Library::MoveLibrary();
lib->LoadMove("testMove", new PkmnLib::Library::MoveData("testMove", 0, PkmnLib::Library::MoveCategory::Physical,
50, 100, 20, CreatureLib::Library::AttackTarget::Adjacent,0, {}));
lib->LoadMove("testMove",
new PkmnLib::Library::MoveData("testMove", 0, PkmnLib::Library::MoveCategory::Physical, 50, 100, 20,
CreatureLib::Library::AttackTarget::Adjacent, 0, {}));
return lib;
}
PkmnLib::Library::ItemLibrary* TestLibrary::BuildItemLibrary() {
auto lib = new PkmnLib::Library::ItemLibrary();
lib->LoadItem("testItem", new PkmnLib::Library::Item("testItem", CreatureLib::Library::ItemCategory::MiscItem,
CreatureLib::Library::BattleItemCategory::None, 0, {}));
return lib;
}

View File

@ -40,10 +40,7 @@ public:
static PkmnLib::Library::MoveLibrary* BuildMoveLibrary();
static PkmnLib::Library::ItemLibrary* BuildItemLibrary() {
auto lib = new PkmnLib::Library::ItemLibrary();
return lib;
}
static PkmnLib::Library::ItemLibrary* BuildItemLibrary();
static CreatureLib::Library::GrowthRateLibrary* BuildGrowthRateLibrary() {
auto lib = new CreatureLib::Library::GrowthRateLibrary();