-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPluginCache.as
More file actions
240 lines (198 loc) · 5.2 KB
/
PluginCache.as
File metadata and controls
240 lines (198 loc) · 5.2 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
namespace PluginCache
{
const int CurrentVersion = 1;
class @Info
{
string m_name;
int m_siteID = 0;
Version m_version;
Info() {}
Info(Json::Value@ js)
{
m_name = js.Get("name", "");
m_siteID = js.Get("siteid", 0);
m_version = Version(js.Get("version", ""));
}
Json::Value@ Serialize()
{
auto ret = Json::Object();
ret["name"] = m_name;
ret["siteid"] = m_siteID;
ret["version"] = m_version.ToString();
return ret;
}
}
dictionary g_infos;
bool g_dirty = false;
string GetPath()
{
return IO::FromStorageFolder("PluginCache.json");
}
void Initialize()
{
Load();
startnew(Loop);
}
void Loop()
{
while (true) {
yield();
if (g_dirty) {
g_dirty = false;
Save();
}
}
}
void Save()
{
auto pathInstalled = GetPath();
auto js = Json::Object();
js["version"] = 1;
auto jsInstalled = Json::Object();
auto arrInfosKeys = g_infos.GetKeys();
for (uint i = 0; i < arrInfosKeys.Length; i++) {
string key = arrInfosKeys[i];
auto info = cast<Info>(g_infos[key]);
jsInstalled[key] = info.Serialize();
}
js["installed"] = jsInstalled;
Json::ToFile(pathInstalled, js, true);
if (Setting_VerboseLog) {
trace("Saved PluginCache.json");
}
}
void Load()
{
g_infos.DeleteAll();
auto pathInstalled = GetPath();
if (!IO::FileExists(pathInstalled)) {
return;
}
auto js = Json::FromFile(pathInstalled);
if (js is null) {
return;
}
if (js.GetType() != Json::Type::Object) {
error("PluginCache.json: Root is not an object!");
return;
}
auto jsVersion = js.Get("version");
if (jsVersion is null || jsVersion.GetType() != Json::Type::Number) {
error("PluginCache.json: \"version\" is not a number!");
return;
}
if (int(jsVersion) > CurrentVersion) {
error("PluginCache.json: Version is too new! (" + int(jsVersion) + ", we only support up to " + CurrentVersion + ")");
return;
}
auto jsInstalled = js.Get("installed");
if (jsInstalled is null || jsInstalled.GetType() != Json::Type::Object) {
error("PluginCache.json: \"installed\" is not an object!");
return;
}
auto installedKeys = jsInstalled.GetKeys();
for (uint i = 0; i < installedKeys.Length; i++) {
string key = installedKeys[i];
auto jsItem = jsInstalled.Get(key);
auto newItem = Info(jsItem);
g_infos.Set(key, @newItem);
}
if (Setting_VerboseLog) {
trace("Loaded PluginCache.json");
}
}
Info@ GetInfo(const string &in id)
{
Info@ ret = null;
g_infos.Get(id, @ret);
return ret;
}
void Sync(Meta::Plugin@ plugin)
{
Info@ info;
if (!g_infos.Get(plugin.ID, @info)) {
@info = Info();
g_infos.Set(plugin.ID, @info);
}
info.m_name = plugin.Name;
info.m_siteID = plugin.SiteID;
info.m_version = Version(plugin.Version);
if (Setting_VerboseLog) {
trace("Plugin cache: Synchronized " + plugin.ID + " " + plugin.Version);
}
// Mark the cache as dirty
g_dirty = true;
}
void SyncUnloaded(const string &in identifier, int siteID, const Version &in version)
{
Info@ info;
if (!g_infos.Get(identifier, @info)) {
@info = Info();
g_infos.Set(identifier, @info);
}
info.m_name = identifier;
info.m_siteID = siteID;
info.m_version = version;
if (Setting_VerboseLog) {
trace("Plugin cache: Synchronized unloaded " + identifier + " " + version.ToString());
}
// Mark the cache as dirty
g_dirty = true;
}
void SyncRemove(const string &in identifier)
{
g_infos.Delete(identifier);
if (Setting_VerboseLog) {
trace("Plugin cache: Removed " + identifier);
}
// Mark the cache as dirty
g_dirty = true;
}
void Sync()
{
// First we go through every plugin in the cache to see if we removed or uninstalled any
auto arrKeys = g_infos.GetKeys();
for (uint i = 0; i < arrKeys.Length; i++) {
string identifier = arrKeys[i];
auto info = cast<Info>(g_infos[identifier]);
// Check if the plugin is currently loaded
auto loadedPlugin = Meta::GetPluginFromSiteID(info.m_siteID);
if (loadedPlugin !is null) {
continue;
}
// The plugin is not loaded right now, check if it's unloaded
if (IO::FileExists(IO::FromDataFolder("Plugins/" + identifier + ".op"))) {
continue;
}
// The plugin is not unloaded either, so it has been removed entirely
g_infos.Delete(identifier);
warn("Plugin cache: Automatically removed " + identifier);
}
// Go through all of our loaded plugins and update the plugin cache with its current state
auto loadedPlugins = Meta::AllPlugins();
for (uint i = 0; i < loadedPlugins.Length; i++) {
auto plugin = loadedPlugins[i];
// We can only manage packaged plugins
if (plugin.Type == Meta::PluginType::Zip) {
Sync(plugin);
}
}
// Go through all of our unloaded plugins
auto unloadedPlugins = Meta::UnloadedPlugins();
for (uint i = 0; i < unloadedPlugins.Length; i++) {
auto upi = unloadedPlugins[i];
// We can only manage packaged plugins
if (upi.Type != Meta::PluginType::Zip) {
continue;
}
// If we don't already have this plugin in the cache, we add it and assume an empty version
if (!g_infos.Exists(upi.ID)) {
auto newItem = Info();
newItem.m_name = upi.ID;
g_infos.Set(upi.ID, @newItem);
}
}
// Mark the cache as dirty
g_dirty = true;
}
}