Added lots of security using asserts.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-03-22 13:42:26 +01:00
parent 970ca8ddd5
commit 899e432271
35 changed files with 138 additions and 56 deletions

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_BASELIBRARY_HPP
#define CREATURELIB_BASELIBRARY_HPP
#include <Arbutils/Assert.hpp>
#include <Arbutils/ConstString.hpp>
#include <algorithm>
#include <string>
@@ -21,21 +22,19 @@ namespace CreatureLib::Library {
}
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
AssertNotNull(value)
_values.insert({key.GetHash(), value});
}
inline void Insert(uint32_t hashedKey, const T* value) { _values.insert({hashedKey, value}); }
inline void Insert(uint32_t hashedKey, const T* value) {
AssertNotNull(value)
_values.insert({hashedKey, value});
}
inline void Delete(const Arbutils::CaseInsensitiveConstString& key) { _values.erase(key.GetHash()); }
inline void Delete(uint32_t hashedKey) { _values.erase({hashedKey}); }
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
auto find = this->_values.find(name.GetHash());
if (find == this->_values.end()) {
out = nullptr;
return false;
}
out = find->second;
return true;
return TryGet(name.GetHash(), out);
}
bool TryGet(uint32_t hashedKey, const T*& out) const {
auto find = this->_values.find(hashedKey);

View File

@@ -1,11 +1,14 @@
#include "CreatureSpecies.hpp"
#include <Arbutils/Assert.hpp>
using namespace CreatureLib::Library;
CreatureSpecies::CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant,
float genderRatio, const ConstString& growthRate, uint8_t captureRate)
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
_variants({{"default"_cnc, defaultVariant}}) {}
_variants({{"default"_cnc, defaultVariant}}) {
AssertNotNull(defaultVariant)
}
bool CreatureSpecies::HasVariant(const ConstString& name) const { return _variants.find(name) != _variants.end(); }

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_LEARNABLEATTACKS_HPP
#define CREATURELIB_LEARNABLEATTACKS_HPP
#include <Arbutils/Assert.hpp>
#include <unordered_map>
#include <vector>
#include "../Attacks/AttackData.hpp"
@@ -11,7 +12,12 @@ namespace CreatureLib::Library {
public:
LearnableAttacks(size_t levelAttackCapacity)
: _learnedByLevel(std::unordered_map<uint8_t, std::vector<const AttackData*>>(levelAttackCapacity)) {}
: _learnedByLevel(std::unordered_map<uint8_t, std::vector<const AttackData*>>(levelAttackCapacity)) {
for (auto kv : _learnedByLevel) {
for (auto attack : kv.second)
AssertNotNull(attack)
}
}
void AddLevelMove(uint8_t level, const AttackData* attack);

View File

@@ -37,7 +37,7 @@ const CreatureLib::Library::LearnableAttacks* CreatureLib::Library::SpeciesVaria
CreatureLib::Library::SpeciesVariant::SpeciesVariant(ConstString name, float height, float weight,
uint32_t baseExperience, std::vector<uint8_t> types,
CreatureLib::Library::StatisticSet<uint16_t> baseStats,
const CreatureLib::Library::StatisticSet<uint16_t>& baseStats,
std::vector<ConstString> talents,
std::vector<ConstString> secretTalents,
const LearnableAttacks* attacks)

View File

@@ -23,14 +23,14 @@ namespace CreatureLib::Library {
private:
std::vector<uint8_t> _types;
const Library::StatisticSet<uint16_t> _baseStatistics;
const Library::StatisticSet<uint16_t>& _baseStatistics;
std::vector<ConstString> _talents;
std::vector<ConstString> _secretTalents;
const LearnableAttacks* _attacks;
public:
SpeciesVariant(ConstString name, float height, float weight, uint32_t baseExperience,
std::vector<uint8_t> types, Library::StatisticSet<uint16_t> baseStats,
std::vector<uint8_t> types, const Library::StatisticSet<uint16_t>& baseStats,
std::vector<ConstString> talents, std::vector<ConstString> secretTalents,
const LearnableAttacks* attacks);

View File

@@ -6,7 +6,14 @@ CreatureLib::Library::DataLibrary::DataLibrary(LibrarySettings* settings, Creatu
CreatureLib::Library::GrowthRateLibrary* growthRates,
TypeLibrary* typeLibrary)
: _settings(settings), _species(species), _attacks(attacks), _items(items), _growthRates(growthRates),
_typeLibrary(typeLibrary) {}
_typeLibrary(typeLibrary) {
AssertNotNull(_settings)
AssertNotNull(_species)
AssertNotNull(_attacks)
AssertNotNull(_items)
AssertNotNull(_growthRates)
AssertNotNull(_typeLibrary)
}
const CreatureLib::Library::LibrarySettings* CreatureLib::Library::DataLibrary::GetSettings() const {
return _settings;

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_EXTERNGROWTHRATE_HPP
#define CREATURELIB_EXTERNGROWTHRATE_HPP
#include <Arbutils/Assert.hpp>
#include "GrowthRate.hpp"
namespace CreatureLib::Library {
class ExternGrowthRate : public GrowthRate {
@@ -9,7 +10,10 @@ namespace CreatureLib::Library {
public:
ExternGrowthRate(uint8_t (*calcLevel)(uint32_t), uint32_t (*calcExperience)(uint8_t level))
: _calcLevel(calcLevel), _calcExperience(calcExperience) {}
: _calcLevel(calcLevel), _calcExperience(calcExperience) {
AssertNotNull(calcLevel)
AssertNotNull(calcExperience)
}
uint8_t CalculateLevel(uint32_t experience) const override { return _calcLevel(experience); }
uint32_t CalculateExperience(uint8_t level) const override { return _calcExperience(level); }

View File

@@ -20,7 +20,10 @@ namespace CreatureLib::Library {
return _experience[_experience.size() - 1];
}
uint32_t CalculateExperience(uint8_t level) const override { return _experience[level - 1]; }
uint32_t CalculateExperience(uint8_t level) const override {
Assert(level <= _experience.size())
return _experience[level - 1];
}
};
}

View File

@@ -15,6 +15,9 @@ namespace CreatureLib::Library {
T _magicalDefense;
T _speed;
private:
StatisticSet<T>(const StatisticSet<T>& v) = delete;
public:
StatisticSet(T health, T physicalAttack, T physicalDefense, T magicalAttack, T magicalDefense, T speed)
: _health(health), _physicalAttack(physicalAttack), _physicalDefense(physicalDefense),

View File

@@ -1,5 +1,5 @@
#include "TypeLibrary.hpp"
#include <algorithm>
#include <Arbutils/Assert.hpp>
using namespace CreatureLib::Library;
@@ -12,6 +12,8 @@ float TypeLibrary::GetEffectiveness(uint8_t attacking, const std::vector<uint8_t
}
float TypeLibrary::GetSingleEffectiveness(uint8_t attacking, uint8_t defensive) const {
Assert(attacking < _effectiveness.size())
Assert(defensive < _effectiveness.size())
return _effectiveness[attacking][defensive];
}
@@ -36,5 +38,7 @@ uint8_t TypeLibrary::RegisterType(uint32_t key) {
}
void TypeLibrary::SetEffectiveness(uint8_t attacking, uint8_t defensive, float effectiveness) {
Assert(attacking < _effectiveness.size())
Assert(defensive < _effectiveness.size())
_effectiveness[attacking][defensive] = effectiveness;
}