blob: 8ec31b44bad3bfb06b59d9ad3827184df6faa9ad (
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
|
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 = new byte[0];
/// <summary>
/// File data
/// </summary>
public IEnumerable<byte> Data
{
get => Array.AsReadOnly(data);
set => data = value.ToArray();
}
/// <summary>
/// Set by dumper. True when checksum is ok
/// </summary>
public bool CrcOk { get; set; } = true;
/// <summary>
/// Set by dumper. True when "end of head" flag was meet during dumping
/// </summary>
public bool EndOfHeadMeet { get; set; } = false;
/// <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>
/// Return raw data
/// </summary>
/// <returns>Data</returns>
public byte[] ToBytes()
{
var result = new List<byte>
{
blockType
};
result.AddRange(Data);
return result.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());
}
}
}
|