aboutsummaryrefslogtreecommitdiffstats
path: root/src/gamestate.hpp
blob: 86a419800e28f5e8796a9da6b9c701e39ec3ff07 (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
128
129
130
131
132
/**
 * @file gamestate.hpp
 * Provides functionality to load and save entity data to or from JSON files.
 *
 * Copyright (C) 2019 Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GAMELOAD_HPP_
#define GAMELOAD_HPP_

#include <cereal/cereal.hpp>
#include <cereal/archives/json.hpp>
#include <entityx/entityx.h>

#include <fstream>

struct EntitySerializer
{
    template<class Archive>
    static void serialize(entityx::EntityManager& entities, entityx::Entity entity, bool save, Archive& ar)
    {
        // TODO try and reimplement without modifying entityx?
        // Or, make a new branch...

        //const auto mask = entity.component_mask();

        //for (size_t i = 0; i < entities.component_helpers_.size(); i++) {
        //    auto helper = entities.component_helpers_[i];
        //    if (helper && mask.test(i)) {
        //        auto* c = helper->get_component(entity);
        //        ar.setNextName(c->serializeName().c_str());
        //        ar.startNode();
        //        c->internal_serialize(save, static_cast<void*>(&ar));
        //        ar.finishNode();
        //    }
        //}
    }
};


/**
 * @class GameState
 * Manages save files that contain entity data.
 */
class GameState
{
public:
    /**
     * Loads entity data from a JSON file into entities contained in the
     * entity manager.
     *
     * The order of entities and the components they have must match between
     * those in the manager and those in the file.
     *
     * @param file The file to load from
     * @param entities The entity manager to load into
     * @return True if successful load
     */
    static bool load(const std::string& file, entityx::EntityManager &entities)
    {
        std::ifstream saveFile (file);
        bool opened = saveFile.good();

        if (opened) {
            cereal::JSONInputArchive archive (saveFile);
            serializeEntities(archive, false, entities);
        }

        return opened;
    }

    /**
     * Saves entity data from the entities in the provided manager into a JSON
     * file,
     *
     * @param file The file to load from
     * @param entities The entity manager to get entity data from
     * @return True if successful save
     */
    static bool save(const std::string& file, entityx::EntityManager &entities)
    {
        std::ofstream saveFile (file);
        bool opened = saveFile.good();

        if (opened) {
            cereal::JSONOutputArchive archive (saveFile);
            serializeEntities(archive, true, entities);
        }

        return opened;
    }

private:
    /**
     * Calls the serialization funciton for each entity in the given entity
     * manager.
     * @param archive The archive to save/load with (cereal::JSON...)
     * @param save True to save data, false to load
     * @param entities The entity manager to iterate through
     */
    template<class Archive>
    static void serializeEntities(Archive& archive,
                                  bool save,
                                  entityx::EntityManager& entities)
    {
        std::string name ("entity");
        int i = 0;

        for (auto entity : entities.entities_for_debugging()) {
            archive.setNextName((name + std::to_string(i++)).c_str());
            archive.startNode();
            EntitySerializer::serialize(entities, entity, save, archive);
            archive.finishNode();
        }
    }
};

#endif // GAMELOAD_HPP_