summaryrefslogtreecommitdiffhomepage
path: root/loader/json.cpp
blob: 007bafeb839942e7312d25d4bff8a6017f56470a (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
#include "impl.hpp"
#include "compat/assert.hpp"
#include "compat/exception.hpp"
#include "src/tile-atlas.hpp"
#include "serialize/json-helper.hpp"
#include "serialize/anim.hpp"
#include "serialize/tile-atlas.hpp"
#include "serialize/scenery.hpp"
#include "loader/scenery.hpp"
#include <Corrade/Containers/StringStlView.h>
#include <Corrade/Utility/Path.h>

namespace floormat::loader_detail {

anim_def loader_impl::deserialize_anim(StringView filename)
{
    return json_helper::from_json<anim_def>(filename);
}

void loader_impl::get_scenery_list()
{
    sceneries_array.clear();
    sceneries_array = json_helper::from_json<std::vector<serialized_scenery>>(Path::join(SCENERY_PATH, "scenery.json"));
    sceneries_map.clear();
    sceneries_map.reserve(sceneries_array.size() * 2);
    for (const serialized_scenery& s : sceneries_array)
    {
        if (sceneries_map.contains(s.name))
            fm_abort("duplicate scenery name '%s'", s.name.data());
        sceneries_map[s.name] = &s;
    }
    fm_assert(!sceneries_map.empty());
}

const std::vector<serialized_scenery>& loader_impl::sceneries()
{
    if (sceneries_array.empty())
        get_scenery_list();
    return sceneries_array;
}

const scenery_proto& loader_impl::scenery(StringView name) noexcept(false)
{
    fm_soft_assert(check_atlas_name(name));
    if (sceneries_array.empty())
        get_scenery_list();
    auto it = sceneries_map.find(name);
    if (it == sceneries_map.end())
        fm_throw("no such scenery: '{}'"_cf, name);
    return it->second->proto;
}

} // namespace floormat::loader_detail

namespace floormat {

std::vector<std::shared_ptr<class tile_atlas>> loader_::tile_atlases(StringView filename, pass_mode p)
{
    auto vec = json_helper::from_json<std::vector<std::shared_ptr<class tile_atlas>>>(
        Path::join(loader_::IMAGE_PATH, filename));
    for (auto& x : vec)
        if (!x->pass_mode())
            x->set_pass_mode(p);
    return vec;
}

} // namespace floormat