diff --git a/conanfile.py b/conanfile.py index 8302b06..0a5496a 100644 --- a/conanfile.py +++ b/conanfile.py @@ -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: diff --git a/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp b/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp index 7072774..067a783 100644 --- a/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp +++ b/src/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp @@ -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(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 \ No newline at end of file diff --git a/src/Library/Items/Item.hpp b/src/Library/Items/Item.hpp index e76059e..2e83c8d 100644 --- a/src/Library/Items/Item.hpp +++ b/src/Library/Items/Item.hpp @@ -3,7 +3,13 @@ #include 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 flags) + : CreatureLib::Library::Item(name, category, battleCategory, price, flags) {} + }; } #endif // PKMNLIB_ITEM_HPP diff --git a/tests/ScriptTests/ScriptTypeTests/ItemDataTests.cpp b/tests/ScriptTests/ScriptTypeTests/ItemDataTests.cpp new file mode 100644 index 0000000..e260b8f --- /dev/null +++ b/tests/ScriptTests/ScriptTypeTests/ItemDataTests.cpp @@ -0,0 +1,58 @@ +#ifdef TESTS_BUILD +#include "../../../extern/catch.hpp" +#include "../../../src/AngelScript/AngelScripResolver.hpp" +#include "../../TestLibrary/TestLibrary.hpp" + +static std::unordered_map _scripts = + std::unordered_map{{"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(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(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(item)); + auto name = item->GetName(); + data.Context->SetArgAddress(1, &name); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnWord()); +} + +#endif \ No newline at end of file diff --git a/tests/ScriptTests/ScriptTypeTests/MoveTests.cpp b/tests/ScriptTests/ScriptTypeTests/MoveTests.cpp index 6653879..a348e93 100644 --- a/tests/ScriptTests/ScriptTypeTests/MoveTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/MoveTests.cpp @@ -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(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(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") { diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 955b2c9..19079cb 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -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(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(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(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(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(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(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; } diff --git a/tests/TestLibrary/TestLibrary.hpp b/tests/TestLibrary/TestLibrary.hpp index 15913cd..ef01e44 100644 --- a/tests/TestLibrary/TestLibrary.hpp +++ b/tests/TestLibrary/TestLibrary.hpp @@ -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();