summaryrefslogtreecommitdiffhomepage
path: root/loader/json.cpp
blob: aec7c7b880d0bf37e83c675dbdc6866fed430f2a (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
#include "impl.hpp"
#include "compat/assert.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/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;
    }
}

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)
{
    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());
    return it->second->proto;
}

} // namespace floormat::loader_detail

namespace floormat {

std::vector<std::shared_ptr<struct tile_atlas>> loader_::tile_atlases(StringView filename)
{
    return json_helper::from_json<std::vector<std::shared_ptr<struct tile_atlas>>>(Path::join(loader_::IMAGE_PATH, filename));
}

} // namespace floormat