2019-11-08 17:45:43 +00:00
|
|
|
#ifndef CREATURELIB_SCRIPTSET_HPP
|
|
|
|
#define CREATURELIB_SCRIPTSET_HPP
|
|
|
|
|
2020-04-09 16:19:21 +00:00
|
|
|
#include <Arbutils/Collections/Dictionary.hpp>
|
2020-06-02 13:03:31 +00:00
|
|
|
#include <Arbutils/Memory/UniquePtrList.hpp>
|
2019-11-08 17:45:43 +00:00
|
|
|
#include <any>
|
|
|
|
#include "Script.hpp"
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
namespace CreatureLib::Battling {
|
|
|
|
class ScriptSet {
|
2020-06-02 13:03:31 +00:00
|
|
|
ArbUt::UniquePtrList<Script> _scripts;
|
2020-05-26 16:31:06 +00:00
|
|
|
ArbUt::Dictionary<uint32_t, size_t> _lookup;
|
2019-11-28 11:55:22 +00:00
|
|
|
|
2019-11-08 17:45:43 +00:00
|
|
|
public:
|
2020-06-02 13:03:31 +00:00
|
|
|
~ScriptSet() = default;
|
2020-02-05 13:52:50 +00:00
|
|
|
|
2020-04-25 09:33:25 +00:00
|
|
|
static constexpr size_t defaultCapacity = 8;
|
|
|
|
ScriptSet() : _scripts(defaultCapacity), _lookup(defaultCapacity){};
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void Add(Script* script) {
|
2020-03-25 18:07:36 +00:00
|
|
|
size_t v;
|
|
|
|
if (_lookup.TryGet(script->GetName(), v)) {
|
|
|
|
_scripts[v]->Stack();
|
2020-02-16 16:44:41 +00:00
|
|
|
delete script;
|
2019-11-12 17:38:09 +00:00
|
|
|
return;
|
2019-11-08 17:45:43 +00:00
|
|
|
}
|
2020-03-25 18:07:36 +00:00
|
|
|
|
2020-03-22 18:21:40 +00:00
|
|
|
_scripts.Append(script);
|
2020-03-25 18:07:36 +00:00
|
|
|
_lookup.Insert(script->GetName(), _scripts.Count() - 1);
|
2019-11-08 17:45:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 13:03:31 +00:00
|
|
|
ArbUt::BorrowedPtr<Script> Get(const ArbUt::CaseInsensitiveConstString& key) const {
|
|
|
|
return Get(key.GetHash());
|
|
|
|
}
|
2020-03-09 09:16:57 +00:00
|
|
|
|
2020-06-02 13:03:31 +00:00
|
|
|
ArbUt::BorrowedPtr<Script> Get(uint32_t keyHash) const noexcept {
|
2020-03-25 18:07:36 +00:00
|
|
|
size_t v;
|
|
|
|
if (_lookup.TryGet(keyHash, v)) {
|
|
|
|
return _scripts[v];
|
2020-02-16 16:44:41 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:31:06 +00:00
|
|
|
void Remove(const ArbUt::CaseInsensitiveConstString& key) { Remove(key.GetHash()); }
|
2020-03-09 09:16:57 +00:00
|
|
|
|
|
|
|
void Remove(uint32_t keyHash) {
|
2020-03-25 18:07:36 +00:00
|
|
|
size_t v;
|
|
|
|
if (_lookup.TryGet(keyHash, v)) {
|
|
|
|
auto script = _scripts[v];
|
2020-02-05 13:52:50 +00:00
|
|
|
script->OnRemove();
|
2020-03-25 18:07:36 +00:00
|
|
|
_scripts.Remove(v);
|
|
|
|
_lookup.Remove(keyHash);
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
2019-11-08 17:45:43 +00:00
|
|
|
}
|
2019-11-09 11:15:45 +00:00
|
|
|
|
2019-12-14 11:15:30 +00:00
|
|
|
void Clear() {
|
2020-02-05 13:52:50 +00:00
|
|
|
for (auto s : _scripts) {
|
2020-03-25 18:07:36 +00:00
|
|
|
s->OnRemove();
|
2020-02-05 13:52:50 +00:00
|
|
|
delete s;
|
|
|
|
}
|
2020-03-22 18:21:40 +00:00
|
|
|
_scripts.Clear();
|
2020-03-25 18:07:36 +00:00
|
|
|
_lookup.Clear();
|
2019-12-14 11:15:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 16:31:06 +00:00
|
|
|
bool Has(const ArbUt::CaseInsensitiveConstString& key) const { return _lookup.Has(key); }
|
2020-03-09 09:16:57 +00:00
|
|
|
|
2020-03-25 18:07:36 +00:00
|
|
|
bool Has(uint32_t keyHash) const { return _lookup.Has(keyHash); }
|
2020-02-23 15:17:57 +00:00
|
|
|
|
2020-04-23 21:23:58 +00:00
|
|
|
inline size_t Count() const { return _scripts.Count(); }
|
2019-11-09 11:15:45 +00:00
|
|
|
|
2020-06-05 16:06:45 +00:00
|
|
|
inline const ArbUt::UniquePtrList<Script>& GetIterator() const { return _scripts; }
|
2019-11-08 17:45:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
#endif // CREATURELIB_SCRIPTSET_HPP
|