/* libretro-plugin.c generated by valac 0.36.1.39-72bb2, the Vala compiler
 * generated from libretro-plugin.vala, do not modify */

/* This file is part of GNOME Games. License: GPL-3.0+.*/

#include <glib.h>
#include <glib-object.h>
#include <libgnome-games.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <retro-gtk.h>


#define GAMES_TYPE_LIBRETRO_PLUGIN (games_libretro_plugin_get_type ())
#define GAMES_LIBRETRO_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_LIBRETRO_PLUGIN, GamesLibretroPlugin))
#define GAMES_LIBRETRO_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_LIBRETRO_PLUGIN, GamesLibretroPluginClass))
#define GAMES_IS_LIBRETRO_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_LIBRETRO_PLUGIN))
#define GAMES_IS_LIBRETRO_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_LIBRETRO_PLUGIN))
#define GAMES_LIBRETRO_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_LIBRETRO_PLUGIN, GamesLibretroPluginClass))

typedef struct _GamesLibretroPlugin GamesLibretroPlugin;
typedef struct _GamesLibretroPluginClass GamesLibretroPluginClass;
typedef struct _GamesLibretroPluginPrivate GamesLibretroPluginPrivate;
enum  {
	GAMES_LIBRETRO_PLUGIN_DUMMY_PROPERTY,
	GAMES_LIBRETRO_PLUGIN_LAST_PROPERTY
};
static GParamSpec* games_libretro_plugin_properties[GAMES_LIBRETRO_PLUGIN_LAST_PROPERTY];

#define GAMES_TYPE_LIBRETRO_URI_SOURCE (games_libretro_uri_source_get_type ())
#define GAMES_LIBRETRO_URI_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_LIBRETRO_URI_SOURCE, GamesLibretroUriSource))
#define GAMES_LIBRETRO_URI_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_LIBRETRO_URI_SOURCE, GamesLibretroUriSourceClass))
#define GAMES_IS_LIBRETRO_URI_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_LIBRETRO_URI_SOURCE))
#define GAMES_IS_LIBRETRO_URI_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_LIBRETRO_URI_SOURCE))
#define GAMES_LIBRETRO_URI_SOURCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_LIBRETRO_URI_SOURCE, GamesLibretroUriSourceClass))

typedef struct _GamesLibretroUriSource GamesLibretroUriSource;
typedef struct _GamesLibretroUriSourceClass GamesLibretroUriSourceClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _games_uri_unref0(var) ((var == NULL) ? NULL : (var = (games_uri_unref (var), NULL)))

#define GAMES_TYPE_LIBRETRO_UID (games_libretro_uid_get_type ())
#define GAMES_LIBRETRO_UID(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_LIBRETRO_UID, GamesLibretroUid))
#define GAMES_LIBRETRO_UID_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_LIBRETRO_UID, GamesLibretroUidClass))
#define GAMES_IS_LIBRETRO_UID(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_LIBRETRO_UID))
#define GAMES_IS_LIBRETRO_UID_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_LIBRETRO_UID))
#define GAMES_LIBRETRO_UID_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_LIBRETRO_UID, GamesLibretroUidClass))

typedef struct _GamesLibretroUid GamesLibretroUid;
typedef struct _GamesLibretroUidClass GamesLibretroUidClass;

#define GAMES_TYPE_LIBRETRO_TITLE (games_libretro_title_get_type ())
#define GAMES_LIBRETRO_TITLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_LIBRETRO_TITLE, GamesLibretroTitle))
#define GAMES_LIBRETRO_TITLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_LIBRETRO_TITLE, GamesLibretroTitleClass))
#define GAMES_IS_LIBRETRO_TITLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_LIBRETRO_TITLE))
#define GAMES_IS_LIBRETRO_TITLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_LIBRETRO_TITLE))
#define GAMES_LIBRETRO_TITLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_LIBRETRO_TITLE, GamesLibretroTitleClass))

typedef struct _GamesLibretroTitle GamesLibretroTitle;
typedef struct _GamesLibretroTitleClass GamesLibretroTitleClass;

