Horizon – Blame information for rev 27
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
27 | office | 2 | using System.Security.Policy; |
3 | using System.Threading; |
||
1 | office | 4 | using System.Windows.Forms; |
27 | office | 5 | using System.Windows.Markup; |
1 | office | 6 | using Be.Windows.Forms; |
7 | using Horizon.Database; |
||
8 | using Horizon.Utilities; |
||
27 | office | 9 | using Serilog; |
1 | office | 10 | |
11 | namespace Horizon.Snapshots |
||
12 | { |
||
13 | public partial class HexViewForm : Form |
||
14 | { |
||
15 | #region Public Events & Delegates |
||
16 | |||
17 | public event EventHandler<SaveDataEventArgs> SaveData; |
||
18 | |||
19 | #endregion |
||
20 | |||
21 | #region Public Methods |
||
22 | |||
23 | public void UpdateData(byte[] data) |
||
24 | { |
||
25 | hexBox1.InvokeIfRequired(hexBox => |
||
26 | { |
||
27 | _originalData = (byte[])data.Clone(); |
||
28 | |||
29 | _dynamicByteProvider = new DynamicByteProvider(data); |
||
30 | hexBox.ByteProvider = _dynamicByteProvider; |
||
31 | hexBox.Refresh(); |
||
32 | }); |
||
33 | } |
||
34 | |||
35 | #endregion |
||
36 | |||
37 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
38 | |||
39 | private readonly SnapshotDatabase _snapshotDatabase; |
||
40 | |||
41 | private DynamicByteProvider _dynamicByteProvider; |
||
27 | office | 42 | private byte[] _originalData; |
1 | office | 43 | private string _hash; |
27 | office | 44 | private readonly CancellationToken _cancellationToken; |
1 | office | 45 | |
46 | #endregion |
||
47 | |||
48 | #region Constructors, Destructors and Finalizers |
||
49 | |||
50 | public HexViewForm() |
||
51 | { |
||
52 | InitializeComponent(); |
||
53 | } |
||
54 | |||
27 | office | 55 | public HexViewForm(string hash, SnapshotDatabase snapshotDatabase, CancellationToken cancellationToken) : this() |
1 | office | 56 | { |
57 | _hash = hash; |
||
58 | _snapshotDatabase = snapshotDatabase; |
||
59 | _snapshotDatabase.SnapshotDataUpdate += SnapshotDatabaseSnapshotDataUpdate; |
||
27 | office | 60 | _cancellationToken = cancellationToken; |
1 | office | 61 | } |
62 | |||
63 | /// <summary> |
||
64 | /// Clean up any resources being used. |
||
65 | /// </summary> |
||
66 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
67 | protected override void Dispose(bool disposing) |
||
68 | { |
||
69 | if (disposing && components != null) |
||
70 | { |
||
71 | components.Dispose(); |
||
72 | } |
||
73 | |||
74 | _snapshotDatabase.SnapshotDataUpdate -= SnapshotDatabaseSnapshotDataUpdate; |
||
75 | |||
76 | base.Dispose(disposing); |
||
77 | } |
||
78 | |||
79 | #endregion |
||
80 | |||
81 | #region Event Handlers |
||
27 | office | 82 | private async void HexViewForm_Load(object sender, EventArgs e) |
8 | office | 83 | { |
84 | Utilities.WindowState.FormTracker.Track(this); |
||
27 | office | 85 | |
86 | try |
||
87 | { |
||
88 | using var memoryStream = await _snapshotDatabase.RetrieveFileStreamAsync(_hash, _cancellationToken); |
||
89 | |||
90 | if (memoryStream == null) |
||
91 | { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | var data = memoryStream.ToArray(); |
||
96 | |||
97 | _originalData = (byte[])data.Clone(); |
||
98 | |||
99 | _dynamicByteProvider = new DynamicByteProvider(data); |
||
100 | hexBox1.ByteProvider = _dynamicByteProvider; |
||
101 | } |
||
102 | catch (Exception exception) |
||
103 | { |
||
104 | Log.Error(exception, "Could not retrieve snapshot data."); |
||
105 | } |
||
8 | office | 106 | } |
1 | office | 107 | private void SnapshotDatabaseSnapshotDataUpdate(object sender, SnapshotDataUpdateEventArgs e) |
108 | { |
||
109 | switch (e) |
||
110 | { |
||
111 | case SnapshotDataUpdateSuccessEventArgs snapshotDataUpdateSuccessEventArgs: |
||
112 | _hash = snapshotDataUpdateSuccessEventArgs.NewHash; |
||
113 | |||
114 | statusStrip1.InvokeIfRequired(statusStrip => |
||
115 | { |
||
116 | toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; |
||
117 | toolStripStatusLabel1.Text = "Data saved."; |
||
118 | }); |
||
119 | break; |
||
120 | case SnapshotDataUpdateFailureEventArgs _: |
||
121 | statusStrip1.InvokeIfRequired(statusStrip => |
||
122 | { |
||
123 | toolStripProgressBar1.Value = toolStripProgressBar1.Minimum; |
||
124 | toolStripStatusLabel1.Text = "Could not save data."; |
||
125 | }); |
||
126 | break; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | private void SaveToolStripMenuItem_Click(object sender, EventArgs e) |
||
131 | { |
||
132 | if (_dynamicByteProvider.HasChanges()) |
||
133 | { |
||
134 | _dynamicByteProvider.ApplyChanges(); |
||
135 | |||
136 | statusStrip1.InvokeIfRequired(statusStrip => |
||
137 | { |
||
138 | toolStripProgressBar1.Value = toolStripProgressBar1.Minimum; |
||
139 | toolStripStatusLabel1.Text = "Saving."; |
||
140 | }); |
||
141 | |||
142 | SaveData?.Invoke(this, new SaveDataEventArgs(_hash, _dynamicByteProvider.Bytes.ToArray())); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | private void ReloadToolStripMenuItem_Click(object sender, EventArgs e) |
||
147 | { |
||
148 | hexBox1.InvokeIfRequired(hexBox => |
||
149 | { |
||
150 | _dynamicByteProvider = new DynamicByteProvider(_originalData); |
||
151 | hexBox.ByteProvider = _dynamicByteProvider; |
||
152 | hexBox.Refresh(); |
||
153 | }); |
||
154 | } |
||
155 | |||
156 | #endregion |
||
8 | office | 157 | |
158 | |||
1 | office | 159 | } |
160 | } |