Exemplo n.º 1
0
        private void Open()
        {
            string fileJson = FileAccess.LoadString(_root, false);

            if (string.IsNullOrEmpty(fileJson))
            {
                _items = new Dictionary <string, object>();

                return;
            }

            string decryptedJson;

            try
            {
                decryptedJson = Cryptography.Decrypt(fileJson, _settings.SecurityMode, _settings.Password);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Decryption failed", e);
            }

            try
            {
                _items = JsonSerialiser.Deserialise <Dictionary <string, object> >(decryptedJson) ?? new Dictionary <string, object>();
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Failed to deserialise json", e);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads the contents of the root into the specified object using the specified settings
        /// </summary>
        /// <typeparam name="T">The type of object to load</typeparam>
        /// <param name="root">The root this object was saved under</param>
        /// <param name="settings">Settings</param>
        /// <returns>The object that was loaded</returns>
        public static T Load <T>(string root, QuickSaveSettings settings)
        {
            string fileJson = FileAccess.LoadString(root, false);

            if (string.IsNullOrEmpty(fileJson))
            {
                throw new QuickSaveException("File either does not exist or is empty");
            }

            string decryptedJson;

            try
            {
                decryptedJson = Cryptography.Decrypt(fileJson, settings.SecurityMode, settings.Password);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Decryption failed", e);
            }

            try
            {
                return(JsonSerialiser.Deserialise <T>(decryptedJson));
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Failed to deserialise json", e);
            }
        }
        public WebhookResponse CreateWebhook(string access_token, string owner, string repo)
        {
            // 不是自己的仓库,不做任何处理
            if (!repo.StartsWith(owner))
            {
                return(null);
            }

            url = string.Format("https://api.github.com/repos/{0}/hooks", repo);
            string token    = string.Format("access_token={0}", access_token);
            string response = HttpService.HttpPostWebhook(url, access_token);   // 使用Header传递token

            //string response = HttpService.HttpPostWebhook(url + "?" + token);   // 使用路径传递token

            try
            {
                JsonSerialiser jsonSerialiser = new JsonSerialiser();
                var            webhook        = jsonSerialiser.Deserialise <WebhookResponse>(response);

                if (webhook.Id == null)
                {
                    return(null);
                }

                return(webhook);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Attempts to load an object from a root under the specified key using the specified settings
        /// </summary>
        /// <typeparam name="T">The type of object to load</typeparam>
        /// <param name="root">The root this object was saved under</param>
        /// <param name="key">The key this object was saved under</param>
        /// <param name="settings">Settings</param>
        /// <param name="result">The object that was loaded</param>
        /// <returns>Was the load successful</returns>
        public static bool TryLoad <T>(string root, string key, QuickSaveSettings settings, out T result)
        {
            result = default(T);

            try
            {
                string fileJson = FileAccess.LoadString(root, false);

                if (string.IsNullOrEmpty(fileJson))
                {
                    return(false);
                }

                string decryptedJson = Cryptography.Decrypt(fileJson, settings.SecurityMode, settings.Password);

                Dictionary <string, object> items = JsonSerialiser.Deserialise <Dictionary <string, object> >(decryptedJson) ?? new Dictionary <string, object>();

                if (!items.ContainsKey(key))
                {
                    return(false);
                }

                string propertyJson = JsonSerialiser.Serialise(items[key]);

                result = JsonSerialiser.Deserialise <T>(propertyJson);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public void Deserialise_EmptyEpisodeAirDate_SetsToNone()
        {
            var serialiser = new JsonSerialiser();

            var value = serialiser.Deserialise <TvDbEpisodeData>(@"{
                firstAired: """"
            }")
                        .FirstAired;

            value.IsNone.Should().BeTrue();
        }
Exemplo n.º 6
0
        public void Deserialise_ZeroAirsDayOfWeek_SetsToMonday()
        {
            var serialiser = new JsonSerialiser();

            var value = serialiser.Deserialise <TvDbSeriesData>(@"{
                airsDayOfWeek: 0
            }")
                        .AirsDayOfWeek;

            value.ValueUnsafe().Should().Be(AirDay.Monday);
        }
Exemplo n.º 7
0
        public void Deserialise_ValidAirsDayOfWeek_ReturnsSome()
        {
            var serialiser = new JsonSerialiser();

            var value = serialiser.Deserialise <TvDbSeriesData>(@"{
                airsDayOfWeek: ""Saturday""
            }")
                        .AirsDayOfWeek;

            value.ValueUnsafe().Should().Be(AirDay.Saturday);
        }
Exemplo n.º 8
0
        public void Deserialise_NullAirsDayOfWeek_SetsToNone()
        {
            var serialiser = new JsonSerialiser();

            var value = serialiser.Deserialise <TvDbSeriesData>(@"{
                airsDayOfWeek: null
            }")
                        .AirsDayOfWeek;

            value.IsNone.Should().BeTrue();
        }
Exemplo n.º 9
0
        public async Task <T> GetResponseContentModel <T>()
        {
            var httpContent = Response.Content;

            byte[] byteArr = await httpContent.ReadAsByteArrayAsync();

            var serialiser = new JsonSerialiser <T>(Encoding.UTF8);
            T   model      = serialiser.Deserialise(byteArr);

            return(model);
        }
        public List <GithubRepositoryInfo> GetRepositories(string access_token)
        {
            url  = "https://api.github.com/user/repos";
            data = string.Format("access_token={0}", access_token);
            string response = HttpService.HttpGet(url + "?" + data);

            try
            {
                JsonSerialiser jsonSerialiser = new JsonSerialiser();
                return(jsonSerialiser.Deserialise <List <GithubRepositoryInfo> >(response));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public GithubUserInfo GetUserInfo(string access_token)
        {
            url  = "https://api.github.com/user";
            data = string.Format("access_token={0}", access_token);
            string response = HttpService.HttpGet(url + "?" + data);

            try
            {
                JsonSerialiser jsonSerialiser = new JsonSerialiser();
                return(jsonSerialiser.Deserialise <GithubUserInfo>(response));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Reads an object under the specified key
        /// </summary>
        /// <typeparam name="T">The type of object to read</typeparam>
        /// <param name="key">The key this object was saved under</param>
        /// <returns>The object that was loaded</returns>
        public T Read <T>(string key)
        {
            if (!_items.ContainsKey(key))
            {
                throw new QuickSaveException("Key does not exists");
            }

            try
            {
                string propertyJson = JsonSerialiser.Serialise(_items[key]);

                return(JsonSerialiser.Deserialise <T>(propertyJson));
            }
            catch
            {
                throw new QuickSaveException("Unable to deserialise json");
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Attempts to save an object to a root under the specified key using the specified settings
        /// </summary>
        /// <typeparam name="T">The type of object to save</typeparam>
        /// <param name="root">The root this object will be saved under</param>
        /// <param name="key">The key this object will be saved under</param>
        /// <param name="value">The object to save</param>
        /// <param name="settings">Settings</param>
        /// <returns>Was the save successful</returns>
        public static bool TrySave <T>(string root, string key, T value, QuickSaveSettings settings)
        {
            try
            {
                string fileJson = FileAccess.LoadString(root, false);

                Dictionary <string, object> items = null;

                if (string.IsNullOrEmpty(fileJson))
                {
                    items = new Dictionary <string, object>();
                }
                else
                {
                    string decryptedJson = Cryptography.Decrypt(fileJson, settings.SecurityMode, settings.Password);

                    items = JsonSerialiser.Deserialise <Dictionary <string, object> >(decryptedJson) ?? new Dictionary <string, object>();

                    if (items.ContainsKey(key))
                    {
                        items.Remove(key);
                    }
                }

                items.Add(key, TypeHelper.ReplaceIfUnityType(value));

                string jsonToSave = JsonSerialiser.Serialise(items);

                string encryptedJson = Cryptography.Encrypt(jsonToSave, settings.SecurityMode, settings.Password);

                FileAccess.SaveString(root, false, encryptedJson);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Attempts to read an object under the specified key
        /// </summary>
        /// <typeparam name="T">The type of object to read</typeparam>
        /// <param name="key">The key this object was saved under</param>
        /// <param name="result">The object that was loaded</param>
        /// <returns>Was the read successful</returns>
        public bool TryRead <T>(string key, out T result)
        {
            result = default(T);

            if (!_items.ContainsKey(key))
            {
                return(false);
            }

            try
            {
                string propertyJson = JsonSerialiser.Serialise(_items[key]);

                result = JsonSerialiser.Deserialise <T>(propertyJson);

                return(true);
            }
            catch
            {
                return(false);
            }
        }