RetroArch/core_info.c

513 lines
14 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - 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/>.
*/
#include "core_info.h"
2014-08-02 12:04:43 +00:00
#include "general.h"
#include "file.h"
#include "file_ext.h"
#include "file_extract.h"
#include "config.def.h"
#ifdef HAVE_CONFIG_H
2014-08-02 12:04:43 +00:00
#include "config.h"
#endif
2014-06-17 15:28:39 +00:00
static core_info_list_t *global_core_list;
2014-09-02 03:10:54 +00:00
static void core_info_list_resolve_all_extensions(
core_info_list_t *core_info_list)
{
size_t i, all_ext_len = 0;
2014-06-01 00:29:19 +00:00
if (!core_info_list)
return;
for (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)
{
core_info_list->all_ext = (char*)calloc(1, all_ext_len);
}
if (core_info_list->all_ext)
{
for (i = 0; i < core_info_list->count; i++)
{
if (core_info_list->list[i].supported_extensions)
{
2014-09-02 03:10:54 +00:00
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);
}
}
}
}
2014-09-02 03:10:54 +00:00
static void core_info_list_resolve_all_firmware(
core_info_list_t *core_info_list)
{
2013-11-24 01:08:20 +00:00
size_t i;
unsigned c;
2014-06-01 00:29:19 +00:00
if (!core_info_list)
return;
2014-03-03 09:11:07 +00:00
for (i = 0; i < core_info_list->count; i++)
{
2014-08-02 12:11:06 +00:00
unsigned count = 0;
core_info_t *info = (core_info_t*)&core_info_list->list[i];
if (!info->data)
continue;
if (!config_get_uint(info->data, "firmware_count", &count))
continue;
2014-09-02 03:10:54 +00:00
info->firmware = (core_info_firmware_t*)
calloc(count, sizeof(*info->firmware));
if (!info->firmware)
continue;
2014-03-03 09:11:07 +00:00
for (c = 0; c < count; c++)
{
char path_key[64], desc_key[64], opt_key[64];
2014-03-04 08:34:34 +00:00
snprintf(path_key, sizeof(path_key), "firmware%u_path", c);
snprintf(desc_key, sizeof(desc_key), "firmware%u_desc", c);
2014-03-04 08:34:34 +00:00
snprintf(opt_key, sizeof(opt_key), "firmware%u_opt", c);
config_get_string(info->data, path_key, &info->firmware[c].path);
2014-03-03 05:22:28 +00:00
config_get_string(info->data, desc_key, &info->firmware[c].desc);
2014-03-04 08:34:34 +00:00
config_get_bool(info->data, opt_key , &info->firmware[c].optional);
}
}
}
core_info_list_t *core_info_list_new(const char *modules_path)
{
size_t i;
core_info_t *core_info = NULL;
core_info_list_t *core_info_list = NULL;
2014-09-02 03:10:54 +00:00
struct string_list *contents = (struct string_list*)
dir_list_new(modules_path, EXT_EXECUTABLES, false);
2013-06-26 00:57:19 +00:00
if (!contents)
return NULL;
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;
core_info_list->list = core_info;
2013-06-26 00:57:19 +00:00
core_info_list->count = contents->size;
for (i = 0; i < contents->size; i++)
{
char info_path_base[PATH_MAX], info_path[PATH_MAX];
2013-06-26 00:57:19 +00:00
core_info[i].path = strdup(contents->elems[i].data);
if (!core_info[i].path)
break;
2013-06-26 00:57:19 +00:00
2014-09-02 03:10:54 +00:00
fill_pathname_base(info_path_base, contents->elems[i].data,
sizeof(info_path_base));
path_remove_extension(info_path_base);
#if defined(RARCH_MOBILE) || defined(RARCH_CONSOLE)
char *substr = strrchr(info_path_base, '_');
2013-06-26 00:57:19 +00:00
if (substr)
*substr = '\0';
#endif
2013-10-10 22:24:01 +00:00
strlcat(info_path_base, ".info", sizeof(info_path_base));
2014-09-02 03:10:54 +00:00
fill_pathname_join(info_path, (*g_settings.libretro_info_path) ?
g_settings.libretro_info_path : modules_path,
info_path_base, sizeof(info_path));
2013-06-26 00:57:19 +00:00
core_info[i].data = config_file_new(info_path);
2013-06-26 00:57:19 +00:00
if (core_info[i].data)
{
2014-03-03 09:11:07 +00:00
unsigned count = 0;
2014-09-02 03:10:54 +00:00
config_get_string(core_info[i].data, "display_name",
&core_info[i].display_name);
config_get_string(core_info[i].data, "systemname",
&core_info[i].systemname);
2014-03-03 09:11:07 +00:00
config_get_uint(core_info[i].data, "firmware_count", &count);
core_info[i].firmware_count = count;
2014-09-02 03:10:54 +00:00
if (config_get_string(core_info[i].data, "supported_extensions",
&core_info[i].supported_extensions) &&
core_info[i].supported_extensions)
2014-09-02 03:10:54 +00:00
core_info[i].supported_extensions_list =
string_split(core_info[i].supported_extensions, "|");
2013-11-07 23:30:00 +00:00
2014-09-02 03:10:54 +00:00
if (config_get_string(core_info[i].data, "authors",
&core_info[i].authors) &&
2013-11-07 23:30:00 +00:00
core_info[i].authors)
2014-09-02 03:10:54 +00:00
core_info[i].authors_list =
string_split(core_info[i].authors, "|");
2013-12-14 18:29:14 +00:00
2014-09-02 03:10:54 +00:00
if (config_get_string(core_info[i].data, "permissions",
&core_info[i].permissions) &&
2013-12-14 18:29:14 +00:00
core_info[i].permissions)
2014-09-02 03:10:54 +00:00
core_info[i].permissions_list =
string_split(core_info[i].permissions, "|");
2014-09-08 18:18:36 +00:00
if (config_get_string(core_info[i].data, "license",
&core_info[i].licenses) &&
core_info[i].licenses)
core_info[i].licenses_list =
string_split(core_info[i].licenses, "|");
2014-09-02 03:10:54 +00:00
if (config_get_string(core_info[i].data, "notes",
&core_info[i].notes) &&
2014-03-03 05:22:28 +00:00
core_info[i].notes)
2014-03-03 09:11:07 +00:00
core_info[i].note_list = string_split(core_info[i].notes, "|");
config_get_bool(core_info[i].data, "supports_no_game",
&core_info[i].supports_no_game);
}
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));
}
core_info_list_resolve_all_extensions(core_info_list);
core_info_list_resolve_all_firmware(core_info_list);
2013-10-05 15:07:56 +00:00
2013-06-26 00:57:19 +00:00
dir_list_free(contents);
return core_info_list;
error:
if (contents)
dir_list_free(contents);
core_info_list_free(core_info_list);
return NULL;
}
2014-06-17 15:28:39 +00:00
void core_info_list_free(core_info_list_t *core_info_list)
{
size_t i, j;
2013-06-26 00:57:19 +00:00
if (!core_info_list)
return;
for (i = 0; i < core_info_list->count; i++)
{
core_info_t *info = (core_info_t*)&core_info_list->list[i];
free(info->path);
free(info->systemname);
free(info->display_name);
free(info->supported_extensions);
free(info->authors);
2013-12-14 18:29:14 +00:00
free(info->permissions);
2014-09-08 18:18:36 +00:00
free(info->licenses);
2014-03-03 09:11:07 +00:00
free(info->notes);
if (info->supported_extensions_list)
string_list_free(info->supported_extensions_list);
string_list_free(info->authors_list);
2014-03-03 09:11:07 +00:00
string_list_free(info->note_list);
2013-12-14 18:29:14 +00:00
string_list_free(info->permissions_list);
2014-09-08 18:18:36 +00:00
string_list_free(info->licenses_list);
config_file_free(info->data);
for (j = 0; j < info->firmware_count; j++)
{
free(info->firmware[j].path);
free(info->firmware[j].desc);
}
free(info->firmware);
}
2013-10-05 15:07:56 +00:00
free(core_info_list->all_ext);
free(core_info_list->list);
free(core_info_list);
}
2013-06-26 00:57:19 +00:00
2014-06-17 15:28:39 +00:00
size_t core_info_list_num_info_files(core_info_list_t *core_info_list)
{
size_t i, num;
2014-06-01 00:29:19 +00:00
if (!core_info_list)
return 0;
num = 0;
for (i = 0; i < core_info_list->count; i++)
num += !!core_info_list->list[i].data;
return num;
}
2014-09-02 03:10:54 +00:00
bool core_info_list_get_display_name(core_info_list_t *core_info_list,
const char *path, char *buf, size_t size)
{
size_t i;
2014-06-01 00:29:19 +00:00
if (!core_info_list)
return false;
for (i = 0; i < core_info_list->count; i++)
{
const core_info_t *info = &core_info_list->list[i];
2014-09-02 03:10:54 +00:00
if (!strcmp(path_basename(info->path), path_basename(path))
&& info->display_name)
{
strlcpy(buf, info->display_name, size);
return true;
}
}
return false;
}
2014-09-02 03:10:54 +00:00
bool core_info_list_get_info(core_info_list_t *core_info_list,
core_info_t *out_info, const char *path)
{
size_t i;
2014-06-01 00:29:19 +00:00
if (!core_info_list || !out_info)
2014-06-01 00:16:48 +00:00
return false;
memset(out_info, 0, sizeof(*out_info));
for (i = 0; i < core_info_list->count; i++)
{
const core_info_t *info = &core_info_list->list[i];
if (!strcmp(path_basename(info->path), path_basename(path)))
{
*out_info = *info;
return true;
}
}
return false;
}
2014-09-02 03:10:54 +00:00
bool core_info_does_support_any_file(const core_info_t *core,
const struct string_list *list)
{
size_t i;
if (!list || !core || !core->supported_extensions_list)
return false;
for (i = 0; i < list->size; i++)
2014-09-02 03:10:54 +00:00
if (string_list_find_elem_prefix(core->supported_extensions_list,
".", path_get_extension(list->elems[i].data)))
return true;
return false;
}
2014-06-17 15:28:39 +00:00
bool core_info_does_support_file(const core_info_t *core, const char *path)
2013-06-26 00:57:19 +00:00
{
if (!path || !core || !core->supported_extensions_list)
2013-06-26 00:57:19 +00:00
return false;
2014-09-02 03:10:54 +00:00
return string_list_find_elem_prefix(
core->supported_extensions_list, ".", path_get_extension(path));
2013-06-26 00:57:19 +00:00
}
2014-06-17 15:28:39 +00:00
const char *core_info_list_get_all_extensions(core_info_list_t *core_info_list)
2013-10-05 15:07:56 +00:00
{
2014-06-01 00:29:19 +00:00
if (core_info_list)
return core_info_list->all_ext;
return "";
2013-10-05 15:07:56 +00:00
}
2014-09-02 03:10:54 +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_;
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;
2014-08-02 12:01:35 +00:00
return strcasecmp(a->display_name, b->display_name);
2013-10-05 15:07:56 +00:00
}
2014-09-02 03:10:54 +00:00
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)
2013-10-05 15:07:56 +00:00
{
2014-06-01 00:29:19 +00:00
if (!core_info_list)
return;
2013-10-05 15:07:56 +00:00
core_info_tmp_path = path;
#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
2014-09-02 03:10:54 +00:00
/* Let supported core come first in list so we can return
* a pointer to them. */
qsort(core_info_list->list, core_info_list->count,
sizeof(core_info_t), core_info_qsort_cmp);
2013-10-05 15:07:56 +00:00
size_t supported, i;
supported = 0;
for (i = 0; i < core_info_list->count; i++, supported++)
2013-10-05 15:07:56 +00:00
{
const core_info_t *core = &core_info_list->list[i];
2013-10-08 20:45:08 +00:00
if (!core_info_does_support_file(core, path)
#ifdef HAVE_ZLIB
&& !core_info_does_support_any_file(core, list)
#endif
2014-03-03 09:11:07 +00:00
)
2013-10-05 15:07:56 +00:00
break;
}
#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;
}
2014-09-02 03:10:54 +00:00
static core_info_t *find_core_info(core_info_list_t *list,
const char *core)
{
size_t i;
2014-06-01 00:29:19 +00:00
for (i = 0; i < list->count; i++)
{
2014-08-02 12:01:35 +00:00
core_info_t *info = (core_info_t*)&list->list[i];
if (info->path && !strcmp(info->path, core))
return info;
}
return NULL;
}
static int core_info_firmware_cmp(const void *a_, const void *b_)
{
const core_info_firmware_t *a = (const core_info_firmware_t*)a_;
const core_info_firmware_t *b = (const core_info_firmware_t*)b_;
int order = b->missing - a->missing;
2014-08-02 12:01:35 +00:00
if (order)
return order;
2014-08-02 12:01:35 +00:00
return strcasecmp(a->path, b->path);
}
2014-06-17 15:28:39 +00:00
void core_info_list_update_missing_firmware(core_info_list_t *core_info_list,
2014-03-03 06:02:29 +00:00
const char *core, const char *systemdir)
{
size_t i;
char path[PATH_MAX];
2014-08-02 12:01:35 +00:00
core_info_t *info = NULL;
if (!core_info_list || !core)
2014-06-01 00:29:19 +00:00
return;
2014-03-03 06:02:29 +00:00
2014-08-02 12:01:35 +00:00
if (!(info = find_core_info(core_info_list, core)))
2014-03-03 06:02:29 +00:00
return;
for (i = 0; i < info->firmware_count; i++)
{
2014-03-03 09:11:07 +00:00
if (info->firmware[i].path)
{
2014-09-02 03:10:54 +00:00
fill_pathname_join(path, systemdir,
info->firmware[i].path, sizeof(path));
2014-03-03 09:11:07 +00:00
info->firmware[i].missing = !path_file_exists(path);
}
2014-03-03 06:02:29 +00:00
}
}
2014-06-17 15:28:39 +00:00
void core_info_list_get_missing_firmware(core_info_list_t *core_info_list,
const char *core, const char *systemdir,
const core_info_firmware_t **firmware, size_t *num_firmware)
{
size_t i;
char path[PATH_MAX];
2014-08-02 12:01:35 +00:00
core_info_t *info = NULL;
if (!core_info_list || !core)
2014-06-01 00:29:19 +00:00
return;
*firmware = NULL;
*num_firmware = 0;
2014-08-02 12:01:35 +00:00
if (!(info = find_core_info(core_info_list, core)))
return;
*firmware = info->firmware;
for (i = 1; i < info->firmware_count; i++)
{
fill_pathname_join(path, systemdir, info->firmware[i].path, sizeof(path));
info->firmware[i].missing = !path_file_exists(path);
*num_firmware += info->firmware[i].missing;
}
2014-09-02 03:10:54 +00:00
qsort(info->firmware, info->firmware_count, sizeof(*info->firmware),
core_info_firmware_cmp);
}
2014-08-15 15:59:50 +00:00
/* TODO - remove all of the functions below */
void core_info_set_core_path(void)
{
global_core_list = core_info_list_new(g_defaults.core_dir);
}
core_info_list_t *core_info_list_get(void)
{
return global_core_list;
}
2014-06-17 15:28:39 +00:00
const core_info_t *core_info_list_get_by_id(const char *core_id)
{
unsigned i;
2014-06-17 15:28:39 +00:00
const core_info_list_t* cores = core_info_list_get();
2014-08-02 12:01:35 +00:00
if (!core_id || !cores)
return NULL;
2014-08-02 12:01:35 +00:00
for (i = 0; i < cores->count; i ++)
if (cores->list[i].path && strcmp(core_id, cores->list[i].path) == 0)
return &cores->list[i];
return NULL;
}
bool core_info_get_custom_config(const char *core_id,
char *buf, size_t sizeof_buf)
{
2014-08-17 15:35:17 +00:00
if (!core_id || !buf || !sizeof_buf)
return false;
2014-09-02 03:10:54 +00:00
fill_pathname_join(buf, g_defaults.menu_config_dir,
path_basename(core_id), sizeof_buf);
2014-08-17 15:35:17 +00:00
fill_pathname(buf, buf, ".cfg", sizeof_buf);
if (buf[0] == '\0')
return false;
return path_file_exists(buf);
}