#define GAMES_TYPE_LIBRETRO_ICON (games_libretro_icon_get_type ())
#define GAMES_LIBRETRO_ICON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_LIBRETRO_ICON, GamesLibretroIcon))
#define GAMES_LIBRETRO_ICON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_LIBRETRO_ICON, GamesLibretroIconClass))
#define GAMES_IS_LIBRETRO_ICON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_LIBRETRO_ICON))
#define GAMES_IS_LIBRETRO_ICON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_LIBRETRO_ICON))
#define GAMES_LIBRETRO_ICON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_LIBRETRO_ICON, GamesLibretroIconClass))

typedef struct _GamesLibretroIcon GamesLibretroIcon;
typedef struct _GamesLibretroIconClass GamesLibretroIconClass;

struct _GamesLibretroPlugin {
	GObject parent_instance;
	GamesLibretroPluginPrivate * priv;
};

struct _GamesLibretroPluginClass {
	GObjectClass parent_class;
};

typedef enum  {
	GAMES_LIBRETRO_ERROR_NOT_A_LIBRETRO_DESCRIPTOR,
	GAMES_LIBRETRO_ERROR_NOT_A_GAME
} GamesLibretroError;
#define GAMES_LIBRETRO_ERROR games_libretro_error_quark ()

static gpointer games_libretro_plugin_parent_class = NULL;
static GamesPluginIface * games_libretro_plugin_games_plugin_parent_iface = NULL;
static GType games_libretro_plugin_type_id = 0;

GType games_libretro_plugin_get_type (void) G_GNUC_CONST;
#define GAMES_LIBRETRO_PLUGIN_LIBRETRO_FILE_SCHEME "libretro+file"
static GamesUriSource** games_libretro_plugin_real_get_uri_sources (GamesPlugin* base, int* result_length1);
GType games_libretro_uri_source_get_type (void) G_GNUC_CONST;
GamesLibretroUriSource* games_libretro_uri_source_new (void);
GamesLibretroUriSource* games_libretro_uri_source_construct (GType object_type);
static GamesUriGameFactory** games_libretro_plugin_real_get_uri_game_factories (GamesPlugin* base, int* result_length1);
static GamesGame* games_libretro_plugin_game_for_uri (GamesUri* uri, GError** error);
static GamesGame* _games_libretro_plugin_game_for_uri_games_generic_game_uri_adapter_game_for_uri (GamesUri* uri, gpointer self, GError** error);
GQuark games_libretro_error_quark (void);
GType games_libretro_uid_get_type (void) G_GNUC_CONST;
GamesLibretroUid* games_libretro_uid_new (RetroCoreDescriptor* core_descriptor);
GamesLibretroUid* games_libretro_uid_construct (GType object_type, RetroCoreDescriptor* core_descriptor);
GType games_libretro_title_get_type (void) G_GNUC_CONST;
GamesLibretroTitle* games_libretro_title_new (RetroCoreDescriptor* core_descriptor);
GamesLibretroTitle* games_libretro_title_construct (GType object_type, RetroCoreDescriptor* core_descriptor);
GType games_libretro_icon_get_type (void) G_GNUC_CONST;
GamesLibretroIcon* games_libretro_icon_new (RetroCoreDescriptor* core_descriptor);
GamesLibretroIcon* games_libretro_icon_construct (GType object_type, RetroCoreDescriptor* core_descriptor);
GamesLibretroPlugin* games_libretro_plugin_new (void);
GamesLibretroPlugin* games_libretro_plugin_construct (GType object_type);
GType register_games_plugin (GTypeModule* module);
GType games_libretro_icon_register_type (GTypeModule * module);
GType games_libretro_plugin_register_type (GTypeModule * module);
GType games_libretro_title_register_type (GTypeModule * module);
GType games_libretro_uid_register_type (GTypeModule * module);
GType games_libretro_uri_iterator_register_type (GTypeModule * module);
GType games_libretro_uri_source_register_type (GTypeModule * module);


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static GamesUriSource** games_libretro_plugin_real_get_uri_sources (GamesPlugin* base, int* result_length1) {
	GamesLibretroPlugin * self;
	GamesUriSource** result = NULL;
	GamesLibretroUriSource* source = NULL;
	GamesLibretroUriSource* _tmp0_;
	GamesUriSource* _tmp1_;
	GamesUriSource** _tmp2_;
	GamesUriSource** _tmp3_;
	gint _tmp3__length1;
	self = (GamesLibretroPlugin*) base;
	_tmp0_ = games_libretro_uri_source_new ();
	source = _tmp0_;
	_tmp1_ = _g_object_ref0 ((GamesUriSource*) source);
	_tmp2_ = g_new0 (GamesUriSource*, 1 + 1);
	_tmp2_[0] = _tmp1_;
	_tmp3_ = _tmp2_;
	_tmp3__length1 = 1;
	if (result_length1) {
		*result_length1 = _tmp3__length1;
	}
	result = _tmp3_;
	_g_object_unref0 (source);
	return result;
}


