Example #1
0
            /// <summary>
            /// Creates a new row based on the given paramdef with default values.
            /// </summary>
            public Row(long id, string name, PARAMDEF paramdef)
            {
                ID   = id;
                Name = name;

                var cells = new Cell[paramdef.Fields.Count];

                for (int i = 0; i < paramdef.Fields.Count; i++)
                {
                    PARAMDEF.Field field = paramdef.Fields[i];
                    object         value = ParamUtil.CastDefaultValue(field);
                    cells[i] = new Cell(field, value);
                }
                Cells = cells;
            }
Example #2
0
        public static object CastDefaultValue(PARAMDEF.Field field)
        {
            switch (field.DisplayType)
            {
                case DefType.s8: return (sbyte)field.Default;
                case DefType.u8: return (byte)field.Default;
                case DefType.s16: return (short)field.Default;
                case DefType.u16: return (ushort)field.Default;
                case DefType.s32: return (int)field.Default;
                case DefType.u32: return (uint)field.Default;
                case DefType.f32: return field.Default;
                case DefType.fixstr: return "";
                case DefType.fixstrW: return "";
                case DefType.dummy8:
                    if (field.BitSize == -1)
                        return new byte[field.ArrayLength];
                    else
                        return (byte)field.Default;

                default:
                    throw new NotImplementedException($"Default not implemented for type {field.DisplayType}");
            }
        }
Example #3
0
 internal Cell(PARAMDEF.Field def, object value)
 {
     Def   = def;
     Value = value;
 }
