From aa8915b3149da455955043c6d378e80517e3835e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 7 Apr 2020 12:04:23 +0200 Subject: [PATCH] Angelscript support for ConstString. --- .../Battling/RegisterBattleClass.cpp | 4 +- .../Battling/RegisterPokemonClass.cpp | 8 ++-- .../AngelScript/TypeRegistry/ConstString.cpp | 47 ++++++++++++++++++- .../Library/RegisterGrowthRateTypes.cpp | 4 +- .../Library/RegisterItemTypes.cpp | 6 +-- .../Library/RegisterMoveTypes.cpp | 6 +-- .../Library/RegisterSpeciesTypes.cpp | 6 +-- .../Library/RegisterTypeLibrary.cpp | 2 +- .../ScriptTypeTests/Battle/PokemonTests.cpp | 2 +- .../ScriptTypeTests/Library/FormesTests.cpp | 2 +- .../ScriptTypeTests/Library/ItemDataTests.cpp | 2 +- .../ScriptTypeTests/Library/MoveTests.cpp | 2 +- 12 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp index 6a22e9e..d147743 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp @@ -61,11 +61,11 @@ void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) { asMETHOD(CreatureLib::Battling::Battle, GetCurrentTurnQueue), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod( - "Battle", "void AddVolatile(const string &in name) const", + "Battle", "void AddVolatile(const constString &in name) const", asMETHODPR(CreatureLib::Battling::Battle, AddVolatileScript, (const ConstString&), void), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod( - "Battle", "void RemoveVolatile(const string &in name) const", + "Battle", "void RemoveVolatile(const constString &in name) const", asMETHODPR(CreatureLib::Battling::Battle, RemoveVolatileScript, (const ConstString&), void), asCALL_THISCALL); assert(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp index 6efd1f5..d946cf5 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp @@ -114,7 +114,7 @@ void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) { r = engine->RegisterObjectMethod("Pokemon", "const Item@ get_HeldItem() const property", asMETHOD(PkmnLib::Battling::Pokemon, GetHeldItem), asCALL_THISCALL); assert(r >= 0); - r = engine->RegisterObjectMethod("Pokemon", "bool HasHeldItem(const string &in name) const", + r = engine->RegisterObjectMethod("Pokemon", "bool HasHeldItem(const constString &in name) const", asFUNCTION(HasHeldItem), asCALL_CDECL_OBJLAST); assert(r >= 0); r = engine->RegisterObjectMethod( @@ -133,7 +133,7 @@ void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) { r = engine->RegisterObjectMethod("Pokemon", "const string& get_Nickname() const property", asMETHOD(PkmnLib::Battling::Pokemon, GetNickname), asCALL_THISCALL); assert(r >= 0); - r = engine->RegisterObjectMethod("Pokemon", "const string& get_ActiveAbility() const property", + r = engine->RegisterObjectMethod("Pokemon", "const constString& get_ActiveAbility() const property", asMETHOD(PkmnLib::Battling::Pokemon, GetActiveTalent), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("Pokemon", "bool get_IsFainted() const property", @@ -182,11 +182,11 @@ void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) { asMETHOD(PkmnLib::Battling::Pokemon, GetStatBoost), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod( - "Pokemon", "void AddVolatile(const string &in name) const", + "Pokemon", "void AddVolatile(const constString &in name) const", asMETHODPR(PkmnLib::Battling::Pokemon, AddVolatileScript, (const ConstString&), void), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod( - "Pokemon", "void RemoveVolatile(const string &in name) const", + "Pokemon", "void RemoveVolatile(const constString &in name) const", asMETHODPR(PkmnLib::Battling::Pokemon, RemoveVolatileScript, (const ConstString&), void), asCALL_THISCALL); assert(r >= 0); } \ No newline at end of file diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/ConstString.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/ConstString.cpp index eb01b95..adaf9e5 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/ConstString.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/ConstString.cpp @@ -1,14 +1,59 @@ #include "ConstString.hpp" using ConstString = Arbutils::CaseInsensitiveConstString; +static void ConstructConstString(ConstString* self) { self = new ConstString(); } +static void CopyConstructConstString(const ConstString& other, ConstString* self) { self = new ConstString(other); } +static void DestructConstString(ConstString* self) { self->~ConstString(); } static bool ConstStringEquality(const ConstString& a, const ConstString& b) { return a == b; } +static bool ConstStringStdStringEquality(const ConstString& a, const std::string& b) { return a == b; } +static ConstString ImplStdStringConstStringConv(const std::string& s) { return ConstString(s); } +static std::string ImplConstStringStdStringConv(const ConstString& s) { return s.std_str(); } +static uint32_t ImplConstStringHashConv(const ConstString& s) { return s.GetHash(); } void ConstStringRegister::Register(asIScriptEngine* engine) { auto r = engine->RegisterObjectType("constString", sizeof(Arbutils::CaseInsensitiveConstString), - asOBJ_VALUE | asOBJ_POD); + asOBJ_VALUE | asOBJ_POD | asGetTypeTraits()); Assert(r >= 0); + + r = engine->RegisterObjectBehaviour("constString", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructConstString), + asCALL_CDECL_OBJLAST); + Assert(r >= 0); + + r = engine->RegisterObjectBehaviour("constString", asBEHAVE_CONSTRUCT, "void f(const constString &in)", + asFUNCTION(CopyConstructConstString), asCALL_CDECL_OBJLAST); + Assert(r >= 0); + + r = engine->RegisterObjectBehaviour("constString", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructConstString), + asCALL_CDECL_OBJLAST); + Assert(r >= 0); + + r = engine->RegisterObjectMethod("string", "constString &opAssign(const constString &in)", + asMETHODPR(ConstString, operator=,(const ConstString&), ConstString&), + asCALL_THISCALL); + Assert(r >= 0); + r = engine->RegisterObjectMethod("constString", "bool opEquals(const constString &in) const", asFUNCTIONPR(ConstStringEquality, (const ConstString&, const ConstString&), bool), asCALL_CDECL_OBJFIRST); Assert(r >= 0); + r = engine->RegisterObjectMethod( + "constString", "bool opEquals(const string &in) const", + asFUNCTIONPR(ConstStringStdStringEquality, (const ConstString&, const std::string&), bool), + asCALL_CDECL_OBJFIRST); + Assert(r >= 0); + + r = engine->RegisterObjectMethod("string", "constString opImplConv()", + asFUNCTIONPR(ImplStdStringConstStringConv, (const std::string&), ConstString), + asCALL_CDECL_OBJFIRST); + Assert(r >= 0); + + r = engine->RegisterObjectMethod("constString", "string opImplConv()", + asFUNCTIONPR(ImplConstStringStdStringConv, (const ConstString&), std::string), + asCALL_CDECL_OBJFIRST); + Assert(r >= 0); + + r = engine->RegisterObjectMethod("constString", "uint opImplConv()", + asFUNCTIONPR(ImplConstStringHashConv, (const ConstString&), uint32_t), + asCALL_CDECL_OBJFIRST); + Assert(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterGrowthRateTypes.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterGrowthRateTypes.cpp index 068d600..f5c87bd 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterGrowthRateTypes.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterGrowthRateTypes.cpp @@ -31,11 +31,11 @@ void RegisterGrowthRateTypes::RegisterGrowthRateLibrary(asIScriptEngine* engine) [[maybe_unused]] int r = engine->RegisterObjectType("GrowthRateLibrary", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); r = engine->RegisterObjectMethod("GrowthRateLibrary", - "uint8 CalculateLevel(const string &in growthRate, uint experience) const", + "uint8 CalculateLevel(const constString &in growthRate, uint experience) const", asFUNCTION(CalculateLevel), asCALL_CDECL_OBJLAST); assert(r >= 0); r = engine->RegisterObjectMethod("GrowthRateLibrary", - "uint CalculateExperience(const string &in growthRate, uint8 experience) const", + "uint CalculateExperience(const constString &in growthRate, uint8 experience) const", asFUNCTION(CalculateExperience), asCALL_CDECL_OBJLAST); assert(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp index 46148b2..c40503a 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterItemTypes.cpp @@ -61,7 +61,7 @@ static bool HasFlag(const PkmnLib::Library::Item* obj, const Arbutils::CaseInsen void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("Item", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("Item", "const string& get_Name() const property", + r = engine->RegisterObjectMethod("Item", "const constString& get_Name() const property", asMETHOD(PkmnLib::Library::Item, GetName), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("Item", "ItemCategory get_Category() const property", @@ -73,7 +73,7 @@ void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) { r = engine->RegisterObjectMethod("Item", "int get_Price() const property", asMETHOD(PkmnLib::Library::Item, GetPrice), asCALL_THISCALL); assert(r >= 0); - r = engine->RegisterObjectMethod("Item", "bool HasFlag(const string &in flag) const", + r = engine->RegisterObjectMethod("Item", "bool HasFlag(const constString &in flag) const", asFUNCTION(HasFlag), asCALL_CDECL_OBJLAST); assert(r >= 0); @@ -82,7 +82,7 @@ void RegisterItemTypes::RegisterItemType(asIScriptEngine* engine) { void RegisterItemTypes::RegisterItemLibrary(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("ItemLibrary", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("ItemLibrary", "const Item@ Get(const string &in name) const", + r = engine->RegisterObjectMethod("ItemLibrary", "const Item@ Get(const constString &in name) const", asMETHOD(PkmnLib::Library::ItemLibrary, Get), asCALL_THISCALL); assert(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp index 9322d88..c01d466 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterMoveTypes.cpp @@ -53,7 +53,7 @@ static bool HasFlag(const PkmnLib::Library::MoveData* obj, const Arbutils::CaseI void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("MoveData", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("MoveData", "const string& get_Name() const property", + r = engine->RegisterObjectMethod("MoveData", "const constString& get_Name() const property", asMETHOD(PkmnLib::Library::MoveData, GetName), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("MoveData", "uint8 get_Type() const property", @@ -77,14 +77,14 @@ void RegisterMoveTypes::RegisterMoveType(asIScriptEngine* engine) { r = engine->RegisterObjectMethod("MoveData", "int8 get_Priority() const property", asMETHOD(PkmnLib::Library::MoveData, GetPriority), asCALL_THISCALL); assert(r >= 0); - r = engine->RegisterObjectMethod("MoveData", "bool HasFlag(const string &in flag) const", + r = engine->RegisterObjectMethod("MoveData", "bool HasFlag(const constString &in flag) const", asFUNCTION(HasFlag), asCALL_CDECL_OBJLAST); assert(r >= 0); } void RegisterMoveTypes::RegisterMoveLibrary(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("MoveLibrary", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("MoveLibrary", "const MoveData@ Get(const string &in name) const", + r = engine->RegisterObjectMethod("MoveLibrary", "const MoveData@ Get(const constString &in name) const", asMETHOD(PkmnLib::Library::MoveLibrary, Get), asCALL_THISCALL); assert(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterSpeciesTypes.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterSpeciesTypes.cpp index d8926bf..cb25663 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterSpeciesTypes.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterSpeciesTypes.cpp @@ -48,7 +48,7 @@ static const PkmnLib::Library::PokemonForme* GetFormeWrapper(const std::string& void RegisterSpeciesTypes::RegisterSpeciesType(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("Species", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("Species", "const string& get_Name() const property", + r = engine->RegisterObjectMethod("Species", "const constString& get_Name() const property", asMETHOD(PkmnLib::Library::PokemonSpecies, GetName), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("Species", "uint16 get_Id() const property", @@ -78,7 +78,7 @@ const ConstString& GetAbility(PkmnLib::Library::PokemonForme* p, bool hidden, ui void RegisterSpeciesTypes::RegisterFormeType(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("Forme", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("Forme", "const string& get_Name() const property", + r = engine->RegisterObjectMethod("Forme", "const constString& get_Name() const property", asMETHOD(PkmnLib::Library::PokemonForme, GetName), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("Forme", "float get_Weight() const property", @@ -108,7 +108,7 @@ void RegisterSpeciesTypes::RegisterFormeType(asIScriptEngine* engine) { void RegisterSpeciesTypes::RegisterSpeciesLibrary(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("SpeciesLibrary", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("SpeciesLibrary", "const Species@ Get(const string &in name) const", + r = engine->RegisterObjectMethod("SpeciesLibrary", "const Species@ Get(const constString &in name) const", asMETHOD(PkmnLib::Library::SpeciesLibrary, Get), asCALL_THISCALL); assert(r >= 0); } diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterTypeLibrary.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterTypeLibrary.cpp index f396de2..ba72dae 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterTypeLibrary.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Library/RegisterTypeLibrary.cpp @@ -13,7 +13,7 @@ static bool GetTypeId(const CreatureLib::Library::TypeLibrary* obj, const Arbuti void RegisterTypeLibrary::RegisterTypeLibraryType(asIScriptEngine* engine) { [[maybe_unused]] int r = engine->RegisterObjectType("TypeLibrary", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0); - r = engine->RegisterObjectMethod("TypeLibrary", "uint8 GetTypeId(const string &in name) const", + r = engine->RegisterObjectMethod("TypeLibrary", "uint8 GetTypeId(const constString &in name) const", asFUNCTION(GetTypeId), asCALL_CDECL_OBJLAST); assert(r >= 0); r = engine->RegisterObjectMethod("TypeLibrary", "float GetSingleEffectiveness(uint8 attacking, uint8 defensive) const", diff --git a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp index 7132df9..64ae51e 100644 --- a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp @@ -17,7 +17,7 @@ class testScript1 { bool testHeldItem(Pokemon@ p, Item@ item){ return p.HeldItem is item; } bool testCurrentHealth(Pokemon@ p, uint health){ return p.CurrentHealth == health; } bool testNickname(Pokemon@ p, const string& name){ return p.Nickname == name; } - bool testActiveAbility(Pokemon@ p, const string &in ability){ return p.ActiveAbility == ability; } + bool testActiveAbility(Pokemon@ p, const constString &in ability){ return p.ActiveAbility == ability; } bool testIsFainted(Pokemon@ p, bool b){ return p.IsFainted == b; } bool testType(Pokemon@ p, uint index, uint8 type){ return p.GetTypes()[index] == type; } bool testHasType(Pokemon@ p, uint8 type){ return p.HasType(type); } diff --git a/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp index 3ed101d..71eeae6 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/FormesTests.cpp @@ -166,7 +166,7 @@ TEST_CASE("Validate Forme GetAbility in Script") { auto ability = forme->GetAbility(CreatureLib::Library::TalentIndex(false, 0)); data.Context->SetArgAddress(1, &ability); REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); -// REQUIRE((bool)data.Context->GetReturnWord()); + REQUIRE((bool)data.Context->GetReturnWord()); } #endif \ No newline at end of file diff --git a/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp index b644817..d8cec8b 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/ItemDataTests.cpp @@ -7,7 +7,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( namespace Pokemon{ class testScript1 { - bool testName(const Item@ i, const string &in name){ return i.Name == name; } + bool testName(const Item@ i, const constString &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; } diff --git a/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp b/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp index 9f4d28b..e4eeb7c 100644 --- a/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Library/MoveTests.cpp @@ -7,7 +7,7 @@ static std::unordered_map _scripts = std::unordered_map{{"testScript1", R"( namespace Pokemon{ class testScript1 { - bool testName(const MoveData@ s, const string &in name){ return s.Name == name; } + bool testName(const MoveData@ s, const constString &in name){ return s.Name == name; } bool testType(const MoveData@ s, uint8 type){ return s.Type == type; } bool testCategory(const MoveData@ s, MoveCategory category){ return s.Category == category; } bool testBasePower(const MoveData@ s, uint8 basePower){ return s.BasePower == basePower; }