Make all individual scripts smart pointers.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2020-06-02 13:43:44 +02:00
parent dba1275813
commit e4b9550efa
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
11 changed files with 48 additions and 58 deletions

View File

@ -5,9 +5,9 @@ using namespace CreatureLib::Battling;
export uint8_t CreatureLib_ExecutingAttack_Construct(ExecutingAttack*& out, Creature* const* targets, export uint8_t CreatureLib_ExecutingAttack_Construct(ExecutingAttack*& out, Creature* const* targets,
size_t targetCount, uint8_t numberHits, Creature* user, size_t targetCount, uint8_t numberHits, Creature* user,
LearnedAttack* attack, Script* script) { LearnedAttack* attack, Script* script) {
Try(auto ls = ArbUt::List<ArbUt::BorrowedPtr<Creature>>(targetCount); for (size_t i = 0; i < targetCount; i++) { Try(auto ls = ArbUt::List<ArbUt::BorrowedPtr<Creature>>(targetCount);
ls.Append(targets[i]); for (size_t i = 0; i < targetCount; i++) { ls.Append(targets[i]); } auto s = std::unique_ptr<Script>(script);
} out = new ExecutingAttack(ls, numberHits, user, attack, script);) out = new ExecutingAttack(ls, numberHits, user, attack, s);)
} }
export void CreatureLib_ExecutingAttack_Destruct(ExecutingAttack* p) { delete p; } export void CreatureLib_ExecutingAttack_Destruct(ExecutingAttack* p) { delete p; }

View File

@ -30,7 +30,7 @@ SIMPLE_GET_FUNC(AttackTurnChoice, GetKind, TurnChoiceKind)
export uint8_t CreatureLib_BaseTurnChoice_GetPriority(int8_t& out, AttackTurnChoice* p) { Try(out = p->GetPriority()); } export uint8_t CreatureLib_BaseTurnChoice_GetPriority(int8_t& out, AttackTurnChoice* p) { Try(out = p->GetPriority()); }
SIMPLE_GET_FUNC(AttackTurnChoice, GetAttackScript, Script*) SIMPLE_GET_FUNC_SMART_PTR(AttackTurnChoice, GetAttackScript, Script*)
export uint8_t CreatureLib_BaseTurnChoice_GetTargetSideIndex(const AttackTurnChoice* p) { export uint8_t CreatureLib_BaseTurnChoice_GetTargetSideIndex(const AttackTurnChoice* p) {
return p->GetTarget().GetSideIndex(); return p->GetTarget().GetSideIndex();
} }

View File