static GamesGame* _games_libretro_plugin_game_for_uri_games_generic_game_uri_adapter_game_for_uri (GamesUri* uri, gpointer self, GError** error) {
	GamesGame* result;
	result = games_libretro_plugin_game_for_uri (uri, error);
	return result;
}


static GamesUriGameFactory** games_libretro_plugin_real_get_uri_game_factories (GamesPlugin* base, int* result_length1) {
	GamesLibretroPlugin * self;
	GamesUriGameFactory** result = NULL;
	GamesGenericGameUriAdapter* game_uri_adapter = NULL;
	GamesGenericGameUriAdapter* _tmp0_;
	GamesGenericUriGameFactory* factory = NULL;
	GamesGenericUriGameFactory* _tmp1_;
	GamesUriGameFactory* _tmp2_;
	GamesUriGameFactory** _tmp3_;
	GamesUriGameFactory** _tmp4_;
	gint _tmp4__length1;
	self = (GamesLibretroPlugin*) base;
	_tmp0_ = games_generic_game_uri_adapter_new (_games_libretro_plugin_game_for_uri_games_generic_game_uri_adapter_game_for_uri, NULL, NULL);
	game_uri_adapter = _tmp0_;
	_tmp1_ = games_generic_uri_game_factory_new ((GamesGameUriAdapter*) game_uri_adapter);
	factory = _tmp1_;
	games_generic_uri_game_factory_add_scheme (factory, GAMES_LIBRETRO_PLUGIN_LIBRETRO_FILE_SCHEME);
	_tmp2_ = _g_object_ref0 ((GamesUriGameFactory*) factory);
	_tmp3_ = g_new0 (GamesUriGameFactory*, 1 + 1);
	_tmp3_[0] = _tmp2_;
	_tmp4_ = _tmp3_;
	_tmp4__length1 = 1;
	if (result_length1) {
		*result_length1 = _tmp4__length1;
	}
	result = _tmp4_;
	_g_object_unref0 (factory);
	_g_object_unref0 (game_uri_adapter);
	return result;
}


