<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Snowflake.Game;
using Newtonsoft.Json;
using Snowflake.Service;
using Snowflake.Utility;
using System.Data.SQLite;
namespace Snowflake.Emulator.Configuration
{
    public class ConfigurationFlagStore : BaseDatabase, IConfigurationFlagStore
    {
        public string EmulatorBridgeID { get; private set; }
        readonly string configurationFlagLocation;
        readonly IEmulatorBridge emulatorBridge;
        public ConfigurationFlagStore(IEmulatorBridge emulatorBridge)
            : base(Path.Combine(emulatorBridge.PluginDataPath, "flagscache.db"))
        {
            this.CreateDatabase();
            this.EmulatorBridgeID = emulatorBridge.PluginName;
            this.configurationFlagLocation = Path.Combine(emulatorBridge.PluginDataPath, "flagscache.db");
            this.AddDefaults(emulatorBridge.ConfigurationFlags);
            this.emulatorBridge = emulatorBridge;
        }
        private void CreateDatabase()
        {
            SQLiteConnection dbConnection = this.GetConnection();
            dbConnection.Open();
            var sqlCommand = new SQLiteCommand(@"CREATE TABLE IF NOT EXISTS flags(
                                                                flagKey TEXT PRIMARY KEY,
                                                                flagValue TEXT)", dbConnection);
            sqlCommand.ExecuteNonQuery();
            dbConnection.Close();
        }
        public void AddGame(IGameInfo gameInfo, IDictionary<string, string> flagValues)
        {


            var flagDb = this.GetConnection();
            flagDb.Open();
            foreach (KeyValuePair<string, string> flagPair in flagValues)
            {
                using (var sqliteCommand = new SQLiteCommand(@"INSERT OR REPLACE INTO flags VALUES(
                                          @flagKey,
                                          @flagValue)", flagDb))
                {
                    sqliteCommand.Parameters.AddWithValue("@flagKey", gameInfo.UUID + flagPair.Key);
                    sqliteCommand.Parameters.AddWithValue("@flagValue", flagPair.Value.ToString());
                    sqliteCommand.ExecuteNonQuery();
                }
            }
            flagDb.Close();
        }
        public void AddGame(IGameInfo gameInfo)
        {
            IDictionary<string, string> flagValues = new Dictionary<string, string>();
            this.AddGame(gameInfo, flagValues);
        }
        public dynamic GetValue(IGameInfo gameInfo, string key, ConfigurationFlagTypes type)
        {
            return this.GetValue(key, type, gameInfo.UUID, this.GetDefaultValue(key, type));
        }
        public void SetValue(IGameInfo gameInfo, string key, object value, ConfigurationFlagTypes type)
        {
            this.SetValue(key, value, type, gameInfo.UUID);
        }
        public dynamic GetDefaultValue(string key, ConfigurationFlagTypes type)
        {
            return this.GetValue(key, type, "default", this.emulatorBridge.ConfigurationFlags[key].DefaultValue);
        }
        public void SetDefaultValue(string key, object value, ConfigurationFlagTypes type)
        {
            this.SetValue(key, value, type, "default");
        }
        private void AddDefaults(IDictionary<string, IConfigurationFlag> configurationFlags)
        {<commit_msg>ConfigurationFlagStore: Fix AddGame bug thanks to unit tests<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Snowflake.Game;
using Newtonsoft.Json;
using Snowflake.Service;
using Snowflake.Utility;
using System.Data.SQLite;
namespace Snowflake.Emulator.Configuration
{
    public class ConfigurationFlagStore : BaseDatabase, IConfigurationFlagStore
    {
        public string EmulatorBridgeID { get; private set; }
        readonly string configurationFlagLocation;
        readonly IEmulatorBridge emulatorBridge;
        public ConfigurationFlagStore(IEmulatorBridge emulatorBridge)
            : base(Path.Combine(emulatorBridge.PluginDataPath, "flagscache.db"))
        {
            this.CreateDatabase();
            this.EmulatorBridgeID = emulatorBridge.PluginName;
            this.configurationFlagLocation = Path.Combine(emulatorBridge.PluginDataPath, "flagscache.db");
            this.AddDefaults(emulatorBridge.ConfigurationFlags);
            this.emulatorBridge = emulatorBridge;
        }
        private void CreateDatabase()
        {
            SQLiteConnection dbConnection = this.GetConnection();
            dbConnection.Open();
            var sqlCommand = new SQLiteCommand(@"CREATE TABLE IF NOT EXISTS flags(
                                                                flagKey TEXT PRIMARY KEY,
                                                                flagValue TEXT)", dbConnection);
            sqlCommand.ExecuteNonQuery();
            dbConnection.Close();
        }
        public void AddGame(IGameInfo gameInfo, IDictionary<string, string> flagValues)
        {


            var flagDb = this.GetConnection();
            flagDb.Open();
            foreach (KeyValuePair<string, string> flagPair in flagValues)
            {
                using (var sqliteCommand = new SQLiteCommand(@"INSERT OR REPLACE INTO flags VALUES(
                                          @flagKey,
                                          @flagValue)", flagDb))
                {
                    sqliteCommand.Parameters.AddWithValue("@flagKey", gameInfo.UUID + "-" + flagPair.Key);
                    sqliteCommand.Parameters.AddWithValue("@flagValue", flagPair.Value.ToString());
                    sqliteCommand.ExecuteNonQuery();
                }
            }
            flagDb.Close();
        }
        public void AddGame(IGameInfo gameInfo)
        {
            IDictionary<string, string> flagValues = new Dictionary<string, string>();
            this.AddGame(gameInfo, flagValues);
        }
        public dynamic GetValue(IGameInfo gameInfo, string key, ConfigurationFlagTypes type)
        {
            return this.GetValue(key, type, gameInfo.UUID, this.GetDefaultValue(key, type));
        }
        public void SetValue(IGameInfo gameInfo, string key, object value, ConfigurationFlagTypes type)
        {
            this.SetValue(key, value, type, gameInfo.UUID);
        }
        public dynamic GetDefaultValue(string key, ConfigurationFlagTypes type)
        {
            return this.GetValue(key, type, "default", this.emulatorBridge.ConfigurationFlags[key].DefaultValue);
        }
        public void SetDefaultValue(string key, object value, ConfigurationFlagTypes type)
        {
            this.SetValue(key, value, type, "default");
        }
        private void AddDefaults(IDictionary<string, IConfigurationFlag> configurationFlags)
        {
{'fraction_non_alphanumeric': 0.08062437347844766, 'fraction_numerical': 0.0, 'mean_word_length': 2.122038444345105, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '528877', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1630, 'n_words': 377}