Reworks setting script owner to something a lot more clean
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2022-03-06 12:04:21 +01:00
parent 66c742e225
commit e18767995c
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 55 additions and 22 deletions

View File

@ -19,15 +19,26 @@ AngelScriptScript::AngelScriptScript(const ArbUt::OptionalBorrowedPtr<void>& 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<void>& 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<CreatureLib::Library::EffectParameter*>& 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<void>& 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, {

View File

@ -18,6 +18,7 @@ private:
AngelScriptResolver* _resolver = nullptr;
AngelScriptTypeInfo* _type = nullptr;
ContextPool* _ctxPool = nullptr;
CScriptHandle* _ownerHandle = nullptr;
asIScriptObject* _obj = nullptr;
NativeArray<ArbUt::List<CreatureLib::Library::EffectParameter*>>*
@ -28,7 +29,7 @@ public:
AngelScriptResolver* resolver, AngelScriptTypeInfo* type, asIScriptObject* obj,
ContextPool* ctxPool);
~AngelScriptScript() override { _obj->Release(); }
~AngelScriptScript() override;
BattleScript* Clone(const ArbUt::OptionalBorrowedPtr<void>& 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<CreatureLib::Library::EffectParameter*>& parameters) override;
void Stack() override;

View File

@ -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<EffectParameter@>@ parameters)");

View File

@ -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<EffectParameter@>@ parameters){};

View File

@ -13,6 +13,7 @@ static std::unordered_map<const char*, const char*> _scripts =
void Test(){
auto mon = cast<Pokemon@>(GetOwner());
if (mon is null){
print("mon was null");
throw("Owner was null!");
}
}