summaryrefslogtreecommitdiffhomepage
path: root/anim/serialize.cpp
blob: 7e6d738aeb5a7866812c1c7bb7380d789baab610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "serialize.hpp"
#include "../json.hpp"

#include <algorithm>
#include <utility>
#include <fstream>
#include <exception>
#include <Corrade/Utility/Debug.h>
#include <Corrade/Utility/DebugStl.h>

using Corrade::Utility::Error;

namespace nlohmann {

template<>
struct adl_serializer<Magnum::Vector2i> final {
    static void to_json(json& j, const Magnum::Vector2i& x);
    static void from_json(const json& j, Magnum::Vector2i& x);
};

void adl_serializer<Magnum::Vector2i>::to_json(json& j, const Magnum::Vector2i& val)
{
    char buf[64];
    snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]);
    j = buf;
}

void adl_serializer<Magnum::Vector2i>::from_json(const json& j, Magnum::Vector2i& val)
{
    std::string str = j;
    int x = 0, y = 0, n = 0;
    int ret = std::sscanf(str.c_str(), "%d x %d%n", &x, &y, &n);
    if (ret != 2 || (std::size_t)n != str.size())
    {
        std::string msg; msg.reserve(64 + str.size());
        msg += "failed to parse string '";
        msg += str;
        msg += "' as Magnum::Vector2i";
        throw std::invalid_argument(msg);
    }
    val = { x, y };
}

} // namespace nlohmann

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_frame, ground, offset, size);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim_group, name, frames, ground);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups);

std::tuple<anim, bool> anim::from_json(const std::filesystem::path& pathname)
{
    using namespace nlohmann;
    std::ifstream s;
    s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
    try {
        s.open(pathname, std::ios_base::in);
    } catch (const std::ios::failure& e) {
        Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "':" << e.what();
        return { {}, false };
    }
    anim ret;
    try {
        json j;
        s >> j;
        using nlohmann::from_json;
        from_json(j, ret);
    } catch (const std::exception& e) {
        Error{Error::Flag::NoSpace} << "failed to parse '" << pathname << "':" << e.what();
        return { {}, false };
    }
    return { std::move(ret), true };
}

bool anim::to_json(const std::filesystem::path& pathname)
{
    nlohmann::json j = *this;

    std::ofstream s;
    s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit);
    try {
        s.open(pathname, std::ios_base::out | std::ios_base::trunc);
    } catch (const std::ios::failure& e) {
        Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what();
        return false;
    }
    try {
        s << j.dump(4);
        s.flush();
    } catch (const std::exception& e) {
        Error{Error::Flag::NoSpace} << "failed writing '" << pathname << "' :" << e.what();
        return false;
    }

    return true;
}