Exemplo n.º 1
0
        /// <summary>
        /// Load pre saved memory
        /// </summary>
        /// <param name="data">Memory data</param>
        /// <param name="Clear">Clear memory before load ?</param>
        public void Load(byte[] data, bool Clear = true)
        {
            byte[] decryptedData = MoonSecurity.AES_Decrypt(data);

            if (decryptedData == null || decryptedData.Length == 0)
            {
                return;
            }

            if (Clear)
            {
                memory.Clear();
            }

            MemoryStream mem = new MemoryStream(decryptedData);

            BinaryFormatter formatter = new BinaryFormatter();

            Dictionary <string, object> dic = (Dictionary <string, object>)formatter.Deserialize(mem);

            mem.Close();

            string[] keys = new string[dic.Keys.Count];

            dic.Keys.CopyTo(keys, 0);

            for (int i = 0; i < keys.Length; i++)
            {
                object currentValue;

                if (dic.TryGetValue(keys[i], out currentValue))
                {
                    System.Type etype = currentValue.GetType();

                    string ekey = keys[i];

                    if (etype == typeof(sColor))
                    {
                        Color col = (sColor)currentValue;
                        SetValue(ekey, col);
                    }
                    else if (etype == typeof(sVector))
                    {
                        sVector svec = (sVector)currentValue;
                        switch (svec.Type)
                        {
                        case sVector.VectorType.Vector2:

                            Vector2 v2 = svec;

                            SetValue(ekey, v2);

                            break;

                        case sVector.VectorType.Vector3:

                            Vector3 v3 = svec;

                            SetValue(ekey, v3);

                            break;

                        case sVector.VectorType.Vector4:

                            Vector4 v4 = svec;

                            SetValue(ekey, v4);

                            break;
                        }
                    }
                    else if (etype == typeof(sLayerMask))
                    {
                        LayerMask mask = (sLayerMask)currentValue;
                        SetValue(ekey, mask);
                    }
                    else
                    {
                        SetValue(ekey, currentValue);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Save the memory for later use.
        /// Note: it will not save GameObjects.
        /// </summary>
        /// <returns>Byte array memory data</returns>
        public byte[] Save()
        {
            Dictionary <string, object> MemoryData = new Dictionary <string, object>();

            string[] keys = new string[memory.Keys.Count];

            memory.Keys.CopyTo(keys, 0);

            for (int i = 0; i < keys.Length; i++)
            {
                string currentKey = keys[i];

                object currentVal = memory[currentKey];

                if (currentVal == null)
                {
                    continue;
                }
                else
                {
                    System.Type currtype = currentVal.GetType();

                    if (currtype == typeof(Color) || currtype == typeof(Color32))
                    {
                        Color col = (Color)currentVal;
                        MemoryData.Add(currentKey, new sColor(col.r, col.g, col.b, col.a));
                    }
                    else if (currtype == typeof(Vector2))
                    {
                        Vector2 v2 = (Vector2)currentVal;
                        MemoryData.Add(currentKey, new sVector(v2.x, v2.y));
                    }
                    else if (currtype == typeof(Vector3))
                    {
                        Vector3 v3 = (Vector3)currentVal;
                        MemoryData.Add(currentKey, new sVector(v3.x, v3.y, v3.z));
                    }
                    else if (currtype == typeof(Vector4))
                    {
                        Vector4 v4 = (Vector4)currentVal;
                        MemoryData.Add(currentKey, new sVector(v4.x, v4.y, v4.z, v4.w));
                    }
                    else if (currtype == typeof(LayerMask))
                    {
                        LayerMask mask = (LayerMask)currentVal;
                        MemoryData.Add(currentKey, new sLayerMask()
                        {
                            Value = mask.value
                        });
                    }
                    else if (currtype.BaseType == typeof(Object))
                    {
                        continue;
                    }
                    else
                    {
                        object[] serdata = currentVal.GetType().GetCustomAttributes(typeof(System.SerializableAttribute), true);
                        if (serdata != null && serdata.Length > 0)
                        {
                            MemoryData.Add(currentKey, currentVal);
                        }
                    }
                }
            }

            MemoryStream mem = new MemoryStream();

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(mem, MemoryData);

            byte[] encryptedData = MoonSecurity.AES_Encrypt(mem.ToArray());

            mem.Close();

            return(encryptedData);
        }