Example #4
0
            internal void WriteCells(BinaryWriterEx bw, byte format2D, int index)
            {
                if ((format2D & 0x7F) < 4)
                {
                    bw.FillUInt32($"RowOffset{index}", (uint)bw.Position);
                }
                else
                {
                    bw.FillInt64($"RowOffset{index}", bw.Position);
                }

                int bitOffset = -1;

                PARAMDEF.DefType bitType  = PARAMDEF.DefType.u8;
                uint             bitValue = 0;

                for (int i = 0; i < Cells.Count; i++)
                {
                    Cell             cell  = Cells[i];
                    object           value = cell.Value;
                    PARAMDEF.Field   field = cell.Def;
                    PARAMDEF.DefType type  = field.DisplayType;

                    if (type == PARAMDEF.DefType.s8)
                    {
                        bw.WriteSByte((sbyte)value);
                    }
                    else if (type == PARAMDEF.DefType.s16)
                    {
                        bw.WriteInt16((short)value);
                    }
                    else if (type == PARAMDEF.DefType.s32)
                    {
                        bw.WriteInt32((int)value);
                    }
                    else if (type == PARAMDEF.DefType.f32)
                    {
                        bw.WriteSingle((float)value);
                    }
                    else if (type == PARAMDEF.DefType.fixstr)
                    {
                        bw.WriteFixStr((string)value, field.ArrayLength);
                    }
                    else if (type == PARAMDEF.DefType.fixstrW)
                    {
                        bw.WriteFixStrW((string)value, field.ArrayLength * 2);
                    }
                    else if (ParamUtil.IsBitType(type))
                    {
                        if (field.BitSize == -1)
                        {
                            if (type == PARAMDEF.DefType.u8)
                            {
                                bw.WriteByte((byte)value);
                            }
                            else if (type == PARAMDEF.DefType.u16)
                            {
                                bw.WriteUInt16((ushort)value);
                            }
                            else if (type == PARAMDEF.DefType.u32)
                            {
                                bw.WriteUInt32((uint)value);
                            }
                            else if (type == PARAMDEF.DefType.dummy8)
                            {
                                bw.WriteBytes((byte[])value);
                            }
                        }
                        else
                        {
                            if (bitOffset == -1)
                            {
                                bitOffset = 0;
                                bitType   = type == PARAMDEF.DefType.dummy8 ? PARAMDEF.DefType.u8 : type;
                                bitValue  = 0;
                            }

                            uint shifted = 0;
                            if (bitType == PARAMDEF.DefType.u8)
                            {
                                shifted = (byte)value;
                            }
                            else if (bitType == PARAMDEF.DefType.u16)
                            {
                                shifted = (ushort)value;
                            }
                            else if (bitType == PARAMDEF.DefType.u32)
                            {
                                shifted = (uint)value;
                            }
                            // Shift left first to clear any out-of-range bits
                            shifted    = shifted << (32 - field.BitSize) >> (32 - field.BitSize - bitOffset);
                            bitValue  |= shifted;
                            bitOffset += field.BitSize;

                            bool write = false;
                            if (i == Cells.Count - 1)
                            {
                                write = true;
                            }
                            else
                            {
                                PARAMDEF.Field   nextField = Cells[i + 1].Def;
                                PARAMDEF.DefType nextType  = nextField.DisplayType;
                                int bitLimit = ParamUtil.GetBitLimit(bitType);
                                if (!ParamUtil.IsBitType(nextType) || nextField.BitSize == -1 || bitOffset + nextField.BitSize > bitLimit ||
                                    (nextType == PARAMDEF.DefType.dummy8 ? PARAMDEF.DefType.u8 : nextType) != bitType)
                                {
                                    write = true;
                                }
                            }

                            if (write)
                            {
                                bitOffset = -1;
                                if (bitType == PARAMDEF.DefType.u8)
                                {
                                    bw.WriteByte((byte)bitValue);
                                }
                                else if (bitType == PARAMDEF.DefType.u16)
                                {
                                    bw.WriteUInt16((ushort)bitValue);
                                }
                                else if (bitType == PARAMDEF.DefType.u32)
                                {
                                    bw.WriteUInt32(bitValue);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException($"Unsupported field type: {type}");
                    }
                }
            }
Example #5
0
            internal void ReadCells(BinaryReaderEx br, PARAMDEF paramdef)
            {
                // In case someone decides to add new rows before applying the paramdef (please don't do that)
                if (DataOffset == 0)
                {
                    return;
                }

                Def = paramdef;

                br.Position = DataOffset;
                var cells = new Cell[paramdef.Fields.Count];

                int bitOffset = -1;

                PARAMDEF.DefType bitType  = PARAMDEF.DefType.u8;
                uint             bitValue = 0;

                for (int i = 0; i < paramdef.Fields.Count; i++)
                {
                    PARAMDEF.Field   field = paramdef.Fields[i];
                    object           value = null;
                    PARAMDEF.DefType type  = field.DisplayType;

                    if (type == PARAMDEF.DefType.s8)
                    {
                        value = br.ReadSByte();
                    }
                    else if (type == PARAMDEF.DefType.s16)
                    {
                        value = br.ReadInt16();
                    }
                    else if (type == PARAMDEF.DefType.s32)
                    {
                        value = br.ReadInt32();
                    }
                    else if (type == PARAMDEF.DefType.f32)
                    {
                        value = br.ReadSingle();
                    }
                    else if (type == PARAMDEF.DefType.fixstr)
                    {
                        value = br.ReadFixStr(field.ArrayLength);
                    }
                    else if (type == PARAMDEF.DefType.fixstrW)
                    {
                        value = br.ReadFixStrW(field.ArrayLength * 2);
                    }
                    else if (ParamUtil.IsBitType(type))
                    {
                        if (field.BitSize == -1)
                        {
                            if (type == PARAMDEF.DefType.u8)
                            {
                                value = br.ReadByte();
                            }
                            else if (type == PARAMDEF.DefType.u16)
                            {
                                value = br.ReadUInt16();
                            }
                            else if (type == PARAMDEF.DefType.u32)
                            {
                                value = br.ReadUInt32();
                            }
                            else if (type == PARAMDEF.DefType.dummy8)
                            {
                                value = br.ReadBytes(field.ArrayLength);
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException($"Unsupported field type: {type}");
                    }

                    if (value != null)
                    {
                        bitOffset = -1;
                    }
                    else
                    {
                        PARAMDEF.DefType newBitType = type == PARAMDEF.DefType.dummy8 ? PARAMDEF.DefType.u8 : type;
                        int bitLimit = ParamUtil.GetBitLimit(newBitType);

                        if (field.BitSize == 0)
                        {
                            throw new NotImplementedException($"Bit size 0 is not supported.");
                        }
                        if (field.BitSize > bitLimit)
                        {
                            throw new InvalidDataException($"Bit size {field.BitSize} is too large to fit in type {newBitType}.");
                        }

                        if (bitOffset == -1 || newBitType != bitType || bitOffset + field.BitSize > bitLimit)
                        {
                            bitOffset = 0;
                            bitType   = newBitType;
                            if (bitType == PARAMDEF.DefType.u8)
                            {
                                bitValue = br.ReadByte();
                            }
                            else if (bitType == PARAMDEF.DefType.u16)
                            {
                                bitValue = br.ReadUInt16();
                            }
                            else if (bitType == PARAMDEF.DefType.u32)
                            {
                                bitValue = br.ReadUInt32();
                            }
                        }

                        uint shifted = bitValue << (32 - field.BitSize - bitOffset) >> (32 - field.BitSize);
                        bitOffset += field.BitSize;
                        if (bitType == PARAMDEF.DefType.u8)
                        {
                            value = (byte)shifted;
                        }
                        else if (bitType == PARAMDEF.DefType.u16)
                        {
                            value = (ushort)shifted;
                        }
                        else if (bitType == PARAMDEF.DefType.u32)
                        {
                            value = shifted;
                        }
                    }

                    cells[i] = new Cell(field, value);
                }
                Cells = cells;
            }
Example #6
0
            /// <summary>
            /// Converts the layout to a paramdef with the given type, and all child enums to paramtdfs.
            /// </summary>
            public PARAMDEF ToParamdef(string paramType, out List <PARAMTDF> paramtdfs)
            {
                paramtdfs = new List <PARAMTDF>(Enums.Count);
                foreach (string enumName in Enums.Keys)
                {
                    paramtdfs.Add(Enums[enumName].ToParamtdf(enumName));
                }

                var def = new PARAMDEF {
                    ParamType = paramType, Unicode = true, FormatVersion = 201
                };

                foreach (Entry entry in this)
                {
                    PARAMDEF.DefType fieldType;
                    switch (entry.Type)
                    {
                    case CellType.dummy8: fieldType = PARAMDEF.DefType.dummy8; break;

                    case CellType.b8:
                    case CellType.u8:
                    case CellType.x8: fieldType = PARAMDEF.DefType.u8; break;

                    case CellType.s8: fieldType = PARAMDEF.DefType.s8; break;

                    case CellType.b16:
                    case CellType.u16:
                    case CellType.x16: fieldType = PARAMDEF.DefType.u16; break;

                    case CellType.s16: fieldType = PARAMDEF.DefType.s16; break;

                    case CellType.b32:
                    case CellType.u32:
                    case CellType.x32: fieldType = PARAMDEF.DefType.u32; break;

                    case CellType.s32: fieldType = PARAMDEF.DefType.s32; break;

                    case CellType.f32: fieldType = PARAMDEF.DefType.f32; break;

                    case CellType.fixstr: fieldType = PARAMDEF.DefType.fixstr; break;

                    case CellType.fixstrW: fieldType = PARAMDEF.DefType.fixstrW; break;

                    default:
                        throw new NotImplementedException($"DefType not specified for CellType {entry.Type}.");
                    }

                    var field = new PARAMDEF.Field(fieldType, entry.Name);
                    field.Description = entry.Description;
                    if (entry.Enum != null)
                    {
                        field.InternalType = entry.Enum;
                    }

                    if (entry.Type == CellType.s8)
                    {
                        field.Default = (sbyte)entry.Default;
                    }
                    else if (entry.Type == CellType.u8 || entry.Type == CellType.x8)
                    {
                        field.Default = (byte)entry.Default;
                    }
                    else if (entry.Type == CellType.s16)
                    {
                        field.Default = (short)entry.Default;
                    }
                    else if (entry.Type == CellType.u16 || entry.Type == CellType.x16)
                    {
                        field.Default = (ushort)entry.Default;
                    }
                    else if (entry.Type == CellType.s32)
                    {
                        field.Default = (int)entry.Default;
                    }
                    else if (entry.Type == CellType.u32 || entry.Type == CellType.x32)
                    {
                        field.Default = (uint)entry.Default;
                    }
                    else if (entry.Type == CellType.dummy8 || entry.Type == CellType.fixstr)
                    {
                        field.ArrayLength = entry.Size;
                    }
                    else if (entry.Type == CellType.fixstrW)
                    {
                        field.ArrayLength = entry.Size / 2;
                    }
                    else if (entry.Type == CellType.b8 || entry.Type == CellType.b16 || entry.Type == CellType.b32)
                    {
                        field.Default = (bool)entry.Default ? 1 : 0;
                        field.BitSize = 1;
                    }

                    def.Fields.Add(field);
                }
                return(def);
            }