Example #1
0
        public static bool FromBinary(this IJSONSerializable target, byte[] bytes)
        {
            if (bytes.Length < sizeof(int))
            {
                return(false);
            }

            int checksum = bytes.ToInt32();


            int streamChecksum = bytes.CheckSum(sizeof(int));

            if (checksum != streamChecksum)
            {
                return(false);
            }

            Stream stream = new MemoryStream(bytes, sizeof(int), bytes.Length - sizeof(int));

            byte[] decompressed = stream.GZipDecompress();

            string message = Encoding.UTF8.GetString(decompressed);
            JToken jToken  = JToken.Parse(message);

            stream.Close();
            return(target.FromJSON(jToken));
        }
Example #2
0
        public static void DeserializeComponents(JSONArray components, GameObject obj)
        {
            foreach (JSONNode node in components.Childs)
            {
                string            clsName = node["_meta_cls"];
                Type              t       = Type.GetType("AU." + clsName);
                IJSONSerializable s       = obj.AddComponent(t) as IJSONSerializable;

                s.OnDeserialize(node);
            }
        }
Example #3
0
        public static byte[] ToBinary(this IJSONSerializable target)
        {
            string message = target.ToJSON().ToString();

            byte[] utf8String = Encoding.UTF8.GetBytes(message);
            byte[] compressed = utf8String.GZipCompress();

            int checksum = compressed.CheckSum();

            byte[] result = Utility.Combine(checksum.ToBytes(), compressed, true);
            return(result);
        }
    /// <summary>
    /// Saves an object from memory to a JSON file in storage
    /// </summary>
    public static bool SaveToStorage(string fileName, IJSONSerializable origin)
    {
        bool result = false;

        if (GameManager.Instance.Settings.SavegameEnabled)
        {
            JSONObject j = new JSONObject();
            origin.Serialize(j);

            if (GameManager.Instance.Settings.UsePlayerPref)
            {
                PlayerPrefs.SetString(processFileName(fileName), j.ToString());
                result = true;
            }
            else
            {
                safeSaveDataToFile(processFileName(fileName), j.ToString());
                result = true;
            }
        }

        return(result);
    }
    /// <summary>
    /// Reconstruct an object in memory from JSON data in storage
    /// </summary>
    public static bool LoadFromStorage(string fileName, IJSONSerializable destination)
    {
        bool result = false;

        if (GameManager.Instance.Settings.SavegameEnabled)
        {
            string data = null;
            if (GameManager.Instance.Settings.UsePlayerPref)
            {
                data = PlayerPrefs.GetString(processFileName(fileName));
            }
            else
            {
                data = safeLoadingDataFromFile(processFileName(fileName));
            }

            //transform the string into JSON and let the object
            //reconstruct itself from it
            JSONObject j = JSONObject.Parse(data);
            if (j != null)
            {
                destination.Deserialize(j);
            }

            //everything went ok
            result = true;
        }

        //if it didn't go well, load defaults
        if (!result)
        {
            destination.LoadDefaults();
        }

        return(result);
    }
Example #6
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="key">
 /// A <see cref="System.String"/>
 /// </param>
 /// <param name="json">
 /// A <see cref="IJSONSerializable"/>
 /// </param>
 public void Deserialize(string key, IJSONSerializable json)
 {
     Manager.Cd(JSONManager.Path.Relative, key);
     json.JSONDeserialize(this);
     Manager.CdBack();
 }
Example #7
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="key">
 /// A <see cref="System.String"/>
 /// </param>
 /// <param name="json">
 /// A <see cref="IJSONSerializable"/>
 /// </param>
 public void Serialize(string key, IJSONSerializable json)
 {
     Manager.SetToObject(key);
     Manager.Cd(JSONManager.Path.Relative, key);
     if (json != null)
         json.JSONSerialize(this);
     else
         SerializeNull(key);
     Manager.CdBack();
 }
Example #8
0
 public static string ToBinaryBase64(this IJSONSerializable target, bool isUrlSafe = true)
 {
     byte[] bytes = target.ToBinary();
     return(isUrlSafe ? Convert.ToBase64String(bytes).EncodeBase64ToUrlSafeBase64() : Convert.ToBase64String(bytes));
 }
Example #9
0
 public static bool FromBinaryBase64(this IJSONSerializable target, string base64, bool isUrlSafe = true)
 {
     byte[] bytes = Convert.FromBase64String(isUrlSafe ? base64.DecodeUrlSafeBase64ToBase64() : base64);
     return(target.FromBinary(bytes));
 }