-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathRetroArchPlaylistManager.m
More file actions
340 lines (270 loc) · 12.1 KB
/
Copy pathRetroArchPlaylistManager.m
File metadata and controls
340 lines (270 loc) · 12.1 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// RetroArchPlaylistManager.m
// RetroArch
//
// Unified playlist management for iOS and tvOS
//
#import "RetroArchPlaylistManager.h"
#include "../../../playlist.h"
#include "../../../paths.h"
#include "../../../retroarch.h"
#include "../../../runloop.h"
#include "../../../verbosity.h"
#include "../../../file_path_special.h"
#include "../../../defaults.h"
#include "../../../libretro-common/include/lists/dir_list.h"
#include "../../../libretro-common/include/file/file_path.h"
#include "../../../libretro-common/include/string/stdstring.h"
NS_ASSUME_NONNULL_BEGIN
// Block type for enumerating playlist entries
typedef void (^PlaylistEntryBlock)(const struct playlist_entry *entry, playlist_t *playlist, NSString *playlistName, uint32_t index);
@implementation RetroArchPlaylistGame
@end
@implementation RetroArchPlaylistManager
+ (NSArray<NSString *> * _Nonnull)getPlaylistNames
{
NSMutableArray<NSString *> *playlistNames = [[NSMutableArray alloc] init];
// Get RetroArch's playlist directory
char playlist_dir[PATH_MAX_LENGTH];
settings_t *settings = config_get_ptr();
if (!settings) {
RARCH_LOG("RetroArch not initialized yet, cannot access playlists\n");
return playlistNames;
}
fill_pathname_join_special(playlist_dir,
settings->paths.directory_playlist,
"",
sizeof(playlist_dir));
// Use dir_list to discover actual playlist files (like menu system does)
struct string_list str_list = {0};
if (!dir_list_initialize(&str_list, playlist_dir, NULL, true,
settings->bools.show_hidden_files, true, false)) {
RARCH_LOG("Could not scan playlist directory: %s\n", playlist_dir);
return playlistNames;
}
// Sort playlists (same as menu system)
dir_list_sort_ignore_ext(&str_list, true);
// Process each file, filtering for valid playlists
for (size_t i = 0; i < str_list.size; i++) {
const char *path = str_list.elems[i].data;
const char *playlist_file = path_basename(path);
if (!playlist_file || !*playlist_file)
continue;
/* Only include .lpl files (same logic as
* menu_displaylist_parse_playlists) */
if (!string_is_equal_noncase(path_get_extension(playlist_file), "lpl"))
continue;
// Exclude history and favorites files (same logic as menu system)
if (string_ends_with_size(path, "_history.lpl",
strlen(path), STRLEN_CONST("_history.lpl")) ||
string_is_equal(playlist_file, FILE_PATH_CONTENT_FAVORITES))
continue;
// Add valid playlist to our list
[playlistNames addObject:[NSString stringWithUTF8String:playlist_file]];
}
dir_list_deinitialize(&str_list);
RARCH_LOG("App Intents: Discovered %lu playlists in %s\n",
(unsigned long)playlistNames.count, playlist_dir);
return playlistNames;
}
+ (void)enumerateAllPlaylistEntries:(PlaylistEntryBlock _Nonnull)block
{
// Get RetroArch's playlist directory
char playlist_dir[PATH_MAX_LENGTH];
settings_t *settings = config_get_ptr();
if (!settings) {
RARCH_LOG("RetroArch not initialized yet, cannot enumerate playlists\n");
return;
}
fill_pathname_join_special(playlist_dir,
settings->paths.directory_playlist,
"",
sizeof(playlist_dir));
NSArray<NSString *> *playlistNames = [self getPlaylistNames];
for (NSString *playlistName in playlistNames) {
char playlist_path[PATH_MAX_LENGTH];
fill_pathname_join_special(playlist_path,
playlist_dir,
[playlistName UTF8String],
sizeof(playlist_path));
// Try to load the playlist
playlist_config_t config;
config.capacity = COLLECTION_SIZE;
config.old_format = false;
config.compress = false;
config.fuzzy_archive_match = false;
config.autofix_paths = false;
strlcpy(config.path, playlist_path, sizeof(config.path));
strlcpy(config.base_content_directory, "", sizeof(config.base_content_directory));
playlist_t *playlist = playlist_init(&config);
if (!playlist)
continue;
uint32_t playlist_size = playlist_get_size(playlist);
// Enumerate all entries in this playlist
for (uint32_t i = 0; i < playlist_size; i++) {
const struct playlist_entry *entry = NULL;
playlist_get_index(playlist, i, &entry);
if (entry && entry->path && entry->label) {
block(entry, playlist, playlistName, i);
}
}
playlist_free(playlist);
}
}
+ (NSArray<RetroArchPlaylistGame *> * _Nonnull)getAllGames
{
NSMutableArray<RetroArchPlaylistGame *> *games = [[NSMutableArray alloc] init];
// Check if RetroArch is properly initialized
runloop_state_t *runloop_st = runloop_state_get_ptr();
if (!runloop_st || !(runloop_st->flags & RUNLOOP_FLAG_IS_INITED)) {
RARCH_LOG("RetroArch not fully initialized, cannot access playlists\n");
return games;
}
// Double-check that config is available
settings_t *settings = config_get_ptr();
if (!settings) {
RARCH_LOG("RetroArch configuration not available, cannot access playlists\n");
return games;
}
[self enumerateAllPlaylistEntries:^(const struct playlist_entry *entry, playlist_t *playlist, NSString *playlistName, uint32_t index) {
RetroArchPlaylistGame *game = [[RetroArchPlaylistGame alloc] init];
/* Create a unique ID from path and playlist */
game.gameId = [NSString stringWithFormat:@"%@:%@", playlistName, @(index)];
game.title = [NSString stringWithUTF8String:entry->label];
game.fullPath = [NSString stringWithUTF8String:entry->path];
/* System/playlist display name (strip trailing ".lpl") */
if ([playlistName.pathExtension.lowercaseString isEqualToString:@"lpl"])
game.system = playlistName.stringByDeletingPathExtension;
else
game.system = playlistName;
/* Extract filename from path */
const char *filename = path_basename(entry->path);
game.filename = [NSString stringWithUTF8String:filename];
const char *a = playlist_get_default_core_path(playlist);
const char *b = playlist_get_default_core_name(playlist);
if (entry->core_path && *entry->core_path && !string_is_equal(entry->core_path, FILE_PATH_DETECT))
game.corePath = [NSString stringWithUTF8String:entry->core_path];
else if (a && *a)
game.corePath = [NSString stringWithUTF8String:a];
if (entry->core_name && *entry->core_name && !string_is_equal(entry->core_name, FILE_PATH_DETECT))
game.coreName = [NSString stringWithUTF8String:entry->core_name];
else if (b && *b)
game.coreName = [NSString stringWithUTF8String:b];
[games addObject:game];
}];
RARCH_LOG("App Intents: Found %lu games across all playlists\n", (unsigned long)games.count);
return games;
}
+ (nullable RetroArchPlaylistGame *)findGameByFilename:(NSString * _Nonnull)filename
{
// Check if RetroArch is properly initialized
runloop_state_t *runloop_st = runloop_state_get_ptr();
if (!runloop_st || !(runloop_st->flags & RUNLOOP_FLAG_IS_INITED)) {
RARCH_LOG("RetroArch not fully initialized, cannot find games\n");
return nil;
}
NSArray<RetroArchPlaylistGame *> *allGames = [self getAllGames];
for (RetroArchPlaylistGame *game in allGames) {
if ([game.filename isEqualToString:filename]) {
return game;
}
}
return nil;
}
+ (nullable NSData *)exportAllGamesAsJSONData
{
NSArray<RetroArchPlaylistGame *> *allGames = [self getAllGames];
NSMutableArray<NSDictionary *> *serialized =
[[NSMutableArray alloc] initWithCapacity:allGames.count];
for (RetroArchPlaylistGame *game in allGames) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
// "titleId" is the same <filename> used in the retroarch://game/<filename> launch scheme
dict[@"titleId"] = game.filename ?: @"";
dict[@"titleName"] = game.title ?: @"";
dict[@"filename"] = game.filename ?: @"";
dict[@"gameId"] = game.gameId ?: @"";
dict[@"developer"] = @"";
dict[@"version"] = @"";
if (game.system)
dict[@"system"] = game.system;
if (game.coreName)
dict[@"coreName"] = game.coreName;
[serialized addObject:dict];
}
NSError *error = nil;
NSData *json = [NSJSONSerialization dataWithJSONObject:serialized
options:0
error:&error];
if (!json) {
RARCH_WARN("Failed to serialize game library: %s\n",
[[error localizedDescription] UTF8String]);
return nil;
}
return json;
}
+ (nullable NSString *)exportAllGamesAsBase64URLString
{
NSData *json = [self exportAllGamesAsJSONData];
if (!json)
return nil;
/* URL-safe base64 (base64url) without padding, matching the encoding other
* front-ends use for their library callbacks. */
NSString *encoded = [json base64EncodedStringWithOptions:0];
encoded = [encoded stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
encoded = [encoded stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
encoded = [encoded stringByReplacingOccurrencesOfString:@"=" withString:@""];
return encoded;
}
// Private helper method to extract games from a playlist
+ (NSArray<RetroArchPlaylistGame *> *)getGamesFromPlaylist:(playlist_t *)playlist
withIdPrefix:(NSString *)idPrefix
maxGames:(uint32_t)maxGames
{
NSMutableArray<RetroArchPlaylistGame *> *games = [[NSMutableArray alloc] init];
if (!playlist) {
return games;
}
uint32_t playlist_size = playlist_get_size(playlist);
uint32_t games_to_get = MIN(maxGames, playlist_size);
for (uint32_t i = 0; i < games_to_get; i++) {
const struct playlist_entry *entry = NULL;
playlist_get_index(playlist, i, &entry);
if (entry && entry->path && entry->label) {
RetroArchPlaylistGame *game = [[RetroArchPlaylistGame alloc] init];
game.gameId = [NSString stringWithFormat:@"%@:%u", idPrefix, i];
game.title = [NSString stringWithUTF8String:entry->label];
game.fullPath = [NSString stringWithUTF8String:entry->path];
game.filename = [NSString stringWithUTF8String:path_basename(entry->path)];
if (entry->core_path)
game.corePath = [NSString stringWithUTF8String:entry->core_path];
if (entry->core_name)
game.coreName = [NSString stringWithUTF8String:entry->core_name];
[games addObject:game];
}
}
return games;
}
+ (NSArray<RetroArchPlaylistGame *> *)getHistoryGames
{
// Check if history is enabled
settings_t *settings = config_get_ptr();
if (!settings || !settings->bools.history_list_enable) {
RARCH_LOG("History list disabled, cannot access history\n");
return @[];
}
NSArray *games = [self getGamesFromPlaylist:g_defaults.content_history
withIdPrefix:@"history"
maxGames:10];
RARCH_LOG("App Intents: Returning %lu history games\n", (unsigned long)games.count);
return games;
}
+ (NSArray<RetroArchPlaylistGame *> *)getFavoriteGames
{
NSArray *games = [self getGamesFromPlaylist:g_defaults.content_favorites
withIdPrefix:@"favorites"
maxGames:10];
RARCH_LOG("App Intents: Returning %lu favorite games\n", (unsigned long)games.count);
return games;
}
@end
NS_ASSUME_NONNULL_END