summaryrefslogtreecommitdiffhomepage
path: root/crop-tool/serialize.cpp
blob: acefe4369d2432afd5de238faec8e9050df18cc7 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "serialize.hpp"
#include "../json.hpp"

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

using Corrade::Utility::Debug;
using Corrade::Utility::Error;

static constexpr
std::pair<anim_direction, const char*> anim_direction_map[] = {
    { anim_direction::N,        "n"     },
    { anim_direction::NE,       "ne"    },
    { anim_direction::E,        "e"     },
    { anim_direction::SE,       "se"    },
    { anim_direction::S,        "s"     },
    { anim_direction::SW,       "sw"    },
    { anim_direction::W,        "w"     },
    { anim_direction::NW,       "nw"    },
};

const char* anim_group::direction_to_string(anim_direction group)
{
    auto it = std::find_if(std::cbegin(anim_direction_map), std::cend(anim_direction_map),
                           [=](const auto& pair) { return group == pair.first; });
    if (it != std::cend(anim_direction_map))
        return it->second;
    else
        return "(unknown)";
}

anim_direction anim_group::string_to_direction(const std::string& str)
{
    auto it = std::find_if(std::cbegin(anim_direction_map), std::cend(anim_direction_map),
                           [&](const auto& pair) { return str == pair.second; });
    if (it != std::cend(anim_direction_map))
        return it->first;
    else
        return (anim_direction)0;
}

namespace nlohmann {

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

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& x)
{
    j["x"] = x[0];
    j["y"] = x[1];
}

void adl_serializer<Magnum::Vector2i>::from_json(const json& j, Magnum::Vector2i& x)
{
    j.at("x").get_to(x[0]);
    j.at("y").get_to(x[1]);
}

void adl_serializer<anim_direction>::to_json(json& j, anim_direction x)
{
    j = anim_group::direction_to_string(x);
}

void adl_serializer<anim_direction>::from_json(const json& j, anim_direction& x)
{
    x = anim_group::string_to_direction(j);
}

} // 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::optional<anim> 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{} << "failed to open" << pathname << ':' << e.what();
        return std::nullopt;
    }
    anim ret;
    try {
        json j;
        s >> j;
        using nlohmann::from_json;
        from_json(j, ret);
    } catch (const std::exception& e) {
        Error{} << "failed to parse" << pathname << ':' << e.what();
        return std::nullopt;
    }
    return std::make_optional(std::move(ret));
}

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{} << "failed to open" << pathname << "for writing:" << e.what();
        return false;
    }
    s << j.dump(4);
    s.flush();

    return true;
}