/* steam-uri-source.c generated by valac 0.36.6.6-c7bee, the Vala compiler
 * generated from steam-uri-source.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 <glib/gstdio.h>


#define GAMES_TYPE_STEAM_URI_SOURCE (games_steam_uri_source_get_type ())
#define GAMES_STEAM_URI_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_STEAM_URI_SOURCE, GamesSteamUriSource))
#define GAMES_STEAM_URI_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_STEAM_URI_SOURCE, GamesSteamUriSourceClass))
#define GAMES_IS_STEAM_URI_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_STEAM_URI_SOURCE))
#define GAMES_IS_STEAM_URI_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_STEAM_URI_SOURCE))
#define GAMES_STEAM_URI_SOURCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_STEAM_URI_SOURCE, GamesSteamUriSourceClass))

typedef struct _GamesSteamUriSource GamesSteamUriSource;
typedef struct _GamesSteamUriSourceClass GamesSteamUriSourceClass;
typedef struct _GamesSteamUriSourcePrivate GamesSteamUriSourcePrivate;

#define GAMES_TYPE_STEAM_REGISTRY (games_steam_registry_get_type ())
#define GAMES_STEAM_REGISTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_STEAM_REGISTRY, GamesSteamRegistry))
#define GAMES_STEAM_REGISTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_STEAM_REGISTRY, GamesSteamRegistryClass))
#define GAMES_IS_STEAM_REGISTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_STEAM_REGISTRY))
#define GAMES_IS_STEAM_REGISTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_STEAM_REGISTRY))
#define GAMES_STEAM_REGISTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_STEAM_REGISTRY, GamesSteamRegistryClass))

typedef struct _GamesSteamRegistry GamesSteamRegistry;
typedef struct _GamesSteamRegistryClass GamesSteamRegistryClass;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _games_steam_registry_unref0(var) ((var == NULL) ? NULL : (var = (games_steam_registry_unref (var), NULL)))

#define GAMES_TYPE_STEAM_URI_ITERATOR (games_steam_uri_iterator_get_type ())
#define GAMES_STEAM_URI_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_STEAM_URI_ITERATOR, GamesSteamUriIterator))
#define GAMES_STEAM_URI_ITERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_STEAM_URI_ITERATOR, GamesSteamUriIteratorClass))
#define GAMES_IS_STEAM_URI_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_STEAM_URI_ITERATOR))
#define GAMES_IS_STEAM_URI_ITERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_STEAM_URI_ITERATOR))
#define GAMES_STEAM_URI_ITERATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_STEAM_URI_ITERATOR, GamesSteamUriIteratorClass))

typedef struct _GamesSteamUriIterator GamesSteamUriIterator;
typedef struct _GamesSteamUriIteratorClass GamesSteamUriIteratorClass;

struct _GamesSteamUriSource {
	GObject parent_instance;
	GamesSteamUriSourcePrivate * priv;
};

struct _GamesSteamUriSourceClass {
	GObjectClass parent_class;
};

struct _GamesSteamUriSourcePrivate {
	gchar** directories;
	gint directories_length1;
	gint _directories_size_;
};


static gpointer games_steam_uri_source_parent_class = NULL;
static GamesUriSourceIface * games_steam_uri_source_games_uri_source_parent_iface = NULL;
static GType games_steam_uri_source_type_id = 0;

GType games_steam_uri_source_get_type (void) G_GNUC_CONST;
#define GAMES_STEAM_URI_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GAMES_TYPE_STEAM_URI_SOURCE, GamesSteamUriSourcePrivate))
enum  {
	GAMES_STEAM_URI_SOURCE_0_PROPERTY
};
#define GAMES_STEAM_URI_SOURCE_REGISTRY_PATH "/.steam/registry.vdf"
#define GAMES_STEAM_URI_SOURCE_DEFAULT_INSTALL_DIR_SYMLINK "/.steam/steam"
#define GAMES_STEAM_URI_SOURCE_LIBRARY_DIRS_REG "/libraryfolders.vdf"
GamesSteamUriSource* games_steam_uri_source_new (GError** error);
GamesSteamUriSource* games_steam_uri_source_construct (GType object_type, GError** error);
gpointer games_steam_registry_ref (gpointer instance);
void games_steam_registry_unref (gpointer instance);
GParamSpec* games_param_spec_steam_registry (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void games_value_set_steam_registry (GValue* value, gpointer v_object);
void games_value_take_steam_registry (GValue* value, gpointer v_object);
gpointer games_value_get_steam_registry (const GValue* value);
GType games_steam_registry_get_type (void) G_GNUC_CONST;
GamesSteamRegistry* games_steam_registry_new (const gchar* appmanifest_path, GError** error);
GamesSteamRegistry* games_steam_registry_construct (GType object_type, const gchar* appmanifest_path, GError** error);
gchar* games_steam_registry_get_data (GamesSteamRegistry* self, gchar** path, int path_length1);
static void games_steam_uri_source_add_library (GamesSteamUriSource* self, const gchar* library);
gchar** games_steam_registry_get_children (GamesSteamRegistry* self, gchar** path, int path_length1, int* result_length1);
static GamesUriIterator* games_steam_uri_source_real_iterator (GamesUriSource* base);
GamesSteamUriIterator* games_steam_uri_iterator_new (gchar** directories, int directories_length1);
GamesSteamUriIterator* games_steam_uri_iterator_construct (GType object_type, gchar** directories, int directories_length1);
GType games_steam_uri_iterator_get_type (void) G_GNUC_CONST;
static void _vala_array_add3 (gchar** * array, int* length, int* size, gchar* value);
static void games_steam_uri_source_finalize (GObject * obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);

static const gchar* GAMES_STEAM_URI_SOURCE_STEAMAPPS_DIRS[2] = {"/SteamApps", "/steamapps"};
static const gchar* GAMES_STEAM_URI_SOURCE_INSTALL_PATH_REGISTRY_PATH[6] = {"Registry", "HKLM", "Software", "Valve", "Steam", "InstallPath"};
static GRegex* _tmp_regex_0 = NULL;

static inline GRegex* _thread_safe_regex_init (GRegex** re, const gchar * pattern, GRegexMatchFlags match_options) {
	if (g_once_init_enter ((volatile gsize*) re)) {
		GRegex* val = g_regex_new (pattern, match_options, 0, NULL);
		g_once_init_leave ((volatile gsize*) re, (gsize) val);
	}
	return *re;
}


GamesSteamUriSource* games_steam_uri_source_construct (GType object_type, GError** error) {
	GamesSteamUriSource * self = NULL;
	gchar** _tmp0_;
	gchar* home = NULL;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* registry_path = NULL;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	GamesSteamRegistry* registry = NULL;
	const gchar* _tmp5_;
	GamesSteamRegistry* _tmp6_;
	gchar* install_path = NULL;
	GamesSteamRegistry* _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	const gchar* _tmp12_;
	GError * _inner_error_ = NULL;
	self = (GamesSteamUriSource*) g_object_new (object_type, NULL);
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	self->priv->directories = (_vala_array_free (self->priv->directories, self->priv->directories_length1, (GDestroyNotify) g_free), NULL);
	self->priv->directories = _tmp0_;
	self->priv->directories_length1 = 0;
	self->priv->_directories_size_ = self->priv->directories_length1;
	_tmp1_ = g_get_home_dir ();
	_tmp2_ = g_strdup (_tmp1_);
	home = _tmp2_;
	_tmp3_ = home;
	_tmp4_ = g_strconcat (_tmp3_, GAMES_STEAM_URI_SOURCE_REGISTRY_PATH, NULL);
	registry_path = _tmp4_;
	_tmp5_ = registry_path;
	_tmp6_ = games_steam_registry_new (_tmp5_, &_inner_error_);
	registry = _tmp6_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_free0 (registry_path);
		_g_free0 (home);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp7_ = registry;
	_tmp8_ = games_steam_registry_get_data (_tmp7_, GAMES_STEAM_URI_SOURCE_INSTALL_PATH_REGISTRY_PATH, G_N_ELEMENTS (GAMES_STEAM_URI_SOURCE_INSTALL_PATH_REGISTRY_PATH));
	install_path = _tmp8_;
	_tmp9_ = home;
	_tmp10_ = g_strconcat (_tmp9_, GAMES_STEAM_URI_SOURCE_DEFAULT_INSTALL_DIR_SYMLINK, NULL);
	_tmp11_ = _tmp10_;
	games_steam_uri_source_add_library (self, _tmp11_);
	_g_free0 (_tmp11_);
	_tmp12_ = install_path;
	games_steam_uri_source_add_library (self, _tmp12_);
	{
		const gchar** steamapps_dir_collection = NULL;
		gint steamapps_dir_collection_length1 = 0;
		gint _steamapps_dir_collection_size_ = 0;
		gint steamapps_dir_it = 0;
		steamapps_dir_collection = GAMES_STEAM_URI_SOURCE_STEAMAPPS_DIRS;
		steamapps_dir_collection_length1 = G_N_ELEMENTS (GAMES_STEAM_URI_SOURCE_STEAMAPPS_DIRS);
		for (steamapps_dir_it = 0; steamapps_dir_it < G_N_ELEMENTS (GAMES_STEAM_URI_SOURCE_STEAMAPPS_DIRS); steamapps_dir_it = steamapps_dir_it + 1) {
			const gchar* steamapps_dir = NULL;
			steamapps_dir = steamapps_dir_collection[steamapps_dir_it];
			{
				gchar* install_steamapps_dir = NULL;
				const gchar* _tmp13_;
				const gchar* _tmp14_;
				gchar* _tmp15_;
				GFile* file = NULL;
				const gchar* _tmp16_;
				GFile* _tmp17_;
				GFile* _tmp18_;
				gboolean _tmp19_;
				gchar* library_reg_path = NULL;
				const gchar* _tmp20_;
				gchar* _tmp21_;
				GamesSteamRegistry* library_reg = NULL;
				const gchar* _tmp22_;
				GamesSteamRegistry* _tmp23_;
				GamesSteamRegistry* _tmp24_;
				gchar* _tmp25_;
				gchar** _tmp26_;
				gchar** _tmp27_;
				gint _tmp27__length1;
				gint _tmp28_;
				gchar** _tmp29_;
				gchar** _tmp30_;
				gint _tmp30__length1;
				_tmp13_ = install_path;
				_tmp14_ = steamapps_dir;
				_tmp15_ = g_strconcat (_tmp13_, _tmp14_, NULL);
				install_steamapps_dir = _tmp15_;
				_tmp16_ = install_steamapps_dir;
				_tmp17_ = g_file_new_for_path (_tmp16_);
				file = _tmp17_;
				_tmp18_ = file;
				_tmp19_ = g_file_query_exists (_tmp18_, NULL);
				if (!_tmp19_) {
					_g_object_unref0 (file);
					_g_free0 (install_steamapps_dir);
					continue;
				}
				_tmp20_ = install_steamapps_dir;
				_tmp21_ = g_strconcat (_tmp20_, GAMES_STEAM_URI_SOURCE_LIBRARY_DIRS_REG, NULL);
				library_reg_path = _tmp21_;
				_tmp22_ = library_reg_path;
				_tmp23_ = games_steam_registry_new (_tmp22_, &_inner_error_);
				library_reg = _tmp23_;
				if (G_UNLIKELY (_inner_error_ != NULL)) {
					g_propagate_error (error, _inner_error_);
					_g_free0 (library_reg_path);
					_g_object_unref0 (file);
					_g_free0 (install_steamapps_dir);
					_g_free0 (install_path);
					_games_steam_registry_unref0 (registry);
					_g_free0 (registry_path);
					_g_free0 (home);
					_g_object_unref0 (self);
					return NULL;
				}
				_tmp24_ = library_reg;
				_tmp25_ = g_strdup ("LibraryFolders");
				_tmp26_ = g_new0 (gchar*, 1 + 1);
				_tmp26_[0] = _tmp25_;
				_tmp27_ = _tmp26_;
				_tmp27__length1 = 1;
				_tmp29_ = games_steam_registry_get_children (_tmp24_, _tmp27_, 1, &_tmp28_);
				_tmp30_ = _tmp29_;
				_tmp30__length1 = _tmp28_;
				_tmp27_ = (_vala_array_free (_tmp27_, _tmp27__length1, (GDestroyNotify) g_free), NULL);
				{
					gchar** child_collection = NULL;
					gint child_collection_length1 = 0;
					gint _child_collection_size_ = 0;
					gint child_it = 0;
					child_collection = _tmp30_;
					child_collection_length1 = _tmp30__length1;
					for (child_it = 0; child_it < _tmp30__length1; child_it = child_it + 1) {
						gchar* _tmp31_;
						gchar* child = NULL;
						_tmp31_ = g_strdup (child_collection[child_it]);
						child = _tmp31_;
						{
							const gchar* _tmp32_;
							gboolean _tmp33_;
							_tmp32_ = child;
							_tmp33_ = g_regex_match (_thread_safe_regex_init (&_tmp_regex_0, "^\\d+$", 0), _tmp32_, 0, NULL);
							if (_tmp33_) {
								GamesSteamRegistry* _tmp34_;
								gchar* _tmp35_;
								const gchar* _tmp36_;
								gchar* _tmp37_;
								gchar** _tmp38_;
								gchar** _tmp39_;
								gint _tmp39__length1;
								gchar* _tmp40_;
								gchar* _tmp41_;
								_tmp34_ = library_reg;
								_tmp35_ = g_strdup ("LibraryFolders");
								_tmp36_ = child;
								_tmp37_ = g_strdup (_tmp36_);
								_tmp38_ = g_new0 (gchar*, 2 + 1);
								_tmp38_[0] = _tmp35_;
								_tmp38_[1] = _tmp37_;
								_tmp39_ = _tmp38_;
								_tmp39__length1 = 2;
								_tmp40_ = games_steam_registry_get_data (_tmp34_, _tmp39_, 2);
								_tmp41_ = _tmp40_;
								games_steam_uri_source_add_library (self, _tmp41_);
								_g_free0 (_tmp41_);
								_tmp39_ = (_vala_array_free (_tmp39_, _tmp39__length1, (GDestroyNotify) g_free), NULL);
							}
							_g_free0 (child);
						}
					}
					child_collection = (_vala_array_free (child_collection, child_collection_length1, (GDestroyNotify) g_free), NULL);
				}
				_games_steam_registry_unref0 (library_reg);
				_g_free0 (library_reg_path);
				_g_object_unref0 (file);
				_g_free0 (install_steamapps_dir);
			}
		}
	}
	_g_free0 (install_path);
	_games_steam_registry_unref0 (registry);
	_g_free0 (registry_path);
	_g_free0 (home);
	return self;
}


GamesSteamUriSource* games_steam_uri_source_new (GError** error) {
	return games_steam_uri_source_construct (GAMES_TYPE_STEAM_URI_SOURCE, error);
}


static GamesUriIterator* games_steam_uri_source_real_iterator (GamesUriSource* base) {
	GamesSteamUriSource * self;
	GamesUriIterator* result = NULL;
	gchar** _tmp0_;
	gint _tmp0__length1;
	GamesSteamUriIterator* _tmp1_;
	self = (GamesSteamUriSource*) base;
	_tmp0_ = self->priv->directories;
	_tmp0__length1 = self->priv->directories_length1;
	_tmp1_ = games_steam_uri_iterator_new (_tmp0_, _tmp0__length1);
	result = (GamesUriIterator*) _tmp1_;
	return result;
}


static void _vala_array_add3 (gchar** * array, int* length, int* size, gchar* value) {
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}


static void games_steam_uri_source_add_library (GamesSteamUriSource* self, const gchar* library) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (library != NULL);
	{
		const gchar** steamapps_dir_collection = NULL;
		gint steamapps_dir_collection_length1 = 0;
		gint _steamapps_dir_collection_size_ = 0;
		gint steamapps_dir_it = 0;
		steamapps_dir_collection = GAMES_STEAM_URI_SOURCE_STEAMAPPS_DIRS;
		steamapps_dir_collection_length1 = G_N_ELEMENTS (GAMES_STEAM_URI_SOURCE_STEAMAPPS_DIRS);
		for (steamapps_dir_it = 0; steamapps_dir_it < G_N_ELEMENTS (GAMES_STEAM_URI_SOURCE_STEAMAPPS_DIRS); steamapps_dir_it = steamapps_dir_it + 1) {
			const gchar* steamapps_dir = NULL;
			steamapps_dir = steamapps_dir_collection[steamapps_dir_it];
			{
				gchar* library_steamapps_dir = NULL;
				const gchar* _tmp0_;
				const gchar* _tmp1_;
				gchar* _tmp2_;
				const gchar* _tmp3_;
				gboolean _tmp4_;
				_tmp0_ = library;
				_tmp1_ = steamapps_dir;
				_tmp2_ = g_strconcat (_tmp0_, _tmp1_, NULL);
				library_steamapps_dir = _tmp2_;
				_tmp3_ = library_steamapps_dir;
				_tmp4_ = g_file_test (_tmp3_, G_FILE_TEST_EXISTS);
				if (_tmp4_) {
					gchar** _tmp5_;
					gint _tmp5__length1;
					const gchar* _tmp6_;
					gchar* _tmp7_;
					_tmp5_ = self->priv->directories;
					_tmp5__length1 = self->priv->directories_length1;
					_tmp6_ = library_steamapps_dir;
					_tmp7_ = g_strdup (_tmp6_);
					_vala_array_add3 (&self->priv->directories, &self->priv->directories_length1, &self->priv->_directories_size_, _tmp7_);
				}
				_g_free0 (library_steamapps_dir);
			}
		}
	}
}


static void games_steam_uri_source_class_init (GamesSteamUriSourceClass * klass) {
	games_steam_uri_source_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GamesSteamUriSourcePrivate));
	G_OBJECT_CLASS (klass)->finalize = games_steam_uri_source_finalize;
}


static void games_steam_uri_source_games_uri_source_interface_init (GamesUriSourceIface * iface) {
	games_steam_uri_source_games_uri_source_parent_iface = g_type_interface_peek_parent (iface);
	iface->iterator = (GamesUriIterator* (*) (GamesUriSource *)) games_steam_uri_source_real_iterator;
}


static void games_steam_uri_source_instance_init (GamesSteamUriSource * self) {
	self->priv = GAMES_STEAM_URI_SOURCE_GET_PRIVATE (self);
}


static void games_steam_uri_source_finalize (GObject * obj) {
	GamesSteamUriSource * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_STEAM_URI_SOURCE, GamesSteamUriSource);
	self->priv->directories = (_vala_array_free (self->priv->directories, self->priv->directories_length1, (GDestroyNotify) g_free), NULL);
	G_OBJECT_CLASS (games_steam_uri_source_parent_class)->finalize (obj);
}


GType games_steam_uri_source_get_type (void) {
	return games_steam_uri_source_type_id;
}


GType games_steam_uri_source_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (GamesSteamUriSourceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_steam_uri_source_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesSteamUriSource), 0, (GInstanceInitFunc) games_steam_uri_source_instance_init, NULL };
	static const GInterfaceInfo games_uri_source_info = { (GInterfaceInitFunc) games_steam_uri_source_games_uri_source_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	games_steam_uri_source_type_id = g_type_module_register_type (module, G_TYPE_OBJECT, "GamesSteamUriSource", &g_define_type_info, 0);
	g_type_module_add_interface (module, games_steam_uri_source_type_id, GAMES_TYPE_URI_SOURCE, &games_uri_source_info);
	return games_steam_uri_source_type_id;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



