summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--compat/assert.hpp20
-rw-r--r--compat/debug.cpp28
-rw-r--r--compat/debug.hpp39
-rw-r--r--compat/strerror.cpp2
-rw-r--r--compat/strerror.hpp1
-rw-r--r--loader/filesystem.cpp13
-rw-r--r--wall-tileset-tool/main.cpp35
7 files changed, 82 insertions, 56 deletions
diff --git a/compat/assert.hpp b/compat/assert.hpp
index bdb6b578..d72186ea 100644
--- a/compat/assert.hpp
+++ b/compat/assert.hpp
@@ -92,16 +92,16 @@
{ \
if (a != b) [[unlikely]] \
{ \
- WARN_nospace << Debug::color(Debug::Color::Magenta) \
- << "fatal:" \
- << Debug::resetColor << " " \
- << "Equality assertion failed at " \
- << __FILE__ << ":" << __LINE__; \
- WARN_nospace << #__VA_ARGS__; \
- WARN_nospace << " expected: " << a; \
- WARN_nospace << " actual: " << b; \
- fm_EMIT_ABORT(); \
- } \
+ ERR_nospace << Debug::color(Debug::Color::Magenta) \
+ << "fatal:" \
+ << Debug::resetColor << " " \
+ << "Equality assertion failed at " \
+ << __FILE__ << ":" << __LINE__; \
+ ERR_nospace << #__VA_ARGS__; \
+ ERR_nospace << " expected: " << a; \
+ ERR_nospace << " actual: " << b; \
+ fm_EMIT_ABORT(); \
+ } \
})(__VA_ARGS__)
#ifdef __GNUG__
diff --git a/compat/debug.cpp b/compat/debug.cpp
index e3efaf5c..a3c2ba25 100644
--- a/compat/debug.cpp
+++ b/compat/debug.cpp
@@ -1,8 +1,22 @@
#include "debug.hpp"
#include "compat/strerror.hpp"
+#include <cerrno>
+#include <Corrade/Containers/StringView.h>
+
+// Error{} << "error" << colon() << "can't open file" << colon() << quoted("foo") << error_string(EINVAL);
+// ===> "error: can't open file 'foo': Invalid argument"
namespace floormat::detail::corrade_debug {
+Debug& operator<<(Debug& dbg, Colon)
+{
+ auto flags = dbg.flags();
+ dbg.setFlags(flags | Debug::Flag::NoSpace);
+ dbg << ":";
+ dbg.setFlags(flags);
+ return dbg;
+}
+
Debug& operator<<(Debug& dbg, ErrorString box)
{
auto flags = dbg.flags();
@@ -14,9 +28,7 @@ Debug& operator<<(Debug& dbg, ErrorString box)
return dbg;
}
-template struct Quoted<StringView>;
-
-Debug::Flags debug1(Debug& dbg, char c)
+Debug::Flags quoted_begin(Debug& dbg, char c)
{
auto flags = dbg.flags();
dbg << "";
@@ -26,7 +38,7 @@ Debug::Flags debug1(Debug& dbg, char c)
return flags;
}
-Debug& debug2(Debug& dbg, Debug::Flags flags, char c)
+Debug& quoted_end(Debug& dbg, Debug::Flags flags, char c)
{
char buf[2] { c, '\0' };
dbg << buf;
@@ -34,10 +46,16 @@ Debug& debug2(Debug& dbg, Debug::Flags flags, char c)
return dbg;
}
+template struct Quoted<StringView>;
+
} // namespace floormat::detail::corrade_debug
namespace floormat {
-floormat::detail::corrade_debug::ErrorString error_string(int error) { return { error }; }
+using namespace floormat::detail::corrade_debug;
+
+Colon colon() { return Colon{}; }
+ErrorString error_string(int error) { return { error }; }
+ErrorString error_string() { return { errno }; }
} // namespace floormat
diff --git a/compat/debug.hpp b/compat/debug.hpp
index 0009ce52..56db4b0f 100644
--- a/compat/debug.hpp
+++ b/compat/debug.hpp
@@ -1,31 +1,50 @@
#pragma once
#include <Corrade/Utility/Debug.h>
-#include <Corrade/Utility/Move.h>
#include <Corrade/Containers/Containers.h>
+#include <concepts>
+
+namespace floormat {
+
+template<typename T>
+concept DebugPrintable = requires(Debug& dbg, const T& value) {
+ { dbg << value } -> std::convertible_to<Debug&>;
+};
+
+} // namespace floormat
namespace floormat::detail::corrade_debug {
-struct ErrorString final { int value; };
+// ***** colon *****
+struct Colon {};
+Debug& operator<<(Debug& dbg, Colon box);
+
+// ***** error string *****
+struct ErrorString { int value; };
Debug& operator<<(Debug& dbg, ErrorString box);
-template<typename T> struct Quoted final { const T& value; };
-Debug::Flags debug1(Debug& dbg, char c);
-Debug& debug2(Debug& dbg, Debug::Flags flags, char c);
+// ***** quoted *****
+template<typename T> struct Quoted { const T& value; };
+Debug::Flags quoted_begin(Debug& dbg, char c);
+Debug& quoted_end(Debug& dbg, Debug::Flags flags, char c);
template<typename T> Debug& operator<<(Debug& dbg, Quoted<T> box)
{
- Debug::Flags flags = debug1(dbg, '\'');
+ Debug::Flags flags = quoted_begin(dbg, '\'');
dbg << box.value;
- return debug2(dbg, flags, '\'');
+ return quoted_end(dbg, flags, '\'');
}
-extern template struct Quoted<StringView>;
-
} // namespace floormat::detail::corrade_debug
+// ***** api *****
+
+// ***** functions *****
+
namespace floormat {
+floormat::detail::corrade_debug::Colon colon();
floormat::detail::corrade_debug::ErrorString error_string(int error);
-template<typename T> floormat::detail::corrade_debug::Quoted<T> quoted(const T& value) { return { value }; }
+floormat::detail::corrade_debug::ErrorString error_string();
+template<DebugPrintable T> floormat::detail::corrade_debug::Quoted<T> quoted(const T& value) { return { value }; }
} // namespace floormat
diff --git a/compat/strerror.cpp b/compat/strerror.cpp
index 8b237153..f952cd3c 100644
--- a/compat/strerror.cpp
+++ b/compat/strerror.cpp
@@ -1,6 +1,7 @@
#include "strerror.hpp"
#include <cerrno>
#include <string.h>
+#include <Corrade/Containers/StringView.h>
namespace floormat {
@@ -19,7 +20,6 @@ StringView get_error_string(ArrayView<char> buf, int error)
if (status == 0)
return { buf.data() };
#endif
-
return "Unknown error"_s;
};
diff --git a/compat/strerror.hpp b/compat/strerror.hpp
index 8bb33ec7..6b3c24eb 100644
--- a/compat/strerror.hpp
+++ b/compat/strerror.hpp
@@ -1,6 +1,5 @@
#pragma once
#include <Corrade/Containers/ArrayView.h>
-#include <Corrade/Containers/StringView.h>
namespace floormat {
diff --git a/loader/filesystem.cpp b/loader/filesystem.cpp
index 65878209..be3aa1df 100644
--- a/loader/filesystem.cpp
+++ b/loader/filesystem.cpp
@@ -1,10 +1,9 @@
#include "impl.hpp"
#include "compat/assert.hpp"
+#include "compat/debug.hpp"
#include <cerrno>
#include <Corrade/Containers/Pair.h>
#include <Corrade/Containers/String.h>
-#include <Corrade/Utility/Debug.h>
-#include <Corrade/Utility/Implementation/ErrorString.h>
#include <Corrade/Utility/Path.h>
#ifdef _WIN32
#include <Corrade/Containers/Array.h>
@@ -31,10 +30,8 @@ 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, error);
+ auto err = error_string();
+ ERR << "chdir: can't change directory to" << quoted(pathname) << err;
}
return !ret;
}
@@ -55,8 +52,8 @@ void loader_impl::set_application_working_directory()
original_working_directory = std::move(*loc);
else
{
- Error err; err << "can't get original working directory:";
- Corrade::Utility::Implementation::printErrnoErrorString(err, errno);
+ auto err = error_string();
+ ERR << "can't get original working directory" << err;
original_working_directory = "."_s;
}
if (const auto loc = Path::executableLocation())
diff --git a/wall-tileset-tool/main.cpp b/wall-tileset-tool/main.cpp
index 20e5b23a..8fc463c8 100644
--- a/wall-tileset-tool/main.cpp
+++ b/wall-tileset-tool/main.cpp
@@ -60,6 +60,7 @@ const Direction& get_direction(const wall_atlas_def& atlas, size_t i)
fm_assert(atlas.direction_mask[i]);
auto idx = atlas.direction_map[i];
fm_assert(idx);
+ fm_assert(idx < atlas.direction_array.size());
return atlas.direction_array[idx.val];
}
@@ -68,10 +69,12 @@ Direction& get_direction(wall_atlas_def& atlas, size_t i)
return const_cast<Direction&>(get_direction(const_cast<const wall_atlas_def&>(atlas), i));
}
-bool do_group(state& st, size_t i, size_t j)
+bool do_group(state st, size_t i, size_t j)
{
- const auto& old_dir = get_direction(st.old_atlas, i);
- auto& new_dir = get_direction(st.new_atlas, i);
+ const wall_atlas_def& old_atlas = st.old_atlas;
+ wall_atlas_def& new_atlas = st.new_atlas;
+ const auto& old_dir = get_direction(old_atlas, (size_t)i);
+ //auto& new_dir = get_direction(new_atlas, (size_t)i);
return true;
}
@@ -84,22 +87,6 @@ bool do_direction(state& st, size_t i)
const auto& old_dir = get_direction(st.old_atlas, i);
-#if 0
- // todo from_rotation
- auto path = Path::join(st.opts.input_dir, name);
- if (!Path::isDirectory(path))
- {
- char errbuf[128];
- 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;
- }
-#endif
-
fm_assert(!atlas.direction_mask[i]);
fm_assert(!atlas.direction_map[i]);
const auto dir_idx = atlas.direction_array.size();
@@ -112,6 +99,12 @@ bool do_direction(state& st, size_t i)
.passability = old_dir.passability,
};
+ for (auto [_str, _ptr, tag] : Direction::groups)
+ {
+ if (!do_group(st, i, (size_t)tag))
+ return false;
+ }
+
st.new_atlas.direction_array.push_back(std::move(dir));
return true;
@@ -169,8 +162,8 @@ Triple<options, Arguments, bool> parse_cmdline(int argc, const char* const* argv
if (opts.output_dir.isEmpty())
opts.output_dir = opts.input_dir;
- DBG << "input-dir" << quoted(opts.input_dir);
- DBG << "output-dir" << quoted(opts.output_dir);
+ DBG << "input-dir" << colon() << quoted(opts.input_dir);
+ DBG << "output-dir" << colon() << quoted(opts.output_dir);
if (!Path::exists(opts.input_file))
Error{Error::Flag::NoSpace} << "fatal: input file '" << opts.input_file << "' doesn't exist";