Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FdsBlockFileData.cs - github.com/ClusterM/nes-containers.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b329d4b099a158dfa19b99d0efff0eddd2805ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;

namespace com.clusterrr.Famicom.Containers
{
    /// <summary>
    /// File data FDS block (block type 4)
    /// </summary>
    public class FdsBlockFileData : IFdsBlock, IEquatable<FdsBlockFileData>
    {
        private byte blockType = 4;
        /// <summary>
        /// Valid block type ID
        /// </summary>
        public byte ValidTypeID { get => 4; }
        /// <summary>
        /// True if block type ID is valid
        /// </summary>
        public bool IsValid { get => blockType == ValidTypeID; }

        private byte[] data = Array.Empty<byte>();
        /// <summary>
        /// File data
        /// </summary>
        public IEnumerable<byte> Data
        {
            get => Array.AsReadOnly(data);
            set => data = value.ToArray();
        }

        /// <summary>
        /// Length of the block
        /// </summary>
        public uint Length => (uint)(data.Length + 1);

        /// <summary>
        /// Create FdsBlockFileData object from raw data
        /// </summary>
        /// <param name="data">Data</param>
        /// <param name="offset">Offset</param>
        /// <param name="length">Length</param>
        /// <returns>FdsBlockFileData object</returns>
        public static FdsBlockFileData FromBytes(byte[] data, int offset = 0, int length = -1)
        {
            var retobj = new FdsBlockFileData
            {
                blockType = data[offset],
                data = new byte[length < 0 ? data.Length - offset - 1 : length - 1]
            };
            Array.Copy(data, offset + 1, retobj.data, 0, retobj.data.Length);
            return retobj;
        }

        /// <summary>
        /// Returns raw data
        /// </summary>
        /// <returns>Data</returns>
        public byte[] ToBytes() => Enumerable.Concat<byte>(new[] { blockType }, data).ToArray();

        /// <summary>
        /// String representation
        /// </summary>
        /// <returns>Number of bytes as string</returns>
        public override string ToString() => $"{data.Length} bytes";

        /// <summary>
        /// Equality comparer
        /// </summary>
        /// <param name="other">Other FdsBlockFileData object</param>
        /// <returns>True if objects are equal</returns>
        public bool Equals(FdsBlockFileData other)
        {
            return Enumerable.SequenceEqual(this.ToBytes(), other.ToBytes());
        }
    }
}