Beispiel #1
0
        public static object DeserializeBinary(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                Log.W("DeserializeBinary Without Valid Path.");
                return(null);
            }

            FileInfo fileInfo = new FileInfo(path);

            if (!fileInfo.Exists)
            {
                Log.W("DeserializeBinary File Not Exit.");
                return(null);
            }

            using (FileStream fs = fileInfo.OpenRead()) {
                BinaryFormatter bf   = new BinaryFormatter();
                object          data = bf.Deserialize(fs);
                // fs.Close();
                if (data != null)
                {
                    byte[] txEncrypt = EncryptHelp.AESDecrypt((Byte[])data, SaveDefaultData.EncryptKey, SaveDefaultData.EncryptValue);
                    object str       = Encoding.UTF8.GetString(txEncrypt);
                    return(str);
                    //return data;
                }
            }

            Log.W("DeserializeBinary Failed:" + path);
            return(null);
        }
Beispiel #2
0
        public static object DeserializeXML <T>(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                Log.W("DeserializeBinary Without Valid Path.");
                return(null);
            }

            FileInfo fileInfo = new FileInfo(path);

            using (FileStream fs = fileInfo.OpenRead()) {
                XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
                object        data          = xmlserializer.Deserialize(fs);

                if (data != null)
                {
                    byte[] txEncrypt = EncryptHelp.AESDecrypt((Byte[])data, SaveDefaultData.EncryptKey, SaveDefaultData.EncryptValue);
                    object str       = Encoding.UTF8.GetString(txEncrypt);
                    return(str);
                    //return data;
                }
            }

            Log.W("DeserializeBinary Failed:" + path);
            return(null);
        }