static GamesGame* games_libretro_plugin_game_for_uri (GamesUri* uri, GError** error) {
	GamesGame* result = NULL;
	GamesUri* file_uri = NULL;
	GamesUri* _tmp0_;
	GamesUri* _tmp1_;
	GFile* file = NULL;
	GamesUri* _tmp2_;
	GFile* _tmp3_;
	GFile* _tmp4_;
	gboolean _tmp5_;
	gchar* path = NULL;
	GFile* _tmp11_;
	gchar* _tmp12_;
	RetroCoreDescriptor* core_descriptor = NULL;
	const gchar* _tmp13_;
	RetroCoreDescriptor* _tmp14_;
	gboolean _tmp15_ = FALSE;
	RetroCoreDescriptor* _tmp16_;
	gboolean _tmp17_;
	GamesLibretroUid* uid = NULL;
	RetroCoreDescriptor* _tmp23_;
	GamesLibretroUid* _tmp24_;
	GamesLibretroTitle* title = NULL;
	RetroCoreDescriptor* _tmp25_;
	GamesLibretroTitle* _tmp26_;
	GamesLibretroIcon* icon = NULL;
	RetroCoreDescriptor* _tmp27_;
	GamesLibretroIcon* _tmp28_;
	GamesDummyCover* cover = NULL;
	GamesDummyCover* _tmp29_;
	GamesRetroRunner* runner = NULL;
	RetroCoreDescriptor* _tmp30_;
	GamesLibretroUid* _tmp31_;
	GamesLibretroTitle* _tmp32_;
	GamesRetroRunner* _tmp33_;
	GamesLibretroTitle* _tmp34_;
	GamesLibretroIcon* _tmp35_;
	GamesDummyCover* _tmp36_;
	GamesRetroRunner* _tmp37_;
	GamesGenericGame* _tmp38_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (uri != NULL, NULL);
	_tmp0_ = uri;
	_tmp1_ = games_uri_new_from_uri_and_scheme (_tmp0_, "file", &_inner_error_);
	file_uri = _tmp1_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		return NULL;
	}
	_tmp2_ = file_uri;
	_tmp3_ = games_uri_to_file (_tmp2_);
	file = _tmp3_;
	_tmp4_ = file;
	_tmp5_ = g_file_query_exists (_tmp4_, NULL);
	if (!_tmp5_) {
		GamesUri* _tmp6_;
		gchar* _tmp7_;
		gchar* _tmp8_;
		GError* _tmp9_;
		GError* _tmp10_;
		_tmp6_ = uri;
		_tmp7_ = games_uri_to_string (_tmp6_);
		_tmp8_ = _tmp7_;
		_tmp9_ = g_error_new (GAMES_LIBRETRO_ERROR, GAMES_LIBRETRO_ERROR_NOT_A_LIBRETRO_DESCRIPTOR, "This isn’t a Libretro core descriptor: %s", _tmp8_);
		_tmp10_ = _tmp9_;
		_g_free0 (_tmp8_);
		_inner_error_ = _tmp10_;
		g_propagate_error (error, _inner_error_);
		_g_object_unref0 (file);
		_games_uri_unref0 (file_uri);
		return NULL;
	}
	_tmp11_ = file;
	_tmp12_ = g_file_get_path (_tmp11_);
	path = _tmp12_;
	_tmp13_ = path;
	_tmp14_ = retro_core_descriptor_new (_tmp13_, &_inner_error_);
	core_descriptor = _tmp14_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_free0 (path);
		_g_object_unref0 (file);
		_games_uri_unref0 (file_uri);
		return NULL;
	}
	_tmp16_ = core_descriptor;
	_tmp17_ = retro_core_descriptor_get_is_game (_tmp16_, &_inner_error_);
	_tmp15_ = _tmp17_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_object_unref0 (core_descriptor);
		_g_free0 (path);
		_g_object_unref0 (file);
		_games_uri_unref0 (file_uri);
		return NULL;
	}
	if (!_tmp15_) {
		GamesUri* _tmp18_;
		gchar* _tmp19_;
		gchar* _tmp20_;
		GError* _tmp21_;
		GError* _tmp22_;
		_tmp18_ = uri;
		_tmp19_ = games_uri_to_string (_tmp18_);
		_tmp20_ = _tmp19_;
		_tmp21_ = g_error_new (GAMES_LIBRETRO_ERROR, GAMES_LIBRETRO_ERROR_NOT_A_GAME, "This Libretro core descriptor doesn't isn’t a game: %s", _tmp20_);
		_tmp22_ = _tmp21_;
		_g_free0 (_tmp20_);
		_inner_error_ = _tmp22_;
		g_propagate_error (error, _inner_error_);
		_g_object_unref0 (core_descriptor);
		_g_free0 (path);
		_g_object_unref0 (file);
		_games_uri_unref0 (file_uri);
		return NULL;
	}
	_tmp23_ = core_descriptor;
	_tmp24_ = games_libretro_uid_new (_tmp23_);
	uid = _tmp24_;
	_tmp25_ = core_descriptor;
	_tmp26_ = games_libretro_title_new (_tmp25_);
	title = _tmp26_;
	_tmp27_ = core_descriptor;
	_tmp28_ = games_libretro_icon_new (_tmp27_);
	icon = _tmp28_;
	_tmp29_ = games_dummy_cover_new ();
	cover = _tmp29_;
	_tmp30_ = core_descriptor;
	_tmp31_ = uid;
	_tmp32_ = title;
	_tmp33_ = games_retro_runner_new_for_core_descriptor (_tmp30_, (GamesUid*) _tmp31_, (GamesTitle*) _tmp32_);
	runner = _tmp33_;
	_tmp34_ = title;
	_tmp35_ = icon;
	_tmp36_ = cover;
	_tmp37_ = runner;
	_tmp38_ = games_generic_game_new ((GamesTitle*) _tmp34_, (GamesIcon*) _tmp35_, (GamesCover*) _tmp36_, (GamesRunner*) _tmp37_);
	result = (GamesGame*) _tmp38_;
	_g_object_unref0 (runner);
	_g_object_unref0 (cover);
	_g_object_unref0 (icon);
	_g_object_unref0 (title);
	_g_object_unref0 (uid);
	_g_object_unref0 (core_descriptor);
	_g_free0 (path);
	_g_object_unref0 (file);
	_games_uri_unref0 (file_uri);
	return result;
}


