/* libretro-uri-iterator.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from libretro-uri-iterator.vala, do not modify */

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

#include "games-libretro-plugin.h"
#include <retro-gtk.h>
#include "gnome-games.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib-object.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	GAMES_LIBRETRO_URI_ITERATOR_0_PROPERTY,
	GAMES_LIBRETRO_URI_ITERATOR_NUM_PROPERTIES
};
static GParamSpec* games_libretro_uri_iterator_properties[GAMES_LIBRETRO_URI_ITERATOR_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _games_uri_unref0(var) ((var == NULL) ? NULL : (var = (games_uri_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _GamesLibretroUriIteratorPrivate {
	RetroModuleIterator* iterator;
	GamesUri* uri;
};

static gint GamesLibretroUriIterator_private_offset;
static gpointer games_libretro_uri_iterator_parent_class = NULL;
static GamesUriIteratorIface * games_libretro_uri_iterator_games_uri_iterator_parent_iface = NULL;
static GType games_libretro_uri_iterator_type_id = 0;

static GamesUri* games_libretro_uri_iterator_real_get (GamesUriIterator* base);
static gboolean games_libretro_uri_iterator_real_next (GamesUriIterator* base);
static GObject * games_libretro_uri_iterator_constructor (GType type,
                                                   guint n_construct_properties,
                                                   GObjectConstructParam * construct_properties);
static void games_libretro_uri_iterator_finalize (GObject * obj);

static inline gpointer
games_libretro_uri_iterator_get_instance_private (GamesLibretroUriIterator* self)
{
	return G_STRUCT_MEMBER_P (self, GamesLibretroUriIterator_private_offset);
}

static gpointer
_games_uri_ref0 (gpointer self)
{
	return self ? games_uri_ref (self) : NULL;
}

static GamesUri*
games_libretro_uri_iterator_real_get (GamesUriIterator* base)
{
	GamesLibretroUriIterator * self;
	GamesUri* _tmp0_;
	GamesUri* _tmp1_;
	GamesUri* result;
	self = (GamesLibretroUriIterator*) base;
	_tmp0_ = self->priv->uri;
	_tmp1_ = _games_uri_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

static gboolean
games_libretro_uri_iterator_real_next (GamesUriIterator* base)
{
	GamesLibretroUriIterator * self;
	GError* _inner_error0_ = NULL;
	gboolean result;
	self = (GamesLibretroUriIterator*) base;
	while (TRUE) {
		RetroModuleIterator* _tmp0_;
		RetroCoreDescriptor* core_descriptor = NULL;
		RetroModuleIterator* _tmp1_;
		RetroCoreDescriptor* _tmp2_;
		gchar* string_uri = NULL;
		RetroCoreDescriptor* _tmp8_;
		gchar* _tmp9_;
		const gchar* _tmp10_;
		const gchar* _tmp11_;
		gchar* _tmp12_;
		gchar* _tmp13_;
		GamesUri* _tmp14_;
		_tmp0_ = self->priv->iterator;
		if (!retro_module_iterator_next (_tmp0_)) {
			break;
		}
		_tmp1_ = self->priv->iterator;
		_tmp2_ = retro_module_iterator_get (_tmp1_);
		core_descriptor = _tmp2_;
		{
			gboolean _tmp3_ = FALSE;
			RetroCoreDescriptor* _tmp4_;
			_tmp4_ = core_descriptor;
			_tmp3_ = retro_core_descriptor_get_is_game (_tmp4_, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
			if (!_tmp3_) {
				_g_object_unref0 (core_descriptor);
				continue;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* e = NULL;
			GError* _tmp5_;
			const gchar* _tmp6_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp5_ = e;
			_tmp6_ = _tmp5_->message;
			g_debug ("libretro-uri-iterator.vala:25: %s", _tmp6_);
			_g_error_free0 (e);
			_g_object_unref0 (core_descriptor);
			continue;
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp7_ = FALSE;
			_g_object_unref0 (core_descriptor);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return _tmp7_;
		}
		_tmp8_ = core_descriptor;
		_tmp9_ = retro_core_descriptor_get_uri (_tmp8_);
		string_uri = _tmp9_;
		_tmp10_ = string_uri;
		_tmp11_ = string_to_string (_tmp10_);
		_tmp12_ = g_strconcat ("libretro+", _tmp11_, NULL);
		_tmp13_ = _tmp12_;
		_tmp14_ = games_uri_new (_tmp13_);
		_games_uri_unref0 (self->priv->uri);
		self->priv->uri = _tmp14_;
		_g_free0 (_tmp13_);
		result = TRUE;
		_g_free0 (string_uri);
		_g_object_unref0 (core_descriptor);
		return result;
	}
	_games_uri_unref0 (self->priv->uri);
	self->priv->uri = NULL;
	result = FALSE;
	return result;
}

GamesLibretroUriIterator*
games_libretro_uri_iterator_construct (GType object_type)
{
	GamesLibretroUriIterator * self = NULL;
	self = (GamesLibretroUriIterator*) g_object_new (object_type, NULL);
	return self;
}

GamesLibretroUriIterator*
games_libretro_uri_iterator_new (void)
{
	return games_libretro_uri_iterator_construct (GAMES_TYPE_LIBRETRO_URI_ITERATOR);
}

static GObject *
games_libretro_uri_iterator_constructor (GType type,
                                         guint n_construct_properties,
                                         GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GamesLibretroUriIterator * self;
	RetroModuleQuery* modules = NULL;
	RetroModuleQuery* _tmp0_;
	RetroModuleQuery* _tmp1_;
	RetroModuleIterator* _tmp2_;
	parent_class = G_OBJECT_CLASS (games_libretro_uri_iterator_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_LIBRETRO_URI_ITERATOR, GamesLibretroUriIterator);
	_tmp0_ = retro_module_query_new (TRUE);
	modules = _tmp0_;
	_tmp1_ = modules;
	_tmp2_ = retro_module_query_iterator (_tmp1_);
	_g_object_unref0 (self->priv->iterator);
	self->priv->iterator = _tmp2_;
	_games_uri_unref0 (self->priv->uri);
	self->priv->uri = NULL;
	_g_object_unref0 (modules);
	return obj;
}

static void
games_libretro_uri_iterator_class_init (GamesLibretroUriIteratorClass * klass,
                                        gpointer klass_data)
{
	games_libretro_uri_iterator_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesLibretroUriIterator_private_offset);
	G_OBJECT_CLASS (klass)->constructor = games_libretro_uri_iterator_constructor;
	G_OBJECT_CLASS (klass)->finalize = games_libretro_uri_iterator_finalize;
}

static void
games_libretro_uri_iterator_games_uri_iterator_interface_init (GamesUriIteratorIface * iface,
                                                               gpointer iface_data)
{
	games_libretro_uri_iterator_games_uri_iterator_parent_iface = g_type_interface_peek_parent (iface);
	iface->get = (GamesUri* (*) (GamesUriIterator*)) games_libretro_uri_iterator_real_get;
	iface->next = (gboolean (*) (GamesUriIterator*)) games_libretro_uri_iterator_real_next;
}

static void
games_libretro_uri_iterator_instance_init (GamesLibretroUriIterator * self,
                                           gpointer klass)
{
	self->priv = games_libretro_uri_iterator_get_instance_private (self);
}

static void
games_libretro_uri_iterator_finalize (GObject * obj)
{
	GamesLibretroUriIterator * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_LIBRETRO_URI_ITERATOR, GamesLibretroUriIterator);
	_g_object_unref0 (self->priv->iterator);
	_games_uri_unref0 (self->priv->uri);
	G_OBJECT_CLASS (games_libretro_uri_iterator_parent_class)->finalize (obj);
}

GType
games_libretro_uri_iterator_get_type (void)
{
	return games_libretro_uri_iterator_type_id;
}

GType
games_libretro_uri_iterator_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesLibretroUriIteratorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_libretro_uri_iterator_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesLibretroUriIterator), 0, (GInstanceInitFunc) games_libretro_uri_iterator_instance_init, NULL };
	static const GInterfaceInfo games_uri_iterator_info = { (GInterfaceInitFunc) games_libretro_uri_iterator_games_uri_iterator_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	games_libretro_uri_iterator_type_id = g_type_module_register_type (module, G_TYPE_OBJECT, "GamesLibretroUriIterator", &g_define_type_info, 0);
	g_type_module_add_interface (module, games_libretro_uri_iterator_type_id, GAMES_TYPE_URI_ITERATOR, &games_uri_iterator_info);
	GamesLibretroUriIterator_private_offset = sizeof (GamesLibretroUriIteratorPrivate);
	return games_libretro_uri_iterator_type_id;
}

