Beispiel #1
0
        private UtfTableImage PrepareTable <T>(T[] tableRows) where T : UtfRowBase
        {
            var tableName  = GetTableName(tableRows);
            var tableImage = new UtfTableImage(tableName, Alignment);

            foreach (var tableRow in tableRows)
            {
                var targetMembers = SerializationHelper.GetSearchTargetFieldsAndProperties(tableRow);
                var tableImageRow = new List <UtfFieldImage>();
                tableImage.Rows.Add(tableImageRow);
                // TODO: Save misc data first, then the tables.
                foreach (var member in targetMembers)
                {
                    var fieldInfo      = member.FieldInfo;
                    var fieldType      = fieldInfo.FieldType;
                    var fieldAttribute = member.FieldAttribute;
                    CheckFieldType(fieldType);
                    var fieldValue = member.FieldInfo.GetValue(tableRow);
                    var fieldImage = new UtfFieldImage {
                        Order   = fieldAttribute.Order,
                        Name    = string.IsNullOrEmpty(fieldAttribute.FieldName) ? fieldInfo.Name : fieldAttribute.FieldName,
                        Storage = fieldAttribute.Storage,
                    };
                    // Empty tables are treated as empty data.
                    if (IsTypeRowList(fieldType) && fieldValue != null && ((UtfRowBase[])fieldValue).Length > 0)
                    {
                        var tableBytes = GetTableBytes((UtfRowBase[])fieldValue);
                        fieldImage.SetValue(tableBytes);
                        fieldImage.IsTable = true;
                    }
                    else if (fieldType == typeof(byte[]) && member.ArchiveAttribute != null)
                    {
                        var files = new List <byte[]> {
                            (byte[])fieldValue
                        };
                        var archiveBytes = SerializationHelper.GetAfs2ArchiveBytes(files.AsReadOnly(), Alignment);
                        fieldImage.SetValue(archiveBytes);
                        fieldImage.IsTable = true;
                    }
                    else
                    {
                        if (fieldValue == null)
                        {
                            fieldImage.SetNullValue(MapFromRawType(fieldType));
                        }
                        else
                        {
                            fieldImage.SetValue(fieldValue);
                        }
                    }
                    tableImageRow.Add(fieldImage);
                }
            }
            return(tableImage);
        }
Beispiel #2
0
        private UtfTableImage PrepareTable(UtfRowBase[] tableRows)
        {
            if (tableRows.Length < 1)
            {
                throw new ArgumentException("There should be at least one row in a table.", nameof(tableRows));
            }

            var tableRowTypes = tableRows.Select(row => row.GetType()).ToArray();

            if (tableRowTypes.Length > 1)
            {
                for (var i = 1; i < tableRowTypes.Length; ++i)
                {
                    if (tableRowTypes[i] != tableRowTypes[i - 1])
                    {
                        throw new ArgumentException("All rows must have the same CLR type.");
                    }
                }
            }

            var tableName  = GetTableName(tableRows, tableRowTypes[0]);
            var tableImage = new UtfTableImage(tableName, Alignment);

            foreach (var tableRow in tableRows)
            {
                var targetMembers = SerializationHelper.GetSearchTargetFieldsAndProperties(tableRow);
                var tableImageRow = new List <UtfFieldImage>();

                tableImage.Rows.Add(tableImageRow);

                // TODO: Save misc data first, then the tables.
                foreach (var member in targetMembers)
                {
                    var fieldInfo      = member.FieldInfo;
                    var fieldType      = fieldInfo.FieldType;
                    var fieldAttribute = member.FieldAttribute;

                    CheckFieldType(fieldType);

                    var fieldValue = member.FieldInfo.GetValue(tableRow);

                    var fieldImage = new UtfFieldImage {
                        Order   = fieldAttribute.Order,
                        Name    = string.IsNullOrEmpty(fieldAttribute.FieldName) ? fieldInfo.Name : fieldAttribute.FieldName,
                        Storage = fieldAttribute.Storage,
                    };

                    // Empty tables are treated as empty data.
                    if (IsTypeRowList(fieldType) && fieldValue != null && ((UtfRowBase[])fieldValue).Length > 0)
                    {
                        var tableBytes = GetTableBytes((UtfRowBase[])fieldValue);

                        fieldImage.SetValue(tableBytes);
                        fieldImage.IsTable = true;
                    }
                    else if (fieldType == typeof(byte[]) && member.ArchiveAttribute != null)
                    {
                        var files = new List <byte[]> {
                            (byte[])fieldValue
                        };

                        var archiveBytes = SerializationHelper.GetAfs2ArchiveBytes(files.AsReadOnly(), Alignment);

                        fieldImage.SetValue(archiveBytes);
                        fieldImage.IsTable = true;
                    }
                    else
                    {
                        if (fieldValue == null)
                        {
                            fieldImage.SetNullValue(MapUtfColumeTypeFromRawType(fieldType));
                        }
                        else
                        {
                            fieldImage.SetValue(fieldValue);
                        }
                    }

                    tableImageRow.Add(fieldImage);
                }
            }
            return(tableImage);
        }