diff --git a/src/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp b/src/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp index 351226d..fb4b608 100644 --- a/src/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp +++ b/src/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp @@ -46,6 +46,13 @@ void RegisterItemTypes::RegisterBattleItemCategoryEnum(asIScriptEngine* engine) assert(r >= 0); } +// 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::Item, GetCategory) +ENUM__SIZE_WRAPPER(Move_BattleCategoryWrapper, PkmnLib::Library::Item, GetBattleCategory) + void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("Item", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); @@ -53,10 +60,10 @@ void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) { asMETHOD(PkmnLib::Library::Item, GetName), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("Item", "ItemCategory get_Category() const property", - asMETHOD(PkmnLib::Library::Item, GetCategory), asCALL_THISCALL); + asFUNCTION(Move_CategoryWrapper), asCALL_CDECL_OBJLAST); assert(r >= 0); r = engine->RegisterObjectMethod("Item", "BattleItemCategory get_BattleCategory() const property", - asMETHOD(PkmnLib::Library::Item, GetBattleCategory), asCALL_THISCALL); + asFUNCTION(Move_BattleCategoryWrapper), asCALL_CDECL_OBJLAST); assert(r >= 0); r = engine->RegisterObjectMethod("Item", "int get_Price() const property", asMETHOD(PkmnLib::Library::Item, GetPrice), asCALL_THISCALL); diff --git a/tests/ScriptTests/ScriptTypeTests/ItemDataTests.cpp b/tests/ScriptTests/ScriptTypeTests/ItemDataTests.cpp index e260b8f..442c0bc 100644 --- a/tests/ScriptTests/ScriptTypeTests/ItemDataTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/ItemDataTests.cpp @@ -7,6 +7,9 @@ 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; } + bool testCategory(const Item@ i, ItemCategory category){ return i.Category == category; } + bool testBattleCategory(const Item@ i, BattleItemCategory category){ return i.BattleCategory == category; } + bool testPrice(const Item@ i, int price){ return i.Price == price; } } )"}}; @@ -55,4 +58,43 @@ TEST_CASE("Validate Item Name in Script") { REQUIRE((bool)data.Context->GetReturnWord()); } +TEST_CASE("Validate Item Category in Script") { + auto mainLib = TestLibrary::GetLibrary(); + auto data = GetScript(mainLib, "testCategory"); + + auto item = mainLib->GetItemLibrary()->GetItem("testItem"); + data.Context->SetArgObject(0, const_cast(item)); + data.Context->SetArgDWord(1, static_cast(item->GetCategory())); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnWord()); +} + +TEST_CASE("Validate Item Battle Category in Script") { + auto mainLib = TestLibrary::GetLibrary(); + auto data = GetScript(mainLib, "testBattleCategory"); + + auto item = mainLib->GetItemLibrary()->GetItem("testItem"); + data.Context->SetArgObject(0, const_cast(item)); + data.Context->SetArgDWord(1, static_cast(item->GetBattleCategory())); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnWord()); +} + +TEST_CASE("Validate Item Price in Script") { + auto mainLib = TestLibrary::GetLibrary(); + auto data = GetScript(mainLib, "testPrice"); + + auto item = mainLib->GetItemLibrary()->GetItem("testItem"); + data.Context->SetArgObject(0, const_cast(item)); + data.Context->SetArgDWord(1, static_cast(item->GetPrice())); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnWord()); +} + + + + #endif \ No newline at end of file