예제 #1
0
        public static void Save(ElementToStorage newElment, string filePath, string key, bool encrypted)
        {
            Dictionary <string, ElementToStorage> dict = null;

            AssetManager.CreateSubDirectoriesForPath(filePath);
            string JSONstring;

            try
            {
                if (encrypted)
                {
                    JSONstring = Encrypted.Decrypt(filePath, StorageManager.Key);
                }
                else
                {
                    JSONstring = File.ReadAllText(filePath);
                }
                dict = JsonConvert.DeserializeObject <Dictionary <string, ElementToStorage> >(JSONstring);
            }
            catch (Exception)
            {
            }

            if (dict == null)
            {
                dict = new Dictionary <string, ElementToStorage>();
            }

            dict.Update(key, newElment);
            JSONstring = JsonConvert.SerializeObject(dict);

            if (encrypted)
            {
                Encrypted.Encrypt(filePath, StorageManager.Key, JSONstring, out _);
            }
            else
            {
                File.WriteAllText(filePath, JSONstring);
            }
        }
예제 #2
0
        public static bool Load(string key, string filePath, object defaultValue, bool encrypted, out object result)
        {
            Dictionary <string, ElementToStorage> dict;

            AssetManager.CreateSubDirectoriesForPath(filePath);


            try
            {
                string JSONstring = "";
                if (encrypted)
                {
                    JSONstring = Encrypted.Decrypt(filePath, StorageManager.Key);
                }
                else
                {
                    JSONstring = File.ReadAllText(filePath);
                }
                dict = JsonConvert.DeserializeObject <Dictionary <string, ElementToStorage> >(JSONstring);
            }


            catch (Exception)
            {
                result = defaultValue;
                return(false);
            }


            if (dict.IsNotNullAndTryGetValue(key, out var element))
            {
                Type   type      = element.type;
                object obj       = element.value;
                string objString = obj.ToString();

                if (type == typeof(String))
                {
                    result = objString;
                }
                else if (type == typeof(Boolean))
                {
                    result = Boolean.Parse(objString);
                }
                else if (type == typeof(Single))
                {
                    result = float.Parse(objString);
                }
                else if (type == typeof(Double))
                {
                    result = double.Parse(objString);
                }
                else
                {
                    result = JsonConvert.DeserializeObject(objString, type);
                }

                return(true);
            }
            else
            {
                result = defaultValue;
                return(false);
            }
        }