summaryrefslogtreecommitdiff
path: root/components/lua/storage.cpp
blob: 91a339cb0365451cd7e49a06ef2b6e2fa87205ef (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "storage.hpp"

#include <filesystem>
#include <fstream>

#include <components/debug/debuglog.hpp>

namespace sol
{
    template <>
    struct is_automagical<LuaUtil::LuaStorage::SectionMutableView> : std::false_type {};
    template <>
    struct is_automagical<LuaUtil::LuaStorage::SectionReadOnlyView> : std::false_type {};
}

namespace LuaUtil
{
    LuaStorage::Value LuaStorage::Section::sEmpty;

    sol::object LuaStorage::Value::getCopy(lua_State* L) const
    {
        return deserialize(L, mSerializedValue);
    }

    sol::object LuaStorage::Value::getReadOnly(lua_State* L) const
    {
        if (mReadOnlyValue == sol::nil && !mSerializedValue.empty())
            mReadOnlyValue = deserialize(L, mSerializedValue, nullptr, true);
        return mReadOnlyValue;
    }

    const LuaStorage::Value& LuaStorage::Section::get(std::string_view key) const
    {
        auto it = mValues.find(key);
        if (it != mValues.end())
            return it->second;
        else
            return sEmpty;
    }

    void LuaStorage::Section::set(std::string_view key, const sol::object& value)
    {
        mValues[std::string(key)] = Value(value);
        mChangeCounter++;
        if (mStorage->mListener)
            (*mStorage->mListener)(mSectionName, key, value);
    }

    bool LuaStorage::Section::wasChanged(int64_t& lastCheck)
    {
        bool res = lastCheck < mChangeCounter;
        lastCheck = mChangeCounter;
        return res;
    }

    sol::table LuaStorage::Section::asTable()
    {
        sol::table res(mStorage->mLua, sol::create);
        for (const auto& [k, v] : mValues)
            res[k] = v.getCopy(mStorage->mLua);
        return res;
    }

    void LuaStorage::initLuaBindings(lua_State* L)
    {
        sol::state_view lua(L);
        sol::usertype<SectionReadOnlyView> roView = lua.new_usertype<SectionReadOnlyView>("ReadOnlySection");
        sol::usertype<SectionMutableView> mutableView = lua.new_usertype<SectionMutableView>("MutableSection");
        roView["get"] = [](sol::this_state s, SectionReadOnlyView& section, std::string_view key)
        {
            return section.mSection->get(key).getReadOnly(s);
        };
        roView["getCopy"] = [](sol::this_state s, SectionReadOnlyView& section, std::string_view key)
        {
            return section.mSection->get(key).getCopy(s);
        };
        roView["wasChanged"] = [](SectionReadOnlyView& section) { return section.mSection->wasChanged(section.mLastCheck); };
        roView["asTable"] = [](SectionReadOnlyView& section) { return section.mSection->asTable(); };
        mutableView["get"] = [](sol::this_state s, SectionMutableView& section, std::string_view key)
        {
            return section.mSection->get(key).getReadOnly(s);
        };
        mutableView["getCopy"] = [](sol::this_state s, SectionMutableView& section, std::string_view key)
        {
            return section.mSection->get(key).getCopy(s);
        };
        mutableView["wasChanged"] = [](SectionMutableView& section) { return section.mSection->wasChanged(section.mLastCheck); };
        mutableView["asTable"] = [](SectionMutableView& section) { return section.mSection->asTable(); };
        mutableView["reset"] = [](SectionMutableView& section, sol::optional<sol::table> newValues)
        {
            section.mSection->mValues.clear();
            if (newValues)
            {
                for (const auto& [k, v] : *newValues)
                {
                    try
                    {
                        section.mSection->set(k.as<std::string_view>(), v);
                    }
                    catch (std::exception& e)
                    {
                        Log(Debug::Error) << "LuaUtil::LuaStorage::Section::reset(table): " << e.what();
                    }
                }
            }
            section.mSection->mChangeCounter++;
            section.mLastCheck = section.mSection->mChangeCounter;
        };
        mutableView["removeOnExit"] = [](SectionMutableView& section) { section.mSection->mPermanent = false; };
        mutableView["set"] = [](SectionMutableView& section, std::string_view key, const sol::object& value)
        {
            if (section.mLastCheck == section.mSection->mChangeCounter)
                section.mLastCheck++;
            section.mSection->set(key, value);
        };
    }

    void LuaStorage::clearTemporary()
    {
        auto it = mData.begin();
        while (it != mData.end())
        {
            if (!it->second->mPermanent)
            {
                it->second->mValues.clear();
                it = mData.erase(it);
            }
            else
                ++it;
        }
    }

    void LuaStorage::load(const std::string& path)
    {
        assert(mData.empty());  // Shouldn't be used before loading
        try
        {
            Log(Debug::Info) << "Loading Lua storage \"" << path << "\" (" << std::filesystem::file_size(path) << " bytes)";
            std::ifstream fin(path, std::fstream::binary);
            std::string serializedData((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
            sol::table data = deserialize(mLua, serializedData);
            for (const auto& [sectionName, sectionTable] : data)
            {
                const std::shared_ptr<Section>& section = getSection(sectionName.as<std::string_view>());
                for (const auto& [key, value] : sol::table(sectionTable))
                    section->set(key.as<std::string_view>(), value);
            }
        }
        catch (std::exception& e)
        {
            Log(Debug::Error) << "Can not read \"" << path << "\": " << e.what();
        }
    }

    void LuaStorage::save(const std::string& path) const
    {
        sol::table data(mLua, sol::create);
        for (const auto& [sectionName, section] : mData)
        {
            if (section->mPermanent)
                data[sectionName] = section->asTable();
        }
        std::string serializedData = serialize(data);
        Log(Debug::Info) << "Saving Lua storage \"" << path << "\" (" << serializedData.size() << " bytes)";
        std::ofstream fout(path, std::fstream::binary);
        fout.write(serializedData.data(), serializedData.size());
        fout.close();
    }

    const std::shared_ptr<LuaStorage::Section>& LuaStorage::getSection(std::string_view sectionName)
    {
        auto it = mData.find(sectionName);
        if (it != mData.end())
            return it->second;
        auto section = std::make_shared<Section>(this, std::string(sectionName));
        sectionName = section->mSectionName;
        auto [newIt, _] = mData.emplace(sectionName, std::move(section));
        return newIt->second;
    }

    sol::object LuaStorage::getReadOnlySection(std::string_view sectionName)
    {
        const std::shared_ptr<Section>& section = getSection(sectionName);
        return sol::make_object<SectionReadOnlyView>(mLua, SectionReadOnlyView{section, section->mChangeCounter});
    }

    sol::object LuaStorage::getMutableSection(std::string_view sectionName)
    {
        const std::shared_ptr<Section>& section = getSection(sectionName);
        return sol::make_object<SectionMutableView>(mLua, SectionMutableView{section, section->mChangeCounter});
    }

    sol::table LuaStorage::getAllSections()
    {
        sol::table res(mLua, sol::create);
        for (const auto& [sectionName, _] : mData)
            res[sectionName] = getMutableSection(sectionName);
        return res;
    }

}