Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate missile sprite data to txtdata #6821

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMake/Assets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ set(devilutionx_assets
txtdata/items/item_suffixes.tsv
txtdata/items/itemdat.tsv
txtdata/items/unique_itemdat.tsv
txtdata/missiles/missile_sprites.tsv
txtdata/monsters/monstdat.tsv
txtdata/monsters/unique_monstdat.tsv
txtdata/sound/effects.tsv
Expand Down
16 changes: 16 additions & 0 deletions Source/data/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ tl::expected<DataFile, DataFile::Error> DataFile::load(std::string_view path)
return DataFile { std::move(data), size };
}

DataFile DataFile::loadOrDie(std::string_view path)
{
tl::expected<DataFile, DataFile::Error> dataFileResult = DataFile::load(path);
if (!dataFileResult.has_value()) {
DataFile::reportFatalError(dataFileResult.error(), path);
}
return *std::move(dataFileResult);
}

void DataFile::reportFatalError(Error code, std::string_view fileName)
{
switch (code) {
Expand Down Expand Up @@ -135,6 +144,13 @@ tl::expected<void, DataFile::Error> DataFile::skipHeader()
return {};
}

void DataFile::skipHeaderOrDie(std::string_view path)
{
if (tl::expected<void, DataFile::Error> result = skipHeader(); !result.has_value()) {
DataFile::reportFatalError(result.error(), path);
}
}

[[nodiscard]] size_t DataFile::numRecords() const
{
if (content_.empty()) return 0;
Expand Down
4 changes: 4 additions & 0 deletions Source/data/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class DataFile {
*/
static tl::expected<DataFile, Error> load(std::string_view path);

static DataFile loadOrDie(std::string_view path);

static void reportFatalError(Error code, std::string_view fileName);
static void reportFatalFieldError(DataFileField::Error code, std::string_view fileName, std::string_view fieldName, const DataFileField &field, std::string_view details = {});

Expand Down Expand Up @@ -110,6 +112,8 @@ class DataFile {

[[nodiscard]] tl::expected<void, DataFile::Error> skipHeader();

void skipHeaderOrDie(std::string_view path);

[[nodiscard]] RecordIterator begin() const
{
return { body_, data() + size(), body_ != data() };
Expand Down
20 changes: 16 additions & 4 deletions Source/data/iterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,36 @@ class DataFileField {
return tl::make_unexpected(DataFileField::Error::InvalidValue);
}

template <typename T, size_t N>
[[nodiscard]] tl::expected<void, Error> parseIntArray(T (&destination)[N])
template <typename T>
[[nodiscard]] tl::expected<void, Error> parseIntArray(T *destination, size_t n)
{
size_t i = 0;
for (const std::string_view part : SplitByChar(value(), ',')) {
if (i == N)
if (i == n)
return tl::make_unexpected(Error::InvalidValue);
const std::from_chars_result result
= std::from_chars(part.data(), part.data() + part.size(), destination[i]);
if (result.ec != std::errc())
return mapError(result.ec);
++i;
}
if (i != N)
if (i != n)
return tl::make_unexpected(Error::InvalidValue);
return {};
}

template <typename T, size_t N>
[[nodiscard]] tl::expected<void, Error> parseIntArray(T (&destination)[N])
{
return parseIntArray<T>(destination, N);
}

template <typename T, size_t N>
[[nodiscard]] tl::expected<void, Error> parseIntArray(std::array<T, N> &destination)
{
return parseIntArray<T>(destination.data(), N);
}

template <typename T, typename ParseFn>
[[nodiscard]] tl::expected<void, std::string> parseEnumList(T &destination, ParseFn &&parseFn)
{
Expand Down
58 changes: 35 additions & 23 deletions Source/data/record_reader.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <array>
#include <string_view>
#include <type_traits>

Expand Down Expand Up @@ -29,9 +30,7 @@ class RecordReader {
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseInt(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseInt(out), name, field);
}

template <typename T>
Expand All @@ -41,19 +40,23 @@ class RecordReader {
advance();
DataFileField field = *it_;
if (field.value().empty()) return;
if (tl::expected<void, DataFileField::Error> result = field.parseInt(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseInt(out), name, field);
}

template <typename T, size_t N>
void readIntArray(std::string_view name, T (&out)[N])
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseIntArray(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseIntArray(out), name, field);
}

template <typename T, size_t N>
void readIntArray(std::string_view name, std::array<T, N> &out)
{
advance();
DataFileField field = *it_;
failOnError(field.parseIntArray(out), name, field);
}

template <typename T>
Expand All @@ -62,18 +65,14 @@ class RecordReader {
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseFixed6(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseFixed6(out), name, field);
}

void readBool(std::string_view name, bool &out)
{
advance();
DataFileField field = *it_;
if (tl::expected<void, DataFileField::Error> result = field.parseBool(out); !result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
failOnError(field.parseBool(out), name, field);
}

void readString(std::string_view name, std::string &out)
Expand All @@ -87,21 +86,18 @@ class RecordReader {
{
advance();
DataFileField field = *it_;
if (tl::expected<T, std::string> result = parseFn(field.value()); result.has_value()) {
out = *std::move(result);
} else {
DataFile::reportFatalFieldError(DataFileField::Error::InvalidValue, filename_, name, field, result.error());
}
tl::expected<T, std::string> result = parseFn(field.value());
failOnError(result, name, field, DataFileField::Error::InvalidValue);
out = *std::move(result);
}

template <typename T, typename F>
void readEnumList(std::string_view name, T &out, F &&parseFn)
{
advance();
DataFileField field = *it_;
if (tl::expected<void, std::string> result = field.parseEnumList(out, std::forward<F>(parseFn)); !result.has_value()) {
DataFile::reportFatalFieldError(DataFileField::Error::InvalidValue, filename_, name, field, result.error());
}
failOnError(field.parseEnumList(out, std::forward<F>(parseFn)),
name, field, DataFileField::Error::InvalidValue);
}

std::string_view value()
Expand All @@ -114,6 +110,22 @@ class RecordReader {
void advance();

private:
template <typename T>
void failOnError(const tl::expected<T, DataFileField::Error> &result, std::string_view name, const DataFileField &field)
{
if (!result.has_value()) {
DataFile::reportFatalFieldError(result.error(), filename_, name, field);
}
}

template <typename T>
void failOnError(const tl::expected<T, std::string> &result, std::string_view name, const DataFileField &field, DataFileField::Error error)
{
if (!result.has_value()) {
DataFile::reportFatalFieldError(error, filename_, name, field, result.error());
}
}

FieldIterator it_;
const FieldIterator end_;
std::string_view filename_;
Expand Down
1 change: 1 addition & 0 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
}
} else if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) {
LastMouseButtonAction = MouseActionType::AttackPlayerTarget;
NetSendCmdParam1(true, CMD_RATTACKPID, PlayerUnderCursor->getId());

Check warning on line 276 in Source/diablo.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'size_t' to 'uint16_t', possible loss of data
}
} else {
if (bShift) {
Expand All @@ -293,7 +293,7 @@
NetSendCmdParam1(true, CMD_ATTACKID, pcursmonst);
} else if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) {
LastMouseButtonAction = MouseActionType::AttackPlayerTarget;
NetSendCmdParam1(true, CMD_ATTACKPID, PlayerUnderCursor->getId());

Check warning on line 296 in Source/diablo.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'size_t' to 'uint16_t', possible loss of data
}
}
if (!bShift && pcursitem == -1 && ObjectUnderCursor == nullptr && pcursmonst == -1 && PlayerUnderCursor == nullptr) {
Expand Down Expand Up @@ -2476,6 +2476,7 @@
LoadPlayerDataFiles();

// TODO: We can probably load this much later (when the game is starting).
LoadMissileData();
LoadMonsterData();
LoadItemData();

Expand All @@ -2496,7 +2497,7 @@
{
if (pcurs == CURSOR_RESURRECT) {
if (PlayerUnderCursor != nullptr) {
NetSendCmdParam1(true, CMD_RESURRECT, PlayerUnderCursor->getId());

Check warning on line 2500 in Source/diablo.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'size_t' to 'uint16_t', possible loss of data
NewCursor(CURSOR_HAND);
return true;
}
Expand All @@ -2506,7 +2507,7 @@

if (pcurs == CURSOR_HEALOTHER) {
if (PlayerUnderCursor != nullptr) {
NetSendCmdParam1(true, CMD_HEALOTHER, PlayerUnderCursor->getId());

Check warning on line 2510 in Source/diablo.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'size_t' to 'uint16_t', possible loss of data
NewCursor(CURSOR_HAND);
return true;
}
Expand Down Expand Up @@ -2578,7 +2579,7 @@
} else if (pcursmonst != -1) {
NetSendCmdParam5(true, CMD_SPELLID, pcursmonst, static_cast<int8_t>(spellID), static_cast<uint8_t>(spellType), spellLevel, spellFrom);
} else if (PlayerUnderCursor != nullptr && !myPlayer.friendlyMode) {
NetSendCmdParam5(true, CMD_SPELLPID, PlayerUnderCursor->getId(), static_cast<int8_t>(spellID), static_cast<uint8_t>(spellType), spellLevel, spellFrom);

Check warning on line 2582 in Source/diablo.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'size_t' to 'uint16_t', possible loss of data
} else {
NetSendCmdLocParam4(true, CMD_SPELLXY, cursPosition, static_cast<int8_t>(spellID), static_cast<uint8_t>(spellType), spellLevel, spellFrom);
}
Expand Down
12 changes: 2 additions & 10 deletions Source/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,8 @@ tl::expected<sfx_flag, std::string> ParseSfxFlag(std::string_view value)
void LoadEffectsData()
{
const std::string_view filename = "txtdata\\sound\\effects.tsv";
tl::expected<DataFile, DataFile::Error> dataFileResult = DataFile::load(filename);
if (!dataFileResult.has_value()) {
DataFile::reportFatalError(dataFileResult.error(), filename);
}

DataFile &dataFile = dataFileResult.value();
if (tl::expected<void, DataFile::Error> result = dataFile.skipHeader();
!result.has_value()) {
DataFile::reportFatalError(result.error(), filename);
}
DataFile dataFile = DataFile::loadOrDie(filename);
dataFile.skipHeaderOrDie(filename);

sgSFX.clear();
sgSFX.reserve(dataFile.numRecords());
Expand Down
36 changes: 6 additions & 30 deletions Source/itemdat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,8 @@ tl::expected<goodorevil, std::string> ParseAffixAlignment(std::string_view value
void LoadItemDat()
{
const std::string_view filename = "txtdata\\items\\itemdat.tsv";
tl::expected<DataFile, DataFile::Error> dataFileResult = DataFile::load(filename);
if (!dataFileResult.has_value()) {
DataFile::reportFatalError(dataFileResult.error(), filename);
}

DataFile &dataFile = dataFileResult.value();
if (tl::expected<void, DataFile::Error> result = dataFile.skipHeader();
!result.has_value()) {
DataFile::reportFatalError(result.error(), filename);
}
DataFile dataFile = DataFile::loadOrDie(filename);
dataFile.skipHeaderOrDie(filename);

AllItemsList.clear();
AllItemsList.reserve(dataFile.numRecords());
Expand Down Expand Up @@ -531,16 +523,8 @@ void ReadItemPower(RecordReader &reader, std::string_view fieldName, ItemPower &
void LoadUniqueItemDat()
{
const std::string_view filename = "txtdata\\items\\unique_itemdat.tsv";
tl::expected<DataFile, DataFile::Error> dataFileResult = DataFile::load(filename);
if (!dataFileResult.has_value()) {
DataFile::reportFatalError(dataFileResult.error(), filename);
}

DataFile &dataFile = dataFileResult.value();
if (tl::expected<void, DataFile::Error> result = dataFile.skipHeader();
!result.has_value()) {
DataFile::reportFatalError(result.error(), filename);
}
DataFile dataFile = DataFile::loadOrDie(filename);
dataFile.skipHeaderOrDie(filename);

UniqueItems.clear();
UniqueItems.reserve(dataFile.numRecords());
Expand All @@ -565,16 +549,8 @@ void LoadUniqueItemDat()

void LoadItemAffixesDat(std::string_view filename, std::vector<PLStruct> &out)
{
tl::expected<DataFile, DataFile::Error> dataFileResult = DataFile::load(filename);
if (!dataFileResult.has_value()) {
DataFile::reportFatalError(dataFileResult.error(), filename);
}

DataFile &dataFile = dataFileResult.value();
if (tl::expected<void, DataFile::Error> result = dataFile.skipHeader();
!result.has_value()) {
DataFile::reportFatalError(result.error(), filename);
}
DataFile dataFile = DataFile::loadOrDie(filename);
dataFile.skipHeaderOrDie(filename);

out.clear();
out.reserve(dataFile.numRecords());
Expand Down
Loading
Loading