mirror of
https://github.com/diasurgical/DevilutionX.git
synced 2026-10-12 02:13:06 +00:00
Replaces ParseFixed6/parseFixed6/readFixed6/readDecimal's raw-int plumbing with the new FixedPoint type, then propagates strongly-typed fixed-point fields through everywhere that consumed them: player _pHitPoints/_pMana (and their base/max variants), ClassAttributes' percentage/multiplier fields, Item::_iPLHP/_iPLMana, and Monster::hitPoints/maxHitPoints. Eliminates manual <<6/>>6 bit-shifting at nearly every call site in favor of fromInt/fromRaw/whole() and the new fixed-point arithmetic operators, while preserving raw() only at genuine serialization/network wire-format boundaries. Co-Authored-By: Claude Sonnet 5 <[email protected]>
107 lines
3.1 KiB
C++
107 lines
3.1 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <expected>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
|
|
#include <function_ref.hpp>
|
|
|
|
#include "data/file.hpp"
|
|
#include "data/iterators.hpp"
|
|
#include "utils/fixed_point.hpp"
|
|
|
|
namespace devilution {
|
|
|
|
/**
|
|
* @brief A value reader.
|
|
*/
|
|
class ValueReader {
|
|
public:
|
|
ValueReader(DataFile &dataFile, std::string_view filename);
|
|
|
|
DataFileField getValueField(std::string_view expectedKey);
|
|
|
|
template <typename T, typename F>
|
|
void readValue(std::string_view expectedKey, T &outValue, F &&readFn)
|
|
{
|
|
DataFileField valueField = getValueField(expectedKey);
|
|
if (const std::expected<void, devilution::DataFileField::Error> result = readFn(valueField, outValue);
|
|
!result.has_value()) {
|
|
DataFile::reportFatalFieldError(result.error(), filename_, "Value", valueField);
|
|
}
|
|
++it_;
|
|
}
|
|
|
|
template <typename T, typename F>
|
|
void read(std::string_view expectedKey, T &outValue, F &&parseFn)
|
|
{
|
|
readValue(expectedKey, outValue, [&parseFn](DataFileField &valueField, T &outValue) -> std::expected<void, devilution::DataFileField::Error> {
|
|
const auto result = parseFn(valueField.value());
|
|
if (!result.has_value()) {
|
|
return std::unexpected(devilution::DataFileField::Error::InvalidValue);
|
|
}
|
|
|
|
outValue = result.value();
|
|
return {};
|
|
});
|
|
}
|
|
|
|
template <typename T, typename F>
|
|
void readEnumList(std::string_view expectedKey, T &outValue, F &&parseFn)
|
|
{
|
|
readValue(expectedKey, outValue, [&parseFn](DataFileField &valueField, T &outValue) -> std::expected<void, devilution::DataFileField::Error> {
|
|
const auto result = valueField.parseEnumList(outValue, std::forward<F>(parseFn));
|
|
if (!result.has_value()) {
|
|
return std::unexpected(devilution::DataFileField::Error::InvalidValue);
|
|
}
|
|
|
|
return {};
|
|
});
|
|
}
|
|
|
|
template <typename T>
|
|
typename std::enable_if_t<std::is_integral_v<T>, void>
|
|
readInt(std::string_view expectedKey, T &outValue)
|
|
{
|
|
readValue(expectedKey, outValue, [](DataFileField &valueField, T &outValue) {
|
|
return valueField.parseInt(outValue);
|
|
});
|
|
}
|
|
|
|
template <Fixed6Type T>
|
|
void readDecimal(std::string_view expectedKey, T &outValue)
|
|
{
|
|
readValue(expectedKey, outValue, [](DataFileField &valueField, T &outValue) {
|
|
return valueField.parseFixed6(outValue);
|
|
});
|
|
}
|
|
|
|
void readString(std::string_view expectedKey, std::string &outValue)
|
|
{
|
|
readValue(expectedKey, outValue, [](DataFileField &valueField, std::string &outValue) {
|
|
outValue = valueField.value();
|
|
return std::expected<void, devilution::DataFileField::Error> {};
|
|
});
|
|
}
|
|
|
|
void readChar(std::string_view expectedKey, char &outValue)
|
|
{
|
|
readValue(expectedKey, outValue, [](DataFileField &valueField, char &outValue) -> std::expected<void, devilution::DataFileField::Error> {
|
|
if (valueField.value().size() != 1) {
|
|
return std::unexpected(devilution::DataFileField::Error::InvalidValue);
|
|
}
|
|
|
|
outValue = valueField.value().at(0);
|
|
return {};
|
|
});
|
|
}
|
|
|
|
private:
|
|
RecordIterator it_;
|
|
const RecordIterator end_;
|
|
std::string_view filename_;
|
|
};
|
|
|
|
} // namespace devilution
|