<commit_before>﻿using Newtonsoft.Json;
using Playnite.Common.System;
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
using TwitchLibrary.Models;
using TwitchLibrary.Services;

namespace TwitchLibrary
{
    public class TwitchLibrary : ILibraryPlugin
    {
        private ILogger logger = LogManager.GetLogger();
        private readonly IPlayniteAPI playniteApi;
        internal readonly string TokensPath;

        internal TwitchLibrarySettings LibrarySettings
        {
            get => (TwitchLibrarySettings)Settings;
        }

        internal TwitchLoginData LoginData
        {
            get
            {
                if (!File.Exists(TokensPath))
                {
                    return null;
                }

                try
                {
                    return JsonConvert.DeserializeObject<TwitchLoginData>(File.ReadAllText(TokensPath));
                }
                catch (Exception e) when (!Environment.IsDebugBuild)
                {
                    logger.Error(e, "Failed to load twitch login information.");
                    return null;
                }
            }
        }

        public TwitchLibrary(IPlayniteAPI api)
        {
            playniteApi = api;
            LibraryIcon = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Resources\twitchicon.png");
            Settings = new TwitchLibrarySettings(this, playniteApi);
            TokensPath = Path.Combine(api.GetPluginUserDataPath(this), "tokens.json");
        }

        public static GameAction GetPlayAction(string gameId)
        {
            return new GameAction()
            {
                Type = GameActionType.URL,
                Path = $"twitch://fuel-launch/{gameId}",
                IsHandledByPlugin = true
            };
        }

        internal Dictionary<string, Game> GetInstalledGames()
        {
            var games = new Dictionary<string, Game>();
            var programs = Programs.GetUnistallProgramsList();
            foreach (var program in programs)
            {
                if (string.IsNullOrEmpty(program.UninstallString) || program.UninstallString.IndexOf("TwitchGameRemover", StringComparison.InvariantCultureIgnoreCase) < 0)
                {
                    continue;
                }

                if (!Directory.Exists(program.InstallLocation))
                {
                    continue;
                }

                var gameId = program.RegistryKeyName.Trim(new char[] { '{', '}' }).ToLower();
                var game = new Game()
                {
                    InstallDirectory = Paths.FixSeparators(program.InstallLocation),
                    GameId = gameId,
                    PluginId = Id,
                    Source = "Twitch",
                    Name = program.DisplayName,
                    State = new GameState() { Installed = true },
                    PlayAction = GetPlayAction(gameId)
                };

                games.Add(game.GameId, game);
            }

            return games;
        }

        public List<Game> GetLibraryGames()
        {
            var login = LoginData;
            if (login == null)
            {
                throw new Exception("User is not logged in.");
            }

            var games = new List<Game>();
            var libraryGames = AmazonEntitlementClient.GetAccountEntitlements(login.AccountId, login.AccessToken);
            foreach (var item in libraryGames)
            {<commit_msg>Twitch importing non-game items<commit_after>﻿using Newtonsoft.Json;
using Playnite.Common.System;
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
using TwitchLibrary.Models;
using TwitchLibrary.Services;

namespace TwitchLibrary
{
    public class TwitchLibrary : ILibraryPlugin
    {
        private ILogger logger = LogManager.GetLogger();
        private readonly IPlayniteAPI playniteApi;
        internal readonly string TokensPath;

        internal TwitchLibrarySettings LibrarySettings
        {
            get => (TwitchLibrarySettings)Settings;
        }

        internal TwitchLoginData LoginData
        {
            get
            {
                if (!File.Exists(TokensPath))
                {
                    return null;
                }

                try
                {
                    return JsonConvert.DeserializeObject<TwitchLoginData>(File.ReadAllText(TokensPath));
                }
                catch (Exception e) when (!Environment.IsDebugBuild)
                {
                    logger.Error(e, "Failed to load twitch login information.");
                    return null;
                }
            }
        }

        public TwitchLibrary(IPlayniteAPI api)
        {
            playniteApi = api;
            LibraryIcon = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Resources\twitchicon.png");
            Settings = new TwitchLibrarySettings(this, playniteApi);
            TokensPath = Path.Combine(api.GetPluginUserDataPath(this), "tokens.json");
        }

        public static GameAction GetPlayAction(string gameId)
        {
            return new GameAction()
            {
                Type = GameActionType.URL,
                Path = $"twitch://fuel-launch/{gameId}",
                IsHandledByPlugin = true
            };
        }

        internal Dictionary<string, Game> GetInstalledGames()
        {
            var games = new Dictionary<string, Game>();
            var programs = Programs.GetUnistallProgramsList();
            foreach (var program in programs)
            {
                if (string.IsNullOrEmpty(program.UninstallString) || program.UninstallString.IndexOf("TwitchGameRemover", StringComparison.InvariantCultureIgnoreCase) < 0)
                {
                    continue;
                }

                if (!Directory.Exists(program.InstallLocation))
                {
                    continue;
                }

                var gameId = program.RegistryKeyName.Trim(new char[] { '{', '}' }).ToLower();
                var game = new Game()
                {
                    InstallDirectory = Paths.FixSeparators(program.InstallLocation),
                    GameId = gameId,
                    PluginId = Id,
                    Source = "Twitch",
                    Name = program.DisplayName,
                    State = new GameState() { Installed = true },
                    PlayAction = GetPlayAction(gameId)
                };

                games.Add(game.GameId, game);
            }

            return games;
        }

        public List<Game> GetLibraryGames()
        {
            var login = LoginData;
            if (login == null)
            {
                throw new Exception("User is not logged in.");
            }

            var games = new List<Game>();
            var libraryGames = AmazonEntitlementClient.GetAccountEntitlements(login.AccountId, login.AccessToken);
            foreach (var item in libraryGames)
            {
                if (item.product.productLine != "Twitch:FuelGame")
                {
                    continue;
                }

{'fraction_non_alphanumeric': 0.08389348500517063, 'fraction_numerical': 0.0002585315408479835, 'mean_word_length': 1.5902243053230667, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1206489', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1840, 'n_words': 399}