GamesLibretroPlugin* games_libretro_plugin_construct (GType object_type) {
	GamesLibretroPlugin * self = NULL;
	self = (GamesLibretroPlugin*) g_object_new (object_type, NULL);
	return self;
}


GamesLibretroPlugin* games_libretro_plugin_new (void) {
	return games_libretro_plugin_construct (GAMES_TYPE_LIBRETRO_PLUGIN);
}


static void games_libretro_plugin_class_init (GamesLibretroPluginClass * klass) {
	games_libretro_plugin_parent_class = g_type_class_peek_parent (klass);
}


static void games_libretro_plugin_games_plugin_interface_init (GamesPluginIface * iface) {
	games_libretro_plugin_games_plugin_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_uri_sources = (GamesUriSource** (*) (GamesPlugin *, int*)) games_libretro_plugin_real_get_uri_sources;
	iface->get_uri_game_factories = (GamesUriGameFactory** (*) (GamesPlugin *, int*)) games_libretro_plugin_real_get_uri_game_factories;
}


static void games_libretro_plugin_instance_init (GamesLibretroPlugin * self) {
}


GType games_libretro_plugin_get_type (void) {
	return games_libretro_plugin_type_id;
}


GType games_libretro_plugin_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (GamesLibretroPluginClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_libretro_plugin_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesLibretroPlugin), 0, (GInstanceInitFunc) games_libretro_plugin_instance_init, NULL };
	static const GInterfaceInfo games_plugin_info = { (GInterfaceInitFunc) games_libretro_plugin_games_plugin_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	games_libretro_plugin_type_id = g_type_module_register_type (module, G_TYPE_OBJECT, "GamesLibretroPlugin", &g_define_type_info, 0);
	g_type_module_add_interface (module, games_libretro_plugin_type_id, GAMES_TYPE_PLUGIN, &games_plugin_info);
	return games_libretro_plugin_type_id;
}


GType register_games_plugin (GTypeModule* module) {
	GType result = 0UL;
	g_return_val_if_fail (module != NULL, 0UL);
	games_libretro_icon_register_type (module);
	games_libretro_plugin_register_type (module);
	games_libretro_title_register_type (module);
	games_libretro_uid_register_type (module);
	games_libretro_uri_iterator_register_type (module);
	games_libretro_uri_source_register_type (module);
	result = GAMES_TYPE_LIBRETRO_PLUGIN;
	return result;
}



