diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.cpp b/src/ScriptResolving/AngelScript/AngelScriptScript.cpp index 62131bd..d6d6702 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.cpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.cpp @@ -19,15 +19,26 @@ AngelScriptScript::AngelScriptScript(const ArbUt::OptionalBorrowedPtr& own AngelScriptResolver* resolver, AngelScriptTypeInfo* type, asIScriptObject* obj, ContextPool* ctxPool) : PkmnLib::Battling::PkmnScript(owner), _resolver(resolver), _type(type), _ctxPool(ctxPool), _obj(obj) { - if (_type->GetGetOwner().Exists && owner.HasValue()) { + if (_type->GetSetOwner().Exists && owner.HasValue()) { if (ownerType == nullptr) { THROW("Script was created with owner value, but with unknown owner type.") } - auto* handle = GetAngelscriptOwner(); - handle->Set(owner.GetValue(), ownerType); + _ownerHandle = new CScriptHandle(owner.GetValue(), ownerType); + SetAngelscriptOwner(_ownerHandle); } } +CreatureLib::Battling::BattleScript* AngelScriptScript::Clone(const ArbUt::OptionalBorrowedPtr& owner) { + auto* ctx = _ctxPool->RequestContext(); + auto* obj = _type->Instantiate(ctx); + if (_obj != nullptr) { + obj->CopyFrom(_obj); + } + _ctxPool->ReturnContextToPool(ctx); + + return new AngelScriptScript(owner, GetAngelscriptOwner()->GetType(), _resolver, _type, obj, _ctxPool); +} + CScriptHandle* AngelScriptScript::GetAngelscriptOwner() { CScriptHandle* handle = nullptr; AngelScriptUtils::AngelscriptFunctionCall( @@ -36,6 +47,20 @@ CScriptHandle* AngelScriptScript::GetAngelscriptOwner() { return handle; } +void AngelScriptScript::SetAngelscriptOwner(CScriptHandle* owner) { + AngelScriptUtils::AngelscriptFunctionCall( + _type->GetSetOwner().Function, _ctxPool, _obj, _resolver, GetName(), + [&](asIScriptContext* ctx) { ctx->SetArgObject(0, owner); }, [&](asIScriptContext*) {}); +} + +AngelScriptScript::~AngelScriptScript() { + if (_ownerHandle != nullptr){ + delete _ownerHandle; + } + _obj->Release(); +} + + void AngelScriptScript::OnInitialize(const CreatureLib::Battling::BattleLibrary* library, const ArbUt::List& parameters) { CALL_HOOK(OnInitialize, { @@ -356,23 +381,13 @@ void AngelScriptScript::PreventOpponentSwitch(const CreatureLib::Battling::Switc }) } void AngelScriptScript::OnEndTurn() { CALL_HOOK(OnEndTurn, {}) } -void AngelScriptScript::ModifyNumberOfHits(CreatureLib::Battling::AttackTurnChoice* choice, - u8* numberOfHits){CALL_HOOK(ModifyNumberOfHits, - { - ctx->SetArgObject(0, (void*)choice); - ctx->SetArgAddress(1, numberOfHits); - })} - -CreatureLib::Battling::BattleScript* AngelScriptScript::Clone(const ArbUt::OptionalBorrowedPtr& owner) { - auto* ctx = _ctxPool->RequestContext(); - auto* obj = _type->Instantiate(ctx); - if (_obj != nullptr) { - obj->CopyFrom(_obj); - } - _ctxPool->ReturnContextToPool(ctx); - - return new AngelScriptScript(owner, GetAngelscriptOwner()->GetType(), _resolver, _type, obj, _ctxPool); +void AngelScriptScript::ModifyNumberOfHits(CreatureLib::Battling::AttackTurnChoice* choice, u8* numberOfHits) { + CALL_HOOK(ModifyNumberOfHits, { + ctx->SetArgObject(0, (void*)choice); + ctx->SetArgAddress(1, numberOfHits); + }) } + void AngelScriptScript::OnDamage(CreatureLib::Battling::Creature* creature, CreatureLib::Battling::DamageSource source, [[maybe_unused]] u32 oldHealth, [[maybe_unused]] u32 newHealth) { CALL_HOOK(OnDamage, { diff --git a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp index 6cd0ac0..295981c 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptScript.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptScript.hpp @@ -18,6 +18,7 @@ private: AngelScriptResolver* _resolver = nullptr; AngelScriptTypeInfo* _type = nullptr; ContextPool* _ctxPool = nullptr; + CScriptHandle* _ownerHandle = nullptr; asIScriptObject* _obj = nullptr; NativeArray>* @@ -28,7 +29,7 @@ public: AngelScriptResolver* resolver, AngelScriptTypeInfo* type, asIScriptObject* obj, ContextPool* ctxPool); - ~AngelScriptScript() override { _obj->Release(); } + ~AngelScriptScript() override; BattleScript* Clone(const ArbUt::OptionalBorrowedPtr& owner) override; @@ -47,6 +48,7 @@ public: ContextPool* GetContextPool() { return _ctxPool; } CScriptHandle* GetAngelscriptOwner(); + void SetAngelscriptOwner(CScriptHandle*); void OnInitialize(const CreatureLib::Battling::BattleLibrary*, const ArbUt::List& parameters) override; void Stack() override; diff --git a/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp b/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp index 9ab0d9c..67d015e 100644 --- a/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp +++ b/src/ScriptResolving/AngelScript/AngelScriptTypeInfo.hpp @@ -76,7 +76,19 @@ private: FunctionInfo InitializeGetOwner() { auto t = _type; while (t != nullptr) { - auto val = t->GetMethodByDecl("ref@& GetOwner()", true); + auto val = t->GetMethodByDecl("const ref@& GetOwner()", true); + if (val != nullptr) { + return FunctionInfo{.Exists = true, .Function = val}; + } + t = t->GetBaseType(); + } + return FunctionInfo{.Exists = false, .Function = nullptr}; + } + + FunctionInfo InitializeSetOwner() { + auto t = _type; + while (t != nullptr) { + auto val = t->GetMethodByDecl("void SetOwner(ref@ owner)", true); if (val != nullptr) { return FunctionInfo{.Exists = true, .Function = val}; } @@ -86,9 +98,11 @@ private: } FunctionInfo __GetOwner = InitializeGetOwner(); + FunctionInfo __SetOwner = InitializeSetOwner(); public: const FunctionInfo& GetGetOwner() const { return __GetOwner; } + const FunctionInfo& GetSetOwner() const { return __SetOwner; } SCRIPT_HOOK_FUNCTION(OnInitialize, "void OnInitialize(const BattleLibrary@ library, const narray@ parameters)"); diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp index 44d9b93..2c6afba 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/BasicScriptClass.cpp @@ -8,7 +8,8 @@ void BasicScriptClass::Register(asIScriptEngine* engine) { shared abstract class PkmnScript { private ref@ __owner; - protected ref@& GetOwner() { return __owner; }; + protected const ref@& GetOwner() { return __owner; }; + protected void SetOwner(ref@ o) { @__owner = @o; }; // CreatureLib methods void OnInitialize(const BattleLibrary@ library, const narray@ parameters){}; diff --git a/tests/ScriptTests/ScriptOwnerTest.cpp b/tests/ScriptTests/ScriptOwnerTest.cpp index cfc0395..f4a1596 100644 --- a/tests/ScriptTests/ScriptOwnerTest.cpp +++ b/tests/ScriptTests/ScriptOwnerTest.cpp @@ -13,6 +13,7 @@ static std::unordered_map _scripts = void Test(){ auto mon = cast(GetOwner()); if (mon is null){ + print("mon was null"); throw("Owner was null!"); } }