2013-06-24 11:52:14 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-06-24 01:18:23 +00:00
|
|
|
#include "core_info.h"
|
|
|
|
#include "general.h"
|
2013-06-26 00:57:19 +00:00
|
|
|
#include "file.h"
|
2013-07-15 12:54:40 +00:00
|
|
|
#include "file_ext.h"
|
2013-10-05 16:33:22 +00:00
|
|
|
#include "file_extract.h"
|
2013-06-26 00:57:19 +00:00
|
|
|
#include "config.def.h"
|
2013-06-24 01:18:23 +00:00
|
|
|
|
2013-10-05 16:33:22 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-05 11:48:08 +00:00
|
|
|
core_info_list_t *core_info_list_new(const char *modules_path)
|
2013-06-24 01:18:23 +00:00
|
|
|
{
|
2013-06-26 00:57:19 +00:00
|
|
|
struct string_list *contents = dir_list_new(modules_path, EXT_EXECUTABLES, false);
|
2013-10-06 15:00:26 +00:00
|
|
|
size_t all_ext_len;
|
2013-06-26 00:57:19 +00:00
|
|
|
|
2013-10-05 11:37:38 +00:00
|
|
|
core_info_t *core_info = NULL;
|
|
|
|
core_info_list_t *core_info_list = NULL;
|
2013-06-24 01:18:23 +00:00
|
|
|
|
2013-06-26 00:57:19 +00:00
|
|
|
if (!contents)
|
2013-06-24 01:18:23 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-10-05 11:37:38 +00:00
|
|
|
core_info_list = (core_info_list_t*)calloc(1, sizeof(*core_info_list));
|
|
|
|
if (!core_info_list)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
core_info = (core_info_t*)calloc(contents->size, sizeof(*core_info));
|
|
|
|
if (!core_info)
|
|
|
|
goto error;
|
2013-06-24 01:18:23 +00:00
|
|
|
|
|
|
|
core_info_list->list = core_info;
|
2013-06-26 00:57:19 +00:00
|
|
|
core_info_list->count = contents->size;
|
2013-06-24 01:18:23 +00:00
|
|
|
|
2013-10-05 11:37:38 +00:00
|
|
|
for (size_t i = 0; i < contents->size; i++)
|
2013-06-24 01:18:23 +00:00
|
|
|
{
|
2013-06-26 00:57:19 +00:00
|
|
|
char info_path[PATH_MAX];
|
|
|
|
core_info[i].path = strdup(contents->elems[i].data);
|
2013-10-06 10:27:08 +00:00
|
|
|
if (!core_info[i].path)
|
|
|
|
break;
|
2013-06-26 00:57:19 +00:00
|
|
|
|
2013-10-06 18:56:01 +00:00
|
|
|
#if defined(RARCH_MOBILE) || defined(RARCH_CONSOLE)
|
2013-10-06 10:16:44 +00:00
|
|
|
// Libs are deployed with a suffix (*_ios.dylib, *_qnx.so, etc).
|
|
|
|
char buffer[PATH_MAX];
|
2013-10-05 11:37:38 +00:00
|
|
|
strlcpy(buffer, contents->elems[i].data, sizeof(buffer));
|
|
|
|
char *substr = strrchr(buffer, '_');
|
2013-06-26 00:57:19 +00:00
|
|
|
if (substr)
|
2013-10-05 11:37:38 +00:00
|
|
|
*substr = '\0';
|
2013-10-06 10:16:44 +00:00
|
|
|
fill_pathname(info_path, buffer, ".info", sizeof(info_path));
|
2013-10-05 15:07:56 +00:00
|
|
|
#else
|
|
|
|
fill_pathname(info_path, core_info[i].path, ".info", sizeof(info_path));
|
|
|
|
#endif
|
2013-06-24 01:18:23 +00:00
|
|
|
|
2013-06-26 00:57:19 +00:00
|
|
|
core_info[i].data = config_file_new(info_path);
|
2013-06-24 11:52:14 +00:00
|
|
|
|
2013-06-26 00:57:19 +00:00
|
|
|
if (core_info[i].data)
|
2013-06-24 01:18:23 +00:00
|
|
|
{
|
2013-06-26 00:57:19 +00:00
|
|
|
config_get_string(core_info[i].data, "display_name", &core_info[i].display_name);
|
2013-06-26 01:04:16 +00:00
|
|
|
if (config_get_string(core_info[i].data, "supported_extensions", &core_info[i].supported_extensions) &&
|
|
|
|
core_info[i].supported_extensions)
|
|
|
|
core_info[i].supported_extensions_list = string_split(core_info[i].supported_extensions, "|");
|
2013-06-24 01:18:23 +00:00
|
|
|
}
|
2013-06-26 00:57:19 +00:00
|
|
|
|
|
|
|
if (!core_info[i].display_name)
|
|
|
|
core_info[i].display_name = strdup(path_basename(core_info[i].path));
|
2013-06-24 01:18:23 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 15:00:26 +00:00
|
|
|
all_ext_len = 0;
|
2013-10-05 15:07:56 +00:00
|
|
|
for (size_t i = 0; i < core_info_list->count; i++)
|
|
|
|
{
|
|
|
|
all_ext_len += core_info_list->list[i].supported_extensions ?
|
|
|
|
(strlen(core_info_list->list[i].supported_extensions) + 2) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_ext_len)
|
2013-10-05 16:33:22 +00:00
|
|
|
{
|
|
|
|
all_ext_len += strlen("|zip");
|
2013-10-05 15:07:56 +00:00
|
|
|
core_info_list->all_ext = (char*)calloc(1, all_ext_len);
|
2013-10-05 16:33:22 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 15:07:56 +00:00
|
|
|
if (core_info_list->all_ext)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < core_info_list->count; i++)
|
|
|
|
{
|
|
|
|
if (core_info_list->list[i].supported_extensions)
|
|
|
|
{
|
|
|
|
strlcat(core_info_list->all_ext, core_info_list->list[i].supported_extensions, all_ext_len);
|
|
|
|
strlcat(core_info_list->all_ext, "|", all_ext_len);
|
|
|
|
}
|
|
|
|
}
|
2013-10-05 16:33:22 +00:00
|
|
|
strlcat(core_info_list->all_ext, "|zip", all_ext_len);
|
2013-10-05 15:07:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 00:57:19 +00:00
|
|
|
dir_list_free(contents);
|
2013-06-24 01:18:23 +00:00
|
|
|
return core_info_list;
|
2013-10-05 11:37:38 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (contents)
|
|
|
|
dir_list_free(contents);
|
2013-10-05 11:48:08 +00:00
|
|
|
core_info_list_free(core_info_list);
|
2013-10-05 11:37:38 +00:00
|
|
|
return NULL;
|
2013-06-24 01:18:23 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 11:48:08 +00:00
|
|
|
void core_info_list_free(core_info_list_t *core_info_list)
|
2013-06-24 01:18:23 +00:00
|
|
|
{
|
2013-06-26 00:57:19 +00:00
|
|
|
if (!core_info_list)
|
|
|
|
return;
|
|
|
|
|
2013-10-05 11:37:38 +00:00
|
|
|
for (size_t i = 0; i < core_info_list->count; i++)
|
2013-06-24 01:18:23 +00:00
|
|
|
{
|
|
|
|
free(core_info_list->list[i].path);
|
2013-06-26 00:57:19 +00:00
|
|
|
free(core_info_list->list[i].display_name);
|
2013-06-26 01:04:16 +00:00
|
|
|
free(core_info_list->list[i].supported_extensions);
|
|
|
|
string_list_free(core_info_list->list[i].supported_extensions_list);
|
2013-06-24 01:18:23 +00:00
|
|
|
config_file_free(core_info_list->list[i].data);
|
|
|
|
}
|
2013-06-24 11:52:14 +00:00
|
|
|
|
2013-10-05 15:07:56 +00:00
|
|
|
free(core_info_list->all_ext);
|
2013-06-24 01:18:23 +00:00
|
|
|
free(core_info_list->list);
|
|
|
|
free(core_info_list);
|
|
|
|
}
|
2013-06-26 00:57:19 +00:00
|
|
|
|
2013-10-06 10:27:08 +00:00
|
|
|
bool core_info_list_get_display_name(core_info_list_t *core_info_list, const char *path, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < core_info_list->count; i++)
|
|
|
|
{
|
|
|
|
const core_info_t *info = &core_info_list->list[i];
|
|
|
|
if (!strcmp(info->path, path) && info->display_name)
|
|
|
|
{
|
|
|
|
strlcpy(buf, info->display_name, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-05 16:33:22 +00:00
|
|
|
bool core_info_does_support_any_file(const core_info_t *core, const struct string_list *list)
|
|
|
|
{
|
|
|
|
if (!list || !core || !core->supported_extensions_list)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < list->size; i++)
|
|
|
|
if (string_list_find_elem_prefix(core->supported_extensions_list, ".", path_get_extension(list->elems[i].data)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-05 15:07:56 +00:00
|
|
|
bool core_info_does_support_file(const core_info_t *core, const char *path)
|
2013-06-26 00:57:19 +00:00
|
|
|
{
|
2013-06-26 01:04:16 +00:00
|
|
|
if (!path || !core || !core->supported_extensions_list)
|
2013-06-26 00:57:19 +00:00
|
|
|
return false;
|
|
|
|
|
2013-06-26 01:04:16 +00:00
|
|
|
return string_list_find_elem_prefix(core->supported_extensions_list, ".", path_get_extension(path));
|
2013-06-26 00:57:19 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 15:07:56 +00:00
|
|
|
const char *core_info_list_get_all_extensions(core_info_list_t *core_info_list)
|
|
|
|
{
|
|
|
|
return core_info_list->all_ext;
|
|
|
|
}
|
|
|
|
|
2013-10-05 16:33:22 +00:00
|
|
|
// qsort_r() is not in standard C, sadly.
|
|
|
|
static const char *core_info_tmp_path;
|
|
|
|
static const struct string_list *core_info_tmp_list;
|
|
|
|
|
2013-10-05 15:07:56 +00:00
|
|
|
static int core_info_qsort_cmp(const void *a_, const void *b_)
|
|
|
|
{
|
|
|
|
const core_info_t *a = (const core_info_t*)a_;
|
|
|
|
const core_info_t *b = (const core_info_t*)b_;
|
2013-10-05 16:33:22 +00:00
|
|
|
|
|
|
|
int support_a = core_info_does_support_any_file(a, core_info_tmp_list) ||
|
|
|
|
core_info_does_support_file(a, core_info_tmp_path);
|
|
|
|
int support_b = core_info_does_support_any_file(b, core_info_tmp_list) ||
|
|
|
|
core_info_does_support_file(b, core_info_tmp_path);
|
|
|
|
|
2013-10-05 15:07:56 +00:00
|
|
|
if (support_a != support_b)
|
|
|
|
return support_b - support_a;
|
|
|
|
else
|
|
|
|
return strcasecmp(a->display_name, b->display_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void core_info_list_get_supported_cores(core_info_list_t *core_info_list, const char *path,
|
|
|
|
const core_info_t **infos, size_t *num_infos)
|
|
|
|
{
|
|
|
|
core_info_tmp_path = path;
|
2013-10-05 16:33:22 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
struct string_list *list = NULL;
|
|
|
|
if (!strcasecmp(path_get_extension(path), "zip"))
|
|
|
|
list = zlib_get_file_list(path);
|
|
|
|
core_info_tmp_list = list;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Let supported core come first in list so we can return a pointer to them.
|
2013-10-05 15:07:56 +00:00
|
|
|
qsort(core_info_list->list, core_info_list->count, sizeof(core_info_t), core_info_qsort_cmp);
|
|
|
|
|
|
|
|
size_t supported = 0;
|
|
|
|
for (size_t i = 0; i < core_info_list->count; i++, supported++)
|
|
|
|
{
|
2013-10-05 16:33:22 +00:00
|
|
|
const core_info_t *core = &core_info_list->list[i];
|
|
|
|
if (!core_info_does_support_file(core, path) && !core_info_does_support_any_file(core, list))
|
2013-10-05 15:07:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-05 16:33:22 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (list)
|
|
|
|
string_list_free(list);
|
|
|
|
#endif
|
|
|
|
|
2013-10-05 15:07:56 +00:00
|
|
|
*infos = core_info_list->list;
|
|
|
|
*num_infos = supported;
|
|
|
|
}
|
|
|
|
|