Skip to content

Commit 0056945

Browse files
committed
LIBRETRO: use Common JSON classes for playlist generation
1 parent c69ba24 commit 0056945

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

backends/platform/libretro/src/libretro-options-widget.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include "backends/platform/libretro/include/libretro-fs.h"
2222
#include "backends/platform/libretro/include/libretro-os.h"
2323
#include "gui/launcher.h"
24-
2524
#include "common/translation.h"
25+
#include "common/formats/json.h"
2626

2727
enum {
2828
kPlaylistPathCmd = 'pchp',
@@ -142,6 +142,9 @@ bool LibretroOptionsWidget::generatePlaylist(Common::String playlistPath) {
142142
Common::String hookFilePath;
143143
Common::String title;
144144

145+
Common::JSONObject root;
146+
Common::JSONArray items;
147+
145148
/* Create playlist file */
146149
RFILE *playlistFile = filestream_open(Common::String(playlistPath + "/" + CORE_NAME + ".lpl").c_str(), RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE);
147150
if (!playlistFile) {
@@ -166,9 +169,14 @@ bool LibretroOptionsWidget::generatePlaylist(Common::String playlistPath) {
166169
LibRetroFilesystemNode corePath(".");
167170
if (cCorePath)
168171
corePath = LibRetroFilesystemNode(Common::String(cCorePath));
169-
filestream_printf(playlistFile, "{\n \"version\": \"1.5\",\n \"default_core_path\": \"%s\",\n \"default_core_name\": \"ScummVM\",\n \"label_display_mode\": 0,\n \"right_thumbnail_mode\": 2,\n \"left_thumbnail_mode\": 3,\n \"sort_mode\": 0,\n \"items\": [", corePath.exists() ? corePath.getPath().c_str() : "");
170-
playlistElement = "%s\n {\n \"path\": \"%s\",\n \"label\": \"%s\",\n \"core_path\": \"DETECT\",\n \"core_name\": \"DETECT\",\n \"crc32\": \"DETECT\",\n \"db_name\": \"" CORE_NAME ".lpl\"\n }";
171-
playlistFooter = "\n ]\n}";
172+
173+
root["version"] = new Common::JSONValue(Common::String("1.5"));
174+
root["default_core_path"] = new Common::JSONValue(corePath.exists() ? corePath.getPath() : Common::String(""));
175+
root["default_core_name"] = new Common::JSONValue(Common::String("ScummVM"));
176+
root["label_display_mode"] = new Common::JSONValue((long long)0);
177+
root["right_thumbnail_mode"] = new Common::JSONValue((long long)2);
178+
root["left_thumbnail_mode"] = new Common::JSONValue((long long)3);
179+
root["sort_mode"] = new Common::JSONValue((long long)0);
172180
} else
173181
playlistElement = "%s%s\n%s\nDETECT\nDETECT\nDETECT\nScummVM.lpl\n";
174182

@@ -193,11 +201,22 @@ bool LibretroOptionsWidget::generatePlaylist(Common::String playlistPath) {
193201
iter->_value.tryGetVal("description", title);
194202
hookFilePath = hookPath + + "/" + iter->_key.c_str() + "." + CORE_EXTENSIONS;
195203

196-
filestream_printf(playlistFile, playlistElement.c_str(), separator, hookFilePath.c_str(), title.c_str());
197-
198-
if (isFirstEntry && ConfMan.getInt("libretro_playlist_version", _domain) != kPlaylistFormat6lines) {
199-
*separator = ',';
200-
isFirstEntry = false;
204+
if (ConfMan.getInt("libretro_playlist_version", _domain) != kPlaylistFormat6lines) {
205+
Common::JSONObject item;
206+
item["path"] = new Common::JSONValue(hookFilePath);
207+
item["label"] = new Common::JSONValue(title);
208+
item["core_path"] = new Common::JSONValue(Common::String("DETECT"));
209+
item["core_name"] = new Common::JSONValue(Common::String("DETECT"));
210+
item["crc32"] = new Common::JSONValue(Common::String("DETECT"));
211+
item["db_name"] = new Common::JSONValue(Common::String(CORE_NAME ".lpl"));
212+
213+
items.push_back(new Common::JSONValue(item));
214+
} else {
215+
filestream_printf(playlistFile, playlistElement.c_str(), separator, hookFilePath.c_str(), title.c_str());
216+
if (isFirstEntry) {
217+
*separator = ',';
218+
isFirstEntry = false;
219+
}
201220
}
202221

203222
/* Create hook file */
@@ -214,7 +233,15 @@ bool LibretroOptionsWidget::generatePlaylist(Common::String playlistPath) {
214233
filestream_close(hookFile);
215234
}
216235

217-
filestream_printf(playlistFile, playlistFooter.c_str());
236+
if (ConfMan.getInt("libretro_playlist_version", _domain) != kPlaylistFormat6lines) {
237+
root["items"] = new Common::JSONValue(items);
238+
Common::JSONValue *rootVal = new Common::JSONValue(root);
239+
Common::String out = Common::JSON::stringify(rootVal);
240+
filestream_write(playlistFile, out.c_str(), out.size());
241+
delete rootVal;
242+
} else {
243+
filestream_printf(playlistFile, playlistFooter.c_str());
244+
}
218245
filestream_close(playlistFile);
219246

220247
Common::String response;

0 commit comments

Comments
 (0)