Adds a GetOwner pattern to ItemUseScript
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-03-27 13:07:58 +02:00
parent 09638c8d14
commit c0e7f905a9
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 38 additions and 4 deletions

View File

@ -1,7 +1,16 @@
#include "AngelScriptItemUseScript.hpp"
#include "AngelScriptFunctionCall.hpp"
#include "AngelScriptResolver.hpp"
#include "AngelscriptUserdata.hpp"
AngelScriptItemUseScript::AngelScriptItemUseScript(asIScriptObject* scriptObject, AngelScriptResolver* resolver,
const CreatureLib::Library::Item* item)
: _scriptObject(scriptObject), _resolver(resolver) {
if (__SetOwner.Exists) {
AngelScriptUtils::AngelscriptFunctionCall(
__SetOwner.Function, _resolver->GetContextPool(), _scriptObject, _resolver, ""_cnc,
[&](asIScriptContext* ctx) { ctx->SetArgObject(0, (void*)item); }, [&](asIScriptContext*) {});
}
}
NativeArray<ArbUt::List<CreatureLib::Library::EffectParameter*>>*
AngelScriptItemUseScript::GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls) {

View File

@ -2,6 +2,7 @@
#define PKMNLIB_ANGELSCRIPTITEMUSESCRIPT_HPP
#include <CreatureLib/Battling/ScriptHandling/ItemUseScript.hpp>
#include <CreatureLib/Library/Items/Item.hpp>
#include <scriptarray/scriptarray.h>
#include "../../Battling/PkmnItemUseScript.hpp"
#include "TypeRegistry/NativeArray.hpp"
@ -10,8 +11,8 @@ class AngelScriptResolver;
class AngelScriptItemUseScript final : public PkmnLib::Battling::PkmnItemUseScript {
public:
AngelScriptItemUseScript(asIScriptObject* scriptObject, AngelScriptResolver* resolver)
: _scriptObject(scriptObject), _resolver(resolver) {}
AngelScriptItemUseScript(asIScriptObject* scriptObject, AngelScriptResolver* resolver,
const CreatureLib::Library::Item* item);
~AngelScriptItemUseScript() {
if (_scriptObject != nullptr) {
@ -50,6 +51,25 @@ private:
NativeArray<ArbUt::List<CreatureLib::Library::EffectParameter*>>*
GetEffectParameters(const ArbUt::List<CreatureLib::Library::EffectParameter*>& ls);
FunctionInfo InitializeGetOwner() {
auto val = _scriptObject->GetObjectType()->GetMethodByDecl("const Item@ GetOwner()", true);
if (val != nullptr) {
return FunctionInfo{.Exists = true, .Function = val};
}
return FunctionInfo{.Exists = false, .Function = nullptr};
}
FunctionInfo InitializeSetOwner() {
auto val = _scriptObject->GetObjectType()->GetMethodByDecl("void SetOwner(ref@ owner)", true);
if (val != nullptr) {
return FunctionInfo{.Exists = true, .Function = val};
}
return FunctionInfo{.Exists = false, .Function = nullptr};
}
FunctionInfo __GetOwner = InitializeGetOwner();
FunctionInfo __SetOwner = InitializeSetOwner();
#define ITEM_USE_SCRIPT_HOOK_FUNCTION(name, decl) FunctionInfo __##name = Initialize(decl);
ITEM_USE_SCRIPT_HOOK_FUNCTION(

View File

@ -216,7 +216,7 @@ PkmnLib::Battling::PkmnItemUseScript* AngelScriptResolver::LoadItemScript(const
}
asIScriptObject* obj = *(asIScriptObject**)ctx->GetAddressOfReturnValue();
obj->AddRef();
auto scriptObject = new AngelScriptItemUseScript(obj, this);
auto scriptObject = new AngelScriptItemUseScript(obj, this, item);
scriptObject->OnInitialize(item->GetEffect().GetValue()->GetParameters());
_itemUseScripts.Insert(item, scriptObject);
_contextPool->ReturnContextToPool(ctx);

View File

@ -80,6 +80,11 @@ shared abstract class PkmnScript {
Ensure(r >= 0);
r = engine->GetModuleByIndex(0)->AddScriptSection("ItemUseScript", R"(
shared abstract class ItemUseScript {
private Item@ __owner;
protected const Item@ GetOwner() { return __owner; };
protected void SetOwner(Item@ o) { @__owner = @o; };
void OnInitialize(const narray<EffectParameter@>@ parameters){};
bool IsItemUsable() { return false; };
bool IsPokemonUseItem() { return false; };