@@ -38,7 +38,7 @@ static JsonObject attributesToJson(const AudioResourceAttributes& attributes)
38
38
JsonObject result;
39
39
40
40
for (auto it = attributes.cbegin (); it != attributes.cend (); ++it) {
41
- if (it->second == audio::PLAYBACK_SETUP_DATA_ATTRIBUTE) {
41
+ if (it->first == audio::PLAYBACK_SETUP_DATA_ATTRIBUTE) {
42
42
continue ;
43
43
}
44
44
@@ -54,10 +54,17 @@ static JsonObject metaToJson(const AudioResourceMeta& meta)
54
54
55
55
result.set (" id" , meta.id );
56
56
result.set (" type" , mu::value (RESOURCE_TYPE_TO_STRING_MAP, meta.type , " Undefined" ));
57
- result.set (" vendor" , meta.vendor );
58
- result.set (" attributes" , attributesToJson (meta.attributes ));
59
57
result.set (" hasNativeEditorSupport" , meta.hasNativeEditorSupport );
60
58
59
+ if (!meta.vendor .empty ()) {
60
+ result.set (" vendor" , meta.vendor );
61
+ }
62
+
63
+ JsonObject attributesJson = attributesToJson (meta.attributes );
64
+ if (!attributesJson.empty ()) {
65
+ result.set (" attributes" , attributesJson);
66
+ }
67
+
61
68
return result;
62
69
}
63
70
@@ -79,9 +86,13 @@ static AudioResourceMeta metaFromJson(const JsonObject& object)
79
86
result.id = object.value (" id" ).toStdString ();
80
87
result.type = mu::key (RESOURCE_TYPE_TO_STRING_MAP, object.value (" type" ).toStdString ());
81
88
result.vendor = object.value (" vendor" ).toStdString ();
82
- result.attributes = attributesFromJson (object.value (" attributes" ).toObject ());
83
89
result.hasNativeEditorSupport = object.value (" hasNativeEditorSupport" ).toBool ();
84
90
91
+ JsonValue attributes = object.value (" attributes" );
92
+ if (attributes.isObject ()) {
93
+ result.attributes = attributesFromJson (attributes.toObject ());
94
+ }
95
+
85
96
return result;
86
97
}
87
98
}
@@ -90,50 +101,45 @@ mu::Ret KnownAudioPluginsRegister::load()
90
101
{
91
102
TRACEFUNC;
92
103
93
- io::path_t knownAudioPluginsDir = configuration ()->knownAudioPluginsDir ();
104
+ m_loaded = false ;
105
+ m_pluginInfoMap.clear ();
106
+ m_pluginPaths.clear ();
94
107
95
- if (!fileSystem ()->exists (knownAudioPluginsDir)) {
96
- fileSystem ()->makePath (knownAudioPluginsDir);
108
+ io::path_t knownAudioPluginsPath = configuration ()->knownAudioPluginsFilePath ();
109
+ if (!fileSystem ()->exists (knownAudioPluginsPath)) {
110
+ m_loaded = true ;
111
+ return make_ok ();
97
112
}
98
113
99
- RetVal<io::paths_t > paths = fileSystem ()->scanFiles (knownAudioPluginsDir,
100
- { " *.json" },
101
- io::ScanMode::FilesInCurrentDir);
102
- if (!paths.ret ) {
103
- return paths.ret ;
114
+ RetVal<ByteArray> file = fileSystem ()->readFile (knownAudioPluginsPath);
115
+ if (!file.ret ) {
116
+ return file.ret ;
104
117
}
105
118
106
- m_pluginInfoMap.clear ();
107
- m_pluginPaths.clear ();
108
-
109
- for (const io::path_t & infoPath : paths.val ) {
110
- RetVal<ByteArray> file = fileSystem ()->readFile (infoPath);
111
- if (!file.ret ) {
112
- LOGE () << file.ret .toString ();
113
- continue ;
114
- }
119
+ std::string err;
120
+ JsonDocument json = JsonDocument::fromJson (file.val , &err);
121
+ if (!err.empty ()) {
122
+ return Ret (static_cast <int >(Ret::Code::UnknownError), err);
123
+ }
115
124
116
- std::string err;
117
- JsonDocument json = JsonDocument::fromJson (file.val , &err);
118
- if (!err.empty ()) {
119
- LOGE () << err;
120
- continue ;
121
- }
125
+ JsonArray array = json.rootArray ();
122
126
123
- JsonObject object = json.rootObject ();
127
+ for (size_t i = 0 ; i < array.size (); ++i) {
128
+ JsonObject object = array.at (i).toObject ();
124
129
125
130
AudioPluginInfo info;
126
131
info.meta = metaFromJson (object.value (" meta" ).toObject ());
127
- info.meta .attributes .insert ({ audio::PLAYBACK_SETUP_DATA_ATTRIBUTE, mpe::GENERIC_SETUP_DATA_STRING } );
128
- info.type = audioPluginTypeFromCategoriesString (info.meta .attributeVal (audio::CATEGORIES_ATTRIBUTE). toStdString () );
132
+ info.meta .attributes .emplace ( audio::PLAYBACK_SETUP_DATA_ATTRIBUTE, mpe::GENERIC_SETUP_DATA_STRING);
133
+ info.type = audioPluginTypeFromCategoriesString (info.meta .attributeVal (audio::CATEGORIES_ATTRIBUTE));
129
134
info.path = object.value (" path" ).toString ();
130
135
info.enabled = object.value (" enabled" ).toBool ();
131
136
info.errorCode = object.value (" errorCode" ).toInt ();
132
137
133
- m_pluginInfoMap[info.meta .id ] = info;
134
138
m_pluginPaths.insert (info.path );
139
+ m_pluginInfoMap.emplace (info.meta .id , std::move (info));
135
140
}
136
141
142
+ m_loaded = true ;
137
143
return make_ok ();
138
144
}
139
145
@@ -177,60 +183,69 @@ bool KnownAudioPluginsRegister::exists(const AudioResourceId& resourceId) const
177
183
178
184
mu::Ret KnownAudioPluginsRegister::registerPlugin (const AudioPluginInfo& info)
179
185
{
180
- TRACEFUNC;
181
-
182
- JsonObject obj;
183
- obj.set (" meta" , metaToJson (info.meta ));
184
- obj.set (" path" , info.path .toStdString ());
185
- obj.set (" enabled" , info.enabled );
186
-
187
- if (info.errorCode != 0 ) {
188
- obj.set (" errorCode" , info.errorCode );
186
+ IF_ASSERT_FAILED (m_loaded) {
187
+ return false ;
189
188
}
190
189
191
- io::path_t path = pluginInfoPath (info.meta .vendor , info.meta .id );
192
- Ret ret = fileSystem ()->writeFile (path, JsonDocument (obj).toJson ());
193
- if (!ret) {
194
- return ret;
190
+ auto it = m_pluginInfoMap.find (info.meta .id );
191
+ if (it != m_pluginInfoMap.end ()) {
192
+ IF_ASSERT_FAILED (it->second .path != info.path ) {
193
+ return false ;
194
+ }
195
195
}
196
196
197
- m_pluginInfoMap[ info.meta .id ] = info;
197
+ m_pluginInfoMap. emplace ( info.meta .id , info) ;
198
198
m_pluginPaths.insert (info.path );
199
199
200
- return make_ok ();
200
+ Ret ret = writePluginsInfo ();
201
+ return ret;
201
202
}
202
203
203
204
mu::Ret KnownAudioPluginsRegister::unregisterPlugin (const AudioResourceId& resourceId)
204
205
{
205
- TRACEFUNC;
206
+ IF_ASSERT_FAILED (m_loaded) {
207
+ return false ;
208
+ }
206
209
207
210
if (!exists (resourceId)) {
208
211
return make_ok ();
209
212
}
210
213
211
- AudioPluginInfo info = m_pluginInfoMap[resourceId];
212
- io::path_t path = pluginInfoPath (info.meta .vendor , resourceId);
213
-
214
- Ret ret = fileSystem ()->remove (path);
215
- if (!ret) {
216
- return ret;
214
+ for (const auto & pair : m_pluginInfoMap) {
215
+ if (pair.first == resourceId) {
216
+ mu::remove (m_pluginPaths, pair.second .path );
217
+ }
217
218
}
218
219
219
- mu::remove (m_pluginInfoMap, resourceId);
220
- mu::remove (m_pluginPaths, info.path );
220
+ m_pluginInfoMap.erase (resourceId);
221
221
222
- return make_ok ();
222
+ Ret ret = writePluginsInfo ();
223
+ return ret;
223
224
}
224
225
225
- mu::io:: path_t KnownAudioPluginsRegister::pluginInfoPath ( const AudioResourceVendor& vendor, const AudioResourceId& resourceId) const
226
+ mu::Ret KnownAudioPluginsRegister::writePluginsInfo ()
226
227
{
227
- io::path_t fileName;
228
+ TRACEFUNC;
229
+
230
+ JsonArray array;
231
+
232
+ for (const auto & pair : m_pluginInfoMap) {
233
+ const AudioPluginInfo& info = pair.second ;
228
234
229
- if (vendor.empty ()) {
230
- fileName = io::escapeFileName (resourceId);
231
- } else {
232
- fileName = io::escapeFileName (vendor + " _" + resourceId);
235
+ JsonObject obj;
236
+ obj.set (" meta" , metaToJson (info.meta ));
237
+ obj.set (" path" , info.path .toStdString ());
238
+ obj.set (" enabled" , info.enabled );
239
+
240
+ if (info.errorCode != 0 ) {
241
+ obj.set (" errorCode" , info.errorCode );
242
+ }
243
+
244
+ array << obj;
233
245
}
234
246
235
- return configuration ()->knownAudioPluginsDir () + " /" + fileName + " .json" ;
247
+ io::path_t knownAudioPluginsPath = configuration ()->knownAudioPluginsFilePath ();
248
+ Ret ret = fileSystem ()->writeFile (knownAudioPluginsPath, JsonDocument (array).toJson ());
249
+
250
+ return ret;
236
251
}
0 commit comments