Esempio n. 1
0
        public void Set(object valueContainer)
        {
            unchecked
            {
                switch (type)
                {
                case EnumValueTypes.Bool:
                    value.asBool = Convert.ToByte(valueContainer) == 1 ? true : false;
                    break;

                case EnumValueTypes.Int8:
                    value.asInt8 = Convert.ToSByte(valueContainer);
                    break;

                case EnumValueTypes.UInt8:
                    value.asUInt8 = Convert.ToByte(valueContainer);
                    break;

                case EnumValueTypes.Int16:
                    value.asInt16 = Convert.ToInt16(valueContainer);
                    break;

                case EnumValueTypes.UInt16:
                    value.asUInt16 = Convert.ToUInt16(valueContainer);
                    break;

                case EnumValueTypes.Int32:
                    value.asInt32 = Convert.ToInt32(valueContainer);
                    break;

                case EnumValueTypes.UInt32:
                    value.asUInt32 = Convert.ToUInt32(valueContainer);
                    break;

                case EnumValueTypes.Int64:
                    value.asInt64 = Convert.ToInt64(valueContainer);
                    break;

                case EnumValueTypes.UInt64:
                    value.asUInt64 = Convert.ToUInt64(valueContainer);
                    break;

                case EnumValueTypes.Float:
                    value.asFloat = Convert.ToSingle(valueContainer);
                    break;

                case EnumValueTypes.Double:
                    value.asDouble = Convert.ToDouble(valueContainer);
                    break;

                case EnumValueTypes.String:
                {
                    value.asString = valueContainer switch
                    {
                        byte[] byteArray => byteArray,
                        string str => Encoding.UTF8.GetBytes(str),
                        _ => new byte[0]
                    };
                    break;
                }

                case EnumValueTypes.Array:
                    value.asArray = (AssetTypeArray)valueContainer;
                    break;

                case EnumValueTypes.ByteArray:
                {
                    value.asByteArray = valueContainer switch
                    {
                        AssetTypeByteArray byteArrayStruct => byteArrayStruct,
                        byte[]             byteArray => new AssetTypeByteArray(byteArray),
                        _ => new AssetTypeByteArray(new byte[0])
                    };
                    break;
                }
                }
            }
        }
        public AssetTypeValueField ReadType(AssetsFileReader reader, AssetTypeValueField valueField)
        {
            if (valueField.templateField.isArray)
            {
                if (valueField.templateField.childrenCount == 2)
                {
                    EnumValueTypes sizeType = valueField.templateField.children[0].valueType;
                    if (sizeType == EnumValueTypes.Int32 ||
                        sizeType == EnumValueTypes.UInt32)
                    {
                        if (valueField.templateField.valueType == EnumValueTypes.ByteArray)
                        {
                            valueField.childrenCount = 0;
                            valueField.children      = new AssetTypeValueField[0];
                            int    size = reader.ReadInt32();
                            byte[] data = reader.ReadBytes(size);
                            if (valueField.templateField.align)
                            {
                                reader.Align();
                            }
                            AssetTypeByteArray atba = new AssetTypeByteArray();
                            atba.size        = (uint)size;
                            atba.data        = data;
                            valueField.value = new AssetTypeValue(EnumValueTypes.ByteArray, atba);
                        }
                        else
                        {
                            valueField.childrenCount = reader.ReadInt32();
                            valueField.children      = new AssetTypeValueField[valueField.childrenCount];
                            for (int i = 0; i < valueField.childrenCount; i++)
                            {
                                valueField.children[i] = new AssetTypeValueField();
                                valueField.children[i].templateField = valueField.templateField.children[1];
                                valueField.children[i] = ReadType(reader, valueField.children[i]);
                            }
                            if (valueField.templateField.align)
                            {
                                reader.Align();
                            }
                            AssetTypeArray ata = new AssetTypeArray();
                            ata.size         = valueField.childrenCount;
                            valueField.value = new AssetTypeValue(EnumValueTypes.Array, ata);
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid array value type! Found an unexpected " + sizeType.ToString() + " type instead!");
                    }
                }
                else
                {
                    throw new Exception("Invalid array!");
                }
            }
            else
            {
                EnumValueTypes type = valueField.templateField.valueType;
                if (type != 0)
                {
                    valueField.value = new AssetTypeValue(type, null);
                }
                if (type == EnumValueTypes.String)
                {
                    int length = reader.ReadInt32();
                    valueField.value.Set(reader.ReadBytes(length));
                    reader.Align();
                }
                else
                {
                    valueField.childrenCount = valueField.templateField.childrenCount;
                    if (valueField.childrenCount == 0)
                    {
                        valueField.children = new AssetTypeValueField[0];
                        switch (valueField.templateField.valueType)
                        {
                        case EnumValueTypes.Int8:
                            valueField.value.Set(reader.ReadSByte());
                            if (valueField.templateField.align)
                            {
                                reader.Align();
                            }
                            break;

                        case EnumValueTypes.UInt8:
                        case EnumValueTypes.Bool:
                            valueField.value.Set(reader.ReadByte());
                            if (valueField.templateField.align)
                            {
                                reader.Align();
                            }
                            break;

                        case EnumValueTypes.Int16:
                            valueField.value.Set(reader.ReadInt16());
                            if (valueField.templateField.align)
                            {
                                reader.Align();
                            }
                            break;

                        case EnumValueTypes.UInt16:
                            valueField.value.Set(reader.ReadUInt16());
                            if (valueField.templateField.align)
                            {
                                reader.Align();
                            }
                            break;

                        case EnumValueTypes.Int32:
                            valueField.value.Set(reader.ReadInt32());
                            break;

                        case EnumValueTypes.UInt32:
                            valueField.value.Set(reader.ReadUInt32());
                            break;

                        case EnumValueTypes.Int64:
                            valueField.value.Set(reader.ReadInt64());
                            break;

                        case EnumValueTypes.UInt64:
                            valueField.value.Set(reader.ReadUInt64());
                            break;

                        case EnumValueTypes.Float:
                            valueField.value.Set(reader.ReadSingle());
                            break;

                        case EnumValueTypes.Double:
                            valueField.value.Set(reader.ReadDouble());
                            break;
                        }
                    }
                    else
                    {
                        valueField.children = new AssetTypeValueField[valueField.childrenCount];
                        for (int i = 0; i < valueField.childrenCount; i++)
                        {
                            valueField.children[i] = new AssetTypeValueField();
                            valueField.children[i].templateField = valueField.templateField.children[i];
                            valueField.children[i] = ReadType(reader, valueField.children[i]);
                        }
                        if (valueField.templateField.align)
                        {
                            reader.Align();
                        }
                    }
                }
            }
            return(valueField);
        }