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: dc11ed438a6a54fa4dfb4fe75632905dcca5a320 (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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace com.clusterrr.Famicom.Containers
{
    [StructLayout(LayoutKind.Sequential, Size = 18, Pack = 1, CharSet = CharSet.Ansi)]
    public class FdsBlockFileHeader : IFdsBlock
    {
        public enum Kind
        {
            Program = 0, 
            Character = 1,
            NameTable = 2
        }

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

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

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

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        private char[] fileName;
        public string FileName { get => new string(fileName).Trim(new char[] { '\0', ' ' }); set => fileName = value.PadRight(0).ToCharArray(0, value.Length > 8 ? 8 : value.Length); }

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

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

        [MarshalAs(UnmanagedType.U1)]
        private byte fileKind;
        public Kind FileKind { get => (Kind)fileKind; set => fileKind = (byte)value; }

        [MarshalAs(UnmanagedType.U1)]
        private bool crcOk = true;
        /// <summary>
        /// Set by dumper. True when checksum is ok
        /// </summary>
        public bool CrcOk { get => crcOk; set => crcOk = value; }

        [MarshalAs(UnmanagedType.U1)]
        private bool endOfHeadMeet = false;
        /// <summary>
        /// Set by dumper. True when "end of head" flag was meet during dumping
        /// </summary>
        public bool EndOfHeadMeet { get => endOfHeadMeet; set => endOfHeadMeet = value; }

        public uint Length => 16;

        public static FdsBlockFileHeader FromBytes(byte[] rawData, int position = 0)
        {
            int rawsize = Marshal.SizeOf(typeof(FdsBlockFileHeader));
            if (rawsize > rawData.Length - position)
            {
                if (rawsize <= rawData.Length - position + 2)
                {
                    var newRawData = new byte[rawsize];
                    Array.Copy(rawData, position, newRawData, 0, rawsize - 2);
                    rawData = newRawData;
                    position = 0;
                }
                else
                {
                    throw new ArgumentException("Not enough data to fill FdsFileHeaderBlock class. Array length from position: " + (rawData.Length - position) + ", Struct length: " + rawsize);
                }
            }
            IntPtr buffer = Marshal.AllocHGlobal(rawsize);
            Marshal.Copy(rawData, position, buffer, rawsize);
            FdsBlockFileHeader retobj = (FdsBlockFileHeader)Marshal.PtrToStructure(buffer, typeof(FdsBlockFileHeader));
            Marshal.FreeHGlobal(buffer);
            return retobj;
        }

        public byte[] ToBytes()
        {
            int rawSize = Marshal.SizeOf(this);
            IntPtr buffer = Marshal.AllocHGlobal(rawSize);
            Marshal.StructureToPtr(this, buffer, false);
            byte[] rawDatas = new byte[rawSize - 2];
            Marshal.Copy(buffer, rawDatas, 0, rawSize - 2);
            Marshal.FreeHGlobal(buffer);
            return rawDatas;
        }

        public override string ToString() => $"{FileName} ({FileKind})";
    }
}