@ -62,7 +62,6 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> choic
ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets = {target}; ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets = {target};
auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
choice->MarkScriptAsTaken();
bool prevented = false; bool prevented = false;
HOOK_LOCAL(PreventAttack, attack, &attack, &prevented); HOOK_LOCAL(PreventAttack, attack, &attack, &prevented);
if (prevented) { if (prevented) {

View File

@ -21,7 +21,7 @@ Battling::Creature::Creature(const BattleLibrary* library,
AssertNotNull(species) AssertNotNull(species)
AssertNotNull(variant) AssertNotNull(variant)
_activeTalent = _library->LoadScript(ScriptCategory::Talent, GetActiveTalent()); _activeTalent = std::unique_ptr<Script>(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent()));
if (_nickname.empty()) { if (_nickname.empty()) {
_nickname = species->GetName().std_str(); _nickname = species->GetName().std_str();
} }
@ -142,9 +142,8 @@ void Battling::Creature::Heal(uint32_t amount, bool canRevive) {
void Battling::Creature::OverrideActiveTalent(const ConstString& talent) { void Battling::Creature::OverrideActiveTalent(const ConstString& talent) {
_hasOverridenTalent = true; _hasOverridenTalent = true;
_activeTalent->OnRemove(); _activeTalent->OnRemove();
delete _activeTalent;
_overridenTalentName = talent; _overridenTalentName = talent;
_activeTalent = this->_library->LoadScript(ScriptCategory::Talent, talent); _activeTalent.reset(this->_library->LoadScript(ScriptCategory::Talent, talent));
} }
const ArbUt::List<uint8_t>& Battling::Creature::GetTypes() const noexcept { const ArbUt::List<uint8_t>& Battling::Creature::GetTypes() const noexcept {

View File

@ -47,7 +47,7 @@ namespace CreatureLib::Battling {
std::string _nickname = ""; std::string _nickname = "";
CreatureLib::Library::TalentIndex _talentIndex; CreatureLib::Library::TalentIndex _talentIndex;
Script* _activeTalent = nullptr; std::unique_ptr<Script> _activeTalent = nullptr;
bool _hasOverridenTalent; bool _hasOverridenTalent;
ArbUt::CaseInsensitiveConstString _overridenTalentName = ""_cnc; ArbUt::CaseInsensitiveConstString _overridenTalentName = ""_cnc;
@ -56,7 +56,7 @@ namespace CreatureLib::Battling {
ArbUt::UniquePtrList<LearnedAttack> _attacks; ArbUt::UniquePtrList<LearnedAttack> _attacks;
bool _allowedExperienceGain; bool _allowedExperienceGain;
Script* _status = nullptr; std::unique_ptr<Script> _status = nullptr;
ScriptSet _volatile = {}; ScriptSet _volatile = {};
private: private:
@ -70,10 +70,7 @@ namespace CreatureLib::Battling {
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks, const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
bool allowedExperienceGain = true); bool allowedExperienceGain = true);
virtual ~Creature() { virtual ~Creature() = default;
delete _activeTalent;
delete _status;
};
virtual void Initialize() { virtual void Initialize() {
RecalculateFlatStats(); RecalculateFlatStats();

View File

@ -41,24 +41,25 @@ namespace CreatureLib::Battling {
HitData* _hits; HitData* _hits;
Creature* _user; Creature* _user;
ArbUt::BorrowedPtr<LearnedAttack> _attack; ArbUt::BorrowedPtr<LearnedAttack> _attack;
Script* _script; std::unique_ptr<Script> _script = nullptr;
public: public:
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user, ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user,
const ArbUt::BorrowedPtr<LearnedAttack>& attack, Script* script) const ArbUt::BorrowedPtr<LearnedAttack>& attack, const std::unique_ptr<Script>& script)
: _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]), : _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]),
_user(user), _attack(attack), _script(script) { _user(user), _attack(attack) {
AssertNotNull(user) AssertNotNull(user)
AssertNotNull(attack) AssertNotNull(attack)
for (auto target : targets) { for (auto target : targets) {
_targets.Append(target); _targets.Append(target);
} }
// Take ownership of the script of the attack choice, and give attack choice our initial nullptr.
_script.swap(const_cast<std::unique_ptr<Script>&>(script));
} }
ExecutingAttack(const ExecutingAttack&) = delete; ExecutingAttack(const ExecutingAttack&) = delete;
ExecutingAttack& operator=(const ExecutingAttack&) = delete; ExecutingAttack& operator=(const ExecutingAttack&) = delete;
virtual ~ExecutingAttack() noexcept { virtual ~ExecutingAttack() noexcept {
delete _script;
delete[] _hits; delete[] _hits;
}; };

View File

@ -42,7 +42,7 @@ namespace CreatureLib::Battling {
auto s = current.GetScript(); auto s = current.GetScript();
if (s == nullptr) if (s == nullptr)
return nullptr; return nullptr;
return *s; return (*s).get();
} else { } else {
auto& set = *current.GetScriptSet()->GetIterator(); auto& set = *current.GetScriptSet()->GetIterator();
auto count = set.Count(); auto count = set.Count();

View File

@ -1,7 +1,7 @@
#ifndef CREATURELIB_SCRIPTWRAPPER_HPP #ifndef CREATURELIB_SCRIPTWRAPPER_HPP
#define CREATURELIB_SCRIPTWRAPPER_HPP #define CREATURELIB_SCRIPTWRAPPER_HPP
#include <variant> #include <memory>
#include "Script.hpp" #include "Script.hpp"
#include "ScriptSet.hpp" #include "ScriptSet.hpp"
@ -10,20 +10,20 @@ namespace CreatureLib::Battling {
bool _isSet; bool _isSet;
union { union {
Script* const* _script; std::unique_ptr<Script> const* _script;
const ScriptSet* _scriptSet; const ScriptSet* _scriptSet;
}; };
ScriptWrapper(Script** s, bool isSet) : _isSet(isSet), _script(s){}; ScriptWrapper(std::unique_ptr<Script>* s, bool isSet) : _isSet(isSet), _script(s){};
ScriptWrapper(ScriptSet* s, bool isSet) : _isSet(isSet), _scriptSet(s){}; ScriptWrapper(ScriptSet* s, bool isSet) : _isSet(isSet), _scriptSet(s){};
public: public:
static inline ScriptWrapper FromScript(Script** s) { return ScriptWrapper(s, false); } static inline ScriptWrapper FromScript(std::unique_ptr<Script>* s) { return ScriptWrapper(s, false); }
static inline ScriptWrapper FromSet(ScriptSet* s) { return ScriptWrapper(s, true); } static inline ScriptWrapper FromSet(ScriptSet* s) { return ScriptWrapper(s, true); }
bool IsSet() const { return _isSet; } bool IsSet() const { return _isSet; }
inline Script* const* GetScript() const { return _script; } inline const std::unique_ptr<Script>* GetScript() const { return _script; }
inline const ScriptSet* GetScriptSet() const { return _scriptSet; } inline const ScriptSet* GetScriptSet() const { return _scriptSet; }
}; };
} }

View File

@ -11,7 +11,7 @@ namespace CreatureLib::Battling {
class AttackTurnChoice : public BaseTurnChoice { class AttackTurnChoice : public BaseTurnChoice {
ArbUt::BorrowedPtr<LearnedAttack> _attack; ArbUt::BorrowedPtr<LearnedAttack> _attack;
CreatureIndex _target; CreatureIndex _target;
Script* _attackScript = nullptr; std::unique_ptr<Script> _attackScript = nullptr;
bool _scriptIsTaken = false; bool _scriptIsTaken = false;
void ResolveScript() { void ResolveScript() {
@ -25,7 +25,8 @@ namespace CreatureLib::Battling {
if (_attack->GetAttack()->HasSecondaryEffect()) { if (_attack->GetAttack()->HasSecondaryEffect()) {
auto library = battle->GetLibrary(); auto library = battle->GetLibrary();
auto& effect = _attack->GetAttack()->GetSecondaryEffect(); auto& effect = _attack->GetAttack()->GetSecondaryEffect();
_attackScript = library->LoadScript(ScriptCategory::Attack, effect->GetEffectName()); _attackScript =
std::unique_ptr<Script>(library->LoadScript(ScriptCategory::Attack, effect->GetEffectName()));
if (_attackScript != nullptr) { if (_attackScript != nullptr) {
_attackScript->OnInitialize(effect->GetParameters()); _attackScript->OnInitialize(effect->GetParameters());
} }
@ -45,7 +46,6 @@ namespace CreatureLib::Battling {
~AttackTurnChoice() { ~AttackTurnChoice() {
if (!_scriptIsTaken) { if (!_scriptIsTaken) {
delete _attackScript;
} }
} }
@ -58,11 +58,9 @@ namespace CreatureLib::Battling {
return _attack->GetAttack()->GetPriority(); return _attack->GetAttack()->GetPriority();
} }
void MarkScriptAsTaken() { _scriptIsTaken = true; }
const CreatureIndex& GetTarget() const noexcept { return _target; } const CreatureIndex& GetTarget() const noexcept { return _target; }
Script* GetAttackScript() const noexcept { return _attackScript; } const std::unique_ptr<Script>& GetAttackScript() const noexcept { return _attackScript; }
size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); } size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); }
protected: protected:

View File

@ -19,9 +19,10 @@ public:
}; };
TEST_CASE("Script Aggregator properly iterates containing script.", "[Battling, Scripting]") { TEST_CASE("Script Aggregator properly iterates containing script.", "[Battling, Scripting]") {
Script* script = new TestScript("test"); auto script = std::make_unique<TestScript>("test");
auto ran = 0; auto ran = 0;
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(&script)}; auto vec =
ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script))};
auto aggr = ScriptAggregator(vec); auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) { while (aggr.HasNext()) {
auto next = aggr.GetNext(); auto next = aggr.GetNext();
@ -29,16 +30,17 @@ TEST_CASE("Script Aggregator properly iterates containing script.", "[Battling,
dynamic_cast<TestScript*>(next)->TestMethod(ran); dynamic_cast<TestScript*>(next)->TestMethod(ran);
} }
CHECK(ran == 1); CHECK(ran == 1);
delete script;
} }
TEST_CASE("Script Aggregator properly iterates multiple scripts.", "[Battling, Scripting]") { TEST_CASE("Script Aggregator properly iterates multiple scripts.", "[Battling, Scripting]") {
Script* script = new TestScript("test"); auto script = std::make_unique<TestScript>("test");
Script* script2 = new TestScript("test2"); auto script2 = std::make_unique<TestScript>("test2");
Script* script3 = new TestScript("test3"); auto script3 = std::make_unique<TestScript>("test3");
auto ran = 0; auto ran = 0;
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(&script), ScriptWrapper::FromScript(&script2), auto vec =
ScriptWrapper::FromScript(&script3)}; ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script)),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script2)),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script3))};
auto aggr = ScriptAggregator(vec); auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) { while (aggr.HasNext()) {
auto next = aggr.GetNext(); auto next = aggr.GetNext();
@ -46,9 +48,6 @@ TEST_CASE("Script Aggregator properly iterates multiple scripts.", "[Battling, S
dynamic_cast<TestScript*>(next)->TestMethod(ran); dynamic_cast<TestScript*>(next)->TestMethod(ran);
} }
CHECK(ran == 3); CHECK(ran == 3);
delete script;
delete script2;
delete script3;
} }
TEST_CASE("Script Aggregator properly iterates Script Set.", "[Battling, Scripting]") { TEST_CASE("Script Aggregator properly iterates Script Set.", "[Battling, Scripting]") {
@ -71,14 +70,15 @@ TEST_CASE("Script Aggregator properly iterates Script Set.", "[Battling, Scripti
} }
TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.", "[Battling, Scripting]") { TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.", "[Battling, Scripting]") {
Script* script = new TestScript("test"); auto script = std::make_unique<TestScript>("test");
Script* script2 = new TestScript("test2"); Script* script2 = new TestScript("test2");
Script* script3 = new TestScript("test3"); Script* script3 = new TestScript("test3");
auto ran = 0; auto ran = 0;
auto set = ScriptSet(); auto set = ScriptSet();
set.Add(script2); set.Add(script2);
set.Add(script3); set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set), ScriptWrapper::FromScript(&script)}; auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromSet(&set), ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script))};
auto aggr = ScriptAggregator(vec); auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) { while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull(); auto next = aggr.GetNextNotNull();
@ -86,18 +86,18 @@ TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.",
dynamic_cast<TestScript*>(next)->TestMethod(ran); dynamic_cast<TestScript*>(next)->TestMethod(ran);
} }
CHECK(ran == 3); CHECK(ran == 3);
delete script;
} }
TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.", "[Battling, Scripting]") { TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.", "[Battling, Scripting]") {
Script* script = new TestScript("test"); auto script = std::make_unique<TestScript>("test");
Script* script2 = new TestScript("test2"); Script* script2 = new TestScript("test2");
Script* script3 = new TestScript("test3"); Script* script3 = new TestScript("test3");
auto ran = 0; auto ran = 0;
auto set = ScriptSet(); auto set = ScriptSet();
set.Add(script2); set.Add(script2);
set.Add(script3); set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(&script), ScriptWrapper::FromSet(&set)}; auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script)), ScriptWrapper::FromSet(&set)};
auto aggr = ScriptAggregator(vec); auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) { while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull(); auto next = aggr.GetNextNotNull();
@ -105,20 +105,20 @@ TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.",
dynamic_cast<TestScript*>(next)->TestMethod(ran); dynamic_cast<TestScript*>(next)->TestMethod(ran);
} }
CHECK(ran == 3); CHECK(ran == 3);
delete script;
} }
TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.", "[Battling, Scripting]") { TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.", "[Battling, Scripting]") {
Script* script = new TestScript("test"); auto script = std::make_unique<TestScript>("test");
Script* script2 = new TestScript("test2"); Script* script2 = new TestScript("test2");
Script* script3 = new TestScript("test3"); Script* script3 = new TestScript("test3");
Script* script4 = new TestScript("test4"); auto script4 = std::make_unique<TestScript>("test4");
auto ran = 0; auto ran = 0;
auto set = ScriptSet(); auto set = ScriptSet();
set.Add(script2); set.Add(script2);
set.Add(script3); set.Add(script3);
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(&script), ScriptWrapper::FromSet(&set), auto vec = ArbUt::List<ScriptWrapper>{
ScriptWrapper::FromScript(&script4)}; ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script)), ScriptWrapper::FromSet(&set),
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<Script>*>(&script4))};
auto aggr = ScriptAggregator(vec); auto aggr = ScriptAggregator(vec);
while (aggr.HasNext()) { while (aggr.HasNext()) {
auto next = aggr.GetNextNotNull(); auto next = aggr.GetNextNotNull();
@ -126,8 +126,6 @@ TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Sc
dynamic_cast<TestScript*>(next)->TestMethod(ran); dynamic_cast<TestScript*>(next)->TestMethod(ran);
} }
CHECK(ran == 4); CHECK(ran == 4);
delete script;
delete script4;
} }
TEST_CASE("Script Aggregator properly iterates when empty.", "[Battling, Scripting]") { TEST_CASE("Script Aggregator properly iterates when empty.", "[Battling, Scripting]") {

View File

@ -21,7 +21,7 @@ public:
class ScriptSourceWithScriptPtr : public ScriptSource { class ScriptSourceWithScriptPtr : public ScriptSource {
public: public:
Script* ScriptPtr = nullptr; std::unique_ptr<Script> ScriptPtr = nullptr;
protected: protected:
size_t ScriptCount() const override { return 1; } size_t ScriptCount() const override { return 1; }
@ -50,11 +50,10 @@ TEST_CASE("Script source with unset script ptr.", "[Battling, Scripting]") {
TEST_CASE("Script source with script ptr being set.", "[Battling, Scripting]") { TEST_CASE("Script source with script ptr being set.", "[Battling, Scripting]") {
auto source = ScriptSourceWithScriptPtr(); auto source = ScriptSourceWithScriptPtr();
source.ScriptPtr = new TestScript("foobar"); source.ScriptPtr = std::make_unique<TestScript>("foobar");
auto scripts = source.GetScriptIterator(); auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext(); auto first = scripts.GetNext();
CHECK(first != nullptr); CHECK(first != nullptr);
delete source.ScriptPtr;
} }
TEST_CASE("Script source with script ptr being set after first iteration.", "[Battling, Scripting]") { TEST_CASE("Script source with script ptr being set after first iteration.", "[Battling, Scripting]") {
@ -62,11 +61,10 @@ TEST_CASE("Script source with script ptr being set after first iteration.", "[Ba
auto scripts = source.GetScriptIterator(); auto scripts = source.GetScriptIterator();
auto first = scripts.GetNext(); auto first = scripts.GetNext();
CHECK(first == nullptr); CHECK(first == nullptr);
source.ScriptPtr = new TestScript("foobar"); source.ScriptPtr = std::make_unique<TestScript>("foobar");
scripts = source.GetScriptIterator(); scripts = source.GetScriptIterator();
first = scripts.GetNext(); first = scripts.GetNext();
CHECK(first != nullptr); CHECK(first != nullptr);
delete source.ScriptPtr;
} }
TEST_CASE("Script source with empty script set.", "[Battling, Scripting]") { TEST_CASE("Script source with empty script set.", "[Battling, Scripting]") {