Пример #1
0
        private void ReadFromStream(BinaryReader reader, string assetName, out RuntimeCsvRepresentation csvOutput)
        {
            csvOutput = new FlatRedBall.IO.Csv.RuntimeCsvRepresentation();

            int numberOfRows = reader.ReadInt32();

            int numberOfColumns = reader.ReadInt32();

            csvOutput.Name = assetName;

            csvOutput.Headers = new CsvHeader[numberOfColumns];

            for (int i = 0; i < numberOfColumns; i++)
            {
                csvOutput.Headers[i] = new CsvHeader(reader.ReadString());
            }

            csvOutput.Records = new List <string[]>();

            for (int row = 0; row < numberOfRows; row++)
            {
                string[] record = new string[numberOfColumns];
                csvOutput.Records.Add(record);
                for (int column = 0; column < numberOfColumns; column++)
                {
                    record[column] = reader.ReadString();
                }
            }
        }
        private static void AddItemToRcr <T>(RuntimeCsvRepresentation rcrToReturn, List <FieldInfo> fieldList, List <PropertyInfo> propertyList, int totalLength, T item)
        {
            int numberOfRows = 1;

            // First determine the number of rows:
            foreach (FieldInfo field in fieldList)
            {
                object value = field.GetValue(item);

                if (value is IList)
                {
                    numberOfRows = System.Math.Max((value as IList).Count, numberOfRows);
                }
            }
            foreach (PropertyInfo property in propertyList)
            {
                object valueBeforeToString = property.GetValue(item, null);
                object value = null;
                if (valueBeforeToString != null)
                {
                    value = valueBeforeToString.ToString();
                }

                if (value is IList)
                {
                    numberOfRows = System.Math.Max((value as IList).Count, numberOfRows);
                }
            }

            for (int rowInEntry = 0; rowInEntry < numberOfRows; rowInEntry++)
            {
                // Now we write this entry
                string[] record = new string[totalLength];

                int currentIndex = 0;

                foreach (FieldInfo field in fieldList)
                {
                    object value = field.GetValue(item);

                    SetRecordAtIndex(rowInEntry, record, currentIndex, value);


                    currentIndex++;
                }

                foreach (PropertyInfo property in propertyList)
                {
                    object value = property.GetValue(item, null);
                    SetRecordAtIndex(rowInEntry, record, currentIndex, value);
                    currentIndex++;
                }


                rcrToReturn.Records.Add(record);
            }
        }
Пример #3
0
        public static void Serialize(RuntimeCsvRepresentation rcr, string fileName)
        {
            if (rcr == null)
            {
                throw new ArgumentNullException("rcr");
            }

            string toSave = rcr.GenerateCsvString(Delimiter);

            FileManager.SaveText(toSave, fileName);
        }
        public static RuntimeCsvRepresentation FromList<T>(IList<T> items)
        {
            RuntimeCsvRepresentation rcrToReturn = new RuntimeCsvRepresentation();

            Type itemType = null;
            
            itemType = typeof(T);



            List<CsvHeader> headers = new List<CsvHeader>();

            List<FieldInfo> fieldList;
            List<PropertyInfo> propertyList;

            GetSerializableFieldsAndTypes(itemType, out fieldList, out propertyList);


            foreach (FieldInfo field in fieldList)
            {
                CsvHeader header = new CsvHeader(field.Name);
                header.OriginalText = field.Name + " (" + TypeAsFriendlyString(field.FieldType) + ")";
                headers.Add(header);
            }

            foreach (PropertyInfo property in propertyList)
            {
                CsvHeader header = new CsvHeader(property.Name);
                header.OriginalText = property.Name + " (" + TypeAsFriendlyString(property.PropertyType) + ")";
                headers.Add(header);
            }

            rcrToReturn.Headers = headers.ToArray();
            rcrToReturn.Records = new List<string[]>();
            int totalLength = fieldList.Count + propertyList.Count;

            foreach (T item in items)
            {

                AddItemToRcr<T>(rcrToReturn, fieldList, propertyList, totalLength, item);
            }

            return rcrToReturn;


        }
Пример #5
0
        protected override TRead Read(ContentReader input, TRead existingInstance)
        {
            FlatRedBall.IO.Csv.RuntimeCsvRepresentation csv = null;
            string assetName = input.AssetName;

            //read encrypted/not-encrypted flag first
            bool isEncrypted = input.ReadBoolean();

#if !XBOX360
            if (isEncrypted)
            {
                string password  = FlatRedBallServices.EncryptionKey;
                byte[] saltValue = Encoding.UTF8.GetBytes(FlatRedBallServices.EncryptionSaltValue);

                if (password == null)
                {
                    throw new InvalidOperationException("FlatRedBallServices.EncryptionKey cannot be null, this encrypted CSV cannot be read.");
                }

                AesManaged   aes           = null;
                CryptoStream cryptoStream  = null;
                BinaryReader cryptoWrapper = null;

                try
                {
                    Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, saltValue);
                    aes     = new AesManaged();
                    aes.Key = deriveBytes.GetBytes(aes.KeySize / 8);
                    aes.IV  = deriveBytes.GetBytes(aes.BlockSize / 8);

                    cryptoStream  = new CryptoStream(input.BaseStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
                    cryptoWrapper = new BinaryReader(cryptoStream);
                    ReadFromStream(cryptoWrapper, assetName, out csv);
                }
                finally
                {
                    if (cryptoWrapper != null)
                    {
                        cryptoWrapper.Close();
                    }

                    if (cryptoStream != null)
                    {
                        cryptoStream.Close();
                    }

                    if (aes != null)
                    {
                        aes.Clear();
                    }
                }
            }
#elif XBOX360
            if (isEncrypted)
            {
                throw new NotSupportedException();
            }
#endif
            else
            {
                ReadFromStream(input, assetName, out csv);
            }

            return(csv);
        }
Пример #6
0
        public static void CsvDeserializeDictionary <KeyType, ValueType>(string fileName, Dictionary <KeyType, ValueType> dictionaryToPopulate, out RuntimeCsvRepresentation rcr)
        {
            rcr = CsvDeserializeToRuntime(fileName);

            rcr.CreateObjectDictionary <KeyType, ValueType>(dictionaryToPopulate, ContentManagerName);
        }
Пример #7
0
        public static void CsvDeserializeDictionary <KeyType, ValueType>(string fileName, Dictionary <KeyType, ValueType> dictionaryToPopulate)
        {
            RuntimeCsvRepresentation rcr = null;

            CsvDeserializeDictionary(fileName, dictionaryToPopulate, out rcr);
        }
Пример #8
0
        public static void CsvDeserializeList(Type typeOfElement, string fileName, IList listToPopulate)
        {
            RuntimeCsvRepresentation rcr = CsvDeserializeToRuntime(fileName);

            rcr.CreateObjectList(typeOfElement, listToPopulate, ContentManagerName);
        }