Beispiel #3
0
    private object ReadValue(SaveImplementType type, EnumSaveTypeKey keyType)
    {
        switch (type)
        {
        case SaveImplementType.ImplementByte:
            Stream stream = FileMgr.Instance.OpenReadStream(this.saveSetting.path);
            if (stream != null)
            {
                stream.Close();
                var data = SerializeHelper.DeserializeBinary(this.saveSetting.path);
                if (data != null)
                {
                    switch (keyType)
                    {
                    case EnumSaveTypeKey.SaveInt:
                        return(int.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveLong:
                        return(long.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveFolat:
                        return(float.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveString:
                        return(data.ToString());

                    case EnumSaveTypeKey.SaveDouble:
                        return(double.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveBool:
                        return(bool.Parse(data.ToString()));

                    case EnumSaveTypeKey.Savebyte:
                        return(byte.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveShort:
                        return(short.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveUint:
                        return(uint.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveUlong:
                        return(ulong.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveUshort:
                        return(ushort.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveChar:
                        return(char.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveVector2:
                        return(StringExtention.GetValue <Vector2>(data.ConverToString()));

                    case EnumSaveTypeKey.SaveVector3:
                        return(StringExtention.GetValue <Vector3>(data.ConverToString()));

                    case EnumSaveTypeKey.SaveVector4:
                        return(StringExtention.GetValue <Vector4>(data.ConverToString()));

                    case EnumSaveTypeKey.SaveQuaternion:
                        return(StringExtention.GetValue <Quaternion>(data.ConverToString()));

                    case EnumSaveTypeKey.SaveColor:
                        return(StringExtention.GetValue <Color>(data.ConverToString()));

                    case EnumSaveTypeKey.SaveEnum:
                        return(StringExtention.GetValue(data.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveList:
                        return(StringExtention.GetValue(data.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveDictionary:
                        return(StringExtention.GetValue(data.ConverToString(), (Type)saveSetting.curObject));;

                    case EnumSaveTypeKey.SaveDateTime:
                        return(DateTime.Parse(data.ToString()));

                    case EnumSaveTypeKey.SaveArray:
                        return(StringExtention.GetValue(data.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveHashtable:
                        return(StringExtention.GetValue(data.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveArrayList:
                        return(StringExtention.GetValue(data.ConverToString(), (Type)saveSetting.curObject));
                    }
                    return(null);
                }
            }
            break;

        case SaveImplementType.ImplementJson:
            if (!string.IsNullOrEmpty(saveSetting.path))
            {
                var data = SerializeHelper.LoadJson <JsonTestFloat>(this.saveSetting.path);
                if (data != null)
                {
                    byte[] txEncrypt = EncryptHelp.AESDecrypt(data.SaveValue, SaveDefaultData.EncryptKey, SaveDefaultData.EncryptValue);
                    string str       = Encoding.UTF8.GetString(txEncrypt);
                    switch (keyType)
                    {
                    case EnumSaveTypeKey.SaveInt:
                        return(int.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveLong:
                        return(long.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveFolat:
                        return(float.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveString:
                        return(str.ToString());

                    case EnumSaveTypeKey.SaveDouble:
                        return(double.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveBool:
                        return(bool.Parse(str.ToString()));

                    case EnumSaveTypeKey.Savebyte:
                        return(byte.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveShort:
                        return(short.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveUint:
                        return(uint.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveUlong:
                        return(ulong.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveUshort:
                        return(ushort.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveChar:
                        return(char.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveVector2:
                        return(StringExtention.GetValue <Vector2>(str.ConverToString()));

                    case EnumSaveTypeKey.SaveVector3:
                        return(StringExtention.GetValue <Vector3>(str.ConverToString()));

                    case EnumSaveTypeKey.SaveVector4:
                        return(StringExtention.GetValue <Vector4>(str.ConverToString()));

                    case EnumSaveTypeKey.SaveQuaternion:
                        return(StringExtention.GetValue <Quaternion>(str.ConverToString()));

                    case EnumSaveTypeKey.SaveColor:
                        return(StringExtention.GetValue <Color>(str.ConverToString()));

                    case EnumSaveTypeKey.SaveEnum:
                        return(StringExtention.GetValue(str.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveList:
                        return(StringExtention.GetValue(str.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveDictionary:
                        return(StringExtention.GetValue(str.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveDateTime:
                        return(DateTime.Parse(str.ToString()));

                    case EnumSaveTypeKey.SaveArray:
                        return(StringExtention.GetValue(str.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveHashtable:
                        return(StringExtention.GetValue(str.ConverToString(), (Type)saveSetting.curObject));

                    case EnumSaveTypeKey.SaveArrayList:
                        return(StringExtention.GetValue(str.ConverToString(), (Type)saveSetting.curObject));
                    }
                    return(null);
                }
            }
            break;

        case SaveImplementType.ImplementProto:
            if (!string.IsNullOrEmpty(saveSetting.path))
            {
                var data = SerializeHelper.LoadProtoBuff <ProtoBufSave>(saveSetting.path);
                if (data != null)
                {
                    switch (keyType)
                    {
                    case EnumSaveTypeKey.SaveInt:
                        return(data.SaveValue);

                    //case EnumSaveTypeKey.SaveFolat:
                    //    return (float)data.SaveValue;
                    case EnumSaveTypeKey.SaveString:
                        return(data.SaveValue.ToString());

                    case EnumSaveTypeKey.SaveBool:
                        return(bool.Parse(data.SaveValue.ToString()));
                    }
                    return(null);
                }
            }
            break;

        case SaveImplementType.ImplementXML:
            if (!string.IsNullOrEmpty(saveSetting.path))
            {
                switch (keyType)
                {
                case EnumSaveTypeKey.SaveInt:
                    var dataInt = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataInt == null)
                    {
                        return(null);
                    }
                    return(int.Parse(dataInt.ToString()));

                case EnumSaveTypeKey.SaveLong:
                    var dataLong = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataLong == null)
                    {
                        return(null);
                    }
                    return(long.Parse(dataLong.ToString()));

                case EnumSaveTypeKey.SaveFolat:
                    var dataFolat = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataFolat == null)
                    {
                        return(null);
                    }
                    return(float.Parse(dataFolat.ToString()));

                case EnumSaveTypeKey.SaveString:
                    var dataString = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataString == null)
                    {
                        return(null);
                    }
                    return(dataString.ToString());

                case EnumSaveTypeKey.SaveDouble:
                    var dataDouble = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataDouble == null)
                    {
                        return(null);
                    }
                    return(double.Parse(dataDouble.ToString()));

                case EnumSaveTypeKey.SaveBool:
                    var dataBool = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataBool == null)
                    {
                        return(null);
                    }
                    return(bool.Parse(dataBool.ToString()));

                case EnumSaveTypeKey.Savebyte:
                    var dataByte = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataByte == null)
                    {
                        return(null);
                    }
                    return(byte.Parse(dataByte.ToString()));

                case EnumSaveTypeKey.SaveShort:
                    var dataShort = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataShort == null)
                    {
                        return(null);
                    }
                    return(short.Parse(dataShort.ToString()));

                case EnumSaveTypeKey.SaveUint:
                    var dataUint = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataUint == null)
                    {
                        return(null);
                    }
                    return(uint.Parse(dataUint.ToString()));

                case EnumSaveTypeKey.SaveUlong:
                    var dataUlong = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataUlong == null)
                    {
                        return(dataUlong);
                    }
                    return(ulong.Parse(dataUlong.ToString()));

                case EnumSaveTypeKey.SaveUshort:
                    var dataUshort = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataUshort == null)
                    {
                        return(dataUshort);
                    }
                    return(ushort.Parse(dataUshort.ToString()));

                case EnumSaveTypeKey.SaveChar:
                    var dataChar = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataChar == null)
                    {
                        return(dataChar);
                    }
                    return(char.Parse(dataChar.ToString()));

                case EnumSaveTypeKey.SaveVector2:
                    var dataVector2 = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataVector2 == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue <Vector2>(dataVector2.ConverToString()));

                case EnumSaveTypeKey.SaveVector3:
                    var dataVector3 = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataVector3 == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue <Vector3>(dataVector3.ConverToString()));

                case EnumSaveTypeKey.SaveVector4:
                    var dataVector4 = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataVector4 == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue <Vector4>(dataVector4.ConverToString()));

                case EnumSaveTypeKey.SaveQuaternion:
                    var dataQuaternion = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataQuaternion == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue <Quaternion>(dataQuaternion.ConverToString()));

                case EnumSaveTypeKey.SaveColor:
                    var dataColor = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataColor == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue <Color>(dataColor.ConverToString()));

                case EnumSaveTypeKey.SaveEnum:
                    var dataEnum = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataEnum == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue(dataEnum.ConverToString(), (Type)saveSetting.curObject));

                case EnumSaveTypeKey.SaveList:

                    var dataList = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataList == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue(dataList.ConverToString(), (Type)saveSetting.curObject));

                case EnumSaveTypeKey.SaveDictionary:
                    var dataDictionary = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataDictionary == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue(dataDictionary.ConverToString(), (Type)saveSetting.curObject));

                case EnumSaveTypeKey.SaveDateTime:
                    var dataDateTime = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataDateTime == null)
                    {
                        return(null);
                    }
                    return(DateTime.Parse(dataDateTime.ToString()));

                case EnumSaveTypeKey.SaveArray:
                    var dataArray = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataArray == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue(dataArray.ConverToString(), (Type)saveSetting.curObject));

                case EnumSaveTypeKey.SaveHashtable:
                    var dataHashtable = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataHashtable == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue(dataHashtable.ConverToString(), (Type)saveSetting.curObject));

                case EnumSaveTypeKey.SaveArrayList:
                    var dataArrayList = SerializeHelper.DeserializeXML <byte[]>(saveSetting.path);
                    if (dataArrayList == null)
                    {
                        return(null);
                    }
                    return(StringExtention.GetValue(dataArrayList.ConverToString(), (Type)saveSetting.curObject));
                }
            }
            break;
        }
        return(null);
    }