diff options
-rw-r--r-- | anim-crop-tool/main.cpp | 6 | ||||
-rw-r--r-- | compat/prelude.hpp | 1 | ||||
-rw-r--r-- | compat/strerror.cpp | 19 | ||||
-rw-r--r-- | compat/strerror.hpp | 1 | ||||
-rw-r--r-- | loader/filesystem.cpp | 3 | ||||
-rw-r--r-- | serialize/world-reader.cpp | 3 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 18 | ||||
-rw-r--r-- | wall-tileset-tool/main.cpp | 48 |
8 files changed, 70 insertions, 29 deletions
diff --git a/anim-crop-tool/main.cpp b/anim-crop-tool/main.cpp index 52fd2c6b..9cd3bcd4 100644 --- a/anim-crop-tool/main.cpp +++ b/anim-crop-tool/main.cpp @@ -1,6 +1,7 @@ #include "atlas.hpp" #include "compat/assert.hpp" #include "compat/defs.hpp" +#include "compat/strerror.hpp" #include "compat/sysexits.hpp" #include "compat/fix-argv0.hpp" #include "loader/loader.hpp" @@ -270,8 +271,9 @@ int main(int argc, char** argv) : anim_info.object_name; if (auto pathname = Path::join(opts.output_dir, (base_name + ".png")); !atlas.dump(pathname)) { - Error{} << "error: failed writing image to" << pathname << ":" - << std::strerror(errno); // NOLINT(concurrency-mt-unsafe) + char errbuf[128]; + auto errstr = get_error_string(errbuf); + ERR_nospace << "error: failed writing image to '" << pathname << "': " << errstr; return EX_CANTCREAT; } anim_info.pixel_size = Vector2ui(atlas.size()); diff --git a/compat/prelude.hpp b/compat/prelude.hpp index 79f54942..bb5c4da0 100644 --- a/compat/prelude.hpp +++ b/compat/prelude.hpp @@ -5,6 +5,7 @@ #include <Corrade/Utility/Macros.h> #include <Magnum/Magnum.h> +// todo add colors prefix thing #define DBG_nospace (::Corrade::Utility::Debug{::Corrade::Utility::Debug::Flag::NoSpace}) #define WARN_nospace (::Corrade::Utility::Warning{::Corrade::Utility::Debug::Flag::NoSpace}) #define ERR_nospace (::Corrade::Utility::Error{::Corrade::Utility::Debug::Flag::NoSpace}) diff --git a/compat/strerror.cpp b/compat/strerror.cpp index 303c78be..8b237153 100644 --- a/compat/strerror.cpp +++ b/compat/strerror.cpp @@ -1,26 +1,31 @@ #include "strerror.hpp" -#include <errno.h> +#include <cerrno> #include <string.h> namespace floormat { -StringView get_error_string(ArrayView<char> buf) +StringView get_error_string(ArrayView<char> buf, int error) { #ifdef _WIN32 - ::strerror_s(buf.data(), buf.size(), errno); + ::strerror_s(buf.data(), buf.size(), error); if (buf[0]) - return buf; + return { buf.data() }; #elif defined __GLIBC__ && !((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE) - char* str { ::strerror_r(errno, buf.data(), buf.size()) }; + char* str { ::strerror_r(error, buf.data(), buf.size()) }; if (str) return str; #else - int status { ::strerror_r(errno, buf.data(), buf.size()) }; + int status { ::strerror_r(error, buf.data(), buf.size()) }; if (status == 0) - return buf; + return { buf.data() }; #endif return "Unknown error"_s; }; +StringView get_error_string(ArrayView<char> buf) +{ + return get_error_string(buf, errno); +} + } // namespace floormat diff --git a/compat/strerror.hpp b/compat/strerror.hpp index 1929d97d..8bb33ec7 100644 --- a/compat/strerror.hpp +++ b/compat/strerror.hpp @@ -4,6 +4,7 @@ namespace floormat { +StringView get_error_string(ArrayView<char> buf, int error); StringView get_error_string(ArrayView<char> buf); } // namespace floormat diff --git a/loader/filesystem.cpp b/loader/filesystem.cpp index a7926da1..65878209 100644 --- a/loader/filesystem.cpp +++ b/loader/filesystem.cpp @@ -31,9 +31,10 @@ bool loader_impl::chdir(StringView pathname) #endif if (ret) { + auto error = errno; Error err; err << "chdir: can't change directory to" << pathname << Error::nospace << ":"; - Corrade::Utility::Implementation::printErrnoErrorString(err, errno); + Corrade::Utility::Implementation::printErrnoErrorString(err, error); } return !ret; } diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index 555785ff..ec8b733a 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -11,6 +11,7 @@ #include "src/anim-atlas.hpp" #include "src/chunk-scenery.hpp" #include "compat/strerror.hpp" +#include <cerrno> #include <cstring> #include <memory> @@ -473,9 +474,7 @@ world world::deserialize(StringView filename) fm_soft_assert(filename.flags() & StringViewFlag::NullTerminated); FILE_raii f = ::fopen(filename.data(), "rb"); if (!f) - { fm_throw("fopen(\"{}\", \"r\"): {}"_cf, filename, get_error_string(errbuf)); - } if (int ret = ::fseek(f, 0, SEEK_END); ret != 0) fm_throw("fseek(SEEK_END): {}"_cf, get_error_string(errbuf)); size_t len; diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index 9fb65e3d..c41fa89d 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -14,8 +14,9 @@ #include "src/anim-atlas.hpp" #include "src/light.hpp" #include "compat/strerror.hpp" -#include <concepts> +#include <cerrno> #include <cstring> +#include <concepts> #include <vector> #include <algorithm> #include <string_view> @@ -630,13 +631,22 @@ void world::serialize(StringView filename) Path::remove(filename); FILE_raii file = ::fopen(filename.data(), "wb"); if (!file) - fm_abort("fopen(\"%s\", \"w\"): %s", filename.data(), get_error_string(errbuf).data()); + { + int error = errno; + fm_abort("fopen(\"%s\", \"w\"): %s", filename.data(), get_error_string(errbuf, error).data()); + } writer_state s{*this}; const auto array = s.serialize_world(); if (auto len = ::fwrite(array.data(), array.size(), 1, file); len != 1) - fm_abort("fwrite: %s", get_error_string(errbuf).data()); + { + int error = errno; + fm_abort("fwrite: %s", get_error_string(errbuf, error).data()); + } if (int ret = ::fflush(file); ret != 0) - fm_abort("fflush: %s", get_error_string(errbuf).data()); + { + int error = errno; + fm_abort("fflush: %s", get_error_string(errbuf, error).data()); + } } } // namespace floormat diff --git a/wall-tileset-tool/main.cpp b/wall-tileset-tool/main.cpp index 974273fc..9245cd8c 100644 --- a/wall-tileset-tool/main.cpp +++ b/wall-tileset-tool/main.cpp @@ -2,6 +2,7 @@ #include "compat/sysexits.hpp" #include "compat/fix-argv0.hpp" #include "compat/strerror.hpp" +#include "compat/format.hpp" #include "src/wall-atlas.hpp" #include "serialize/wall-atlas.hpp" #include "serialize/json-helper.hpp" @@ -68,23 +69,38 @@ struct state int& error; }; -bool do_direction(state& st, Direction_ i) +bool do_direction(state& st, size_t i) { - const auto& name = wall_atlas::directions[(size_t)i].name; - DBG_nospace << " direction '" << name << "'"; - auto dir = Path::join(st.opts.input_dir, name); - if (!Path::isDirectory(dir)) + const auto name = wall_atlas::directions[i].name; + auto& atlas = st.new_atlas; + DBG << "-" << name; + + auto path = Path::join(st.opts.input_dir, name); + if (!Path::isDirectory(path)) { char errbuf[128]; - auto error = get_error_string(errbuf); - WARN_nospace << "fatal: direction '" << name - << "' has missing directory '" << dir - << "': " << error; + auto error = errno; + auto errstr = get_error_string(errbuf, error); + auto dbg = WARN_nospace; + dbg << "error: direction '" << name << "' needs directory '" << path << "'"; + if (error) + dbg << ": " << errstr; return false; } - auto dir_count = st.old_atlas.direction_mask.count(); - st.new_atlas.direction_array = std::vector<Direction>{dir_count}; + const auto dir_idx = atlas.direction_array.size(); + fm_assert(dir_idx == (uint8_t)dir_idx); + + fm_assert(!atlas.direction_mask[i]); + atlas.direction_mask[i] = 1; + fm_assert(!atlas.direction_map[i]); + atlas.direction_map[i] = DirArrayIndex{(uint8_t)dir_idx}; + + auto dir = Direction{ + + }; + + st.new_atlas.direction_array.push_back(std::move(dir)); return true; } @@ -97,13 +113,19 @@ bool do_input_file(state& st) fm_assert(loader.check_atlas_name(st.old_atlas.header.name)); fm_assert(st.old_atlas.direction_mask.any()); - st.new_atlas.header = std::move(const_cast<wall_atlas_def&>(st.old_atlas).header); + auto& atlas = st.new_atlas; + atlas.header = std::move(const_cast<wall_atlas_def&>(st.old_atlas).header); + + fm_assert(!atlas.frames.size()); + fm_assert(!atlas.direction_mask.any()); + fm_assert(atlas.direction_map == std::array<Wall::DirArrayIndex, Direction_COUNT>{}); + fm_assert(atlas.direction_array.empty()); for (auto i = 0uz; i < Direction_COUNT; i++) { if (!st.old_atlas.direction_mask[i]) continue; - if (!do_direction(st, (Direction_)i)) + if (!do_direction(st, i)) return false; } |