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
|
/*
*
* (C) 2003-2025 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
#include "services.h"
#include "anope.h"
#include "serialize.h"
#include "modules.h"
#include "account.h"
#include "bots.h"
#include "regchannel.h"
#include "xline.h"
#include "access.h"
using namespace Serialize;
std::vector<Anope::string> Type::TypeOrder;
std::map<Anope::string, Type *> Serialize::Type::Types;
std::list<Serializable *> *Serializable::SerializableItems;
void Serialize::RegisterTypes()
{
static Type nc("NickCore", NickCore::Unserialize), na("NickAlias", NickAlias::Unserialize), bi("BotInfo", BotInfo::Unserialize),
ci("ChannelInfo", ChannelInfo::Unserialize), access("ChanAccess", ChanAccess::Unserialize),
akick("AutoKick", AutoKick::Unserialize), memo("Memo", Memo::Unserialize), xline("XLine", XLine::Unserialize);
}
void Serialize::CheckTypes()
{
for (const auto &[_, t] : Serialize::Type::GetTypes())
{
t->Check();
}
}
Serializable::Serializable(const Anope::string &serialize_type)
{
if (SerializableItems == NULL)
SerializableItems = new std::list<Serializable *>();
SerializableItems->push_back(this);
this->s_type = Type::Find(serialize_type);
this->s_iter = SerializableItems->end();
--this->s_iter;
FOREACH_MOD(OnSerializableConstruct, (this));
}
Serializable::Serializable(const Serializable &other)
{
SerializableItems->push_back(this);
this->s_iter = SerializableItems->end();
--this->s_iter;
this->s_type = other.s_type;
FOREACH_MOD(OnSerializableConstruct, (this));
}
Serializable::~Serializable()
{
FOREACH_MOD(OnSerializableDestruct, (this));
SerializableItems->erase(this->s_iter);
}
Serializable &Serializable::operator=(const Serializable &)
{
return *this;
}
void Serializable::QueueUpdate()
{
/* Schedule updater */
FOREACH_MOD(OnSerializableUpdate, (this));
/* Check for modifications now - this can delete this object! */
FOREACH_MOD(OnSerializeCheck, (this->GetSerializableType()));
}
bool Serializable::IsCached(Serialize::Data &data)
{
return this->last_commit == data.Hash();
}
void Serializable::UpdateCache(Serialize::Data &data)
{
this->last_commit = data.Hash();
}
bool Serializable::IsTSCached()
{
return this->last_commit_time == Anope::CurTime;
}
void Serializable::UpdateTS()
{
this->last_commit_time = Anope::CurTime;
}
const std::list<Serializable *> &Serializable::GetItems()
{
return *SerializableItems;
}
Serialize::DataType Serialize::Data::GetType(const Anope::string &key) const
{
auto it = this->types.find(key);
if (it != this->types.end())
return it->second;
return Serialize::DataType::TEXT;
}
void Serialize::Data::SetType(const Anope::string &key, Serialize::DataType dt)
{
this->types[key] = dt;
}
Type::Type(const Anope::string &n, unserialize_func f, Module *o) : name(n), unserialize(f), owner(o)
{
TypeOrder.push_back(this->name);
Types[this->name] = this;
FOREACH_MOD(OnSerializeTypeCreate, (this));
}
Type::~Type()
{
/* null the type of existing serializable objects of this type */
if (Serializable::SerializableItems != NULL)
{
for (auto *s : *Serializable::SerializableItems)
{
if (s->s_type == this)
s->s_type = NULL;
}
}
std::vector<Anope::string>::iterator it = std::find(TypeOrder.begin(), TypeOrder.end(), this->name);
if (it != TypeOrder.end())
TypeOrder.erase(it);
Types.erase(this->name);
}
Serializable *Type::Unserialize(Serializable *obj, Serialize::Data &data)
{
return this->unserialize(obj, data);
}
void Type::Check()
{
FOREACH_MOD(OnSerializeCheck, (this));
}
time_t Type::GetTimestamp() const
{
return this->timestamp;
}
void Type::UpdateTimestamp()
{
this->timestamp = Anope::CurTime;
}
Type *Serialize::Type::Find(const Anope::string &name)
{
std::map<Anope::string, Type *>::iterator it = Types.find(name);
if (it != Types.end())
return it->second;
return NULL;
}
const std::vector<Anope::string> &Type::GetTypeOrder()
{
return TypeOrder;
}
const std::map<Anope::string, Serialize::Type *>& Type::GetTypes()
{
return Types;
}
|