From db50c7a6a38eff251a14d57dcb9ae551ffe3a1d6 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 2 Dec 2022 16:01:28 +0100 Subject: loader, serialize: use more exception handling --- loader/atlas.cpp | 25 +++++++++++++------------ loader/impl.cpp | 2 +- loader/impl.hpp | 10 +++++----- loader/json.cpp | 6 ++++-- loader/loader.hpp | 8 ++++---- loader/texture.cpp | 14 ++++++++------ 6 files changed, 35 insertions(+), 30 deletions(-) (limited to 'loader') diff --git a/loader/atlas.cpp b/loader/atlas.cpp index 584d6b5a..d7f21ce5 100644 --- a/loader/atlas.cpp +++ b/loader/atlas.cpp @@ -1,5 +1,6 @@ #include "impl.hpp" #include "compat/assert.hpp" +#include "compat/exception.hpp" #include "src/emplacer.hpp" #include "src/tile-atlas.hpp" #include "src/anim-atlas.hpp" @@ -7,15 +8,15 @@ #include #include #include -#include +#include #include #include namespace floormat::loader_detail { -std::shared_ptr loader_impl::tile_atlas(StringView name, Vector2ub size) +std::shared_ptr loader_impl::tile_atlas(StringView name, Vector2ub size) noexcept(false) { - fm_assert(check_atlas_name(name)); + fm_soft_assert(check_atlas_name(name)); const emplacer e{[&] { return std::make_shared(name, texture(IMAGE_PATH, name), size); }}; auto atlas = tile_atlas_map.try_emplace(name, e).first->second; @@ -29,9 +30,9 @@ ArrayView loader_impl::anim_atlas_list() return anim_atlases; } -std::shared_ptr loader_impl::anim_atlas(StringView name, StringView dir) +std::shared_ptr loader_impl::anim_atlas(StringView name, StringView dir) noexcept(false) { - fm_assert(check_atlas_name(name)); + fm_soft_assert(check_atlas_name(name)); if (auto it = anim_atlas_map.find(name); it != anim_atlas_map.end()) return it->second; @@ -47,7 +48,7 @@ std::shared_ptr loader_impl::anim_atlas(StringView name, StringView auto it = std::find_if(anim_info.groups.cbegin(), anim_info.groups.cend(), [&](const anim_group& x) { return x.name == group.mirror_from; }); if (it == anim_info.groups.cend()) - fm_abort("can't find group '%s' to mirror from '%s'", group.mirror_from.data(), group.name.data()); + fm_throw("can't find group '{}' to mirror from '{}'"_cf, group.mirror_from.data(), group.name.data()); group.frames = it->frames; for (anim_frame& f : group.frames) f.ground = Vector2i((Int)f.size[0] - f.ground[0], f.ground[1]); @@ -56,14 +57,14 @@ std::shared_ptr loader_impl::anim_atlas(StringView name, StringView auto tex = texture("", path); - fm_assert(!anim_info.object_name.isEmpty()); - fm_assert(anim_info.pixel_size.product() > 0); - fm_assert(!anim_info.groups.empty()); - fm_assert(anim_info.nframes > 0); - fm_assert(anim_info.nframes == 1 || anim_info.fps > 0); + fm_soft_assert(!anim_info.object_name.isEmpty()); + fm_soft_assert(anim_info.pixel_size.product() > 0); + fm_soft_assert(!anim_info.groups.empty()); + fm_soft_assert(anim_info.nframes > 0); + fm_soft_assert(anim_info.nframes == 1 || anim_info.fps > 0); const auto size = tex.pixels().size(); const auto width = size[1], height = size[0]; - fm_assert(Vector2uz{anim_info.pixel_size} == Vector2uz{width, height}); + fm_soft_assert(Vector2uz{anim_info.pixel_size} == Vector2uz{width, height}); auto atlas = std::make_shared(path, tex, std::move(anim_info)); return anim_atlas_map[atlas->name()] = atlas; diff --git a/loader/impl.cpp b/loader/impl.cpp index 45c5b055..ba055464 100644 --- a/loader/impl.cpp +++ b/loader/impl.cpp @@ -12,7 +12,7 @@ namespace floormat::loader_detail { -StringView loader_impl::shader(StringView filename) +StringView loader_impl::shader(StringView filename) noexcept { if (!shader_res) shader_res = Optional(InPlaceInit, "floormat/shaders"); diff --git a/loader/impl.hpp b/loader/impl.hpp index 2032c095..c50cb1d7 100644 --- a/loader/impl.hpp +++ b/loader/impl.hpp @@ -28,13 +28,13 @@ struct loader_impl final : loader_ std::vector sceneries_array; std::unordered_map sceneries_map; - StringView shader(StringView filename) override; - Trade::ImageData2D texture(StringView prefix, StringView filename); - std::shared_ptr tile_atlas(StringView filename, Vector2ub size) override; + StringView shader(StringView filename) noexcept override; + Trade::ImageData2D texture(StringView prefix, StringView filename) noexcept(false); + std::shared_ptr tile_atlas(StringView filename, Vector2ub size) noexcept(false) override; ArrayView anim_atlas_list() override; - std::shared_ptr anim_atlas(StringView name, StringView dir) override; + std::shared_ptr anim_atlas(StringView name, StringView dir) noexcept(false) override; const std::vector& sceneries() override; - const scenery_proto& scenery(StringView name) override; + const scenery_proto& scenery(StringView name) noexcept(false) override; void get_anim_atlas_list(); void get_scenery_list(); diff --git a/loader/json.cpp b/loader/json.cpp index aec7c7b8..77da5905 100644 --- a/loader/json.cpp +++ b/loader/json.cpp @@ -1,10 +1,12 @@ #include "impl.hpp" #include "compat/assert.hpp" +#include "compat/exception.hpp" #include "serialize/json-helper.hpp" #include "serialize/anim.hpp" #include "serialize/tile-atlas.hpp" #include "serialize/scenery.hpp" #include "loader/scenery.hpp" +#include #include namespace floormat::loader_detail { @@ -35,13 +37,13 @@ const std::vector& loader_impl::sceneries() return sceneries_array; } -const scenery_proto& loader_impl::scenery(StringView name) +const scenery_proto& loader_impl::scenery(StringView name) noexcept(false) { if (sceneries_array.empty()) get_scenery_list(); auto it = sceneries_map.find(name); if (it == sceneries_map.end()) - fm_abort("no such scenery: '%s'", name.data()); + fm_throw("no such scenery: '{}'"_cf, name); return it->second->proto; } diff --git a/loader/loader.hpp b/loader/loader.hpp index 458bf34d..8883ed8a 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -14,15 +14,15 @@ struct scenery_proto; struct loader_ { - virtual StringView shader(StringView filename) = 0; - virtual std::shared_ptr tile_atlas(StringView filename, Vector2ub size) = 0; + virtual StringView shader(StringView filename) noexcept = 0; + virtual std::shared_ptr tile_atlas(StringView filename, Vector2ub size) noexcept(false) = 0; virtual ArrayView anim_atlas_list() = 0; - virtual std::shared_ptr anim_atlas(StringView name, StringView dir = ANIM_PATH) = 0; + virtual std::shared_ptr anim_atlas(StringView name, StringView dir = ANIM_PATH) noexcept(false) = 0; static void destroy(); static loader_& default_loader() noexcept; static std::vector> tile_atlases(StringView filename); virtual const std::vector& sceneries() = 0; - virtual const scenery_proto& scenery(StringView name) = 0; + virtual const scenery_proto& scenery(StringView name) noexcept(false) = 0; loader_(const loader_&) = delete; loader_& operator=(const loader_&) = delete; diff --git a/loader/texture.cpp b/loader/texture.cpp index e9d1f33e..42940af2 100644 --- a/loader/texture.cpp +++ b/loader/texture.cpp @@ -1,25 +1,27 @@ #include "impl.hpp" #include "compat/assert.hpp" +#include "compat/exception.hpp" #include "compat/defs.hpp" #include "compat/alloca.hpp" #include +#include #include #include namespace floormat::loader_detail { fm_noinline -Trade::ImageData2D loader_impl::texture(StringView prefix, StringView filename_) +Trade::ImageData2D loader_impl::texture(StringView prefix, StringView filename_) noexcept(false) { ensure_plugins(); const auto N = prefix.size(); if (N > 0) fm_assert(prefix[N-1] == '/'); - fm_assert(filename_.size() < 4096); - fm_assert(filename_.find('\\') == filename_.end()); - fm_assert(filename_.find('\0') == filename_.end()); - fm_assert(tga_importer); + fm_soft_assert(filename_.size() < 4096); + fm_soft_assert(filename_.find('\\') == filename_.end()); + fm_soft_assert(filename_.find('\0') == filename_.end()); + fm_soft_assert(tga_importer); constexpr std::size_t max_extension_length = 16; char* const filename = (char*)alloca(filename_.size() + N + 1 + max_extension_length); @@ -44,7 +46,7 @@ Trade::ImageData2D loader_impl::texture(StringView prefix, StringView filename_) } const auto path = Path::currentDirectory(); filename[len] = '\0'; - fm_abort("can't open image '%s' (cwd '%s')", filename, path ? path->data() : "(null)"); + fm_throw("can't open image '{}' (cwd '{}')"_cf, filename, path ? StringView{*path} : "(null)"_s); } } // namespace floormat::loader_detail -- cgit v1.2.3