-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMarkers.cs
134 lines (119 loc) · 3.7 KB
/
Markers.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace hexer
{
[Serializable()]
public class DataMarker : IComparable<DataMarker>
{
public DataType Type
{
get;
set;
}
public int Address
{
get;
set;
}
public int NumBytes
{
get;
set;
}
public string Note
{
get;
set;
}
public DataMarker(int addr, DataType dt)
{
Address = addr;
Type = dt;
NumBytes = dt.NumBytes;
Note = "Unnamed";
}
// For serialization
private DataMarker() { }
public int CompareTo(DataMarker other)
{
return Address.CompareTo(other.Address);
}
}
public class MarkerRepository
{
public static readonly MarkerRepository Instance = new MarkerRepository();
private MarkerRepository() { }
private SortedList<int, DataMarker> markers = new SortedList<int, DataMarker>();
private List<DataMarker> serializedMarkers = new List<DataMarker>();
public void AddMarker(int addr, DataType dt)
{
markers.Add(addr, new DataMarker(addr, dt));
}
internal void RemoveMarker(int selectedAddress)
{
markers.Remove(selectedAddress);
}
public DataMarker GetMarker(int addr)
{
if(markers.ContainsKey(addr)) {
return markers[addr];
}
return null;
}
public void EditMarker(int selectedAddress, HexView hexview)
{
new MarkerEditor(GetMarker(selectedAddress), hexview).Show();
}
public bool isMarker(int addr)
{
return markers.ContainsKey(addr);
}
public DataMarker GetMarkerCovering(int addr)
{
// This is terrible. Should really use something like an interval tree for marker storage.
// On the other hand, it's probably easily fast enough. Doesn't make it any less terrible though.
const int MAX_MARKER_BYTES = 16;
for(int i = 0; i < MAX_MARKER_BYTES; ++i) {
if(markers.ContainsKey(addr)) {
var m = markers[addr];
if(m.NumBytes > i) return m;
else return null;
}
addr -= 8;
}
return null;
}
// --------------------------------------------------------------------- File Handling
#region File Handling
private string fileName = "";
public bool HasFileName()
{
return fileName.Length > 0;
}
public void SaveToFile(string fn = "")
{
if(fn == "") fn = fileName;
var serializer = new XmlSerializer(typeof(List<DataMarker>));
using(TextWriter writer = new StreamWriter(fn)) {
serializedMarkers = markers.Values.ToList();
serializer.Serialize(writer, serializedMarkers);
}
fileName = fn;
}
public void LoadFromFile(string fn)
{
var serializer = new XmlSerializer(typeof(List<DataMarker>));
using(TextReader reader = new StreamReader(fn)) {
serializedMarkers = serializer.Deserialize(reader) as List<DataMarker>;
foreach(DataMarker marker in serializedMarkers) markers.Add(marker.Address, marker);
}
fileName = fn;
}
#endregion
}
}