Beispiel #1
0
        /// <summary>
        /// Adds <see cref="CPAttribute"/> with key provided.
        /// </summary>
        /// <param name="key">Key of the new <see cref="CPAttribute"/>.</param>
        public override void AddAttribute(uint key)
        {
            if (!Map.CarPartKeys.TryGetValue(key, out var type))
            {
                throw new MappingFailException($"Attribute of key type 0x{key:X8} is not a valid attribute");
            }

            CPAttribute attribute = type switch
            {
                CarPartAttribType.Boolean => new BoolAttribute(eBoolean.False),
                CarPartAttribType.Floating => new FloatAttribute((float)0),
                CarPartAttribType.String => new StringAttribute(String.Empty),
                CarPartAttribType.Key => new KeyAttribute(String.Empty),
                _ => new IntAttribute((int)0)
            };

            attribute.Key = key;
            this.Attributes.Add(attribute);
        }
Beispiel #2
0
        private CPAttribute[] ReadAttribs(BinaryReader br, BinaryReader str, int maxlen, Dictionary <uint, CustomCP> cpmap)
        {
            var size   = br.ReadInt32();
            var offset = br.BaseStream.Position;
            var result = new CPAttribute[size >> 3];             // set initial capacity

            int count = 0;

            while (count < maxlen && br.BaseStream.Position < offset + size)
            {
                var      key = br.ReadUInt32();
                CustomCP cp  = null;

                // If existing map does not have attribute key, it might be a custom key then
                if (!Map.CarPartKeys.TryGetValue(key, out var type))
                {
                    type = CarPartAttribType.Custom;
                    if (!cpmap.TryGetValue(key, out cp))
                    {
                        cp = new CustomCP(key);
                    }
                }

                result[count] = type switch
                {
                    CarPartAttribType.Boolean => new BoolAttribute(br, key),
                    CarPartAttribType.CarPartID => new PartIDAttribute(br, key),
                    CarPartAttribType.Floating => new FloatAttribute(br, key),
                    CarPartAttribType.String => new StringAttribute(br, str, key),
                    CarPartAttribType.TwoString => new TwoStringAttribute(br, str, key),
                    CarPartAttribType.Color => new ColorAttribute(br, key),
                    CarPartAttribType.Key => new KeyAttribute(br, key),
                    CarPartAttribType.ModelTable => new ModelTableAttribute(br, key),
                    CarPartAttribType.Custom => new CustomAttribute(br, str, cp),
                    _ => new IntAttribute(br, key),
                };

                ++count;
            }

            return(result);
        }