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

FdsBlockFileHeader.cs - github.com/ClusterM/nes-containers.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a884a87c906d4fb53e20721eb8d3b0c856017af6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace com.clusterrr.Famicom.Containers
{
    /// <summary>
    /// File header FDS block (block type 3)
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Size = 16, Pack = 1)]
    public class FdsBlockFileHeader : IFdsBlock, IEquatable<FdsBlockFileHeader>
    {
        /// <summary>
        /// Kind of the file
        /// </summary>
        public enum Kind
        {
            /// <summary>
            /// PRG data
            /// </summary>
            Program = 0,
            /// <summary>
            /// CHR data
            /// </summary>
            Character = 1,
            /// <summary>
            /// Nametable data
            /// </summary>
            NameTable = 2
        }

        [MarshalAs(UnmanagedType.U1)]
        private readonly byte blockType = 3;
        /// <summary>
        /// Valid block type ID
        /// </summary>
        public byte ValidTypeID { get => 3; }
        /// <summary>
        /// True if block type ID is valid
        /// </summary>
        public bool IsValid { get => blockType == 3; }

        [MarshalAs(UnmanagedType.U1)]
        private byte fileNumber;
        /// <summary>
        /// File number
        /// </summary>
        public byte FileNumber { get => fileNumber; set => fileNumber = value; }

        [MarshalAs(UnmanagedType.U1)]
        private byte fileIndicateCode;
        /// <summary>
        /// File indicate code (ID specified at disk-read function call)
        /// </summary>
        public byte FileIndicateCode { get => fileIndicateCode; set => fileIndicateCode = value; }

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        private byte[] fileName = Encoding.ASCII.GetBytes("FILENAME");
        /// <summary>
        /// Filename
        /// </summary>
        public string FileName { get => Encoding.ASCII.GetString(fileName).Trim(new char[] { '\0', ' ' }); set => fileName = Encoding.ASCII.GetBytes(value.PadRight(8)).Take(8).ToArray(); }

        [MarshalAs(UnmanagedType.U2)]
        // the destination address when loading
        private ushort fileAddress;
        /// <summary>
        /// File address - the destination address when loading
        /// </summary>
        public ushort FileAddress { get => fileAddress; set => fileAddress = value; }

        [MarshalAs(UnmanagedType.U2)]
        private ushort fileSize;
        /// <summary>
        /// File size
        /// </summary>
        public ushort FileSize { get => fileSize; set => fileSize = value; }

        [MarshalAs(UnmanagedType.U1)]
        private byte fileKind;
        /// <summary>
        /// Kind of the file: program, character or nametable
        /// </summary>
        public Kind FileKind { get => (Kind)fileKind; set => fileKind = (byte)value; }

        /// <summary>
        /// Length of the block
        /// </summary>
        public uint Length { get => 16; }

        /// <summary>
        /// Create FdsBlockFileHeader object from raw data
        /// </summary>
        /// <param name="data">Data</param>
        /// <param name="offset">Offset</param>
        /// <returns></returns>
        /// <exception cref="InvalidDataException"></exception>
        public static FdsBlockFileHeader FromBytes(byte[] data, int offset = 0)
        {
            int rawsize = Marshal.SizeOf(typeof(FdsBlockFileHeader));
            if (rawsize > data.Length - offset)
            {
                if (rawsize <= data.Length - offset + 2)
                {
                    var newRawData = new byte[rawsize];
                    Array.Copy(data, offset, newRawData, 0, rawsize - 2);
                    data = newRawData;
                    offset = 0;
                }
                else
                {
                    throw new InvalidDataException("Not enough data to fill FdsFileHeaderBlock class. Array length from position: " + (data.Length - offset) + ", struct length: " + rawsize);
                }
            }
            IntPtr buffer = Marshal.AllocHGlobal(rawsize);
            Marshal.Copy(data, offset, buffer, rawsize);
            FdsBlockFileHeader retobj = (FdsBlockFileHeader)Marshal.PtrToStructure(buffer, typeof(FdsBlockFileHeader));
            Marshal.FreeHGlobal(buffer);
            return retobj;
        }

        /// <summary>
        /// Returns raw data
        /// </summary>
        /// <returns>Data</returns>
        public byte[] ToBytes()
        {
            int rawSize = Marshal.SizeOf(this);
            IntPtr buffer = Marshal.AllocHGlobal(rawSize);
            Marshal.StructureToPtr(this, buffer, false);
            byte[] data = new byte[rawSize];
            Marshal.Copy(buffer, data, 0, rawSize);
            Marshal.FreeHGlobal(buffer);
            return data;
        }

        /// <summary>
        /// String representation
        /// </summary>
        /// <returns>File name and file kind as string</returns>
        public override string ToString() => $"{FileName} ({FileKind})";

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